Jump to content

User:AnomieBOT/source/tasks/TemplateReplacer11.pm

From Wikipedia, the free encyclopedia
package tasks::TemplateReplacer11;

=pod

=for warning
Due to breaking changes in AnomieBOT::API, this task will probably not run
anymore. If you really must run it, try getting a version from before
2009-03-23.

=begin metadata

Bot:     AnomieBOT
Task:    TemplateReplacer11
BRFA:    Wikipedia:Bots/Requests for approval/AnomieBOT 22
Status:  Completed 2009-01-11
Created: 2009-01-10

For all pages in [[:Category:Film articles using old needs infobox parameter]],
replace the <code>needs infobox</code> parameter with
<code>needs-infobox</code>, remove the obsolete <code>importance</code>
parameter, and canonicalize redirects for {{tl|Film}}.

=end metadata

=cut

use utf8;
use strict;

use AnomieBOT::Task;
use vars qw/@ISA/;
@ISA=qw/AnomieBOT::Task/;

sub new {
    my $class=shift;
    my $self=$class->SUPER::new();
    bless $self, $class;
    return $self;
}

=pod

=for info
Speedily approved 2009-01-10, completed 2009-01-11<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 22]]

=cut

sub approved {
    return -1;
}

sub run {
    my ($self, $api)=@_;
    my $res;

    $api->task('TemplateReplacer11');
    $api->read_throttle(0);
    $api->edit_throttle(10);

    # List of templates to replace in this task
    my @templates=('Film');
    my $req="[[WP:BOTREQ#.7B.7BFilm.7D.7D_cleanup|request]]";

    # Spend a max of 5 minutes on this task before restarting
    my $endtime=time()+300;

    # Get a list of templates redirecting to our targets
    my %templates=();
    foreach my $template (@templates){
        $templates{"Template:$template"}=1;
        $res=$api->query([],
            list          => 'backlinks',
            bltitle       => "Template:$template",
            blfilterredir => 'redirects',
            bllimit       => 'max',
        );
        $templates{$_->{'title'}}=1 foreach (@{$res->{'query'}{'backlinks'}});
    }

    # Get the list of pages to check
    my %q=(
        list    => 'categorymembers',
        cmtitle => "Category:Film articles using old needs infobox parameter",
        cmlimit => 'max',
    );
    do {
        $res=$api->query(%q);
        if($res->{'code'} ne 'success'){
            $self->warn("Failed to retrieve category listing for: ".$res->{'error'}."\n");
            return 60;
        }
        if(exists($res->{'query-continue'})){
            $q{'cmcontinue'}=$res->{'query-continue'}{'categorymembers'}{'cmcontinue'};
        } else {
            delete $q{'cmcontinue'};
        }

        # Process found pages
        foreach (@{$res->{'query'}{'categorymembers'}}){
            my $title=$_->{'title'};
            $self->warn("Checking for Template:Film in $title\n");

            # WTF?
            if(exists($_->{'missing'})){
                $self->warn("$title is missing? WTF?\n");
                next;
            }

            # Ok, check the page
            my $tok=$api->edittoken($title);
            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 $title: ".$tok->{'error'}."\n");
                next;
            }
            next if exists($tok->{'missing'});

            # Get page text
            my $intxt=$tok->{'revisions'}[0]{'*'};

            # Now, we actually perform the replacement
            my $summary="Removing obsolete parameters and canonicalizing redirects in {{Film}} per $req";
            my $minor=1;
            my $outtxt=$self->process_templates($intxt, sub {
                my $name=shift;
                my @params=@{shift()};
                shift; # $wikitext
                shift; # $data
                my $oname=shift;

                return undef unless exists($templates{"Template:$name"});
                @params=grep(!/^\s*importance\s*=/, @params);
                s/^(\s*needs) (infobox\s*=)/$1-$2/ foreach @params;

                my $n='Film';
                $oname=~s/^(\s*)\S(?:.*\S)?(\s*)$/$1$n$2/is;

                # Ok, return the new template code now.
                return "{{$oname|".join("|", @params)."}}";
            });

            # Need to edit?
            if($outtxt ne $intxt){
                $self->warn("$summary in $title\n");
                my $r=$api->edit($tok, $outtxt, $summary, $minor, $minor);
                if($r->{'code'} ne 'success'){
                    $self->warn("Write failed on $title: ".$r->{'error'}."\n");
                    next;
                }
            } else {
                $self->warn("Nothing to do in $title\n");
            }

            # If we've been at it long enough, let another task have a go.
            return 0 if time()>=$endtime;
        }
    } while(exists($q{'cmcontinue'}));

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

1;