extensible search v1

git-svn-id: file:///home/shish/svn/shimmie2/trunk@713 7f39781d-f577-437e-ae19-be835c7a54ca
This commit is contained in:
shish 2008-02-06 17:24:08 +00:00
parent 0effe70b6f
commit 2948a4435e
4 changed files with 73 additions and 37 deletions

View File

@ -9,12 +9,13 @@ class Querylet {
var $sql; var $sql;
var $variables; var $variables;
public function querylet($sql, $variables=array()) { public function Querylet($sql, $variables=array()) {
$this->sql = $sql; $this->sql = $sql;
$this->variables = $variables; $this->variables = $variables;
} }
public function append($querylet) { public function append($querylet) {
assert(!is_null($querylet));
$this->sql .= $querylet->sql; $this->sql .= $querylet->sql;
$this->variables = array_merge($this->variables, $querylet->variables); $this->variables = array_merge($this->variables, $querylet->variables);
} }
@ -157,41 +158,10 @@ class Database {
$term = $this->resolve_alias($term); $term = $this->resolve_alias($term);
$matches = array(); $stpe = new SearchTermParseEvent($term);
if(preg_match("/size(<|>|<=|>=|=)(\d+)x(\d+)/", $term, $matches)) { send_event($stpe);
$cmp = $matches[1]; if($stpe->is_querylet_set()) {
$args = array(int_escape($matches[2]), int_escape($matches[3])); $img_search->append($stpe->get_querylet());
$img_search->append(new Querylet("AND (width $cmp ? AND height $cmp ?)", $args));
}
else if(preg_match("/ratio(<|>|<=|>=|=)(\d+):(\d+)/", $term, $matches)) {
$cmp = $matches[1];
$args = array(int_escape($matches[2]), int_escape($matches[3]));
$img_search->append(new Querylet("AND (width / height $cmp ? / ?)", $args));
}
else if(preg_match("/(filesize|id)(<|>|<=|>=|=)(\d+[kmg]?b?)/i", $term, $matches)) {
$col = $matches[1];
$cmp = $matches[2];
$val = parse_shorthand_int($matches[3]);
$img_search->append(new Querylet("AND (images.$col $cmp $val)"));
}
else if(preg_match("/(poster|user)=(.*)/i", $term, $matches)) {
global $database;
$user = $database->get_user_by_name($matches[2]);
if(!is_null($user)) {
$user_id = $user->id;
}
else {
$user_id = -1;
}
$img_search->append(new Querylet("AND (images.owner_id = $user_id)"));
}
else if(preg_match("/(hash=|md5:)([0-9a-fA-F]*)/i", $term, $matches)) {
$hash = strtolower($matches[2]);
$img_search->append(new Querylet("AND (images.hash = '$hash')"));
}
else if(preg_match("/(filetype|ext)=([a-zA-Z0-9]*)/i", $term, $matches)) {
$ext = strtolower($matches[2]);
$img_search->append(new Querylet("AND (images.ext = '$ext')"));
} }
else { else {
$term = str_replace("*", "%", $term); $term = str_replace("*", "%", $term);

View File

@ -244,4 +244,31 @@ class ThumbnailGenerationEvent extends Event {
$this->type = $type; $this->type = $type;
} }
} }
/*
* SearchTermParseEvent:
* Signal that a search term needs parsing
*/
class SearchTermParseEvent extends Event {
var $term = null;
var $querylet = null;
public function SearchTermParseEvent($term) {
assert(!is_null($term));
$this->term = $term;
}
public function is_querylet_set() {
return !is_null($this->querylet);
}
public function get_querylet() {
return $this->querylet;
}
public function set_querylet($q) {
$this->querylet = $q;
}
}
?> ?>

View File

@ -79,6 +79,45 @@ class Index extends Extension {
$event->panel->add_block($sb); $event->panel->add_block($sb);
} }
if(is_a($event, 'SearchTermParseEvent')) {
$matches = array();
if(preg_match("/size(<|>|<=|>=|=)(\d+)x(\d+)/", $event->term, $matches)) {
$cmp = $matches[1];
$args = array(int_escape($matches[2]), int_escape($matches[3]));
$event->set_querylet(new Querylet("AND (width $cmp ? AND height $cmp ?)", $args));
}
else if(preg_match("/ratio(<|>|<=|>=|=)(\d+):(\d+)/", $event->term, $matches)) {
$cmp = $matches[1];
$args = array(int_escape($matches[2]), int_escape($matches[3]));
$event->set_querylet(new Querylet("AND (width / height $cmp ? / ?)", $args));
}
else if(preg_match("/(filesize|id)(<|>|<=|>=|=)(\d+[kmg]?b?)/i", $event->term, $matches)) {
$col = $matches[1];
$cmp = $matches[2];
$val = parse_shorthand_int($matches[3]);
$event->set_querylet(new Querylet("AND (images.$col $cmp $val)"));
}
else if(preg_match("/(poster|user)=(.*)/i", $event->term, $matches)) {
global $database;
$user = $database->get_user_by_name($matches[2]);
if(!is_null($user)) {
$user_id = $user->id;
}
else {
$user_id = -1;
}
$event->set_querylet(new Querylet("AND (images.owner_id = $user_id)"));
}
else if(preg_match("/(hash=|md5:)([0-9a-fA-F]*)/i", $event->term, $matches)) {
$hash = strtolower($matches[2]);
$event->set_querylet(new Querylet("AND (images.hash = '$hash')"));
}
else if(preg_match("/(filetype|ext)=([a-zA-Z0-9]*)/i", $event->term, $matches)) {
$ext = strtolower($matches[2]);
$event->set_querylet(new Querylet("AND (images.ext = '$ext')"));
}
}
} }
} }
add_event_listener(new Index()); add_event_listener(new Index());

View File

@ -1,6 +1,6 @@
<?php <?php
// set up and purify the environment // set up and purify the environment
define("DEBUG", false); define("DEBUG", true);
define("VERSION", 'trunk'); define("VERSION", 'trunk');
if(DEBUG) { if(DEBUG) {