tags(image_id,tag) split into image_tags(image_id,tag_id) and tags(id,tag,count)

git-svn-id: file:///home/shish/svn/shimmie2/trunk@227 7f39781d-f577-437e-ae19-be835c7a54ca
This commit is contained in:
shish 2007-07-05 21:30:37 +00:00
parent 9d6becdb6c
commit b6809c3b0a
11 changed files with 75 additions and 34 deletions

View file

@ -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) {

View file

@ -1,11 +1,15 @@
<?php
class AdminUtilsTheme extends Themelet {
public function display_form() {
public function display_form($page) {
$html = "
<p><form action='".make_link("admin_utils")."' method='POST'>
<input type='hidden' name='action' value='lowercase all tags'>
<input type='submit' value='Lowercase All Tags'>
<select name='action'>
<option value='lowercase all tags'>All tags to lowercase</option>
<option value='recount tag use'>Recount tag use</option>
<option value='purge unused tags'>Purge unused tags</option>
</select>
<input type='submit' value='Go'>
</form>
";
$page->add_block(new Block("Misc Admin Tools", $html));

View file

@ -1,7 +1,6 @@
<?php
class AutoComplete extends Extension {
// event handling {{{
public function receive_event($event) {
if(is_a($event, 'PageRequestEvent') && ($event->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());
?>

View file

@ -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

View file

@ -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));
}
}
// }}}

View file

@ -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 {{{

View file

@ -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();

View file

@ -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());
?>

View file

@ -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) {

View file

@ -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 = <<<EOD
<form action='http://shimmie.shishnet.org/register.php' method='POST'>

View file

@ -89,7 +89,7 @@ class TagList extends Extension {
$tags_min = $config->get_int('tags_min');
$result = $database->Execute(
"SELECT tag,COUNT(image_id) AS count FROM tags GROUP BY tag HAVING count > ? ORDER BY tag",
"SELECT tag,count FROM tags WHERE count > ? ORDER BY tag",
array($tags_min));
$html = "";
@ -113,7 +113,7 @@ class TagList extends Extension {
$tags_min = $config->get_int('tags_min');
$result = $database->Execute(
"SELECT tag,COUNT(image_id) AS count FROM tags GROUP BY tag HAVING count > ? ORDER BY tag",
"SELECT tag,count FROM tags WHERE count > ? ORDER BY tag",
array($tags_min));
$html = "";
@ -140,7 +140,7 @@ class TagList extends Extension {
$tags_min = $config->get_int('tags_min');
$result = $database->Execute(
"SELECT tag,COUNT(image_id) AS count FROM tags GROUP BY tag HAVING count > ? ORDER BY count DESC, tag ASC",
"SELECT tag,count FROM tags WHERE count > ? ORDER BY count DESC, tag ASC",
array($tags_min)
);
@ -168,19 +168,23 @@ class TagList extends Extension {
global $config;
$query = "
SELECT COUNT(t3.image_id) as count, t3.tag
SELECT COUNT(it3.image_id) as count, t3.tag
FROM
image_tags AS it1,
image_tags AS it2,
image_tags AS it3,
tags AS t1,
tags AS t2,
tags AS t3
tags AS t3
WHERE
t1.image_id=?
AND t1.tag=t2.tag
AND t2.image_id=t3.image_id
it1.image_id=?
AND it1.tag_id=it2.tag_id
AND it2.image_id=it3.image_id
AND t1.tag != 'tagme'
AND t3.tag != 'tagme'
GROUP by t3.tag
ORDER by count DESC
AND t1.id = it1.tag_id
AND t3.id = it3.tag_id
GROUP BY it3.tag_id
ORDER BY count DESC
LIMIT ?
";
$args = array($image->id, $config->get_int('tag_list_length'));
@ -196,9 +200,8 @@ class TagList extends Extension {
global $config;
$query = "
SELECT tag, COUNT(image_id) AS count
SELECT tag, count
FROM tags
GROUP BY tag
ORDER BY count DESC
LIMIT ?
";
@ -219,13 +222,17 @@ class TagList extends Extension {
$s_tag_list = join(',', $s_tags);
$query = "
SELECT t2.tag, COUNT(t2.image_id) AS count
SELECT t2.tag, COUNT(it2.image_id) AS count
FROM
image_tags AS it1,
image_tags AS it2,
tags AS t1,
tags AS t2
WHERE
t1.tag IN($s_tag_list)
AND t1.image_id=t2.image_id
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 count
DESC LIMIT ?
@ -233,6 +240,7 @@ class TagList extends Extension {
$args = array($config->get_int('tag_list_length'));
$tags = $database->db->GetAll($query, $args);
print $database->db->ErrorMsg();
if(count($tags) > 0) {
$this->theme->display_refine_block($page, $tags, $search);
}