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\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;
25 * Retain a cache of score adjusted terms for specific search options.
27 protected WeakMap $termAdjustmentCache;
29 public function __construct(
30 protected EntityProvider $entityProvider,
31 protected PermissionApplicator $permissions,
32 protected EntityQueries $entityQueries,
34 $this->termAdjustmentCache = new WeakMap();
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.
42 * @return array{total: int, count: int, has_more: bool, results: Collection<Entity>}
44 public function searchEntities(SearchOptions $searchOpts, string $entityType = 'all', int $page = 1, int $count = 20): array
46 $entityTypes = array_keys($this->entityProvider->all());
47 $entityTypesToSearch = $entityTypes;
49 $filterMap = $searchOpts->filters->toValueMap();
50 if ($entityType !== 'all') {
51 $entityTypesToSearch = [$entityType];
52 } elseif (isset($filterMap['type'])) {
53 $entityTypesToSearch = explode('|', $filterMap['type']);
60 foreach ($entityTypesToSearch as $entityType) {
61 if (!in_array($entityType, $entityTypes)) {
65 $searchQuery = $this->buildQuery($searchOpts, $entityType);
66 $entityTotal = $searchQuery->count();
67 $searchResults = $this->getPageOfDataFromQuery($searchQuery, $entityType, $page, $count);
69 if ($entityTotal > ($page * $count)) {
73 $total += $entityTotal;
74 $results = $results->merge($searchResults);
79 'count' => count($results),
80 'has_more' => $hasMore,
81 'results' => $results->sortByDesc('score')->values(),
86 * Search a book for entities.
88 public function searchBook(int $bookId, string $searchString): Collection
90 $opts = SearchOptions::fromString($searchString);
91 $entityTypes = ['page', 'chapter'];
92 $filterMap = $opts->filters->toValueMap();
93 $entityTypesToSearch = isset($filterMap['type']) ? explode('|', $filterMap['type']) : $entityTypes;
96 foreach ($entityTypesToSearch as $entityType) {
97 if (!in_array($entityType, $entityTypes)) {
101 $search = $this->buildQuery($opts, $entityType)->where('book_id', '=', $bookId)->take(20)->get();
102 $results = $results->merge($search);
105 return $results->sortByDesc('score')->take(20);
109 * Search a chapter for entities.
111 public function searchChapter(int $chapterId, string $searchString): Collection
113 $opts = SearchOptions::fromString($searchString);
114 $pages = $this->buildQuery($opts, 'page')->where('chapter_id', '=', $chapterId)->take(20)->get();
116 return $pages->sortByDesc('score');
120 * Get a page of result data from the given query based on the provided page parameters.
122 protected function getPageOfDataFromQuery(EloquentBuilder $query, string $entityType, int $page = 1, int $count = 20): EloquentCollection
124 $relations = ['tags'];
126 if ($entityType === 'page' || $entityType === 'chapter') {
127 $relations['book'] = function (BelongsTo $query) {
128 $query->scopes('visible');
132 if ($entityType === 'page') {
133 $relations['chapter'] = function (BelongsTo $query) {
134 $query->scopes('visible');
138 return $query->clone()
139 ->with(array_filter($relations))
140 ->skip(($page - 1) * $count)
146 * Create a search query for an entity.
148 protected function buildQuery(SearchOptions $searchOpts, string $entityType): EloquentBuilder
150 $entityModelInstance = $this->entityProvider->get($entityType);
151 $entityQuery = $this->entityQueries->visibleForList($entityType);
153 // Handle normal search terms
154 $this->applyTermSearch($entityQuery, $searchOpts, $entityType);
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 . '%');
164 $exact->negated ? $entityQuery->whereNot($filter) : $entityQuery->where($filter);
167 // Handle tag searches
168 foreach ($searchOpts->tags->all() as $tagOption) {
169 $this->applyTagSearch($entityQuery, $tagOption);
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);
184 * For the given search query, apply the queries for handling the regular search terms.
186 protected function applyTermSearch(EloquentBuilder $entityQuery, SearchOptions $options, string $entityType): void
188 $terms = $options->searches->toValueArray();
189 if (count($terms) === 0) {
193 $scoredTerms = $this->getTermAdjustments($options);
194 $scoreSelect = $this->selectForScoredTerms($scoredTerms);
196 $subQuery = DB::table('search_terms')->select([
199 DB::raw($scoreSelect['statement']),
202 $subQuery->addBinding($scoreSelect['bindings'], 'select');
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 . '%');
211 $subQuery->groupBy('entity_type', 'entity_id');
213 $entityQuery->joinSub($subQuery, 's', 'id', '=', 'entity_id');
214 $entityQuery->addSelect('s.score');
215 $entityQuery->orderBy('score', 'desc');
219 * Create a select statement, with prepared bindings, for the given
220 * set of scored search terms.
222 * @param array<string, float> $scoredTerms
224 * @return array{statement: string, bindings: string[]}
226 protected function selectForScoredTerms(array $scoredTerms): array
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).
234 foreach ($scoredTerms as $term => $score) {
235 $ifChain = 'IF(term like ?, score * ' . (float) $score . ', ' . $ifChain . ')';
236 $bindings[] = $term . '%';
240 'statement' => 'SUM(' . $ifChain . ') as score',
241 'bindings' => array_reverse($bindings),
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.
250 * @return array<string, float>
252 protected function getTermAdjustments(SearchOptions $options): array
254 if (isset($this->termAdjustmentCache[$options])) {
255 return $this->termAdjustmentCache[$options];
258 $termQuery = SearchTerm::query()->toBase();
259 $whenStatements = [];
262 foreach ($options->searches->toValueArray() as $term) {
263 $whenStatements[] = 'WHEN term LIKE ? THEN ?';
264 $whenBindings[] = $term . '%';
265 $whenBindings[] = $term;
267 $termQuery->orWhere('term', 'like', $term . '%');
270 $case = 'CASE ' . implode(' ', $whenStatements) . ' END';
271 $termQuery->selectRaw($case . ' as term', $whenBindings);
272 $termQuery->selectRaw('COUNT(*) as count');
273 $termQuery->groupByRaw($case, $whenBindings);
275 $termCounts = $termQuery->pluck('count', 'term')->toArray();
276 $adjusted = $this->rawTermCountsToAdjustments($termCounts);
278 $this->termAdjustmentCache[$options] = $adjusted;
280 return $this->termAdjustmentCache[$options];
284 * Convert counts of terms into a relative-count normalised multiplier.
286 * @param array<string, int> $termCounts
288 * @return array<string, int>
290 protected function rawTermCountsToAdjustments(array $termCounts): array
292 if (empty($termCounts)) {
297 $max = max(array_values($termCounts));
299 foreach ($termCounts as $term => $count) {
300 $percent = round($count / $max, 5);
301 $multipliers[$term] = 1.3 - $percent;
308 * Apply a tag search term onto an entity query.
310 protected function applyTagSearch(EloquentBuilder $query, TagSearchOption $option): void
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']);
319 if (!empty($tagParts['name'])) {
320 $query->where('name', '=', $tagParts['name']);
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']));
334 $query->where('value', $tagParts['operator'], $tagParts['value']);
338 $option->negated ? $query->whereDoesntHave('tags', $filter) : $query->whereHas('tags', $filter);
341 protected function applyNegatableWhere(EloquentBuilder $query, bool $negated, string $column, string $operator, mixed $value): void
344 $query->whereNot($column, $operator, $value);
346 $query->where($column, $operator, $value);
351 * Custom entity search filters.
353 protected function filterUpdatedAfter(EloquentBuilder $query, Entity $model, string $input, bool $negated): void
355 $date = date_create($input);
356 $this->applyNegatableWhere($query, $negated, 'updated_at', '>=', $date);
359 protected function filterUpdatedBefore(EloquentBuilder $query, Entity $model, string $input, bool $negated): void
361 $date = date_create($input);
362 $this->applyNegatableWhere($query, $negated, 'updated_at', '<', $date);
365 protected function filterCreatedAfter(EloquentBuilder $query, Entity $model, string $input, bool $negated): void
367 $date = date_create($input);
368 $this->applyNegatableWhere($query, $negated, 'created_at', '>=', $date);
371 protected function filterCreatedBefore(EloquentBuilder $query, Entity $model, string $input, bool $negated)
373 $date = date_create($input);
374 $this->applyNegatableWhere($query, $negated, 'created_at', '<', $date);
377 protected function filterCreatedBy(EloquentBuilder $query, Entity $model, string $input, bool $negated)
379 $userSlug = $input === 'me' ? user()->slug : trim($input);
380 $user = User::query()->where('slug', '=', $userSlug)->first(['id']);
382 $this->applyNegatableWhere($query, $negated, 'created_by', '=', $user->id);
386 protected function filterUpdatedBy(EloquentBuilder $query, Entity $model, string $input, bool $negated)
388 $userSlug = $input === 'me' ? user()->slug : trim($input);
389 $user = User::query()->where('slug', '=', $userSlug)->first(['id']);
391 $this->applyNegatableWhere($query, $negated, 'updated_by', '=', $user->id);
395 protected function filterOwnedBy(EloquentBuilder $query, Entity $model, string $input, bool $negated)
397 $userSlug = $input === 'me' ? user()->slug : trim($input);
398 $user = User::query()->where('slug', '=', $userSlug)->first(['id']);
400 $this->applyNegatableWhere($query, $negated, 'owned_by', '=', $user->id);
404 protected function filterInName(EloquentBuilder $query, Entity $model, string $input, bool $negated)
406 $this->applyNegatableWhere($query, $negated, 'name', 'like', '%' . $input . '%');
409 protected function filterInTitle(EloquentBuilder $query, Entity $model, string $input, bool $negated)
411 $this->filterInName($query, $model, $input, $negated);
414 protected function filterInBody(EloquentBuilder $query, Entity $model, string $input, bool $negated)
416 $this->applyNegatableWhere($query, $negated, $model->textField, 'like', '%' . $input . '%');
419 protected function filterIsRestricted(EloquentBuilder $query, Entity $model, string $input, bool $negated)
421 $negated ? $query->whereDoesntHave('permissions') : $query->whereHas('permissions');
424 protected function filterViewedByMe(EloquentBuilder $query, Entity $model, string $input, bool $negated)
426 $filter = function ($query) {
427 $query->where('user_id', '=', user()->id);
430 $negated ? $query->whereDoesntHave('views', $filter) : $query->whereHas('views', $filter);
433 protected function filterNotViewedByMe(EloquentBuilder $query, Entity $model, string $input, bool $negated)
435 $filter = function ($query) {
436 $query->where('user_id', '=', user()->id);
439 $negated ? $query->whereHas('views', $filter) : $query->whereDoesntHave('views', $filter);
442 protected function filterIsTemplate(EloquentBuilder $query, Entity $model, string $input, bool $negated)
444 if ($model instanceof Page) {
445 $this->applyNegatableWhere($query, $negated, 'template', '=', true);
449 protected function filterSortBy(EloquentBuilder $query, Entity $model, string $input, bool $negated)
451 $functionName = Str::camel('sort_by_' . $input);
452 if (method_exists($this, $functionName)) {
453 $this->$functionName($query, $model, $negated);
458 * Sorting filter options.
460 protected function sortByLastCommented(EloquentBuilder $query, Entity $model, bool $negated)
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');
466 $query->join($commentQuery, $model->getTable() . '.id', '=', DB::raw('comments.entity_id'))
467 ->orderBy('last_commented', $negated ? 'asc' : 'desc');