ignore duplicate tag history entries

This commit is contained in:
Shish 2009-08-24 03:33:51 +01:00
parent 3e1edae90d
commit d5dea776c5
2 changed files with 24 additions and 9 deletions

View File

@ -59,7 +59,7 @@ class Tag_History implements Extension {
$event->panel->add_block($sb); $event->panel->add_block($sb);
} }
if(($event instanceof TagSetEvent)) { if(($event instanceof TagSetEvent)) {
$this->add_tag_history($event->image->id, $event->tags); $this->add_tag_history($event->image, $event->tags);
} }
} }
@ -185,13 +185,15 @@ class Tag_History implements Extension {
/* /*
* this function is called just before an images tag are changed * this function is called just before an images tag are changed
*/ */
private function add_tag_history($image_id, $tags) private function add_tag_history($image, $tags)
{ {
global $database; global $database;
global $config; global $config;
global $user; global $user;
if(is_array($tags)) $tags = implode(' ', $tags); $new_tags = Tag::implode($tags);
$old_tags = Tag::implode($image->get_tag_array());
if($new_tags == $old_tags) return;
// add a history entry // add a history entry
$allowed = $config->get_int("history_limit"); $allowed = $config->get_int("history_limit");
@ -200,18 +202,18 @@ class Tag_History implements Extension {
$row = $database->execute(" $row = $database->execute("
INSERT INTO tag_histories(image_id, tags, user_id, user_ip, date_set) INSERT INTO tag_histories(image_id, tags, user_id, user_ip, date_set)
VALUES (?, ?, ?, ?, now())", VALUES (?, ?, ?, ?, now())",
array($image_id, $tags, $user->id, $_SERVER['REMOTE_ADDR'])); array($image->id, $new_tags, $user->id, $_SERVER['REMOTE_ADDR']));
$entries = $database->db->GetOne("SELECT COUNT(*) FROM `tag_histories` WHERE image_id = ?", array($image_id));
// if needed remove oldest one // if needed remove oldest one
if($allowed == -1) return; if($allowed == -1) return;
$entries = $database->db->GetOne("SELECT COUNT(*) FROM tag_histories WHERE image_id = ?", array($image->id));
if($entries > $allowed) if($entries > $allowed)
{ {
// TODO: Make these queries better // TODO: Make these queries better
$min_id = $database->db->GetOne("SELECT MIN(id) FROM tag_histories WHERE image_id = ?", array($image_id)); $min_id = $database->db->GetOne("SELECT MIN(id) FROM tag_histories WHERE image_id = ?", array($image->id));
$database->execute("DELETE FROM tag_histories WHERE id = ?", array($min_id)); $database->execute("DELETE FROM tag_histories WHERE id = ?", array($min_id));
} }
} }
} }
add_event_listener(new Tag_History()); add_event_listener(new Tag_History(), 40); // in before tags are actually set, so that "get current tags" works
?> ?>

View File

@ -230,7 +230,7 @@ class Image {
* Get this image's tags as a string * Get this image's tags as a string
*/ */
public function get_tag_list() { public function get_tag_list() {
return implode(' ', $this->get_tag_array()); return Tag::implode($this->get_tag_array());
} }
/** /**
@ -829,6 +829,19 @@ class Tag {
return $tag_array; return $tag_array;
} }
public static function implode($tags) {
assert(is_string($tags) || is_array($tags));
if(is_string($tags)) {
// do nothing
}
else if(is_array($tags)) {
$tags = implode(' ', $tags);
}
return $tags;
}
public static function resolve_alias($tag) { public static function resolve_alias($tag) {
assert(is_string($tag)); assert(is_string($tag));