add order metatag

not too happy with how this works...but it does work
This commit is contained in:
Daku 2014-01-03 01:24:55 +00:00
parent 9cae856df7
commit 2c2f27ca64
2 changed files with 40 additions and 21 deletions

View File

@ -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
@ -836,6 +844,15 @@ class Image {
$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);
if($stpe->is_querylet_set()) {
@ -1082,23 +1099,12 @@ 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
return $new;

View File

@ -87,6 +87,11 @@
* <ul>
* <li>source=http://example.com -- find all images with "http://example.com" in the source
* </ul>
* <li>order (id, width, height, filesize, filename)_(ASC, DESC), eg
* <ul>
* <li>order=width -- find all images sorted from highest > lowest width
* <li>order=filesize_asc -- find all images sorted from lowest > highest filesize
* </ul>
* </ul>
* <p>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++;
}