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

Content deleted Content added
AnomieBOT (talk | contribs)
Updating published sources: AutoAssessor2: * New task, AutoAssessor2. General: * A bit more sanitizing of the <code>$name</code> parameter.
 
AnomieBOT (talk | contribs)
Updating published sources: AutoAssessor2: * Retire this task too, I think Lua now tends to handle this sort of thing.
 
(29 intermediate revisions by the same user not shown)
Line 1:
{{ombox|type=notice|text= ApprovalApproved requested2008-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.}}
<source lang="perl">
<syntaxhighlight lang="perl">
package tasks::AutoAssessor2;
 
Line 7 ⟶ 8:
=begin metadata
 
Bot: AnomieBOT
Task: AutoAssessor2
Task: AutoAssessor2
BRFA: Wikipedia:Bots/Requests for approval/AnomieBOT 15
BRFA: Wikipedia:Bots/Requests for approval/AnomieBOT 15
Status: BRFA
Status: Inactive 2025-02-01
Rate: Max 6 edits/minute
Created: 2008-11-26
OnDemand: true
 
Redirects and disambiguation pages will be automatically assessed with
Line 27 ⟶ 30:
use vars qw/@ISA/;
@ISA=qw/AnomieBOT::Task/;
 
# Request link, for edit summary.
my $req="[[User:AnomieBOT/req/WikiProject Radio Stations 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=4;
 
# If any of the 'verify' or 'params' functions need the page contents, set this
# flag.
my $need_page_contents=0;
 
# Configuration for each template that is going to be applied. Options are:
# ns => Hash mapping namespace numbers (or 'stub', 'redirect', 'disambig',
# or '' for ns 0) to an array [ $class, $classre, $imp, $impre].
# stubauto => If defined, add this "=yes" when tagging a stub.
# importance => If specified, this is the name of the "importance" parameter
# instead of "importance".
# 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,
# true if the page should be tagged and false otherwise.
# params => Subroutines that will be called with the page and talkpage
# results, the template name, and the existing template parameters. It
# should adjust the parameter array as necessary.
my %cfg_templates;
%cfg_templates=(
'WikiProject Radio Stations' => {
meta => 1,
stubauto => undef,
canonicalize => 'WikiProject Radio Stations',
addempty => 0,
verify => undef,
params => undef,
},
);
 
