1 <?php namespace BookStack\Entities\Tools;
3 use BookStack\Auth\Permissions\PermissionService;
4 use BookStack\Entities\EntityProvider;
5 use BookStack\Entities\Models\Entity;
6 use Illuminate\Database\Connection;
7 use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
8 use Illuminate\Database\Query\Builder;
9 use Illuminate\Database\Query\JoinClause;
10 use Illuminate\Support\Collection;
11 use Illuminate\Support\Str;
19 protected $entityProvider;
27 * @var PermissionService
29 protected $permissionService;
33 * Acceptable operators to be used in a query
36 protected $queryOperators = ['<=', '>=', '=', '<', '>', 'like', '!='];
39 public function __construct(EntityProvider $entityProvider, Connection $db, PermissionService $permissionService)
41 $this->entityProvider = $entityProvider;
43 $this->permissionService = $permissionService;
47 * Search all entities in the system.
48 * The provided count is for each entity to search,
49 * Total returned could can be larger and not guaranteed.
51 public function searchEntities(SearchOptions $searchOpts, string $entityType = 'all', int $page = 1, int $count = 20, string $action = 'view'): array
53 $entityTypes = array_keys($this->entityProvider->all());
54 $entityTypesToSearch = $entityTypes;
56 if ($entityType !== 'all') {
57 $entityTypesToSearch = $entityType;
58 } else if (isset($searchOpts->filters['type'])) {
59 $entityTypesToSearch = explode('|', $searchOpts->filters['type']);
66 foreach ($entityTypesToSearch as $entityType) {
67 if (!in_array($entityType, $entityTypes)) {
70 $search = $this->searchEntityTable($searchOpts, $entityType, $page, $count, $action);
71 $entityTotal = $this->searchEntityTable($searchOpts, $entityType, $page, $count, $action, true);
72 if ($entityTotal > $page * $count) {
75 $total += $entityTotal;
76 $results = $results->merge($search);
81 'count' => count($results),
82 'has_more' => $hasMore,
83 '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->buildEntitySearchQuery($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->buildEntitySearchQuery($opts, 'page')->where('chapter_id', '=', $chapterId)->take(20)->get();
116 return $pages->sortByDesc('score');
120 * Search across a particular entity type.
121 * Setting getCount = true will return the total
122 * matching instead of the items themselves.
123 * @return \Illuminate\Database\Eloquent\Collection|int|static[]
125 protected function searchEntityTable(SearchOptions $searchOpts, string $entityType = 'page', int $page = 1, int $count = 20, string $action = 'view', bool $getCount = false)
127 $query = $this->buildEntitySearchQuery($searchOpts, $entityType, $action);
129 return $query->count();
132 $query = $query->skip(($page-1) * $count)->take($count);
133 return $query->get();
137 * Create a search query for an entity
139 protected function buildEntitySearchQuery(SearchOptions $searchOpts, string $entityType = 'page', string $action = 'view'): EloquentBuilder
141 $entity = $this->entityProvider->get($entityType);
142 $entitySelect = $entity->newQuery();
144 // Handle normal search terms
145 if (count($searchOpts->searches) > 0) {
146 $rawScoreSum = $this->db->raw('SUM(score) as score');
147 $subQuery = $this->db->table('search_terms')->select('entity_id', 'entity_type', $rawScoreSum);
148 $subQuery->where('entity_type', '=', $entity->getMorphClass());
149 $subQuery->where(function (Builder $query) use ($searchOpts) {
150 foreach ($searchOpts->searches as $inputTerm) {
151 $query->orWhere('term', 'like', $inputTerm .'%');
153 })->groupBy('entity_type', 'entity_id');
154 $entitySelect->join($this->db->raw('(' . $subQuery->toSql() . ') as s'), function (JoinClause $join) {
155 $join->on('id', '=', 'entity_id');
156 })->selectRaw($entity->getTable().'.*, s.score')->orderBy('score', 'desc');
157 $entitySelect->mergeBindings($subQuery);
160 // Handle exact term matching
161 foreach ($searchOpts->exacts as $inputTerm) {
162 $entitySelect->where(function (EloquentBuilder $query) use ($inputTerm, $entity) {
163 $query->where('name', 'like', '%'.$inputTerm .'%')
164 ->orWhere($entity->textField, 'like', '%'.$inputTerm .'%');
168 // Handle tag searches
169 foreach ($searchOpts->tags as $inputTerm) {
170 $this->applyTagSearch($entitySelect, $inputTerm);
174 foreach ($searchOpts->filters as $filterTerm => $filterValue) {
175 $functionName = Str::camel('filter_' . $filterTerm);
176 if (method_exists($this, $functionName)) {
177 $this->$functionName($entitySelect, $entity, $filterValue);
181 return $this->permissionService->enforceEntityRestrictions($entityType, $entitySelect, $action);
185 * Get the available query operators as a regex escaped list.
187 protected function getRegexEscapedOperators(): string
189 $escapedOperators = [];
190 foreach ($this->queryOperators as $operator) {
191 $escapedOperators[] = preg_quote($operator);
193 return join('|', $escapedOperators);
197 * Apply a tag search term onto a entity query.
199 protected function applyTagSearch(EloquentBuilder $query, string $tagTerm): EloquentBuilder
201 preg_match("/^(.*?)((".$this->getRegexEscapedOperators().")(.*?))?$/", $tagTerm, $tagSplit);
202 $query->whereHas('tags', function (EloquentBuilder $query) use ($tagSplit) {
203 $tagName = $tagSplit[1];
204 $tagOperator = count($tagSplit) > 2 ? $tagSplit[3] : '';
205 $tagValue = count($tagSplit) > 3 ? $tagSplit[4] : '';
206 $validOperator = in_array($tagOperator, $this->queryOperators);
207 if (!empty($tagOperator) && !empty($tagValue) && $validOperator) {
208 if (!empty($tagName)) {
209 $query->where('name', '=', $tagName);
211 if (is_numeric($tagValue) && $tagOperator !== 'like') {
212 // We have to do a raw sql query for this since otherwise PDO will quote the value and MySQL will
213 // search the value as a string which prevents being able to do number-based operations
214 // on the tag values. We ensure it has a numeric value and then cast it just to be sure.
215 $tagValue = (float) trim($query->getConnection()->getPdo()->quote($tagValue), "'");
216 $query->whereRaw("value ${tagOperator} ${tagValue}");
218 $query->where('value', $tagOperator, $tagValue);
221 $query->where('name', '=', $tagName);
228 * Custom entity search filters
231 protected function filterUpdatedAfter(EloquentBuilder $query, Entity $model, $input)
234 $date = date_create($input);
235 } catch (\Exception $e) {
238 $query->where('updated_at', '>=', $date);
241 protected function filterUpdatedBefore(EloquentBuilder $query, Entity $model, $input)
244 $date = date_create($input);
245 } catch (\Exception $e) {
248 $query->where('updated_at', '<', $date);
251 protected function filterCreatedAfter(EloquentBuilder $query, Entity $model, $input)
254 $date = date_create($input);
255 } catch (\Exception $e) {
258 $query->where('created_at', '>=', $date);
261 protected function filterCreatedBefore(EloquentBuilder $query, Entity $model, $input)
264 $date = date_create($input);
265 } catch (\Exception $e) {
268 $query->where('created_at', '<', $date);
271 protected function filterCreatedBy(EloquentBuilder $query, Entity $model, $input)
273 if (!is_numeric($input) && $input !== 'me') {
276 if ($input === 'me') {
279 $query->where('created_by', '=', $input);
282 protected function filterUpdatedBy(EloquentBuilder $query, Entity $model, $input)
284 if (!is_numeric($input) && $input !== 'me') {
287 if ($input === 'me') {
290 $query->where('updated_by', '=', $input);
293 protected function filterInName(EloquentBuilder $query, Entity $model, $input)
295 $query->where('name', 'like', '%' .$input. '%');
298 protected function filterInTitle(EloquentBuilder $query, Entity $model, $input)
300 $this->filterInName($query, $model, $input);
303 protected function filterInBody(EloquentBuilder $query, Entity $model, $input)
305 $query->where($model->textField, 'like', '%' .$input. '%');
308 protected function filterIsRestricted(EloquentBuilder $query, Entity $model, $input)
310 $query->where('restricted', '=', true);
313 protected function filterViewedByMe(EloquentBuilder $query, Entity $model, $input)
315 $query->whereHas('views', function ($query) {
316 $query->where('user_id', '=', user()->id);
320 protected function filterNotViewedByMe(EloquentBuilder $query, Entity $model, $input)
322 $query->whereDoesntHave('views', function ($query) {
323 $query->where('user_id', '=', user()->id);
327 protected function filterSortBy(EloquentBuilder $query, Entity $model, $input)
329 $functionName = Str::camel('sort_by_' . $input);
330 if (method_exists($this, $functionName)) {
331 $this->$functionName($query, $model);
337 * Sorting filter options
340 protected function sortByLastCommented(EloquentBuilder $query, Entity $model)
342 $commentsTable = $this->db->getTablePrefix() . 'comments';
343 $morphClass = str_replace('\\', '\\\\', $model->getMorphClass());
344 $commentQuery = $this->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');
346 $query->join($commentQuery, $model->getTable() . '.id', '=', 'comments.entity_id')->orderBy('last_commented', 'desc');