User:AnomieBOT/source/tasks/TemplateReplacer14.pm

{{ombox|type=content|style=border:1px solid #b22222|image=40px|text= 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 2018-08-12.}}

{{ombox|type=notice|text= Approved 2009-03-03, completed 2009-03-26
Wikipedia:Bots/Requests for approval/AnomieBOT 25}}

package tasks::TemplateReplacer14;

=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

2018-08-12.

=begin metadata

Bot: AnomieBOT

Task: TemplateReplacer14

BRFA: Wikipedia:Bots/Requests for approval/AnomieBOT 25

Status: Completed 2009-03-26

Created: 2009-03-02

Replace the obsolete {{tlx|Infobox Television}} imdb_id and

ttv_com_id parameters with {{tl|imdb title}}, and {{tl|tv.com}} in

the External links section, respectively.

=end metadata

=cut

use utf8;

use strict;

use AnomieBOT::Task;

use Data::Dumper;

use vars qw/@ISA/;

@ISA=qw/AnomieBOT::Task/;

my $tv_extlink_templates_re=undef;

my $no_edit_just_to_remove_parameters=0;

sub new {

my $class=shift;

my $self=$class->SUPER::new();

bless $self, $class;

return $self;

}

=pod

=for info

Approved 2009-03-03, completed 2009-03-26
Wikipedia:Bots/Requests for approval/AnomieBOT 25

=cut

sub approved {

return -1;

}

sub run {

my ($self, $api)=@_;

my $res;

$api->task('TemplateReplacer14', 0, 10, qw/d::Sections/);

return 60 if(!defined($api->load_interlanguage_map($api)));

if(!defined($tv_extlink_templates_re)){

my @links=();

my $res=$api->query([],

list => 'categorymembers',

cmtitle => 'Category:Television external link templates',

cmnamespace => 10,

cmlimit => 'max',

);

if($res->{'code'} ne 'success'){

$api->warn("Failed to retrieve tv external link template list: ".$res->{'error'}."\n");

return 60;

}

#unshift @{$res->{'query'}{'categorymembers'}}, { title=>'Template:Official' };

foreach (@{$res->{'query'}{'categorymembers'}}){

my $t=$_->{'title'};

next if $t=~m{/(?:doc|sandbox)$}i;

$t="\Q".substr($t,9);

$t=~s/^(.)/(?i:$1)/;

$t=~s/ /[ _]/g;

push @links, $t;

my $res2=$api->query([],

list => 'backlinks',

bltitle => $_->{'title'},

blfilterredir => 'redirects',

bllimit => 'max',

);

if($res2->{'code'} ne 'success'){

$api->warn("Failed to retrieve redirects for ".$_->{'title'}.": ".$res2->{'error'}."\n");

return 60;

}

foreach (@{$res2->{'query'}{'backlinks'}}){

$_="\Q".substr($_->{'title'},9);

s/^Template:(.)/(?i:$1)/;

s/ /[ _]/g;

push @links, $_;

}

}

$tv_extlink_templates_re=join('|', @links);

$tv_extlink_templates_re=qr/{{\s*(?:$tv_extlink_templates_re)\s*(?:\||}})/o;

}

my $req="request";

# Spend a max of 5 minutes on this task before restarting

my $endtime=time()+300;

$self->_output_log($api);

# Get a list of templates redirecting to our target

my %templates=();

$templates{"Template:Infobox Television"}=1;

$res=$api->query([],

list => 'backlinks',

bltitle => "Template:Infobox Television",

blfilterredir => 'redirects',

bllimit => 'max',

);

if($res->{'code'} ne 'success'){

$api->warn("Failed to retrieve redirects for Template:Infobox Television: ".$res->{'error'}."\n");

return 60;

}

$templates{$_->{'title'}}=1 foreach (@{$res->{'query'}{'backlinks'}});

# Matching external links; "%X" is the text of the template parameter

my @to_process=qw/imdb_id tv_com_id/;

my %ext_links=(

'imdb_id' => [

'http://www.imdb.com/title/tt%X',

'http://www.imdb.com/title/tt%X/',

'http://imdb.com/title/tt%X',

'http://imdb.com/title/tt%X/',

],

'tv_com_id' => [

'http://www.tv.com/show/%X/summary.html',

'http://tv.com/show/%X/summary.html',

],

);

# External link generating templates; parameter 1 is the id, and optional

# parameter 2 is the infobox's "show_name" parameter.

my %ext_templates=(

'imdb_id' => 'imdb title',

'tv_com_id' => 'tv.com',

);

# Get the list of pages to check

foreach my $cat ('Category:Television articles with an IMDb link in the infobox', 'Category:Television articles with a TV.com link in the infobox'){

my %q=(

list => 'categorymembers',

cmtitle => $cat,

cmnamespace => 0,

cmlimit => 'max',

);

do {

$res=$api->query(%q);

if($res->{'code'} ne 'success'){

$api->warn("Failed to retrieve category member list for $cat: ".$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 $pageid=$_->{'pageid'};

next if exists($api->store->{$pageid});

# Cleanup the log

my $log={};

$log=$api->store->{'log'} if exists($api->store->{'log'});

delete $log->{$_}{$pageid} foreach (keys %$log);

$api->store->{'log'}=$log;

my $title=$_->{'title'};

$api->log("Processing $title");

# WTF?

if(exists($_->{'missing'})){

$api->warn("$title is missing? WTF?\n");

next;

}

# Ok, check the page

my $tok=$api->edittoken($title);

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;

}

next if exists($tok->{'missing'});

# Get page text

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

# Step 1: Find the parameters for the infobox. Also, strip the

# parameters we are intending to process.

my %infobox_params=();

my $ct=0;

my @process=();

my $outtxt=$api->process_templates($intxt, sub {

my $name=shift;

my @params=@{shift()};

shift; # $wikitext

shift; # $data

my $oname=shift;

return undef unless exists($templates{"Template:$name"});

if($ct++>0){ # More than one infobox?

$self->_log($api, 'Multiple infoboxen', $pageid, $title, "$ct instances of the infobox detected.");

$api->store->{$pageid}=1;

return undef;

}

my @out=();

foreach ($api->process_paramlist(@params)){

$_->{'value'}=~s/^\s+|\s+$//g;

$infobox_params{$_->{'name'}}=$_->{'value'} unless $_->{'value'} eq '';

if(exists($ext_links{$_->{'name'}})){

push @process, $_->{'name'} unless $_->{'value'} eq '';

} else {

push @out, $_->{'text'};

}

}

return "{{$oname|".join("|", @out)."}}";

});

next if $ct>1;

if($ct<1){

$self->_log($api, 'No infobox', $pageid, $title, "No instance of the infobox was found in the page.");

$api->store->{$pageid}=2;

next;

}

unless(@process){

# Nothing to do here.

$api->store->{$pageid}=1000000;

next;

}

# Step 2: Extract the external links section

my $nowiki;

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

my $comments=[];

while(my ($k,$v)=each(%$nowiki)){

push @$comments, $k if $v=~/^