Skip to content

Commit 3beb762

Browse files
authored
feat!:Version 0.21.0 (#234)
* fix: solr accents (#232) * fix: remove double encoding * fix: exclude from search * fix(search): retain skipped items * fix(solr): re-index failed document * style: formatting * feat: add function to abstract class. * chore: remove SolrDate * feat: empty core * fix: syntax problem * feat: DateTime for SearchItem * style: formatting
1 parent 3fc8ed5 commit 3beb762

File tree

7 files changed

+93
-75
lines changed

7 files changed

+93
-75
lines changed

src/Http/Controllers/Pages/PageController.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,4 @@ public function searchSubitems(): array
171171
{
172172
return [];
173173
}
174-
175-
public function solrDate($pageId)
176-
{
177-
$this->pageId = $pageId;
178-
$date = $this->menuItem()->updated_at;
179-
if ($date) {
180-
return $date->toIso8601String();
181-
} else {
182-
return '2023-01-01T10:00:00Z';
183-
}
184-
}
185174
}

src/Models/CmsSearch.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@ class CmsSearch extends BaseModel
4040
{
4141
protected $table = 'cms_search';
4242

43-
private array $status = ['PENDING', 'ADDED', 'SKIPPED', 'UPDATED', 'NOT_INDEXABLE', 'NOT_FOUND'];
43+
protected $casts = [
44+
'updated_at' => 'datetime',
45+
'created_at' => 'datetime',
46+
'deleted_at' => 'datetime',
47+
];
48+
// private array $status = ['PENDING', 'ADDED', 'SKIPPED', 'UPDATED', 'NOT_INDEXABLE', 'NOT_FOUND'];
4449

4550
private static array $skipStatus = ['NOT_INDEXABLE', 'NOT_FOUND'];
4651

src/Models/Indexes/SolrIndex.php

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use NotFound\Framework\Mail\Indexer\FileIndexError;
77
use NotFound\Framework\Mail\Indexer\QueryError;
88
use NotFound\Framework\Models\BaseModel;
9-
use NotFound\Framework\Models\CmsSearch;
109
use NotFound\Framework\Services\Indexer\SearchItem;
1110

1211
/**
@@ -71,25 +70,20 @@ public function __construct($debug = false)
7170

7271
public function emptyCore()
7372
{
74-
$searchItems = CmsSearch::all();
75-
if (count($searchItems) == 0) {
76-
$curl = $this->solrHandler();
77-
$url = sprintf('%s/update/?wt=%s&commit=true*', $this->getSolrBaseUrl(), $this->wt);
78-
curl_setopt($curl, CURLOPT_URL, $url);
79-
$payload = ['delete' => ['query' => '*:*']];
80-
81-
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($payload));
82-
$result = curl_exec($curl);
73+
$curl = $this->solrHandler();
74+
$url = sprintf('%s/update/?wt=%s&commit=true*', $this->getSolrBaseUrl(), $this->wt);
75+
curl_setopt($curl, CURLOPT_URL, $url);
76+
$payload = ['delete' => ['query' => '*:*']];
8377

84-
$json = json_decode($result);
78+
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($payload));
79+
$result = curl_exec($curl);
8580

86-
if (! $json || ! isset($json->responseHeader) || $json->responseHeader->status !== 0) {
87-
$this->mailQueryError($url, $result);
81+
$json = json_decode($result);
8882

89-
return false;
90-
}
83+
if (! $json || ! isset($json->responseHeader) || $json->responseHeader->status !== 0) {
84+
$this->mailQueryError($url, $result);
9185

92-
return true;
86+
return false;
9387
}
9488

9589
return true;

src/Services/Indexer/AbstractIndexService.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace NotFound\Framework\Services\Indexer;
44

5+
use DateTime;
56
use NotFound\Framework\Models\CmsSearch;
67

78
abstract class AbstractIndexService
@@ -12,14 +13,20 @@ abstract class AbstractIndexService
1213

1314
public ?string $domain;
1415

15-
abstract public function __construct($debug = false);
16+
protected bool $debug;
17+
18+
protected bool $fresh;
19+
20+
abstract public function __construct(bool $debug = false, bool $fresh = false);
1621

1722
abstract public function startUpdate(): bool;
1823

1924
abstract public function finishUpdate(): object;
2025

2126
abstract public function upsertItem(SearchItem $searchItem): object;
2227

28+
abstract public function retainItem(string $url): void;
29+
2330
abstract public function checkConnection(): bool;
2431

2532
public function clean(): bool
@@ -29,10 +36,10 @@ public function clean(): bool
2936
return true;
3037
}
3138

32-
public function urlNeedsUpdate(string $url, $updated): bool
39+
public function urlNeedsUpdate(string $url, ?DateTime $updated): bool
3340
{
3441
$searchItem = CmsSearch::whereUrl($this->siteUrl($url))->first();
35-
if ($searchItem && ($searchItem->updated_at !== null && $searchItem->updated_at->timestamp > $updated)) {
42+
if ($searchItem && ($searchItem->updated_at !== null && $searchItem->updated_at >= $updated)) {
3643
CmsSearch::whereUrl($url)->update(['search_status' => 'SKIPPED']);
3744

3845
return false;

src/Services/Indexer/IndexBuilderService.php

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,17 @@
1010

1111
class IndexBuilderService
1212
{
13-
private bool $debug;
14-
15-
private bool $fresh;
16-
1713
private $locales;
1814

1915
private $domain;
2016

2117
private $sitemapFile;
2218

19+
private $padding = 140;
20+
2321
private AbstractIndexService $searchServer;
2422

25-
public function __construct($debug = false, $fresh = false)
23+
public function __construct(private bool $debug = false, private bool $fresh = false)
2624
{
2725
$serverType = config('indexer.engine');
2826
$this->debug = $debug;
@@ -32,14 +30,14 @@ public function __construct($debug = false, $fresh = false)
3230
$this->domain = rtrim(env('APP_URL', ''), '/');
3331
switch ($serverType) {
3432
case 'solr':
35-
$this->searchServer = new SolrIndexService($this->debug);
33+
$this->searchServer = new SolrIndexService($this->debug, $this->fresh);
3634
break;
3735
default:
3836
exit('Unknown search index type');
3937
}
4038
}
4139

42-
public function run()
40+
public function run(): void
4341
{
4442
if (! $this->searchServer->checkConnection()) {
4543
$this->writeDebug("\n\n Error connecting to search server! \n\n");
@@ -64,7 +62,7 @@ public function run()
6462
$this->sitemapFile = fopen($sitemapFileName, 'w') or exit('Could not open sitemap file for writing');
6563
} else {
6664
$this->sitemapFile = false;
67-
$this->writeDebug(" skipping sitemap\n");
65+
$this->writeDebug(" ⏭️ Skipping sitemap\n");
6866
}
6967

7068
$siteId = $site->id;
@@ -74,41 +72,42 @@ public function run()
7472
$this->searchServer->languageId = 1;
7573

7674
// insert all pages, starting from the root
77-
$this->writeDebug(" INDEXING PAGES\n ==============\n");
75+
$this->writeDebug(" INDEXING PAGES\n ==============\n");
7876
$this->indexChildPages($site->root);
7977

8078
if ($this->sitemapFile) {
8179
fclose($this->sitemapFile);
8280
}
83-
$finish = $this->searchServer->finishUpdate();
84-
85-
$this->writeDebug($finish->message);
8681
}
8782
} else {
8883
$this->writeDebug("No sites to index\n");
84+
85+
return;
8986
}
87+
$finish = $this->searchServer->finishUpdate();
88+
89+
$this->writeDebug($finish->message);
9090
}
9191

9292
private function indexChildPages($parentId)
9393
{
9494
$childPages = Menu::whereParent_id($parentId)->whereEnabled(1)->get();
9595
foreach ($childPages as $page) {
96-
$this->writeDebug(sprintf(" * Page \e[1m%s\e[0m (id: %d)", $page->url, $page->id));
96+
$this->writeDebug("\n");
97+
$this->writeDebug(sprintf('%s (id: %d)', $page->url, $page->id), true, '┣━┓ 📂 Page ');
9798

9899
if (! isset($page->template->id)) {
99-
$this->writeDebug(" skipping, no template found\n");
100+
$this->writeDebug(": ❌ Fail, skipping, no template found\n");
100101

101102
continue;
102103
}
103104

104105
$menu = Menu::whereId($page->id)->firstOrFail();
105106

106107
if (! isset($page->template->properties->searchable) || $page->template->properties->searchable == 0) {
107-
$this->writeDebug(" skipping, template not searchable\n");
108-
108+
$this->writeDebug(": ⏭️ Skipping, template excluded from search\n");
109109
} elseif (isset($page->properties->excludeFromSearch) && $page->properties->excludeFromSearch == true) {
110-
$this->writeDebug(" skipping, page not searchable\n");
111-
110+
$this->writeDebug(": ⏭️ Skipping, page excluded from search\n");
112111
} else {
113112

114113
foreach ($this->locales as $lang) {
@@ -133,7 +132,7 @@ private function updatePage($menu, $lang)
133132
$url = $menu->getLocalizedPath();
134133
}
135134

136-
if ($this->searchServer->urlNeedsUpdate($url, strtotime($menu->updated_at))) {
135+
if ($this->searchServer->urlNeedsUpdate($url, $menu->updated_at)) {
137136
$this->writeDebug(': update needed: ');
138137

139138
$searchText = '';
@@ -149,7 +148,7 @@ private function updatePage($menu, $lang)
149148

150149
$c = null;
151150
$priority = 1;
152-
$solrDate = '';
151+
153152
if (class_exists($className)) {
154153
$c = new $className();
155154
if (method_exists($className, 'customSearchValues')) {
@@ -158,30 +157,31 @@ private function updatePage($menu, $lang)
158157
if (method_exists($className, 'searchPriority')) {
159158
$priority = $c->searchPriority();
160159
}
161-
if (method_exists($className, 'solrDate')) {
162-
$solrDate = $c->solrDate($menu->id);
163-
}
164160
}
165161
$searchText = rtrim($searchText, ', ');
166162
if (! empty($title) && ! empty($searchText)) {
167163

168164
$searchItem = new SearchItem($url, $title);
169-
$searchItem->setContent($searchText)->setLanguage($lang->url)->setPriority($priority)->setPublicationDate(new DateTime($menu->updated_at));
165+
$searchItem->setContent($searchText)
166+
->setLanguage($lang->url)
167+
->setPriority($priority)
168+
->setPublicationDate(new DateTime($menu->updated_at));
170169
foreach ($customValues as $key => $value) {
171170
$searchItem->setCustomValue($key, $value);
172171
}
173172
$result = $this->searchServer->upsertItem($searchItem);
174173

175174
if ($result->errorCode == 0) {
176-
$this->writeDebug(" success\n");
175+
$this->writeDebug(": ✅ Success\n");
177176
} else {
178-
$this->writeDebug(" FAILED\n");
177+
$this->writeDebug(": ❌ FAILED\n");
179178
}
180179
} else {
181-
$this->writeDebug(" empty page or title\n");
180+
$this->writeDebug(": ❌ Empty page or title\n");
182181
}
183182
} else {
184-
$this->writeDebug(": Does not need updating\n");
183+
$this->writeDebug(": ✅ Page does not need updating\n");
184+
$this->searchServer->retainItem($url);
185185
}
186186

187187
if ($this->sitemapFile) {
@@ -235,9 +235,9 @@ private function updateSubitems($class, $lang)
235235
$success = false;
236236
if ((new \ReflectionClass($searchItem))->getShortName() == 'SearchItem') {
237237
$url = $searchItem->url();
238-
$this->writeDebug($url);
238+
$this->writeDebug($url, true, '┃ ┣━ 📄 ');
239239

240-
if ($this->searchServer->urlNeedsUpdate($url, strtotime($searchItem->lastUpdated()))) {
240+
if ($this->searchServer->urlNeedsUpdate($url, $searchItem->lastUpdated())) {
241241

242242
$searchItem->setLanguage($lang->url);
243243
$success = $this->searchServer->upsertItem($searchItem);
@@ -251,12 +251,13 @@ private function updateSubitems($class, $lang)
251251
}
252252

253253
if ($success->errorCode == 0) {
254-
$this->writeDebug(" success\n");
254+
$this->writeDebug(": ✅ Success\n");
255255
} else {
256256
$this->writeDebug($success->message);
257257
}
258258
} else {
259-
$this->writeDebug(": Does not need updating\n");
259+
$this->writeDebug(": ✅ Item does not need updating\n");
260+
$success = $this->searchServer->retainItem($url);
260261
}
261262
} else {
262263
dd('Please use the SearchItem class');
@@ -275,10 +276,15 @@ private function createFolderIfNotExists($fullFilePath)
275276
}
276277
}
277278

278-
private function writeDebug($text)
279+
private function writeDebug($text, $padding = false, $prefix = '')
279280
{
280281
if ($this->debug) {
281-
printf($text);
282+
283+
if ($padding) {
284+
$text = substr($text, 0, $this->padding - strlen($prefix));
285+
$text = str_pad($text, $this->padding - strlen($prefix), ' ');
286+
}
287+
printf("\e[1m".$prefix."\e[0m".$text);
282288
}
283289
}
284290

src/Services/Indexer/SearchItem.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DateTime;
66
use NotFound\Framework\Models\Lang;
7+
use NotFound\Framework\View\Components\Forms\Fields\Date;
78

89
final class SearchItem
910
{
@@ -155,9 +156,9 @@ public function publicationDate(): ?string
155156
* This is when the content was last updated
156157
* This is used to determine if the content needs to be re-indexed
157158
*/
158-
public function lastUpdated(): ?string
159+
public function lastUpdated(): ?DateTime
159160
{
160-
return $this->toDateString($this->lastUpdated ?? $this->publicationDate);
161+
return $this->lastUpdated ?? $this->publicationDate;
161162
}
162163

163164
public function customValues(): ?array

0 commit comments

Comments
 (0)