X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/9135a85de4eef32a91c7a3ee0aa405ed454e5a4c..refs/pull/5349/head:/app/Search/SearchIndex.php diff --git a/app/Search/SearchIndex.php b/app/Search/SearchIndex.php index 54ed95ebb..d9fc4e7aa 100644 --- a/app/Search/SearchIndex.php +++ b/app/Search/SearchIndex.php @@ -2,11 +2,11 @@ namespace BookStack\Search; -use BookStack\Actions\Tag; +use BookStack\Activity\Models\Tag; use BookStack\Entities\EntityProvider; use BookStack\Entities\Models\Entity; use BookStack\Entities\Models\Page; -use DOMDocument; +use BookStack\Util\HtmlDocument; use DOMNode; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Collection; @@ -15,25 +15,18 @@ 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 */ - protected $entityProvider; + public static string $delimiters = " \n\t.,!?:;()[]{}<>`'\""; - public function __construct(EntityProvider $entityProvider) - { - $this->entityProvider = $entityProvider; + public function __construct( + protected EntityProvider $entityProvider + ) { } /** * Index the given entity. */ - public function indexEntity(Entity $entity) + public function indexEntity(Entity $entity): void { $this->deleteEntityTerms($entity); $terms = $this->entityToTermDataArray($entity); @@ -45,7 +38,7 @@ class SearchIndex * * @param Entity[] $entities */ - public function indexEntities(array $entities) + public function indexEntities(array $entities): void { $terms = []; foreach ($entities as $entity) { @@ -69,7 +62,7 @@ class SearchIndex * * @param callable(Entity, int, int):void|null $progressCallback */ - public function indexAllEntities(?callable $progressCallback = null) + public function indexAllEntities(?callable $progressCallback = null): void { SearchTerm::query()->truncate(); @@ -101,7 +94,7 @@ class SearchIndex /** * Delete related Entity search terms. */ - public function deleteEntityTerms(Entity $entity) + public function deleteEntityTerms(Entity $entity): void { $entity->searchTerms()->delete(); } @@ -145,16 +138,11 @@ class SearchIndex 'h6' => 1.5, ]; - $html = '' . $html . ''; $html = str_ireplace(['
', '
', '
'], "\n", $html); + $doc = new HtmlDocument($html); - libxml_use_internal_errors(true); - $doc = new DOMDocument(); - $doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8')); - - $topElems = $doc->documentElement->childNodes->item(0)->childNodes; /** @var DOMNode $child */ - foreach ($topElems as $child) { + foreach ($doc->getBodyChildren() as $child) { $nodeName = $child->nodeName; $termCounts = $this->textToTermCountMap(trim($child->textContent)); foreach ($termCounts as $term => $count) { @@ -175,7 +163,6 @@ class SearchIndex */ protected function generateTermScoreMapFromTags(array $tags): array { - $scoreMap = []; $names = []; $values = [];