From f81fa0807778dc7d734e5a32b034bced6ccd6dff Mon Sep 17 00:00:00 2001 From: Shish Date: Thu, 5 Apr 2012 17:33:50 +0100 Subject: [PATCH 1/3] source search --- ext/index/main.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ext/index/main.php b/ext/index/main.php index f5244d54..2fe58742 100644 --- a/ext/index/main.php +++ b/ext/index/main.php @@ -224,7 +224,11 @@ class Index extends Extension { } 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.'%"')); + $event->add_querylet(new Querylet('images.filename LIKE :fn', array("fn"=>"%$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("/^posted(<|>|<=|>=|=)([0-9-]*)$/", $event->term, $matches)) { $cmp = $matches[1]; From fd725d3363edd36d39fbbb378e864e817e28c93a Mon Sep 17 00:00:00 2001 From: Shish Date: Wed, 16 May 2012 05:12:19 +0100 Subject: [PATCH 2/3] merge image ID lists before scanning the images table, not after --- core/imageboard.pack.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/core/imageboard.pack.php b/core/imageboard.pack.php index cd356513..22885329 100644 --- a/core/imageboard.pack.php +++ b/core/imageboard.pack.php @@ -613,6 +613,9 @@ class Image { } /** + * WARNING: this description is no longer accurate, though it does get across + * the general idea - the actual method has a few extra optimisiations + * * "foo bar -baz user=foo" becomes * * SELECT * FROM images WHERE @@ -746,32 +749,29 @@ class Image { $have_pos = count($positive_tag_id_array) > 0; $have_neg = count($negative_tag_id_array) > 0; - $sql = "SELECT images.* FROM images WHERE "; + $sql = "SELECT images.* FROM images WHERE images.id IN ("; if($have_pos) { $positive_tag_id_list = join(', ', $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 - ) + 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 "; } if($have_pos && $have_neg) { - $sql .= " AND "; + $sql .= " EXCEPT "; } if($have_neg) { $negative_tag_id_list = join(', ', $negative_tag_id_array); $sql .= " - images.id NOT IN ( - SELECT image_id - FROM image_tags - WHERE tag_id IN ($negative_tag_id_list) - ) + SELECT image_id + FROM image_tags + WHERE tag_id IN ($negative_tag_id_list) "; } + $sql .= ")"; $query = new Querylet($sql); if(strlen($img_search->sql) > 0) { From 708b117b2aa41f5cf86365d8e9b3eab6a05f948d Mon Sep 17 00:00:00 2001 From: Shish Date: Wed, 16 May 2012 06:33:04 +0100 Subject: [PATCH 3/3] caching for related tags --- ext/tag_list/main.php | 80 +++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 37 deletions(-) diff --git a/ext/tag_list/main.php b/ext/tag_list/main.php index 8f36345f..e90af658 100644 --- a/ext/tag_list/main.php +++ b/ext/tag_list/main.php @@ -394,46 +394,52 @@ class TagList extends Extension { global $database; global $config; - $wild_tags = Tag::explode($search); - // $search_tags = array(); + $related_tags = $database->cache->get("related_tags:$search"); - $tag_id_array = array(); - $tags_ok = true; - foreach($wild_tags as $tag) { - $tag = str_replace("*", "%", $tag); - $tag = str_replace("?", "_", $tag); - $tag_ids = $database->get_col("SELECT id FROM tags WHERE tag LIKE :tag", array("tag"=>$tag)); - // $search_tags = array_merge($search_tags, - // $database->get_col("SELECT tag FROM tags WHERE tag LIKE :tag", array("tag"=>$tag))); - $tag_id_array = array_merge($tag_id_array, $tag_ids); - $tags_ok = count($tag_ids) > 0; - if(!$tags_ok) break; - } - $tag_id_list = join(', ', $tag_id_array); + if(empty($related_tags)) { + $wild_tags = Tag::explode($search); + // $search_tags = array(); - if($tags_ok) { - $query = " - SELECT t2.tag AS tag, COUNT(it2.image_id) AS calc_count - FROM - image_tags AS it1, - image_tags AS it2, - tags AS t1, - tags AS t2 - WHERE - t1.id IN($tag_id_list) - AND it1.image_id=it2.image_id - AND it1.tag_id = t1.id - AND it2.tag_id = t2.id - GROUP BY t2.tag - ORDER BY calc_count - DESC LIMIT :limit - "; - $args = array("limit"=>$config->get_int('tag_list_length')); - - $related_tags = $database->get_all($query, $args); - if(count($related_tags) > 0) { - $this->theme->display_refine_block($page, $related_tags, $wild_tags); + $tag_id_array = array(); + $tags_ok = true; + foreach($wild_tags as $tag) { + $tag = str_replace("*", "%", $tag); + $tag = str_replace("?", "_", $tag); + $tag_ids = $database->get_col("SELECT id FROM tags WHERE tag LIKE :tag", array("tag"=>$tag)); + // $search_tags = array_merge($search_tags, + // $database->get_col("SELECT tag FROM tags WHERE tag LIKE :tag", array("tag"=>$tag))); + $tag_id_array = array_merge($tag_id_array, $tag_ids); + $tags_ok = count($tag_ids) > 0; + if(!$tags_ok) break; } + $tag_id_list = join(', ', $tag_id_array); + + if($tags_ok) { + $query = " + SELECT t2.tag AS tag, COUNT(it2.image_id) AS calc_count + FROM + image_tags AS it1, + image_tags AS it2, + tags AS t1, + tags AS t2 + WHERE + t1.id IN($tag_id_list) + AND it1.image_id=it2.image_id + AND it1.tag_id = t1.id + AND it2.tag_id = t2.id + GROUP BY t2.tag + ORDER BY calc_count + DESC LIMIT :limit + "; + $args = array("limit"=>$config->get_int('tag_list_length')); + + $related_tags = $database->get_all($query, $args); + $database->cache->set("related_tags:$search", $related_tags, 60*60); + } + } + + if(count($related_tags) > 0) { + $this->theme->display_refine_block($page, $related_tags, $wild_tags); } } // }}}