]> BookStack Code Mirror - bookstack/blob - app/Services/SearchService.php
Fixed entity type filter bug in new search system
[bookstack] / app / Services / SearchService.php
1 <?php namespace BookStack\Services;
2
3 use BookStack\Book;
4 use BookStack\Chapter;
5 use BookStack\Entity;
6 use BookStack\Page;
7 use BookStack\SearchTerm;
8 use Illuminate\Database\Connection;
9 use Illuminate\Database\Query\Builder;
10 use Illuminate\Database\Query\JoinClause;
11 use Illuminate\Support\Collection;
12
13 class SearchService
14 {
15     protected $searchTerm;
16     protected $book;
17     protected $chapter;
18     protected $page;
19     protected $db;
20     protected $permissionService;
21     protected $entities;
22
23     /**
24      * Acceptable operators to be used in a query
25      * @var array
26      */
27     protected $queryOperators = ['<=', '>=', '=', '<', '>', 'like', '!='];
28
29     /**
30      * SearchService constructor.
31      * @param SearchTerm $searchTerm
32      * @param Book $book
33      * @param Chapter $chapter
34      * @param Page $page
35      * @param Connection $db
36      * @param PermissionService $permissionService
37      */
38     public function __construct(SearchTerm $searchTerm, Book $book, Chapter $chapter, Page $page, Connection $db, PermissionService $permissionService)
39     {
40         $this->searchTerm = $searchTerm;
41         $this->book = $book;
42         $this->chapter = $chapter;
43         $this->page = $page;
44         $this->db = $db;
45         $this->entities = [
46             'page' => $this->page,
47             'chapter' => $this->chapter,
48             'book' => $this->book
49         ];
50         $this->permissionService = $permissionService;
51     }
52
53     /**
54      * Search all entities in the system.
55      * @param string $searchString
56      * @param string $entityType
57      * @param int $page
58      * @param int $count
59      * @return Collection
60      */
61     public function searchEntities($searchString, $entityType = 'all', $page = 0, $count = 20)
62     {
63         $terms = $this->parseSearchString($searchString);
64         $entityTypes = array_keys($this->entities);
65         $entityTypesToSearch = $entityTypes;
66         $results = collect();
67
68         if ($entityType !== 'all') {
69             $entityTypesToSearch = $entityType;
70         } else if (isset($terms['filters']['type'])) {
71             $entityTypesToSearch = explode('|', $terms['filters']['type']);
72         }
73
74         // TODO - Check drafts don't show up in results
75         foreach ($entityTypesToSearch as $entityType) {
76             if (!in_array($entityType, $entityTypes)) continue;
77             $search = $this->searchEntityTable($terms, $entityType, $page, $count);
78             $results = $results->merge($search);
79         }
80
81         return $results->sortByDesc('score');
82     }
83
84     /**
85      * Search across a particular entity type.
86      * @param array $terms
87      * @param string $entityType
88      * @param int $page
89      * @param int $count
90      * @return \Illuminate\Database\Eloquent\Collection|static[]
91      */
92     public function searchEntityTable($terms, $entityType = 'page', $page = 0, $count = 20)
93     {
94         $entity = $this->getEntity($entityType);
95         $entitySelect = $entity->newQuery();
96
97         // Handle normal search terms
98         if (count($terms['search']) > 0) {
99             $subQuery = $this->db->table('search_terms')->select('entity_id', 'entity_type', \DB::raw('SUM(score) as score'));
100             $subQuery->where(function(Builder $query) use ($terms) {
101                 foreach ($terms['search'] as $inputTerm) {
102                     $query->orWhere('term', 'like', $inputTerm .'%');
103                 }
104             })->groupBy('entity_type', 'entity_id');
105             $entitySelect->join(\DB::raw('(' . $subQuery->toSql() . ') as s'), function(JoinClause $join) {
106                 $join->on('id', '=', 'entity_id');
107             })->selectRaw($entity->getTable().'.*, s.score')->orderBy('score', 'desc');
108             $entitySelect->mergeBindings($subQuery);
109         }
110
111         // Handle exact term matching
112         if (count($terms['exact']) > 0) {
113             $entitySelect->where(function(\Illuminate\Database\Eloquent\Builder $query) use ($terms, $entity) {
114                 foreach ($terms['exact'] as $inputTerm) {
115                     $query->where(function (\Illuminate\Database\Eloquent\Builder $query) use ($inputTerm, $entity) {
116                         $query->where('name', 'like', '%'.$inputTerm .'%')
117                             ->orWhere($entity->textField, 'like', '%'.$inputTerm .'%');
118                     });
119                 }
120             });
121         }
122
123         // Handle tag searches
124         foreach ($terms['tags'] as $inputTerm) {
125             $this->applyTagSearch($entitySelect, $inputTerm);
126         }
127
128         // Handle filters
129         foreach ($terms['filters'] as $filterTerm => $filterValue) {
130             $functionName = camel_case('filter_' . $filterTerm);
131             if (method_exists($this, $functionName)) $this->$functionName($entitySelect, $entity, $filterValue);
132         }
133
134         $entitySelect->skip($page * $count)->take($count);
135         $query = $this->permissionService->enforceEntityRestrictions($entityType, $entitySelect, 'view');
136         return $query->get();
137     }
138
139
140     /**
141      * Parse a search string into components.
142      * @param $searchString
143      * @return array
144      */
145     protected function parseSearchString($searchString)
146     {
147         $terms = [
148             'search' => [],
149             'exact' => [],
150             'tags' => [],
151             'filters' => []
152         ];
153
154         $patterns = [
155             'exact' => '/"(.*?)"/',
156             'tags' => '/\[(.*?)\]/',
157             'filters' => '/\{(.*?)\}/'
158         ];
159
160         // Parse special terms
161         foreach ($patterns as $termType => $pattern) {
162             $matches = [];
163             preg_match_all($pattern, $searchString, $matches);
164             if (count($matches) > 0) {
165                 $terms[$termType] = $matches[1];
166                 $searchString = preg_replace($pattern, '', $searchString);
167             }
168         }
169
170         // Parse standard terms
171         foreach (explode(' ', trim($searchString)) as $searchTerm) {
172             if ($searchTerm !== '') $terms['search'][] = $searchTerm;
173         }
174
175         // Split filter values out
176         $splitFilters = [];
177         foreach ($terms['filters'] as $filter) {
178             $explodedFilter = explode(':', $filter, 2);
179             $splitFilters[$explodedFilter[0]] = (count($explodedFilter) > 1) ? $explodedFilter[1] : '';
180         }
181         $terms['filters'] = $splitFilters;
182
183         return $terms;
184     }
185
186     /**
187      * Get the available query operators as a regex escaped list.
188      * @return mixed
189      */
190     protected function getRegexEscapedOperators()
191     {
192         $escapedOperators = [];
193         foreach ($this->queryOperators as $operator) {
194             $escapedOperators[] = preg_quote($operator);
195         }
196         return join('|', $escapedOperators);
197     }
198
199     /**
200      * Apply a tag search term onto a entity query.
201      * @param \Illuminate\Database\Eloquent\Builder $query
202      * @param string $tagTerm
203      * @return mixed
204      */
205     protected function applyTagSearch(\Illuminate\Database\Eloquent\Builder $query, $tagTerm) {
206         preg_match("/^(.*?)((".$this->getRegexEscapedOperators().")(.*?))?$/", $tagTerm, $tagSplit);
207         $query->whereHas('tags', function(\Illuminate\Database\Eloquent\Builder $query) use ($tagSplit) {
208             $tagName = $tagSplit[1];
209             $tagOperator = count($tagSplit) > 2 ? $tagSplit[3] : '';
210             $tagValue = count($tagSplit) > 3 ? $tagSplit[4] : '';
211             $validOperator = in_array($tagOperator, $this->queryOperators);
212             if (!empty($tagOperator) && !empty($tagValue) && $validOperator) {
213                 if (!empty($tagName)) $query->where('name', '=', $tagName);
214                 if (is_numeric($tagValue) && $tagOperator !== 'like') {
215                     // We have to do a raw sql query for this since otherwise PDO will quote the value and MySQL will
216                     // search the value as a string which prevents being able to do number-based operations
217                     // on the tag values. We ensure it has a numeric value and then cast it just to be sure.
218                     $tagValue = (float) trim($query->getConnection()->getPdo()->quote($tagValue), "'");
219                     $query->whereRaw("value ${tagOperator} ${tagValue}");
220                 } else {
221                     $query->where('value', $tagOperator, $tagValue);
222                 }
223             } else {
224                 $query->where('name', '=', $tagName);
225             }
226         });
227         return $query;
228     }
229
230     /**
231      * Get an entity instance via type.
232      * @param $type
233      * @return Entity
234      */
235     protected function getEntity($type)
236     {
237         return $this->entities[strtolower($type)];
238     }
239
240     /**
241      * Index the given entity.
242      * @param Entity $entity
243      */
244     public function indexEntity(Entity $entity)
245     {
246         $this->deleteEntityTerms($entity);
247         $nameTerms = $this->generateTermArrayFromText($entity->name, 5);
248         $bodyTerms = $this->generateTermArrayFromText($entity->getText(), 1);
249         $terms = array_merge($nameTerms, $bodyTerms);
250         foreach ($terms as $index => $term) {
251             $terms[$index]['entity_type'] = $entity->getMorphClass();
252             $terms[$index]['entity_id'] = $entity->id;
253         }
254         $this->searchTerm->newQuery()->insert($terms);
255     }
256
257     /**
258      * Index multiple Entities at once
259      * @param Entity[] $entities
260      */
261     protected function indexEntities($entities) {
262         $terms = [];
263         foreach ($entities as $entity) {
264             $nameTerms = $this->generateTermArrayFromText($entity->name, 5);
265             $bodyTerms = $this->generateTermArrayFromText($entity->getText(), 1);
266             foreach (array_merge($nameTerms, $bodyTerms) as $term) {
267                 $term['entity_id'] = $entity->id;
268                 $term['entity_type'] = $entity->getMorphClass();
269                 $terms[] = $term;
270             }
271         }
272
273         $chunkedTerms = array_chunk($terms, 500);
274         foreach ($chunkedTerms as $termChunk) {
275             $this->searchTerm->newQuery()->insert($termChunk);
276         }
277     }
278
279     /**
280      * Delete and re-index the terms for all entities in the system.
281      */
282     public function indexAllEntities()
283     {
284         $this->searchTerm->truncate();
285
286         // Chunk through all books
287         $this->book->chunk(1000, function ($books) {
288             $this->indexEntities($books);
289         });
290
291         // Chunk through all chapters
292         $this->chapter->chunk(1000, function ($chapters) {
293             $this->indexEntities($chapters);
294         });
295
296         // Chunk through all pages
297         $this->page->chunk(1000, function ($pages) {
298             $this->indexEntities($pages);
299         });
300     }
301
302     /**
303      * Delete related Entity search terms.
304      * @param Entity $entity
305      */
306     public function deleteEntityTerms(Entity $entity)
307     {
308         $entity->searchTerms()->delete();
309     }
310
311     /**
312      * Create a scored term array from the given text.
313      * @param $text
314      * @param float|int $scoreAdjustment
315      * @return array
316      */
317     protected function generateTermArrayFromText($text, $scoreAdjustment = 1)
318     {
319         $tokenMap = []; // {TextToken => OccurrenceCount}
320         $splitText = explode(' ', $text);
321         foreach ($splitText as $token) {
322             if ($token === '') continue;
323             if (!isset($tokenMap[$token])) $tokenMap[$token] = 0;
324             $tokenMap[$token]++;
325         }
326
327         $terms = [];
328         foreach ($tokenMap as $token => $count) {
329             $terms[] = [
330                 'term' => $token,
331                 'score' => $count * $scoreAdjustment
332             ];
333         }
334         return $terms;
335     }
336
337
338
339
340     /**
341      * Custom entity search filters
342      */
343
344     protected function filterUpdatedAfter(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
345     {
346         try { $date = date_create($input);
347         } catch (\Exception $e) {return;}
348         $query->where('updated_at', '>=', $date);
349     }
350
351     protected function filterUpdatedBefore(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
352     {
353         try { $date = date_create($input);
354         } catch (\Exception $e) {return;}
355         $query->where('updated_at', '<', $date);
356     }
357
358     protected function filterCreatedAfter(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
359     {
360         try { $date = date_create($input);
361         } catch (\Exception $e) {return;}
362         $query->where('created_at', '>=', $date);
363     }
364
365     protected function filterCreatedBefore(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
366     {
367         try { $date = date_create($input);
368         } catch (\Exception $e) {return;}
369         $query->where('created_at', '<', $date);
370     }
371
372     protected function filterCreatedBy(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
373     {
374         if (!is_numeric($input)) return;
375         $query->where('created_by', '=', $input);
376     }
377
378     protected function filterUpdatedBy(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
379     {
380         if (!is_numeric($input)) return;
381         $query->where('updated_by', '=', $input);
382     }
383
384     protected function filterInName(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
385     {
386         $query->where('name', 'like', '%' .$input. '%');
387     }
388
389     protected function filterInTitle(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input) {$this->filterInName($query, $model, $input);}
390
391     protected function filterInBody(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
392     {
393         $query->where($model->textField, 'like', '%' .$input. '%');
394     }
395
396     protected function filterIsRestricted(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
397     {
398         $query->where('restricted', '=', true);
399     }
400
401     protected function filterViewedByMe(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
402     {
403         $query->whereHas('views', function($query) {
404             $query->where('user_id', '=', user()->id);
405         });
406     }
407
408     protected function filterNotViewedByMe(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
409     {
410         $query->whereDoesntHave('views', function($query) {
411             $query->where('user_id', '=', user()->id);
412         });
413     }
414
415 }