![]() | Approved 2009-04-08 Wikipedia:Bots/Requests for approval/AnomieBOT 28 |
package tasks::WikiProjectWorker;
=pod
=begin metadata
Bot: AnomieBOT
Task: WikiProjectWorker
BRFA: Wikipedia:Bots/Requests for approval/AnomieBOT 28
Status: Approved 2009-04-08
Created: 2009-03-27
OnDemand: true
Perform various tasks at the request of the affected WikiProjects:
* Add or remove banners on a specific set of pages (e.g. pages in a category, pages transcluding a template).
* Adjust banner parameters, particularly assessments and task forces.
* Fix banner shells on pages edited for the above reasons.
=end metadata
=cut
use utf8;
use strict;
use Data::Dumper;
use AnomieBOT::Task;
use vars qw/@ISA/;
@ISA=qw/AnomieBOT::Task/;
### Request link, for edit summary.
my $req="[[User:AnomieBOT/req/WikiProject Women writers 3|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=24;
### How to find the pages?
my @cats=(
'Category:16th-century women scientists',
'Category:17th-century women scientists',
'Category:18th-century women scientists',
'Category:19th-century women scientists',
'Category:American female geologists',
'Category:American women scientists',
'Category:American women social scientists',
'Category:Australian female geologists',
'Category:Australian women scientists',
'Category:Austrian women scientists',
'Category:Belgian women scientists',
'Category:Brazilian female geologists',
'Category:Brazilian women scientists',
'Category:British female geologists',
'Category:British women scientists',
'Category:Bulgarian female geologists',
'Category:Burmese female geologists',
'Category:Canadian female geologists',
'Category:Canadian women scientists',
'Category:Chinese female geologists',
'Category:Chinese women scientists',
'Category:Danish female geologists',
'Category:Danish women scientists',
'Category:Dutch women scientists',
'Category:Egyptian female geologists',
'Category:English female geologists',
'Category:Ethiopian women scientists',
'Category:Female Fellows of the Royal Society',
'Category:Female geologists',
'Category:Filipino women scientists',
'Category:French female geologists',
'Category:French women scientists',
'Category:German female geologists',
'Category:German women scientists',
'Category:Harvard Computers',
'Category:Imperial Russian women scientists',
'Category:Indian female geologists',
'Category:Indian women scientists',
'Category:Iranian women scientists',
'Category:Iraqi women scientists',
'Category:Irish female geologists',
'Category:Irish women scientists',
'Category:Israeli female geologists',
'Category:Israeli women scientists',
'Category:Italian female geologists',
'Category:Italian women scientists',
'Category:Japanese female geologists',
'Category:Kazakhstani women scientists',
'Category:Lebanese women scientists',
'Category:Lists of women scientists',
'Category:L\'Oréal-UNESCO Awards for Women in Science laureates',
'Category:Malaysian women scientists',
'Category:Mexican women scientists',
'Category:New Zealand women scientists',
'Category:Nigerian female geologists',
'Category:Norwegian women scientists',
'Category:Peruvian women scientists',
'Category:Polish female geologists',
'Category:Polish women scientists',
'Category:Portuguese women scientists',
'Category:Recipients of the Garvan–Olin Medal',
'Category:Romanian female geologists',
'Category:Romanian women scientists',
'Category:Russian female geologists',
'Category:Russian women scientists',
'Category:Scottish female geologists',
'Category:Senegalese women scientists',
'Category:South African female geologists',
'Category:Swedish female geologists',
'Category:Swedish women scientists',
'Category:Tunisian women scientists',
'Category:Women agronomists',
'Category:Women anatomists',
'Category:Women anthropologists',
'Category:Women archaeologists',
'Category:Women astronomers',
'Category:Women atmospheric scientists',
'Category:Women biochemists',
'Category:Women bioengineers',
'Category:Women biologists',
'Category:Women biophysicists',
'Category:Women biotechnologists',
'Category:Women botanists',
'Category:Women bryologists',
'Category:Women chemists',
'Category:Women cognitive scientists',
'Category:Women computer scientists',
'Category:Women cyberneticists',
'Category:Women earth scientists',
'Category:Women ecologists',
'Category:Women economists',
'Category:Women entomologists',
'Category:Women ethnologists',
'Category:Women ethologists',
'Category:Women evolutionary biologists',
'Category:Women food scientists',
'Category:Women forensic scientists',
'Category:Women geneticists',
'Category:Women geographers',
'Category:Women horticulturists and gardeners',
'Category:Women linguists',
'Category:Women mammalogists',
'Category:Women marine biologists',
'Category:Women materials scientists and engineers',
'Category:Women mathematicians',
'Category:Women medical researchers',
'Category:Women meteorologists',
'Category:Women microbiologists',
'Category:Women molecular biologists',
'Category:Women mycologists',
'Category:Women naturalists',
'Category:Women neuroscientists',
'Category:Women oceanographers',
'Category:Women paleontologists',
'Category:Women parasitologists',
'Category:Women pathologists',
'Category:Women pharmacologists',
'Category:Women phycologists',
'Category:Women physicists',
'Category:Women physiologists',
'Category:Women phytopathologists',
'Category:Women planktologists',
'Category:Women political scientists',
'Category:Women primatologists',
'Category:Women psychologists',
'Category:Women rocket scientists',
'Category:Women scientists',
'Category:Women sexologists',
'Category:Women social scientists',
'Category:Women sociologists',
'Category:Women systems scientists',
'Category:Women taxonomists',
'Category:Women virologists',
'Category:Women zoologists',
);
my @iterators=(
{
generator => 'categorymembers',
gcmtitle => [@cats],
gcmlimit => 100,
},
);
### Filter function: manipulate the found data as necessary, returning the talk
### page to tag (or undef to skip).
sub filter {
$_[0]->{'title'}='Talk:'.$_[0]->{'title'} if $_[0]->{'ns'}==0;
$_[0]->{'title'}=~s/^([^:]*):/$1 talk:/ if($_[0]->{'ns'}!=0 && ($_[0]->{'ns'}&1)==0);
return $_[0]->{'title'};
}
### How to copy other projects' assessments
sub copy_class {
return ($_[0]->WPBmax($_[2]))[0];
#return '';
}
sub copy_importance {
#return ($_[0]->WPBmax($_[2]))[1];
return '';
}
# Banner configurations.
my $main_banner='WikiProject Women scientists';
my @preprocess_templates=();
my %banner_cfgs=(
'WikiProject Women scientists' => {
meta => 1,
canonicalize => 'WikiProject Women scientists',
},
);
my @bio_wg = (
's&a-work-group',
);
my @main_banners=(
$main_banner,
);
sub new {
my $class=shift;
my $self=$class->SUPER::new();
$self->{'config loaded'}=0;
$self->{'iter'}=undef;
$self->{'iterators'}=[@iterators];
bless $self, $class;
return $self;
}
=pod
=for info
Approved 2009-04-08<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 28]]
=cut
sub approved {
return 1;
}
sub run {
my ($self, $api)=@_;
my $res;
$api->task('WikiProjectWorker', 0, 10, qw/d::WikiProjectTagging/);
my $errto = 'Errors? [[User:'.$api->user.'/shutoff/WikiProjectWorker]]';
# Load configs, if necessary
if(!$self->{'config loaded'}){
my %cfg=();
while(my ($banner,$cfg)=each %banner_cfgs){
$cfg=$api->WPBMetaConfig($cfg->{'meta'}, %$cfg) if exists($cfg->{'meta'});
$cfg{$banner}=$cfg;
$banner_cfgs{$banner}{'stubauto'}=$cfg->{'stubauto'};
}
$api->WPBconfig(%cfg);
$self->{'config loaded'}=1;
}
if(($api->store->{'configured'} // 0) < $seq){
### Initialize configuration here
}
# Spend a max of 5 minutes on this task before restarting
my $endtime=time()+300;
while(1){
my $iter=$self->{'iter'};
if(!defined($iter)){
my $i=shift @{$self->{'iterators'}};
last unless $i;
$iter=$api->iterator(%$i);
$self->{'iter'}=$iter;
}
while(my $page=$iter->next()){
if(!$page->{'_ok_'}){
$api->warn("Could not retrieve page from iterator: ".$page->{'error'}."\n");
return 60;
}
my $pageid=$page->{'pageid'};
next if ($api->store->{$pageid} // 0) >= $seq;
my $title=filter($page);
if(!defined($title)){
$api->log("Skipping ".$page->{'title'}.", filter returned undef");
$api->store->{$pageid}=$seq;
next;
}
my $tok=$api->edittoken($title, EditRedir => 1);
if($tok->{'code'} eq 'shutoff'){
$api->warn("Task disabled: ".$tok->{'content'}."\n");
return 300;
}
if($tok->{'code'} ne 'success'){
$api->warn("Failed to get edit token for $title: ".$tok->{'error'}."\n");
next;
}
if(($tok->{'ns'}&1)==0){
$api->log("Cannot edit $title: namespace ".$tok->{'ns'}." is non-talk");
$api->store->{$pageid}=$seq;
next;
}
if(exists($tok->{'redirect'})){
$api->log("$title is a redirect, skipping.");
$api->store->{$pageid}=$seq;
next;
}
$api->log("Checking $title...");
my $intxt=$tok->{'revisions'}[0]{'*'} // '';
my ($outtxt,$nowiki)=$api->strip_nowiki($intxt);
my @params=();
my $assess = undef;
my $class = copy_class($api, $title, $intxt);
if(ref($class) eq 'HASH'){
$api->warn("Processing $title failed: ".$class->{'error'}."\n");
next;
}
$class=~s/^\s+|\s+$//g;
$assess=$api->WPBassess($title);
if(ref($assess) eq 'HASH'){
if($assess->{'code'} eq 'pagemissing'){
# No subject page, doesn't matter
$assess=undef;
} else {
$api->warn("Processing $title failed: ".$assess->{'error'}."\n");
next;
}
}
my $copy_importance = 1; # Always copy?
if($class eq '' || lc($class) eq lc($assess)){
$class='';
} else {
$assess=undef;
push @params, "class=$class";
$copy_importance = 1;
}
if($copy_importance){
my $imp = copy_importance($api, $title, $intxt);
if(ref($imp) eq 'HASH'){
$api->warn("Processing $title failed: ".$imp->{'error'}."\n");
next;
}
push @params, "importance=$imp"; # if ($imp//'') ne '';
}
my $need_main_banner = ($api->WPBcheck($outtxt, $main_banner) == 0);
if(0){
# grr, already redirected so the above always returns true
$need_main_banner=1;
$api->WPBcheck($outtxt, sub {
my $banner=shift;
my $name=shift;
$need_main_banner=0 if(grep $name eq $_, @main_banners);
return undef;
}, $main_banner);
}
### PROCESSING ###
my @merge=();
my @summary=();
my $no_summary = 0;
if(@preprocess_templates){
(undef,$outtxt)=$api->WPBcheck($outtxt, sub {
my $banner=shift;
my $name=shift;
my $oname=shift;
my $params=shift;
my $wikitext=shift;
return undef if(grep $name eq $_, @main_banners);
push @merge, $oname;
return '' unless $need_main_banner;
$need_main_banner=0;
return "{{$main_banner}}";
}, @preprocess_templates);
if(ref($outtxt) eq 'HASH'){
$api->warn("Preprocessing $title failed: ".$outtxt->{'error'}."\n");
next;
}
if(@merge){
$merge[-1]='and '.$merge[-1] if @merge>1;
push @summary, "merging ".join((@merge>2)?', ':' ', @merge)." into $main_banner" if @merge;
$no_summary = 1 if @merge;
} else {
# $api->warn("$title contains ".$iter->iterval.", but not in section 0\n");
next;
}
}
if ( @bio_wg ) {
my %added_wg=();
(undef,$outtxt)=$api->WPBcheck($outtxt, sub {
my $banner=shift;
my $name=shift;
my $oname=shift;
my $params=shift;
my $wikitext=shift;
my %have = ();
foreach ($api->process_paramlist(@$params)) {
$have{$_->{'name'}} = 1;
}
my $any = 0;
foreach my $p (@bio_wg){
next if exists($have{$p});
push @$params, "$p=yes";
$added_wg{"$p=yes"}=1;
$any = 1;
}
return undef unless $any;
my $out="{{$oname";
$out.="|".join("|", @$params) if @$params;
$out.="}}";
return $out;
}, 'WikiProject Biography');
if(ref($outtxt) eq 'HASH'){
$api->warn("WPBIO workgroups in $title failed: ".$outtxt->{'error'}."\n");
next;
}
if(%added_wg){
my @added_wg = keys %added_wg;
$added_wg[-1]='and '.$added_wg[-1] if @added_wg>1;
push @summary, "added " . join((@added_wg>2)?', ':' ', @added_wg) . " to {{WikiProject Biography}}";
}
}
my $tag;
my $pg=$title;
$pg=~s/^Talk://;
if($page->{'title'} ne $pg){
$page=$api->query(
titles => $pg,
prop => 'info',
redirects => 1,
);
if($page->{'code'} ne 'success'){
$api->warn("Could not load $pg info: ".$page->{'error'}."\n");
return 60;
}
$page=(values %{$page->{'query'}{'pages'}})[0];
}
$outtxt=$api->WPBadd($outtxt, $assess, sub {
my $banner=shift; # banner
my $name=shift; # name
my $oname=shift;
my $params=shift;
my $wikitext=shift;
my $new=shift;
#return '' if $new;
return undef unless(grep $banner eq $_, @main_banners);
my %cfg=%{$banner_cfgs{$banner}};
my $any=0;
foreach my $p (@params){
next unless $p=~/^(.+?)=(.*)$/;
my ($k,$v)=($1,$2);
my $re=qr/\S.*?/;
unless(grep(/^\s*\Q$k\E\s*=\s*$re\s*$/, @$params)){
next if(grep(/^\s*\Q$k\E\s*=\s*(?i:\Q$v\E)\s*$/s, @$params));
unshift @$params, "$k=$v" unless(grep(s/^(\s*\Q$k\E\s*=\s*?)(?:\S.*?)?(\s*)$/$1$v$2/s, @$params));
$any=1 unless $v eq '';
if($k eq 'class' && ($cfg{'meta'}//0) && ($cfg{'stubauto'}//'')){
my $aa=$cfg{'stubauto'};
$aa=~s/\s*=.*//;
push @$params, "$aa=inherit" unless(grep(s/^(\s*\Q$aa\E\s*=\s*?)(?:\S.*?)?(\s*)$/$1inherit$2/s, @$params));
}
}
}
if($new && $class && (grep /^class=/, @$params) && ($cfg{'meta'}//0) && ($cfg{'stubauto'}//'')){
my $aa=$cfg{'stubauto'};
$aa=~s/\s*=.*//;
push @$params, "$aa=inherit" unless(grep(s/^(\s*\Q$aa\E\s*=\s*?)(?:\S.*?)?(\s*)$/$1inherit$2/s, @$params));
$any=1;
}
return $wikitext unless $any;
if(defined($cfg{'canonicalize'})){
my $n=$cfg{'canonicalize'};
$oname=~s/_/ /g;
$oname=~s/^(\s*)\S(?:.*\S)?(\s*)$/$1$n$2/is;
}
my $out="{{$oname";
$out.="|".join("|", @$params) if @$params;
$out.="}}";
return $out;
}, $main_banner, @params);
if(ref($outtxt) eq 'HASH'){
$api->warn("Processing $title failed: ".$outtxt->{'error'}."\n");
next;
}
push @summary, "Tagging with {{$main_banner}}" unless $no_summary;
$outtxt=$api->replace_nowiki($outtxt, $nowiki);
# Need to edit?
if($outtxt ne $intxt){
if(!@summary){
$api->warn("$title changed, but nothing in \@summary\n");
next;
}
my $cat=$iter->iterval;
$summary[-1]='and '.$summary[-1] if @summary>1;
my $summary = ucfirst(join((@summary>2)?', ':' ', @summary))." per $req";
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;
$summary.=". $errto";
$api->log("$summary in $title");
my $r=$api->edit($tok, $outtxt, $summary, 1, 1);
if($r->{'code'} ne 'success'){
$api->warn("Write failed on $title: ".$r->{'error'}."\n");
next;
}
} else {
$api->log("Nothing to do in $title");
}
# Remember that we processed this page already
$api->store->{$pageid}=$seq;
# If we've been at it long enough, let another task have a go.
return 0 if time()>=$endtime;
}
$self->{'iter'}=undef;
}
# No more pages to check, try again in 10 minutes or so in case of errors.
$self->{'iter'}=undef;
$self->{'iterators'}=[@iterators];
$api->log("WikiProjectWorker may be DONE!");
return 600;
}