From cff1bbad7df4e1e8eaca37482a18e390276946ad Mon Sep 17 00:00:00 2001 From: Shish Date: Wed, 21 Feb 2024 21:31:25 +0000 Subject: [PATCH] [various] split Index::onSearchTermParse into relevant extensions --- ext/index/main.php | 43 +--------------------------------------- ext/media/main.php | 29 +++++++++++++++++---------- ext/post_source/main.php | 24 ++++++++++++++++++++++ ext/post_tags/main.php | 20 +++++++++++++++++++ 4 files changed, 64 insertions(+), 52 deletions(-) diff --git a/ext/index/main.php b/ext/index/main.php index f6c5ad90..3bf49f95 100644 --- a/ext/index/main.php +++ b/ext/index/main.php @@ -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"; diff --git a/ext/media/main.php b/ext/media/main.php index aeacad12..06ef0719 100644 --- a/ext/media/main.php +++ b/ext/media/main.php @@ -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) { diff --git a/ext/post_source/main.php b/ext/post_source/main.php index b1808453..a2fed4ef 100644 --- a/ext/post_source/main.php +++ b/ext/post_source/main.php @@ -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)) { diff --git a/ext/post_tags/main.php b/ext/post_tags/main.php index bb569e47..ed332718 100644 --- a/ext/post_tags/main.php +++ b/ext/post_tags/main.php @@ -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;