[PerlWikiBot] SF.net SVN: perlwikibot: [19] trunk
Status: Pre-Alpha
Brought to you by:
rotemliss
From: <rot...@us...> - 2006-09-16 11:46:09
|
Revision: 19 http://svn.sourceforge.net/perlwikibot/?rev=19&view=rev Author: rotemliss Date: 2006-09-16 04:45:57 -0700 (Sat, 16 Sep 2006) Log Message: ----------- Reformatting the code: whitespaces, code style, return statements, etc. Modified Paths: -------------- trunk/bot.pl trunk/config/configure.sample trunk/config/runtime.sample trunk/includes/functions.pm trunk/includes/http.pm Modified: trunk/bot.pl =================================================================== --- trunk/bot.pl 2006-09-16 11:06:13 UTC (rev 18) +++ trunk/bot.pl 2006-09-16 11:45:57 UTC (rev 19) @@ -10,27 +10,22 @@ use config::runtime; # Counters -my ($i, $j, $k, $l, $m); +my ( $i, $j, $k, $l, $m ); # Do nothing if no server specified -if (!$ARGV[0]) -{ - die("No server name set - please set server name!\n"); +if ( !$ARGV[0] ) { + die "No server name set - please set server name!\n"; } # Show notes about the sending pages configuration -if ($configure::sendPages == 0) -{ +if ( $configure::sendPages == 0 ) { print "Note: There will be no changes in the real server. Please set sendPages to 1 to do them.\n"; -} -elsif ($configure::sendPages == 2) -{ +} elsif ( $configure::sendPages == 2 ) { print "Note: A prefix will be used when editing pages. Please set sendPages to 1 to disable that.\n"; } # Log in to the server only if you have to do some stuff in the server -if ($configure::sendPages == 1 || $configure::sendPages == 2) -{ +if ( $configure::sendPages == 1 || $configure::sendPages == 2 ) { functions::login(); } @@ -38,15 +33,13 @@ my @pages = functions::getMatchingPages(); # Execute actions of all the pages -for ($i = 0; ($i <= $#pages); $i += 2) -{ +for ( $i = 0; $i <= $#pages; $i += 2 ) { # Get title and action my $title = $pages[$i]; - my $action = $configure::actions{$pages[$i + 1]}; - + my $action = $configure::actions{ $pages[$i + 1] }; + # Execute the action (TODO: add the actions which were supported in the previous bot) - if ($action eq "replace") - { - functions::replaceInPage($title); + if ( $action eq "replace" ) { + functions::replaceInPage( $title ); } } Modified: trunk/config/configure.sample =================================================================== --- trunk/config/configure.sample 2006-09-16 11:06:13 UTC (rev 18) +++ trunk/config/configure.sample 2006-09-16 11:45:57 UTC (rev 19) @@ -8,9 +8,6 @@ # Libraries use Time::local; -# Counters -my ($i, $j, $k, $l, $m); - # Available servers and details about them, the bot should work on our %servers = ( "server" => "http://www.server.com", @@ -34,7 +31,7 @@ "server" => "site.xml", ); # The XML file of the database our %initialEditSummaries = ( - "server" => "[[Project:Replace bot|Replace bot]] – " + "server" => "[[Project:Replace bot|Replace bot]] – ", ); # Initial edit summary, when several replaces are done and several edit summaries are integrated, using the replace script our %serverPrefixes = ( "server" => "User:My-bot/Tests/", @@ -42,26 +39,25 @@ # Avilable actions and details about them our %actions = ( - "action" => "move" + "action" => "move", ); # The global actions done our %actionServers = ( - "action" => "server" + "action" => "server", ); # Which server should it be executed on? TODO - allow using multiple servers like that, currently ignored our %titles = ( - "action" => "^שיחת משתמש:[\\d]+\\.[\\d]+\\.[\\d]+\\.[\\d]+\$" + "action" => "^שיחת משתמש:[\\d]+\\.[\\d]+\\.[\\d]+\\.[\\d]+\$", ); # Restrict the actions to these titles our %texts = ( - "action" => "^אנונימי\$" + "action" => "^אנונימי\$", ); # Restrict the actions to the pages our %news = ( - "action" => "שיחת משתמש:אנונימי" + "action" => "שיחת משתמש:אנונימי", ); # New title (move) or text (replace), leave blank otherwise our %reasons = ( - "action" => "מחליף שיחות אנונימיות" + "action" => "מחליף שיחות אנונימיות", ); # Summary/reason sent about the action our %minimumTimes = ( - "action" => timegm(0, 0, 0, 1, 1 - 1, 1970 - 1900) + "action" => timegm( 0, 0, 0, 1, 1 - 1, 1970 - 1900 ), ); # The minimum diff between the current time and the last edit time -# Return a true value -1; +return 1; Modified: trunk/config/runtime.sample =================================================================== --- trunk/config/runtime.sample 2006-09-16 11:06:13 UTC (rev 18) +++ trunk/config/runtime.sample 2006-09-16 11:45:57 UTC (rev 19) @@ -5,17 +5,14 @@ use warnings; use strict; -# Counters -my ($i, $j, $k, $l, $m); - # Actions to execute our @executedActions = ( - "action" + "action", ); # Actions to execute by the way, if you are already editing the page (replace only) our @bywayActions = ( - "actionB" + "actionB", ); # This variable enables and disables the actual sends to the server. @@ -27,5 +24,4 @@ # advanced tests, before you use 1 for the real changes. our $sendPages = 0; -# Return a true value -1; +return 1; Modified: trunk/includes/functions.pm =================================================================== --- trunk/includes/functions.pm 2006-09-16 11:06:13 UTC (rev 18) +++ trunk/includes/functions.pm 2006-09-16 11:45:57 UTC (rev 19) @@ -12,221 +12,191 @@ use config::runtime; # Counters -my ($i, $j, $k, $l, $m); +my ( $i, $j, $k, $l, $m ); # Get all the pages matching specific restrictions -sub getMatchingPages -{ +sub getMatchingPages { # Get the pages - my $pages = mwdump->new("dumps/".$configure::xmlFiles{$ARGV[0]}); - + my $pages = mwdump->new( "dumps/".$configure::xmlFiles{ $ARGV[0] } ); + # Can use only "first-letter", TODO: enable also "case-sensitive" for Wiktionary - if ($pages->case ne "first-letter") - { + if ( $pages->case ne "first-letter" ) { die "Unable to handle any case setting besides \"first-letter\".\n"; } - + # Get the executed actions my @possibleActions = @configure::executedActions; - + # An array contains all the pages my @matchingPages; - + # Reset counter my $counter = 0; - + # Iterate over all the pages print "Checking all the pages...\n"; - while (my $page = $pages->page) - { + while ( my $page = $pages->page ) { # Temporary array counter my $tempCounter; - + # Get the page possible actions my @pagePossibleActions = @possibleActions; - + # Check if the page title matches to one of the titles my @tempTitleArray; my $title = $page->title; - utf8::encode($title); + utf8::encode( $title ); $tempCounter = 0; - for ($i = 0; ($i <= $#pagePossibleActions); $i++) - { - my $wantedTitle = $configure::titles{$pagePossibleActions[$i]}; - if ($title =~ /$wantedTitle/) - { + for ( $i = 0; $i <= $#pagePossibleActions; $i++ ) { + my $wantedTitle = $configure::titles{ $pagePossibleActions[$i] }; + if ( $title =~ /$wantedTitle/ ) { $tempTitleArray[$tempCounter++] = $pagePossibleActions[$i]; } } - + # Continue if there are no matching pages @pagePossibleActions = @tempTitleArray; - if ($#pagePossibleActions < 0) - { + if ( $#pagePossibleActions < 0 ) { next; } - + # Check if the page text matches to one of the texts my @tempTextArray; my $text = $page->text; - utf8::encode($$text); + utf8::encode( $$text ); $tempCounter = 0; - for ($i = 0; ($i <= $#pagePossibleActions); $i++) - { - my $wantedText = $configure::texts{$pagePossibleActions[$i]}; - if ($$text =~ /$wantedText/) - { + for ( $i = 0; $i <= $#pagePossibleActions; $i++ ) { + my $wantedText = $configure::texts{ $pagePossibleActions[$i] }; + if ($$text =~ /$wantedText/) { $tempTextArray[$tempCounter++] = $pagePossibleActions[$i]; } } - + # Continue if there are no matching pages @pagePossibleActions = @tempTextArray; - if ($#pagePossibleActions < 0) - { + if ( $#pagePossibleActions < 0 ) { next; } - + # All the previous checks were OK - add the page, using the first available action $matchingPages[$counter++] = $title; $matchingPages[$counter++] = $pagePossibleActions[0]; } print "\tDone!\n"; - - @matchingPages; # Return + + return @matchingPages; } # Get page last edit # TODO: Move to getMatchingPages -sub getPageLastEdit -{ +sub getPageLastEdit { # Get parameters - my ($xmlFile, $title) = @_; - utf8::decode($title); - + my ( $xmlFile, $title ) = @_; + utf8::decode( $title ); + # Get the pages - my $pages = Parse::MediaWikiDump::Pages->new($xmlFile); - + my $pages = mwdump->new($xmlFile); + # Can use only "first-letter", which is also the only currently known value, # but there could be more in the future - if ($pages->case ne "first-letter") - { + if ( $pages->case ne "first-letter" ) { die "Unable to handle any case setting besides \"first-letter\".\n"; } - + # Searching for the page, and getting its timestamp my $lastEditTimestamp = "0"; - while (($lastEditTimestamp eq "0") && (my $page = $pages->page)) - { - if ($title eq $page->title) - { + while ( $lastEditTimestamp eq "0" && ( my $page = $pages->page ) ) { + if ( $title eq $page->title ) { $lastEditTimestamp = $page->timestamp; } } - + # Check the time by RegExp $lastEditTimestamp =~ /([\d]+)-([\d]+)-([\d]+)T([\d]+):([\d]+):([\d]+)Z/; - - timegm($6, $5, $4, $3, $2 - 1, $1 - 1900); # Return + + return timegm( $6, $5, $4, $3, $2 - 1, $1 - 1900 ); } # Log in -sub login -{ +sub login { # Is sysop finally required? my $sysop = 0; - + # Go through the array, and check if sysop permission is needed. - for ($i = 0; ($i <= $#configure::executedActions); $i++) - { - my $action = $configure::actions{$configure::executedActions[$i]}; - if ($action eq "replace") - { + for ( $i = 0; $i <= $#configure::executedActions; $i++ ) { + my $action = $configure::actions{ $configure::executedActions[$i] }; + if ( $action eq "replace" ) { # Continue - } - elsif ($action eq "refresh") - { + } elsif ($action eq "refresh") { # Continue - } - elsif ($action eq "delete") - { + } elsif ($action eq "delete") { $sysop = 1; - } - elsif ($action eq "move") - { + } elsif ($action eq "move") { # Continue } } - + # Get user name and password - my ($username, $password); - if ($sysop == 1) - { - $username = $configure::sysopUserNames{$ARGV[0]}; - $password = $configure::sysopPasswords{$ARGV[0]}; + my ( $username, $password ); + if ( $sysop == 1 ) { + $username = $configure::sysopUserNames{ $ARGV[0] }; + $password = $configure::sysopPasswords{ $ARGV[0] }; + } else { + $username = $configure::userNames{ $ARGV[0] }; + $password = $configure::passwords{ $ARGV[0] }; } - else - { - $username = $configure::userNames{$ARGV[0]}; - $password = $configure::passwords{$ARGV[0]}; - } - + # Log in print "Logging in...\n"; - http::postPage("Special:Userlogin", "submitlogin", + http::postPage( "Special:Userlogin", "submitlogin", [ wpName => $username, wpPassword => $password, - wpRemember => 0 + wpRemember => 0, ], - , "type=login"); + , "type=login" ); print "\tDone!\n"; } # Get the contents of a page -sub getPageContents -{ +sub getPageContents { # Get parameters - my ($title) = @_; - - http::getPage($title, "raw"); # Return + my ( $title ) = @_; + + return http::getPage( $title, "raw" ); } # Edit page -sub editPage -{ +sub editPage { # Get parameters - my ($title, $text, $editSummary) = @_; - + my ( $title, $text, $editSummary ) = @_; + print "Editing page $title...\n"; - - if ($configure::sendPages == 2) - { - my $serverPrefix = $configure::serverPrefixes{$ARGV[0]}; + + if ( $configure::sendPages == 2 ) { + my $serverPrefix = $configure::serverPrefixes{ $ARGV[0] }; $title = $serverPrefix.$title; } - + # Get the edit page contents - my $editPage = http::getPage($title, "edit"); - + my $editPage = http::getPage( $title, "edit" ); + # Get the start time $editPage =~ /<input type='hidden' value="([0-9]{14})" name="wpStarttime" \/>/; my $startTime = $1; - + # Get the edit time $editPage =~ /<input type='hidden' value="([0-9]{14})" name="wpEdittime" \/>/; my $editTime = $1; - + # Get edit token $editPage =~ /<input type='hidden' value="([0-9a-f]{32})" name="wpEditToken" \/>/; my $editToken = $1; - + # Send page - if (defined($editToken) && $editToken =~ /[0-9a-f]{32}/) - { - if ($configure::sendPages == 1 || $configure::sendPages == 2) - { - http::postPage($title, "submit", + if ( defined( $editToken ) && $editToken =~ /[0-9a-f]{32}/ ) { + if ( $configure::sendPages == 1 || $configure::sendPages == 2 ) { + http::postPage( $title, "submit", [ wpSection => "", wpStarttime => $startTime, @@ -234,102 +204,88 @@ wpEditToken => $editToken, wpSummary => $editSummary, wpTextbox1 => $text, - wpMinoredit => 1 + wpMinoredit => 1, ], ); } print "\tDone!\n"; - } - else - { + } else { print "Error!\nThis may be a protected page you don't have permission to edit, or it has deleted since the dump file you use created.\n"; } } # Replace regular expressions in the page -sub replaceInPage -{ +sub replaceInPage { # Get parameters my ($title) = @_; - - if ($configure::sendPages == 1 || $configure::sendPages == 2) - { + + if ( $configure::sendPages == 1 || $configure::sendPages == 2 ) { # Get the edited page contents - my $editedPage = getPageContents($title); - + my $editedPage = getPageContents( $title ); + # Set initial edit summary - my $editSummary = $configure::initialEditSummaries{$ARGV[0]}; - + my $editSummary = $configure::initialEditSummaries{ $ARGV[0] }; + # Groups array - my @actions = (@configure::executedActions, @configure::bywayActions); - + my @actions = ( @configure::executedActions, @configure::bywayActions ); + # Replaced something at all? Flag to check my $replaced = 0; - + # Replace regular expressions - for ($i = 0; ($i <= $#actions); $i++) - { + for ( $i = 0; $i <= $#actions; $i++ ) { my $action = $actions[$i]; - my $search = $configure::texts{$action}; - if ($editedPage =~ /$search/) - { - my $replace = $configure::news{$action}; + my $search = $configure::texts{ $action }; + if ( $editedPage =~ /$search/ ) { + my $replace = $configure::news{ $action }; $editedPage =~ s/$search/$replace/g; - if ($replaced == 1) - { - $editSummary = $editSummary.", "; + if ( $replaced == 1 ) { + $editSummary = "$editSummary, "; } - $editSummary = $editSummary.$configure::reasons{$action}; + $editSummary = $editSummary.$configure::reasons{ $action }; $replaced = 1; } } - + # Edit page only of replaced something - if ($replaced == 1) - { - editPage($title, $editedPage, $editSummary); + if ( $replaced == 1 ) { + editPage( $title, $editedPage, $editSummary ); } - } - else - { + } else { print "Replaced in page $title.\n"; } } # Refresh page -sub refreshPage -{ +sub refreshPage { # Get parameters - my ($server, $title) = @_; - - editPage($title, getPageContents($title), "Refreshing page"); + my ( $server, $title ) = @_; + + editPage( $title, getPageContents( $title ), "Refreshing page" ); } # Move page -sub movePage -{ +sub movePage { # Get parameters - my ($server, $title, $newTitle, $moveReason) = @_; - + my ( $server, $title, $newTitle, $moveReason ) = @_; + print "Moving page $title to $newTitle...\n"; - + # Get the delete page contents - my $movePage = http::getPage("Special:Movepage/$title"); - + my $movePage = http::getPage( "Special:Movepage/$title" ); + # Get the edit token - if ($movePage =~ /<input type='hidden' name='wpEditToken' value="([0-9,a-f]{32})" \/>/) - { + if ($movePage =~ /<input type='hidden' name='wpEditToken' value="([0-9,a-f]{32})" \/>/) { my $editToken = $1; - + # Send page - if ($configure::sendPages == 1) - { - http::postPage("Special:Movepage", "submit", + if ( $configure::sendPages == 1 ) { + http::postPage( "Special:Movepage", "submit", [ wpOldTitle => $title, wpNewTitle => $newTitle, wpReason => $moveReason, - wpEditToken => $editToken + wpEditToken => $editToken, ], ); } @@ -342,38 +298,32 @@ } # Delete page -sub deletePage -{ +sub deletePage { # Get parameters - my ($server, $title, $reason) = @_; - + my ( $server, $title, $reason ) = @_; + print "Deleting page $title...\n"; - + # Get the delete page contents - my $deletePage = http::getPage($title, "delete"); - + my $deletePage = http::getPage( $title, "delete" ); + # Get the edit token - if ($deletePage =~ /<input type='hidden' name='wpEditToken' value="([0-9,a-f]{32})" \/>/) - { + if ($deletePage =~ /<input type='hidden' name='wpEditToken' value="([0-9,a-f]{32})" \/>/) { my $editToken = $1; - + # Send page - if ($configure::sendPages == 1) - { - http::postPage($title, "delete", + if ( $configure::sendPages == 1 ) { + http::postPage( $title, "delete", [ wpReason => $reason, - wpEditToken => $editToken + wpEditToken => $editToken, ], ); } print "\tDone!\n"; - } - else - { + } else { print "Error!\nYou may not have the permission to delete pages, or the page is already deleted.\n"; } } -# Return a true value -1; +return 1; Modified: trunk/includes/http.pm =================================================================== --- trunk/includes/http.pm 2006-09-16 11:06:13 UTC (rev 18) +++ trunk/includes/http.pm 2006-09-16 11:45:57 UTC (rev 19) @@ -9,72 +9,62 @@ use LWP; use config::configure; -# Counters -my ($i, $j, $k, $l, $m); - # The browser we use to surf; enable cookies, and use a specific user agent -my $browser = LWP::UserAgent->new; -$browser->cookie_jar({}); -$browser->agent("Mozilla/5.0 (compatible; Perl MediaWiki Robot)"); +my $browser = LWP::UserAgent->new(); +$browser->cookie_jar( {} ); +$browser->agent( "Mozilla/5.0 (compatible; Perl MediaWiki Robot)" ); # Build the URL of a wiki page -sub buildPageURL -{ +sub buildPageURL { # Get parameters - my ($title, $action, $get) = @_; - + my ( $title, $action, $get ) = @_; + # Initial URL: server, script path and title - my $server = $configure::servers{$ARGV[0]}; - my $scriptPath = $configure::scriptPaths{$ARGV[0]}; + my $server = $configure::servers{ $ARGV[0] }; + my $scriptPath = $configure::scriptPaths{ $ARGV[0] }; my $url = "$server$scriptPath?title=$title"; - + # Action - if (defined($action)) - { + if ( defined( $action ) ) { $url = "$url&action=$action"; } - + # Parameters - if (defined($get)) - { + if ( defined( $get ) ) { $url = "$url&$get"; } - - $url; # Return + + return $url; } # Get a wiki page, try again and again if error -sub getPage -{ +sub getPage { # Get parameters - my ($title, $action, $get) = @_; - - my $url = buildPageURL($title, $action, $get); - + my ( $title, $action, $get ) = @_; + + my $url = buildPageURL( $title, $action, $get ); + my $result; - do - { - $result = $browser->get($url); - } - while (!$result->is_success && $result->status_line =~ /302[\s\w]+/); - - $result->content; # Return + do { + $result = $browser->get( $url ); + } while ( !$result->is_success && $result->status_line =~ /302[\s\w]+/ ); + + return $result->content; } # Post a wiki page, try again and again if error -sub postPage -{ +sub postPage { # Get parameters my ($title, $action, $post, $get) = @_; - - my $url = buildPageURL($title, $action, $get); - + + my $url = buildPageURL( $title, $action, $get ); + my $result; - do - { - $result = $browser->post($url, $post); - } - while (!$result->is_success && !$result->status_line =~ /302[\s\w]+/); - - $result->content; # Return + do { + $result = $browser->post( $url, $post ); + } while ( !$result->is_success && !$result->status_line =~ /302[\s\w]+/ ); + + return $result->content; } + +return 1; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |