1010
1111class 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
0 commit comments