Jump to content

User:AnomieBOT/source/tasks/TemplateReplacer5.pm and User:AnomieBOT/source/tasks/TemplateReplacer6.pm: Difference between pages

From Wikipedia, the free encyclopedia
(Difference between pages)
Content deleted Content added
AnomieBOT (talk | contribs)
Updating published sources: TemplateReplacer5: * Speedily approved!
 
AnomieBOT (talk | contribs)
Updating published sources: General: * New task, TemplateReplacer6.
 
Line 1: Line 1:
{{ombox|type=notice|text= Approved 2008-11-11<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 10]]}}
{{ombox|type=content|text= Approval requested.<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 12]]}}
<source lang="perl">
<source lang="perl">
package tasks::TemplateReplacer5;
package tasks::TemplateReplacer6;


=pod
=pod
Line 7: Line 7:
=begin metadata
=begin metadata


Task: TemplateReplacer5
Task: TemplateReplacer6
BRFA: Wikipedia:Bots/Requests for approval/AnomieBOT 10
BRFA: Wikipedia:Bots/Requests for approval/AnomieBOT 12
Status: Approved 2008-11-11
Status: BRFA
Rate: Max 6 edits/minute
Rate: Max 6 edits/minute


Convert {{tl|Infobox Aircraft}} in article space to the new
Replace {{tl|Segaproject}} (and old redirects) with {{tlx|WikiProject
{{tl|Infobox Aircraft Begin}}{{tl|Infobox Aircraft Type}} system.
Video games|2=tf=Sega}}. Also preserve Sonic task force taging and
canonicalizes redirects to {{tl|WikiProject Video games}} on the pages it edits
for this reason, since it's editing them anyway.


=end metadata
=end metadata
Line 27: Line 25:
use vars qw/@ISA/;
use vars qw/@ISA/;
@ISA=qw/AnomieBOT::Task/;
@ISA=qw/AnomieBOT::Task/;

my @begin_params=(
'name',
'image',
'caption',
'long caption',
'logo',
);
my @type_params=(
'type',
'national origin',
'manufacturer',
'manufacturers',
'designer',
'first flight',
'introduction',
'introduced',
'retired',
'status',
'primary user',
'more users',
'produced',
'number built',
'program cost',
'unit cost',
'developed from',
'variants with their own articles',
);
my $req="[[Wikipedia:Bot requests#Template substitution|request]]";


sub new {
sub new {
Line 37: Line 64:
=pod
=pod


=for info
=for notice
Approved 2008-11-11<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 10]]
Approval requested.<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 12]]


=cut
=cut


sub approved {
sub approved {
return 1;
return 0;
}
}


Line 50: Line 77:
my $res;
my $res;


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


# List of templates to replace in this task
# List of templates to replace in this task
my @templates=('Segaproject');
my @templates=('Infobox Aircraft');
my $tf='Sega';
my $req="[[Wikipedia talk:WikiProject Video games/Sega#Bot request|request]]";


# Spend a max of 5 minutes on this task before restarting
# Spend a max of 5 minutes on this task before restarting
Line 74: Line 99:
$templates{$_->{'title'}}=1 foreach (@{$res->{'query'}{'backlinks'}});
$templates{$_->{'title'}}=1 foreach (@{$res->{'query'}{'backlinks'}});
}
}

# Before starting, get the list of alternate names for our target
my %WPVG=('Template:WikiProject Video games'=>1);
$res=$self->fullquery($api, undef,
list => 'backlinks',
bltitle => 'Template:WikiProject Video games',
blfilterredir => 'redirects',
bllimit => 'max',
);
$WPVG{$_->{'title'}}=1 foreach (@{$res->{'query'}{'backlinks'}});
delete $WPVG{$_} foreach (values %templates);

