User:QuietBot

From TheKolWiki
Revision as of 22:47, 3 November 2006 by QuietBot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
<?

$wikiroot = "http://kol.coldfront.net/thekolwiki";
$username = "QuietBot";
$password = "(hidden)";

if ($argc != 2)
{
	echo "Syntax: [php] bulkinsert.php <filename>\n";
	return 1;
}
if (!is_file($argv[1]))
{
	echo "Unable to open file $argv[1]!\n";
	return 2;
}

require("Snoopy.class.php");
$snoopy = new Snoopy;

$login_url = $wikiroot . "/index.php?title=Special:Userlogin&action=submitlogin";
$login_vars['wpName'] = $username;
$login_vars['wpPassword'] = $password;
$login_vars['wpRemember'] = "0";

$snoopy->submit($login_url,$login_vars);

$pages = split("--ENDPAGE--", file_get_contents($argv[1]));

$total = count($pages);

for ($i = 0; $i < $total; $i++)
{
	list($title, $body) = split("--ENDTITLE--", $pages[$i]);

	$title = trim($title);
	if (!$title)
		continue;

	// encode page title for proper wiki URL (spaces to underscores, and urlencode everything else)
	$safetitle = urlencode(str_replace(' ', '_', $title));

	echo "Creating page: $title\n";

	if (!$snoopy->submit($wikiroot ."/index.php?title=". $safetitle . "&action=edit", $login_vars))
	{
		echo "Failed to load page info! Skipping to next page...\n";
		continue;
	}
	$editpage = $snoopy->results;

	preg_match('/.*value="(.*?)".*name="wpEditToken"/',$editpage, $matches);
	$token = $matches[1];

	$submit_vars['wpTextbox1'] = $body;
	$submit_vars['wpSummary'] = "Bulk page import ($i of $pages)";
	$submit_vars['wpSection'] = "";
	$submit_vars['wpEdittime'] = "";
	$submit_vars['wpMinoredit'] = "1";
	$submit_vars['wpSave'] = "Save page";
	$submit_vars['wpEditToken'] = $token;

	if (!$snoopy->submit($wikiroot . "/index.php?title=" . $safetitle . "&action=submit", $submit_vars))
		echo "Failed to submit page!\n";
	else
		echo "Record $i of $pages imported.\n";
}
?>