<?php
# log in credentials should be included here
require_once('botodologin');
# Snoopy should be set so the host name is en.wikipedia.org.
require_once('Snoopy.class.php');
$bot = new Snoopy();
define("debugon", $_GET["debug"]);
define("editinterval", 60);
define("wikiroot", "http://en.wikipedia.org/w/index.php?");
function debug($b="Ping", $t="", $a="") {
if (debugon){
print "<br><b>$b</b> $t <a href='$a'>$a</a><br>";
}
}
function content($title) {
// Returns the raw wikitext, unless there's a bot prohibition, in which case it returns false.
global $bot;
$bot->fetchtext(wikiroot . "title=" . $title . "&action=raw");
$rawtext = $bot->results;
if (!preg_match("/{{nobots}}|{{bots|deny=[^}]*(" . USERNAME . "|all)[^}]*}}|{{bots|allow=none}}/", $rawtext)) return $rawtext;
return false;
}
function countMainLinks($title) {
// Counts the links to the mainpage
global $bot;
if(preg_match("/\w*:(.*)/", $title, $title)) $title = $title[1]; //Gets {{PAGENAME}}
$url = wikiroot . "title=Special%3AWhatlinkshere&target=$title&limit=500&namespace=$namespace&from=$offset";
$bot->fetchtext($url);
debug("Just fetched:", '"What links here", URL:', $url);
$response = explode("\n", $bot->results);
return count($response) - 68;
}
function whatTranscludes($title, $namespace = false /* 0 = main */, $limit = 10000, $offset = 0) {
// Returns an array of links to a specified page.
global $bot;
$url = wikiroot . "title=Special%3AWhatlinkshere&target=Template:" . urlencode($title) . "&limit=" . $limit . "&namespace=$namespace&from=$offset";
$bot->fetchtext($url);
debug("Just fetched:", '"What links here", URL:', $url);
$response = $bot->results;
if (preg_match_all("/(500\)|\n)([^\|]*)( \(transclusion\))? +\(links\)/U", $response, $links)) {
debug("Number of results:", count($links[2]));
return $links[2];
}
return false;
}
function nextTransclusion($title, $namespace = false /* 0 = main */) {
// Returns an array of links to a specified page.
global $bot;
global $offset;
$url = wikiroot . "title=Special%3AWhatlinkshere&target=Template:" . urlencode($title) . "&limit=1&namespace=$namespace&from=$offset";
$bot->fetch($url);
$page = $bot->results;
preg_match("/from=(\d*)&back=.*>next<\/a>/U", $page, $nextoffset);
preg_match("|/wiki/[^\$]*/wiki/([^\"]*)\"|Us", $page, $articlename);
debug("Just fetched:", '"What links here", Offset was ' . $nextoffset[1] . ', URL:', $url);
$offset = $nextoffset[1];
return $articlename[1];
return false;
}
function logIn($username, $password) {
global $bot;
$loginUrl = wikiroot . "title=Special:Userlogin&action=submitlogin&type=login";
$submit_vars["wpName"] = $username;
$submit_vars["wpPassword"] = $password;
$submit_vars["wpRemember"] = 1;
$submit_vars["wpLoginattempt"] = "Log+in";
$bot->submit($loginUrl, $submit_vars);
debug("Logging in:", "as " . $username . " at", $loginUrl);
if (ereg("You have successfully signed in to Wikipedia", $bot->results)) return true;
debug("Login failed!"); return false;
}
function write($page, $data, $editsummary = "Bot edit") {
global $bot;
//Load edit page so we can scrape starttimes
$editUrl = wikiroot . "title=$page&action=edit";
$bot->fetchform($editUrl);
$form = $bot->results;
preg_match_all("~name=\"(.*)\"~U", $form, $name);
preg_match_all("~value=\"(.*)\"~U", $form, $value);
for ($i= 15; $i>=0; $i--) $value[1][$i] = $value[1][$i-1]; //Aligns name and value; textarea and form name combine to knock them adrift.
//Save the variable set on the edit page
$page_vars = array_combine($name[1], $value[1]);
//Set our post vars to the values of the inputs:
$submit_vars["wpEdittime"] = $page_vars["wpEdittime"];
$submit_vars["wpStarttime"] = $page_vars["wpStarttime"];
$submit_vars["wpEditToken"] = $page_vars["wpEditToken"];
// The less glamorous post vars also need setting:
$submit_vars["wpScrollTop"] = "0";
$submit_vars["wpSection"] = "";
$submit_vars["wpTextbox1"] = $data;
$submit_vars["wpSummary"] = $editsummary;
$submit_vars["wpMinoredit"] = 1;
$submit_vars["wpWatchthis"] = 0;
// Set this var to determine the action - Save page!
$submit_vars["wpSave"] = "Save+page";
$submitUrl = wikiroot . "title=$page&action=submit";
debug("Attempting to edit:", $page, $submitUrl);
debug("with variables:", "Starttime: " . $submit_vars["wpStarttime"] . " - Edit time:" . $submit_vars["wpEdittime"] . " - Edit token: " . $sumbit_vars["wpEditToken"]);
$bot->submit($submitUrl, $submit_vars);
debug("Edit made", "to $page");
}
print "<html><head><title>Botodo</title></head><body>";
debug("Welcome to BoToDo!", "Debug mode is <b>on</b>");
logIn(USERNAME, PASSWORD);
for ($i = 1; $i<10; $i++) {
$starttime = time();
$page = nextTransclusion("Todo", false);
$nolinks = countMainLinks($page);
print "Found <b>$nolinks</b> links regarding to do box at <a href='" . wikiroot . "title=$page'>$page</a>;\n";
if ($nolinks < 3) $p = 9;
else if ($nolinks < 6) $p=8;
else if ($nolinks < 11) $p=7;
else if ($nolinks < 21) $p=6;
else if ($nolinks < 51) $p=5;
else if ($nolinks < 101) $p=4;
else if ($nolinks < 251) $p=3;
else if ($nolinks < 501) $p=2;
else $p=1;
print "Assigning priority <b>$p</b>...\n\n";
$bot->fetchtext(wikiroot . "title=$page&action=raw");
$pagecode = $bot->results;
write($page, preg_replace("~{{todo(\|small=[^\|\}]*)?(\|list=[^\|\}]*)?(\|\d)?}}~U", "{{todo$1$2|$p}}", $pagecode), "[[Category:To do, by priority|Categorising]] To Do box as priority $p");
$endtime = time();
$timetaken = $endtime - $starttime;
print " <em>Done</em> in ${timetaken}s.<br>\n\n";
sleep(editinterval-$timetaken);
print "<br>Catching my breath for " . editinterval-$timetaken." seconds...<br><br>";
}