GuildWiki:Bot tasks/Relinking moved pages

Description
This script moves all articles in one category to another category.


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

Process
For moving articles from "Category:Monk Bosses" to "Category:Monk bosses":
 * 1) Gets list of articles from.
 * 2) For each article, performs the following edit:

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

use Perlwikipedia;

my $oldcat = $ARGV[1]; my $newcat = $ARGV[2];
 * 1) Read the category names from the command line

open (OFP, ">$0.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; print OFP "Replacing Category:$oldname with Category:$newname\n"; }
 * 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 "Login failed: $editor->{errstr}\n"; }
 * 1) Login to guildwiki

my $is_minor = 1; my $edit_summary="Category rename: Category:$oldname -> )";
 * 1) Editing options

my @all_pages = $editor->get_pages_in_category("Category:$oldname");
 * 1) Get all pages in the old category

foreach my $article (@all_pages) { # Get the text of the article my $text = $editor->get_text($article->{title}); # Replace the old category with the new one $text =~ s/\[\]Category:$oldname(\|.*?|)\]\]/\[\]Category:$newname$1\]\]/gi; # For cats with spaces in the name, look for underscore version too if ($oldname =~ / /) { $oldname =~ s/ /_/g; $text =~ s/\[\[Category:$oldname(\|.*?|)\]\]/\[\[Category:$newname$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);