wildcard search, and remove the lower() stuff (it scares mysql)
git-svn-id: file:///home/shish/svn/shimmie2/trunk@1066 7f39781d-f577-437e-ae19-be835c7a54ca
This commit is contained in:
parent
bf8f2e847f
commit
40431f4c26
2 changed files with 25 additions and 6 deletions
|
@ -224,6 +224,22 @@ class Database {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function resolve_wildcard($tag) {
|
||||||
|
if(strpos($tag, "%") === false && strpos($tag, "_") === false) {
|
||||||
|
return array($tag);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
$newtags = $this->db->GetCol("SELECT tag FROM tags WHERE tag LIKE ?", array($tag));
|
||||||
|
if(count($newtags) > 0) {
|
||||||
|
$resolved = $newtags;
|
||||||
|
} else {
|
||||||
|
$resolved = array($tag);
|
||||||
|
}
|
||||||
|
return $resolved;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public function sanitise($tag) {
|
public function sanitise($tag) {
|
||||||
assert(is_string($tag));
|
assert(is_string($tag));
|
||||||
$tag = preg_replace("/[\s?*]/", "", $tag);
|
$tag = preg_replace("/[\s?*]/", "", $tag);
|
||||||
|
|
|
@ -231,7 +231,7 @@ class Image {
|
||||||
private static function build_search_querylet(Config $config, Database $database, $terms) {
|
private static function build_search_querylet(Config $config, Database $database, $terms) {
|
||||||
$tag_querylets = array();
|
$tag_querylets = array();
|
||||||
$img_querylets = array();
|
$img_querylets = array();
|
||||||
|
$positive_tag_count = 0;
|
||||||
|
|
||||||
// parse the words that are searched for into
|
// parse the words that are searched for into
|
||||||
// various types of querylet
|
// various types of querylet
|
||||||
|
@ -253,7 +253,11 @@ class Image {
|
||||||
$term = str_replace("*", "%", $term);
|
$term = str_replace("*", "%", $term);
|
||||||
$term = str_replace("?", "_", $term);
|
$term = str_replace("?", "_", $term);
|
||||||
if(!preg_match("/^[%_]+$/", $term)) {
|
if(!preg_match("/^[%_]+$/", $term)) {
|
||||||
$tag_querylets[] = new TagQuerylet($term, $positive);
|
$expansions = $database->resolve_wildcard($term);
|
||||||
|
if($positive) $positive_tag_count++;
|
||||||
|
foreach($expansions as $term) {
|
||||||
|
$tag_querylets[] = new TagQuerylet($term, $positive);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -287,7 +291,7 @@ class Image {
|
||||||
$query = new Querylet("
|
$query = new Querylet("
|
||||||
SELECT images.* FROM images
|
SELECT images.* FROM images
|
||||||
JOIN image_tags ON images.id = image_tags.image_id
|
JOIN image_tags ON images.id = image_tags.image_id
|
||||||
WHERE tag_id = (SELECT tags.id FROM tags WHERE lower(tag) = lower(?))
|
WHERE tag_id = (SELECT tags.id FROM tags WHERE tag = ?)
|
||||||
", array($tag_querylets[0]->tag));
|
", array($tag_querylets[0]->tag));
|
||||||
|
|
||||||
if(strlen($img_search->sql) > 0) {
|
if(strlen($img_search->sql) > 0) {
|
||||||
|
@ -303,7 +307,7 @@ class Image {
|
||||||
$tags_ok = true;
|
$tags_ok = true;
|
||||||
|
|
||||||
foreach($tag_querylets as $tq) {
|
foreach($tag_querylets as $tq) {
|
||||||
$tag_ids = $database->db->GetCol("SELECT id FROM tags WHERE lower(tag) = lower(?)", array($tq->tag));
|
$tag_ids = $database->db->GetCol("SELECT id FROM tags WHERE tag = ?", array($tq->tag));
|
||||||
if($tq->positive) {
|
if($tq->positive) {
|
||||||
$positive_tag_id_array = array_merge($positive_tag_id_array, $tag_ids);
|
$positive_tag_id_array = array_merge($positive_tag_id_array, $tag_ids);
|
||||||
$tags_ok = count($tag_ids) > 0;
|
$tags_ok = count($tag_ids) > 0;
|
||||||
|
@ -321,14 +325,13 @@ class Image {
|
||||||
$sql = "SELECT images.* FROM images WHERE ";
|
$sql = "SELECT images.* FROM images WHERE ";
|
||||||
if($have_pos) {
|
if($have_pos) {
|
||||||
$positive_tag_id_list = join(', ', $positive_tag_id_array);
|
$positive_tag_id_list = join(', ', $positive_tag_id_array);
|
||||||
$positive_tag_count = count($positive_tag_id_array);
|
|
||||||
$sql .= "
|
$sql .= "
|
||||||
images.id IN (
|
images.id IN (
|
||||||
SELECT image_id
|
SELECT image_id
|
||||||
FROM image_tags
|
FROM image_tags
|
||||||
WHERE tag_id IN ($positive_tag_id_list)
|
WHERE tag_id IN ($positive_tag_id_list)
|
||||||
GROUP BY image_id
|
GROUP BY image_id
|
||||||
HAVING COUNT(image_id)=$positive_tag_count
|
HAVING COUNT(image_id)>=$positive_tag_count
|
||||||
)
|
)
|
||||||
";
|
";
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue