User:AnomieBOT/source/tasks/DeletionSortingCleaner.pm: Difference between revisions

Content deleted Content added
AnomieBOT (talk | contribs)
Updating published sources: d::Redirects: * Redo redirects_to and redirects_to_resolved to use the new prop=redirects. Much faster and more accurate, but let's hope I didn't introduce any bugs.
AnomieBOT (talk | contribs)
Updating published sources: DeletionSortingCleaner: * Supplemental BRFA approved!
 
(16 intermediate revisions by the same user not shown)
Line 1:
{{ombox|type=notice|text= Approved 2010-07-06<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 40]]}}
{{ombox|type=notice|text= Supplemental BFRA approved 2020-06-06<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 79]]}}
{{ombox|type=notice|text= Supplemental BFRA approved 2025-08-22<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 85]]}}
<syntaxhighlight lang="perl">
package tasks::DeletionSortingCleaner;
Line 11 ⟶ 13:
BRFA: Wikipedia:Bots/Requests for approval/AnomieBOT 40
Status: Approved 2010-07-06
+BRFA: Wikipedia:Bots/Requests for approval/AnomieBOT 79
+Status: Approved 2020-06-06
+BRFA: Wikipedia:Bots/Requests for approval/AnomieBOT 85
+Status: Approved 2025-08-22
Created: 2010-06-18
 
Perform certain tasks for [[WP:WikiProject Deletion sorting]]:
* Subst various AfD templates that should be substed
* Archive discussions for closed AfDsXfDs
* Remove duplicate XfD listings
* Remove deleted XfD listings
 
If necessary, the bot may be kept off a deletion sorting subpage by adding
Line 37 ⟶ 45:
my $class=shift;
my $self=$class->SUPER::new();
$self->{'pages'}=undef;
$self->{'lasttime'}=0;
$self->{'broken'}=0;
Line 47 ⟶ 56:
=for info
Approved 2010-07-06<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 40]]
 
=for info
Supplemental BFRA approved 2020-06-06<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 79]]
 
=for info
Supplemental BFRA approved 2025-08-22<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 85]]
 
=cut
Line 83 ⟶ 98:
 
