mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-07-31 15:24:31 +03:00
Search: Added structure for search term inputs
Sets things up to allow more complex terms ready to handle negation.
This commit is contained in:
12
app/Search/SearchOption.php
Normal file
12
app/Search/SearchOption.php
Normal file
@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
namespace BookStack\Search;
|
||||
|
||||
class SearchOption
|
||||
{
|
||||
public function __construct(
|
||||
public string $value,
|
||||
public bool $negated = false,
|
||||
) {
|
||||
}
|
||||
}
|
56
app/Search/SearchOptionSet.php
Normal file
56
app/Search/SearchOptionSet.php
Normal file
@ -0,0 +1,56 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
@ -6,22 +6,26 @@ use Illuminate\Http\Request;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -44,34 +48,37 @@ class SearchOptions
|
||||
$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' => [],
|
||||
@ -94,28 +101,30 @@ class SearchOptions
|
||||
}
|
||||
|
||||
// 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));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -175,7 +184,9 @@ class SearchOptions
|
||||
*/
|
||||
public function setFilter(string $filterName, string $filterValue = ''): void
|
||||
{
|
||||
$this->filters[$filterName] = $filterValue;
|
||||
$this->filters = $this->filters->merge(
|
||||
new SearchOptionSet([$filterName => new SearchOption($filterValue)])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -183,22 +194,47 @@ class SearchOptions
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
|
@ -55,10 +55,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 +98,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,7 +163,7 @@ class SearchRunner
|
||||
$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 . '%')
|
||||
@ -170,12 +172,12 @@ class SearchRunner
|
||||
}
|
||||
|
||||
// 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);
|
||||
@ -190,7 +192,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 +211,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 +266,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;
|
||||
|
Reference in New Issue
Block a user