sub new {
my $class=shift;
my $self=$class->SUPER::new();
$self->{'did_templates'}=0;
$self->{'config loaded'}=0;
bless $self, $class;
return $self;
Line 38 ⟶ 81:
 
=for info
ApprovalApproved requested2008-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
 
sub approved {
return 0-1;
}
 
Line 50 ⟶ 96:
my $res;
 
$api->task('AutoAssessor2', 0, 10, qw/d::WikiProjectTagging d::Redirects/);
$api->read_throttle(0);
$api->edit_throttle(10);
 
# Load configs, if necessary
# List of templates to assess in this task
if(!$self->{'config loaded'}){
my @templates=('Physics');
while(my ($banner,$cfg)=each %cfg_templates){
my $req='[[User talk:Anomie#Could you run AnomieBOT 8 again?]]';
next unless exists($cfg->{'meta'});
my $canonicalize=0;
my $ae=$cfg->{'addempty'} // 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){
# Spend a max of 5 minutes on this task before restarting
delete $api->store->{'template'};
my $endtime=time()+300;
delete $api->store->{'geicontinue'};
$api->store->{'metaseq'}=$seq;
}
 
# Get a listList of templates redirectingto toassess in ourthis targetstask
if(!$self->{'did_templates'}){
while(my ($t,$c)=each %cfg_templates){
my %redirs=$api->redirects_to_resolved("Template:$t");
if(exists($redirs{''})){
$api->warn("Failed to get redirects to Template:$t".$redirs{''}{'error'}."\n");
return 60;
}
$c->{'names'}=[ keys %redirs ];
}
$self->{'did_templates'}=1;
}
my %templates=();
foreach while(my ($templatet,$c)=each (@templates%cfg_templates){
$templates{"Template:$template"_}=$templatec foreach (@{$c->{'names'}});
$res=$self->fullquery($api, undef,
list => 'backlinks',
bltitle => "Template:$template",
blfilterredir => 'redirects',
bllimit => 'max',
);
$templates{$_->{'title'}}=$template foreach (@{$res->{'query'}{'backlinks'}});
}
 
# Spend a max of 10 minutes on this task before restarting
foreach my $template (@templates){
my $endtime=time()+600;
 
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',
tllimit => 'max',
);
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 104 ⟶ 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 109 ⟶ 181:
while(@pageids){
$q2{'pageids'}=join('|', splice(@pageids, 0, 500));
$res=$selfapi->fullqueryquery($api, undef, %q2);
foreach my $page (values %{$res->{'query'}{'pages'}}){
myreturn 0 if $pageid=$_api->{'pageid'}halting;
my $checked=$api->fetch($pageid);
next if defined($checked);
 
my ($imp,pageid=$impre,$class,$classre,$auto)=('',undef,page->{'pageid',undef,0)};
if(my $_title=$page->{'nstitle'}==2){ # User;
next if(exists($api->store->{$pageid}) && $classapi->store->{$pageid}>='NA'$seq);
 
$classre=qr/na/i;
my $impns=$page->{'NAns'};
my $impauto=qr/na/i0;
}my elsif($_->{'ns'}==4){ # Wikipediatalk;
if(($classns&1)='project';=1){
$classre=qr/project/i;# Talk page, ignore it.
$impapi->store->{$pageid}='NA'$seq;
$imp=qr/na/i;
} elsif($_->{'ns'}==6){ # Image
$class='image';
$classre=qr/image/i;
$imp='NA';
$imp=qr/na/i;
} elsif($_->{'ns'}==8){ # MediaWiki
$class='NA';
$classre=qr/na/i;
$imp='NA';
$imp=qr/na/i;
} elsif($_->{'ns'}==10){ # Template
$class='template';
$classre=qr/template|templ|temp/i;
$imp='NA';
$imp=qr/na/i;
} elsif($_->{'ns'}==12){ # Help
$class='NA';
$classre=qr/na/i;
$imp='NA';
$imp=qr/na/i;
} elsif($_->{'ns'}==14){ # Category
$class='category';
$classre=qr/category|categ|cat/i;
$imp='NA';
$imp=qr/na/i;
} elsif($_->{'ns'}==100){ # Portal
$class='portal';
$classre=qr/portal/i;
$imp='NA';
$imp=qr/na/i;
} elsif($_->{'ns'}!='0'){
$class='NA';
$classre=qr/na/i;
$imp='NA';
$imp=qr/na/i;
} elsif(exists($_->{'redirect'})){
$class='redirect';
$classre=qr/redirect|red|redir/i;
$imp='NA';
$imp=qr/na/i;
} elsif(grep { $_->{'title'} eq 'Category:All disambiguation pages' } @{$_->{'categories'}}){
$class='disambig';
$classre=qr/disambig|dab/i;
$imp='NA';
$imp=qr/na/i;
} elsif(grep { $_->{'title'}=~/^Category:.* stubs?$/i } @{$_->{'categories'}}){
$class='stub';
$classre=qr/\S.*/is;
$auto=1;
} else {
# Save checked revision
$api->store($pageid, \1);
next;
}
if($ns==0){
 
my if(exists($title=$_page->{'titleredirect'};)){
if( $_->{'ns='}==0){redirect';
} elsif(grep { $_->{'title="Talk'} eq 'Category:All disambiguation pages' } @{$title";page->{'categories'}}){
$ns='disambig';
} elsif(grep { $_->{'title'}=~/^Category:.* stubs?$/i } @{$page->{'categories'}}){
$ns='stub';
$auto=1;
}
$talk="Talk:$title";
} else {
($talk=$title)=~s/^([^:]+*):/$1 talk:/;
}
 
$selfapi->warnlog("Assessing $title\n");
 
# Ok, check the page
my $tok=$api->edittoken($titletalk, 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 $titletalk: ".$tok->{'error'}."\n");
next;
}
Line 203 ⟶ 226:
 
# Get page text
my $intxt=$tok->{'revisions'}[0]{'slots'}{'main'}{'*'};
my ($outtxt,$nowiki)=$api->strip_nowiki($intxt);
 
# Now, we actually perform the replacement
my @found=();
my $any=0;
my $outtxt@found=$self->process_templates($intxt, sub {);
 
$outtxt=$api->process_templates($outtxt, sub {
my $name=shift;
my @params=@{shift()};
Line 216 ⟶ 239:
 
return undef unless exists($templates{"Template:$name"});
my %cfg=%{$templates{"Template:$name"}};
return undef if(defined($cfg{'verify'}) && !$cfg{'verify'}($api,$page,$tok));
my ($class,$classre,$imp,$impre);
my $impname;
if(exists($cfg{'ns'}{$ns})){
($class,$classre,$imp,$impre)=@{$cfg{'ns'}{$ns}};
$impname=exists($cfg{'importance'})?$cfg{'importance'}:'importance';
} else {
($class,$classre,$imp,$impre)=(undef,undef,undef,undef);
$impname=undef;
}
 
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($autocfg{'stubauto'})){
pushmy @params, "auto$a=yes" unless grep(s/^(\s*auto\s*=\s*).*?(\s*)$/$1yes$2/s, @params)cfg{'stubauto'};
} else if($auto){
push @params, "$a=yes" grepif(!grep(s/^(\s*auto\Q$a\E\s*=(?:\s*(?=\S))?).*?(\s*)$/$1yes$2/s, @params));
} else {
@params = grep(!/^\s*\Q$a\E\s*=/, @params);
}
}
}
if($imp && defined($impre) && !grep(/^\s*importance$impname\s*=\s*$impre\s*$/s, @params)){
push @params, "importance$impname=NA$imp" unless (grep(s/^(\s*importance$impname\s*=(?:\s*(?=\S))?).*?(\s*)$/${1}NA$imp$2/s, @params));
}
 
# 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);
pushif($orig_param @found,ne "{{$name}}";new_param){
$any=1 if $orig_param ne $new_paramany=1;
if my @s=($canonicalize){;
mypush $n@s, "class=$templates{class"Template: if($name"}class && defined($classre));
push @s, "$impname=$imp" if($imp && defined($impre));
push @found, "{{$name}} (".join(' ', @s).')';
}
 
# Add empty parameters for future human taggers,
# after setting $any so we don't make useless
# edits.
push @params, "class=" unless grep(/^\s*class\s*=/, @params);
push @params, "$impname=" if(defined($impname) && !grep(/^\s*$impname\s*=/, @params));
 
if(defined($cfg{'canonicalize'})){
my $n=$cfg{'canonicalize'};
$oname=~s/_/ /g;
$oname=~s/^(\s*)\S(?:.*\S)?(\s*)$/$1$n$2/is;
}
return "{{$oname|$new_param".join("|",@params)."}}";
});
$outtxt=$api->replace_nowiki($outtxt, $nowiki);
 
# Need to edit?
if($outtxt ne $intxt && $any){
$found[-1]='and '.$found[-1] if @found>1;
my $summary="Assessing ".join((@found>2)?', ':' ', @found)." as class=$class".(defined($impre)?" importance=$imp":"")." per $req";
 
$self->warn("$summary in $title\n");
my @cleanup=();
$outtxt=$api->WPBfixshell($outtxt, \@cleanup);
if(ref($outtxt) eq 'HASH'){
$api->warn("Processing $title failed: ".$outtxt->{'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 $titletalk: ".$r->{'error'}."\n");
next;
}
} else {
$selfapi->warnlog("Nothing to do in $title\ntalk");
}
 
# Save checked revision
$api->store(->{$pageid, \1)}=$seq;
 
# If we've been at it long enough, let another task have a
Line 265 ⟶ 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 273 ⟶ 345:
1;
 
</syntaxhighlight>
</source>