![]() | Approved 2008-12-07 Wikipedia:Bots/Requests for approval/AnomieBOT 15 |
package tasks::AutoAssessor2;
=pod
=begin metadata
Task: AutoAssessor2
BRFA: Wikipedia:Bots/Requests for approval/AnomieBOT 15
Status: Approved 2008-12-07
Rate: Max 6 edits/minute
Redirects and disambiguation pages will be automatically assessed with
class=redirect/disambig and importance=NA, stubs will be automatically assessed
with class=stub if not already assessed, and non-article pages will be
automatically assessed with the appropriate class and importance=NA.
=end metadata
=cut
use utf8;
use strict;
use AnomieBOT::Task;
use vars qw/@ISA/;
@ISA=qw/AnomieBOT::Task/;
# Request link, for edit summary.
my $req="[[User talk:Anomie#Could you run AnomieBOT 8 again?|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=1;
# 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.
# 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=(
'Physics' => {
ns => {
stub => [ 'stub', qr/\S.*/s, undef, undef ],
redirect => [ 'redirect', qr/redirect|red|redir/i, 'NA', qr/na/i ],
disambig => [ 'disambig', qr/disambig|dab/i, 'NA', qr/na/i ],
0 => [ undef, undef, undef, undef ],
2 => [ 'NA', qr/na/i, 'NA', qr/na/i ], # User
4 => [ 'project', 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,
},
);
sub new {
my $class=shift;
my $self=$class->SUPER::new();
$self->{'did_templates'}=0;
bless $self, $class;
return $self;
}
=pod
=for info
Approved 2008-12-07<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 15]]
=cut
sub approved {
return -1;
}
sub run {
my ($self, $api)=@_;
my $res;
$api->task('AutoAssessor2');
$api->read_throttle(0);
$api->edit_throttle(10);
# List of templates to assess in this task
if(!$self->{'did_templates'}){
while(my ($t,$c)=each %cfg_templates){
$c->{'names'}=[ "Template:$t" ];
$res=$api->query([],
list => 'backlinks',
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'} foreach (@{$res->{'query'}{'backlinks'}});
}
$self->{'did_templates'}=1;
}
my %templates=();
while(my ($t,$c)=each %cfg_templates){
$templates{$_}=$c foreach (@{$c->{'names'}});
}
# Spend a max of 5 minutes on this task before restarting
my $endtime=time()+300;
foreach my $template (keys %cfg_templates){
my %q1=(
generator => 'embeddedin',
geititle => "Template:$template",
geilimit => 'max',
prop => 'info',
inprop => 'subjectid',
);
my %q2=(
pageids => '',
prop => 'info|categories',
cllimit => 'max',
);
if($need_page_contents){
$q2{'prop'}.='|revisions';
$q2{'rvprop'}='content';
}
# Get the list of pages to check
do {
$res=$api->query(%q1);
if($res->{'code'} ne 'success'){
$self->warn("Failed to retrieve transclusion list for $template: ".$res->{'error'}."\n");
return 60;
}
if(exists($res->{'query-continue'})){
$q1{'geicontinue'}=$res->{'query-continue'}{'embeddedin'}{'geicontinue'};
} else {
delete $q1{'geicontinue'};
}
# Collect page IDs of articles for found talk pages
my @pageids=map { exists($_->{'subjectid'})?($_->{'subjectid'}):() } values %{$res->{'query'}{'pages'}};
# Look up info for found pages, in blocks of 500 because that's the
# max pageids per query allowed.
while(@pageids){
$q2{'pageids'}=join('|', splice(@pageids, 0, 500));
$res=$api->query(%q2);
foreach my $page (values %{$res->{'query'}{'pages'}}){
my $pageid=$page->{'pageid'};
my $title=$page->{'title'};
my $checked=$api->fetch($pageid);
next if(defined($checked) && $$checked>=$seq);
my $ns=$page->{'ns'};
my $auto=0;
my $talk;
if(($ns&1)==1){
# Talk page, ignore it.
$api->store($pageid, \$seq);
next;
}
if($ns==0){
if(exists($page->{'redirect'})){
$ns='redirect';
} elsif(grep { $_->{'title'} eq 'Category:All disambiguation pages' } @{$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:/;
}
$self->warn("Assessing $title\n");
# Ok, check the page
my $tok=$api->edittoken($talk, 1);
if($tok->{'code'} eq 'shutoff'){
$self->warn("Task disabled: ".$tok->{'content'}."\n");
return 300;
}
if($tok->{'code'} ne 'success'){
$self->warn("Failed to get edit token for $talk: ".$tok->{'error'}."\n");
next;
}
next if exists($tok->{'missing'});
# Get page text
my $intxt=$tok->{'revisions'}[0]{'*'};
my ($outtxt,$nowiki)=$self->strip_nowiki($intxt);
my $any=0;
my @found=();
$outtxt=$self->process_templates($outtxt, sub {
my $name=shift;
my @params=@{shift()};
shift; # $wikitext
shift; # $data
my $oname=shift;
return undef unless exists($templates{"Template:$name"});
my %cfg=%{$templates{"Template:$name"}};
return undef unless exists($cfg{'ns'}{$ns});
my ($class,$classre,$imp,$impre)=@{$cfg{'ns'}{$ns}};
return undef if(defined($cfg{'verify'}) && !$cfg{'verify'}($page,$tok));
my $impname=exists($cfg{'importance'})?$cfg{'importance'}:'importance';
my $orig_param=join('|', @params);
# Auto-assess
if(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'})){
my $a=$cfg{'stubauto'};
if($auto){
push @params, "$a=yes" if(!grep(s/^(\s*\Q$a\E\s*=(?:\s*(?=\S))?).*?(\s*)$/$1yes$2/s, @params));
} else {
@params = grep(!/^\s*\Q$a\E\s*=/, @params);
}
}
}
if(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));
}
# Do extra parameters, if necessary
$cfg{'params'}($page,$tok,$name,\@params) if defined($cfg{'params'});
my $new_param=join('|', @params);
if($orig_param ne $new_param){
$any=1;
my @s=();
push @s, "class=$class" if defined($classre);
push @s, "$impname=$imp" if 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=" unless 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|".join("|",@params)."}}";
});
$outtxt=$self->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)." per $req";
$self->warn("$summary in $talk\n");
my $r=$api->edit($tok, $outtxt, $summary, 1, 1);
if($r->{'code'} ne 'success'){
$self->warn("Write failed on $talk: ".$r->{'error'}."\n");
next;
}
} else {
$self->warn("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
# go.
return 0 if time()>=$endtime;
}
}
} while(exists($q1{'geicontinue'}));
}
# No more pages to check, try again in 10 minutes or so in case of errors.
return 600;
}
1;