GuildWiki:Bot tasks/Template substitution

Description
This script substitutes templates that are currently transcluded. The version show below is for Template:Unsigned, but it can be adapted 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 changes "{{unsigned|" to "{{subst:unsigned|"

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 $user = 'Bot ishmael'; my $pass = $ARGV[0]; my $editor = Perlwikipedia->new($user); $editor->set_wiki('guildwars.wikia.com',''); $editor->login($user, $pass);
 * 1) Create a Perlwikipedia object and login

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

my $is_minor = 1; my $edit_summary='Unsigned template substitution by Bot ishmael';
 * 1) Editing options

my @what_links_here = $editor->what_links_here("Template:Unsigned");
 * 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: $text =~ s/\{\{(unsigned\|.*?)}}/\{\{subst:$1}}/g; # 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);