User:Atrox~mediawikiwiki/Phpwiki2Mediawiki/bot.php

# Copyright (c) 2010 Artjom Vassiljev <artjom@max.ee>, MAX 123 AS, Estonia
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#    This product includes software developed by Artjom Vassiljev.
# 4. Neither the name of the author nor the names of any co-contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY Artjom Vassiljev AND "MAX 123" ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
# THE POSSIBILITY OF SUCH DAMAGE.

<?

/*
Changelog:
(26.Jul.2010), Silver
* replace in pages' names: ' ' -> '_'
(22.Jul.2010), Silver
* rename pages: '^Kategooria...' -> 'Category:...'
(20.Jul.2010), Silver
* replaced split() with explode() as suggested by PHP 5.3
(07.Jun.2010), Silver
* use pages beginning with 'Template' as templates
(03.Jun.2010), Silver
* don't urlencode() contents of pages
* don't convert parent pages into categories
*/

include("SxWiki.php");

# TODO: add page using author's original ID

# CONFIG
# url path to api.php
$url = 'http://mediawiki.mydomain/';
# user to add pages
$lu = "WikiSysop";
$lp = "WikiSysopPswd";
# tree file produced by converter
$tree = "/home/artjom/converter/tree.txt";
# folder with converted files
$home = "/home/artjom/converter/converted";
# log file with errors
$log = "/home/artjom/converter/log.txt";

# open log file
$l = fopen($log, "a");

if (!file_exists($tree) || !file_exists($home)) {
	fwrite($l, "Check whether the $tree or $home is accessible");
	fclose($l);
	exit(0);
}


# login to wiki
$sxLgTA = sxLogin($lu, $lp);
$sxLgID = $sxLgTA[uid];
$sxLgT = $sxLgTA[token];

# set request timeout
$maxlag = "5";
# set edits per minute
$epm = setEPM(10000);


# read the tree
$fd = fopen($tree, "r");
if ($fd) {
	print "\nImporting pages:\n\n";
	while(!feof($fd)) {
		$buf = fgets($fd, 4096);
		# get file name, page name, author's id, summary
		list ($f, $p, $a, $s) = explode(";", $buf, 4);
		// convert parent pages into categories
// 			if (!preg_match('/\//', $p)) {
// 				$p = "Category:$p";
// 			}
		// use pages beginning with 'Template' as templates
		if (preg_match("/^Template/", $p))
			$p = preg_replace("/^Template/", "Template:", $p);
		if (preg_match("/^Category/", $p))
			$p = preg_replace("/^Category/", "Category:", $p);
		if (preg_match("/ /", $p))
			$p = preg_replace("/ /", "_", $p);
		// rename: HomePage -> MainPage -- this does not work that way :P
// 			if ($p == "HomePage")
// 				$p = "MainPage";
		# open file with wiki text
		print "$f::$p\n";
		$w = fopen("$home/$f", "r");
		$le = filesize("$home/$f");
		$contents = fread($w, $le);
// 			$contents = urlencode($contents);
		fclose($w);

		# ready to put the page to wiki
		$b = sxPutPage($p, $s, $contents, 0, 0);

		if (!$b) {
			fwrite($l, "could not add page $p to wiki\n");
		}
	}
}

fclose($fd);
fclose($l);

print "\nDone\n";

?>