User:AnomieBOT/source/tasks/CommonsFileTemplateFixer.pm: Difference between revisions
Content deleted Content added
Updating published sources: CommonsFileTemplateFixer: * Fix logic for "ineligible for copy" check. |
Updating published sources: CommonsFileTemplateFixer: * Handle the possibility of too many categories to query with the edit token. AnomieBOT::API: * Fix adding of the xxlimit parameter for stuff queried with the edit token. Apparently this was broken since 2017, sigh. |
||
(4 intermediate revisions by the same user not shown) | |||
Line 1:
{{ombox|type=notice|text= Approval requested 2025-08-26, in trial.<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 87]]}}
<syntaxhighlight lang="perl">
package tasks::CommonsFileTemplateFixer;
Line 10:
Task: CommonsFileTemplateFixer
BRFA: Wikipedia:Bots/Requests for approval/AnomieBOT 87
Status:
Created: 2025-08-25
Line 54:
=for info
Approval requested 2025-08-26, in trial.<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 87]]
=cut
sub approved {
return 6;
}
sub is_trial_done {
my ($self, $api) = @_;
return ! $self->check_for_trial( $api, 1, 2, 7, 8, 9, 11, 15);
}
sub update_trial_info {
my ($self, $api) = @_;
my $trialinfo = $api->store->{'trialinfo'} // {};
my $txt = "Trial status for [[Wikipedia:Bots/Requests for approval/AnomieBOT 87]].\n\n";
$txt .= "<onlyinclude>\n";
$txt .= qq({| class="wikitable"\n);
$txt .= "|-\n";
$txt .= "! Task !! Count !! Edits\n";
foreach my $t (1, 2, 7, 8, 9, 11, 15) {
my @revids = @{ $trialinfo->{$t} // [] };
$txt .= "|-\n",
$txt .= "| $t || " . scalar( @revids ) . " || " . join( " ", map { "[[Special:Diff/$_|$_]]" } @revids ) . "\n";
}
$txt .= qq(|}\n);
$txt .= "</onlyinclude>";
my $title = 'User:AnomieBOT/CommonsFileTemplateFixer trial info';
my $tok=$api->edittoken( $title, EditRedir => 1, NoShutoff => 1, NoExclusion => 1 );
if ( $tok->{'code'} ne 'success' ) {
$api->warn( "Failed to get edit token for $title: " . $tok->{'error'} . "\n" );
return 0;
}
my $intxt = $tok->{'revisions'}[0]{'slots'}{'main'}{'*'} // '';
$intxt =~ s/\s*$//;
if ( $intxt ne $txt ) {
$api->log( "Updating trial info in $title" );
my $res = $api->edit( $tok, $txt, "Updating bot trial info", 0, 1 );
if ( $res->{'code'} ne 'success' ) {
$api->warn( "Save failed for $title: " . $res->{'error'} . "\n" );
return 0;
}
}
return 1;
}
sub check_for_trial {
my ($self, $api, @tasks) = @_;
my $trialinfo = $api->store->{'trialinfo'} // {};
foreach my $t (@tasks) {
return 1 if @{ $trialinfo->{$t} // [] } < 10;
}
return 0;
}
sub save_trial_edit {
my ($self, $api, $revid, @tasks) = @_;
my $trialinfo = $api->store->{'trialinfo'} // {};
foreach my $t (@tasks) {
$trialinfo->{$t} //= [];
push @{ $trialinfo->{$t} }, $revid;
}
$api->store->{'trialinfo'} = $trialinfo;
}
Line 66 ⟶ 131:
my $capi = $api->copy( wikibase => 'https://commons.wikimedia.org/w/', assert => 'user' );
$api->task('CommonsFileTemplateFixer', 0, 0, qw/d::Talk d::Redirects d::Templates d::Trial/);
$screwup='Report errors at [[User:' . $api->user . '/shutoff/CommonsFileTemplateFixer]]';
return 60 unless $self->update_trial_info( $api );
return $api->trial_complete( 'AnomieBOT 87' ) if $self->is_trial_done( $api );
my $docopybotuntil = $self->{'docopybotuntil'} // 0;
Line 133 ⟶ 201:
keys %goodcopycats,
);
my $qcatlimit = $api->paramLimit( 'query+categories', 'categories' );
if ( ! $self->{'iter'} ) {
$api->log( "Beginning " . ( $docopybotuntil ? 'bot-copy' : 'main' ) . " run" );
$self->{'broken'} = 0;
$self->{'iter'} = $api->iterator(
Line 145 ⟶ 215:
cllimit => 'max',
);
} else {
$api->log( "Continuing " . ( $docopybotuntil ? 'bot-copy' : 'main' ) . " run" );
}
Line 170 ⟶ 242:
next if ( $self->{'iter'}->iterval eq $r{'Template:Copy to Wikimedia Commons'} && ( $api->store->{"copyCheckTs:$title"} // 0 ) ge $copyCheckTs );
my $tok=$api->edittoken( $title, EditRedir => 1,
if ( $tok->{'code'} eq 'shutoff' ) {
$api->warn( "Task disabled: " . $tok->{'content'} . "\n" );
Line 228 ⟶ 300:
my @summary = ();
my %trialtasks = ();
my $fail = 0;
my $outtxt = $api->process_templates( $intxt, sub {
Line 242 ⟶ 315:
# Is keep-localed? Remove it.
if ( $isKeepLocal ) {
$trialtasks{15} = 1;
push @summary, 'remove {{Now Commons}} as files tagged {{Keep local}} are not eligible for [[WP:CSD#G8]]';
return '';
Line 266 ⟶ 340:
if ( @{ $cfile->{'templates'} // [] } ) {
my $t = $cfile->{'title'};
$trialtasks{7} = 1;
push @summary, "replace {{Now Commons}} with {{Nominated for deletion on Commons}} as [[c:$t]] is nominated for deletion";
$summary[$#summary] .= ' (and dedupe)' if $hasDelNomOnCommons{$t} // 0;
Line 277 ⟶ 352:
# If it needs a date, add one.
if ( ! defined( $p{'date'} ) || ! is_valid_date( $p{'date'} ) ) {
$trialtasks{11} = 1;
push @summary, "date {{Now Commons}}";
my $dt = "|date=" . strftime( '%-d %B %Y', gmtime );
Line 295 ⟶ 371:
return undef;
} elsif ( $tpl eq $r{'Template:Copy to Wikimedia Commons'} ) {
# Already on Commons?
my @dups = map { my $x = 'File:' . $_->{'name'}; $x =~ s/_/ /g; $x; } grep { exists( $_->{'shared'} ) } @{ $tok->{'duplicatefiles'} // [] };
if ( @dups ) {
@dups = ( $title ) if ( grep { $_ eq $title } @dups ); # Prefer matching title.
$trialtasks{1} = 1;
if ( $isKeepLocal ) {
my $d = $dups[0];
$trialtasks{15} = 1;
push @summary, "remove {{Copy to Wikimedia Commons}}, on Commons as [[c:$d]] but tagged {{Keep local}} here";
return '';
}
push @summary, "replace {{Copy to Wikimedia Commons}} with {{Now Commons}}, on Commons as " . join( " ", map { "[[c:$_]]" } @dups );
my $sidx = $#summary;
my @ret = ();
foreach my $d (@dups) {
$summary[$sidx] .= ' (and dedupe)' if $sidx >= 0 && $hasNowCommons{$d} // 0;
$sidx = -1 if $hasNowCommons{$d} // 0;
next if $hasNowCommons{$d} // 0; # Dedupe
$hasNowCommons{$d} = 1;
# Check if it's already up for deletion on Commons.
my $cfile;
if ( defined( $cache{'info'}{$d} ) ) {
$cfile = $cache{'info'}{$d};
} else {
$cfile = $self->get_commons_file_info( $capi, $d );
if ( ! $cfile ) {
$fail = 1;
return undef;
}
$cache{'info'}{$d} = $cfile;
}
if ( @{ $cfile->{'templates'} // [] } ) {
my $t = $cfile->{'title'};
$trialtasks{7} = 1;
push @summary, "replace {{Now Commons}} with {{Nominated for deletion on Commons}} as [[c:$t]] is nominated for deletion";
$summary[$#summary] .= ' (and dedupe)' if $hasDelNomOnCommons{$t} // 0;
next if $hasDelNomOnCommons{$t} // 0; # Dedupe
$hasDelNomOnCommons{$t} = 1;
$t =~ s/^File://;
$t = "1=$t" if $t =~ /=/;
push @ret, "{{Nominated for deletion on Commons|$t}}";
} else {
$d = "1=$d" if $d =~ /=/;
push @ret, "{{Now Commons|$d|date=" . strftime( '%-d %B %Y', gmtime ) . "|bot=" . $api->user . "}}";
}
}
return join( "\n", @ret );
}
# Ineligible?
if ( ! $isCopyToCommonsHuman ) {
foreach my $cat (@{ $tok->{'categories'} // [] }) {
if ( defined( $badcopycats{$cat->{'title'}} ) ) {
$trialtasks{2} = 1;
push @summary, 'remove {{Copy to Wikimedia Commons}}, ineligible due to [[' . $cat->{'title'} . ']] (use `human` parameter to override)';
return '';
}
}
}
Line 396 ⟶ 457:
if ( $cfile->{'missing'} // 0 && @{$cfile->{'logs'}} ) {
my $t = $cfile->{'title'};
$trialtasks{8} = 1;
push @summary, "replace {{Nominated for deletion on Commons}} with {{Deleted on Commons}} as [[c:$t]] was deleted";
$summary[$#summary] .= ' (and dedupe)' if $hasDeletedOnCommons{$t} // 0;
Line 407 ⟶ 469:
if ( ! ( $cfile->{'missing'} // 0 ) && ! @{ $cfile->{'templates'} // [] } ) {
my $t = $cfile->{'title'};
$trialtasks{9} = 1;
if ( $isKeepLocal ) {
$trialtasks{15} = 1;
push @summary, "remove {{Nominated for deletion on Commons}}, as [[c:$t]] is no longer tagged for deletion and it's tagged {{Keep local}} here";
return '';
Line 432 ⟶ 496:
if ( $outtxt ne $intxt ) {
if ( ! $self->check_for_trial( $api, keys %trialtasks ) ) {
return 0 if time() > $endtime;
next;
}
my $summary = ucfirst( join( '; ', @summary ) );
$api->log( "$summary in $title" );
Line 438 ⟶ 506:
$api->warn( "Save failed for $title: " . $res->{'error'} . "\n" );
$self->{'broken'} = 1;
} elsif ( exists( $res->{'edit'}{'newrevid'} ) ) {
$self->save_trial_edit( $api, $res->{'edit'}{'newrevid'}, keys %trialtasks );
return 0 if $self->is_trial_done( $api );
}
} else {
Line 446 ⟶ 517:
}
$api->log( "Finished " . ( $docopybotuntil ? 'bot-copy' : 'main' ) . " run" );
$self->{'iter'} = undef;
|