User:Speedy Deletion Bot/sourcecode.pl

From Wikipedia, the free encyclopedia

#!/usr/bin/perl
#Note - when running the script please enter the time
# and the date as arguments (eg. perl main.pl 17:25 12/07/2007) 



# Call the Perlwikipedia module.

# Note that the 'p' is capitalized, due to Perl style.

use Perlwikipedia;

#Start or Logging or Exit with error message
print "Atepting to start logging\n";
open (LOGFILE, ">>logfile.txt") or die "Unable to start logging : $!";
open (ERRORLOG, ">>errorlog.txt") or die "Unable to start logging : $!";
open (LOG, ">>logfile.txt") or die "Unable to start logging : $!";
print "Logging Started Successfully\n";

# Add some text at the beging of the logfile
print LOGFILE "\nLogging Started Succesfully at $_[0] on $_[1].\n";
print ERRORLOG "\nLogging Started Succesfully at $_[0] on $_[1].\n";

#Create a Perlwikipedia object or Exit with error message

print "Atempting to create Perlwikipedia object.\n";
print LOGFILE "Atempting to create Perlwikipedia object.\n";
 $editor=Perlwikipedia->new('Speedy Deletion Bot') or print ERRORLOG "Unable to Create object : $!\nExiting." and die "Unable to create object : $!";

print "Object created Succesfully!\n";
print LOGFILE "Object created Succesfully!\n";


# Turn debugging on, to see what the bot is doing

$editor->{debug} = 1;
print "Debugging is now on.\n";
print LOGFILE "Debugging activated.\n";

login();
getRSSFeed();  

sub login {
        # Username and password (change these when the bot has an account). 

        my $username = 'Speedy Deletion Bot'; 
        my $password = 'secertpassword';

        print "Bot is now trying to log in.\n"; 
        print LOGFILE "Atempting to log in as $username with $password as password.\n";
        
        #try to login - if fails exit with error message        
        $editor->login($username, $password);
        
        print "Bot is now logged in.\n";
        print LOGFILE "Bot now logged in Succesfuly.\n";
        #Sleep for 10 sec
        print "Sleeping for 10 sec\n";
        sleep 10;
}

sub getRSSFeed {
        #Open RSS Feed
        print "Atempting to open RSS Feed.\n";
        print LOGFILE "Atempting to open RSS Feed.\n"; 
        
        #Loop until shutdown request is found on talk page
        $i = 1;
        while ($i != 0) {
                #get RSS Feed
                @RSSFeed = $editor->update_rc(50);
        
                #Check diff's

                for $i (1 .. 50) {
                        #Get info from array 
                        $pagename = $RSSFeed[i]{"pagname"};
                        $revid = $RSSFeed[i]{"revid"};
                        $oldid = $RSSFeed[i]{"oldid"};        
        
                        #Lgging
                        print LOGFILE "\tGetting diffs.\n";

                        #Get diffs
                        $diff1 = $editor->get_text($pagename, $revid) or print ERRORLOG "Error getting diffs : $!.\n" and die "Error getting diffs : $! \n";
                        $diff1 = $editor->get_text($pagename, $oldid) or print ERRORLOG "Error getting diffs : $!.\n" and die "Error getting diffs : $! \n";

                        #Logging
                        print LOGFILE "\tDiffs retrieved  Succesfuly.\n";
                        print LOGFILE "\tChecking Diffs.\n";
                
                
                }
        #Check for shutdown request
        checkEdit();
        @RSSFeed = 0;
        }
}

sub checkDiff {
#Arguments : 1st diff, 2nd diif, Artcile Name, User Name and diff link
#Returns no if user did not remove speedy tag
#Returns yes if user did remove the speedy tag

#Set Up arguments in an array
my (@ops) = @_;

print LOGFILE "\tChecking diff's for $ops[2].\n";


#Check for removal of any db tag

#Run if first diff has a tag   
if ($ops[0] =~ /{{db[|-][^}]*}}/) {
        
        #Run if second diff is missing a tag  
        unless ($ops[1] =~ /{{db[|-][^}]*}}/) {
                #Logging
                print "$ops[3] did remove tag on $ops[2], checking if $ops[3] created the article.\n";
                print LOGFILE "\t\t$ops[3] did remove tag on $ops[2], checking if $ops[3] created the article.\n";
                
                                
                        #Logging
                        print "$ops[3] is the creator of the article reporting.\n";
                        print LOGFILE "\t\t$ops[3] is the creator of the article reporting.\n";
                                print LOG "\t\t$ops[3] is the creator of the article reporting.\n";


#Create Report Text
#Get wikicode from report page
my $text = $editor->get_text($ops[2]);

#Add report to the end of the code

$text = $text . "\n* {{User:Speedy Deletion Bot/Report Template|$ops[3]|$ops[2]}}";

#Check if bot is allowed to edit
checkEdit();

#Try to report
$editor->edit("User:Speedy Deletion Bot/Report", $text, "Reporting removal of speedy deletion tag by creator of article(BOT EDIT)", 1) or print ERRORLOG "Error Reporting shutting down : $! \n" and die "Unable to report shutting down : $!";

                        #Logging                
                        print "Edit Reported.\n";
                        print LOGFILE "Edit Reported Succsesfuly.\n";

                        # Sleep for 10 sec
                        sleep 10;

                        #Return with a value of yes             
                        return "yes";
        
        }
}

        #If no tag
        else {
                print "$ops[3] did not remove tag on $ops[2] or there is no tag on $ops[2].\n";
                print LOGFILE "$ops[3] did not remove tag on $ops[2] or there is no tag on $ops[2].\n";
                
                #Return with a value of no      
                return "no";
        }
}


sub checkEdit {
        #This Sub checks if a request has been made to shut it down on its
        #talk page
        print "Checking talk page for shutdown requests.\n";
        print LOGFILE "Checking talk page for shutdown requests.\n";

        #Get text from talkpage
        my $text=$editor->get_text("User_talk:Speedy_Deletion_Bot");

        #Check for shutdown template
        if ($text =~ "{{User:Speedy Deletion Bot/Shutdown") {
                print LOGFILE "Shutdown request found on talkpage, shutting down.\n" and 
                die "Shutdown request found on talkpage, shutting down.\n";
        }
        else {
                print "No request found bot will not shutdown.\n";
                print LOFILE "No request found bot will not shutdown.\n";
        } 
        
}