User:Plastikspork/orphan.pl


 * 1) !/usr/bin/perl

use strict; use utf8;
 * 1) Insist that all variables be declared
 * 1) Allow for utf8 characters

use MediaWiki::Bot; use Encode;
 * 1) Use the MediaWiki::Bot library
 * 1) Use the Encode library

my $user = 'Plasticspork'; print "User: $user\n";
 * 1) Username

print "Password: "; my $pass = ;
 * 1) Prompt for password

$pass =~ s/[ \t\r\n]+$//;
 * 1) Removing trailing spaces/newlines from password

my $editor=MediaWiki::Bot->new($user); $editor->set_wiki('en.wikipedia.org','w');
 * 1) Create an editor object on the English language WP

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

if( $editor->login($user, $pass) ) { print "Failed to login!\n"; exit; }
 * 1) Log in

my $template = "Foo";
 * 1) Specify the template to orphan, use 'Foo' for 'Template:Foo'

my $edit_summary='Orphan template per Templates for discussion';
 * 1) Specify what to state in the edit summary

my $is_minor = 1;
 * 1) Label edit as minor. This argument is optional.

my @articlelist = $editor->what_links_here_ns("Template:".$template, 0);
 * 1) Get the list of all transclusions in article-space (namespace 0)

$template =~ s/([\-\.\[\]])/\\$1/gi;
 * 1) Escape any characters used in regular expressions

$template =~ s/^([A-Za-z])(.*)$/[\u$1\l$1]$2/;
 * 1) First character is case insensitive

$template =~ s/ /[_ ]/gi;
 * 1) Wikipedia allows for underscores and spaces in template references

print "Regular expression: $template\n";
 * 1) Display the template regular expression for debuging purposes

foreach my $articlestruct (@articlelist) {
 * 1) Loop over all articles in the article list

# Get the actual article name from the article structure my $article = $articlestruct->{'title'};

# Only want transclusions, not links if( $articlestruct->{'type'} =~ /transclusion/ ) {

# Show which articles are being processed print "Processing: ".$article."\n";

# Pull the wikitext for the article my $text=$editor->get_text($article);

# Remove the template from the article $text =~ s/[ ]*[\r\n]?//g;

# Ask for confirmation to commit the changes print "Press enter to commit or s to skip\n"; my $response = ;

if ($response =~ /s/i) { print "Skipping...\n"; } else { # Submit to Wikipedia. # Note: This does not warn of edit conflicts, it just overwrites existing text. print "Submitting...\n"; $editor->edit($article, $text, $edit_summary, $is_minor); }

# Take a break (frequent edits are forbidden per bot policy) print "Sleep 5\n"; sleep 5; } }

sub MediaWiki::Bot::what_links_here_ns { my $self   = shift; my $article = shift; my $ns     = shift; my @links;
 * 1) Custom module for namespace restricted "what links here" list

$article = MediaWiki::Bot::uri_escape_utf8( $article );

my $res = $self->_get( 'Special:Whatlinkshere', 'view',     "&target=$article&limit=5000&namespace=$ns" ); unless (ref($res) eq 'HTTP::Response' && $res->is_success) { return 1; } my $content = $res->decoded_content; while (   $content =~ m{[^<]+([^<]*)}g ) { my $title = $1; my $type = $2; if ( $type !~ /\(redirect page\)/ && $type !~ /\(transclusion\)/ ) { $type = ""; }   if ( $type =~ /\(redirect page\)/ ) { $type = "redirect"; } if ( $type =~ /\(transclusion\)/ ) { $type = "transclusion"; }

push @links, { title => $title, type => $type }; }

return @links; }