diff --git a/core/imageboard/image.php b/core/imageboard/image.php index 64ac27ab..3e239206 100644 --- a/core/imageboard/image.php +++ b/core/imageboard/image.php @@ -13,8 +13,6 @@ class Image public const IMAGE_DIR = "images"; public const THUMBNAIL_DIR = "thumbs"; - public static $order_sql = null; // this feels ugly - /** @var null|int */ public $id = null; @@ -162,12 +160,9 @@ class Image } } - $order = (Image::$order_sql ?: "images.".$config->get_string(IndexConfig::ORDER)); - $querylet = Image::build_search_querylet($tags, $order, $limit, $start); + $querylet = Image::build_search_querylet($tags, $limit, $start); $result = $database->get_all_iterable($querylet->sql, $querylet->variables); - Image::$order_sql = null; - return $result; } @@ -284,14 +279,18 @@ class Image $tag_conditions = []; $img_conditions = []; $stpen = 0; // search term parse event number + $order = null; /* * Turn a bunch of strings into a bunch of TagCondition * and ImgCondition objects */ + /** @var $stpe SearchTermParseEvent */ $stpe = send_event(new SearchTermParseEvent($stpen++, null, $terms)); - if ($stpe->is_querylet_set()) { - foreach ($stpe->get_querylets() as $querylet) { + if ($stpe->order) { + $order = $stpe->order; + } elseif ($stpe->querylets) { + foreach ($stpe->querylets as $querylet) { $img_conditions[] = new ImgCondition($querylet, true); } } @@ -306,9 +305,12 @@ class Image continue; } + /** @var $stpe SearchTermParseEvent */ $stpe = send_event(new SearchTermParseEvent($stpen++, $term, $terms)); - if ($stpe->is_querylet_set()) { - foreach ($stpe->get_querylets() as $querylet) { + if ($stpe->order) { + $order = $stpe->order; + } elseif ($stpe->querylets) { + foreach ($stpe->querylets as $querylet) { $img_conditions[] = new ImgCondition($querylet, $positive); } } else { @@ -318,7 +320,7 @@ class Image } } } - return [$tag_conditions, $img_conditions]; + return [$tag_conditions, $img_conditions, $order]; } /* @@ -834,12 +836,14 @@ class Image * #param string[] $terms */ private static function build_search_querylet( - array $tags, - ?string $order=null, + array $terms, ?int $limit=null, ?int $offset=null ): Querylet { - list($tag_conditions, $img_conditions) = self::terms_to_conditions($tags); + global $config; + + list($tag_conditions, $img_conditions, $order) = self::terms_to_conditions($terms); + $order = ($order ?: "images.".$config->get_string(IndexConfig::ORDER)); $positive_tag_count = 0; $negative_tag_count = 0; diff --git a/ext/index/events.php b/ext/index/events.php index 40a1f9b6..a4a8585f 100644 --- a/ext/index/events.php +++ b/ext/index/events.php @@ -8,12 +8,14 @@ class SearchTermParseEvent extends Event { /** @var int */ public $id = 0; - /** @var null|string */ + /** @var null|string */ public $term = null; /** @var string[] */ public $context = []; /** @var Querylet[] */ public $querylets = []; + /** @var null|string */ + public $order = null; public function __construct(int $id, string $term=null, array $context=[]) { @@ -23,16 +25,6 @@ class SearchTermParseEvent extends Event $this->context = $context; } - public function is_querylet_set(): bool - { - return (count($this->querylets) > 0); - } - - public function get_querylets(): array - { - return $this->querylets; - } - public function add_querylet(Querylet $q) { $this->querylets[] = $q; diff --git a/ext/index/main.php b/ext/index/main.php index 2c4a5c1b..b0a30c81 100644 --- a/ext/index/main.php +++ b/ext/index/main.php @@ -235,21 +235,18 @@ class Index extends Extension $ord = strtolower($matches[1]); $default_order_for_column = preg_match("/^(id|filename)$/", $matches[1]) ? "ASC" : "DESC"; $sort = isset($matches[2]) ? strtoupper($matches[2]) : $default_order_for_column; - Image::$order_sql = "images.$ord $sort"; - $event->add_querylet(new Querylet("1=1")); //small hack to avoid metatag being treated as normal tag + $event->order = "images.$ord $sort"; } elseif (preg_match("/^order[=|:]random[_]([0-9]{1,4})$/i", $event->term, $matches)) { // requires a seed to avoid duplicates // since the tag can't be changed during the parseevent, we instead generate the seed during submit using js $seed = $matches[1]; - Image::$order_sql = "RAND($seed)"; - $event->add_querylet(new Querylet("1=1")); //small hack to avoid metatag being treated as normal tag + $event->order = "RAND($seed)"; } elseif (preg_match("/^order[=|:]dailyshuffle$/i", $event->term, $matches)) { // will use today's date as seed, thus allowing for a dynamic randomized list without outside intervention. // This way the list will change every day, giving a more dynamic feel to the imageboard. // recommended to change homepage to "post/list/order:dailyshuffle/1" $seed = date("Ymd"); - Image::$order_sql = "RAND($seed)"; - $event->add_querylet(new Querylet("1=1")); + $event->order = "RAND($seed)"; } } } diff --git a/ext/numeric_score/main.php b/ext/numeric_score/main.php index 7811f304..e980fb65 100644 --- a/ext/numeric_score/main.php +++ b/ext/numeric_score/main.php @@ -272,8 +272,7 @@ class NumericScore extends Extension } elseif (preg_match("/^order[=|:](?:numeric_)?(score)(?:_(desc|asc))?$/i", $event->term, $matches)) { $default_order_for_column = "DESC"; $sort = isset($matches[2]) ? strtoupper($matches[2]) : $default_order_for_column; - Image::$order_sql = "images.numeric_score $sort"; - $event->add_querylet(new Querylet("1=1")); //small hack to avoid metatag being treated as normal tag + $event->order = "images.numeric_score $sort"; } }