Purple exclamation mark.svg Planning the future of Botwiki! - Help us bring Botwiki up to date, contribute to our strategy discussion, add bot scripts, and contribute manuals, guides, and tutorials! Almost anything related to bots, particularly those used to edit mediawiki, is welcome.

Red exclamation mark.svg UNABLE TO EDIT? - We've experienced attacks by spambots lately and now require you to confirm your e-mail before you can edit (go to your preferences, enter an e-mail address, and request a confirmation e-mail, then go to your e-mail and click on the confirmation link). We also require new accounts to make a few edits and wait a few minutes before before you can create a page; however, if this is a problem contact us in #botwiki and we can manually confirm your account. Sorry for the inconvenience.

Perl:AntiVandalHelper.pl

From Botwiki
Jump to: navigation, search

This is a helper function I wrote to quickly aid in reverting vandalism and blocking users. It is not a bot per se. It depends on the MediaWiki package as well as a customization to the page.pm module given at Perl:MediaWiki Page blockFine

You will need to make some small code changes to point at your wikis, as well as set up two small text files

  • username
  • password

both single line without linefeed containing username and password respectively. Place these in the same dir as the code, and launch from that dir.

It depends on using firefox to display diffs/histories etc, you will need to customise the firefox path as well.

This code is really rough around the edges but may be of some help to some? Eagle101 helped me by giving me a starting point of code that did some things.

#!/usr/bin/perl -w
#---------------------------------------------------------------------------#
# 
# Utility Functions to help admins do things
#                      
# Author: Larry Pieniazek (IBM) as hobby project
# Adapted from stuff I cribbed from all over.  
# (c)Larry Pieniazek 2007. This library is free software; you can redistribute it 
# and/or modify it under the same terms as Perl itself.  
# additionally, it can be redistributed and modified under GFDL or CC-SA as you choose        
# 
# Abstract:
#   Connects to a wiki, scans recent changes and presents a menu of things to do
#
 
use strict;
 
# things to include
 
 
use MediaWiki; 
use Data::Dumper;
 
 
# ------------- forward declare subroutines ------------------
 
sub UserIsBlocked; 
sub ExtractDiffLink;
sub ShowDiff;
sub ShowInboundLinks;
sub BlockAndTag;
sub ShowMainMenu;
sub LaunchSpecialMode;
sub LaunchEditMode;
sub LaunchDiffMode;
sub LaunchHistoryMode;
sub AskForChoice;     
sub DeletePage;
 
# -------------- constants ------------------------------------
 
my $firefoxPath = 'C:\Progra~1\Mozilla\Firefox\firefox.exe'; # customise for your installation 
 
# -------------- data dumper switches -------------------------
    $Data::Dumper::Indent = 2;         # pretty print (3 is with array indices
    $Data::Dumper::Useqq = 1;          # print strings in double quotes
    $Data::Dumper::Pair = " : ";       # specify hash key/value separator
    $Data::Dumper::Purity = 1;         # fill in the holes for eval
    $Data::Dumper::Maxdepth = 4;       # no deeper than 3 refs down
    $Data::Dumper::Deepcopy = 0;       # deep copy    
 
 
# -------------- main start -----------------------------------
 
# declares 
 
my ( $c, $host, $path, $menu, $wch, $done, $isOK, $getString);             
 
# init MW obj
 
$c = MediaWiki->new;
 
# which wiki?
 
print <<EOF1;
   Choose which wiki to work on
      b - Brickwiki
      g - Great Lakes Wiki 
EOF1
 
do {        
   $done="FALSE";
   $wch= AskForChoice("   Wiki","g");
   if ($wch eq "b"){ 
      $host= 'brickwiki.org';
      $path= '';
      $done="TRUE";  
   } elsif ($wch eq "g"){  
      $host= 'greatlakeswiki.org';
      $path= '';
      $done="TRUE";
   } else {
      print "that was not a valid wiki choice, please try again \n\n";
   }  
   #print "done:".$done."\n";
} until ($done eq "TRUE");
 
