[PerlWikiBot] SF.net SVN: perlwikibot: [17] trunk
Status: Pre-Alpha
Brought to you by:
rotemliss
From: <rot...@us...> - 2006-09-16 10:54:55
|
Revision: 17 http://svn.sourceforge.net/perlwikibot/?rev=17&view=rev Author: rotemliss Date: 2006-09-16 03:54:41 -0700 (Sat, 16 Sep 2006) Log Message: ----------- Use namespaces, and directly access the variables instead of using many functions. Modified Paths: -------------- trunk/bot.pl trunk/config/configure.sample trunk/config/runtime.sample trunk/includes/dump.pm trunk/includes/functions.pm trunk/includes/http.pm Modified: trunk/bot.pl =================================================================== --- trunk/bot.pl 2006-09-01 16:04:29 UTC (rev 16) +++ trunk/bot.pl 2006-09-16 10:54:41 UTC (rev 17) @@ -6,6 +6,8 @@ # Libraries use includes::functions; +use config::configure; +use config::runtime; # Counters my ($i, $j, $k, $l, $m); @@ -17,34 +19,34 @@ } # Show notes about the sending pages configuration -if (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 (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 (connectToServer()) +if ($configure::sendPages == 1 || $configure::sendPages == 2) { - login(); + functions::login(); } # All the matching pages in the XML file -my @pages = getMatchingPages(); +my @pages = functions::getMatchingPages(); # Execute actions of all the pages for ($i = 0; ($i <= $#pages); $i += 2) { # Get title and action my $title = $pages[$i]; - my $action = action($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") { - replaceInPage($title); + functions::replaceInPage($title); } } Modified: trunk/config/configure.sample =================================================================== --- trunk/config/configure.sample 2006-09-01 16:04:29 UTC (rev 16) +++ trunk/config/configure.sample 2006-09-16 10:54:41 UTC (rev 17) @@ -1,3 +1,6 @@ +# Package name +package configure; + # Code style use warnings; use strict; @@ -9,165 +12,56 @@ my ($i, $j, $k, $l, $m); # Available servers and details about them, the bot should work on -my %servers = ( +our %servers = ( "server" => "http://www.server.com", ); # The MediaWiki Server -my %scriptPaths = ( +our %scriptPaths = ( "server" => "/w/index.php", ); # The path to the script index.php (or wiki.phtml in old versions) -my %userNames = ( +our %userNames = ( "server" => "My-bot", ); # The bot user name in every server -my %passwords = ( +our %passwords = ( "server" => "password" ); # The bot password in every server -my %sysopUserNames = ( +our %sysopUserNames = ( "server" => "My-sysop", ); # The sysop bot user name in every server, blank if no -my %sysopPasswords = ( +our %sysopPasswords = ( "server" => "strong_password", ); # The sysop bot password in every server, blank if no -my %xmlFiles = ( +our %xmlFiles = ( "server" => "site.xml", ); # The XML file of the database -my %initialEditSummaries = ( +our %initialEditSummaries = ( "server" => "[[Project:Replace bot|Replace bot]] – " ); # Initial edit summary, when several replaces are done and several edit summaries are integrated, using the replace script -my %serverPrefixes = ( +our %serverPrefixes = ( "server" => "User:My-bot/Tests/", ); # Prefixes for the saved pages for tests; won't be used when really running the script # Avilable actions and details about them -my %actions = ( +our %actions = ( "action" => "move" ); # The global actions done -my %actionServers = ( +our %actionServers = ( "action" => "server" ); # Which server should it be executed on? TODO - allow using multiple servers like that, currently ignored -my %titles = ( +our %titles = ( "action" => "^שיחת משתמש:[\\d]+\\.[\\d]+\\.[\\d]+\\.[\\d]+\$" ); # Restrict the actions to these titles -my %texts = ( +our %texts = ( "action" => "^אנונימי\$" ); # Restrict the actions to the pages -my %news = ( +our %news = ( "action" => "שיחת משתמש:אנונימי" ); # New title (move) or text (replace), leave blank otherwise -my %reasons = ( +our %reasons = ( "action" => "מחליף שיחות אנונימיות" ); # Summary/reason sent about the action -my %minimumTimes = ( +our %minimumTimes = ( "action" => timegm(0, 0, 0, 1, 1 - 1, 1970 - 1900) ); # The minimum diff between the current time and the last edit time -sub server -{ - $servers{$ARGV[0]}; # Return -} - -sub scriptPath -{ - $scriptPaths{$ARGV[0]}; # Return -} - -sub userName -{ - # Return the appropriate user name, if the sysop is needed or not, - # using the parameter - if ($_[0] == 1) - { - $sysopUserNames{$ARGV[0]}; # Return - } - else - { - $userNames{$ARGV[0]}; # Return - } -} - -sub password -{ - # Return the appropriate user name, if the sysop is needed or not, - # using the parameter - if ($_[0] == 1) - { - $sysopPasswords{$ARGV[0]}; # Return - } - else - { - $passwords{$ARGV[0]}; # Return - } -} - -sub xmlFile -{ - $xmlFiles{$ARGV[0]}; # Return -} - -sub initialEditSummary -{ - $initialEditSummaries{$ARGV[0]}; # Return -} - -sub serverPrefix -{ - $serverPrefixes{$ARGV[0]}; # Return -} - -sub action -{ - # Get parameter - my $actionName = $_[0]; - - $actions{$actionName}; # Return -} - -sub actionServer -{ - # Get parameter - my $actionName = $_[0]; - - $actionServers{$actionName}; # Return -} - -sub title -{ - # Get parameter - my $actionName = $_[0]; - - $titles{$actionName}; # Return -} - -sub text -{ - # Get parameter - my $actionName = $_[0]; - - $texts{$actionName}; # Return -} - -sub new -{ - # Get parameter - my $actionName = $_[0]; - - $news{$actionName}; # Return -} - -sub reason -{ - # Get parameter - my $actionName = $_[0]; - - $reasons{$actionName}; # Return -} - -sub minimumTime -{ - # Get parameter - my $actionName = $_[0]; - - $minimumTimes{$actionName}; # Return -} - # Return a true value 1; Modified: trunk/config/runtime.sample =================================================================== --- trunk/config/runtime.sample 2006-09-01 16:04:29 UTC (rev 16) +++ trunk/config/runtime.sample 2006-09-16 10:54:41 UTC (rev 17) @@ -1,3 +1,6 @@ +# Package name +package configure; + # Code style use warnings; use strict; @@ -6,12 +9,12 @@ my ($i, $j, $k, $l, $m); # Actions to execute -my @executedActions = ( +our @executedActions = ( "action" ); # Actions to execute by the way, if you are already editing the page (replace only) -my @bywayActions = ( +our @bywayActions = ( "actionB" ); @@ -22,35 +25,7 @@ # * 2 - send to the server with prefix when editing (if not, don't send at all). # It's strongly recommended that you use 0 before making tests, and 2 before more # advanced tests, before you use 1 for the real changes. -my $sendPages = 0; +our $sendPages = 0; -sub executedActions -{ - @executedActions; # Return -} - -sub bywayActions -{ - @bywayActions; # Return -} - -sub sendPages -{ - $sendPages; # Return -} - -# Check if we have to connect to server -sub connectToServer -{ - if ($sendPages == 1 || $sendPages == 2) - { - 1; # Return - } - else - { - 0; # Return - } -} - # Return a true value 1; Modified: trunk/includes/dump.pm =================================================================== --- trunk/includes/dump.pm 2006-09-01 16:04:29 UTC (rev 16) +++ trunk/includes/dump.pm 2006-09-16 10:54:41 UTC (rev 17) @@ -4,7 +4,7 @@ #and shifting values for subroutine input should be avoided in #any subroutines that get called often, like the handlers -package Parse::MediaWikiDump::Pages; +package mwdump; #This parser works by placing all of the start, text, and end events into #a buffer as they come out of XML::Parser. On each call to page() the function Modified: trunk/includes/functions.pm =================================================================== --- trunk/includes/functions.pm 2006-09-01 16:04:29 UTC (rev 16) +++ trunk/includes/functions.pm 2006-09-16 10:54:41 UTC (rev 17) @@ -1,3 +1,6 @@ +# Package name +package functions; + # Code style use warnings; use strict; @@ -15,7 +18,7 @@ sub getMatchingPages { # Get the pages - my $pages = Parse::MediaWikiDump::Pages->new(xmlFile()); + 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") @@ -24,7 +27,7 @@ } # Get the executed actions - my @possibleActions = executedActions(); + my @possibleActions = @configure::executedActions; # An array contains all the pages my @matchingPages; @@ -49,7 +52,7 @@ $tempCounter = 0; for ($i = 0; ($i <= $#pagePossibleActions); $i++) { - my $wantedTitle = title($pagePossibleActions[$i]); + my $wantedTitle = $configure::titles{$pagePossibleActions[$i]}; if ($title =~ /$wantedTitle/) { $tempTitleArray[$tempCounter++] = $pagePossibleActions[$i]; @@ -70,7 +73,7 @@ $tempCounter = 0; for ($i = 0; ($i <= $#pagePossibleActions); $i++) { - my $wantedText = text($pagePossibleActions[$i]); + my $wantedText = $configure::texts{$pagePossibleActions[$i]}; if ($$text =~ /$wantedText/) { $tempTextArray[$tempCounter++] = $pagePossibleActions[$i]; @@ -131,16 +134,13 @@ # Log in sub login { - # Get all the executed actions - my @executedActions = executedActions(); - - # Sysop finally required? + # Is sysop finally required? my $sysop = 0; # Go through the array, and check if sysop permission is needed. - for ($i = 0; ($i <= $#executedActions); $i++) + for ($i = 0; ($i <= $#configure::executedActions); $i++) { - my $action = action($executedActions[$i]); + my $action = $configure::actions{$configure::executedActions[$i]}; if ($action eq "replace") { # Continue @@ -159,12 +159,25 @@ } } + # Get user name and password + 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]}; + } + # Log in print "Logging in...\n"; - postPage("Special:Userlogin", "submitlogin", + http::postPage("Special:Userlogin", "submitlogin", [ - wpName => userName($sysop), - wpPassword => password($sysop), + wpName => $username, + wpPassword => $password, wpRemember => 0 ], , "type=login"); @@ -177,7 +190,7 @@ # Get parameters my $title = $_[0]; - getPage($title, "raw"); # Return + http::getPage($title, "raw"); # Return } # Edit page @@ -190,13 +203,14 @@ print "Editing page $title...\n"; - if (sendPages() == 2) + if ($configure::sendPages == 2) { - $title = serverPrefix().$title; + my $serverPrefix = $configure::serverPrefixes{$ARGV[0]}; + $title = $serverPrefix.$title; } # Get the edit page contents - my $editPage = getPage($title, "edit"); + my $editPage = http::getPage($title, "edit"); # Get the start time $editPage =~ /<input type='hidden' value="([0-9]{14})" name="wpStarttime" \/>/; @@ -211,11 +225,11 @@ my $editToken = $1; # Send page - if ((defined($editToken)) && ($editToken =~ /[0-9a-f]{32}/)) + if (defined($editToken) && $editToken =~ /[0-9a-f]{32}/) { - if ((sendPages() == 1) || (sendPages() == 2)) + if ($configure::sendPages == 1 || $configure::sendPages == 2) { - postPage($title, "submit", + http::postPage($title, "submit", [ wpSection => "", wpStarttime => $startTime, @@ -241,16 +255,16 @@ # Get parameters my $title = $_[0]; - if (connectToServer()) + if ($configure::sendPages == 1 || $configure::sendPages == 2) { # Get the edited page contents my $editedPage = getPageContents($title); # Set initial edit summary - my $editSummary = initialEditSummary(); + my $editSummary = $configure::initialEditSummaries{$ARGV[0]}; # Groups array - my @actions = (executedActions(), bywayActions()); + my @actions = (@configure::executedActions, @configure::bywayActions); # Replaced something at all? Flag to check my $replaced = 0; @@ -259,16 +273,16 @@ for ($i = 0; ($i <= $#actions); $i++) { my $action = $actions[$i]; - my $search = text($action); + my $search = $configure::texts{$action}; if ($editedPage =~ /$search/) { - my $replace = new($action); + my $replace = $configure::news{$action}; $editedPage =~ s/$search/$replace/g; if ($replaced == 1) { $editSummary = $editSummary.", "; } - $editSummary = $editSummary.reason($action); + $editSummary = $editSummary.$configure::reasons{$action}; $replaced = 1; } } @@ -307,7 +321,7 @@ print "Moving page $title to $newTitle...\n"; # Get the delete page contents - my $movePage = 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})" \/>/) @@ -315,9 +329,9 @@ my $editToken = $1; # Send page - if (sendPages() == 1) + if ($configure::sendPages == 1) { - postPage("Special:Movepage", "submit", + http::postPage("Special:Movepage", "submit", [ wpOldTitle => $title, wpNewTitle => $newTitle, @@ -345,7 +359,7 @@ print "Deleting page $title...\n"; # Get the delete page contents - my $deletePage = 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})" \/>/) @@ -353,9 +367,9 @@ my $editToken = $1; # Send page - if (sendPages() == 1) + if ($configure::sendPages == 1) { - postPage($title, "delete", + http::postPage($title, "delete", [ wpReason => $deleteReason, wpEditToken => $editToken Modified: trunk/includes/http.pm =================================================================== --- trunk/includes/http.pm 2006-09-01 16:04:29 UTC (rev 16) +++ trunk/includes/http.pm 2006-09-16 10:54:41 UTC (rev 17) @@ -1,3 +1,6 @@ +# Package name +package http; + # Code style use warnings; use strict; @@ -4,6 +7,7 @@ # Libraries use LWP; +use config::configure; # Counters my ($i, $j, $k, $l, $m); @@ -22,7 +26,9 @@ my $get = $_[2]; # Initial URL: server, script path and title - my $url = server().scriptPath()."?title=$title"; + my $server = $configure::servers{$ARGV[0]}; + my $scriptPath = $configure::scriptPaths{$ARGV[0]}; + my $url = "$server$scriptPath?title=$title"; # Action if (defined($action)) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |