3 namespace BookStack\Entities\Tools;
5 use BookStack\Auth\Permissions\PermissionService;
6 use BookStack\Auth\User;
7 use BookStack\Entities\EntityProvider;
8 use BookStack\Entities\Models\Entity;
9 use BookStack\Entities\Models\Page;
10 use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
11 use Illuminate\Database\Eloquent\Collection as EloquentCollection;
12 use Illuminate\Database\Query\Builder;
13 use Illuminate\Database\Query\JoinClause;
14 use Illuminate\Support\Collection;
15 use Illuminate\Support\Facades\DB;
16 use Illuminate\Support\Str;
23 protected $entityProvider;
26 * @var PermissionService
28 protected $permissionService;
31 * Acceptable operators to be used in a query.
35 protected $queryOperators = ['<=', '>=', '=', '<', '>', 'like', '!='];
37 public function __construct(EntityProvider $entityProvider, PermissionService $permissionService)
39 $this->entityProvider = $entityProvider;
40 $this->permissionService = $permissionService;
44 * Search all entities in the system.
45 * The provided count is for each entity to search,
46 * Total returned could be larger and not guaranteed.
48 public function searchEntities(SearchOptions $searchOpts, string $entityType = 'all', int $page = 1, int $count = 20, string $action = 'view'): array
50 $entityTypes = array_keys($this->entityProvider->all());
51 $entityTypesToSearch = $entityTypes;
53 if ($entityType !== 'all') {
54 $entityTypesToSearch = $entityType;
55 } elseif (isset($searchOpts->filters['type'])) {
56 $entityTypesToSearch = explode('|', $searchOpts->filters['type']);
63 foreach ($entityTypesToSearch as $entityType) {
64 if (!in_array($entityType, $entityTypes)) {
68 $searchQuery = $this->buildQuery($searchOpts, $entityType, $action);
69 $entityTotal = $searchQuery->count();
70 $searchResults = $this->getPageOfDataFromQuery($searchQuery, $page, $count);
72 if ($entityTotal > ($page * $count)) {
76 $total += $entityTotal;
77 $results = $results->merge($searchResults);
82 'count' => count($results),
83 'has_more' => $hasMore,
84 'results' => $results->sortByDesc('score')->values(),
89 * Search a book for entities.
91 public function searchBook(int $bookId, string $searchString): Collection
93 $opts = SearchOptions::fromString($searchString);
94 $entityTypes = ['page', 'chapter'];
95 $entityTypesToSearch = isset($opts->filters['type']) ? explode('|', $opts->filters['type']) : $entityTypes;
98 foreach ($entityTypesToSearch as $entityType) {
99 if (!in_array($entityType, $entityTypes)) {
102 $search = $this->buildQuery($opts, $entityType)->where('book_id', '=', $bookId)->take(20)->get();
103 $results = $results->merge($search);
106 return $results->sortByDesc('score')->take(20);
110 * Search a chapter for entities.
112 public function searchChapter(int $chapterId, string $searchString): Collection
114 $opts = SearchOptions::fromString($searchString);
115 $pages = $this->buildQuery($opts, 'page')->where('chapter_id', '=', $chapterId)->take(20)->get();
117 return $pages->sortByDesc('score');
121 * Get a page of result data from the given query based on the provided page parameters.
123 protected function getPageOfDataFromQuery(EloquentBuilder $query, int $page = 1, int $count = 20): EloquentCollection
125 return $query->clone()
126 ->skip(($page - 1) * $count)
132 * Create a search query for an entity.
134 protected function buildQuery(SearchOptions $searchOpts, string $entityType = 'page', string $action = 'view'): EloquentBuilder
136 $entity = $this->entityProvider->get($entityType);
137 $entityQuery = $entity->newQuery();
139 if ($entity instanceof Page) {
140 $entityQuery->select($entity::$listAttributes);
143 // Handle normal search terms
144 $this->applyTermSearch($entityQuery, $searchOpts->searches, $entity);
146 // Handle exact term matching
147 foreach ($searchOpts->exacts as $inputTerm) {
148 $entityQuery->where(function (EloquentBuilder $query) use ($inputTerm, $entity) {
149 $query->where('name', 'like', '%' . $inputTerm . '%')
150 ->orWhere($entity->textField, 'like', '%' . $inputTerm . '%');
154 // Handle tag searches
155 foreach ($searchOpts->tags as $inputTerm) {
156 $this->applyTagSearch($entityQuery, $inputTerm);
160 foreach ($searchOpts->filters as $filterTerm => $filterValue) {
161 $functionName = Str::camel('filter_' . $filterTerm);
162 if (method_exists($this, $functionName)) {
163 $this->$functionName($entityQuery, $entity, $filterValue);
167 return $this->permissionService->enforceEntityRestrictions($entity, $entityQuery, $action);
171 * For the given search query, apply the queries for handling the regular search terms.
173 protected function applyTermSearch(EloquentBuilder $entityQuery, array $terms, Entity $entity): void
175 if (count($terms) === 0) {
179 $subQuery = DB::table('search_terms')->select([
182 DB::raw('SUM(score) as score'),
185 $subQuery->where('entity_type', '=', $entity->getMorphClass());
187 $subQuery->where(function (Builder $query) use ($terms) {
188 foreach ($terms as $inputTerm) {
189 $query->orWhere('term', 'like', $inputTerm . '%');
191 })->groupBy('entity_type', 'entity_id');
193 $entityQuery->join(DB::raw('(' . $subQuery->toSql() . ') as s'), function (JoinClause $join) {
194 $join->on('id', '=', 'entity_id');
196 ->addSelect(DB::raw('s.score'))
197 ->orderBy('score', 'desc');
199 $entityQuery->mergeBindings($subQuery);
203 * Get the available query operators as a regex escaped list.
205 protected function getRegexEscapedOperators(): string
207 $escapedOperators = [];
208 foreach ($this->queryOperators as $operator) {
209 $escapedOperators[] = preg_quote($operator);
212 return implode('|', $escapedOperators);
216 * Apply a tag search term onto a entity query.
218 protected function applyTagSearch(EloquentBuilder $query, string $tagTerm): EloquentBuilder
220 preg_match('/^(.*?)((' . $this->getRegexEscapedOperators() . ')(.*?))?$/', $tagTerm, $tagSplit);
221 $query->whereHas('tags', function (EloquentBuilder $query) use ($tagSplit) {
222 $tagName = $tagSplit[1];
223 $tagOperator = count($tagSplit) > 2 ? $tagSplit[3] : '';
224 $tagValue = count($tagSplit) > 3 ? $tagSplit[4] : '';
225 $validOperator = in_array($tagOperator, $this->queryOperators);
226 if (!empty($tagOperator) && !empty($tagValue) && $validOperator) {
227 if (!empty($tagName)) {
228 $query->where('name', '=', $tagName);
230 if (is_numeric($tagValue) && $tagOperator !== 'like') {
231 // We have to do a raw sql query for this since otherwise PDO will quote the value and MySQL will
232 // search the value as a string which prevents being able to do number-based operations
233 // on the tag values. We ensure it has a numeric value and then cast it just to be sure.
234 $tagValue = (float) trim($query->getConnection()->getPdo()->quote($tagValue), "'");
235 $query->whereRaw("value ${tagOperator} ${tagValue}");
237 $query->where('value', $tagOperator, $tagValue);
240 $query->where('name', '=', $tagName);
248 * Custom entity search filters.
250 protected function filterUpdatedAfter(EloquentBuilder $query, Entity $model, $input): void
253 $date = date_create($input);
254 $query->where('updated_at', '>=', $date);
255 } catch (\Exception $e) {}
258 protected function filterUpdatedBefore(EloquentBuilder $query, Entity $model, $input): void
261 $date = date_create($input);
262 $query->where('updated_at', '<', $date);
263 } catch (\Exception $e) {}
266 protected function filterCreatedAfter(EloquentBuilder $query, Entity $model, $input): void
269 $date = date_create($input);
270 $query->where('created_at', '>=', $date);
271 } catch (\Exception $e) {}
274 protected function filterCreatedBefore(EloquentBuilder $query, Entity $model, $input)
277 $date = date_create($input);
278 $query->where('created_at', '<', $date);
279 } catch (\Exception $e) {}
282 protected function filterCreatedBy(EloquentBuilder $query, Entity $model, $input)
284 $userSlug = $input === 'me' ? user()->slug : trim($input);
285 $user = User::query()->where('slug', '=', $userSlug)->first(['id']);
287 $query->where('created_by', '=', $user->id);
291 protected function filterUpdatedBy(EloquentBuilder $query, Entity $model, $input)
293 $userSlug = $input === 'me' ? user()->slug : trim($input);
294 $user = User::query()->where('slug', '=', $userSlug)->first(['id']);
296 $query->where('updated_by', '=', $user->id);
300 protected function filterOwnedBy(EloquentBuilder $query, Entity $model, $input)
302 $userSlug = $input === 'me' ? user()->slug : trim($input);
303 $user = User::query()->where('slug', '=', $userSlug)->first(['id']);
305 $query->where('owned_by', '=', $user->id);
309 protected function filterInName(EloquentBuilder $query, Entity $model, $input)
311 $query->where('name', 'like', '%' . $input . '%');
314 protected function filterInTitle(EloquentBuilder $query, Entity $model, $input)
316 $this->filterInName($query, $model, $input);
319 protected function filterInBody(EloquentBuilder $query, Entity $model, $input)
321 $query->where($model->textField, 'like', '%' . $input . '%');
324 protected function filterIsRestricted(EloquentBuilder $query, Entity $model, $input)
326 $query->where('restricted', '=', true);
329 protected function filterViewedByMe(EloquentBuilder $query, Entity $model, $input)
331 $query->whereHas('views', function ($query) {
332 $query->where('user_id', '=', user()->id);
336 protected function filterNotViewedByMe(EloquentBuilder $query, Entity $model, $input)
338 $query->whereDoesntHave('views', function ($query) {
339 $query->where('user_id', '=', user()->id);
343 protected function filterSortBy(EloquentBuilder $query, Entity $model, $input)
345 $functionName = Str::camel('sort_by_' . $input);
346 if (method_exists($this, $functionName)) {
347 $this->$functionName($query, $model);
352 * Sorting filter options.
354 protected function sortByLastCommented(EloquentBuilder $query, Entity $model)
356 $commentsTable = DB::getTablePrefix() . 'comments';
357 $morphClass = str_replace('\\', '\\\\', $model->getMorphClass());
358 $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');
360 $query->join($commentQuery, $model->getTable() . '.id', '=', 'comments.entity_id')->orderBy('last_commented', 'desc');