1 <?php namespace BookStack\Services;
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;
15 protected $searchTerm;
20 protected $permissionService;
24 * Acceptable operators to be used in a query
27 protected $queryOperators = ['<=', '>=', '=', '<', '>', 'like', '!='];
30 * SearchService constructor.
31 * @param SearchTerm $searchTerm
33 * @param Chapter $chapter
35 * @param Connection $db
36 * @param PermissionService $permissionService
38 public function __construct(SearchTerm $searchTerm, Book $book, Chapter $chapter, Page $page, Connection $db, PermissionService $permissionService)
40 $this->searchTerm = $searchTerm;
42 $this->chapter = $chapter;
46 'page' => $this->page,
47 'chapter' => $this->chapter,
50 $this->permissionService = $permissionService;
54 * Search all entities in the system.
55 * @param string $searchString
56 * @param string $entityType
61 public function searchEntities($searchString, $entityType = 'all', $page = 0, $count = 20)
63 $terms = $this->parseSearchString($searchString);
64 $entityTypes = array_keys($this->entities);
65 $entityTypesToSearch = $entityTypes;
68 if ($entityType !== 'all') {
69 $entityTypesToSearch = $entityType;
70 } else if (isset($terms['filters']['type'])) {
71 $entityTypesToSearch = explode('|', $terms['filters']['type']);
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);
81 return $results->sortByDesc('score');
85 * Search across a particular entity type.
87 * @param string $entityType
90 * @return \Illuminate\Database\Eloquent\Collection|static[]
92 public function searchEntityTable($terms, $entityType = 'page', $page = 0, $count = 20)
94 $entity = $this->getEntity($entityType);
95 $entitySelect = $entity->newQuery();
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 .'%');
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);
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 .'%');
123 // Handle tag searches
124 foreach ($terms['tags'] as $inputTerm) {
125 $this->applyTagSearch($entitySelect, $inputTerm);
129 foreach ($terms['filters'] as $filterTerm => $filterValue) {
130 $functionName = camel_case('filter_' . $filterTerm);
131 if (method_exists($this, $functionName)) $this->$functionName($entitySelect, $entity, $filterValue);
134 $entitySelect->skip($page * $count)->take($count);
135 $query = $this->permissionService->enforceEntityRestrictions($entityType, $entitySelect, 'view');
136 return $query->get();
141 * Parse a search string into components.
142 * @param $searchString
145 protected function parseSearchString($searchString)
155 'exact' => '/"(.*?)"/',
156 'tags' => '/\[(.*?)\]/',
157 'filters' => '/\{(.*?)\}/'
160 // Parse special terms
161 foreach ($patterns as $termType => $pattern) {
163 preg_match_all($pattern, $searchString, $matches);
164 if (count($matches) > 0) {
165 $terms[$termType] = $matches[1];
166 $searchString = preg_replace($pattern, '', $searchString);
170 // Parse standard terms
171 foreach (explode(' ', trim($searchString)) as $searchTerm) {
172 if ($searchTerm !== '') $terms['search'][] = $searchTerm;
175 // Split filter values out
177 foreach ($terms['filters'] as $filter) {
178 $explodedFilter = explode(':', $filter, 2);
179 $splitFilters[$explodedFilter[0]] = (count($explodedFilter) > 1) ? $explodedFilter[1] : '';
181 $terms['filters'] = $splitFilters;
187 * Get the available query operators as a regex escaped list.
190 protected function getRegexEscapedOperators()
192 $escapedOperators = [];
193 foreach ($this->queryOperators as $operator) {
194 $escapedOperators[] = preg_quote($operator);
196 return join('|', $escapedOperators);
200 * Apply a tag search term onto a entity query.
201 * @param \Illuminate\Database\Eloquent\Builder $query
202 * @param string $tagTerm
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}");
221 $query->where('value', $tagOperator, $tagValue);
224 $query->where('name', '=', $tagName);
231 * Get an entity instance via type.
235 protected function getEntity($type)
237 return $this->entities[strtolower($type)];
241 * Index the given entity.
242 * @param Entity $entity
244 public function indexEntity(Entity $entity)
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;
254 $this->searchTerm->newQuery()->insert($terms);
258 * Index multiple Entities at once
259 * @param Entity[] $entities
261 protected function indexEntities($entities) {
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();
273 $chunkedTerms = array_chunk($terms, 500);
274 foreach ($chunkedTerms as $termChunk) {
275 $this->searchTerm->newQuery()->insert($termChunk);
280 * Delete and re-index the terms for all entities in the system.
282 public function indexAllEntities()
284 $this->searchTerm->truncate();
286 // Chunk through all books
287 $this->book->chunk(1000, function ($books) {
288 $this->indexEntities($books);
291 // Chunk through all chapters
292 $this->chapter->chunk(1000, function ($chapters) {
293 $this->indexEntities($chapters);
296 // Chunk through all pages
297 $this->page->chunk(1000, function ($pages) {
298 $this->indexEntities($pages);
303 * Delete related Entity search terms.
304 * @param Entity $entity
306 public function deleteEntityTerms(Entity $entity)
308 $entity->searchTerms()->delete();
312 * Create a scored term array from the given text.
314 * @param float|int $scoreAdjustment
317 protected function generateTermArrayFromText($text, $scoreAdjustment = 1)
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;
328 foreach ($tokenMap as $token => $count) {
331 'score' => $count * $scoreAdjustment
341 * Custom entity search filters
344 protected function filterUpdatedAfter(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
346 try { $date = date_create($input);
347 } catch (\Exception $e) {return;}
348 $query->where('updated_at', '>=', $date);
351 protected function filterUpdatedBefore(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
353 try { $date = date_create($input);
354 } catch (\Exception $e) {return;}
355 $query->where('updated_at', '<', $date);
358 protected function filterCreatedAfter(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
360 try { $date = date_create($input);
361 } catch (\Exception $e) {return;}
362 $query->where('created_at', '>=', $date);
365 protected function filterCreatedBefore(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
367 try { $date = date_create($input);
368 } catch (\Exception $e) {return;}
369 $query->where('created_at', '<', $date);
372 protected function filterCreatedBy(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
374 if (!is_numeric($input)) return;
375 $query->where('created_by', '=', $input);
378 protected function filterUpdatedBy(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
380 if (!is_numeric($input)) return;
381 $query->where('updated_by', '=', $input);
384 protected function filterInName(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
386 $query->where('name', 'like', '%' .$input. '%');
389 protected function filterInTitle(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input) {$this->filterInName($query, $model, $input);}
391 protected function filterInBody(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
393 $query->where($model->textField, 'like', '%' .$input. '%');
396 protected function filterIsRestricted(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
398 $query->where('restricted', '=', true);
401 protected function filterViewedByMe(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
403 $query->whereHas('views', function($query) {
404 $query->where('user_id', '=', user()->id);
408 protected function filterNotViewedByMe(\Illuminate\Database\Eloquent\Builder $query, Entity $model, $input)
410 $query->whereDoesntHave('views', function($query) {
411 $query->where('user_id', '=', user()->id);