# Also, get redirects to WikiProjectBanners and WikiProjectBannerShell, for
# renumbering after the wikiproject banner is removed.
my %WPB=('Template:WikiProjectBanners'=>1,'Template:WikiProjectBannerShell'=>1);
$res=$self->fullquery($api, undef,
list => 'backlinks',
bltitle => 'Template:WikiProjectBanners',
blfilterredir => 'redirects',
bllimit => 'max',
);
$WPB{$_->{'title'}}=1 foreach (@{$res->{'query'}{'backlinks'}});
$res=$self->fullquery($api, undef,
list => 'backlinks',
bltitle => 'Template:WikiProjectBannerShell',
blfilterredir => 'redirects',
bllimit => 'max',
);
$WPB{$_->{'title'}}=1 foreach (@{$res->{'query'}{'backlinks'}});


foreach my $template (@templates){
foreach my $template (@templates){
# Get the list of pages to check
# Get the list of pages to check
my %q=(
my %q=(
list => 'embeddedin',
list => 'embeddedin',
eititle => "Template:$template",
eititle => "Template:$template",
eilimit => 'max',
einamespace => 0,
eilimit => 'max',
);
);
do {
do {
Line 148: Line 145:
# Get page text
# Get page text
my $intxt=$tok->{'revisions'}[0]{'*'};
my $intxt=$tok->{'revisions'}[0]{'*'};
my ($outtxt,$nowiki)=$self->strip_nowiki($intxt);

# First step is to determine if the replacement is already on
# the page.
my $has_WPVG=0;
my $has_SonicTF=0;
$self->process_templates($intxt, sub {
$has_WPVG=1 if exists($WPVG{'Template:'.ucfirst($_[0])});
return undef unless exists($templates{'Template:'.ucfirst($_[0])});
$has_SonicTF=grep(/^\s*tf[23]?\s*=\s*Sonic\s*$/, @{$_[1]});
return undef;
});


# Now, we actually perform the replacement
# Now, we actually perform the replacement
my $summary="Replacing {{$template}} with {{Infobox Aircraft Begin}}{{Infobox Aircraft Type}} per $req";
my $append='';
$outtxt=$self->process_templates($outtxt, sub {
my $summary="Replacing {{$template}} with {{WikiProject Video games}} per $req";
my $minor=1;
my $outtxt=$self->process_templates($intxt, sub {
my $name=shift;
my $name=shift;
my @params=@{shift()};
my @params=@{shift()};
Line 172: Line 157:


$name=~s/_/ /g;
$name=~s/_/ /g;
if(exists($templates{'Template:'.ucfirst($name)})){
return undef unless exists($templates{'Template:'.ucfirst($name)});
return '' if $has_WPVG;
my $k;
$oname=~s/\Q$name\E/WikiProject Video games/i;
($k=$oname)=~s/\Q$name\E/Infobox Aircraft Begin/i;
$name='WikiProject Video games';
my @begin=($k);
@params=grep(/^\s*(?:class|importance|small|nested)\s*=/, @params);
($k=$oname)=~s/\Q$name\E/Infobox Aircraft Type/i;
push @params, "tf=$tf";
my @type=($k);
foreach (@params){
$k=$_;
next unless $k=~s/=.*//s;
$k=~s/^\s+|\s+$//g;
next if $k eq '';
warn "param: $k\n";
push @begin, $_ if grep { $_ eq $k } @begin_params;
push @type, $_ if grep { $_ eq $k } @type_params;
}
}
return undef unless exists($WPVG{'Template:'.ucfirst($name)});
my $out="{|";
$out.="{{".join('|', @begin)."}}";
$out.="{{".join('|', @type)."}}";
$out.="\n|}";


# We have the replacement template now, either by finding
$out=~s/\n\s+}}/\n}}/g;
# the existing one or by replacing the target template if
# none was already present. Now, find if there is already a
# 'tf' for this task force.
my @tf=grep(/^\s*tf[23]?\s*=\s*$tf\s*$/, @params);
if(!@tf){
if(!grep(/^\s*tf\s*=/, @params)){
push @params, "tf=$tf";
} elsif(!grep(/^\s*tf2\s*=/, @params)){
push @params, "tf2=$tf";
} elsif(!grep(/^\s*tf3\s*=/, @params)){
push @params, "tf3=$tf";
} else {
$append="\n==Template:$template removal issue==\nPer $req, I am going through all pages transcluding {{tl|$template}} to remove it and add a <code>tf=$tf</code> parameter to the {{tl|WikiProject Video games}} template instead. Unfortunately, this page already uses all three of <code>tf</code>, <code>tf2</code>, and <code>tf3</code> in its {{tl|WikiProject Video games}}, so human intervention is required. ~~~~";
$summary="/* Template:$template removal issue */ Human intervention needed to replace {{$template}} with {{WikiProject Video games|tf=$tf}}";
$minor=0;
}
}
if($has_SonicTF){
my @tf=grep(/^\s*tf[23]?\s*=\s*Sonic\s*$/, @params);
if(!@tf){
if(!grep(/^\s*tf\s*=/, @params)){
push @params, "tf=Sonic";
} elsif(!grep(/^\s*tf2\s*=/, @params)){
push @params, "tf2=Sonic";
} elsif(!grep(/^\s*tf3\s*=/, @params)){
push @params, "tf3=Sonic";
} else {
$append="\n==Template:$template removal issue==\nPer $req, I am going through all pages transcluding {{tl|$template}} to remove it and add a <code>tf=$tf</code> parameter to the {{tl|WikiProject Video games}} template instead; I am also copying the <code>tf=Sonic</code> parameter, if it was present. Unfortunately, this page already uses <code>tf</code> and <code>tf2</code> in its {{tl|WikiProject Video games}} (keeping both Sega and Sonic task forces requires at least two free <code>tf</code> parameters), so human intervention is required. ~~~~";
$summary="/* Template:$template removal issue */ Human intervention needed to replace {{$template}} with {{WikiProject Video games|tf=$tf|tf2=Sonic}}";
$minor=0;
}
}
}

# Canonicalize name
$oname=~s/\Q$name\E/WikiProject Video games/i;


# Ok, return the new template code now.
# Ok, return the new template code now.
return "{{$oname|".join("|", @params)."}}";
return $out;
});
});
$outtxt.=$append;
$outtxt=$self->replace_nowiki($outtxt, $nowiki);


