]> BookStack Code Mirror - bookstack/blob - app/Search/SearchRunner.php
Fix issue BookStackApp#5542 Sorting by name
[bookstack] / app / Search / SearchRunner.php
1 <?php
2
3 namespace BookStack\Search;
4
5 use BookStack\Entities\EntityProvider;
6 use BookStack\Entities\Models\Entity;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Entities\Queries\EntityQueries;
9 use BookStack\Permissions\PermissionApplicator;
10 use BookStack\Search\Options\TagSearchOption;
11 use BookStack\Users\Models\User;
12 use Illuminate\Database\Connection;
13 use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
14 use Illuminate\Database\Eloquent\Collection as EloquentCollection;
15 use Illuminate\Database\Eloquent\Relations\BelongsTo;
16 use Illuminate\Database\Query\Builder;
17 use Illuminate\Support\Collection;
18 use Illuminate\Support\Facades\DB;
19 use Illuminate\Support\Str;
20 use WeakMap;
21
22 class SearchRunner
23 {
24     /**
25      * Retain a cache of score adjusted terms for specific search options.
26      */
27     protected WeakMap $termAdjustmentCache;
28
29     public function __construct(
30         protected EntityProvider $entityProvider,
31         protected PermissionApplicator $permissions,
32         protected EntityQueries $entityQueries,
33     ) {
34         $this->termAdjustmentCache = new WeakMap();
35     }
36
37     /**
38      * Search all entities in the system.
39      * The provided count is for each entity to search,
40      * Total returned could be larger and not guaranteed.
41      *
42      * @return array{total: int, count: int, has_more: bool, results: Collection<Entity>}
43      */
44     public function searchEntities(SearchOptions $searchOpts, string $entityType = 'all', int $page = 1, int $count = 20): array
45     {
46         $entityTypes = array_keys($this->entityProvider->all());
47         $entityTypesToSearch = $entityTypes;
48
49         $filterMap = $searchOpts->filters->toValueMap();
50         if ($entityType !== 'all') {
51             $entityTypesToSearch = [$entityType];
52         } elseif (isset($filterMap['type'])) {
53             $entityTypesToSearch = explode('|', $filterMap['type']);
54         }
55
56         $results = collect();
57         $total = 0;
58         $hasMore = false;
59
60         foreach ($entityTypesToSearch as $entityType) {
61             if (!in_array($entityType, $entityTypes)) {
62                 continue;
63             }
64
65             $searchQuery = $this->buildQuery($searchOpts, $entityType);
66             $entityTotal = $searchQuery->count();
67             $searchResults = $this->getPageOfDataFromQuery($searchQuery, $entityType, $page, $count);
68
69             if ($entityTotal > ($page * $count)) {
70                 $hasMore = true;
71             }
72
73             $total += $entityTotal;
74             $results = $results->merge($searchResults);
75         }
76
77         return [
78             'total'    => $total,
79             'count'    => count($results),
80             'has_more' => $hasMore,
81             'results'  => $results->sortByDesc('score')->values(),
82         ];
83     }
84
85     /**
86      * Search a book for entities.
87      */
88     public function searchBook(int $bookId, string $searchString): Collection
89     {
90         $opts = SearchOptions::fromString($searchString);
91         $entityTypes = ['page', 'chapter'];
92         $filterMap = $opts->filters->toValueMap();
93         $entityTypesToSearch = isset($filterMap['type']) ? explode('|', $filterMap['type']) : $entityTypes;
94
95         $results = collect();
96         foreach ($entityTypesToSearch as $entityType) {
97             if (!in_array($entityType, $entityTypes)) {
98                 continue;
99             }
100
101             $search = $this->buildQuery($opts, $entityType)->where('book_id', '=', $bookId)->take(20)->get();
102             $results = $results->merge($search);
103         }
104
105         return $results->sortByDesc('score')->take(20);
106     }
107
108     /**
109      * Search a chapter for entities.
110      */
111     public function searchChapter(int $chapterId, string $searchString): Collection
112     {
113         $opts = SearchOptions::fromString($searchString);
114         $pages = $this->buildQuery($opts, 'page')->where('chapter_id', '=', $chapterId)->take(20)->get();
115
116         return $pages->sortByDesc('score');
117     }
118
119     /**
120      * Get a page of result data from the given query based on the provided page parameters.
121      */
122     protected function getPageOfDataFromQuery(EloquentBuilder $query, string $entityType, int $page = 1, int $count = 20): EloquentCollection
123     {
124         $relations = ['tags'];
125
126         if ($entityType === 'page' || $entityType === 'chapter') {
127             $relations['book'] = function (BelongsTo $query) {
128                 $query->scopes('visible');
129             };
130         }
131
132         if ($entityType === 'page') {
133             $relations['chapter'] = function (BelongsTo $query) {
134                 $query->scopes('visible');
135             };
136         }
137
138         return $query->clone()
139             ->with(array_filter($relations))
140             ->skip(($page - 1) * $count)
141             ->take($count)
142             ->get();
143     }
144
145     /**
146      * Create a search query for an entity.
147      */
148     protected function buildQuery(SearchOptions $searchOpts, string $entityType): EloquentBuilder
149     {
150         $entityModelInstance = $this->entityProvider->get($entityType);
151         $entityQuery = $this->entityQueries->visibleForList($entityType);
152
153         // Handle normal search terms
154         $this->applyTermSearch($entityQuery, $searchOpts, $entityType);
155
156         // Handle exact term matching
157         foreach ($searchOpts->exacts->all() as $exact) {
158             $filter = function (EloquentBuilder $query) use ($exact, $entityModelInstance) {
159                 $inputTerm = str_replace('\\', '\\\\', $exact->value);
160                 $query->where('name', 'like', '%' . $inputTerm . '%')
161                     ->orWhere($entityModelInstance->textField, 'like', '%' . $inputTerm . '%');
162             };
163
164             $exact->negated ? $entityQuery->whereNot($filter) : $entityQuery->where($filter);
165         }
166
167         // Handle tag searches
168         foreach ($searchOpts->tags->all() as $tagOption) {
169             $this->applyTagSearch($entityQuery, $tagOption);
170         }
171
172         // Handle filters
173         foreach ($searchOpts->filters->all() as $filterOption) {
174             $functionName = Str::camel('filter_' . $filterOption->getKey());
175             if (method_exists($this, $functionName)) {
176                 $this->$functionName($entityQuery, $entityModelInstance, $filterOption->value, $filterOption->negated);
177             }
178         }
179
180         return $entityQuery;
181     }
182
183     /**
184      * For the given search query, apply the queries for handling the regular search terms.
185      */
186     protected function applyTermSearch(EloquentBuilder $entityQuery, SearchOptions $options, string $entityType): void
187     {
188         $terms = $options->searches->toValueArray();
189         if (count($terms) === 0) {
190             return;
191         }
192
193         $scoredTerms = $this->getTermAdjustments($options);
194         $scoreSelect = $this->selectForScoredTerms($scoredTerms);
195
196         $subQuery = DB::table('search_terms')->select([
197             'entity_id',
198             'entity_type',
199             DB::raw($scoreSelect['statement']),
200         ]);
201
202         $subQuery->addBinding($scoreSelect['bindings'], 'select');
203
204         $subQuery->where('entity_type', '=', $entityType);
205         $subQuery->where(function (Builder $query) use ($terms) {
206             foreach ($terms as $inputTerm) {
207                 $escapedTerm = str_replace('\\', '\\\\', $inputTerm);
208                 $query->orWhere('term', 'like', $escapedTerm . '%');
209             }
210         });
211         $subQuery->groupBy('entity_type', 'entity_id');
212
213         $entityQuery->joinSub($subQuery, 's', 'id', '=', 'entity_id');
214         $entityQuery->addSelect('s.score');
215         $entityQuery->orderBy('score', 'desc');
216     }
217
218     /**
219      * Create a select statement, with prepared bindings, for the given
220      * set of scored search terms.
221      *
222      * @param array<string, float> $scoredTerms
223      *
224      * @return array{statement: string, bindings: string[]}
225      */
226     protected function selectForScoredTerms(array $scoredTerms): array
227     {
228         // Within this we walk backwards to create the chain of 'if' statements
229         // so that each previous statement is used in the 'else' condition of
230         // the next (earlier) to be built. We start at '0' to have no score
231         // on no match (Should never actually get to this case).
232         $ifChain = '0';
233         $bindings = [];
234         foreach ($scoredTerms as $term => $score) {
235             $ifChain = 'IF(term like ?, score * ' . (float) $score . ', ' . $ifChain . ')';
236             $bindings[] = $term . '%';
237         }
238
239         return [
240             'statement' => 'SUM(' . $ifChain . ') as score',
241             'bindings'  => array_reverse($bindings),
242         ];
243     }
244
245     /**
246      * For the terms in the given search options, query their popularity across all
247      * search terms then provide that back as score adjustment multiplier applicable
248      * for their rarity. Returns an array of float multipliers, keyed by term.
249      *
250      * @return array<string, float>
251      */
252     protected function getTermAdjustments(SearchOptions $options): array
253     {
254         if (isset($this->termAdjustmentCache[$options])) {
255             return $this->termAdjustmentCache[$options];
256         }
257
258         $termQuery = SearchTerm::query()->toBase();
259         $whenStatements = [];
260         $whenBindings = [];
261
262         foreach ($options->searches->toValueArray() as $term) {
263             $whenStatements[] = 'WHEN term LIKE ? THEN ?';
264             $whenBindings[] = $term . '%';
265             $whenBindings[] = $term;
266
267             $termQuery->orWhere('term', 'like', $term . '%');
268         }
269
270         $case = 'CASE ' . implode(' ', $whenStatements) . ' END';
271         $termQuery->selectRaw($case . ' as term', $whenBindings);
272         $termQuery->selectRaw('COUNT(*) as count');
273         $termQuery->groupByRaw($case, $whenBindings);
274
275         $termCounts = $termQuery->pluck('count', 'term')->toArray();
276         $adjusted = $this->rawTermCountsToAdjustments($termCounts);
277
278         $this->termAdjustmentCache[$options] = $adjusted;
279
280         return $this->termAdjustmentCache[$options];
281     }
282
283     /**
284      * Convert counts of terms into a relative-count normalised multiplier.
285      *
286      * @param array<string, int> $termCounts
287      *
288      * @return array<string, int>
289      */
290     protected function rawTermCountsToAdjustments(array $termCounts): array
291     {
292         if (empty($termCounts)) {
293             return [];
294         }
295
296         $multipliers = [];
297         $max = max(array_values($termCounts));
298
299         foreach ($termCounts as $term => $count) {
300             $percent = round($count / $max, 5);
301             $multipliers[$term] = 1.3 - $percent;
302         }
303
304         return $multipliers;
305     }
306
307     /**
308      * Apply a tag search term onto an entity query.
309      */
310     protected function applyTagSearch(EloquentBuilder $query, TagSearchOption $option): void
311     {
312         $filter = function (EloquentBuilder $query) use ($option): void {
313             $tagParts = $option->getParts();
314             if (empty($tagParts['operator']) || empty($tagParts['value'])) {
315                 $query->where('name', '=', $tagParts['name']);
316                 return;
317             }
318
319             if (!empty($tagParts['name'])) {
320                 $query->where('name', '=', $tagParts['name']);
321             }
322
323             if (is_numeric($tagParts['value']) && $tagParts['operator'] !== 'like') {
324                 // We have to do a raw sql query for this since otherwise PDO will quote the value and MySQL will
325                 // search the value as a string which prevents being able to do number-based operations
326                 // on the tag values. We ensure it has a numeric value and then cast it just to be sure.
327                 /** @var Connection $connection */
328                 $connection = $query->getConnection();
329                 $quotedValue = (float) trim($connection->getPdo()->quote($tagParts['value']), "'");
330                 $query->whereRaw("value {$tagParts['operator']} {$quotedValue}");
331             } else if ($tagParts['operator'] === 'like') {
332                 $query->where('value', $tagParts['operator'], str_replace('\\', '\\\\', $tagParts['value']));
333             } else {
334                 $query->where('value', $tagParts['operator'], $tagParts['value']);
335             }
336         };
337
338         $option->negated ? $query->whereDoesntHave('tags', $filter) : $query->whereHas('tags', $filter);
339     }
340
341     protected function applyNegatableWhere(EloquentBuilder $query, bool $negated, string $column, string $operator, mixed $value): void
342     {
343         if ($negated) {
344             $query->whereNot($column, $operator, $value);
345         } else {
346             $query->where($column, $operator, $value);
347         }
348     }
349
350     /**
351      * Custom entity search filters.
352      */
353     protected function filterUpdatedAfter(EloquentBuilder $query, Entity $model, string $input, bool $negated): void
354     {
355         $date = date_create($input);
356         $this->applyNegatableWhere($query, $negated, 'updated_at', '>=', $date);
357     }
358
359     protected function filterUpdatedBefore(EloquentBuilder $query, Entity $model, string $input, bool $negated): void
360     {
361         $date = date_create($input);
362         $this->applyNegatableWhere($query, $negated, 'updated_at', '<', $date);
363     }
364
365     protected function filterCreatedAfter(EloquentBuilder $query, Entity $model, string $input, bool $negated): void
366     {
367         $date = date_create($input);
368         $this->applyNegatableWhere($query, $negated, 'created_at', '>=', $date);
369     }
370
371     protected function filterCreatedBefore(EloquentBuilder $query, Entity $model, string $input, bool $negated)
372     {
373         $date = date_create($input);
374         $this->applyNegatableWhere($query, $negated, 'created_at', '<', $date);
375     }
376
377     protected function filterCreatedBy(EloquentBuilder $query, Entity $model, string $input, bool $negated)
378     {
379         $userSlug = $input === 'me' ? user()->slug : trim($input);
380         $user = User::query()->where('slug', '=', $userSlug)->first(['id']);
381         if ($user) {
382             $this->applyNegatableWhere($query, $negated, 'created_by', '=', $user->id);
383         }
384     }
385
386     protected function filterUpdatedBy(EloquentBuilder $query, Entity $model, string $input, bool $negated)
387     {
388         $userSlug = $input === 'me' ? user()->slug : trim($input);
389         $user = User::query()->where('slug', '=', $userSlug)->first(['id']);
390         if ($user) {
391             $this->applyNegatableWhere($query, $negated, 'updated_by', '=', $user->id);
392         }
393     }
394
395     protected function filterOwnedBy(EloquentBuilder $query, Entity $model, string $input, bool $negated)
396     {
397         $userSlug = $input === 'me' ? user()->slug : trim($input);
398         $user = User::query()->where('slug', '=', $userSlug)->first(['id']);
399         if ($user) {
400             $this->applyNegatableWhere($query, $negated, 'owned_by', '=', $user->id);
401         }
402     }
403
404     protected function filterInName(EloquentBuilder $query, Entity $model, string $input, bool $negated)
405     {
406         $this->applyNegatableWhere($query, $negated, 'name', 'like', '%' . $input . '%');
407     }
408
409     protected function filterInTitle(EloquentBuilder $query, Entity $model, string $input, bool $negated)
410     {
411         $this->filterInName($query, $model, $input, $negated);
412     }
413
414     protected function filterInBody(EloquentBuilder $query, Entity $model, string $input, bool $negated)
415     {
416         $this->applyNegatableWhere($query, $negated, $model->textField, 'like', '%' . $input . '%');
417     }
418
419     protected function filterIsRestricted(EloquentBuilder $query, Entity $model, string $input, bool $negated)
420     {
421         $negated ? $query->whereDoesntHave('permissions') : $query->whereHas('permissions');
422     }
423
424     protected function filterViewedByMe(EloquentBuilder $query, Entity $model, string $input, bool $negated)
425     {
426         $filter = function ($query) {
427             $query->where('user_id', '=', user()->id);
428         };
429
430         $negated ? $query->whereDoesntHave('views', $filter) : $query->whereHas('views', $filter);
431     }
432
433     protected function filterNotViewedByMe(EloquentBuilder $query, Entity $model, string $input, bool $negated)
434     {
435         $filter = function ($query) {
436             $query->where('user_id', '=', user()->id);
437         };
438
439         $negated ? $query->whereHas('views', $filter) : $query->whereDoesntHave('views', $filter);
440     }
441
442     protected function filterIsTemplate(EloquentBuilder $query, Entity $model, string $input, bool $negated)
443     {
444         if ($model instanceof Page) {
445             $this->applyNegatableWhere($query, $negated, 'template', '=', true);
446         }
447     }
448
449     protected function filterSortBy(EloquentBuilder $query, Entity $model, string $input, bool $negated)
450     {
451         $functionName = Str::camel('sort_by_' . $input);
452         if (method_exists($this, $functionName)) {
453             $this->$functionName($query, $model, $negated);
454         }
455     }
456
457     /**
458      * Sorting filter options.
459      */
460     protected function sortByLastCommented(EloquentBuilder $query, Entity $model, bool $negated)
461     {
462         $commentsTable = DB::getTablePrefix() . 'comments';
463         $morphClass = str_replace('\\', '\\\\', $model->getMorphClass());
464         $commentQuery = DB::raw('(SELECT c1.entity_id, c1.entity_type, c1.created_at as last_commented FROM ' . $commentsTable . ' c1 LEFT JOIN ' . $commentsTable . ' c2 ON (c1.entity_id = c2.entity_id AND c1.entity_type = c2.entity_type AND c1.created_at < c2.created_at) WHERE c1.entity_type = \'' . $morphClass . '\' AND c2.created_at IS NULL) as comments');
465
466         $query->join($commentQuery, $model->getTable() . '.id', '=', DB::raw('comments.entity_id'))
467             ->orderBy('last_commented', $negated ? 'asc' : 'desc');
468     }
469 }