3 namespace BookStack\Entities\Tools;
5 use BookStack\Actions\Tag;
6 use BookStack\Entities\EntityProvider;
7 use BookStack\Entities\Models\Entity;
8 use BookStack\Entities\Models\Page;
9 use BookStack\Entities\Models\SearchTerm;
12 use Illuminate\Database\Eloquent\Builder;
13 use Illuminate\Support\Collection;
18 * A list of delimiter characters used to break-up parsed content into terms for indexing.
22 public static $delimiters = " \n\t.,!?:;()[]{}<>`'\"";
27 protected $entityProvider;
29 public function __construct(EntityProvider $entityProvider)
31 $this->entityProvider = $entityProvider;
35 * Index the given entity.
37 public function indexEntity(Entity $entity)
39 $this->deleteEntityTerms($entity);
40 $terms = $this->entityToTermDataArray($entity);
41 SearchTerm::query()->insert($terms);
45 * Index multiple Entities at once.
47 * @param Entity[] $entities
49 public function indexEntities(array $entities)
52 foreach ($entities as $entity) {
53 $entityTerms = $this->entityToTermDataArray($entity);
54 array_push($terms, ...$entityTerms);
57 $chunkedTerms = array_chunk($terms, 500);
58 foreach ($chunkedTerms as $termChunk) {
59 SearchTerm::query()->insert($termChunk);
64 * Delete and re-index the terms for all entities in the system.
65 * Can take a callback which is used for reporting progress.
66 * Callback receives three arguments:
67 * - An instance of the model being processed
68 * - The number that have been processed so far.
69 * - The total number of that model to be processed.
71 * @param callable(Entity, int, int):void|null $progressCallback
73 public function indexAllEntities(?callable $progressCallback = null)
75 SearchTerm::query()->truncate();
77 foreach ($this->entityProvider->all() as $entityModel) {
78 $indexContentField = $entityModel instanceof Page ? 'html' : 'description';
79 $selectFields = ['id', 'name', $indexContentField];
80 /** @var Builder<Entity> $query */
81 $query = $entityModel->newQuery();
82 $total = $query->withTrashed()->count();
86 $chunkCallback = function (Collection $entities) use ($progressCallback, &$processed, $total, $chunkSize, $entityModel) {
87 $this->indexEntities($entities->all());
88 $processed = min($processed + $chunkSize, $total);
90 if (is_callable($progressCallback)) {
91 $progressCallback($entityModel, $processed, $total);
95 $entityModel->newQuery()
96 ->select($selectFields)
97 ->with(['tags:id,name,value,entity_id,entity_type'])
98 ->chunk($chunkSize, $chunkCallback);
103 * Delete related Entity search terms.
105 public function deleteEntityTerms(Entity $entity)
107 $entity->searchTerms()->delete();
111 * Create a scored term array from the given text, where the keys are the terms
112 * and the values are their scores.
114 * @returns array<string, int>
116 protected function generateTermScoreMapFromText(string $text, int $scoreAdjustment = 1): array
118 $termMap = $this->textToTermCountMap($text);
120 foreach ($termMap as $term => $count) {
121 $termMap[$term] = $count * $scoreAdjustment;
128 * Create a scored term array from the given HTML, where the keys are the terms
129 * and the values are their scores.
131 * @returns array<string, int>
133 protected function generateTermScoreMapFromHtml(string $html): array
140 $elementScoreAdjustmentMap = [
149 $html = '<body>' . $html . '</body>';
150 $html = str_ireplace(['<br>', '<br />', '<br/>'], "\n", $html);
152 libxml_use_internal_errors(true);
153 $doc = new DOMDocument();
154 $doc->loadHTML(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'));
156 $topElems = $doc->documentElement->childNodes->item(0)->childNodes;
157 /** @var DOMNode $child */
158 foreach ($topElems as $child) {
159 $nodeName = $child->nodeName;
160 $termCounts = $this->textToTermCountMap(trim($child->textContent));
161 foreach ($termCounts as $term => $count) {
162 $scoreChange = $count * ($elementScoreAdjustmentMap[$nodeName] ?? 1);
163 $scoresByTerm[$term] = ($scoresByTerm[$term] ?? 0) + $scoreChange;
167 return $scoresByTerm;
171 * Create a scored term map from the given set of entity tags.
175 * @returns array<string, int>
177 protected function generateTermScoreMapFromTags(array $tags): array
183 foreach ($tags as $tag) {
184 $names[] = $tag->name;
185 $values[] = $tag->value;
188 $nameMap = $this->generateTermScoreMapFromText(implode(' ', $names), 3);
189 $valueMap = $this->generateTermScoreMapFromText(implode(' ', $values), 5);
191 return $this->mergeTermScoreMaps($nameMap, $valueMap);
195 * For the given text, return an array where the keys are the unique term words
196 * and the values are the frequency of that term.
198 * @returns array<string, int>
200 protected function textToTermCountMap(string $text): array
202 $tokenMap = []; // {TextToken => OccurrenceCount}
203 $splitChars = static::$delimiters;
204 $token = strtok($text, $splitChars);
206 while ($token !== false) {
207 if (!isset($tokenMap[$token])) {
208 $tokenMap[$token] = 0;
211 $token = strtok($splitChars);
218 * For the given entity, Generate an array of term data details.
219 * Is the raw term data, not instances of SearchTerm models.
221 * @returns array{term: string, score: float, entity_id: int, entity_type: string}[]
223 protected function entityToTermDataArray(Entity $entity): array
225 $nameTermsMap = $this->generateTermScoreMapFromText($entity->name, 40 * $entity->searchFactor);
226 $tagTermsMap = $this->generateTermScoreMapFromTags($entity->tags->all());
228 if ($entity instanceof Page) {
229 $bodyTermsMap = $this->generateTermScoreMapFromHtml($entity->html);
231 $bodyTermsMap = $this->generateTermScoreMapFromText($entity->getAttribute('description') ?? '', $entity->searchFactor);
234 $mergedScoreMap = $this->mergeTermScoreMaps($nameTermsMap, $bodyTermsMap, $tagTermsMap);
237 $entityId = $entity->id;
238 $entityType = $entity->getMorphClass();
239 foreach ($mergedScoreMap as $term => $score) {
243 'entity_type' => $entityType,
244 'entity_id' => $entityId,
252 * For the given term data arrays, Merge their contents by term
253 * while combining any scores.
255 * @param array<string, int>[] ...$scoreMaps
257 * @returns array<string, int>
259 protected function mergeTermScoreMaps(...$scoreMaps): array
263 foreach ($scoreMaps as $scoreMap) {
264 foreach ($scoreMap as $term => $score) {
265 $mergedMap[$term] = ($mergedMap[$term] ?? 0) + $score;