User:AnomieBOT/source/tasks/SafesubstFixer.pm

in specifically named templates with. If appropriate, may also insert  into   and other parser functions, or specifically named template invocations.

=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 BRFA approved 2010-05-05 Bots/Requests for approval/AnomieBOT 37

=cut

sub approved { return -1; }

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

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

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

# Replacements my @re=(       qr/\{\{\{subst\|\}\}\}/, ' safesubst: ',        qr/\{\{#if/, '{{ safesubst: #if',        qr/\{\{#switch/, '{{ safesubst: #switch',    ); my $summary="Replacing {{{subst|}}} with safesubst: (and inserting it into parser function calls) per request";

my $iter=$api->iterator(       generator    => 'allpages',        gapprefix    => 'Uw-',        gapnamespace => 10,        gaplimit     => 'max',        prop         => 'info'    ); while($_=$iter->next){ return 0 if $api->halting;

if(!$_->{'_ok_'}){ $api->warn("Failed to retrieve page list: ".$_->{'error'}."\n"); return 60; }

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

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

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

# Ok, check the page 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; }       next if exists($tok->{'missing'});

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

# Perform the replacements my ($outtxt,$nowiki)=$api->strip_nowiki($intxt); for(my $i=0; $i<@re; $i+=2){ my $search=$re[$i]; my $repl=$re[$i+1]; $outtxt=~s/$search/$repl/g; }       $outtxt=~s! (.*?) ! fix_nested_noinclude($1) !ge; $outtxt=$api->replace_nowiki($outtxt, $nowiki);

# Need to edit? if($outtxt ne $intxt){ $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; }           $api->store->{$_->{'pageid'}}=1; }

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

# No more pages to check return undef; }

sub fix_nested_noinclude { my $x=shift; return $x=~/(.*) safesubst:/ ? " ${1}safesubst: " : " $x "; }

1;