GuildWiki:Bot tasks/Template substitution

Description
This script substitutes templates that are currently transcluded. The process described below is for Template:Unsigned, but the code is generalized for any template.


 * Language: Perl
 * Uses the module Perlwikipedia for most wiki interface tasks.
 * Throttle: 360 edits/hour (10 seconds between edits)
 * Frequency: Should probably be run monthly (weekly?)

Process

 * 1) Gets list of articles from Special:Whatlinkshere/Template:Unsigned.
 * 2) For each article that transcludes the template, edits the article and performs the following edits:

Code
use strict;
 * 1) !/usr/bin/perl

use Perlwikipedia;

open (OFP, ">edits.log") or die "can't open log file: $!\n"; print OFP "\n", "="x20, "\n"; {	my ($sec, $min, $hour, $day, $mon, $year) = (localtime)[0,1,2,3,4,5]; printf OFP "$0 starting at %2d:%02d:%02d %4d-%02d-%02d\n", $hour, $min, $sec, $year+1900, $mon+1, $day; }
 * 1) Log file

my $tname = $ARGV[1];
 * 1) Read the template name from the command line

my $user = 'Bot ishmael'; my $pass = $ARGV[0]; my $editor = Perlwikipedia->new($user);
 * 1) Create a Perlwikipedia object

$editor->{debug} = 1;
 * 1) Turn debugging on, to see what the bot is doing

$editor->set_wiki('guildwars.wikia.com',''); my $status = $editor->login($user, $pass); if ($status) { die "Login failed: $editor->{errstr}\n"; }
 * 1) Login to guildwiki

my $is_minor = 1; my $edit_summary='Template substitution ($tname)';
 * 1) Editing options

my @what_links_here = $editor->what_links_here("Template:$tname");
 * 1) Get all pages that link to the template

foreach my $article (@what_links_here) { # Only process articles that transclude the template if ($article->{type} ne "transclusion") { next; } # Get the text of the article my $text = $editor->get_text($article->{title}); # Insert subst:, looking for both "Template:$tname" and "$tname" by itself $text =~ s/\{\{($tname\|.*?)}}/\{\{subst:$1}}/gi; $text =~ s/\{\{(Template:$tname\|.*?)}}/\{\{subst:$1}}/gi; # For templates with spaces in the name, look for underscore version too if ($tname =~ / /) { $tname =~ s/ /_/g; $text =~ s/\{\{($tname\|.*?)}}/\{\{subst:$1}}/gi; $text =~ s/\{\{(Template:$tname\|.*?)}}/\{\{subst:$1}}/gi; }	# Save the article $editor->edit($article, $text, $edit_summary, $is_minor);

# Record the edit my ($sec, $min, $hour, $day, $mon, $year) = (localtime)[0,1,2,3,4,5]; printf OFP "%2d:%02d:%02d %4d-%02d-%02d\t%s\n", $hour, $min, $sec, $year+1900, $mon+1, $day, $article->{title}; # Wait 10 seconds before making another edit sleep 10; }

{	my ($sec, $min, $hour, $day, $mon, $year) = (localtime)[0,1,2,3,4,5]; printf OFP "\n$0 finishing at %2d:%02d:%02d %4d-%02d-%02d\n", $hour, $min, $sec, $year+1900, $mon+1, $day; } close (OFP);