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

Content deleted Content added
AnomieBOT (talk | contribs)
Updating published sources: General: * BREAKING CHANGE: $api->edittoken() now takes named parameters for parameters other than $page. * $api->edittoken() has two new options, to disable the shutoff or {{tl|bots}} checking. SourceUploader: * Add two m
AnomieBOT (talk | contribs)
Updating published sources: AutoAssessor2: * Retire this task too, I think Lua now tends to handle this sort of thing.
 
(21 intermediate revisions by the same user not shown)
Line 1:
{{ombox|type=notice|text= Approved 2008-12-07<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 15]]}}
{{ombox|type=notice|text= Retired 2025-02-01. Haven't had call to run this in a while, and I think Lua now handles this sort of thing anyway.}}
<sourcesyntaxhighlight lang="perl">
package tasks::AutoAssessor2;
 
Line 7 ⟶ 8:
=begin metadata
 
Bot: AnomieBOT
Task: AutoAssessor2
BRFA: Wikipedia:Bots/Requests for approval/AnomieBOT 15
Status: ApprovedInactive 20082025-1202-0701
Rate: Max 6 edits/minute
Created: 2008-11-26
OnDemand: true
Line 31 ⟶ 32:
 
# Request link, for edit summary.
my $req="[[User talk:Anomie#CouldAnomieBOT/req/WikiProject you run AnomieBOTRadio 8Stations again?1|request]]";
 
# Increment this number every time a new run is started, so we don't have to
# mess around with deleting previous runs' database entries.
my $seq=14;
 
# If any of the 'verify' or 'params' functions need the page contents, set this
Line 49 ⟶ 50:
# canonicalize => If defined, any instance of the template will be renamed to
# this when edits are done to the page.
# addempty => If true, add empty "class=" and "importance=" to all banners.
# verify => If defined, must be a subroutine that will be called with the
# page and talkpage results. The subroutine must return a boolean value,
Line 55 ⟶ 57:
# results, the template name, and the existing template parameters. It
# should adjust the parameter array as necessary.
my %cfg_templates=(;
%cfg_templates=(
'Physics' => {
ns'WikiProject Radio Stations' => {
stubmeta => [ 'stub', qr/\S.*/s, undef, undef ]1,
redirectstubauto => [ 'redirect', qr/redirect|red|redir/i, 'NA', qr/na/i ]undef,
disambigcanonicalize => [ 'disambig',WikiProject qr/disambig|dab/i,Radio Stations'NA', qr/na/i ],
0addempty => [ undef, undef, undef, undef ]0,
2verify => [ 'NA'undef, qr/na/i, 'NA', qr/na/i ], # User
4params => [ 'project'undef, qr/project/i, 'NA', qr/na/i ], # Wikipedia
6 => [ 'image', qr/image/i, 'NA', qr/na/i ], # Image
8 => [ 'NA', qr/na/i, 'NA', qr/na/i ], # MediaWiki
10 => [ 'template', qr/template|templ|temp/i, 'NA', qr/na/i ], # Template
12 => [ 'NA', qr/na/i, 'NA', qr/na/i ], # Help
14 => [ 'category', qr/category|categ|cat/i, 'NA', qr/na/i ], # Category
100 => [ 'portal', qr/portal/i, 'NA', qr/na/i ], # Portal
},
stubauto => 'auto',
canonicalize => undef,
verify => undef,
params => undef,
},
);
 
Line 82 ⟶ 73:
my $self=$class->SUPER::new();
$self->{'did_templates'}=0;
$self->{'config loaded'}=0;
bless $self, $class;
return $self;
Line 90 ⟶ 82:
=for info
Approved 2008-12-07<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 15]]
 
=for info
Retired 2025-02-01. Haven't had call to run this in a while, and I think Lua now handles this sort of thing anyway.
 
=cut
Line 101 ⟶ 96:
my $res;
 
$api->task('AutoAssessor2', 0, 10, qw/d::WikiProjectTagging d::Redirects/);
 
$api->read_throttle(0);
# Load configs, if necessary
$api->edit_throttle(10);
if(!$self->{'config loaded'}){
while(my ($banner,$cfg)=each %cfg_templates){
next unless exists($cfg->{'meta'});
if(my $resae=$cfg->{'codeaddempty'} ne// 'success'){0;
$cfg=$api->WPBMetaConfig($cfg->{'meta'}, %$cfg) if exists($cfg->{'meta'});
$cfg->{'addempty'}=$ae;
$cfg_templates{$banner}=$cfg;
);}
$self->{'config loaded'}=1;
},
 
if(!exists($api->store->{'metaseq'}) || $api->store->{'metaseq'}<$seq){
delete $api->store->{'template'};
delete $api->store->{'geicontinue'};
$api->store->{'metaseq'}=$seq;
}
 
# List of templates to assess in this task
if(!$self->{'did_templates'}){
while(my ($t,$c)=each %cfg_templates){
my %redirs=$capi->{'names'}=[ redirects_to_resolved("Template:$t" ]);
$res=$api->queryif([],exists($redirs{''})){
list$api->warn("Failed to get redirects to => Template:$t".$redirs{''}{'backlinkserror',}."\n");
bltitle => "Template:$t",
blfilterredir => 'redirects',
bllimit => 'max',
);
if($res->{'code'} ne 'success'){
$self->warn("Failed to retrieve redirect list for $t: ".$res->{'error'}."\n");
return 60;
}
push @{$c->{'names'}},=[ $_->{'title'}keys foreach%redirs (@{$res->{'query'}{'backlinks'}})];
}
$self->{'did_templates'}=1;
Line 128 ⟶ 133:
}
 
# Spend a max of 510 minutes on this task before restarting
my $endtime=time()+300600;
 
foreach my $template (sort keys %cfg_templates){
next if(exists($api->store->{'template'}) && $template lt $api->store->{'template'});
$api->store->{'template'}=$template;
my %q1=(
generator => 'embeddedin',
geititle => "Template:$template",
geilimit => 'max1000', # 'max' seems to time out fairly often
prop => 'info',
inprop => 'subjectid',
);
$q1{'geicontinue'}=$api->store->{'geicontinue'} if exists($api->store->{'geicontinue'});
my %q2=(
pageids => '',
prop => 'info|categories|templates',
cllimit => 'max',
stubauto tllimit => 'automax',
);
if($need_page_contents){
$q2{'prop'}.='|revisions';
$q2{'rvprop'}='content';
$q2{'rvslots'}='main';
}
 
# Get the list of pages to check
do {
$api->store->{'geicontinue'}=$q1{'geicontinue'} if exists($q1{'geicontinue'});
$res=$api->query(%q1);
if($res->{'code'} ne 'success'){
$selfapi->warn("Failed to retrieve transclusion list for $template: ".$res->{'error'}."\n");
return 60;
}
Line 164 ⟶ 175:
# Collect page IDs of articles for found talk pages
my @pageids=map { exists($_->{'subjectid'})?($_->{'subjectid'}):() } values %{$res->{'query'}{'pages'}};
@pageids=grep { !exists($api->store->{$_}) || $api->store->{$_}<$seq } @pageids;
 
# Look up info for found pages, in blocks of 500 because that's the
Line 171 ⟶ 183:
$res=$api->query(%q2);
foreach my $page (values %{$res->{'query'}{'pages'}}){
return 0 if $api->halting;
 
my $pageid=$page->{'pageid'};
my $title=$page->{'title'};
mynext if(exists($checked=api->store->{$pageid}) && $api->fetch(store->{$pageid}>=$seq);
next if(defined($checked) && $$checked>=$seq);
 
my $ns=$page->{'ns'};
Line 181 ⟶ 194:
if(($ns&1)==1){
# Talk page, ignore it.
$api->store(->{$pageid, \}=$seq);
next;
}
Line 198 ⟶ 211:
}
 
$selfapi->warnlog("Assessing $title\n");
 
# Ok, check the page
my $tok=$api->edittoken($talk, EditRedir => 1, 'categories'=>1, 'templates'=>1);
if($tok->{'code'} eq 'shutoff'){
$selfapi->warn("Task disabled: ".$tok->{'content'}."\n");
return 300;
}
if($tok->{'code'} ne 'success'){
$selfapi->warn("Failed to get edit token for $talk: ".$tok->{'error'}."\n");
next;
}
Line 213 ⟶ 226:
 
# Get page text
my $intxt=$tok->{'revisions'}[0]{'slots'}{'main'}{'*'};
my ($outtxt,$nowiki)=$selfapi->strip_nowiki($intxt);
my $any=0;
my @found=();
 
$outtxt=$selfapi->process_templates($outtxt, sub {
my $name=shift;
my @params=@{shift()};
Line 227 ⟶ 240:
return undef unless exists($templates{"Template:$name"});
my %cfg=%{$templates{"Template:$name"}};
return undef unless existsif(defined($cfg{'nsverify'}{) && !$nscfg{'verify'}($api,$page,$tok));
my ($class,$classre,$imp,$impre)=@{$cfg{'ns'}{$ns}};
returnmy undef if(defined($cfg{'verify'}) && !$cfg{'verify'}($page,$tok))impname;
my $impname=if(exists($cfg{'importancens'}{$ns})?$cfg){'importance'}:'importance';
($class,$classre,$imp,$impre)=@{$cfg{'ns'}{$ns}};
$impname=exists($cfg{'importance'})?$cfg{'importance'}:'importance';
bltitle => "Template:$t",} else {
($class,$classre,$imp,$impre)=(undef,undef,undef,undef);
$impname=undef;
bllimit => 'max',}
 
my $orig_param=join('|', @params);
 
# Auto-assess
if($class && defined($classre) && !grep(/^\s*class\s*=\s*$classre\s*$/s, @params)){
push @params, "class=$class" unless(grep(s/^(\s*class\s*=(?:\s*(?=\S))?).*?(\s*)$/$1$class$2/s, @params));
if(defined($cfg{'stubauto'})){
Line 246 ⟶ 265:
}
}
if($imp && defined($impre) && !grep(/^\s*$impname\s*=\s*$impre\s*$/s, @params)){
push @params, "$impname=$imp" unless(grep(s/^(\s*$impname\s*=(?:\s*(?=\S))?).*?(\s*)$/$1$imp$2/s, @params));
$self->warn("$summary in $talk\n");}
 
# If requested to add empty parameters, do so
if($cfg{'addempty'} // 0){
push @params, "class=" unless grep(/^\s*class\s*=/, @params);
push @params, "$impname=" if(defined($impname) && !grep(/^\s*$impname\s*=/, @params));
}
 
# Do extra parameters, if necessary
$cfg{'params'}($api,$page,$tok,$name,\@params) if defined($cfg{'params'});
 
my $new_param=join('|', @params);
Line 257 ⟶ 282:
$any=1;
my @s=();
push @s, "class=$class" if($class && defined($classre));
push @s, "$impname=$imp" if($imp && defined($impre));
push @found, "{{$name}} (".join(' ', @s).')';
}
Line 266 ⟶ 291:
# edits.
push @params, "class=" unless grep(/^\s*class\s*=/, @params);
push @params, "$impname=" unlessif(defined($impname) && !grep(/^\s*$impname\s*=/, @params));
 
if(defined($cfg{'canonicalize'})){
Line 275 ⟶ 300:
return "{{$oname|".join("|",@params)."}}";
});
$outtxt=$selfapi->replace_nowiki($outtxt, $nowiki);
 
# Need to edit?
Line 281 ⟶ 306:
$found[-1]='and '.$found[-1] if @found>1;
my $summary="Assessing ".join((@found>2)?', ':' ', @found)." per $req";
 
$self->warn("$summary in $talk\n");
my @cleanup=();
$outtxt=$api->WPBfixshell($outtxt, \@cleanup);
next if(definedref($checkedouttxt) &&eq $$checked>=$seq'HASH');{
$selfapi->warn("Failed to retrieve redirect list forProcessing $ttitle failed: ".$resouttxt->{'error'}."\n");
next;
}
$summary.="; general banner cleanup (".join(', ', @cleanup).")" if @cleanup;
 
$api->log("$summary in $talk");
my $r=$api->edit($tok, $outtxt, $summary, 1, 1);
if($r->{'code'} ne 'success'){
$selfapi->warn("Write failed on $talk: ".$r->{'error'}."\n");
next;
}
} else {
$selfapi->warnlog("Nothing to do in $talk\n");
}
 
# Save checked revision
$api->store(->{$pageid, \}=$seq);
 
# If we've been at it long enough, let another task have a
Line 300 ⟶ 334:
}
} while(exists($q1{'geicontinue'}));
delete $api->store->{'geicontinue'};
}
delete $api->store->{'template'};
 
# No more pages to check, try again in 10 minutes or so in case of errors.
$api->log("AutoAssessor2 may be DONE!");
return 600;
}
Line 308 ⟶ 345:
1;
 
</syntaxhighlight>
</source>