# Need to edit?
# Need to edit?
if($outtxt ne $intxt){
if($outtxt ne $intxt){
# Post-process to fix
# WikiProjectBanners/WikiProjectBannerShell numbering.
$outtxt=$self->process_templates($outtxt, sub {
my $name=shift;
my @params=@{shift()};
shift; # $wikitext
shift; # $data
my $oname=shift;

$name=~s/_/ /g;
return undef unless exists($WPB{'Template:'.ucfirst($name)});
my $idx=0;
my $out='{{'.$oname;
foreach (@params){
if(/^\s*\d+\s*=\s*$/s){
# skip it
} elsif(/^\s*\d+\s*=(.*)$/s){
++$idx;
$out.="|$idx=$1";
} else {
$out.='|'.$_;
}
}
$out.='}}';
return (@params<=1)?undef:$out;
});

$self->warn("$summary in $title\n");
$self->warn("$summary in $title\n");
my $r=$api->edit($tok, $outtxt, $summary, $minor, $minor);
my $r=$api->edit($tok, $outtxt, $summary, 1, 1);
if($r->{'code'} ne 'success'){
if($r->{'code'} ne 'success'){
$self->warn("Write failed on $title: ".$r->{'error'}."\n");
$self->warn("Write failed on $title: ".$r->{'error'}."\n");

Revision as of 00:50, 18 November 2008

package tasks::TemplateReplacer6;

=pod

=begin metadata

Task:   TemplateReplacer6
BRFA:   Wikipedia:Bots/Requests for approval/AnomieBOT 12
Status: BRFA
Rate:   Max 6 edits/minute

Convert {{tl|Infobox Aircraft}} in article space to the new
{{tl|Infobox Aircraft Begin}}{{tl|Infobox Aircraft Type}} system.

=end metadata

=cut

use utf8;
use strict;

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

my @begin_params=(
    'name',
    'image',
    'caption',
    'long caption',
    'logo',
);
my @type_params=(
    'type',
    'national origin',
    'manufacturer',
    'manufacturers',
    'designer',
    'first flight',
    'introduction',
    'introduced',
    'retired',
    'status',
    'primary user',
    'more users',
    'produced',
    'number built',
    'program cost',
    'unit cost',
    'developed from',
    'variants with their own articles',
);
my $req="[[Wikipedia:Bot requests#Template substitution|request]]";

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

=pod

=for notice
Approval requested.<br />[[Wikipedia:Bots/Requests for approval/AnomieBOT 12]]

=cut

sub approved {
    return 0;
}

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

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

    # List of templates to replace in this task
    my @templates=('Infobox Aircraft');

    # 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=$self->fullquery($api, undef,
            list          => 'backlinks',
            bltitle       => "Template:$template",
            blfilterredir => 'redirects',
            bllimit       => 'max',
        );
        $templates{$_->{'title'}}=1 foreach (@{$res->{'query'}{'backlinks'}});
    }

    foreach my $template (@templates){
        # Get the list of pages to check
        my %q=(
            list        => 'embeddedin',
            eititle     => "Template:$template",
            einamespace => 0,
            eilimit     => 'max',
        );
        do {
            $res=$api->query(%q);
            if($res->{'code'} ne 'success'){
                $self->warn("Failed to retrieve transclusion list for $template: ".$res->{'error'}."\n");
                return 60;
            }
            if(exists($res->{'query-continue'})){
                $q{'eicontinue'}=$res->{'query-continue'}{'embeddedin'}{'eicontinue'};
            } else {
                delete $q{'eicontinue'};
            }

            # Process found pages
            foreach (@{$res->{'query'}{'embeddedin'}}){
                my $title=$_->{'title'};
                $self->warn("Checking for $template 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]{'*'};
                my ($outtxt,$nowiki)=$self->strip_nowiki($intxt);

                # Now, we actually perform the replacement
                my $summary="Replacing {{$template}} with {{Infobox Aircraft Begin}}{{Infobox Aircraft Type}} per $req";
                $outtxt=$self->process_templates($outtxt, sub {
                    my $name=shift;
                    my @params=@{shift()};
                    shift; # $wikitext
                    shift; # $data
                    my $oname=shift;

                    $name=~s/_/ /g;
                    return undef unless exists($templates{'Template:'.ucfirst($name)});
                    my $k;
                    ($k=$oname)=~s/\Q$name\E/Infobox Aircraft Begin/i;
                    my @begin=($k);
                    ($k=$oname)=~s/\Q$name\E/Infobox Aircraft Type/i;
                    my @type=($k);
                    foreach (@params){
                        $k=$_;
                        next unless $k=~s/=.*//s;
                        $k=~s/^\s+|\s+$//g;
                        next if $k eq '';
                        warn "param: $k\n";
                        push @begin, $_ if grep { $_ eq $k } @begin_params;
                        push @type, $_ if grep { $_ eq $k } @type_params;
                    }
                    my $out="{|";
                    $out.="{{".join('|', @begin)."}}";
                    $out.="{{".join('|', @type)."}}";
                    $out.="\n|}";

                    $out=~s/\n\s+}}/\n}}/g;

                    # Ok, return the new template code now.
                    return $out;
                });
                $outtxt=$self->replace_nowiki($outtxt, $nowiki);

                # Need to edit?
                if($outtxt ne $intxt){
                    $self->warn("$summary in $title\n");
                    my $r=$api->edit($tok, $outtxt, $summary, 1, 1);
                    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{'eicontinue'}));
    }

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

1;