From d5dea776c5091aa97479cfcecb73c69c8a5c1529 Mon Sep 17 00:00:00 2001 From: Shish Date: Mon, 24 Aug 2009 03:33:51 +0100 Subject: [PATCH] ignore duplicate tag history entries --- contrib/tag_history/main.php | 18 ++++++++++-------- core/imageboard.pack.php | 15 ++++++++++++++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/contrib/tag_history/main.php b/contrib/tag_history/main.php index f4561a54..2ce64339 100644 --- a/contrib/tag_history/main.php +++ b/contrib/tag_history/main.php @@ -59,7 +59,7 @@ class Tag_History implements Extension { $event->panel->add_block($sb); } if(($event instanceof TagSetEvent)) { - $this->add_tag_history($event->image->id, $event->tags); + $this->add_tag_history($event->image, $event->tags); } } @@ -185,14 +185,16 @@ class Tag_History implements Extension { /* * 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 $config; 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 $allowed = $config->get_int("history_limit"); if($allowed == 0) return; @@ -200,18 +202,18 @@ class Tag_History implements Extension { $row = $database->execute(" INSERT INTO tag_histories(image_id, tags, user_id, user_ip, date_set) VALUES (?, ?, ?, ?, now())", - array($image_id, $tags, $user->id, $_SERVER['REMOTE_ADDR'])); - $entries = $database->db->GetOne("SELECT COUNT(*) FROM `tag_histories` WHERE image_id = ?", array($image_id)); + array($image->id, $new_tags, $user->id, $_SERVER['REMOTE_ADDR'])); // if needed remove oldest one if($allowed == -1) return; + $entries = $database->db->GetOne("SELECT COUNT(*) FROM tag_histories WHERE image_id = ?", array($image->id)); if($entries > $allowed) { // 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)); } } } -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 ?> diff --git a/core/imageboard.pack.php b/core/imageboard.pack.php index 74eb66f4..b56e81ea 100644 --- a/core/imageboard.pack.php +++ b/core/imageboard.pack.php @@ -230,7 +230,7 @@ class Image { * Get this image's tags as a string */ 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; } + 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) { assert(is_string($tag));