diff --git a/core/imageboard/event.php b/core/imageboard/event.php index 9b34d598..f8797b0d 100644 --- a/core/imageboard/event.php +++ b/core/imageboard/event.php @@ -51,7 +51,7 @@ class ImageDeletionEvent extends Event */ class ImageReplaceEvent extends Event { - public string $original_hash; + public string $old_hash; public string $new_hash; /** @@ -66,7 +66,7 @@ class ImageReplaceEvent extends Event public string $tmp_filename, ) { parent::__construct(); - $this->original_hash = $image->hash; + $this->old_hash = $image->hash; $this->new_hash = md5_file($tmp_filename); } } diff --git a/ext/auto_tagger/main.php b/ext/auto_tagger/main.php index da3d74d0..eabb8df8 100644 --- a/ext/auto_tagger/main.php +++ b/ext/auto_tagger/main.php @@ -155,9 +155,9 @@ class AutoTagger extends Extension public function onTagSet(TagSetEvent $event) { - $results = $this->apply_auto_tags($event->tags); + $results = $this->apply_auto_tags($event->new_tags); if (!empty($results)) { - $event->tags = $results; + $event->new_tags = $results; } } diff --git a/ext/ban_words/main.php b/ext/ban_words/main.php index 4e57b7b4..0247c7ea 100644 --- a/ext/ban_words/main.php +++ b/ext/ban_words/main.php @@ -54,7 +54,7 @@ xanax public function onTagSet(TagSetEvent $event) { - $this->test_text(Tag::implode($event->tags), new SCoreException("Tags contain banned terms")); + $this->test_text(Tag::implode($event->new_tags), new SCoreException("Tags contain banned terms")); } public function onSetupBuilding(SetupBuildingEvent $event) diff --git a/ext/bulk_import_export/main.php b/ext/bulk_import_export/main.php index 5acd86fb..3aa5b7d5 100644 --- a/ext/bulk_import_export/main.php +++ b/ext/bulk_import_export/main.php @@ -52,7 +52,7 @@ class BulkImportExport extends DataHandlerExtension file_put_contents($tmpfile, $stream); - $images = add_image($tmpfile, $item->filename, $item->tags)->images; + $images = add_image($tmpfile, $item->filename, $item->new_tags)->images; if (count($images) == 0) { throw new SCoreException("Unable to import file $item->hash"); diff --git a/ext/livefeed/main.php b/ext/livefeed/main.php index 7df0102f..fbc1369a 100644 --- a/ext/livefeed/main.php +++ b/ext/livefeed/main.php @@ -30,7 +30,7 @@ class LiveFeed extends Extension { $this->msg( make_http(make_link("post/view/".$event->image->id))." - ". - "tags set to: ".Tag::implode($event->tags) + "tags set to: ".Tag::implode($event->new_tags) ); } diff --git a/ext/not_a_tag/main.php b/ext/not_a_tag/main.php index 496eb712..57e3a6d7 100644 --- a/ext/not_a_tag/main.php +++ b/ext/not_a_tag/main.php @@ -56,9 +56,9 @@ class NotATag extends Extension { global $user; if ($user->can(Permissions::BAN_IMAGE)) { - $event->tags = $this->strip($event->tags); + $event->new_tags = $this->strip($event->new_tags); } else { - $this->scan($event->tags); + $this->scan($event->new_tags); } } diff --git a/ext/replace_file/main.php b/ext/replace_file/main.php index d8cf668f..8de3f9a9 100644 --- a/ext/replace_file/main.php +++ b/ext/replace_file/main.php @@ -85,6 +85,6 @@ class ReplaceFile extends Extension send_event(new ThumbnailGenerationEvent($image)); - log_info("image", "Replaced >>{$image->id} {$event->original_hash} with {$event->new_hash}"); + log_info("image", "Replaced >>{$image->id} {$event->old_hash} with {$event->new_hash}"); } } diff --git a/ext/s3/main.php b/ext/s3/main.php index 5a0331f8..511ea0dd 100644 --- a/ext/s3/main.php +++ b/ext/s3/main.php @@ -95,7 +95,7 @@ class S3 extends Extension public function onTagSet(TagSetEvent $event) { - $this->sync_post($event->image, $event->tags); + $this->sync_post($event->image, $event->new_tags); } public function onImageDeletion(ImageDeletionEvent $event) @@ -105,7 +105,7 @@ class S3 extends Extension public function onImageReplace(ImageReplaceEvent $event) { - $this->remove_file($event->original_hash); + $this->remove_file($event->old_hash); $this->sync_post($event->image); } diff --git a/ext/tag_edit/main.php b/ext/tag_edit/main.php index c7180059..4d6313d9 100644 --- a/ext/tag_edit/main.php +++ b/ext/tag_edit/main.php @@ -52,7 +52,8 @@ class TagSetException extends UserErrorException class TagSetEvent extends Event { public Image $image; - public array $tags; + public array $old_tags; + public array $new_tags; public array $metatags; /** @@ -62,15 +63,15 @@ class TagSetEvent extends Event { parent::__construct(); $this->image = $image; - - $this->tags = []; + $this->old_tags = $image->get_tag_array(); + $this->new_tags = []; $this->metatags = []; foreach ($tags as $tag) { if ((!str_contains($tag, ':')) && (!str_contains($tag, '='))) { //Tag doesn't contain : or =, meaning it can't possibly be a metatag. //This should help speed wise, as it avoids running every single tag through a bunch of preg_match instead. - $this->tags[] = $tag; + $this->new_tags[] = $tag; continue; } @@ -78,7 +79,7 @@ class TagSetEvent extends Event //seperate tags from metatags if (!$ttpe->metatag) { - $this->tags[] = $tag; + $this->new_tags[] = $tag; } else { $this->metatags[] = $tag; } @@ -224,7 +225,7 @@ class TagEdit extends Extension { global $user; if ($user->can(Permissions::EDIT_IMAGE_TAG) && (!$event->image->is_locked() || $user->can(Permissions::EDIT_IMAGE_LOCK))) { - $event->image->set_tags($event->tags); + $event->image->set_tags($event->new_tags); } foreach ($event->metatags as $tag) { send_event(new TagTermParseEvent($tag, $event->image->id)); diff --git a/ext/tag_history/main.php b/ext/tag_history/main.php index 94da23e5..fa1e78ad 100644 --- a/ext/tag_history/main.php +++ b/ext/tag_history/main.php @@ -9,12 +9,6 @@ class TagHistory extends Extension /** @var TagHistoryTheme */ protected Themelet $theme; - // in before tags are actually set, so that "get current tags" works - public function get_priority(): int - { - return 40; - } - public function onInitExt(InitExtEvent $event) { global $config; @@ -80,7 +74,64 @@ class TagHistory extends Extension public function onTagSet(TagSetEvent $event) { - $this->add_tag_history($event->image, $event->tags); + global $database, $config, $user; + + $new_tags = Tag::implode($event->new_tags); + $old_tags = Tag::implode($event->old_tags); + + if ($new_tags == $old_tags) { + return; + } + + if (empty($old_tags)) { + /* no old tags, so we are probably adding the image for the first time */ + log_debug("tag_history", "adding new tag history: [$new_tags]"); + } else { + log_debug("tag_history", "adding tag history: [$old_tags] -> [$new_tags]"); + } + + $allowed = $config->get_int("history_limit"); + if ($allowed == 0) { + return; + } + + // if the image has no history, make one with the old tags + $entries = $database->get_one("SELECT COUNT(*) FROM tag_histories WHERE image_id = :id", ["id" => $event->image->id]); + if ($entries == 0 && !empty($old_tags)) { + $database->execute( + " + INSERT INTO tag_histories(image_id, tags, user_id, user_ip, date_set) + VALUES (:image_id, :tags, :user_id, :user_ip, now())", + ["image_id" => $event->image->id, "tags" => $old_tags, "user_id" => $config->get_int('anon_id'), "user_ip" => '127.0.0.1'] + ); + $entries++; + } + + // add a history entry + $database->execute( + " + INSERT INTO tag_histories(image_id, tags, user_id, user_ip, date_set) + VALUES (:image_id, :tags, :user_id, :user_ip, now())", + ["image_id" => $event->image->id, "tags" => $new_tags, "user_id" => $user->id, "user_ip" => get_real_ip()] + ); + $entries++; + + // if needed remove oldest one + if ($allowed == -1) { + return; + } + if ($entries > $allowed) { + // TODO: Make these queries better + /* + MySQL does NOT allow you to modify the same table which you use in the SELECT part. + Which means that these will probably have to stay as TWO separate queries... + + https://dev.mysql.com/doc/refman/5.1/en/subquery-restrictions.html + https://stackoverflow.com/questions/45494/mysql-error-1093-cant-specify-target-table-for-update-in-from-clause + */ + $min_id = $database->get_one("SELECT MIN(id) FROM tag_histories WHERE image_id = :image_id", ["image_id" => $event->image->id]); + $database->execute("DELETE FROM tag_histories WHERE id = :id", ["id" => $min_id]); + } } public function onPageSubNavBuilding(PageSubNavBuildingEvent $event) @@ -342,71 +393,4 @@ class TagHistory extends Extension log_info("tag_history", 'Reverted '.count($result).' edits.'); } - - /** - * This function is called just before an images tag are changed. - * - * @param string[] $tags - */ - private function add_tag_history(Image $image, array $tags) - { - global $database, $config, $user; - - $new_tags = Tag::implode($tags); - $old_tags = $image->get_tag_list(); - - if ($new_tags == $old_tags) { - return; - } - - if (empty($old_tags)) { - /* no old tags, so we are probably adding the image for the first time */ - log_debug("tag_history", "adding new tag history: [$new_tags]"); - } else { - log_debug("tag_history", "adding tag history: [$old_tags] -> [$new_tags]"); - } - - $allowed = $config->get_int("history_limit"); - if ($allowed == 0) { - return; - } - - // if the image has no history, make one with the old tags - $entries = $database->get_one("SELECT COUNT(*) FROM tag_histories WHERE image_id = :id", ["id" => $image->id]); - if ($entries == 0 && !empty($old_tags)) { - $database->execute( - " - INSERT INTO tag_histories(image_id, tags, user_id, user_ip, date_set) - VALUES (:image_id, :tags, :user_id, :user_ip, now())", - ["image_id" => $image->id, "tags" => $old_tags, "user_id" => $config->get_int('anon_id'), "user_ip" => '127.0.0.1'] - ); - $entries++; - } - - // add a history entry - $database->execute( - " - INSERT INTO tag_histories(image_id, tags, user_id, user_ip, date_set) - VALUES (:image_id, :tags, :user_id, :user_ip, now())", - ["image_id" => $image->id, "tags" => $new_tags, "user_id" => $user->id, "user_ip" => get_real_ip()] - ); - $entries++; - - // if needed remove oldest one - if ($allowed == -1) { - return; - } - if ($entries > $allowed) { - // TODO: Make these queries better - /* - MySQL does NOT allow you to modify the same table which you use in the SELECT part. - Which means that these will probably have to stay as TWO separate queries... - - https://dev.mysql.com/doc/refman/5.1/en/subquery-restrictions.html - https://stackoverflow.com/questions/45494/mysql-error-1093-cant-specify-target-table-for-update-in-from-clause - */ - $min_id = $database->get_one("SELECT MIN(id) FROM tag_histories WHERE image_id = :image_id", ["image_id" => $image->id]); - $database->execute("DELETE FROM tag_histories WHERE id = :id", ["id" => $min_id]); - } - } }