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 (IFP, "build-corr.txt") or die "can't open file: $!\n"; while () { chomp; my ($old, $new) = split /;/; ($corr{$old} = $new) =~ s/_/ /g; } close (IFP); print "Read corr file.\n";
 * 1) Read the correspondence file (namespace is included in new name)

open (OFP, ">edits.log") or die "can't open log file: $!\n"; print OFP "\n", "="x20, "\n"; { # These brackets are just to block off the time variables so I don't have to worry about multiple declarations 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);
 * 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 $editor->{errstr}; }
 * 1) Login to guildwiki

my $is_minor = 1; my $edit_summary='Builds redlinks rewrite';
 * 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 $build (@builds) { my $buildname = $build->{title}; my $count = $build->{count};

# 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 if ($count < 10 || !defined($new_buildname)) { $text =~ s!\[\[$buildname(.*?)\]\]!\{\{DeletedLink\|$buildname$1\}\}!g; $buildname =~ s/_/ /g; # Links can use underscores or spaces, do this and match again to cover both possibilities $text =~ s!\[\[$buildname(.*?)\]\]!\{\{DeletedLink\|$buildname$1\}\}!g; } else { $text =~ s!\[\[$buildname(\|(.*?)|)\]\]!\[\[PvX:$new_buildname\|$2\]\]!g; $buildname =~ s/_/ /g; $text =~ s!\[\[$buildname(\|(.*?)|)\]\]!\[\[PvX:$new_buildname\|$2\]\]!g; }		# Save the article $editor->edit($article->{title}, $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\t%s\n", $hour, $min, $sec, $year+1900, $mon+1, $day, $article->{title}, $buildname; last; # Wait 10 seconds before making another edit sleep 10; } } { # These brackets are just to block off the time variables so I don't have to worry about multiple declarations 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);



sub builds_from_wantedpages { my @links;

my $res = $editor->_get( 'Special:Wantedpages', 'view', "&limit=1000" ); unless ($res) { return 1; } my $content = $res->decoded_content; while ( $content =~ m{(\d+) links}g ) { my $full_title = $1; my $count = $2; (my $title = $full_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, { title => $full_title, count => $count }; }   }

return @links; }