# following user/password management cribbed from 
#   http://en.wikipedia.org/wiki/User:HBC_AIV_helperbot/source
 
open(PASS,'password');                  # A file with only the password, no carriage return
sysread(PASS, my $password, -s(PASS));  # No password in sourcecode.
close(PASS);
open(USER,'username');                  # A file with only the username, no carriage return
sysread(USER, my $username, -s(USER));  #
close(USER);
 
 $isOK = $c->setup({
        'bot' => { 'user' => $username, 'pass' => $password },
        'wiki' => {
                'host' => $host,
                'path' => $path
        }}) || die "Failed to log in\n";
 
# $isOK = $c->setup( "brickwiki.ini" ) || die "Failed to log in\n";
 
# set default summary
$c->{summary} = "edits via perl MediaWiki framework";
 
# how far back are we going?
my ( $daysToFetch, $daysOrChanges, $changesToFetch, $hideSelfChanges, $pageTitle);
my ($recChangesStruc);
$daysOrChanges= AskForChoice("fetch by (d)ays or number of (c)hanges","c");
 
# hide own edits?
$hideSelfChanges=AskForChoice("Hide my own changes (does not work for all days) y/n ","y");
$hideSelfChanges=($hideSelfChanges eq "y")?"&hidemyself=1":"";  #hidemyself is the string to hide self
 
if ($daysOrChanges eq "d") {                                                                       
    $daysToFetch= AskForChoice("Days to fetch","all");
    if ($daysToFetch eq "all"){ 
        print "fetching all days\n";
        $recChangesStruc = $c->get("Special:Recentchanges");
    } else {
        $daysToFetch = ($daysToFetch =~ /\d+/)?$daysToFetch:"1"; # default to 1 if not all numeric answer
        print "fetching ".$daysToFetch." days\n";       
        $pageTitle="Special:Recentchanges&days=".$daysToFetch.$hideSelfChanges;
 
        # print "fetching page:".$pageTitle."\n";
 
        # $pg = $c->{ua}->get("http://greatlakeswiki.org/index.php?title=Special:Recentchanges&days=1")->content();
        $getString="http://".$host."/".$path."/index.php?title=".$pageTitle;  
        $recChangesStruc = $c->{ua}->get($getString);
    }
} else {
    $changesToFetch= AskForChoice("Recent changes to fetch","30");
    $changesToFetch=($changesToFetch>1000)?1000:$changesToFetch;
    $changesToFetch=($changesToFetch<1)?1:$changesToFetch;   
    print "fetching ".$changesToFetch." changes\n";     
    $pageTitle="Special:Recentchanges&limit=".$changesToFetch.$hideSelfChanges;
    $getString="http://".$host."/".$path."/index.php?title=".$pageTitle;  
    $recChangesStruc = $c->{ua}->get($getString);
}
#http://greatlakeswiki.org/index.php?title=Special:Recentchanges&limit=347
 
my( $recentChanges, @rcMatches, $rcScanOrder, $rcEntries, $origRcEntries);
$recentChanges = $recChangesStruc ->content();                              
 
# grab the entire recent change list
@rcMatches = $recentChanges =~ m|<li>.*?</li>|g;
$origRcEntries = $rcEntries = @rcMatches;
 
print "There are: ".$rcEntries ." recent changes to review\n\n"; 
 
$rcScanOrder=AskForChoice("Scan changes in (f)orward or (r)everse order","r");
 
if ($rcScanOrder eq "r") {
   # reverse order of list to go from oldest to newest         
   @rcMatches = reverse(@rcMatches);
}                                
 
