From 40431f4c26e4aa84bed85d525744f9fb704b0e61 Mon Sep 17 00:00:00 2001 From: shish Date: Thu, 9 Oct 2008 05:56:48 +0000 Subject: [PATCH] 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 --- core/database.class.php | 16 ++++++++++++++++ core/image.class.php | 15 +++++++++------ 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/core/database.class.php b/core/database.class.php index eee1965a..8271e1e6 100644 --- a/core/database.class.php +++ b/core/database.class.php @@ -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) { assert(is_string($tag)); $tag = preg_replace("/[\s?*]/", "", $tag); diff --git a/core/image.class.php b/core/image.class.php index c412331c..4ec6975a 100644 --- a/core/image.class.php +++ b/core/image.class.php @@ -231,7 +231,7 @@ class Image { private static function build_search_querylet(Config $config, Database $database, $terms) { $tag_querylets = array(); $img_querylets = array(); - + $positive_tag_count = 0; // parse the words that are searched for into // various types of querylet @@ -253,7 +253,11 @@ class Image { $term = str_replace("*", "%", $term); $term = str_replace("?", "_", $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(" SELECT images.* FROM images 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)); if(strlen($img_search->sql) > 0) { @@ -303,7 +307,7 @@ class Image { $tags_ok = true; 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) { $positive_tag_id_array = array_merge($positive_tag_id_array, $tag_ids); $tags_ok = count($tag_ids) > 0; @@ -321,14 +325,13 @@ class Image { $sql = "SELECT images.* FROM images WHERE "; if($have_pos) { $positive_tag_id_list = join(', ', $positive_tag_id_array); - $positive_tag_count = count($positive_tag_id_array); $sql .= " images.id IN ( SELECT image_id FROM image_tags WHERE tag_id IN ($positive_tag_id_list) GROUP BY image_id - HAVING COUNT(image_id)=$positive_tag_count + HAVING COUNT(image_id)>=$positive_tag_count ) "; }