User:Plastikspork/orphan.pl

From Wikipedia, the free encyclopedia
#!/usr/bin/perl

# Insist that all variables be declared
use strict;
# Allow for utf8 characters
use utf8;

# Use the MediaWiki::Bot library
use MediaWiki::Bot;
# Use the Encode library
use Encode;

# Username
my $user = 'Plasticspork';
print "User: $user\n";

# Prompt for password
print "Password: ";
my $pass = <STDIN>;

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

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

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

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

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

# Specify what to state in the edit summary
my $edit_summary='Orphan template per [[Wikipedia:Templates for discussion]]';

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

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

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

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

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

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

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

  # 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/{{[_ ]*(?:[Tt]emplate:|)$template[_ ]*[^{}]*}}[ ]*[\r\n]?//g;

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

    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;
  }
}

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

  $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{<li><a href="[^"]+" title="([^"]+)">[^<]+</a>([^<]*)}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;
}