more consistency for 'old' and 'new' terminology

This commit is contained in:
Shish 2024-01-09 15:27:02 +00:00
parent cafef06b14
commit edb8ec33fa
10 changed files with 77 additions and 92 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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