Skip to content

Commit 177cfd7

Browse files
committed
Search: Added structure for search term inputs
Sets things up to allow more complex terms ready to handle negation.
1 parent 34ade50 commit 177cfd7

File tree

7 files changed

+195
-87
lines changed

7 files changed

+195
-87
lines changed

app/Search/SearchOption.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace BookStack\Search;
4+
5+
class SearchOption
6+
{
7+
public function __construct(
8+
public string $value,
9+
public bool $negated = false,
10+
) {
11+
}
12+
}

app/Search/SearchOptionSet.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace BookStack\Search;
4+
5+
class SearchOptionSet
6+
{
7+
/**
8+
* @var SearchOption[]
9+
*/
10+
public array $options = [];
11+
12+
public function __construct(array $options = [])
13+
{
14+
$this->options = $options;
15+
}
16+
17+
public function toValueArray(): array
18+
{
19+
return array_map(fn(SearchOption $option) => $option->value, $this->options);
20+
}
21+
22+
public function toValueMap(): array
23+
{
24+
$map = [];
25+
foreach ($this->options as $key => $option) {
26+
$map[$key] = $option->value;
27+
}
28+
return $map;
29+
}
30+
31+
public function merge(SearchOptionSet $set): self
32+
{
33+
return new self(array_merge($this->options, $set->options));
34+
}
35+
36+
public function filterEmpty(): self
37+
{
38+
$filteredOptions = array_filter($this->options, fn (SearchOption $option) => !empty($option->value));
39+
return new self($filteredOptions);
40+
}
41+
42+
public static function fromValueArray(array $values): self
43+
{
44+
$options = array_map(fn($val) => new SearchOption($val), $values);
45+
return new self($options);
46+
}
47+
48+
public static function fromMapArray(array $values): self
49+
{
50+
$options = [];
51+
foreach ($values as $key => $value) {
52+
$options[$key] = new SearchOption($value);
53+
}
54+
return new self($options);
55+
}
56+
}

app/Search/SearchOptions.php

Lines changed: 75 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,26 @@
66

77
class SearchOptions
88
{
9-
public array $searches = [];
10-
public array $exacts = [];
11-
public array $tags = [];
12-
public array $filters = [];
9+
public SearchOptionSet $searches;
10+
public SearchOptionSet $exacts;
11+
public SearchOptionSet $tags;
12+
public SearchOptionSet $filters;
13+
14+
public function __construct()
15+
{
16+
$this->searches = new SearchOptionSet();
17+
$this->exacts = new SearchOptionSet();
18+
$this->tags = new SearchOptionSet();
19+
$this->filters = new SearchOptionSet();
20+
}
1321

1422
/**
1523
* Create a new instance from a search string.
1624
*/
1725
public static function fromString(string $search): self
1826
{
19-
$decoded = static::decode($search);
20-
$instance = new SearchOptions();
21-
foreach ($decoded as $type => $value) {
22-
$instance->$type = $value;
23-
}
24-
27+
$instance = new self();
28+
$instance->addOptionsFromString($search);
2529
return $instance;
2630
}
2731

@@ -44,34 +48,37 @@ public static function fromRequest(Request $request): self
4448
$inputs = $request->only(['search', 'types', 'filters', 'exact', 'tags']);
4549

4650
$parsedStandardTerms = static::parseStandardTermString($inputs['search'] ?? '');
47-
$instance->searches = array_filter($parsedStandardTerms['terms']);
48-
$instance->exacts = array_filter($parsedStandardTerms['exacts']);
49-
50-
array_push($instance->exacts, ...array_filter($inputs['exact'] ?? []));
51-
52-
$instance->tags = array_filter($inputs['tags'] ?? []);
51+
$inputExacts = array_filter($inputs['exact'] ?? []);
52+
$instance->searches = SearchOptionSet::fromValueArray(array_filter($parsedStandardTerms['terms']));
53+
$instance->exacts = SearchOptionSet::fromValueArray(array_filter($parsedStandardTerms['exacts']));
54+
$instance->exacts = $instance->exacts->merge(SearchOptionSet::fromValueArray($inputExacts));
55+
$instance->tags = SearchOptionSet::fromValueArray(array_filter($inputs['tags'] ?? []));
5356

57+
$keyedFilters = [];
5458
foreach (($inputs['filters'] ?? []) as $filterKey => $filterVal) {
5559
if (empty($filterVal)) {
5660
continue;
5761
}
58-
$instance->filters[$filterKey] = $filterVal === 'true' ? '' : $filterVal;
62+
$cleanedFilterVal = $filterVal === 'true' ? '' : $filterVal;
63+
$keyedFilters[$filterKey] = new SearchOption($cleanedFilterVal);
5964
}
6065

6166
if (isset($inputs['types']) && count($inputs['types']) < 4) {
62-
$instance->filters['type'] = implode('|', $inputs['types']);
67+
$keyedFilters['type'] = new SearchOption(implode('|', $inputs['types']));
6368
}
6469

70+
$instance->filters = new SearchOptionSet($keyedFilters);
71+
6572
return $instance;
6673
}
6774

