Skip to content

Commit

Permalink
Add plugin to copy tags from another archive ID
Browse files Browse the repository at this point in the history
* adds plugin CopyArchiveTags

* Update lib/LANraragi/Utils/Database.pm

Co-authored-by: Difegue <[email protected]>

* Update lib/LANraragi/Plugin/Metadata/CopyArchiveTags.pm

Co-authored-by: Difegue <[email protected]>

* adds more controls over the input (and refactoring)

* removes "wantarray" from get_tags

* introducing the new try/catch

* reverts get_archive_json

---------

Co-authored-by: IceBreeze <>
Co-authored-by: Difegue <[email protected]>
  • Loading branch information
IceBreeze and Difegue authored Jul 28, 2024
1 parent d96c67e commit 1ed1757
Show file tree
Hide file tree
Showing 5 changed files with 393 additions and 4 deletions.
104 changes: 104 additions & 0 deletions lib/LANraragi/Plugin/Metadata/CopyArchiveTags.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package LANraragi::Plugin::Metadata::CopyArchiveTags;

use v5.36;
use experimental 'try';

use strict;
use warnings;

use LANraragi::Model::Plugins;
use LANraragi::Utils::Database;
use LANraragi::Utils::Logging qw(get_plugin_logger);
use LANraragi::Utils::Tags qw(join_tags_to_string split_tags_to_array);
use LANraragi::Utils::String qw(trim);

#Meta-information about your plugin.
sub plugin_info {

return (
#Standard metadata
name => "Copy Archive Tags",
type => "metadata",
namespace => "copy-archive-tags",
author => "IceBreeze",
version => "1.0",
description => "Copy tags from another LRR archive given either the URI or the ID.",
parameters => [
{ type => "bool",
name => 'copy_date_added',
desc => "Enable to also copy the date (but it's up to you to remove the old one)"
}
],
oneshot_arg => "LRR Gallery URL or ID:"
);

}

sub get_tags {
my $params = read_params(@_);
my $logger = get_plugin_logger();

# should be handled in the caller
my %hashdata;
try {
%hashdata = internal_get_tags( $logger, $params );
} catch ($e) {
$logger->error($e);
return ( error => $e );
}

$logger->info( "Sending the following tags to LRR: " . ( $hashdata{tags} || '-' ) );
return %hashdata;
}

sub internal_get_tags {
my ( $logger, $params ) = @_;

my $lrr_gid = extract_archive_id( $params->{'oneshot'} );
if ( !$lrr_gid ) {
die "oneshot_param doesn't contain a valid archive ID\n";
}

if ( $lrr_gid eq $params->{'lrr_info'}{'archive_id'} ) {
die "You are using the current archive ID\n";
}

$logger->info("Copying tags from archive \"${lrr_gid}\"");

my $tags = LANraragi::Utils::Database::get_tags($lrr_gid);

if ( !$params->{'copy_date_added'} ) {
my @tags = split_tags_to_array($tags);
$tags = join_tags_to_string( grep( !m/date_added/, @tags ) );
}

my %hashdata = ( tags => $tags );

return %hashdata;
}

sub extract_archive_id {
my ($oneshot) = @_;
return if ( !$oneshot || length($oneshot) < 40 );
if ( ( lc $oneshot ) =~ m/([0-9a-f]{40,})/ ) {
return $1 if length($1) == 40;
}
return;
}

sub read_params {
my %plugin_info = plugin_info();
my $lrr_info = $_[1];
my @param_cfg = @{ $plugin_info{parameters} };

my %params;
$params{lrr_info} = $lrr_info;
$params{oneshot} = $lrr_info->{oneshot_param};
for ( my $i = 0; $i < scalar @param_cfg; $i++ ) {
my $value = $_[ $i + 2 ] || $param_cfg[$i]->{default};
$params{ $param_cfg[$i]->{name} } = $value;
}
return \%params;
}

1;
19 changes: 17 additions & 2 deletions lib/LANraragi/Utils/Database.pm
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ use LANraragi::Utils::Logging qw(get_logger);

# Functions for interacting with the DB Model.
use Exporter 'import';
our @EXPORT_OK =
qw(redis_encode redis_decode invalidate_cache compute_id change_archive_id set_tags set_title set_summary set_isnew get_computed_tagrules save_computed_tagrules get_archive_json get_archive_json_multi get_tankoubons_by_file);
our @EXPORT_OK = qw(
redis_encode redis_decode invalidate_cache compute_id change_archive_id set_tags set_title set_summary set_isnew get_computed_tagrules save_computed_tagrules get_tankoubons_by_file
get_archive get_archive_json get_archive_json_multi get_tags);

# Creates a DB entry for a file path with the given ID.
# This function doesn't actually require the file to exist at its given location.
Expand Down Expand Up @@ -135,6 +136,14 @@ sub add_pagecount ( $redis, $id ) {
$redis->hset( $id, "pagecount", scalar @images );
}

# Retrieves the archive's info as hash (empty if not found)
sub get_archive ($id) {
my $redis = LANraragi::Model::Config->get_redis;
my %hash = $redis->hgetall($id);
$redis->quit();
return %hash;
}

# Builds a JSON object for an archive registered in the database and returns it.
# If you need to get many JSONs at once, use the multi variant.
sub get_archive_json ( $redis, $id ) {
Expand Down Expand Up @@ -187,6 +196,12 @@ sub get_archive_json_multi (@ids) {
return @archives;
}

sub get_tags ($id) {
my %archive_info = get_archive($id);
return undef if ( !%archive_info );

Check failure on line 201 in lib/LANraragi/Utils/Database.pm

View workflow job for this annotation

GitHub Actions / perlcritic results

"return" statement with explicit "undef"

See page 199 of PBP
Raw output
    Returning `undef' upon failure from a subroutine is pretty common. But    if the subroutine is called in list context, an explicit `return undef;'    statement will return a one-element list containing `(undef)'. Now if    that list is subsequently put in a boolean context to test for failure,    then it evaluates to true. But you probably wanted it to be false.      sub read_file {          my $file = shift;          -f $file || return undef;  #file doesn't exist!          #Continue reading file...      }      #and later...      if ( my @data = read_file($filename) ){          # if $filename doesn't exist,          # @data will be (undef),          # but I'll still be in here!          process(@data);      }      else{          # This is my error handling code.          # I probably want to be in here          # if $filname doesn't exist.          die "$filename not found";      }    The solution is to just use a bare `return' statement whenever you want    to return failure. In list context, Perl will then give you an empty    list (which is false), and `undef' in scalar context (which is also    false).      sub read_file {          my $file = shift;          -f $file || return;  #DWIM!          #Continue reading file...      }
return $archive_info{tags};
}

# Internal function for building an archive JSON.
sub build_json ( $id, %hash ) {

Expand Down
Loading

0 comments on commit 1ed1757

Please sign in to comment.