]> BookStack Code Mirror - bookstack/blobdiff - app/Entities/Tools/SearchIndex.php
Added test for logical-theme-system command registration
[bookstack] / app / Entities / Tools / SearchIndex.php
index 05de341f96089085a370a42a4d7a185e097944b4..79139ec24062433ed136bebd77cd57fd130bfd30 100644 (file)
@@ -13,6 +13,12 @@ use Illuminate\Support\Collection;
 
 class SearchIndex
 {
+    /**
+     * A list of delimiter characters used to break-up parsed content into terms for indexing.
+     *
+     * @var string
+     */
+    public static $delimiters = " \n\t.,!?:;()[]{}<>`'\"";
 
     /**
      * @var EntityProvider
@@ -61,7 +67,7 @@ class SearchIndex
      * - The number that have been processed so far.
      * - The total number of that model to be processed.
      *
-     * @param callable(Entity, int, int)|null $progressCallback
+     * @param callable(Entity, int, int):void|null $progressCallback
      */
     public function indexAllEntities(?callable $progressCallback = null)
     {
@@ -169,7 +175,7 @@ class SearchIndex
         $names = [];
         $values = [];
 
-        foreach($tags as $tag) {
+        foreach ($tags as $tag) {
             $names[] = $tag->name;
             $values[] = $tag->value;
         }
@@ -189,7 +195,7 @@ class SearchIndex
     protected function textToTermCountMap(string $text): array
     {
         $tokenMap = []; // {TextToken => OccurrenceCount}
-        $splitChars = " \n\t.,!?:;()[]{}<>`'\"";
+        $splitChars = static::$delimiters;
         $token = strtok($text, $splitChars);
 
         while ($token !== false) {
@@ -217,7 +223,7 @@ class SearchIndex
         if ($entity instanceof Page) {
             $bodyTermsMap = $this->generateTermScoreMapFromHtml($entity->html);
         } else {
-            $bodyTermsMap = $this->generateTermScoreMapFromText($entity->description, $entity->searchFactor);
+            $bodyTermsMap = $this->generateTermScoreMapFromText($entity->description ?? '', $entity->searchFactor);
         }
 
         $mergedScoreMap = $this->mergeTermScoreMaps($nameTermsMap, $bodyTermsMap, $tagTermsMap);
@@ -227,17 +233,16 @@ class SearchIndex
         $entityType = $entity->getMorphClass();
         foreach ($mergedScoreMap as $term => $score) {
             $dataArray[] = [
-                'term' => $term,
-                'score' => $score,
+                'term'        => $term,
+                'score'       => $score,
                 'entity_type' => $entityType,
-                'entity_id' => $entityId,
+                'entity_id'   => $entityId,
             ];
         }
 
         return $dataArray;
     }
 
-
     /**
      * For the given term data arrays, Merge their contents by term
      * while combining any scores.