6875
/**
69-
* Decode a search string into an array of terms.
76+
* Decode a search string and add its contents to this instance.
7077
*/
71-
protected static function decode(string $searchString): array
78+
protected function addOptionsFromString(string $searchString): void
7279
{
80+
/** @var array<string, string[]> $terms */
7381
$terms = [
74-
'searches' => [],
7582
'exacts' => [],
7683
'tags' => [],
7784
'filters' => [],
@@ -94,28 +101,30 @@ protected static function decode(string $searchString): array
94101
}
95102

96103
// Unescape exacts and backslash escapes
97-
foreach ($terms['exacts'] as $index => $exact) {
98-
$terms['exacts'][$index] = static::decodeEscapes($exact);
99-
}
104+
$escapedExacts = array_map(fn(string $term) => static::decodeEscapes($term), $terms['exacts']);
100105

101106
// Parse standard terms
102107
$parsedStandardTerms = static::parseStandardTermString($searchString);
103-
array_push($terms['searches'], ...$parsedStandardTerms['terms']);
104-
array_push($terms['exacts'], ...$parsedStandardTerms['exacts']);
108+
$this->searches = $this->searches
109+
->merge(SearchOptionSet::fromValueArray($parsedStandardTerms['terms']))
110+
->filterEmpty();
111+
$this->exacts = $this->exacts
112+
->merge(SearchOptionSet::fromValueArray($escapedExacts))
113+
->merge(SearchOptionSet::fromValueArray($parsedStandardTerms['exacts']))
114+
->filterEmpty();
115+
116+
// Add tags
117+
$this->tags = $this->tags->merge(SearchOptionSet::fromValueArray($terms['tags']));
105118

106119
// Split filter values out
120+
/** @var array<string, SearchOption> $splitFilters */
107121
$splitFilters = [];
108122
foreach ($terms['filters'] as $filter) {
109123
$explodedFilter = explode(':', $filter, 2);
110-
$splitFilters[$explodedFilter[0]] = (count($explodedFilter) > 1) ? $explodedFilter[1] : '';
124+
$filterValue = (count($explodedFilter) > 1) ? $explodedFilter[1] : '';
125+
$splitFilters[$explodedFilter[0]] = new SearchOption($filterValue);
111126
}
112-
$terms['filters'] = $splitFilters;
113-
114-
// Filter down terms where required
115-
$terms['exacts'] = array_filter($terms['exacts']);
116-
$terms['searches'] = array_filter($terms['searches']);
117-
118-
return $terms;
127+
$this->filters = $this->filters->merge(new SearchOptionSet($splitFilters));
119128
}
120129

121130
/**
@@ -175,30 +184,57 @@ protected static function parseStandardTermString(string $termString): array
175184
*/
176185
public function setFilter(string $filterName, string $filterValue = ''): void
177186
{
178-
$this->filters[$filterName] = $filterValue;
187+
$this->filters = $this->filters->merge(
188+
new SearchOptionSet([$filterName => new SearchOption($filterValue)])
189+
);
179190
}
180191

