diff --git a/contrib/admin_utils/main.php b/contrib/admin_utils/main.php index 0c743737..8d8d77dc 100644 --- a/contrib/admin_utils/main.php +++ b/contrib/admin_utils/main.php @@ -22,6 +22,12 @@ class AdminUtils extends Extension { case 'lowercase all tags': $this->lowercase_all_tags(); break; + case 'recount tag use': + $this->recount_tag_use(); + break; + case 'purge unused tags': + $this->purge_unused_tags(); + break; } global $page; @@ -41,6 +47,15 @@ class AdminUtils extends Extension { global $database; $database->execute("UPDATE tags SET tag=lower(tag)"); } + private function recout_tag_use() { + global $database; + $database->Execute("UPDATE tags SET count=(SELECT COUNT(image_id) FROM image_tags WHERE tag_id=tags.id GROUP BY tag_id)"); + } + private function purge_unused_tags() { + global $database; + $this->recount_tag_use(); + $database->Execute("DELETE FROM tags WHERE count=0"); + } private function check_for_orphanned_images() { $orphans = array(); foreach(glob("images/*") as $dir) { diff --git a/contrib/admin_utils/theme.php b/contrib/admin_utils/theme.php index f92b13d3..bc9f8f82 100644 --- a/contrib/admin_utils/theme.php +++ b/contrib/admin_utils/theme.php @@ -1,11 +1,15 @@
"; $page->add_block(new Block("Misc Admin Tools", $html)); diff --git a/contrib/autocomplete/main.php b/contrib/autocomplete/main.php index 8f34b174..2d7bdc04 100644 --- a/contrib/autocomplete/main.php +++ b/contrib/autocomplete/main.php @@ -1,7 +1,6 @@ page == "index" || $event->page == "view")) { global $page; @@ -14,14 +13,12 @@ class AutoComplete extends Extension { $page->set_data($this->get_completions($event->get_arg(0))); } } -// }}} -// do things {{{ + private function get_completions($start) { global $database; - $tags = $database->db->GetCol("SELECT tag,count(image_id) AS count FROM tags WHERE tag LIKE ? GROUP BY tag ORDER BY count DESC", array($start.'%')); + $tags = $database->db->GetCol("SELECT tag,count FROM tags WHERE tag LIKE ? ORDER BY count DESC", array($start.'%')); return implode("\n", $tags); } -// }}} } add_event_listener(new AutoComplete()); ?> diff --git a/core/config.class.php b/core/config.class.php index 62e22d7d..16fda4a9 100644 --- a/core/config.class.php +++ b/core/config.class.php @@ -27,7 +27,7 @@ class Config { 'comment_limit' => 3, # comment 'comment_count' => 5, # comment 'popular_count' => 15, # popular - 'info_link' => 'http://tags.shishnet.org/wiki/$tag', # popular + 'info_link' => 'http://en.wikipedia.org/wiki/$tag', # popular 'login_signup_enabled' => true, # user 'image_ilink' => '$base/image/$id.$ext', # view 'image_slink' => '', # view diff --git a/core/database.class.php b/core/database.class.php index 5d6d6563..cdbc6e53 100644 --- a/core/database.class.php +++ b/core/database.class.php @@ -160,7 +160,14 @@ class Database { $query = new Querylet( // MySQL is braindead, and does a full table scan on images, running the subquery once for each row -_- // "{$this->get_images} WHERE images.id IN (SELECT image_id FROM tags WHERE tag LIKE ?) ", - "SELECT *,UNIX_TIMESTAMP(posted) AS posted_timestamp FROM tags, images WHERE tag LIKE ? AND tags.image_id = images.id ", + " + SELECT *, UNIX_TIMESTAMP(posted) AS posted_timestamp + FROM tags, image_tags, images + WHERE + tag LIKE ? + AND tags.id = image_tags.tag_id + AND image_tags.image_id = images.id + ", $tag_search->variables); if(strlen($img_search->sql) > 0) { @@ -172,9 +179,10 @@ class Database { $s_tag_list = join(', ', $s_tag_array); $subquery = new Querylet(" - SELECT *, SUM({$tag_search->sql}) AS score + SELECT images.*, SUM({$tag_search->sql}) AS score FROM images - LEFT JOIN tags ON tags.image_id = images.id + LEFT JOIN image_tags ON image_tags.image_id = images.id + JOIN tags ON image_tags.tag_id = tags.id WHERE tags.tag IN ({$s_tag_list}) GROUP BY images.id HAVING score = ?", @@ -197,7 +205,7 @@ class Database { } public function delete_tags_from_image($image_id) { - $this->execute("DELETE FROM tags WHERE image_id=?", array($image_id)); + $this->execute("DELETE FROM image_tags WHERE image_id=?", array($image_id)); } public function set_tags($image_id, $tags) { @@ -212,7 +220,8 @@ class Database { // insert each new tag foreach($tags as $tag) { - $this->execute("INSERT INTO tags(image_id, tag) VALUES(?, ?)", array($image_id, $tag)); + $this->execute("INSERT IGNORE INTO tags(tag) VALUES (?)", array($tag)); + $this->execute("INSERT INTO image_tags(image_id, tag_id) VALUES(?, (SELECT id FROM tags WHERE tag = ?))", array($image_id, $tag)); } } // }}} diff --git a/core/ext/tag_edit.ext.php b/core/ext/tag_edit.ext.php index 685a46c1..a9010b84 100644 --- a/core/ext/tag_edit.ext.php +++ b/core/ext/tag_edit.ext.php @@ -75,9 +75,16 @@ class TagEdit extends Extension { // }}} // edit {{{ private function mass_tag_edit($search, $replace) { - // FIXME: deal with collisions global $database; - $database->Execute("UPDATE tags SET tag=? WHERE tag=?", Array($replace, $search)); + $search_id = $database->db->GetOne("SELECT id FROM tags WHERE tag=?", array($search)); + $replace_id = $database->db->GetOne("SELECT id FROM tags WHERE tag=?", array($replace)); + if($search_id && $replace_id) { + // FIXME: what if the (image_id,tag_id) pair already exists? + $database->Execute("UPDATE image_tags SET tag_id=? WHERE tag_id=?", Array($replace_id, $search_id)); + } + else if($search_id) { + $database->Execute("UPDATE tags SET tag=? WHERE tag=?", Array($replace, $search)); + } } // }}} // HTML {{{ diff --git a/core/image.class.php b/core/image.class.php index ee2ef8a6..3fa91ea6 100644 --- a/core/image.class.php +++ b/core/image.class.php @@ -77,7 +77,7 @@ class Image { if(!isset($this->tag_array)) { global $database; $this->tag_array = Array(); - $row = $database->Execute("SELECT * FROM tags WHERE image_id=? ORDER BY tag", array($this->id)); + $row = $database->Execute("SELECT * FROM image_tags JOIN tags ON image_tags.tag_id = tags.id WHERE image_id=? ORDER BY tag", array($this->id)); while(!$row->EOF) { $this->tag_array[] = $row->fields['tag']; $row->MoveNext(); diff --git a/ext/alias_editor/main.php b/ext/alias_editor/main.php index b6d92c98..a49a7b63 100644 --- a/ext/alias_editor/main.php +++ b/ext/alias_editor/main.php @@ -12,7 +12,7 @@ class AddAliasEvent extends Event { class AliasEditor extends Extension { var $theme; -// event handler {{{ + public function receive_event($event) { if(is_null($this->theme)) $this->theme = get_theme_object("alias_editor", "AliasEditorTheme"); @@ -65,7 +65,6 @@ class AliasEditor extends Extension { } } } -// }}} } add_event_listener(new AliasEditor()); ?> diff --git a/ext/et/main.php b/ext/et/main.php index d9c62a12..4f3adf4d 100644 --- a/ext/et/main.php +++ b/ext/et/main.php @@ -45,6 +45,7 @@ class ET extends Extension { $info['stat_comments'] = $database->db->GetOne("SELECT COUNT(*) FROM comments"); $info['stat_users'] = $database->db->GetOne("SELECT COUNT(*) FROM users"); $info['stat_tags'] = $database->db->GetOne("SELECT COUNT(*) FROM tags"); + $info['stat_image_tags'] = $database->db->GetOne("SELECT COUNT(*) FROM image_tags"); $els = array(); foreach($_event_listeners as $el) { diff --git a/ext/et/theme.php b/ext/et/theme.php index ad936753..3b2477e2 100644 --- a/ext/et/theme.php +++ b/ext/et/theme.php @@ -30,6 +30,7 @@ Images: {$info['stat_images']} Comments: {$info['stat_comments']} Users: {$info['stat_users']} Tags: {$info['stat_tags']} +Applications: {$info['stat_image_tags']} EOD; $html = <<