From 2c2f27ca640985d49e3eb5134b53516687e91d80 Mon Sep 17 00:00:00 2001 From: Daku Date: Fri, 3 Jan 2014 01:24:55 +0000 Subject: [PATCH] add order metatag not too happy with how this works...but it does work --- core/imageboard.pack.php | 48 ++++++++++++++++++++++------------------ ext/index/main.php | 13 +++++++++++ 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/core/imageboard.pack.php b/core/imageboard.pack.php index eb34035e..ac4d0ea5 100644 --- a/core/imageboard.pack.php +++ b/core/imageboard.pack.php @@ -26,6 +26,7 @@ $tag_n = 0; // temp hack $_flexihash = null; $_fh_last_opts = null; +$order_sql = null; // this feels ugly require_once "lib/flexihash.php"; @@ -114,7 +115,7 @@ class Image { assert(is_numeric($start)); assert(is_numeric($limit)); assert(is_array($tags)); - global $database, $user; + global $database, $user, $order_sql; $images = array(); @@ -128,13 +129,15 @@ class Image { } $querylet = Image::build_search_querylet($tags); - $querylet->append(new Querylet("ORDER BY images.id DESC LIMIT :limit OFFSET :offset", array("limit"=>$limit, "offset"=>$start))); + $querylet->append(new Querylet($order_sql ?: " ORDER BY images.id DESC")); + $querylet->append(new Querylet(" LIMIT :limit OFFSET :offset", array("limit"=>$limit, "offset"=>$start))); #var_dump($querylet->sql); var_dump($querylet->variables); $result = $database->execute($querylet->sql, $querylet->variables); while($row = $result->fetch()) { $images[] = new Image($row); } + $order_sql = null; return $images; } @@ -663,8 +666,6 @@ class Image { } } - $terms = Tag::resolve_aliases($terms); - // parse the words that are searched for into // various types of querylet foreach($terms as $term) { @@ -677,6 +678,15 @@ class Image { continue; } + $aliases = explode(" ", Tag::resolve_alias($term)); + $found = array_search($term, $aliases); + if($found !== false){ + unset($aliases[$found]); + }else{ + $term = array_shift($aliases); + } + foreach($aliases as $alias) array_push($terms, $alias); + $stpe = new SearchTermParseEvent($term, $terms); send_event($stpe); if($stpe->is_querylet_set()) { @@ -824,8 +834,6 @@ class Image { } } - $terms = Tag::resolve_aliases($terms); - reset($terms); // rewind to first element in array. // turn each term into a specific type of querylet @@ -835,6 +843,15 @@ class Image { $negative = true; $term = substr($term, 1); } + + $aliases = explode(" ", Tag::resolve_alias($term)); + $found = array_search($term, $aliases); + if($found !== false){ + unset($aliases[$found]); + }else{ + $term = array_shift($aliases); + } + foreach($aliases as $alias) array_push($terms, $alias); $stpe = new SearchTermParseEvent($term, $terms); send_event($stpe); @@ -1082,22 +1099,11 @@ class Tag { assert(is_array($tags)); $new = array(); - - $i = 0; - $tag_count = count($tags); - while($i<$tag_count) { - $aliases = explode(' ', Tag::resolve_alias($tags[$i])); - foreach($aliases as $alias){ - if(!in_array($alias, $new)){ - if($tags[$i] == $alias){ - $new[] = $alias; - }elseif(!in_array($alias, $tags)){ - $tags[] = $alias; - $tag_count++; - } - } + foreach($tags as $tag) { + $new_set = explode(' ', Tag::resolve_alias($tag)); + foreach($new_set as $new_one) { + $new[] = $new_one; } - $i++; } $new = array_iunique($new); // remove any duplicate tags diff --git a/ext/index/main.php b/ext/index/main.php index 20820fe2..b2df34f0 100644 --- a/ext/index/main.php +++ b/ext/index/main.php @@ -87,6 +87,11 @@ * + *
  • order (id, width, height, filesize, filename)_(ASC, DESC), eg + * * *

    Search items can be combined to search for images which match both, * or you can stick "-" in front of an item to search for things that don't @@ -320,6 +325,14 @@ class Index extends Extension { $cmp = ltrim($matches[1], ":") ?: "="; $event->add_querylet(new Querylet("height $cmp :height{$this->stpen}",array("height{$this->stpen}"=>int_escape($matches[2])))); } + else if(preg_match("/^order[=|:](id|width|height|filesize|filename)[_]?(desc|asc)?$/i", $event->term, $matches)){ + global $order_sql; + $order = strtolower($matches[1]); + $sort = isset($matches[2]) ? strtoupper($matches[2]) : (preg_match("/^(id|filename)$/", $matches[1]) ? "ASC" : "DESC"); + // $event->add_querylet(new Querylet("ORDER BY images.:order :sort", array("order" => $order, "sort" => $sort))); + $event->add_querylet(new Querylet("1=1")); //small hack to avoid metatag being treated as normal tag + $order_sql = " ORDER BY images.$order $sort"; + } $this->stpen++; }