diff --git a/core/imageboard.pack.php b/core/imageboard.pack.php index 2067d0c5..36e9d050 100644 --- a/core/imageboard.pack.php +++ b/core/imageboard.pack.php @@ -609,6 +609,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 @@ -742,32 +745,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) { 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]; 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); } } // }}} diff --git a/ext/upload/main.php b/ext/upload/main.php index f88db765..06723691 100644 --- a/ext/upload/main.php +++ b/ext/upload/main.php @@ -215,7 +215,9 @@ class Upload extends Extension { private function tags_for_upload_slot($id) { if(isset($_POST["tags$id"])) { - $tags = array_merge(Tag::explode($_POST['tags']), Tag::explode($_POST["tags$id"])); + # merge then explode, not explode then merge - else + # one of the merges may create a surplus "tagme" + $tags = Tag::explode($_POST['tags'] . " " . $_POST["tags$id"]); } else { $tags = Tag::explode($_POST['tags']);