Sets things up to allow more complex terms ready to handle negation.
--- /dev/null
+<?php
+
+namespace BookStack\Search;
+
+class SearchOption
+{
+ public function __construct(
+ public string $value,
+ public bool $negated = false,
+ ) {
+ }
+}
--- /dev/null
+<?php
+
+namespace BookStack\Search;
+
+class SearchOptionSet
+{
+ /**
+ * @var SearchOption[]
+ */
+ public 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 $key => $option) {
+ $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_filter($this->options, fn (SearchOption $option) => !empty($option->value));
+ return new self($filteredOptions);
+ }
+
+ public static function fromValueArray(array $values): self
+ {
+ $options = array_map(fn($val) => new SearchOption($val), $values);
+ return new self($options);
+ }
+
+ public static function fromMapArray(array $values): self
+ {
+ $options = [];
+ foreach ($values as $key => $value) {
+ $options[$key] = new SearchOption($value);
+ }
+ return new self($options);
+ }
+}
class SearchOptions
{
- public array $searches = [];
- public array $exacts = [];
- public array $tags = [];
- public array $filters = [];
+ public SearchOptionSet $searches;
+ public SearchOptionSet $exacts;
+ public SearchOptionSet $tags;
+ 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;
}
$inputs = $request->only(['search', 'types', 'filters', 'exact', 'tags']);
$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']));
+ $instance->exacts = SearchOptionSet::fromValueArray(array_filter($parsedStandardTerms['exacts']));
+ $instance->exacts = $instance->exacts->merge(SearchOptionSet::fromValueArray($inputExacts));
+ $instance->tags = SearchOptionSet::fromValueArray(array_filter($inputs['tags'] ?? []));
+ $keyedFilters = [];
foreach (($inputs['filters'] ?? []) as $filterKey => $filterVal) {
if (empty($filterVal)) {
continue;
}
- $instance->filters[$filterKey] = $filterVal === 'true' ? '' : $filterVal;
+ $cleanedFilterVal = $filterVal === 'true' ? '' : $filterVal;
+ $keyedFilters[$filterKey] = new SearchOption($cleanedFilterVal);
}
if (isset($inputs['types']) && count($inputs['types']) < 4) {
- $instance->filters['type'] = implode('|', $inputs['types']);
+ $keyedFilters['type'] = new SearchOption(implode('|', $inputs['types']));
}
+ $instance->filters = new SearchOptionSet($keyedFilters);
+
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, string[]> $terms */
$terms = [
- 'searches' => [],
'exacts' => [],
'tags' => [],
'filters' => [],
}
// Unescape exacts and backslash escapes
- foreach ($terms['exacts'] as $index => $exact) {
- $terms['exacts'][$index] = static::decodeEscapes($exact);
- }
+ $escapedExacts = array_map(fn(string $term) => static::decodeEscapes($term), $terms['exacts']);
// 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']))
+ ->filterEmpty();
+ $this->exacts = $this->exacts
+ ->merge(SearchOptionSet::fromValueArray($escapedExacts))
+ ->merge(SearchOptionSet::fromValueArray($parsedStandardTerms['exacts']))
+ ->filterEmpty();
+
+ // Add tags
+ $this->tags = $this->tags->merge(SearchOptionSet::fromValueArray($terms['tags']));
// Split filter values out
+ /** @var array<string, SearchOption> $splitFilters */
$splitFilters = [];
foreach ($terms['filters'] as $filter) {
$explodedFilter = explode(':', $filter, 2);
- $splitFilters[$explodedFilter[0]] = (count($explodedFilter) > 1) ? $explodedFilter[1] : '';
+ $filterValue = (count($explodedFilter) > 1) ? $explodedFilter[1] : '';
+ $splitFilters[$explodedFilter[0]] = new SearchOption($filterValue);
}
- $terms['filters'] = $splitFilters;
-
- // Filter down terms where required
- $terms['exacts'] = array_filter($terms['exacts']);
- $terms['searches'] = array_filter($terms['searches']);
-
- return $terms;
+ $this->filters = $this->filters->merge(new SearchOptionSet($splitFilters));
}
/**
*/
public function setFilter(string $filterName, string $filterValue = ''): void
{
- $this->filters[$filterName] = $filterValue;
+ $this->filters = $this->filters->merge(
+ new SearchOptionSet([$filterName => new SearchOption($filterValue)])
+ );
}
/**
*/
public function toString(): string
{
- $parts = $this->searches;
+ $parts = $this->searches->toValueArray();
- foreach ($this->exacts as $term) {
+ foreach ($this->exacts->toValueArray() as $term) {
$escaped = str_replace('\\', '\\\\', $term);
$escaped = str_replace('"', '\"', $escaped);
$parts[] = '"' . $escaped . '"';
}
- foreach ($this->tags as $term) {
+ foreach ($this->tags->toValueArray() as $term) {
$parts[] = "[{$term}]";
}
- foreach ($this->filters as $filterName => $filterVal) {
+ foreach ($this->filters->toValueMap() as $filterName => $filterVal) {
$parts[] = '{' . $filterName . ($filterVal ? ':' . $filterVal : '') . '}';
}
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.
+ *
+ * @return array<string, string>
+ */
+ public function getHiddenInputValuesByFieldName(): array
+ {
+ $options = [];
+
+ // Non-[created/updated]-by-me options
+ $filterMap = $this->filters->toValueMap();
+ foreach (['updated_by', 'created_by', 'owned_by'] as $filter) {
+ $value = $filterMap[$filter] ?? null;
+ if ($value !== null && $value !== 'me') {
+ $options["filters[$filter]"] = $value;
+ }
+ }
+
+ // TODO - Negated
+
+ return $options;
+ }
}
* 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,
$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();
{
$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) {
$this->applyTermSearch($entityQuery, $searchOpts, $entityType);
// Handle exact term matching
- foreach ($searchOpts->exacts as $inputTerm) {
+ foreach ($searchOpts->exacts->toValueArray() as $inputTerm) {
$entityQuery->where(function (EloquentBuilder $query) use ($inputTerm, $entityModelInstance) {
$inputTerm = str_replace('\\', '\\\\', $inputTerm);
$query->where('name', 'like', '%' . $inputTerm . '%')
}
// Handle tag searches
- foreach ($searchOpts->tags as $inputTerm) {
+ foreach ($searchOpts->tags->toValueArray() as $inputTerm) {
$this->applyTagSearch($entityQuery, $inputTerm);
}
// Handle filters
- foreach ($searchOpts->filters as $filterTerm => $filterValue) {
+ foreach ($searchOpts->filters->toValueMap() as $filterTerm => $filterValue) {
$functionName = Str::camel('filter_' . $filterTerm);
if (method_exists($this, $functionName)) {
$this->$functionName($entityQuery, $entityModelInstance, $filterValue);
*/
protected function applyTermSearch(EloquentBuilder $entityQuery, SearchOptions $options, string $entityType): void
{
- $terms = $options->searches;
+ $terms = $options->searches->toValueArray();
if (count($terms) === 0) {
return;
}
$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');
$whenStatements = [];
$whenBindings = [];
- foreach ($options->searches as $term) {
+ foreach ($options->searches->toValueArray() as $term) {
$whenStatements[] = 'WHEN term LIKE ? THEN ?';
$whenBindings[] = $term . '%';
$whenBindings[] = $term;
<div>
<h5>{{ trans('entities.search_advanced') }}</h5>
+ @php
+ $filterMap = $options->filters->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'])
</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->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->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])
+
+ @foreach($options->getHiddenInputValuesByFieldName() as $fieldName => $value)
+ <input type="hidden" name="{{ $fieldName }}" value="{{ $value }}">
+ @endforeach
<button type="submit" class="button">{{ trans('entities.search_update') }}</button>
</form>
namespace Tests\Entity;
use BookStack\Search\SearchOptions;
+use BookStack\Search\SearchOptionSet;
use Illuminate\Http\Request;
use Tests\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_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}';
$options = new SearchOptions();
- $options->searches = ['cat'];
- $options->exacts = ['dog'];
- $options->tags = ['tag=good'];
- $options->filters = ['is_tree' => ''];
+ $options->searches = SearchOptionSet::fromValueArray(['cat']);
+ $options->exacts = SearchOptionSet::fromValueArray(['dog']);
+ $options->tags = SearchOptionSet::fromValueArray(['tag=good']);
+ $options->filters = SearchOptionSet::fromMapArray(['is_tree' => '']);
$output = $options->toString();
foreach (explode(' ', $expected) as $term) {
public function test_to_string_escapes_as_expected()
{
$options = new SearchOptions();
- $options->exacts = ['"cat"', '""', '"donkey', '"', '\\', '\\"'];
+ $options->exacts = SearchOptionSet::fromValueArray(['"cat"', '""', '"donkey', '"', '\\', '\\"']);
$output = $options->toString();
$this->assertEquals('"\"cat\"" "\"\"" "\"donkey" "\"" "\\\\" "\\\\\""', $output);
'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()
]);
$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());
}
}