3 namespace BookStack\Search;
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;
24 * Acceptable operators to be used in a query.
28 protected array $queryOperators = ['<=', '>=', '=', '<', '>', 'like', '!='];
31 * Retain a cache of score adjusted terms for specific search options.
32 * From PHP>=8 this can be made into a WeakMap instead.
34 * @var SplObjectStorage
36 protected $termAdjustmentCache;
38 public function __construct(
39 protected EntityProvider $entityProvider,
40 protected PermissionApplicator $permissions,
41 protected EntityQueries $entityQueries,
43 $this->termAdjustmentCache = new SplObjectStorage();
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.
51 * @return array{total: int, count: int, has_more: bool, results: Collection<Entity>}
53 public function searchEntities(SearchOptions $searchOpts, string $entityType = 'all', int $page = 1, int $count = 20): array
55 $entityTypes = array_keys($this->entityProvider->all());
56 $entityTypesToSearch = $entityTypes;
58 $filterMap = $searchOpts->filters->toValueMap();
59 if ($entityType !== 'all') {
60 $entityTypesToSearch = [$entityType];
61 } elseif (isset($filterMap['type'])) {
62 $entityTypesToSearch = explode('|', $filterMap['type']);
69 foreach ($entityTypesToSearch as $entityType) {
70 if (!in_array($entityType, $entityTypes)) {
74 $searchQuery = $this->buildQuery($searchOpts, $entityType);
75 $entityTotal = $searchQuery->count();
76 $searchResults = $this->getPageOfDataFromQuery($searchQuery, $entityType, $page, $count);
78 if ($entityTotal > ($page * $count)) {
82 $total += $entityTotal;
83 $results = $results->merge($searchResults);
88 'count' => count($results),
89 'has_more' => $hasMore,
90 'results' => $results->sortByDesc('score')->values(),
95 * Search a book for entities.
97 public function searchBook(int $bookId, string $searchString): Collection
99 $opts = SearchOptions::fromString($searchString);
100 $entityTypes = ['page', 'chapter'];
101 $filterMap = $opts->filters->toValueMap();
102 $entityTypesToSearch = isset($filterMap['type']) ? explode('|', $filterMap['type']) : $entityTypes;
104 $results = collect();
105 foreach ($entityTypesToSearch as $entityType) {
106 if (!in_array($entityType, $entityTypes)) {
110 $search = $this->buildQuery($opts, $entityType)->where('book_id', '=', $bookId)->take(20)->get();
111 $results = $results->merge($search);
114 return $results->sortByDesc('score')->take(20);
118 * Search a chapter for entities.
120 public function searchChapter(int $chapterId, string $searchString): Collection
122 $opts = SearchOptions::fromString($searchString);
123 $pages = $this->buildQuery($opts, 'page')->where('chapter_id', '=', $chapterId)->take(20)->get();
125 return $pages->sortByDesc('score');
129 * Get a page of result data from the given query based on the provided page parameters.
131 protected function getPageOfDataFromQuery(EloquentBuilder $query, string $entityType, int $page = 1, int $count = 20): EloquentCollection
133 $relations = ['tags'];
135 if ($entityType === 'page' || $entityType === 'chapter') {
136 $relations['book'] = function (BelongsTo $query) {
137 $query->scopes('visible');
141 if ($entityType === 'page') {
142 $relations['chapter'] = function (BelongsTo $query) {
143 $query->scopes('visible');
147 return $query->clone()
148 ->with(array_filter($relations))
149 ->skip(($page - 1) * $count)
155 * Create a search query for an entity.
157 protected function buildQuery(SearchOptions $searchOpts, string $entityType): EloquentBuilder
159 $entityModelInstance = $this->entityProvider->get($entityType);
160 $entityQuery = $this->entityQueries->visibleForList($entityType);
162 // Handle normal search terms
163 $this->applyTermSearch($entityQuery, $searchOpts, $entityType);
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 . '%');
174 // Handle tag searches
175 foreach ($searchOpts->tags->toValueArray() as $inputTerm) {
176 $this->applyTagSearch($entityQuery, $inputTerm);
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);
191 * For the given search query, apply the queries for handling the regular search terms.
193 protected function applyTermSearch(EloquentBuilder $entityQuery, SearchOptions $options, string $entityType): void
195 $terms = $options->searches->toValueArray();
196 if (count($terms) === 0) {
200 $scoredTerms = $this->getTermAdjustments($options);
201 $scoreSelect = $this->selectForScoredTerms($scoredTerms);
203 $subQuery = DB::table('search_terms')->select([
206 DB::raw($scoreSelect['statement']),
209 $subQuery->addBinding($scoreSelect['bindings'], 'select');
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 . '%');
218 $subQuery->groupBy('entity_type', 'entity_id');
220 $entityQuery->joinSub($subQuery, 's', 'id', '=', 'entity_id');
221 $entityQuery->addSelect('s.score');
222 $entityQuery->orderBy('score', 'desc');
226 * Create a select statement, with prepared bindings, for the given
227 * set of scored search terms.
229 * @param array<string, float> $scoredTerms
231 * @return array{statement: string, bindings: string[]}
233 protected function selectForScoredTerms(array $scoredTerms): array
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).
241 foreach ($scoredTerms as $term => $score) {
242 $ifChain = 'IF(term like ?, score * ' . (float) $score . ', ' . $ifChain . ')';
243 $bindings[] = $term . '%';
247 'statement' => 'SUM(' . $ifChain . ') as score',
248 'bindings' => array_reverse($bindings),
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.
257 * @return array<string, float>
259 protected function getTermAdjustments(SearchOptions $options): array
261 if (isset($this->termAdjustmentCache[$options])) {
262 return $this->termAdjustmentCache[$options];
265 $termQuery = SearchTerm::query()->toBase();
266 $whenStatements = [];
269 foreach ($options->searches->toValueArray() as $term) {
270 $whenStatements[] = 'WHEN term LIKE ? THEN ?';
271 $whenBindings[] = $term . '%';
272 $whenBindings[] = $term;
274 $termQuery->orWhere('term', 'like', $term . '%');
277 $case = 'CASE ' . implode(' ', $whenStatements) . ' END';
278 $termQuery->selectRaw($case . ' as term', $whenBindings);
279 $termQuery->selectRaw('COUNT(*) as count');
280 $termQuery->groupByRaw($case, $whenBindings);
282 $termCounts = $termQuery->pluck('count', 'term')->toArray();
283 $adjusted = $this->rawTermCountsToAdjustments($termCounts);
285 $this->termAdjustmentCache[$options] = $adjusted;
287 return $this->termAdjustmentCache[$options];
291 * Convert counts of terms into a relative-count normalised multiplier.
293 * @param array<string, int> $termCounts
295 * @return array<string, int>
297 protected function rawTermCountsToAdjustments(array $termCounts): array
299 if (empty($termCounts)) {
304 $max = max(array_values($termCounts));
306 foreach ($termCounts as $term => $count) {
307 $percent = round($count / $max, 5);
308 $multipliers[$term] = 1.3 - $percent;
315 * Get the available query operators as a regex escaped list.
317 protected function getRegexEscapedOperators(): string
319 $escapedOperators = [];
320 foreach ($this->queryOperators as $operator) {
321 $escapedOperators[] = preg_quote($operator);
324 return implode('|', $escapedOperators);
328 * Apply a tag search term onto a entity query.
330 protected function applyTagSearch(EloquentBuilder $query, string $tagTerm): EloquentBuilder
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);
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}");
351 if ($tagOperator === 'like') {
352 $tagValue = str_replace('\\', '\\\\', $tagValue);
354 $query->where('value', $tagOperator, $tagValue);
357 $query->where('name', '=', $tagName);
365 * Custom entity search filters.
367 protected function filterUpdatedAfter(EloquentBuilder $query, Entity $model, $input): void
370 $date = date_create($input);
371 $query->where('updated_at', '>=', $date);
372 } catch (\Exception $e) {
376 protected function filterUpdatedBefore(EloquentBuilder $query, Entity $model, $input): void
379 $date = date_create($input);
380 $query->where('updated_at', '<', $date);
381 } catch (\Exception $e) {
385 protected function filterCreatedAfter(EloquentBuilder $query, Entity $model, $input): void
388 $date = date_create($input);
389 $query->where('created_at', '>=', $date);
390 } catch (\Exception $e) {
394 protected function filterCreatedBefore(EloquentBuilder $query, Entity $model, $input)
397 $date = date_create($input);
398 $query->where('created_at', '<', $date);
399 } catch (\Exception $e) {
403 protected function filterCreatedBy(EloquentBuilder $query, Entity $model, $input)
405 $userSlug = $input === 'me' ? user()->slug : trim($input);
406 $user = User::query()->where('slug', '=', $userSlug)->first(['id']);
408 $query->where('created_by', '=', $user->id);
412 protected function filterUpdatedBy(EloquentBuilder $query, Entity $model, $input)
414 $userSlug = $input === 'me' ? user()->slug : trim($input);
415 $user = User::query()->where('slug', '=', $userSlug)->first(['id']);
417 $query->where('updated_by', '=', $user->id);
421 protected function filterOwnedBy(EloquentBuilder $query, Entity $model, $input)
423 $userSlug = $input === 'me' ? user()->slug : trim($input);
424 $user = User::query()->where('slug', '=', $userSlug)->first(['id']);
426 $query->where('owned_by', '=', $user->id);
430 protected function filterInName(EloquentBuilder $query, Entity $model, $input)
432 $query->where('name', 'like', '%' . $input . '%');
435 protected function filterInTitle(EloquentBuilder $query, Entity $model, $input)
437 $this->filterInName($query, $model, $input);
440 protected function filterInBody(EloquentBuilder $query, Entity $model, $input)
442 $query->where($model->textField, 'like', '%' . $input . '%');
445 protected function filterIsRestricted(EloquentBuilder $query, Entity $model, $input)
447 $query->whereHas('permissions');
450 protected function filterViewedByMe(EloquentBuilder $query, Entity $model, $input)
452 $query->whereHas('views', function ($query) {
453 $query->where('user_id', '=', user()->id);
457 protected function filterNotViewedByMe(EloquentBuilder $query, Entity $model, $input)
459 $query->whereDoesntHave('views', function ($query) {
460 $query->where('user_id', '=', user()->id);
464 protected function filterIsTemplate(EloquentBuilder $query, Entity $model, $input)
466 if ($model instanceof Page) {
467 $query->where('template', '=', true);
471 protected function filterSortBy(EloquentBuilder $query, Entity $model, $input)
473 $functionName = Str::camel('sort_by_' . $input);
474 if (method_exists($this, $functionName)) {
475 $this->$functionName($query, $model);
480 * Sorting filter options.
482 protected function sortByLastCommented(EloquentBuilder $query, Entity $model)
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');
488 $query->join($commentQuery, $model->getTable() . '.id', '=', 'comments.entity_id')->orderBy('last_commented', 'desc');