1818
1919class GravTNTSearch
2020{
21+ /** @var TNTSearch */
2122 public $ tnt ;
23+ /** @var array */
2224 protected $ options ;
25+ /** @var string[] */
2326 protected $ bool_characters = ['- ' , '( ' , ') ' , 'or ' ];
27+ /** @var string */
2428 protected $ index = 'grav.index ' ;
29+ /** @var false|string */
2530 protected $ language ;
2631
2732 /**
@@ -78,7 +83,7 @@ public function __construct($options = [])
7883 }
7984
8085 /**
81- * @param $query
86+ * @param string $query
8287 * @return object|string
8388 * @throws IndexNotFoundException
8489 */
@@ -97,6 +102,7 @@ public function search($query)
97102 $ limit = (int )$ this ->options ['limit ' ];
98103 $ type = $ type ?? $ this ->options ['search_type ' ];
99104
105+ // TODO: Multiword parameter has been removed from $tnt->search(), please check if this works
100106 $ multiword = null ;
101107 if (isset ($ this ->options ['phrases ' ]) && $ this ->options ['phrases ' ]) {
102108 if (strlen ($ query ) > 2 ) {
@@ -111,7 +117,7 @@ public function search($query)
111117
112118 switch ($ type ) {
113119 case 'basic ' :
114- $ results = $ this ->tnt ->search ($ query , $ limit, $ multiword );
120+ $ results = $ this ->tnt ->search ($ query , $ limit );
115121 break ;
116122 case 'boolean ' :
117123 $ results = $ this ->tnt ->searchBoolean ($ query , $ limit );
@@ -170,7 +176,7 @@ protected function processResults($res, $query)
170176 }
171177
172178 if ($ this ->options ['json ' ]) {
173- return json_encode ($ data , JSON_PRETTY_PRINT );
179+ return json_encode ($ data , JSON_PRETTY_PRINT ) ?: '' ;
174180 }
175181
176182 return $ data ;
@@ -193,14 +199,16 @@ public static function getCleanContent($page)
193199 $ twig = $ grav ['twig ' ];
194200 $ header = $ page ->header ();
195201
202+ // @phpstan-ignore-next-line
196203 if (isset ($ header ->tntsearch ['template ' ])) {
197204 $ processed_page = $ twig ->processTemplate ($ header ->tntsearch ['template ' ] . '.html.twig ' , ['page ' => $ page ]);
198205 $ content = $ processed_page ;
199206 } else {
200207 $ content = $ page ->content ();
201208 }
202209
203- $ content = preg_replace ('/[ \t]+/ ' , ' ' , preg_replace ('/\s*$^\s*/m ' , "\n" , strip_tags ($ content )));
210+ $ content = strip_tags ($ content );
211+ $ content = preg_replace ('/[ \t]+/ ' , ' ' , preg_replace ('/\s*$^\s*/m ' , "\n" , $ content ));
204212
205213 // Restore active page in Grav.
206214 unset($ grav ['page ' ]);
@@ -209,6 +217,9 @@ public static function getCleanContent($page)
209217 return $ content ;
210218 }
211219
220+ /**
221+ * @return void
222+ */
212223 public function createIndex ()
213224 {
214225 $ this ->tnt ->setDatabaseHandle (new GravConnector );
@@ -222,13 +233,18 @@ public function createIndex()
222233 $ indexer ->run ();
223234 }
224235
236+ /**
237+ * @return void
238+ * @throws IndexNotFoundException
239+ */
225240 public function selectIndex ()
226241 {
227242 $ this ->tnt ->selectIndex ($ this ->index );
228243 }
229244
230245 /**
231246 * @param object $object
247+ * @return void
232248 */
233249 public function deleteIndex ($ object )
234250 {
@@ -251,6 +267,7 @@ public function deleteIndex($object)
251267
252268 /**
253269 * @param object $object
270+ * @return void
254271 */
255272 public function updateIndex ($ object )
256273 {
@@ -282,7 +299,8 @@ public function updateIndex($object)
282299 /** @var Collection $collection */
283300 $ collection = $ apage ->collection ($ filter , false );
284301
285- if (array_key_exists ($ object ->path (), $ collection ->toArray ())) {
302+ $ path = $ object ->path ();
303+ if ($ path && array_key_exists ($ path , $ collection ->toArray ())) {
286304 $ fields = $ this ->indexPageData ($ object );
287305 $ document = (array ) $ fields ;
288306
0 commit comments