add support for using : as a metatag seperator + updated docs

This commit is contained in:
Daku 2014-01-02 14:00:24 +00:00
parent 85880804d2
commit 25c286b71f
7 changed files with 61 additions and 49 deletions

View File

@ -476,11 +476,11 @@ class Image {
$this->delete_tags_from_image();
// insert each new tags
foreach($tags as $tag) {
if(preg_match("/^source=(.*)$/i", $tag, $matches)) {
if(preg_match("/^source[=|:](.*)$/i", $tag, $matches)) {
$this->set_source($matches[1]);
continue;
}
if(preg_match("/^pool=(.*)$/i", $tag, $matches)) {
if(preg_match("/^pool[=|:](.*)$/i", $tag, $matches)) {
if(class_exists("Pools")) {
$pls = new Pools();
$pls->add_post_from_tag($matches[1], $this->id);

View File

@ -54,14 +54,6 @@ class DanbooruApi extends Extension {
}
}
public function onSearchTermParse(SearchTermParseEvent $event) {
$matches = array();
if(preg_match("/^md5:([0-9a-fA-F]*)$/i", $event->term, $matches)) {
$hash = strtolower($matches[1]);
$event->add_querylet(new Querylet("images.hash = '$hash'")); // :-O
}
}
// Danbooru API
private function api_danbooru(PageRequestEvent $event)
{

View File

@ -117,12 +117,12 @@ class Favorites extends Extension {
public function onSearchTermParse(SearchTermParseEvent $event) {
$matches = array();
if(preg_match("/favorites(<|>|<=|>=|=)(\d+)/", $event->term, $matches)) {
$cmp = $matches[1];
if(preg_match("/^favorites([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/", $event->term, $matches)) {
$cmp = ltrim($matches[1], ":") ?: "=";
$favorites = $matches[2];
$event->add_querylet(new Querylet("images.id IN (SELECT id FROM images WHERE favorites $cmp $favorites)"));
}
else if(preg_match("/favorited_by=(.*)/i", $event->term, $matches)) {
else if(preg_match("/^favorited_by[=|:](.*)$/i", $event->term, $matches)) {
global $database;
$user = User::by_name($matches[1]);
if(!is_null($user)) {
@ -134,7 +134,7 @@ class Favorites extends Extension {
$event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM user_favorites WHERE user_id = $user_id)"));
}
else if(preg_match("/favorited_by_userno=([0-9]+)/i", $event->term, $matches)) {
else if(preg_match("/^favorited_by_userno[=|:](\d+)$/i", $event->term, $matches)) {
$user_id = int_escape($matches[1]);
$event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM user_favorites WHERE user_id = $user_id)"));
}

View File

@ -38,36 +38,53 @@
* <li>id<20 -- search only the first few images
* <li>id>=500 -- search later images
* </ul>
* <li>user=Username, eg
* <li>user=Username & poster=Username, eg
* <ul>
* <li>user=Shish -- find all of Shish's posts
* <li>poster=Shish -- same as above
* </ul>
* <li>hash=md5sum, eg
* <li>user_id=userID & poster_id=userID, eg
* <ul>
* <li>user_id=2 -- find all posts by user id 2
* <li>poster_id=2 -- same as above
* </ul>
* <li>hash=md5sum & md5=md5sum, eg
* <ul>
* <li>hash=bf5b59173f16b6937a4021713dbfaa72 -- find the "Taiga want up!" image
* <li>md5=bf5b59173f16b6937a4021713dbfaa72 -- same as above
* </ul>
* <li>filetype=type, eg
* <li>filetype=type & ext=type, eg
* <ul>
* <li>filetype=png -- find all PNG images
* <li>ext=png -- same as above
* </ul>
* <li>filename=blah, eg
* <li>filename=blah & name=blah, eg
* <ul>
* <li>filename=kitten -- find all images with "kitten" in the original filename
* <li>name=kitten -- same as above
* </ul>
* <li>posted (=, &lt;, &gt;, &lt;=, &gt;=) date, eg
* <ul>
* <li>posted&gt;=2009-12-25 posted&lt;=2010-01-01 -- find images posted between christmas and new year
* </ul>
* <li>tags (=, &lt;, &gt;, &lt;=, &gt;=) count, eg
* <ul>
* <li>tags=1 -- search for images with only 1 tag
* <li>tags>=10 -- search for images with 10 or more tags
* <li>tags<25 -- search for images with less than 25 tags
* </ul>
* <li>source=url, eg
* <ul>
* <li>source=http://example.com -- find all images with "http://example.com" in the source
* </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
* match it.
* <p>Metatags can be followed by ":" rather than "=" if you prefer.
* <br />I.E: "posted:2014-01-01", "id:>=500" etc.
* <p>Some search methods provided by extensions:
* <ul>
* <li>Danbooru API
* <ul>
* <li>md5:[hash] -- same as "hash=", but the API calls it by a different name
* </ul>
* <li>Numeric Score
* <ul>
* <li>score (=, &lt;, &gt;, &lt;=, &gt;=) number -- seach by score
@ -81,11 +98,14 @@
* <li>Favorites
* <ul>
* <li>favorites (=, &lt;, &gt;, &lt;=, &gt;=) number -- search for images favourited a certain number of times
* <li>favourited_by=Username -- search for a user's choices
* <li>favourited_by=Username -- search for a user's choices by username
* <li>favorited_by_userno=UserID -- search for a user's choice by userID
* </ul>
* <li>Notes
* <ul>
* <li>notes (=, &lt;, &gt;, &lt;=, &gt;=) number -- search by the number of notes an image has
* <li>notes_by=Username -- search for a notes created by username
* <li>notes_by_userno=UserID -- search for a notes created by userID
* </ul>
* </ul>
*/
@ -240,45 +260,45 @@ class Index extends Extension {
public function onSearchTermParse(SearchTermParseEvent $event) {
$matches = array();
// check for tags first as tag based searches are more common.
if(preg_match("/tags(<|>|<=|>=|=)(\d+)/", $event->term, $matches)) {
$cmp = $matches[1];
if(preg_match("/^tags([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)$/", $event->term, $matches)) {
$cmp = ltrim($matches[1], ":") ?: "=";
$tags = $matches[2];
$event->add_querylet(new Querylet('images.id IN (SELECT DISTINCT image_id FROM image_tags GROUP BY image_id HAVING count(image_id) '.$cmp.' '.$tags.')'));
}
else if(preg_match("/^ratio(<|>|<=|>=|=)(\d+):(\d+)$/", $event->term, $matches)) {
$cmp = $matches[1];
else if(preg_match("/^ratio([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+):(\d+)$/", $event->term, $matches)) {
$cmp = preg_replace('/^:/', '=', $matches[1]);
$args = array("width{$this->stpen}"=>int_escape($matches[2]), "height{$this->stpen}"=>int_escape($matches[3]));
$event->add_querylet(new Querylet("width / height $cmp :width{$this->stpen} / :height{$this->stpen}", $args));
}
else if(preg_match("/^(filesize|id)(<|>|<=|>=|=)(\d+[kmg]?b?)$/i", $event->term, $matches)) {
else if(preg_match("/^(filesize|id)([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+[kmg]?b?)$/i", $event->term, $matches)) {
$col = $matches[1];
$cmp = $matches[2];
$cmp = ltrim($matches[2], ":") ?: "=";
$val = parse_shorthand_int($matches[3]);
$event->add_querylet(new Querylet("images.$col $cmp :val{$this->stpen}", array("val{$this->stpen}"=>$val)));
}
else if(preg_match("/^(hash|md5)=([0-9a-fA-F]*)$/i", $event->term, $matches)) {
else if(preg_match("/^(hash|md5)[=|:]([0-9a-fA-F]*)$/i", $event->term, $matches)) {
$hash = strtolower($matches[2]);
$event->add_querylet(new Querylet('images.hash = :hash', array("hash" => $hash)));
}
else if(preg_match("/^(filetype|ext)=([a-zA-Z0-9]*)$/i", $event->term, $matches)) {
else if(preg_match("/^(filetype|ext)[=|:]([a-zA-Z0-9]*)$/i", $event->term, $matches)) {
$ext = strtolower($matches[2]);
$event->add_querylet(new Querylet('images.ext = :ext', array("ext" => $ext)));
}
else if(preg_match("/^(filename|name)=([a-zA-Z0-9]*)$/i", $event->term, $matches)) {
else if(preg_match("/^(filename|name)[=|:]([a-zA-Z0-9]*)$/i", $event->term, $matches)) {
$filename = strtolower($matches[2]);
$event->add_querylet(new Querylet("images.filename LIKE :filename{$this->stpen}", array("filename{$this->stpen}"=>"%$filename%")));
}
else if(preg_match("/^(source)=([a-zA-Z0-9]*)$/i", $event->term, $matches)) {
$filename = strtolower($matches[2]);
$event->add_querylet(new Querylet('images.source LIKE :src', array("src"=>"%$filename%")));
else if(preg_match("/^(source)[=|:]([a-zA-Z0-9]*)$/i", $event->term, $matches)) {
$source = strtolower($matches[2]);
$event->add_querylet(new Querylet('images.source LIKE :src', array("src"=>"%$source%")));
}
else if(preg_match("/^posted(<|>|<=|>=|=)([0-9-]*)$/", $event->term, $matches)) {
$cmp = $matches[1];
else if(preg_match("/^posted([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])([0-9-]*)$/", $event->term, $matches)) {
$cmp = ltrim($matches[1], ":") ?: "=";
$val = $matches[2];
$event->add_querylet(new Querylet("images.posted $cmp :posted{$this->stpen}", array("posted{$this->stpen}"=>$val)));
}
else if(preg_match("/^size(<|>|<=|>=|=)(\d+)x(\d+)$/", $event->term, $matches)) {
$cmp = $matches[1];
else if(preg_match("/^size([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)x(\d+)$/", $event->term, $matches)) {
$cmp = ltrim($matches[1], ":") ?: "=";
$args = array("width{$this->stpen}"=>int_escape($matches[2]), "height{$this->stpen}"=>int_escape($matches[3]));
$event->add_querylet(new Querylet("width $cmp :width{$this->stpen} AND height $cmp :height{$this->stpen}", $args));
}

View File

@ -210,16 +210,16 @@ class Notes extends Extension {
*/
public function onSearchTermParse(SearchTermParseEvent $event) {
$matches = array();
if(preg_match("/note=(.*)/i", $event->term, $matches)) {
if(preg_match("/^note[=|:](.*)$/i", $event->term, $matches)) {
$notes = int_escape($matches[1]);
$event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM notes WHERE note = $notes)"));
}
else if(preg_match("/notes(<|>|<=|>=|=)(\d+)/", $event->term, $matches)) {
$cmp = $matches[1];
else if(preg_match("/^notes([:]?<|[:]?>|[:]?<=|[:]?>=|[:|=])(\d+)%/", $event->term, $matches)) {
$cmp = ltrim($matches[1], ":") ?: "=";
$notes = $matches[2];
$event->add_querylet(new Querylet("images.id IN (SELECT id FROM images WHERE notes $cmp $notes)"));
}
else if(preg_match("/notes_by=(.*)/i", $event->term, $matches)) {
else if(preg_match("/^notes_by[=|:](.*)$/i", $event->term, $matches)) {
global $database;
$user = User::by_name($matches[1]);
if(!is_null($user)) {
@ -231,7 +231,7 @@ class Notes extends Extension {
$event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM notes WHERE user_id = $user_id)"));
}
else if(preg_match("/notes_by_userno=([0-9]+)/i", $event->term, $matches)) {
else if(preg_match("/^notes_by_userno[=|:](\d+)$/i", $event->term, $matches)) {
$user_id = int_escape($matches[1]);
$event->add_querylet(new Querylet("images.id IN (SELECT image_id FROM notes WHERE user_id = $user_id)"));
}

View File

@ -116,7 +116,7 @@ class Ratings extends Extension {
$set = Ratings::privs_to_sql(Ratings::get_user_privs($user));
$event->add_querylet(new Querylet("rating IN ($set)"));
}
if(preg_match("/^rating=(?:([sqeu]+)|(safe|questionable|explicit|unknown))$/D", strtolower($event->term), $matches)) {
if(preg_match("/^rating[=|:](?:([sqeu]+)|(safe|questionable|explicit|unknown))$/D", strtolower($event->term), $matches)) {
$ratings = $matches[1] ? $matches[1] : array($matches[2][0]);
$ratings = array_intersect(str_split($ratings), str_split(Ratings::get_user_privs($user)));
$set = "'" . join("', '", $ratings) . "'";
@ -199,7 +199,7 @@ class Ratings extends Extension {
private function no_rating_query($context) {
foreach($context as $term) {
if(preg_match("/^rating=/", $term)) {
if(preg_match("/^rating[=|:]/", $term)) {
return false;
}
}

View File

@ -309,7 +309,7 @@ class UserPage extends Extension {
global $user;
$matches = array();
if(preg_match("/^(poster|user)=(.*)$/i", $event->term, $matches)) {
if(preg_match("/^(poster|user)[=|:](.*)$/i", $event->term, $matches)) {
$duser = User::by_name($matches[2]);
if(!is_null($duser)) {
$user_id = $duser->id;
@ -319,11 +319,11 @@ class UserPage extends Extension {
}
$event->add_querylet(new Querylet("images.owner_id = $user_id"));
}
else if(preg_match("/^(poster|user)_id=([0-9]+)$/i", $event->term, $matches)) {
else if(preg_match("/^(poster|user)_id[=|:]([0-9]+)$/i", $event->term, $matches)) {
$user_id = int_escape($matches[2]);
$event->add_querylet(new Querylet("images.owner_id = $user_id"));
}
else if($user->can("view_ip") && preg_match("/^(poster|user)_ip=([0-9\.]+)$/i", $event->term, $matches)) {
else if($user->can("view_ip") && preg_match("/^(poster|user)_ip[=|:]([0-9\.]+)$/i", $event->term, $matches)) {
$user_ip = $matches[2]; // FIXME: ip_escape?
$event->add_querylet(new Querylet("images.owner_ip = '$user_ip'"));
}