make order querylets a first class citizen instead of a hack

This commit is contained in:
Shish 2020-10-25 12:55:13 +00:00
parent 19d5cfe8b9
commit 0dca09c230
4 changed files with 25 additions and 33 deletions

View file

@ -13,8 +13,6 @@ class Image
public const IMAGE_DIR = "images"; public const IMAGE_DIR = "images";
public const THUMBNAIL_DIR = "thumbs"; public const THUMBNAIL_DIR = "thumbs";
public static $order_sql = null; // this feels ugly
/** @var null|int */ /** @var null|int */
public $id = null; 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, $limit, $start);
$querylet = Image::build_search_querylet($tags, $order, $limit, $start);
$result = $database->get_all_iterable($querylet->sql, $querylet->variables); $result = $database->get_all_iterable($querylet->sql, $querylet->variables);
Image::$order_sql = null;
return $result; return $result;
} }
@ -284,14 +279,18 @@ class Image
$tag_conditions = []; $tag_conditions = [];
$img_conditions = []; $img_conditions = [];
$stpen = 0; // search term parse event number $stpen = 0; // search term parse event number
$order = null;
/* /*
* Turn a bunch of strings into a bunch of TagCondition * Turn a bunch of strings into a bunch of TagCondition
* and ImgCondition objects * and ImgCondition objects
*/ */
/** @var $stpe SearchTermParseEvent */
$stpe = send_event(new SearchTermParseEvent($stpen++, null, $terms)); $stpe = send_event(new SearchTermParseEvent($stpen++, null, $terms));
if ($stpe->is_querylet_set()) { if ($stpe->order) {
foreach ($stpe->get_querylets() as $querylet) { $order = $stpe->order;
} elseif ($stpe->querylets) {
foreach ($stpe->querylets as $querylet) {
$img_conditions[] = new ImgCondition($querylet, true); $img_conditions[] = new ImgCondition($querylet, true);
} }
} }
@ -306,9 +305,12 @@ class Image
continue; continue;
} }
/** @var $stpe SearchTermParseEvent */
$stpe = send_event(new SearchTermParseEvent($stpen++, $term, $terms)); $stpe = send_event(new SearchTermParseEvent($stpen++, $term, $terms));
if ($stpe->is_querylet_set()) { if ($stpe->order) {
foreach ($stpe->get_querylets() as $querylet) { $order = $stpe->order;
} elseif ($stpe->querylets) {
foreach ($stpe->querylets as $querylet) {
$img_conditions[] = new ImgCondition($querylet, $positive); $img_conditions[] = new ImgCondition($querylet, $positive);
} }
} else { } 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 * #param string[] $terms
*/ */
private static function build_search_querylet( private static function build_search_querylet(
array $tags, array $terms,
?string $order=null,
?int $limit=null, ?int $limit=null,
?int $offset=null ?int $offset=null
): Querylet { ): 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; $positive_tag_count = 0;
$negative_tag_count = 0; $negative_tag_count = 0;

View file

@ -14,6 +14,8 @@ class SearchTermParseEvent extends Event
public $context = []; public $context = [];
/** @var Querylet[] */ /** @var Querylet[] */
public $querylets = []; public $querylets = [];
/** @var null|string */
public $order = null;
public function __construct(int $id, string $term=null, array $context=[]) public function __construct(int $id, string $term=null, array $context=[])
{ {
@ -23,16 +25,6 @@ class SearchTermParseEvent extends Event
$this->context = $context; $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) public function add_querylet(Querylet $q)
{ {
$this->querylets[] = $q; $this->querylets[] = $q;

View file

@ -235,21 +235,18 @@ class Index extends Extension
$ord = strtolower($matches[1]); $ord = strtolower($matches[1]);
$default_order_for_column = preg_match("/^(id|filename)$/", $matches[1]) ? "ASC" : "DESC"; $default_order_for_column = preg_match("/^(id|filename)$/", $matches[1]) ? "ASC" : "DESC";
$sort = isset($matches[2]) ? strtoupper($matches[2]) : $default_order_for_column; $sort = isset($matches[2]) ? strtoupper($matches[2]) : $default_order_for_column;
Image::$order_sql = "images.$ord $sort"; $event->order = "images.$ord $sort";
$event->add_querylet(new Querylet("1=1")); //small hack to avoid metatag being treated as normal tag
} elseif (preg_match("/^order[=|:]random[_]([0-9]{1,4})$/i", $event->term, $matches)) { } elseif (preg_match("/^order[=|:]random[_]([0-9]{1,4})$/i", $event->term, $matches)) {
// requires a seed to avoid duplicates // 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 // since the tag can't be changed during the parseevent, we instead generate the seed during submit using js
$seed = $matches[1]; $seed = $matches[1];
Image::$order_sql = "RAND($seed)"; $event->order = "RAND($seed)";
$event->add_querylet(new Querylet("1=1")); //small hack to avoid metatag being treated as normal tag
} elseif (preg_match("/^order[=|:]dailyshuffle$/i", $event->term, $matches)) { } 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. // 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. // 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" // recommended to change homepage to "post/list/order:dailyshuffle/1"
$seed = date("Ymd"); $seed = date("Ymd");
Image::$order_sql = "RAND($seed)"; $event->order = "RAND($seed)";
$event->add_querylet(new Querylet("1=1"));
} }
} }
} }

View file

@ -272,8 +272,7 @@ class NumericScore extends Extension
} elseif (preg_match("/^order[=|:](?:numeric_)?(score)(?:_(desc|asc))?$/i", $event->term, $matches)) { } elseif (preg_match("/^order[=|:](?:numeric_)?(score)(?:_(desc|asc))?$/i", $event->term, $matches)) {
$default_order_for_column = "DESC"; $default_order_for_column = "DESC";
$sort = isset($matches[2]) ? strtoupper($matches[2]) : $default_order_for_column; $sort = isset($matches[2]) ? strtoupper($matches[2]) : $default_order_for_column;
Image::$order_sql = "images.numeric_score $sort"; $event->order = "images.numeric_score $sort";
$event->add_querylet(new Querylet("1=1")); //small hack to avoid metatag being treated as normal tag
} }
} }