181192
/**
182193
* Encode this instance to a search string.
183194
*/
184195
public function toString(): string
185196
{
186-
$parts = $this->searches;
197+
$parts = $this->searches->toValueArray();
187198

188-
foreach ($this->exacts as $term) {
199+
foreach ($this->exacts->toValueArray() as $term) {
189200
$escaped = str_replace('\\', '\\\\', $term);
190201
$escaped = str_replace('"', '\"', $escaped);
191202
$parts[] = '"' . $escaped . '"';
192203
}
193204

194-
foreach ($this->tags as $term) {
205+
foreach ($this->tags->toValueArray() as $term) {
195206
$parts[] = "[{$term}]";
196207
}
197208

198-
foreach ($this->filters as $filterName => $filterVal) {
209+
foreach ($this->filters->toValueMap() as $filterName => $filterVal) {
199210
$parts[] = '{' . $filterName . ($filterVal ? ':' . $filterVal : '') . '}';
200211
}
201212

202213
return implode(' ', $parts);
203214
}
215+
216+
/**
217+
* Get the search options that don't have UI controls provided for.
218+
* Provided back as a key => value array with the keys being expected
219+
* input names for a search form, and values being the option value.
220+
*
221+
* @return array<string, string>
222+
*/
223+
public function getHiddenInputValuesByFieldName(): array
224+
{
225+
$options = [];
226+
227+
// Non-[created/updated]-by-me options
228+
$filterMap = $this->filters->toValueMap();
229+
foreach (['updated_by', 'created_by', 'owned_by'] as $filter) {
230+
$value = $filterMap[$filter] ?? null;
231+
if ($value !== null && $value !== 'me') {
232+
$options["filters[$filter]"] = $value;
233+
}
234+
}
235+
236+
// TODO - Negated
237+
238+
return $options;
239+
}
204240
}

app/Search/SearchResultsFormatter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ public function format(array $results, SearchOptions $options): void
2525
* Update the given entity model to set attributes used for previews of the item
2626
* primarily within search result lists.
2727
*/
28-
protected function setSearchPreview(Entity $entity, SearchOptions $options)
28+
protected function setSearchPreview(Entity $entity, SearchOptions $options): void
2929
{
3030
$textProperty = $entity->textField;
3131
$textContent = $entity->$textProperty;
32-
$terms = array_merge($options->exacts, $options->searches);
32+
$relevantSearchOptions = $options->exacts->merge($options->searches);
33+
$terms = $relevantSearchOptions->toValueArray();
3334

3435
$originalContentByNewAttribute = [
3536
'preview_name' => $entity->name,

app/Search/SearchRunner.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ public function searchEntities(SearchOptions $searchOpts, string $entityType = '
5555
$entityTypes = array_keys($this->entityProvider->all());
5656
$entityTypesToSearch = $entityTypes;
5757

58+
$filterMap = $searchOpts->filters->toValueMap();
5859
if ($entityType !== 'all') {
5960
$entityTypesToSearch = [$entityType];
60-
} elseif (isset($searchOpts->filters['type'])) {
61-
$entityTypesToSearch = explode('|', $searchOpts->filters['type']);
61+
} elseif (isset($filterMap['type'])) {
62+
$entityTypesToSearch = explode('|', $filterMap['type']);
6263
}
6364

6465
$results = collect();
@@ -97,7 +98,8 @@ public function searchBook(int $bookId, string $searchString): Collection
9798
{
9899
$opts = SearchOptions::fromString($searchString);
99100
$entityTypes = ['page', 'chapter'];
100-
$entityTypesToSearch = isset($opts->filters['type']) ? explode('|', $opts->filters['type']) : $entityTypes;
101+
$filterMap = $opts->filters->toValueMap();
102+
$entityTypesToSearch = isset($filterMap['type']) ? explode('|', $filterMap['type']) : $entityTypes;
101103

102104
$results = collect();
103105
foreach ($entityTypesToSearch as $entityType) {
@@ -161,7 +163,7 @@ protected function buildQuery(SearchOptions $searchOpts, string $entityType): El
161163
$this->applyTermSearch($entityQuery, $searchOpts, $entityType);
162164

163165
// Handle exact term matching
164-
foreach ($searchOpts->exacts as $inputTerm) {
166+
foreach ($searchOpts->exacts->toValueArray() as $inputTerm) {
165167
$entityQuery->where(function (EloquentBuilder $query) use ($inputTerm, $entityModelInstance) {
166168
$inputTerm = str_replace('\\', '\\\\', $inputTerm);
167169
$query->where('name', 'like', '%' . $inputTerm . '%')
@@ -170,12 +172,12 @@ protected function buildQuery(SearchOptions $searchOpts, string $entityType): El
170172
}
171173

172174
// Handle tag searches
173-
foreach ($searchOpts->tags as $inputTerm) {
175+
foreach ($searchOpts->tags->toValueArray() as $inputTerm) {
174176
$this->applyTagSearch($entityQuery, $inputTerm);
175177
}
176178

177179
// Handle filters
178-
foreach ($searchOpts->filters as $filterTerm => $filterValue) {
180+
foreach ($searchOpts->filters->toValueMap() as $filterTerm => $filterValue) {
179181
$functionName = Str::camel('filter_' . $filterTerm);
180182
if (method_exists($this, $functionName)) {
181183
$this->$functionName($entityQuery, $entityModelInstance, $filterValue);
@@ -190,7 +192,7 @@ protected function buildQuery(SearchOptions $searchOpts, string $entityType): El
190192
*/
191193
protected function applyTermSearch(EloquentBuilder $entityQuery, SearchOptions $options, string $entityType): void
192194
{
193-
$terms = $options->searches;
195+
$terms = $options->searches->toValueArray();
194196
if (count($terms) === 0) {
195197
return;
196198
}
@@ -209,8 +211,8 @@ protected function applyTermSearch(EloquentBuilder $entityQuery, SearchOptions $
209211
$subQuery->where('entity_type', '=', $entityType);
210212
$subQuery->where(function (Builder $query) use ($terms) {
211213
foreach ($terms as $inputTerm) {
212-
$inputTerm = str_replace('\\', '\\\\', $inputTerm);
213-
$query->orWhere('term', 'like', $inputTerm . '%');
214+
$escapedTerm = str_replace('\\', '\\\\', $inputTerm);
215+
$query->orWhere('term', 'like', $escapedTerm . '%');
214216
}
215217
});
216218
$subQuery->groupBy('entity_type', 'entity_id');
@@ -264,7 +266,7 @@ protected function getTermAdjustments(SearchOptions $options): array
264266
$whenStatements = [];
265267
$whenBindings = [];
266268

267-
foreach ($options->searches as $term) {
269+
foreach ($options->searches->toValueArray() as $term) {
268270
$whenStatements[] = 'WHEN term LIKE ? THEN ?';
269271
$whenBindings[] = $term . '%';
270272
$whenBindings[] = $term;

0 commit comments

Comments
 (0)