diff --git a/app/Search/Options/ExactSearchOption.php b/app/Search/Options/ExactSearchOption.php new file mode 100644 index 000000000..5651fb99b --- /dev/null +++ b/app/Search/Options/ExactSearchOption.php @@ -0,0 +1,13 @@ +value); + $escaped = str_replace('"', '\"', $escaped); + return ($this->negated ? '-' : '') . '"' . $escaped . '"'; + } +} diff --git a/app/Search/Options/FilterSearchOption.php b/app/Search/Options/FilterSearchOption.php new file mode 100644 index 000000000..1f64f4f9e --- /dev/null +++ b/app/Search/Options/FilterSearchOption.php @@ -0,0 +1,37 @@ +name = $name; + } + + public function toString(): string + { + $valueText = ($this->value ? ':' . $this->value : ''); + $filterBrace = '{' . $this->name . $valueText . '}'; + return ($this->negated ? '-' : '') . $filterBrace; + } + + public function getKey(): string + { + return $this->name; + } + + public static function fromContentString(string $value, bool $negated = false): self + { + $explodedFilter = explode(':', $value, 2); + $filterValue = (count($explodedFilter) > 1) ? $explodedFilter[1] : ''; + $filterName = $explodedFilter[0]; + return new self($filterValue, $filterName, $negated); + } +} diff --git a/app/Search/Options/SearchOption.php b/app/Search/Options/SearchOption.php new file mode 100644 index 000000000..483f2123f --- /dev/null +++ b/app/Search/Options/SearchOption.php @@ -0,0 +1,26 @@ +=', '=', '<', '>', 'like', '!=']; + + public function toString(): string + { + return ($this->negated ? '-' : '') . "[{$this->value}]"; + } + + /** + * @return array{name: string, operator: string, value: string} + */ + public function getParts(): array + { + $operatorRegex = implode('|', array_map(fn($op) => preg_quote($op), $this->queryOperators)); + preg_match('/^(.*?)((' . $operatorRegex . ')(.*?))?$/', $this->value, $tagSplit); + + $extractedOperator = count($tagSplit) > 2 ? $tagSplit[3] : ''; + $tagOperator = in_array($extractedOperator, $this->queryOperators) ? $extractedOperator : '='; + $tagValue = count($tagSplit) > 3 ? $tagSplit[4] : ''; + + return [ + 'name' => $tagSplit[1], + 'operator' => $tagOperator, + 'value' => $tagValue, + ]; + } +} diff --git a/app/Search/Options/TermSearchOption.php b/app/Search/Options/TermSearchOption.php new file mode 100644 index 000000000..c78829fc8 --- /dev/null +++ b/app/Search/Options/TermSearchOption.php @@ -0,0 +1,11 @@ +value; + } +} diff --git a/app/Search/SearchOptionSet.php b/app/Search/SearchOptionSet.php new file mode 100644 index 000000000..bd5e5a5b2 --- /dev/null +++ b/app/Search/SearchOptionSet.php @@ -0,0 +1,82 @@ +options = $options; + } + + public function toValueArray(): array + { + return array_map(fn(SearchOption $option) => $option->value, $this->options); + } + + public function toValueMap(): array + { + $map = []; + foreach ($this->options as $index => $option) { + $key = $option->getKey() ?? $index; + $map[$key] = $option->value; + } + return $map; + } + + public function merge(SearchOptionSet $set): self + { + return new self(array_merge($this->options, $set->options)); + } + + public function filterEmpty(): self + { + $filteredOptions = array_values(array_filter($this->options, fn (SearchOption $option) => !empty($option->value))); + return new self($filteredOptions); + } + + /** + * @param class-string $class + */ + public static function fromValueArray(array $values, string $class): self + { + $options = array_map(fn($val) => new $class($val), $values); + return new self($options); + } + + /** + * @return T[] + */ + public function all(): array + { + return $this->options; + } + + /** + * @return self + */ + public function negated(): self + { + $values = array_values(array_filter($this->options, fn (SearchOption $option) => $option->negated)); + return new self($values); + } + + /** + * @return self + */ + public function nonNegated(): self + { + $values = array_values(array_filter($this->options, fn (SearchOption $option) => !$option->negated)); + return new self($values); + } +} diff --git a/app/Search/SearchOptions.php b/app/Search/SearchOptions.php index fffa03db0..7f9db2a64 100644 --- a/app/Search/SearchOptions.php +++ b/app/Search/SearchOptions.php @@ -2,26 +2,39 @@ namespace BookStack\Search; +use BookStack\Search\Options\ExactSearchOption; +use BookStack\Search\Options\FilterSearchOption; +use BookStack\Search\Options\SearchOption; +use BookStack\Search\Options\TagSearchOption; +use BookStack\Search\Options\TermSearchOption; use Illuminate\Http\Request; class SearchOptions { - public array $searches = []; - public array $exacts = []; - public array $tags = []; - public array $filters = []; + /** @var SearchOptionSet */ + public SearchOptionSet $searches; + /** @var SearchOptionSet */ + public SearchOptionSet $exacts; + /** @var SearchOptionSet */ + public SearchOptionSet $tags; + /** @var SearchOptionSet */ + public SearchOptionSet $filters; + + public function __construct() + { + $this->searches = new SearchOptionSet(); + $this->exacts = new SearchOptionSet(); + $this->tags = new SearchOptionSet(); + $this->filters = new SearchOptionSet(); + } /** * Create a new instance from a search string. */ public static function fromString(string $search): self { - $decoded = static::decode($search); - $instance = new SearchOptions(); - foreach ($decoded as $type => $value) { - $instance->$type = $value; - } - + $instance = new self(); + $instance->addOptionsFromString($search); return $instance; } @@ -41,46 +54,64 @@ class SearchOptions } $instance = new SearchOptions(); - $inputs = $request->only(['search', 'types', 'filters', 'exact', 'tags']); + $inputs = $request->only(['search', 'types', 'filters', 'exact', 'tags', 'extras']); $parsedStandardTerms = static::parseStandardTermString($inputs['search'] ?? ''); - $instance->searches = array_filter($parsedStandardTerms['terms']); - $instance->exacts = array_filter($parsedStandardTerms['exacts']); - - array_push($instance->exacts, ...array_filter($inputs['exact'] ?? [])); - - $instance->tags = array_filter($inputs['tags'] ?? []); + $inputExacts = array_filter($inputs['exact'] ?? []); + $instance->searches = SearchOptionSet::fromValueArray(array_filter($parsedStandardTerms['terms']), TermSearchOption::class); + $instance->exacts = SearchOptionSet::fromValueArray(array_filter($parsedStandardTerms['exacts']), ExactSearchOption::class); + $instance->exacts = $instance->exacts->merge(SearchOptionSet::fromValueArray($inputExacts, ExactSearchOption::class)); + $instance->tags = SearchOptionSet::fromValueArray(array_filter($inputs['tags'] ?? []), TagSearchOption::class); + $cleanedFilters = []; foreach (($inputs['filters'] ?? []) as $filterKey => $filterVal) { if (empty($filterVal)) { continue; } - $instance->filters[$filterKey] = $filterVal === 'true' ? '' : $filterVal; + $cleanedFilterVal = $filterVal === 'true' ? '' : $filterVal; + $cleanedFilters[] = new FilterSearchOption($cleanedFilterVal, $filterKey); } if (isset($inputs['types']) && count($inputs['types']) < 4) { - $instance->filters['type'] = implode('|', $inputs['types']); + $cleanedFilters[] = new FilterSearchOption(implode('|', $inputs['types']), 'types'); + } + + $instance->filters = new SearchOptionSet($cleanedFilters); + + // Parse and merge in extras if provided + if (!empty($inputs['extras'])) { + $extras = static::fromString($inputs['extras']); + $instance->searches = $instance->searches->merge($extras->searches); + $instance->exacts = $instance->exacts->merge($extras->exacts); + $instance->tags = $instance->tags->merge($extras->tags); + $instance->filters = $instance->filters->merge($extras->filters); } return $instance; } /** - * Decode a search string into an array of terms. + * Decode a search string and add its contents to this instance. */ - protected static function decode(string $searchString): array + protected function addOptionsFromString(string $searchString): void { + /** @var array $terms */ $terms = [ - 'searches' => [], 'exacts' => [], 'tags' => [], 'filters' => [], ]; $patterns = [ - 'exacts' => '/"((?:\\\\.|[^"\\\\])*)"/', - 'tags' => '/\[(.*?)\]/', - 'filters' => '/\{(.*?)\}/', + 'exacts' => '/-?"((?:\\\\.|[^"\\\\])*)"/', + 'tags' => '/-?\[(.*?)\]/', + 'filters' => '/-?\{(.*?)\}/', + ]; + + $constructors = [ + 'exacts' => fn(string $value, bool $negated) => new ExactSearchOption($value, $negated), + 'tags' => fn(string $value, bool $negated) => new TagSearchOption($value, $negated), + 'filters' => fn(string $value, bool $negated) => FilterSearchOption::fromContentString($value, $negated), ]; // Parse special terms @@ -88,34 +119,32 @@ class SearchOptions $matches = []; preg_match_all($pattern, $searchString, $matches); if (count($matches) > 0) { - $terms[$termType] = $matches[1]; + foreach ($matches[1] as $index => $value) { + $negated = str_starts_with($matches[0][$index], '-'); + $terms[$termType][] = $constructors[$termType]($value, $negated); + } $searchString = preg_replace($pattern, '', $searchString); } } // Unescape exacts and backslash escapes - foreach ($terms['exacts'] as $index => $exact) { - $terms['exacts'][$index] = static::decodeEscapes($exact); + foreach ($terms['exacts'] as $exact) { + $exact->value = static::decodeEscapes($exact->value); } // Parse standard terms $parsedStandardTerms = static::parseStandardTermString($searchString); - array_push($terms['searches'], ...$parsedStandardTerms['terms']); - array_push($terms['exacts'], ...$parsedStandardTerms['exacts']); + $this->searches = $this->searches + ->merge(SearchOptionSet::fromValueArray($parsedStandardTerms['terms'], TermSearchOption::class)) + ->filterEmpty(); + $this->exacts = $this->exacts + ->merge(new SearchOptionSet($terms['exacts'])) + ->merge(SearchOptionSet::fromValueArray($parsedStandardTerms['exacts'], ExactSearchOption::class)) + ->filterEmpty(); - // Split filter values out - $splitFilters = []; - foreach ($terms['filters'] as $filter) { - $explodedFilter = explode(':', $filter, 2); - $splitFilters[$explodedFilter[0]] = (count($explodedFilter) > 1) ? $explodedFilter[1] : ''; - } - $terms['filters'] = $splitFilters; - - // Filter down terms where required - $terms['exacts'] = array_filter($terms['exacts']); - $terms['searches'] = array_filter($terms['searches']); - - return $terms; + // Add tags & filters + $this->tags = $this->tags->merge(new SearchOptionSet($terms['tags'])); + $this->filters = $this->filters->merge(new SearchOptionSet($terms['filters'])); } /** @@ -175,7 +204,9 @@ class SearchOptions */ public function setFilter(string $filterName, string $filterValue = ''): void { - $this->filters[$filterName] = $filterValue; + $this->filters = $this->filters->merge( + new SearchOptionSet([new FilterSearchOption($filterValue, $filterName)]) + ); } /** @@ -183,22 +214,40 @@ class SearchOptions */ public function toString(): string { - $parts = $this->searches; + $options = [ + ...$this->searches->all(), + ...$this->exacts->all(), + ...$this->tags->all(), + ...$this->filters->all(), + ]; - foreach ($this->exacts as $term) { - $escaped = str_replace('\\', '\\\\', $term); - $escaped = str_replace('"', '\"', $escaped); - $parts[] = '"' . $escaped . '"'; - } - - foreach ($this->tags as $term) { - $parts[] = "[{$term}]"; - } - - foreach ($this->filters as $filterName => $filterVal) { - $parts[] = '{' . $filterName . ($filterVal ? ':' . $filterVal : '') . '}'; - } + $parts = array_map(fn(SearchOption $o) => $o->toString(), $options); return implode(' ', $parts); } + + /** + * Get the search options that don't have UI controls provided for. + * Provided back as a key => value array with the keys being expected + * input names for a search form, and values being the option value. + */ + public function getAdditionalOptionsString(): string + { + $options = []; + + // Non-[created/updated]-by-me options + $userFilters = ['updated_by', 'created_by', 'owned_by']; + foreach ($this->filters->all() as $filter) { + if (in_array($filter->getKey(), $userFilters, true) && $filter->value !== null && $filter->value !== 'me') { + $options[] = $filter; + } + } + + // Negated items + array_push($options, ...$this->exacts->negated()->all()); + array_push($options, ...$this->tags->negated()->all()); + array_push($options, ...$this->filters->negated()->all()); + + return implode(' ', array_map(fn(SearchOption $o) => $o->toString(), $options)); + } } diff --git a/app/Search/SearchResultsFormatter.php b/app/Search/SearchResultsFormatter.php index 02a40632e..b06f81e0e 100644 --- a/app/Search/SearchResultsFormatter.php +++ b/app/Search/SearchResultsFormatter.php @@ -25,11 +25,12 @@ class SearchResultsFormatter * Update the given entity model to set attributes used for previews of the item * primarily within search result lists. */ - protected function setSearchPreview(Entity $entity, SearchOptions $options) + protected function setSearchPreview(Entity $entity, SearchOptions $options): void { $textProperty = $entity->textField; $textContent = $entity->$textProperty; - $terms = array_merge($options->exacts, $options->searches); + $relevantSearchOptions = $options->exacts->merge($options->searches); + $terms = $relevantSearchOptions->toValueArray(); $originalContentByNewAttribute = [ 'preview_name' => $entity->name, diff --git a/app/Search/SearchRunner.php b/app/Search/SearchRunner.php index 94518dbf7..265b9eecc 100644 --- a/app/Search/SearchRunner.php +++ b/app/Search/SearchRunner.php @@ -7,6 +7,7 @@ use BookStack\Entities\Models\Entity; use BookStack\Entities\Models\Page; use BookStack\Entities\Queries\EntityQueries; use BookStack\Permissions\PermissionApplicator; +use BookStack\Search\Options\TagSearchOption; use BookStack\Users\Models\User; use Illuminate\Database\Connection; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; @@ -16,31 +17,21 @@ use Illuminate\Database\Query\Builder; use Illuminate\Support\Collection; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; -use SplObjectStorage; +use WeakMap; class SearchRunner { - /** - * Acceptable operators to be used in a query. - * - * @var string[] - */ - protected array $queryOperators = ['<=', '>=', '=', '<', '>', 'like', '!=']; - /** * Retain a cache of score adjusted terms for specific search options. - * From PHP>=8 this can be made into a WeakMap instead. - * - * @var SplObjectStorage */ - protected $termAdjustmentCache; + protected WeakMap $termAdjustmentCache; public function __construct( protected EntityProvider $entityProvider, protected PermissionApplicator $permissions, protected EntityQueries $entityQueries, ) { - $this->termAdjustmentCache = new SplObjectStorage(); + $this->termAdjustmentCache = new WeakMap(); } /** @@ -55,10 +46,11 @@ class SearchRunner $entityTypes = array_keys($this->entityProvider->all()); $entityTypesToSearch = $entityTypes; + $filterMap = $searchOpts->filters->toValueMap(); if ($entityType !== 'all') { $entityTypesToSearch = [$entityType]; - } elseif (isset($searchOpts->filters['type'])) { - $entityTypesToSearch = explode('|', $searchOpts->filters['type']); + } elseif (isset($filterMap['type'])) { + $entityTypesToSearch = explode('|', $filterMap['type']); } $results = collect(); @@ -97,7 +89,8 @@ class SearchRunner { $opts = SearchOptions::fromString($searchString); $entityTypes = ['page', 'chapter']; - $entityTypesToSearch = isset($opts->filters['type']) ? explode('|', $opts->filters['type']) : $entityTypes; + $filterMap = $opts->filters->toValueMap(); + $entityTypesToSearch = isset($filterMap['type']) ? explode('|', $filterMap['type']) : $entityTypes; $results = collect(); foreach ($entityTypesToSearch as $entityType) { @@ -161,24 +154,26 @@ class SearchRunner $this->applyTermSearch($entityQuery, $searchOpts, $entityType); // Handle exact term matching - foreach ($searchOpts->exacts as $inputTerm) { - $entityQuery->where(function (EloquentBuilder $query) use ($inputTerm, $entityModelInstance) { - $inputTerm = str_replace('\\', '\\\\', $inputTerm); + foreach ($searchOpts->exacts->all() as $exact) { + $filter = function (EloquentBuilder $query) use ($exact, $entityModelInstance) { + $inputTerm = str_replace('\\', '\\\\', $exact->value); $query->where('name', 'like', '%' . $inputTerm . '%') ->orWhere($entityModelInstance->textField, 'like', '%' . $inputTerm . '%'); - }); + }; + + $exact->negated ? $entityQuery->whereNot($filter) : $entityQuery->where($filter); } // Handle tag searches - foreach ($searchOpts->tags as $inputTerm) { - $this->applyTagSearch($entityQuery, $inputTerm); + foreach ($searchOpts->tags->all() as $tagOption) { + $this->applyTagSearch($entityQuery, $tagOption); } // Handle filters - foreach ($searchOpts->filters as $filterTerm => $filterValue) { - $functionName = Str::camel('filter_' . $filterTerm); + foreach ($searchOpts->filters->all() as $filterOption) { + $functionName = Str::camel('filter_' . $filterOption->getKey()); if (method_exists($this, $functionName)) { - $this->$functionName($entityQuery, $entityModelInstance, $filterValue); + $this->$functionName($entityQuery, $entityModelInstance, $filterOption->value, $filterOption->negated); } } @@ -190,7 +185,7 @@ class SearchRunner */ protected function applyTermSearch(EloquentBuilder $entityQuery, SearchOptions $options, string $entityType): void { - $terms = $options->searches; + $terms = $options->searches->toValueArray(); if (count($terms) === 0) { return; } @@ -209,8 +204,8 @@ class SearchRunner $subQuery->where('entity_type', '=', $entityType); $subQuery->where(function (Builder $query) use ($terms) { foreach ($terms as $inputTerm) { - $inputTerm = str_replace('\\', '\\\\', $inputTerm); - $query->orWhere('term', 'like', $inputTerm . '%'); + $escapedTerm = str_replace('\\', '\\\\', $inputTerm); + $query->orWhere('term', 'like', $escapedTerm . '%'); } }); $subQuery->groupBy('entity_type', 'entity_id'); @@ -264,7 +259,7 @@ class SearchRunner $whenStatements = []; $whenBindings = []; - foreach ($options->searches as $term) { + foreach ($options->searches->toValueArray() as $term) { $whenStatements[] = 'WHEN term LIKE ? THEN ?'; $whenBindings[] = $term . '%'; $whenBindings[] = $term; @@ -310,179 +305,165 @@ class SearchRunner } /** - * Get the available query operators as a regex escaped list. + * Apply a tag search term onto an entity query. */ - protected function getRegexEscapedOperators(): string + protected function applyTagSearch(EloquentBuilder $query, TagSearchOption $option): void { - $escapedOperators = []; - foreach ($this->queryOperators as $operator) { - $escapedOperators[] = preg_quote($operator); - } + $filter = function (EloquentBuilder $query) use ($option): void { + $tagParts = $option->getParts(); + if (empty($tagParts['operator']) || empty($tagParts['value'])) { + $query->where('name', '=', $tagParts['name']); + return; + } - return implode('|', $escapedOperators); + if (!empty($tagParts['name'])) { + $query->where('name', '=', $tagParts['name']); + } + + if (is_numeric($tagParts['value']) && $tagParts['operator'] !== 'like') { + // We have to do a raw sql query for this since otherwise PDO will quote the value and MySQL will + // search the value as a string which prevents being able to do number-based operations + // on the tag values. We ensure it has a numeric value and then cast it just to be sure. + /** @var Connection $connection */ + $connection = $query->getConnection(); + $quotedValue = (float) trim($connection->getPdo()->quote($tagParts['value']), "'"); + $query->whereRaw("value {$tagParts['operator']} {$quotedValue}"); + } else if ($tagParts['operator'] === 'like') { + $query->where('value', $tagParts['operator'], str_replace('\\', '\\\\', $tagParts['value'])); + } else { + $query->where('value', $tagParts['operator'], $tagParts['value']); + } + }; + + $option->negated ? $query->whereDoesntHave('tags', $filter) : $query->whereHas('tags', $filter); } - /** - * Apply a tag search term onto a entity query. - */ - protected function applyTagSearch(EloquentBuilder $query, string $tagTerm): EloquentBuilder + protected function applyNegatableWhere(EloquentBuilder $query, bool $negated, string $column, string $operator, mixed $value): void { - preg_match('/^(.*?)((' . $this->getRegexEscapedOperators() . ')(.*?))?$/', $tagTerm, $tagSplit); - $query->whereHas('tags', function (EloquentBuilder $query) use ($tagSplit) { - $tagName = $tagSplit[1]; - $tagOperator = count($tagSplit) > 2 ? $tagSplit[3] : ''; - $tagValue = count($tagSplit) > 3 ? $tagSplit[4] : ''; - $validOperator = in_array($tagOperator, $this->queryOperators); - if (!empty($tagOperator) && !empty($tagValue) && $validOperator) { - if (!empty($tagName)) { - $query->where('name', '=', $tagName); - } - if (is_numeric($tagValue) && $tagOperator !== 'like') { - // We have to do a raw sql query for this since otherwise PDO will quote the value and MySQL will - // search the value as a string which prevents being able to do number-based operations - // on the tag values. We ensure it has a numeric value and then cast it just to be sure. - /** @var Connection $connection */ - $connection = $query->getConnection(); - $tagValue = (float) trim($connection->getPdo()->quote($tagValue), "'"); - $query->whereRaw("value {$tagOperator} {$tagValue}"); - } else { - if ($tagOperator === 'like') { - $tagValue = str_replace('\\', '\\\\', $tagValue); - } - $query->where('value', $tagOperator, $tagValue); - } - } else { - $query->where('name', '=', $tagName); - } - }); - - return $query; + if ($negated) { + $query->whereNot($column, $operator, $value); + } else { + $query->where($column, $operator, $value); + } } /** * Custom entity search filters. */ - protected function filterUpdatedAfter(EloquentBuilder $query, Entity $model, $input): void + protected function filterUpdatedAfter(EloquentBuilder $query, Entity $model, string $input, bool $negated): void { - try { - $date = date_create($input); - $query->where('updated_at', '>=', $date); - } catch (\Exception $e) { - } + $date = date_create($input); + $this->applyNegatableWhere($query, $negated, 'updated_at', '>=', $date); } - protected function filterUpdatedBefore(EloquentBuilder $query, Entity $model, $input): void + protected function filterUpdatedBefore(EloquentBuilder $query, Entity $model, string $input, bool $negated): void { - try { - $date = date_create($input); - $query->where('updated_at', '<', $date); - } catch (\Exception $e) { - } + $date = date_create($input); + $this->applyNegatableWhere($query, $negated, 'updated_at', '<', $date); } - protected function filterCreatedAfter(EloquentBuilder $query, Entity $model, $input): void + protected function filterCreatedAfter(EloquentBuilder $query, Entity $model, string $input, bool $negated): void { - try { - $date = date_create($input); - $query->where('created_at', '>=', $date); - } catch (\Exception $e) { - } + $date = date_create($input); + $this->applyNegatableWhere($query, $negated, 'created_at', '>=', $date); } - protected function filterCreatedBefore(EloquentBuilder $query, Entity $model, $input) + protected function filterCreatedBefore(EloquentBuilder $query, Entity $model, string $input, bool $negated) { - try { - $date = date_create($input); - $query->where('created_at', '<', $date); - } catch (\Exception $e) { - } + $date = date_create($input); + $this->applyNegatableWhere($query, $negated, 'created_at', '<', $date); } - protected function filterCreatedBy(EloquentBuilder $query, Entity $model, $input) + protected function filterCreatedBy(EloquentBuilder $query, Entity $model, string $input, bool $negated) { $userSlug = $input === 'me' ? user()->slug : trim($input); $user = User::query()->where('slug', '=', $userSlug)->first(['id']); if ($user) { - $query->where('created_by', '=', $user->id); + $this->applyNegatableWhere($query, $negated, 'created_by', '=', $user->id); } } - protected function filterUpdatedBy(EloquentBuilder $query, Entity $model, $input) + protected function filterUpdatedBy(EloquentBuilder $query, Entity $model, string $input, bool $negated) { $userSlug = $input === 'me' ? user()->slug : trim($input); $user = User::query()->where('slug', '=', $userSlug)->first(['id']); if ($user) { - $query->where('updated_by', '=', $user->id); + $this->applyNegatableWhere($query, $negated, 'updated_by', '=', $user->id); } } - protected function filterOwnedBy(EloquentBuilder $query, Entity $model, $input) + protected function filterOwnedBy(EloquentBuilder $query, Entity $model, string $input, bool $negated) { $userSlug = $input === 'me' ? user()->slug : trim($input); $user = User::query()->where('slug', '=', $userSlug)->first(['id']); if ($user) { - $query->where('owned_by', '=', $user->id); + $this->applyNegatableWhere($query, $negated, 'owned_by', '=', $user->id); } } - protected function filterInName(EloquentBuilder $query, Entity $model, $input) + protected function filterInName(EloquentBuilder $query, Entity $model, string $input, bool $negated) { - $query->where('name', 'like', '%' . $input . '%'); + $this->applyNegatableWhere($query, $negated, 'name', 'like', '%' . $input . '%'); } - protected function filterInTitle(EloquentBuilder $query, Entity $model, $input) + protected function filterInTitle(EloquentBuilder $query, Entity $model, string $input, bool $negated) { - $this->filterInName($query, $model, $input); + $this->filterInName($query, $model, $input, $negated); } - protected function filterInBody(EloquentBuilder $query, Entity $model, $input) + protected function filterInBody(EloquentBuilder $query, Entity $model, string $input, bool $negated) { - $query->where($model->textField, 'like', '%' . $input . '%'); + $this->applyNegatableWhere($query, $negated, $model->textField, 'like', '%' . $input . '%'); } - protected function filterIsRestricted(EloquentBuilder $query, Entity $model, $input) + protected function filterIsRestricted(EloquentBuilder $query, Entity $model, string $input, bool $negated) { - $query->whereHas('permissions'); + $negated ? $query->whereDoesntHave('permissions') : $query->whereHas('permissions'); } - protected function filterViewedByMe(EloquentBuilder $query, Entity $model, $input) + protected function filterViewedByMe(EloquentBuilder $query, Entity $model, string $input, bool $negated) { - $query->whereHas('views', function ($query) { + $filter = function ($query) { $query->where('user_id', '=', user()->id); - }); + }; + + $negated ? $query->whereDoesntHave('views', $filter) : $query->whereHas('views', $filter); } - protected function filterNotViewedByMe(EloquentBuilder $query, Entity $model, $input) + protected function filterNotViewedByMe(EloquentBuilder $query, Entity $model, string $input, bool $negated) { - $query->whereDoesntHave('views', function ($query) { + $filter = function ($query) { $query->where('user_id', '=', user()->id); - }); + }; + + $negated ? $query->whereHas('views', $filter) : $query->whereDoesntHave('views', $filter); } - protected function filterIsTemplate(EloquentBuilder $query, Entity $model, $input) + protected function filterIsTemplate(EloquentBuilder $query, Entity $model, string $input, bool $negated) { if ($model instanceof Page) { - $query->where('template', '=', true); + $this->applyNegatableWhere($query, $negated, 'template', '=', true); } } - protected function filterSortBy(EloquentBuilder $query, Entity $model, $input) + protected function filterSortBy(EloquentBuilder $query, Entity $model, string $input, bool $negated) { $functionName = Str::camel('sort_by_' . $input); if (method_exists($this, $functionName)) { - $this->$functionName($query, $model); + $this->$functionName($query, $model, $negated); } } /** * Sorting filter options. */ - protected function sortByLastCommented(EloquentBuilder $query, Entity $model) + protected function sortByLastCommented(EloquentBuilder $query, Entity $model, bool $negated) { $commentsTable = DB::getTablePrefix() . 'comments'; $morphClass = str_replace('\\', '\\\\', $model->getMorphClass()); $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'); - $query->join($commentQuery, $model->getTable() . '.id', '=', 'comments.entity_id')->orderBy('last_commented', 'desc'); + $query->join($commentQuery, $model->getTable() . '.id', '=', 'comments.entity_id') + ->orderBy('last_commented', $negated ? 'asc' : 'desc'); } } diff --git a/resources/views/search/all.blade.php b/resources/views/search/all.blade.php index 2d410dbd1..ad437604b 100644 --- a/resources/views/search/all.blade.php +++ b/resources/views/search/all.blade.php @@ -8,66 +8,63 @@
{{ trans('entities.search_advanced') }}
+ @php + $filterMap = $options->filters->nonNegated()->toValueMap(); + @endphp
{{ trans('entities.search_terms') }}
- +
{{ trans('entities.search_content_type') }}
filters['type'] ?? ''); + $types = explode('|', $filterMap['type'] ?? ''); $hasTypes = $types[0] !== ''; ?> @include('search.parts.type-filter', ['checked' => !$hasTypes || in_array('page', $types), 'entity' => 'page', 'transKey' => 'page']) @include('search.parts.type-filter', ['checked' => !$hasTypes || in_array('chapter', $types), 'entity' => 'chapter', 'transKey' => 'chapter'])
- @include('search.parts.type-filter', ['checked' => !$hasTypes || in_array('book', $types), 'entity' => 'book', 'transKey' => 'book']) - @include('search.parts.type-filter', ['checked' => !$hasTypes || in_array('bookshelf', $types), 'entity' => 'bookshelf', 'transKey' => 'shelf']) + @include('search.parts.type-filter', ['checked' => !$hasTypes || in_array('book', $types), 'entity' => 'book', 'transKey' => 'book']) + @include('search.parts.type-filter', ['checked' => !$hasTypes || in_array('bookshelf', $types), 'entity' => 'bookshelf', 'transKey' => 'shelf'])
{{ trans('entities.search_exact_matches') }}
- @include('search.parts.term-list', ['type' => 'exact', 'currentList' => $options->exacts]) + @include('search.parts.term-list', ['type' => 'exact', 'currentList' => $options->exacts->nonNegated()->toValueArray()])
{{ trans('entities.search_tags') }}
- @include('search.parts.term-list', ['type' => 'tags', 'currentList' => $options->tags]) + @include('search.parts.term-list', ['type' => 'tags', 'currentList' => $options->tags->nonNegated()->toValueArray()]) @if(!user()->isGuest())
{{ trans('entities.search_options') }}
- @component('search.parts.boolean-filter', ['filters' => $options->filters, 'name' => 'viewed_by_me', 'value' => null]) + @component('search.parts.boolean-filter', ['filters' => $filterMap, 'name' => 'viewed_by_me', 'value' => null]) {{ trans('entities.search_viewed_by_me') }} @endcomponent - @component('search.parts.boolean-filter', ['filters' => $options->filters, 'name' => 'not_viewed_by_me', 'value' => null]) + @component('search.parts.boolean-filter', ['filters' => $filterMap, 'name' => 'not_viewed_by_me', 'value' => null]) {{ trans('entities.search_not_viewed_by_me') }} @endcomponent - @component('search.parts.boolean-filter', ['filters' => $options->filters, 'name' => 'is_restricted', 'value' => null]) + @component('search.parts.boolean-filter', ['filters' => $filterMap, 'name' => 'is_restricted', 'value' => null]) {{ trans('entities.search_permissions_set') }} @endcomponent - @component('search.parts.boolean-filter', ['filters' => $options->filters, 'name' => 'created_by', 'value' => 'me']) + @component('search.parts.boolean-filter', ['filters' => $filterMap, 'name' => 'created_by', 'value' => 'me']) {{ trans('entities.search_created_by_me') }} @endcomponent - @component('search.parts.boolean-filter', ['filters' => $options->filters, 'name' => 'updated_by', 'value' => 'me']) + @component('search.parts.boolean-filter', ['filters' => $filterMap, 'name' => 'updated_by', 'value' => 'me']) {{ trans('entities.search_updated_by_me') }} @endcomponent - @component('search.parts.boolean-filter', ['filters' => $options->filters, 'name' => 'owned_by', 'value' => 'me']) + @component('search.parts.boolean-filter', ['filters' => $filterMap, 'name' => 'owned_by', 'value' => 'me']) {{ trans('entities.search_owned_by_me') }} @endcomponent @endif
{{ trans('entities.search_date_options') }}
- @include('search.parts.date-filter', ['name' => 'updated_after', 'filters' => $options->filters]) - @include('search.parts.date-filter', ['name' => 'updated_before', 'filters' => $options->filters]) - @include('search.parts.date-filter', ['name' => 'created_after', 'filters' => $options->filters]) - @include('search.parts.date-filter', ['name' => 'created_before', 'filters' => $options->filters]) - - @if(isset($options->filters['created_by']) && $options->filters['created_by'] !== "me") - - @endif - @if(isset($options->filters['updated_by']) && $options->filters['updated_by'] !== "me") - - @endif + @include('search.parts.date-filter', ['name' => 'updated_after', 'filters' => $filterMap]) + @include('search.parts.date-filter', ['name' => 'updated_before', 'filters' => $filterMap]) + @include('search.parts.date-filter', ['name' => 'created_after', 'filters' => $filterMap]) + @include('search.parts.date-filter', ['name' => 'created_before', 'filters' => $filterMap]) +
@@ -77,8 +74,9 @@

{{ trans('entities.search_results') }}

-