# Load list of deletion sorting subpages to process
if ( ! defined( $self->{'pages'} ) ) {
$res=$api->query(titles=>'Wikipedia:WikiProject Deletion sorting/Compact',prop=>'links',plnamespace=>4,pllimit=>'max');
$res=$api->query(titles=>'Wikipedia:WikiProject Deletion sorting/Computer-readable.json',prop=>'revisions',rvprop=>"content",rvslots=>"main",rvlimit=>1,formatversion=>2);
if($res->{'code'} ne 'success'){
if($res->{'code'} ne 'success'){
$api->warn("Failed to get list of pages to process: ".$res->{'error'}."\n");
return 60;
}
my $json;
my @pages=sort grep m!^Wikipedia:WikiProject Deletion sorting/!, map $_->{'title'}, @{(values %{$res->{'query'}{'pages'}})[0]{'links'}};
eval { $json = JSON->new->decode( $res->{'query'}{'pages'}[0]{'revisions'}[0]{'slots'}{'main'}{'content'} // '' ); };
unless(@pages){
$api->warnif ("No pages$@ in list?"); {
$api->warn("Failed to parse Wikipedia:WikiProject Deletion sorting/Computer-readable.json: $@\n");
$self->{'broken'}=1;
$api->store->{'broken'}=1 return 300;
return 3600;}
my %pages = ();
for my $g (values %$json) {
for my $p (@$g) {
$pages{"Wikipedia:WikiProject Deletion sorting/$p"} = 1;
}
}
$self->{'pages'}=[ sort keys %pages ];
unless(@{$self->{'pages'}}){
$api->warn("No pages in list?");
$self->{'broken'}=1;
$api->store->{'broken'}=1;
return 3600;
}
}
my $endtime=time()+300;
 
while ( @{$self->{'pages'}} ) {
return 0 if $api->halting;
my $page = shift @{$self->{'pages'}};
 
for my $page (@pages){
# First, load the page to archive from. Allow for opting out using
# {{bots|optout=AnomieBOT/DeletionSortingCleaner}}
Line 118 ⟶ 151:
# pages. For each one, subst any substable templates and then see if it
# looks closed.
my $intxt=$tok->{'revisions'}[0]{'slots'}{'main'}{'*'};
my @archive=@{$api->store->{"archive $page"} // []}; # Load saved archivals
my @summary=();
my $fail=undef;
my $dups=0;
my %dups=();
my $deleted=0;
my $outtxt=$api->process_templates($intxt, sub {
return undef if defined($fail);
my $name=shift;
shift; # $params
my $wikitext=shift;
 
return undef unless $name=~m!^(?i:Wikipedia|WP) *: *((?:Articles[Aa]rticles|Miscellany[Mm]iscellany) for deletion)/(.+)$!;
my $name2=$12;
 
# Normalize. People do weird things sometimes.
$name = "Wikipedia:\u$1/$2";
 
if(exists($dups{$name})){
$dups=1;
return '';
}
$dups{$name}=1;
 
my $cannoteditreason=undef;
Line 143 ⟶ 190:
titles => $name,
rvprop => 'ids|timestamp|content|flags|user|size|comment',
rvslots => 'main',
);
$xfdtok=(values %{$res->{'query'}{'pages'}})[0];
Line 156 ⟶ 204:
}
if(exists($xfdtok->{'missing'})){
# WTF?Check for a deletion log entry.
my $res2 = $api->query(
letitle => $name,
list => 'logevents',
letype => 'delete',
lelimit => 1,
leprop => 'user|timestamp|comment',
);
if($res2->{'code'} ne 'success'){
$api->warn("Failed to retrieve logs for $name: " . $res2->{'error'} . "\n");
$fail = 60;
return undef;
}
if(exists($res2->{'query'}{'logevents'}[0])){
my $log = $res2->{'query'}{'logevents'}[0];
 
# Check whether the deletion log entry could reasonably
# belong to this listing.
my %q = (
titles => $page,
prop => 'revisions',
rvprop => 'ids|timestamp|content',
rvslots => 'main',
rvlimit => 10,
formatversion => 2,
);
my %cont = ();
my $wt = $wikitext;
do {
my $res3 = $api->query( %q );
if($res3->{'code'} ne 'success'){
$api->warn("Failed to retrieve revisions for $page: " . $res3->{'error'} . "\n");
$fail = 60;
return undef;
}
for my $rev (@{$res3->{'query'}{'pages'}[0]{'revisions'}}) {
next unless exists( $rev->{'slots'}{'main'}{'content'} ); # Revdel?
my $c = $rev->{'slots'}{'main'}{'content'};
my $found = ( $c =~ /\Q$wt\E/ );
if ( ! $found ) {
$api->process_templates($c, sub {
return undef if $found;
my $name2=shift;
shift; # $params
my $wikitext2=shift;
 
return undef unless $name2=~m!^(?i:Wikipedia|WP) *: *((?:[Aa]rticles|[Mm]iscellany) for deletion)/(.+)$!;
 
# Normalize. People do weird things sometimes.
$name2 = "Wikipedia:\u$1/$2";
 
if ( $name2 eq $name ) {
$wt = $wikitext2;
$found = 1;
}
return undef;
});
}
if ( ! $found ) {
$api->warn("XfD page $name linked from $page does not exist, deleted at $log->{'timestamp'} but newer rev $rev->{'revid'} from $rev->{'timestamp'} did not transclude it.\n");
return undef;
}
if ( $rev->{'timestamp'} le $log->{'timestamp'} ) {
$res3->{'continue'} = {};
last;
}
}
%cont = %{ $res3->{'continue'} // {} };
} while ( %cont );
 
# Deleted since it was listed, so remove from page.
$deleted=1;
push @summary, "[[$name]] (was deleted)";
return '';
}
 
$api->warn("XfD page $name linked from $page does not exist\n");
return undef;
Line 162 ⟶ 285:
 
my %substlist=();
my $xfdintxt=$xfdtok->{'revisions'}[0]{'slots'}{'main'}{'*'};
my $xfdouttxt=$api->process_templates($xfdintxt, sub {
my $name=shift;
Line 170 ⟶ 293:
return undef unless exists($xfdtemplates{"Template:$name"});
$substlist{$name}=1;
$wikitext=~s/^\{\{/{{subst:/; # }} }}
return $wikitext;
});
Line 207 ⟶ 330:
# If closed, remove from the main page and note it for archival.
# If still open, do nothing.
return undef unless $xfdintxt=~m!<div class=\x22boilerplate "[^"]*(?:afd|metadata<=[" ])[xt]fd-closed[ "]!;
my ($result,$date)=('(unknown)','(unknown)');
$result=$1 if $xfdintxt=~m!(?:result was|result of the discussion was:(?:'')?)\s*(?:'''|<(?:b|strong)>)(.+?)(?:'''|</(?:b|strong)>)!;
$date=$1 if $xfdintxt=~m!(\d\d:\d\d, \d+ \w+ \d{4} \(UTC\))!;
unshift @archive, "* [[$name|$name2]] - (".length($xfdintxt).") - $result - <small>closed $date</small>";
Line 252 ⟶ 375:
}
my $n=$page; $n=~s!^[^/]*/!!;
my $aintxt=$atok->{'revisions'}[0]{'slots'}{'main'}{'*'} // '';
$atxt=$aintxt;
if(exists($atok->{'missing'})){
Line 269 ⟶ 392:
# Now do the saving
if($outtxt ne $intxt){
$api->log("Archiving closed XfDs and/or removing duplicates from $page...");
my $summarywhat ="[[$apage|Archiving closed XfDs]]".$screwup.": ".join(" ", @summary)'';
$summarywhat .= "[[$apage|Archiving closed XfDs]]".$screwup.": [".scalar(@summary)." discussions]" if length($summary)>250@archive;
$what .= ( @archive ? ' and removing' : 'Removing' ) . ( $deleted ? ' deleted' : '' ) . ( $dups ? ( $deleted ? ' and duplicate' : ' duplicate' ) : '' ) . ' XfDs' if $deleted || $dups;
 
my $summary;
$summary=$what . $screwup . ": " . join(" ", @summary);
$summary=$what . $screwup . ": [" . scalar(@summary) . " discussions]" if length($summary)>500;
$res=$api->edit($tok, $outtxt, $summary, 0, 1);
if($res->{'code'} ne 'success'){
Line 293 ⟶ 421:
delete $api->store->{"archive $page"};
}
 
return 0 if time()>$endtime;
}
 
# Save checked revision
$self->{'pages'}=undef;
$self->{'lasttime'}=$starttime;
$self->{'broken'}=$broken;