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: IFDCloser: * Approved! TemplateReplacer6: * Approved! AutoAssessor2: * Bugfix.
Line 27:
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]]";
 
# 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 => Boolean, whether to add "auto=yes" when tagging a stub.
# canonicalize => If defined, any instance of the template will be renamed to
# this when edits are done to the page.
# importance => If specified, this is the name of the "importance" parameter
# instead of "importance".
# 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 ],
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 => 1,
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;
Line 55 ⟶ 99:
 
# List of templates to assess in this task
if(!$self->{'did_templates'}){
my @templates=('Physics');
while(my ($t,$c)=each %cfg_templates){
my $req='[[User talk:Anomie#Could you run AnomieBOT 8 again?]]';
$c->{'names'}=[ "Template:$t" ];
my $canonicalize=0;
$res=$self->fullquery($api, undef,
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){
# Get a list of templates redirecting to our targets
my %templates=();
foreach my $template (@templates){
$templates{"Template:$template"}=$template;
$res=$self->fullquery($api, undef,
list => 'backlinks',
bltitle => "Template:$template",
blfilterredir => 'redirects',
bllimit => 'max',
);
$templates{$_->{'title'}}=$template foreach (@{$res->{'query'}{'backlinks'}});
}
 
foreach my $template (@templates){
my %q1=(
generator => 'embeddedin',
Line 88 ⟶ 137:
cllimit => 'max',
);
if($need_page_contents){
$q2{'prop'}.='|revisions';
$q2{'rvprop'}='content';
}
 
# Get the list of pages to check
Line 110 ⟶ 163:
$q2{'pageids'}=join('|', splice(@pageids, 0, 500));
$res=$self->fullquery($api, undef, %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);
 
my ($imp,ns=$impre,$class,$classre,$auto)=('',undef,page->{'ns',undef,0)};
if(my $_->{'ns'}auto==2){ # User0;
my $class='NA'talk;
if(($classrens&1)=qr/na/i;=1){
$imp='NA';# Talk page, ignore it.
$imp=qr/na/i;
} elsif($_->{'ns'}==4){ # Wikipedia
$class='project';
$classre=qr/project/i;
$imp='NA';
$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:/;
}
 
Line 191 ⟶ 194:
 
# Ok, check the page
my $tok=$api->edittoken($titletalk, 1);
if($tok->{'code'} eq 'shutoff'){
$self->warn("Task disabled: ".$tok->{'content'}."\n");
Line 197 ⟶ 200:
}
if($tok->{'code'} ne 'success'){
$self->warn("Failed to get edit token for $titletalk: ".$tok->{'error'}."\n");
next;
}
Line 204 ⟶ 207:
# Get page text
my $intxt=$tok->{'revisions'}[0]{'*'};
my ($outtxt,$nowiki)=$self->strip_nowiki($intxt);
 
# Now, we actually perform the replacement
my @found=();
my $any=0;
my @found=();

$outtxt=$self->process_templates($intxtouttxt, sub {
my $name=shift;
my @params=@{shift()};
Line 216 ⟶ 219:
 
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($auto){
push @params, "auto=yes" unlessif($cfg{'stubauto'} && !grep(s/^(\s*auto\s*=(?:\s*(?=\S))?).*?(\s*)$/$1yes$2/s, @params));
} else {
@params = grep(!/^\s*auto\s*=/, @params);
}
}
if(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));
}
 
# Do extra parameters, if necessary
$cfg{'params'}($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 defined($name"}classre);
push @s, "$impname=$imp" if defined($impre);
push @found, "{{$name}} (".join(' ', @s).')';
}
if(defined($cfg{'canonicalize'})){
my $n=$cfg{'canonicalize'};
$oname=~s/_/ /g;
$oname=~s/^(\s*)\S(?:.*\S)?(\s*)$/$1$n$2/is;
Line 241 ⟶ 258:
return "{{$oname|$new_param}}";
});
$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)." as class=$class".(defined($impre)?" importance=$imp":"")." per $req";
$self->warn("$summary in $titletalk\n");
my $r=$api->edit($tok, $outtxt, $summary, 1, 1);
if($r->{'code'} ne 'success'){
$self->warn("Write failed on $titletalk: ".$r->{'error'}."\n");
next;
}
} else {
$self->warn("Nothing to do in $titletalk\n");
}