User:QuietBot/sample

From TheKolWiki
Jump to: navigation, search

This sample script takes a list of pages from a specified text file and renames them, one at a time, from "[pagename]/data" to "Data:[pagename]" (as was originally done on this wiki on January 6, 2007 when the Data namespace was introduced).

<?
require('../lib/wikibot.inc.php');

$bot = new wikibot('http://kol.coldfront.net/thekolwiki', 'username', 'password', FALSE);

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

$result = $bot->login();
if ($result)
	die("Login failed - error $result");
else	echo "Login successful.\n";

$pages = file($argv[1]);

$total = count($pages);
echo "Beginning bulk rename of $total pages...\n";

for ($i = 0; $i < $total; $i++)
{
	$cur = pagenum($i, $total);

	echo "$cur/$total - processing... ";

	$oldtitle = trim($pages[$i]);
	$newtitle = "Data:".str_replace('/data', '', $oldtitle);
	if (!$oldtitle)
	{
		echo "no title, skipping.\n";
		continue;
	}
	$data = $bot->loadPage($oldtitle);
	echo "$oldtitle -> $newtitle - ";
	if (!is_array($data))
	{
		echo "error loading page ($data), skipping.\n";
		continue;
	}
	$result = $bot->movePage($oldtitle, $newtitle, "Moving to data namespace ($cur/$total)");

	if ($result)
		echo "failed ($result)!\n";
	else	echo "done!\n";

	$bot->checkAbort();
}
echo "Bulk rename completed.\n";

$result = $bot->logout();
if ($result)
	die("Logout failed - error $result\n");
else	echo "Logout successful.\n";
?>