GuildWiki:Bot tasks/Builds redlinks rewrite

Description
This script rewrites orphaned links to the old Build: namespace articles, pointing them instead to the appropriate article on the PvX Wiki.


 * Language: Perl
 * Uses the module Perlwikipedia for most wiki interface tasks.
 * Throttle: 360 edits/hour (10 seconds between edits)

Process

 * 1) Scans Special:Wantedpages for links to builds, which are identified by beginning with "Team" or a profession abbreviation followed by a slash, such as "W/".
 * 2) Checks a correspondence file (to be compiled by Ereanor) for the new name of the build on PvX Wiki.
 * 3) For each build redlink found, scans Special:Whatlinkshere for that link and gets a list of the articles containing that specific redlink.
 * 4) For each article containing a build redlink, edits the article to rewrite the link as follows:

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

use Perlwikipedia;

my %corr; open (INF, "corr.txt") or die "can't open file: $!\n"; while () { my ($old, $new) = split /;/; $corr{$old} = $new; } close (INF);
 * 1) Read Ereanor's correspondence file

open (OPF, ">edits.log") or die "can't open log file: $!\n"; print OPF "\n", "="x20, "\n"; {	my ($sec, $min, $hour, $day, $mon, $year) = (localtime)[0,1,2,3,4,5]; printf OPF "$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='Builds redlinks rewrite by Bot ishmael';
 * 1) Editing options

my @builds = builds_from_wantedpages; # Custom function not from Perlwikipedia; see below
 * 1) Pull all build redlinks from Special:Wantedpages

foreach my $buildname (@builds) { # Get the new buildname my $new_buildname = $corr{$buildname}; # Get the list of articles linking to this build my @what_links_here = $editor->what_links_here($buildname); foreach my $article (@what_links_here) { # Get the text of the article my $text = $editor->get_text($article->{title}); # Rewrite the links $text =~ s/\[\[$buildname\]\]/\[\[PvX:Build:$new_buildname\|\]\]/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 OPF "%2d:%02d:%02d %4d-%02d-%02d\t%s\t%s\n", $hour, $min, $sec, $year+1900, $mon+1, $day, $article->{title}, $buildname; # Wait 10 seconds before making another edit sleep 10; } } {	my ($sec, $min, $hour, $day, $mon, $year) = (localtime)[0,1,2,3,4,5]; printf OPF "\n$0 finishing at %2d:%02d:%02d %4d-%02d-%02d\n", $hour, $min, $sec, $year+1900, $mon+1, $day; } close (OPF);



sub builds_from_wantedpages { my @links;

my $res = $editor->_get( 'Special:Wantedpages', 'view', "&limit=5" ); unless ($res) { return 1; } my $content = $res->decoded_content; while ( $content =~ m{<a href="/index.php\?title=(.*?)&amp;action=edit"}g ) { my $full_title = $1; $title =~ s/^Build://; # Only pass through titles that look like build articles if ( 			$title =~ m{^Team} 			|| $title =~ m{^(?:W|R|Mo|Me|N|E|A|Rt|P|D)/(?:W|R|Mo|Me|N|E|A|Rt|P|D|Any)} 			) {			push @links, $full_title; }   }

return @links; }