make order querylets a first class citizen instead of a hack
This commit is contained in:
parent
19d5cfe8b9
commit
0dca09c230
4 changed files with 25 additions and 33 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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)";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue