User:AnomieBOT/source/tasks/WikiProjectWorker.pm

This is an old revision of this page, as edited by AnomieBOT (talk | contribs) at 11:43, 16 April 2009 (Updating published sources: WikiProjectWorker: * WikiProject NATO run seems to be complete AnomieBOT::API * Slightly better error handling when MediaWiki:Missing-article occurs when etching tokens.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
package tasks::WikiProjectWorker;

=pod

=begin metadata

Task:     WikiProjectWorker
BRFA:     Wikipedia:Bots/Requests for approval/AnomieBOT 28
Status:   Approved 2009-04-08
Rate:     Max 6 edits/minute
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 Digest::SHA qw/sha256_base64/;
use AnomieBOT::Task;
use vars qw/@ISA/;
@ISA=qw/AnomieBOT::Task/;

### Request link, for edit summary.
my $req="[[User:AnomieBOT/req/WikiProject NATO 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=3;

### How to find the pages?
my @iterators=(
    {
        list        => 'categorymembers',
        cmtitle     => [
            'Category:NATO',
            'Category:Military facilities of NATO',
            'Category:NATO installations by country',
            'Category:NATO installations in Belgium',
            'Category:NATO installations in Canada',
            'Category:NATO installations in Germany',
            'Category:NATO installations in Italy',
            'Category:NATO installations in Turkey',
            'Category:Enlargement of NATO',
            'Category:NATO Secretaries General',
            'Category:Military operations involving NATO',
            'Category:NATO-led peacekeeping in the former Yugoslavia',
            'Category:NATO operations in Afghanistan',
            'Category:NATO military exercises',
            'Category:Military units and formations of NATO',
            'Category:NATO Standardisation Agreements',
            'Category:NATO agencies',
            'Category:NATO reporting names',
            'Category:Military ranks of NATO',
            'Category:NATO Supreme Allied Commanders',
            'Category:NATO summits',
            'Category:United States ambassadors to NATO',
        ],
        cmnamespace => '0|1',
        cmlimit     => 'max',
    },
);

### Filter function: manipulate the found data as necessary, returning the talk
### page to tag (or undef to skip).
sub filter {
    my $t=$_[0]->{'title'};
    $t="Talk:$t" if $_[0]->{'ns'} == 0;
    return $t;
}

# Banner configurations.
my %banner_cfgs=(
    'WikiProject NATO' => {
        meta => 0,
        stubauto => 'auto',
        canonicalize => 'WikiProject NATO',
    },
);

sub new {
    my $class=shift;
    my $self=$class->SUPER::new();
    $self->{'config loaded'}=0;
    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::Util 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;
        }
        $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;

    foreach my $itercfg (@iterators) {
        my $iter=$api->iterator(%$itercfg);
        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->warn("Skipping ".$page->{'title'}.", filter returned undef\n");
                $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->warn("Cannot edit $title: namespace ".$tok->{'ns'}." is non-talk\n");
                $api->store->{$pageid}=$seq;
                next;
            }
            if(exists($tok->{'redirect'})){
                $api->warn("$title is a redirect, skipping.\n");
                $api->store->{$pageid}=$seq;
                next;
            }

            $api->warn("Checking $title...\n");

            my $intxt=exists($tok->{'revisions'}[0]{'*'})?$tok->{'revisions'}[0]{'*'}:'';
            my ($outtxt,$nowiki)=$api->strip_nowiki($intxt);

            ### PROCESSING ###

            my @params=();

            my $assess = undef;;
            my ($class) = $api->WPBmax($intxt);
            if($class ne 'FA' && $class ne 'GA'){
                ($class) = $api->WPBmin($intxt);
            }
            if($class ne 'FA' && $class ne 'GA'){
                $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;
                    }
                }
                push @params, 'auto=yes' if ($assess // '') eq 'stub';
            } else {
                push @params, "class=$class";
            }

            $outtxt=$api->WPBadd($outtxt, $assess, sub {
                return $_[4];
            }, 'WikiProject NATO', @params);
            if(ref($outtxt) eq 'HASH'){
                $api->warn("Processing $title failed: ".$outtxt->{'error'}."\n");
                next;
            }

            $outtxt=$api->replace_nowiki($outtxt, $nowiki);

            # Need to edit?
            if($outtxt ne $intxt){
                $outtxt=$api->WPBfixshell($outtxt);
                if(ref($outtxt) eq 'HASH'){
                    $api->warn("Processing $title failed: ".$outtxt->{'error'}."\n");
                    next;
                }

                my $cat=$iter->iterval;
                my $summary="Assessing for WikiProject NATO based on membership in [[$cat]] per $req $errto";
                $api->warn("$summary in $title\n");
                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->warn("Nothing to do in $title\n");
            }

            # 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;
        }
    }

    # No more pages to check, try again in 10 minutes or so in case of errors.
    return 600;
}