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