[various] split Index::onSearchTermParse into relevant extensions

This commit is contained in:
Shish 2024-02-21 21:31:25 +00:00 committed by Shish
parent d3352f1563
commit cff1bbad7d
4 changed files with 64 additions and 52 deletions

View file

@ -162,25 +162,7 @@ class Index extends Extension
}
$matches = [];
// check for tags first as tag based searches are more common.
if (preg_match("/^tags([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) {
$cmp = ltrim($matches[1], ":") ?: "=";
$count = $matches[2];
$event->add_querylet(
new Querylet("EXISTS (
SELECT 1
FROM image_tags it
LEFT JOIN tags t ON it.tag_id = t.id
WHERE images.id = it.image_id
GROUP BY image_id
HAVING COUNT(*) $cmp $count
)")
);
} elseif (preg_match("/^ratio([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+):(\d+)$/i", $event->term, $matches)) {
$cmp = preg_replace('/^:/', '=', $matches[1]);
$args = ["width{$event->id}" => int_escape($matches[2]), "height{$event->id}" => int_escape($matches[3])];
$event->add_querylet(new Querylet("width / :width{$event->id} $cmp height / :height{$event->id}", $args));
} elseif (preg_match("/^filesize([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+[kmg]?b?)$/i", $event->term, $matches)) {
if (preg_match("/^filesize([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+[kmg]?b?)$/i", $event->term, $matches)) {
$cmp = ltrim($matches[1], ":") ?: "=";
$val = parse_shorthand_int($matches[2]);
$event->add_querylet(new Querylet("images.filesize $cmp :val{$event->id}", ["val{$event->id}" => $val]));
@ -201,34 +183,11 @@ class Index extends Extension
} elseif (preg_match("/^(filename|name)[=|:](.+)$/i", $event->term, $matches)) {
$filename = strtolower($matches[2]);
$event->add_querylet(new Querylet("lower(images.filename) LIKE :filename{$event->id}", ["filename{$event->id}" => "%$filename%"]));
} elseif (preg_match("/^(source)[=|:](.*)$/i", $event->term, $matches)) {
$source = strtolower($matches[2]);
if (preg_match("/^(any|none)$/i", $source)) {
$not = ($source == "any" ? "NOT" : "");
$event->add_querylet(new Querylet("images.source IS $not NULL"));
} else {
$event->add_querylet(new Querylet('images.source LIKE :src', ["src" => "%$source%"]));
}
} elseif (preg_match("/^posted([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])([0-9-]*)$/i", $event->term, $matches)) {
// TODO Make this able to search = without needing a time component.
$cmp = ltrim($matches[1], ":") ?: "=";
$val = $matches[2];
$event->add_querylet(new Querylet("images.posted $cmp :posted{$event->id}", ["posted{$event->id}" => $val]));
} elseif (preg_match("/^size([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)x(\d+)$/i", $event->term, $matches)) {
$cmp = ltrim($matches[1], ":") ?: "=";
$args = ["width{$event->id}" => int_escape($matches[2]), "height{$event->id}" => int_escape($matches[3])];
$event->add_querylet(new Querylet("width $cmp :width{$event->id} AND height $cmp :height{$event->id}", $args));
} elseif (preg_match("/^width([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) {
$cmp = ltrim($matches[1], ":") ?: "=";
$event->add_querylet(new Querylet("width $cmp :width{$event->id}", ["width{$event->id}" => int_escape($matches[2])]));
} elseif (preg_match("/^height([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) {
$cmp = ltrim($matches[1], ":") ?: "=";
$event->add_querylet(new Querylet("height $cmp :height{$event->id}", ["height{$event->id}" => int_escape($matches[2])]));
} elseif (preg_match("/^length([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(.+)$/i", $event->term, $matches)) {
$value = parse_to_milliseconds($matches[2]);
$cmp = ltrim($matches[1], ":") ?: "=";
$event->add_querylet(new Querylet("length $cmp :length{$event->id}", ["length{$event->id}" => $value]));
} elseif (preg_match("/^order[=|:](id|width|height|length|filesize|filename)[_]?(desc|asc)?$/i", $event->term, $matches)) {
$ord = strtolower($matches[1]);
$default_order_for_column = preg_match("/^(id|filename)$/", $matches[1]) ? "ASC" : "DESC";

View file

@ -237,8 +237,6 @@ class Media extends Extension
// }
}
public const CONTENT_SEARCH_TERM_REGEX = "/^content[=|:]((video)|(audio)|(image)|(unknown))$/i";
public function onSearchTermParse(SearchTermParseEvent $event): void
{
if (is_null($event->term)) {
@ -246,13 +244,31 @@ class Media extends Extension
}
$matches = [];
if (preg_match(self::CONTENT_SEARCH_TERM_REGEX, $event->term, $matches)) {
if (preg_match("/^content[=|:]((video)|(audio)|(image)|(unknown))$/i", $event->term, $matches)) {
$field = $matches[1];
if ($field === "unknown") {
$event->add_querylet(new Querylet("video IS NULL OR audio IS NULL OR image IS NULL"));
} else {
$event->add_querylet(new Querylet("$field = :true", ["true" => true]));
}
} elseif (preg_match("/^ratio([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+):(\d+)$/i", $event->term, $matches)) {
$cmp = preg_replace('/^:/', '=', $matches[1]);
$args = ["width{$event->id}" => int_escape($matches[2]), "height{$event->id}" => int_escape($matches[3])];
$event->add_querylet(new Querylet("width / :width{$event->id} $cmp height / :height{$event->id}", $args));
} elseif (preg_match("/^size([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)x(\d+)$/i", $event->term, $matches)) {
$cmp = ltrim($matches[1], ":") ?: "=";
$args = ["width{$event->id}" => int_escape($matches[2]), "height{$event->id}" => int_escape($matches[3])];
$event->add_querylet(new Querylet("width $cmp :width{$event->id} AND height $cmp :height{$event->id}", $args));
} elseif (preg_match("/^width([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) {
$cmp = ltrim($matches[1], ":") ?: "=";
$event->add_querylet(new Querylet("width $cmp :width{$event->id}", ["width{$event->id}" => int_escape($matches[2])]));
} elseif (preg_match("/^height([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) {
$cmp = ltrim($matches[1], ":") ?: "=";
$event->add_querylet(new Querylet("height $cmp :height{$event->id}", ["height{$event->id}" => int_escape($matches[2])]));
} elseif (preg_match("/^length([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(.+)$/i", $event->term, $matches)) {
$value = parse_to_milliseconds($matches[2]);
$cmp = ltrim($matches[1], ":") ?: "=";
$event->add_querylet(new Querylet("length $cmp :length{$event->id}", ["length{$event->id}" => $value]));
}
}
@ -266,13 +282,6 @@ class Media extends Extension
}
}
public function onTagTermCheck(TagTermCheckEvent $event): void
{
if (preg_match(self::CONTENT_SEARCH_TERM_REGEX, $event->term)) {
$event->metatag = true;
}
}
public function onParseLinkTemplate(ParseLinkTemplateEvent $event): void
{
if ($event->image->width && $event->image->height && $event->image->length) {

View file

@ -59,6 +59,30 @@ class PostSource extends Extension
$event->add_part($this->theme->get_source_editor_html($event->image), 41);
}
public function onSearchTermParse(SearchTermParseEvent $event): void
{
global $database;
if (is_null($event->term)) {
return;
}
if (preg_match("/^tags([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/i", $event->term, $matches)) {
$cmp = ltrim($matches[1], ":") ?: "=";
$count = $matches[2];
$event->add_querylet(
new Querylet("EXISTS (
SELECT 1
FROM image_tags it
LEFT JOIN tags t ON it.tag_id = t.id
WHERE images.id = it.image_id
GROUP BY image_id
HAVING COUNT(*) $cmp $count
)")
);
}
}
public function onTagTermCheck(TagTermCheckEvent $event): void
{
if (preg_match("/^source[=|:](.*)$/i", $event->term)) {

View file

@ -145,6 +145,26 @@ class PostTags extends Extension
}
}
public function onSearchTermParse(SearchTermParseEvent $event): void
{
global $database;
if (is_null($event->term)) {
return;
}
if (preg_match("/^(source)[=|:](.*)$/i", $event->term, $matches)) {
$source = strtolower($matches[2]);
if (preg_match("/^(any|none)$/i", $source)) {
$not = ($source == "any" ? "NOT" : "");
$event->add_querylet(new Querylet("images.source IS $not NULL"));
} else {
$event->add_querylet(new Querylet('images.source LIKE :src', ["src" => "%$source%"]));
}
}
}
public function onTagSet(TagSetEvent $event): void
{
global $user;