my ($eStruc, $pg, $lastUser, $diff, $diffLink, $diffUserPart, $diffPagePart, $pageName, $userID, $isBlocked, $goNext, $gnFlag, $t1, $t2, $specialType, $stFlag);
RCM: foreach $diff (@rcMatches) {
    print "Recent change:\n". $diff . "\n\n";
    $specialType= ""; 
    # need to work to detect blocks, moves, deletes etc... handle specially.
    if ($diff =~ m|title="Special:Log/| ) { # this is a special page, it's a move, protect, delete, etc  
        ( $t1,$specialType ) = split ( /title="Special:Log\// ,$diff);
        ( $specialType,$t2 ) = split ( /"/                    ,$specialType);
        print "This entry is a Special page of type: ".$specialType." ";
    }                
    # insert logic here to handle special pages... for now just skip to next entry
 
    #type upload is one to consider handling, as is type move.
    if ($specialType ne "") {
	print "...skipping to next entry\n\n";
        next RCM;
    }
 
    # does not handle things when the page itself is a user talk page very well...
 
    ($diffPagePart,$diffUserPart) = split ( />hist</ , $diff);   
 
    # $diffPagePart = $diffUserPart = $diff;
 
    # print "diffPagePart:\n".$diffPagePart."\n\n";
    # print "diffUserPart:\n".$diffUserPart."\n\n";
 
    $diffPagePart =~ m|action=history\" title="(.*)?\"|;
    $pageName = $1;     
    $pageName =~ s/&amp;/&/g;
 
 
    # works for BW but not GLW
    # $diff =~ m|Special:Contributions\&amp;target=(.*)" title="Special:Contributions"|; 
    # works for GLW but not BW 
    #$diff =~ m|Special:Contributions(.*)" title="Special:Contributions"|;  
 
    # getting from the talk is not reliable, but getting it from the blockip should be.
    # $diffUserPart =~ m|"User talk:(.*)">Talk</a>|;
 
    $diffUserPart =~ s/Special:Blockip&amp;ip=/Special:Blockip\//g;
    $diffUserPart =~ m|Special:Blockip/(.*)" title="Special:Blockip|;
 
    $userID = $1;  
    print "This revision of the page: [[".$pageName."]] was by [[user:".$userID."]]\n";    
    # who edited this last?
    $pg=$c->get($pageName);                                                   
    $eStruc = $pg->last_edit;
    $lastUser = $eStruc->{user};
 
#    open(DEBUGDUMP,">  ./DataDumper.txt") || die("can't open debugdump: $!");
#    print {DEBUGDUMP} ' ---$pg--------------------------------------------------------'."\n\n"; 
#    print {DEBUGDUMP} Dumper($pg); 
#    print {DEBUGDUMP} ' ---$eStruc--------------------------------------------------------'."\n\n"; 
#    print {DEBUGDUMP} Dumper($eStruc); 
#    print {DEBUGDUMP} ' ---$lastUser-------------------------------------------------------'."\n\n";   
#    print {DEBUGDUMP} Dumper($lastUser);
#    print {DEBUGDUMP} ' -----------------------------------------------------------'; 
 
    $diffLink=ExtractDiffLink($diff);
    print "The link to this diff is: ".$diffLink."\n";  
 
    #showDiff($diffLink);
 
    $isBlocked=UserIsBlocked($userID);
    print "The editor of this revision, [[user:".$userID."]] is apparently ".$isBlocked.", ";
    if ($userID eq $lastUser) {
	print "and is the most recent editor\n";
    } else {
	print "however the most recent edit to the page was by [[user:".$lastUser."]]\n";
    }
    do {
       $gnFlag="FALSE";
       ShowMainMenu(); 
       $goNext=AskForChoice("   Action","n");          
 
       if ($goNext eq "q") { # quit operations
          print "exiting early...";
          $goNext="early exit";
          last RCM;
       } elsif ($goNext eq "n") { # next change
          $rcEntries--;
          $gnFlag = "continue"
       } elsif ($goNext eq "b")  {
          BlockAndTag($userID);
          $rcEntries+=2;
       } elsif ($goNext eq "e")  {
          LaunchEditMode($pageName);
          print "... Launched edit on [[".$pageName."]]\n\n";   
	  $rcEntries++;
       } elsif ($goNext eq "d")  {
          LaunchDiffMode($diffLink);
          print "... Launched current diff of [[".$pageName."]]\n\n";   
	  $rcEntries++;
       } elsif ($goNext eq "h")  {
          LaunchHistoryMode($pageName); 
          print "... Launched history of [[".$pageName."]]\n\n";
	  $rcEntries++;
       }  elsif ($goNext eq "t")  {         
          $t1="User_talk:".$userID;
          LaunchEditMode($t1);
          $rcEntries++;
       }  else {
          print" .... unrecognised action:[".$goNext."] please try again.\n";
       }
   } until ($gnFlag eq "continue"); # end of ask for action loop                                   
 
} #  end of RCM loop
 
if ($goNext ne "early exit") { print "Completed the changes selected...";}
print "approximate Recent Changes (after adding new changes in) remaining: ".$rcEntries."\n\n";	
 
# ----------------------- end of main program --------------------------------
 
# ----------------------------------------------------------------------------
# Get a choice from user
# typical invoke:
#    $wch=AskForChoice($promptText,$defChoice);  
# ----------------------------------------------------------------------------
 
sub AskForChoice {
   my($promptText,$defChoice,$wch);
   $promptText=$_[0];
   $defChoice=$_[1];
   print $promptText.": [".$defChoice."]?";
   $wch = <>;
   chomp $wch ;
   # print ">>>".$wch."<<<\n";
   $wch = ($wch eq "")?$defChoice:$wch;
   # print ">>>".$wch."<<<\n";
   return $wch;
}
 
# ----------------------------------------------------------------------------
# show main menu
# ----------------------------------------------------------------------------
sub ShowMainMenu{
 
print <<EOFMM;
   Choose an action
      ------ user -----------------------------------------
      b - [B]lock [[User:$userID]]  
      t - Edit the [t]alk of [[User:$userID]]
      ------ page -----------------------------------------
      e - [E]dit  [[$pageName]]                              
      h - View [h]istory of   [[$pageName]]           
      d - View this [d]iff of [[$pageName]]                  
      ------ general --------------------------------------
      n - go to [n]ext change in recent changes
      q - [q]uit program
EOFMM
}                                                                             
 
# ----------------------------------------------------------------------------
# Launch diff mode on $pageName 
#   sample string:
#     http://greatlakeswiki.org//index.php?title=Who_should_I_contact_if_I_have_questions&curid=2149&diff=12321&oldid=12170 
#   sample system command:
#     system 'C:\Progra~1\Mozilla\Firefox\firefox.exe -new-tab http://brickwiki.org/index.php?title=Special:Recentchanges';
# ----------------------------------------------------------------------------
sub LaunchDiffMode {        
   my ($diffLink, $specialMode, $cmdString, $isOK);
   $diffLink=$_[0]; 
   $specialMode=$_[1];  
 
   # replace blanks with underscores 
   $diffLink =~ s/ /_/g;
   # decode other URL encodings such as ()    
   # (from http://perldoc.perl.org/perlfaq9.html#How-do-I-decode-or-create-those-%25-encodings-on-the-web%3f)
   $diffLink =~ s/%([A-Fa-f\d]{2})/chr hex $1/eg;  
 
   $cmdString="http://".$host."/".$path."/index.php?title=".$diffLink;     
   $cmdString= $firefoxPath." -new-tab ".$cmdString;
 
   # print "about to run:\n  [[[".$cmdString."]]]\n";
   $isOK = system  $cmdString;      
 
}
 
# ----------------------------------------------------------------------------
# Launch special mode on $pageName 
#   sample string:
#     http://brickwiki.org/index.php?title=Brickwiki:Resources&action=edit  
#   sample system command:
#     system 'C:\Progra~1\Mozilla\Firefox\firefox.exe -new-tab http://brickwiki.org/index.php?title=Special:Recentchanges';
# ----------------------------------------------------------------------------
sub LaunchSpecialMode {        
   my ($pageName, $cmdString, $specialMode, $isOK);
   $pageName=$_[0]; 
   $specialMode=$_[1];  
 
   if (!($specialMode =~ /history|edit/)){
      print "error, ".$specialMode." not supported";
   }
 
   if (!($c->exists($pageName))) {
      print "it seems ".$pageName."does not exist yet"
   } 
   # replace blanks with underscores 
   $pageName =~ s/ /_/g;
   $cmdString="http://".$host."/".$path."/index.php?title=".$pageName."&action=".$specialMode;     
   $cmdString= $firefoxPath." -new-tab ".$cmdString;
 
   # print "about to run:\n  [[[".$cmdString."]]]\n";
   $isOK = system  $cmdString;      
 
}
 
 
# ----------------------------------------------------------------------------
# Launch history on $pageName 
#   sample string:
#     http://brickwiki.org/index.php?title=Brickwiki:Resources&action=history  
#   sample system command:
#     system 'C:\Progra~1\Mozilla\Firefox\firefox.exe -new-tab http://brickwiki.org/index.php?title=Special:Recentchanges';
# ----------------------------------------------------------------------------
sub LaunchHistoryMode {        
   my ($pageName);
   $pageName=$_[0];           
   LaunchSpecialMode($pageName,"history");              
}
 
# ----------------------------------------------------------------------------
# Launch editor on $pageName 
#   sample string:
#     http://brickwiki.org/index.php?title=Brickwiki:Resources&action=edit  
#   sample system command:
#     system 'C:\Progra~1\Mozilla\Firefox\firefox.exe -new-tab http://brickwiki.org/index.php?title=Special:Recentchanges';
# ----------------------------------------------------------------------------
sub LaunchEditMode {        
   my ($pageName);
   $pageName=$_[0];           
   LaunchSpecialMode($pageName,"edit");                                               
}
 
# ----------------------------------------------------------------------------
# extract the link to a diff from the revision history line
# ----------------------------------------------------------------------------
sub ExtractDiffLink{
 
    my ($diff, $diffLink); 
    my ($j1, $j2);
    $diff=$_[0];
    ($diffLink,$j1)= split ( /\" title=\"/,$diff);  
    #print "so far:\n".$diffLink."\n\n";
    ($j1,$diffLink)= split ( /index.php\?title=/,$diffLink);  
    #print "so far:".$diffLink."\n\n";
    $diffLink  =~ s/&amp;/&/g;
    #print "so far:\n".$diffLink."\n\n";
    return $diffLink;
}                                                                             
 
# ----------------------------------------------------------------------------
# show the inbound links, or report none. Return count of links, or neg for error
# ----------------------------------------------------------------------------
sub ShowInboundLinks  {
    my($pageName, $pageText, $getString, $pg);
    $pageName=$_[0];
    $getString="";
    $pg=$c->get("Special:Whatlinkshere/".$pageName);
    $pageText = $pg->content();
}          
 
# ----------------------------------------------------------------------------
# using link to a diff, show the diff key text without all the rest of the page
# ----------------------------------------------------------------------------    
sub ShowDiff{
    my ($diffLink, $getString, $diffResult, $pg, $pageText); 
    my ($j1, $j2);
    $diffLink=$_[0];
    $getString="http://".$host."/".$path."/index.php?title=".$diffLink; 
 
    $pg = $c->{ua}->get($getString);         
    $pageText = $pg->content();                
 
    ($j1,$pageText) = split (/<div id="contentSub">\(Difference between revisions\)<\/div>/,$pageText);    
 
    #($pageText,$j2) = split ( /<hr \/><h2>Current revision|<hr /><h2>Revision as of/,$pageText);
    #($pageText) =~ m!(.*)?(<hr \/><h2>Current revision|<hr /><h2>Revision as of).*!; 
    ($pageText,$j2) = split ( /<hr \/><h2>/,$pageText);
    # print "so far:\n".$pageText."\n\n"; 
 
    ($j1,$pageText) = split (  /Mark as patrolled<\/a>]<\/td>/   ,$pageText);
    print "so far:\n".$pageText."\n\n";
}
 
# ----------------------------------------------------------------------------
# invoke the standard MediaWiki block function
# ----------------------------------------------------------------------------   
sub BlockRegular{     
    my ($userID,   $isOK, $blockDuration );
    $userID=$_[0];         
    $blockDuration=$_[1];  
 
    $isOK=$c->block($userID, $blockDuration);
    return $isOK;
}
 
# ----------------------------------------------------------------------------
# invoke the MediaWiki::page block function
# ----------------------------------------------------------------------------   
sub BlockViaPage{     
    my ($userID,   $isOK, $blockDuration );
    $userID=$_[0];         
    $blockDuration=$_[1];       
 
    my $obj= $c->get("User:$userID", "");  # this is the page of the user
 
    my ($anonOnly, $createAccount, $enableAutoblock);
    $anonOnly="0";
    $createAccount="1";
    $enableAutoblock="1";
 
    $isOK=$obj->blockFine($blockDuration,$anonOnly, $createAccount, $enableAutoblock);     # call page function
    #$isOK=blockPage($obj,$blockDuration);     # call nonpage function
    return $isOK;
}  
 
# ----------------------------------------------------------------------------
# invoke the block via using page function copy (does not work), context problems
# ----------------------------------------------------------------------------   
sub BlockFineGrained{     
    my ($userID, $blockDuration ); # parms
    $userID=$_[0];         
    $blockDuration=$_[1];  
 
    my ( $isOK );         # work vars
 
    my ($obj,$user);   
 
	my $edittoken_rev_regex = qr/(?<=name=['"]wpEditToken['"] value=["'])[0-9a-f+]+\\?(?=["'])/;                  
 
    $obj= $c->get("User:$userID", "");  # this is the page of the user
 
    # from here this is the code for block() from page.pm
 
	$user = $obj->_pagename();   
 
	print "BFG: user: ".$user." \n";    
 
    my $DEBUGDUMP;      
    open($DEBUGDUMP,">  ./DataDumper.txt") || die("can't open debugdump: $!");
    print {$DEBUGDUMP} ' ---\$obj-before get of blockip-----------------------------------'."\n\n"; 
    print {$DEBUGDUMP} Dumper($obj); 
 
 
	if(!$obj->{prepared})
	{
		my $res = $obj->{ua}->get($obj->_wiki_url("Special:Blockip/" . $obj->{title}));
		return unless($res->is_success);
		$res = $res->content;    
        print "BFG before regex eval \n";
		$res =~ /$edittoken_rev_regex/;
        print "BFG after regex eval \n";
		$obj->{edittoken} = $&;
	}
	$obj->{prepared} = 0;
    print "BFG after prepare reset \n";
    print {$DEBUGDUMP} ' ---\$obj-after prepare reset-----------------------------------'."\n\n"; 
    print {$DEBUGDUMP} Dumper($obj); 
 
    my $urlB=  $obj->_wiki_url("Special:Blockip")."&action=submit";
    print "BFG: what URL is:>>". $urlB."<<\n"; 
 
 
	$isOK= $obj->{ua}->request(
		#POST $obj->_wiki_url("Special:Blockip") . "&action=submit",
        POST $urlB,
		Content_Type  => 'application/x-www-form-urlencoded',
		Content       => [(
			'wpBlockAddress' => $obj->_pagename(),
			'wpBlockExpiry' => 'other',
			'wpBlockOther' => $blockDuration,
			'wpBlockReason' => $obj->_summary(),
			'wpEditToken' => $obj->{edittoken},
			'wpBlock' => 'Block'
		)]
	)->code == 302;
 
    return $isOK;
}
 
# ----------------------------------------------------------------------------
# block $userID and tag talk page with {{vandalblock}} appending if already text there 
# ----------------------------------------------------------------------------
sub BlockAndTag { 
    my ($userID,  $pageTitle, $getString, $pg, $pageText, $isOK, $oldText, $newText, $j1, $j2, $blockDate, $blockDuration );
    $userID=$_[0];         
 
    $blockDuration= "infinite";
    $c->{summary} = "Block user as vandal, via perl MediaWiki framework";
    # $isOK=$c->block($userID, $blockDuration);   
    # $isOK=BlockFineGrained( $userID, $blockDuration);
    $isOK=BlockViaPage( $userID, $blockDuration);
 
    if($isOK) {
	print "block of ".$userID." succeeded\n";
    } else {
	print "block of ".$userID." failed, aborting\n";
        return;
    }                                  
    $pageTitle="User_talk:".$userID; 
    if ($c->exists($pageTitle)){                       
        print "Found page: ".$pageTitle."...";
	$newText=$c->get($pageTitle)->content();  
        if ($newText =~ /\{\{vandalblock\}\}/) { # if the vandalblock text is already there
            print "... and it is already tagged with the {{vandalblock}} text\n" ;
            return;
        }
        $newText.="\n";
    } else {      
        print "creating page: ".$pageTitle."...";
        $newText="";
    }
    $newText.="{{vandalblock}}\n";         
    print "proposing to add:".$newText." to ".$pageTitle."\n";                          
    $c->{summary} = "adding {{tl|vandalblock}} notice, via perl MediaWiki framework";
 
    $isOK=$c->text( $pageTitle , $newText );
 
    if($isOK) {
	print "update of:[[".$pageTitle."]] succeeded\n";
    } else {
	print "update of:[[".$pageTitle."]] failed, aborting\n";
        return;
    }      
    #print "isok:".$isOK."\n";
}
 
# ----------------------------------------------------------------------------
# roughly test if user is blocked 
#   (needs to be enhanced to allow for unblock, last block expired, etc)
# ----------------------------------------------------------------------------                     
sub UserIsBlocked {
 
    my ($userID,  $pageTitle, $getString, $pg, $pageText, $isOK, $j1, $j2, $blockDate, $blockDuration, $wasUnblocked,  );
    $userID=$_[0];
    # print "checking user:".$userID."... ";
    # http://brickwiki.org/index.php?title=Special:Log&type=block&page=User:122.154.4.3 
 
    $pageTitle="Special:Log&type=block&page=User:".$userID;
 
    $getString="http://".$host."/".$path."/index.php?title=".$pageTitle;   
 
    $pg = $c->{ua}->get($getString);   
 
    $pageText = $pg->content();
 
    #$pageText =~ m|View \(previous(.*)View \(previous|;    
    #$pageText =$1;
    ($j1,$pageText,$j2)= split(/View \(previous/,$pageText);  
    # print "j1 :".$j1."\n";
    # print "partial page content:".$pageText  ."\n";
    # print "j2 :".$j2."\n";      
    if ( $pageText =~ m|No matching items in log|) {
	# print "not blocked \n"; 
        return "unblocked";
    } else {
        ($j1,$pageText,$j2)= split(/<li>/, $pageText);  # get the very newest block
        ($blockDate,$j1)= split(/ <a href=/,$pageText);
        ($j1,$blockDuration)=split(/ with an expiry time of <span title="/,$pageText);
        ($blockDuration,$j2)=split(/">/,$blockDuration);                           
        $wasUnblocked = $pageText =~ m|a>(\)*) unblocked <a href|;
        print "partial block page content:\n".$pageText  ."\n\n";    
        print "date of block: ". $blockDate." duration: ".$blockDuration."\n";   
        return "unblocked" if $wasUnblocked;
        return "blocked";
    }    
    return 0;
}
Personal tools
Share