Merge pull request #5239 from BookStackApp/search_negation
Search term negation
This commit is contained in:
		
						commit
						6646dcc24d
					
				| 
						 | 
				
			
			@ -0,0 +1,13 @@
 | 
			
		|||
<?php
 | 
			
		||||
 | 
			
		||||
namespace BookStack\Search\Options;
 | 
			
		||||
 | 
			
		||||
class ExactSearchOption extends SearchOption
 | 
			
		||||
{
 | 
			
		||||
    public function toString(): string
 | 
			
		||||
    {
 | 
			
		||||
        $escaped = str_replace('\\', '\\\\', $this->value);
 | 
			
		||||
        $escaped = str_replace('"', '\"', $escaped);
 | 
			
		||||
        return ($this->negated ? '-' : '') . '"' . $escaped . '"';
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,37 @@
 | 
			
		|||
<?php
 | 
			
		||||
 | 
			
		||||
namespace BookStack\Search\Options;
 | 
			
		||||
 | 
			
		||||
class FilterSearchOption extends SearchOption
 | 
			
		||||
{
 | 
			
		||||
    protected string $name;
 | 
			
		||||
 | 
			
		||||
    public function __construct(
 | 
			
		||||
        string $value,
 | 
			
		||||
        string $name,
 | 
			
		||||
        bool $negated = false,
 | 
			
		||||
    ) {
 | 
			
		||||
        parent::__construct($value, $negated);
 | 
			
		||||
        $this->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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,26 @@
 | 
			
		|||
<?php
 | 
			
		||||
 | 
			
		||||
namespace BookStack\Search\Options;
 | 
			
		||||
 | 
			
		||||
abstract class SearchOption
 | 
			
		||||
{
 | 
			
		||||
    public function __construct(
 | 
			
		||||
        public string $value,
 | 
			
		||||
        public bool $negated = false,
 | 
			
		||||
    ) {
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get the key used for this option when used in a map.
 | 
			
		||||
     * Null indicates to use the index of the containing array.
 | 
			
		||||
     */
 | 
			
		||||
    public function getKey(): string|null
 | 
			
		||||
    {
 | 
			
		||||
        return null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Get the search string representation for this search option.
 | 
			
		||||
     */
 | 
			
		||||
    abstract public function toString(): string;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,37 @@
 | 
			
		|||
<?php
 | 
			
		||||
 | 
			
		||||
namespace BookStack\Search\Options;
 | 
			
		||||
 | 
			
		||||
class TagSearchOption extends SearchOption
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * Acceptable operators to be used within a tag search option.
 | 
			
		||||
     *
 | 
			
		||||
     * @var string[]
 | 
			
		||||
     */
 | 
			
		||||
    protected array $queryOperators = ['<=', '>=', '=', '<', '>', '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,
 | 
			
		||||
        ];
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,11 @@
 | 
			
		|||
<?php
 | 
			
		||||
 | 
			
		||||
namespace BookStack\Search\Options;
 | 
			
		||||
 | 
			
		||||
class TermSearchOption extends SearchOption
 | 
			
		||||
{
 | 
			
		||||
    public function toString(): string
 | 
			
		||||
    {
 | 
			
		||||
        return $this->value;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1,82 @@
 | 
			
		|||
<?php
 | 
			
		||||
 | 
			
		||||
namespace BookStack\Search;
 | 
			
		||||
 | 
			
		||||
use BookStack\Search\Options\SearchOption;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * @template T of SearchOption
 | 
			
		||||
 */
 | 
			
		||||
class SearchOptionSet
 | 
			
		||||
{
 | 
			
		||||
    /**
 | 
			
		||||
     * @var T[]
 | 
			
		||||
     */
 | 
			
		||||
    protected array $options = [];
 | 
			
		||||
 | 
			
		||||
    public function __construct(array $options = [])
 | 
			
		||||
    {
 | 
			
		||||
        $this->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<SearchOption> $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<T>
 | 
			
		||||
     */
 | 
			
		||||
    public function negated(): self
 | 
			
		||||
    {
 | 
			
		||||
        $values = array_values(array_filter($this->options, fn (SearchOption $option) => $option->negated));
 | 
			
		||||
        return new self($values);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * @return self<T>
 | 
			
		||||
     */
 | 
			
		||||
    public function nonNegated(): self
 | 
			
		||||
    {
 | 
			
		||||
        $values = array_values(array_filter($this->options, fn (SearchOption $option) => !$option->negated));
 | 
			
		||||
        return new self($values);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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<TermSearchOption> */
 | 
			
		||||
    public SearchOptionSet $searches;
 | 
			
		||||
    /** @var SearchOptionSet<ExactSearchOption> */
 | 
			
		||||
    public SearchOptionSet $exacts;
 | 
			
		||||
    /** @var SearchOptionSet<TagSearchOption> */
 | 
			
		||||
    public SearchOptionSet $tags;
 | 
			
		||||
    /** @var SearchOptionSet<FilterSearchOption> */
 | 
			
		||||
    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<string, SearchOption[]> $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));
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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');
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -8,66 +8,63 @@
 | 
			
		|||
                <div>
 | 
			
		||||
                    <h5>{{ trans('entities.search_advanced') }}</h5>
 | 
			
		||||
 | 
			
		||||
                    @php
 | 
			
		||||
                        $filterMap = $options->filters->nonNegated()->toValueMap();
 | 
			
		||||
                    @endphp
 | 
			
		||||
                    <form method="get" action="{{ url('/search') }}">
 | 
			
		||||
                        <h6>{{ trans('entities.search_terms') }}</h6>
 | 
			
		||||
                        <input type="text" name="search" value="{{ implode(' ', $options->searches) }}">
 | 
			
		||||
                        <input type="text" name="search" value="{{ implode(' ', $options->searches->toValueArray()) }}">
 | 
			
		||||
 | 
			
		||||
                        <h6>{{ trans('entities.search_content_type') }}</h6>
 | 
			
		||||
                        <div class="form-group">
 | 
			
		||||
 | 
			
		||||
                            <?php
 | 
			
		||||
                            $types = explode('|', $options->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'])
 | 
			
		||||
                            <br>
 | 
			
		||||
                                @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'])
 | 
			
		||||
                        </div>
 | 
			
		||||
 | 
			
		||||
                        <h6>{{ trans('entities.search_exact_matches') }}</h6>
 | 
			
		||||
                        @include('search.parts.term-list', ['type' => 'exact', 'currentList' => $options->exacts])
 | 
			
		||||
                        @include('search.parts.term-list', ['type' => 'exact', 'currentList' => $options->exacts->nonNegated()->toValueArray()])
 | 
			
		||||
 | 
			
		||||
                        <h6>{{ trans('entities.search_tags') }}</h6>
 | 
			
		||||
                        @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())
 | 
			
		||||
                            <h6>{{ trans('entities.search_options') }}</h6>
 | 
			
		||||
 | 
			
		||||
                            @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
 | 
			
		||||
 | 
			
		||||
                        <h6>{{ trans('entities.search_date_options') }}</h6>
 | 
			
		||||
                        @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")
 | 
			
		||||
                            <input type="hidden" name="filters[created_by]" value="{{ $options->filters['created_by'] }}">
 | 
			
		||||
                        @endif
 | 
			
		||||
                        @if(isset($options->filters['updated_by']) && $options->filters['updated_by'] !== "me")
 | 
			
		||||
                            <input type="hidden" name="filters[updated_by]" value="{{ $options->filters['updated_by'] }}">
 | 
			
		||||
                        @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])
 | 
			
		||||
 | 
			
		||||
                        <input type="hidden" name="extras" value="{{ $options->getAdditionalOptionsString() }}">
 | 
			
		||||
                        <button type="submit" class="button">{{ trans('entities.search_update') }}</button>
 | 
			
		||||
                    </form>
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -77,8 +74,9 @@
 | 
			
		|||
                <div class="card content-wrap">
 | 
			
		||||
                    <h1 class="list-heading">{{ trans('entities.search_results') }}</h1>
 | 
			
		||||
 | 
			
		||||
                    <form action="{{ url('/search') }}" method="GET"  class="search-box flexible hide-over-l">
 | 
			
		||||
                        <input value="{{$searchTerm}}" type="text" name="term" placeholder="{{ trans('common.search') }}">
 | 
			
		||||
                    <form action="{{ url('/search') }}" method="GET" class="search-box flexible hide-over-l">
 | 
			
		||||
                        <input value="{{$searchTerm}}" type="text" name="term"
 | 
			
		||||
                               placeholder="{{ trans('common.search') }}">
 | 
			
		||||
                        <button type="submit"
 | 
			
		||||
                                aria-label="{{ trans('common.search') }}"
 | 
			
		||||
                                tabindex="-1">@icon('search')</button>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -118,6 +118,32 @@ class EntitySearchTest extends TestCase
 | 
			
		|||
        $exactSearchB->assertStatus(200)->assertDontSee($page->name);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function test_negated_searches()
 | 
			
		||||
    {
 | 
			
		||||
        $page = $this->entities->newPage(['name' => 'My new test negation page', 'html' => '<p>An angry tortoise wore trumpeted plimsoles</p>']);
 | 
			
		||||
        $page->tags()->saveMany([new Tag(['name' => 'DonkCount', 'value' => '500'])]);
 | 
			
		||||
        $page->created_by = $this->users->admin()->id;
 | 
			
		||||
        $page->save();
 | 
			
		||||
 | 
			
		||||
        $editor = $this->users->editor();
 | 
			
		||||
        $this->actingAs($editor);
 | 
			
		||||
 | 
			
		||||
        $exactSearch = $this->get('/search?term=' . urlencode('negation -"tortoise"'));
 | 
			
		||||
        $exactSearch->assertStatus(200)->assertDontSeeText($page->name);
 | 
			
		||||
 | 
			
		||||
        $tagSearchA = $this->get('/search?term=' . urlencode('negation [DonkCount=500]'));
 | 
			
		||||
        $tagSearchA->assertStatus(200)->assertSeeText($page->name);
 | 
			
		||||
        $tagSearchB = $this->get('/search?term=' . urlencode('negation -[DonkCount=500]'));
 | 
			
		||||
        $tagSearchB->assertStatus(200)->assertDontSeeText($page->name);
 | 
			
		||||
 | 
			
		||||
        $filterSearchA = $this->get('/search?term=' . urlencode('negation -{created_by:me}'));
 | 
			
		||||
        $filterSearchA->assertStatus(200)->assertSeeText($page->name);
 | 
			
		||||
        $page->created_by = $editor->id;
 | 
			
		||||
        $page->save();
 | 
			
		||||
        $filterSearchB = $this->get('/search?term=' . urlencode('negation -{created_by:me}'));
 | 
			
		||||
        $filterSearchB->assertStatus(200)->assertDontSeeText($page->name);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function test_search_terms_with_delimiters_are_converted_to_exact_matches()
 | 
			
		||||
    {
 | 
			
		||||
        $this->asEditor();
 | 
			
		||||
| 
						 | 
				
			
			@ -545,11 +571,18 @@ class EntitySearchTest extends TestCase
 | 
			
		|||
        $search->assertSee($page->getUrl(), false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function test_searches_with_user_filters_adds_them_into_advanced_search_form()
 | 
			
		||||
    public function test_searches_with_terms_without_controls_includes_them_in_extras()
 | 
			
		||||
    {
 | 
			
		||||
        $resp = $this->asEditor()->get('/search?term=' . urlencode('test {updated_by:dan} {created_by:dan}'));
 | 
			
		||||
        $this->withHtml($resp)->assertElementExists('form input[name="filters[updated_by]"][value="dan"]');
 | 
			
		||||
        $this->withHtml($resp)->assertElementExists('form input[name="filters[created_by]"][value="dan"]');
 | 
			
		||||
        $resp = $this->asEditor()->get('/search?term=' . urlencode('test {updated_by:dan} {created_by:dan} -{viewed_by_me} -[a=b] -"dog"'));
 | 
			
		||||
        $this->withHtml($resp)->assertFieldHasValue('extras', '{updated_by:dan} {created_by:dan} -"dog" -[a=b] -{viewed_by_me}');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function test_negated_searches_dont_show_in_inputs()
 | 
			
		||||
    {
 | 
			
		||||
        $resp = $this->asEditor()->get('/search?term=' . urlencode('-{created_by:me} -[a=b] -"dog"'));
 | 
			
		||||
        $this->withHtml($resp)->assertElementNotExists('input[name="tags[]"][value="a=b"]');
 | 
			
		||||
        $this->withHtml($resp)->assertElementNotExists('input[name="exact[]"][value="dog"]');
 | 
			
		||||
        $this->withHtml($resp)->assertElementNotExists('input[name="filters[created_by]"][value="me"][checked="checked"]');
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function test_searches_with_user_filters_using_me_adds_them_into_advanced_search_form()
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,7 +2,12 @@
 | 
			
		|||
 | 
			
		||||
namespace Tests\Entity;
 | 
			
		||||
 | 
			
		||||
use BookStack\Search\Options\ExactSearchOption;
 | 
			
		||||
use BookStack\Search\Options\FilterSearchOption;
 | 
			
		||||
use BookStack\Search\Options\TagSearchOption;
 | 
			
		||||
use BookStack\Search\Options\TermSearchOption;
 | 
			
		||||
use BookStack\Search\SearchOptions;
 | 
			
		||||
use BookStack\Search\SearchOptionSet;
 | 
			
		||||
use Illuminate\Http\Request;
 | 
			
		||||
use Tests\TestCase;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -12,27 +17,57 @@ class SearchOptionsTest extends TestCase
 | 
			
		|||
    {
 | 
			
		||||
        $options = SearchOptions::fromString('cat "dog" [tag=good] {is_tree}');
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals(['cat'], $options->searches);
 | 
			
		||||
        $this->assertEquals(['dog'], $options->exacts);
 | 
			
		||||
        $this->assertEquals(['tag=good'], $options->tags);
 | 
			
		||||
        $this->assertEquals(['is_tree' => ''], $options->filters);
 | 
			
		||||
        $this->assertEquals(['cat'], $options->searches->toValueArray());
 | 
			
		||||
        $this->assertEquals(['dog'], $options->exacts->toValueArray());
 | 
			
		||||
        $this->assertEquals(['tag=good'], $options->tags->toValueArray());
 | 
			
		||||
        $this->assertEquals(['is_tree' => ''], $options->filters->toValueMap());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function test_from_string_parses_negations()
 | 
			
		||||
    {
 | 
			
		||||
        $options = SearchOptions::fromString('cat -"dog" -[tag=good] -{is_tree}');
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals(['cat'], $options->searches->toValueArray());
 | 
			
		||||
        $this->assertTrue($options->exacts->all()[0]->negated);
 | 
			
		||||
        $this->assertTrue($options->tags->all()[0]->negated);
 | 
			
		||||
        $this->assertTrue($options->filters->all()[0]->negated);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function test_from_string_properly_parses_escaped_quotes()
 | 
			
		||||
    {
 | 
			
		||||
        $options = SearchOptions::fromString('"\"cat\"" surprise "\"\"" "\"donkey" "\"" "\\\\"');
 | 
			
		||||
 | 
			
		||||
        $this->assertEquals(['"cat"', '""', '"donkey', '"', '\\'], $options->exacts);
 | 
			
		||||
        $this->assertEquals(['"cat"', '""', '"donkey', '"', '\\'], $options->exacts->toValueArray());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function test_to_string_includes_all_items_in_the_correct_format()
 | 
			
		||||
    {
 | 
			
		||||
        $expected = 'cat "dog" [tag=good] {is_tree}';
 | 
			
		||||
        $expected = 'cat "dog" [tag=good] {is_tree} {beans:valid}';
 | 
			
		||||
        $options = new SearchOptions();
 | 
			
		||||
        $options->searches = ['cat'];
 | 
			
		||||
        $options->exacts = ['dog'];
 | 
			
		||||
        $options->tags = ['tag=good'];
 | 
			
		||||
        $options->filters = ['is_tree' => ''];
 | 
			
		||||
        $options->searches = SearchOptionSet::fromValueArray(['cat'], TermSearchOption::class);
 | 
			
		||||
        $options->exacts = SearchOptionSet::fromValueArray(['dog'], ExactSearchOption::class);
 | 
			
		||||
        $options->tags = SearchOptionSet::fromValueArray(['tag=good'], TagSearchOption::class);
 | 
			
		||||
        $options->filters = new SearchOptionSet([
 | 
			
		||||
            new FilterSearchOption('', 'is_tree'),
 | 
			
		||||
            new FilterSearchOption('valid', 'beans'),
 | 
			
		||||
        ]);
 | 
			
		||||
 | 
			
		||||
        $output = $options->toString();
 | 
			
		||||
        foreach (explode(' ', $expected) as $term) {
 | 
			
		||||
            $this->assertStringContainsString($term, $output);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function test_to_string_handles_negations_as_expected()
 | 
			
		||||
    {
 | 
			
		||||
        $expected = 'cat -"dog" -[tag=good] -{is_tree}';
 | 
			
		||||
        $options = new SearchOptions();
 | 
			
		||||
        $options->searches = new SearchOptionSet([new TermSearchOption('cat')]);
 | 
			
		||||
        $options->exacts = new SearchOptionSet([new ExactSearchOption('dog', true)]);
 | 
			
		||||
        $options->tags = new SearchOptionSet([new TagSearchOption('tag=good', true)]);
 | 
			
		||||
        $options->filters = new SearchOptionSet([
 | 
			
		||||
            new FilterSearchOption('', 'is_tree', true),
 | 
			
		||||
        ]);
 | 
			
		||||
 | 
			
		||||
        $output = $options->toString();
 | 
			
		||||
        foreach (explode(' ', $expected) as $term) {
 | 
			
		||||
| 
						 | 
				
			
			@ -43,7 +78,7 @@ class SearchOptionsTest extends TestCase
 | 
			
		|||
    public function test_to_string_escapes_as_expected()
 | 
			
		||||
    {
 | 
			
		||||
        $options = new SearchOptions();
 | 
			
		||||
        $options->exacts = ['"cat"', '""', '"donkey', '"', '\\', '\\"'];
 | 
			
		||||
        $options->exacts = SearchOptionSet::fromValueArray(['"cat"', '""', '"donkey', '"', '\\', '\\"'], ExactSearchOption::class);
 | 
			
		||||
 | 
			
		||||
        $output = $options->toString();
 | 
			
		||||
        $this->assertEquals('"\"cat\"" "\"\"" "\"donkey" "\"" "\\\\" "\\\\\""', $output);
 | 
			
		||||
| 
						 | 
				
			
			@ -57,14 +92,14 @@ class SearchOptionsTest extends TestCase
 | 
			
		|||
            'is_tree' => '',
 | 
			
		||||
            'name'    => 'dan',
 | 
			
		||||
            'cat'     => 'happy',
 | 
			
		||||
        ], $opts->filters);
 | 
			
		||||
        ], $opts->filters->toValueMap());
 | 
			
		||||
    }
 | 
			
		||||
    public function test_it_cannot_parse_out_empty_exacts()
 | 
			
		||||
    {
 | 
			
		||||
        $options = SearchOptions::fromString('"" test ""');
 | 
			
		||||
 | 
			
		||||
        $this->assertEmpty($options->exacts);
 | 
			
		||||
        $this->assertCount(1, $options->searches);
 | 
			
		||||
        $this->assertEmpty($options->exacts->toValueArray());
 | 
			
		||||
        $this->assertCount(1, $options->searches->toValueArray());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function test_from_request_properly_parses_exacts_from_search_terms()
 | 
			
		||||
| 
						 | 
				
			
			@ -74,7 +109,24 @@ class SearchOptionsTest extends TestCase
 | 
			
		|||
        ]);
 | 
			
		||||
 | 
			
		||||
        $options = SearchOptions::fromRequest($request);
 | 
			
		||||
        $this->assertEquals(["biscuits"], $options->searches);
 | 
			
		||||
        $this->assertEquals(['"cheese"', '""', '"baked',  'beans"'], $options->exacts);
 | 
			
		||||
        $this->assertEquals(["biscuits"], $options->searches->toValueArray());
 | 
			
		||||
        $this->assertEquals(['"cheese"', '""', '"baked',  'beans"'], $options->exacts->toValueArray());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function test_from_request_properly_parses_out_extras_as_string()
 | 
			
		||||
    {
 | 
			
		||||
        $request = new Request([
 | 
			
		||||
            'search' => '',
 | 
			
		||||
            'tags' => ['a=b'],
 | 
			
		||||
            'extras' => '-[b=c] -{viewed_by_me} -"dino"'
 | 
			
		||||
        ]);
 | 
			
		||||
 | 
			
		||||
        $options = SearchOptions::fromRequest($request);
 | 
			
		||||
        $this->assertCount(2, $options->tags->all());
 | 
			
		||||
        $this->assertEquals('b=c', $options->tags->negated()->all()[0]->value);
 | 
			
		||||
        $this->assertEquals('viewed_by_me', $options->filters->all()[0]->getKey());
 | 
			
		||||
        $this->assertTrue($options->filters->all()[0]->negated);
 | 
			
		||||
        $this->assertEquals('dino', $options->exacts->all()[0]->value);
 | 
			
		||||
        $this->assertTrue($options->exacts->all()[0]->negated);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue