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;
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();
}
/**
$this->applyTermSearch($entityQuery, $searchOpts, $entityType);
// Handle exact term matching
- foreach ($searchOpts->exacts->toValueArray() 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->toValueArray() as $inputTerm) {
- $this->applyTagSearch($entityQuery, $inputTerm);
+ foreach ($searchOpts->tags->all() as $tagOption) {
+ $this->applyTagSearch($entityQuery, $tagOption);
}
// Handle filters
- foreach ($searchOpts->filters->toValueMap() 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);
}
}
}
/**
- * 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']);
+ }
- /**
- * Apply a tag search term onto a entity query.
- */
- protected function applyTagSearch(EloquentBuilder $query, string $tagTerm): EloquentBuilder
- {
- 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);
- }
+ 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('name', '=', $tagName);
+ $query->where('value', $tagParts['operator'], $tagParts['value']);
}
- });
+ };
- return $query;
+ $option->negated ? $query->whereDoesntHave('tags', $filter) : $query->whereHas('tags', $filter);
+ }
+
+ protected function applyNegatableWhere(EloquentBuilder $query, bool $negated, string $column, string $operator, mixed $value): void
+ {
+ 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');
}
}