User:Atrox~mediawikiwiki/Phpwiki2Mediawiki/SxWiki.php

From mediawiki.org
# small fix to handle UTF8 properly (c) 2009 Artjom Vassiljev/Max123

<?php
 
function setEPM($epm) {
        $epm = 60 / $epm;
        return $epm;
}
 
function sxLogin($username, $password) {
        global $url;
        global $proxyaddr;
        if (!isset($url)) { die("\r\nConfiguration variables not set\r\n"); }
        $request = $url . 'api.php?action=login&lgname=' . $username . '&lgpassword=' . $password . '&format=php';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $request);
        if ($proxyaddr != "") { curl_setopt($ch, CURLOPT_PROXY, $proxyaddr); }
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_COOKIEJAR, "cookiejar.txt");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "wpName=".$username."&wpPassword=".$password."&wpLoginattempt=true");
        $sxLgInput = curl_exec($ch);
        curl_close($ch);
        $sxLgI = unserialize($sxLgInput);
        $result = $sxLgI['login']['result'];
        if ($result != "Success") {
                $failed = "Login failed: $result\r\n";
                die($failed);
        } else {
                echo "\r\nLogged in as $username \r\n";
        }
        $sxLgIA = array("token" => $sxLgI['login']['lgtoken'], "uid" => $sxLgI['login']['lguserid']);
        return($sxLgIA);
}

function sxGetUrl($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookiejar.txt');
        curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookiejar.txt');
        curl_setopt($ch, CURLOPT_URL, $url);
        $response = curl_exec($ch);
        return $response;
} 

function sxGetPage($article) {
        global $url;
        global $proxyaddr;
        if (!isset($url)) { die("\r\nConfiguration variables not set\r\n"); }
        $article = urlencode($article);
        $request = $url . 'api.php?action=query&prop=revisions&titles=' . $article . '&rvprop=content&format=php';
        $sxGetArticle = file_get_contents($request);
        $sxGetA = unserialize($sxGetArticle);
        $sxGetAID = $sxGetA;
        $sxGetAID = array_shift($sxGetAID);
        $sxGetAID = array_shift($sxGetAID);
        $sxGetAID = array_shift($sxGetAID);
        $sxGetAID = array_shift($sxGetAID);
        $sxAText = $sxGetA['query']['pages'][$sxGetAID]['revisions'][0]["*"];
        return($sxAText);
}
 
function sxPutPage($article, $editsum, $newtext, $minor, $botedit) {
        global $maxlag;
        global $url;
        global $epm;
        global $proxyaddr;
        if (!isset($epm) || !isset($url) || !isset($maxlag)) { die("\r\nConfiguration variables not set\r\n"); }
        #$article = urlencode($article);
        $postrequest = $url . 'index.php?action=edit&title=' . $article;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookiejar.txt');
        curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookiejar.txt');
        if ($proxyaddr != "") { curl_setopt($ch, CURLOPT_PROXY, $proxyaddr); }
        curl_setopt($ch, CURLOPT_URL, $postrequest);
        $response = curl_exec($ch);
        preg_match('/\<input type\=\\\'hidden\\\' value\=\"(.*)\" name\=\"wpStarttime\" \/\>/i', $response, $starttime);
        preg_match('/\<input type\=\\\'hidden\\\' value\=\"(.*)\" name\=\"wpEdittime\" \/\>/i', $response, $edittime);
        preg_match('/\<input name\=\"wpAutoSummary\" type\=\"hidden\" value\=\"(.*)\" \/\>/i', $response, $autosum);
        preg_match('/\<input type\=\'hidden\' value\=\"(.*)\" name\=\"wpEditToken\" \/\>/i', $response, $token);
	curl_close($ch);
        $postrequest = $url . 'index.php?title=' . $article . '&action=submit&maxlag=' . $maxlag;
	if ($botedit != NULL) { $postrequest = $postrequest . "&bot=0"; }
        $postData['wpScrolltop'] = '';
        $postData['wpSection'] = '';
        $postData['wpEditToken'] = $token[1];
        $postData['wpAutoSummary'] = $autosum[1];
        $postData['wpSummary'] = $editsum;
        $postData['wpTextbox1'] = utf8_encode($newtext);
        $postData['wpSave'] = "Save page";
        $postData['wpStarttime'] = $starttime[1];
        $postData['wpEdittime'] = $edittime[1];
        if ($minor != null) { $postData['wpMinoredit'] = $minor; }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $postrequest);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        if ($proxyaddr != "") { curl_setopt($ch, CURLOPT_PROXY, $proxyaddr); }
        curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookiejar.txt');
        curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookiejar.txt');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        $response = curl_exec($ch);
        if (preg_match('/^Waiting for (.*) seconds lagged/', $response)) {
                echo "Maxlag hit, not posted\r\n";
                $returnval = false;
        } else {
                $returnval = true;
        }
        if(curl_errno($ch))
          {
            print curl_error($ch);
          }
        curl_close($ch);
	sleep($epm);
        return($returnval);
}
 
function sxGetCat($categoryname, $ns) {
        global $url;
	$fcat = array();
        if (!isset($url)) { die("\r\nConfiguration variables not set\r\n"); }
        $categoryname = urlencode($categoryname);
        $frequest = $url . 'api.php?action=query&list=categorymembers&cmcategory=' . $categoryname . '&format=php&cmlimit=500';
        $finalcat = getit($frequest);
        if(isset($finalcat['query-continue']['categorymembers']['cmcontinue'])) {
                $firstrun = 1;
		$catfrom = $finalcat['query-continue']['categorymembers']['cmcontinue'];
		$catfrom = urlencode($catfrom);
        } else {
                $firstrun = 0;
        }
        foreach($finalcat['query']['categorymembers'] as $fcat_l) {
	        array_push($fcat, $fcat_l);
        }

        while($done != 1) {
                if(isset($cat['query-continue']['categorymembers']['cmcontinue']) || $firstrun == 1) {
                        $request = $url . 'api.php?action=query&list=categorymembers&cmcategory=' . $categoryname.'&format=php&cmlimit=500&cmcontinue=' . 
$catfrom;
                        $cat = getit($request);
			$catfrom = $cat['query-continue']['categorymembers']['cmcontinue'];
			$catfrom = urlencode($catfrom);
			foreach($cat['query']['categorymembers'] as $fcat_l) {
				array_push($fcat, $fcat_l);
			}
                        echo ".";
                        $firstrun = 0;
                } else {
                        $done = 1;
                }
        }
        $result = array();
        foreach($fcat as $catmemb) {
		if ($catmemb['ns'] == $ns || $ns == "all") {
	                array_push($result, $catmemb['title']);
		}
        }
        return($result);
}

function sxGetPrefix($prefixname) {
	$o_prefixname = $prefixname;
        $result = array();
	$searchpf = '/^' . $o_prefixname . '/';
        global $url;
        if (!isset($url)) { die("\r\nConfiguration variables not set\r\n"); }
        $prefixname = urlencode($prefixname);
        $frequest = $url . 'api.php?action=query&list=allpages&apfrom=' . $prefixname . '&format=php&aplimit=500';
        $finalpre = getit($frequest);
        if(isset($finalpre['query-continue']['allpages']['apfrom'])) {
                $firstrun = "1";
		$prefart = urlencode($finalpre['query-continue']['allpages']['apfrom']);
        } else {
                $firstrun = "0";
        }
	foreach($finalpre['query']['allpages'] as $finalpre_l) {
		if(!preg_match($searchpf, $finalpre_l['title'])) {
			$done = 1;
		} else {
			array_push($result, $finalpre_l['title']);
		}
	}

        while($done != 1) {
                if(isset($pref['query-continue']['allpages']['apfrom']) || $firstrun == "1") {
                        $request = $url . 'api.php?action=query&list=allpages&apfrom=' . $prefixname.'&format=php&aplimit=500&apfrom=' . $prefart;
                        $pref = getit($request);
	                $prefart = urlencode($pref['query-continue']['allpages']['apfrom']);
			foreach($pref['query']['allpages'] as $pref_l) {
				if(!preg_match($searchpf, $pref_l['title'])) {
					$done = 1;
				} else {
					array_push($result, $pref_l['title']);
				}
			}
                        echo ".";
                        $firstrun = 0;
                } else {
                        $done = 1;
                }
        }
        return($result);
}

function sxLastEdited($article) {
        global $url;
        $article = urlencode($article);
        $request = $url . 'api.php?action=query&prop=revisions&titles=' . $article . '&rvprop=user|comment&rvlimit=1&format=php';
        $sxGetArticle = file_get_contents($request);
        $sxGetA = unserialize($sxGetArticle);
        $sxGetAID = $sxGetA;
        $sxGetAID = array_shift($sxGetAID);
        $sxGetAID = array_shift($sxGetAID);
        $sxGetAID = array_shift($sxGetAID);
        $sxGetAID = array_shift($sxGetAID);
	$sxAText = array();
        $sxAText['user'] = $sxGetA['query']['pages'][$sxGetAID]['revisions'][0]['user'];
	$sxAText['editsum'] = $sxGetA['query']['pages'][$sxGetAID]['revisions'][0]['comment'];
        return($sxAText);
}

function sxGetTransclusion($templatename, $ns) {
        global $url;
        global $epm;
        global $maxlag;
        $templatename = urlencode($templatename);
		$request = $url . "api.php?action=query&list=embeddedin&eititle=" . $templatename . "&eilimit=500&format=php";
        $result = getit($request);
        $pages = array();
        $oresult = $result;
		foreach ($result['query']['embeddedin'] as $single_result) {
                if ($single_result['ns'] == $ns && $ns != "all") { array_push($pages, $single_result['title']); } else {array_push($pages, $single_result['title']); }
        }
        while ($done != "yes") {
                if(isset($result['query-continue']['embeddedin']['eicontinue'])) {
                        $done = "no";
                        $request2 = $request . "&eicontinue=" . $result['query-continue']['embeddedin']['eicontinue'];
                        $result = file($request2);
                        $result = unserialize($result[0]);
                        foreach ($result['query']['embeddedin'] as $single_result) {
		                if ($single_result['ns'] == $ns && $ns != "all") { array_push($pages, $single_result['title']); } else {array_push($pages, $single_result['title']); }
                        }
                } else {
                        $done = "yes";
                }
        }
	return($pages);
}

function sxBlockUser($user, $expiry, $reason, $ao, $acb, $autoblock, $emailban) {
        global $maxlag;
        global $url;
        global $epm;
        global $proxyaddr;
        if (!isset($epm) || !isset($url) || !isset($maxlag)) { die("\r\nConfiguration variables not set\r\n"); }
        $article = urlencode($article);
        $postrequest = $url . 'index.php?title=Special:Blockip/' . $user;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookiejar.txt');
        curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookiejar.txt');
        if ($proxyaddr != "") { curl_setopt($ch, CURLOPT_PROXY, $proxyaddr); }
        curl_setopt($ch, CURLOPT_URL, $postrequest);
        $response = curl_exec($ch);
        preg_match('/\<input name\=\"wpEditToken\" type\=\"hidden\" value\=\"(.*)\" \/\>/i', $response, $token);
	curl_close($ch);
        $postrequest = $url . 'index.php?title=Special:Blockip/' . $user . '&action=submit&maxlag=' . $maxlag;
        $postData['wpEditToken'] = $token[1];
        $postData['wpBlockAddress'] = $user;
		$postData['wpBlockOther'] = $expiry;
        $postData['wpBlockReason'] = $reason;
		$postData['wpBlock'] = "Block";
        if ($ao != null) { $postData['wpAnonOnly'] = $ao; }
        if ($acb != null) { $postData['wpCreateAccount'] = $acb; }
        if ($autoblock != null) { $postData['wpEnableAutoblock'] = $autoblock; }
        if ($emailban != null) { $postData['wpEmailBan'] = $emailban; }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $postrequest);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        if ($proxyaddr != "") { curl_setopt($ch, CURLOPT_PROXY, $proxyaddr); }
        curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookiejar.txt');
        curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookiejar.txt');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        $response = curl_exec($ch);
        if (preg_match('/^Waiting for (.*) seconds lagged/', $response)) {
                echo "Maxlag hit, not posted\r\n";
                $returnval = false;
        } else {
                $returnval = true;
        }
        if(curl_errno($ch))
          {
            print curl_error($ch);
          }
        curl_close($ch);
	sleep($epm);
        return($returnval);
}

function sxModRollback($user, $reason, $action) {
        global $maxlag;
        global $url;
        global $epm;
        global $proxyaddr;
        if (!isset($epm) || !isset($url) || !isset($maxlag)) { die("\r\nConfiguration variables not set\r\n"); }
	$user2 = urlencode($user);
        $postrequest = $url . 'index.php?title=Special:Userrights&user=' . $user2;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookiejar.txt');
        curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookiejar.txt');
        if ($proxyaddr != "") { curl_setopt($ch, CURLOPT_PROXY, $proxyaddr); }
        curl_setopt($ch, CURLOPT_URL, $postrequest);
        $response = curl_exec($ch);
        preg_match('/\<input name\=\"wpEditToken\" type\=\"hidden\" value\=\"(.*)\" \/\>/i', $response, $token);
        curl_close($ch);
        $postrequest = $url . 'index.php?title=Special:Userrights&user=' . $user2 . '&maxlag=' . $maxlag;
        $postData['wpEditToken'] = $token[1];
        $postData['user'] = $user;
	switch ($action) {
		case "add":
			$postData['available[]'] = "rollbacker";
			$postData['removable[]'] = "";
			break;
		case "del":
			$postData['removable[]'] = "rollbacker";
			$postData['available[]'] = "";
			break;
	}
        $postData['user-reason'] = $reason;
	$postData['saveusergroups'] = "Save User Groups";
	$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $postrequest);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        if ($proxyaddr != "") { curl_setopt($ch, CURLOPT_PROXY, $proxyaddr); }
        curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookiejar.txt');
        curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookiejar.txt');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        $response = curl_exec($ch);
        if (preg_match('/^Waiting for (.*) seconds lagged/', $response)) {
                echo "Maxlag hit, not posted\r\n";
                $returnval = false;
        } else {
                $returnval = true;
        }
        if(curl_errno($ch))
          {
            print curl_error($ch);
          }
        curl_close($ch);
	sleep($epm);
        return($returnval);
}

 function sxUnBlockUser($user, $reason) {
        global $maxlag;
        global $url;
        global $epm;
        global $proxyaddr;
        if (!isset($epm) || !isset($url) || !isset($maxlag)) { die("\r\nConfiguration variables not set\r\n"); }
        $article = urlencode($article);
        $postrequest = $url . 'index.php?title=Special:Ipblocklist&action=unblock&ip=' . $user;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookiejar.txt');
        curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookiejar.txt');
        if ($proxyaddr != "") { curl_setopt($ch, CURLOPT_PROXY, $proxyaddr); }
        curl_setopt($ch, CURLOPT_URL, $postrequest);
        $response = curl_exec($ch);
        preg_match('/\<input name\=\"wpEditToken\" type\=\"hidden\" value\=\"(.*)\" \/\>/i', $response, $token);
	curl_close($ch);
        $postrequest = $url . 'index.php?title=Special:Ipblocklist&action=submit'  . '&action=submit&maxlag=' . $maxlag;
        $postData['wpEditToken'] = $token[1];
        $postData['wpUnblockAddress'] = $user;
		$postData['wpUnblockReason'] = $reason;
		$postData['wpBlock'] = "Unblock";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $postrequest);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        if ($proxyaddr != "") { curl_setopt($ch, CURLOPT_PROXY, $proxyaddr); }
        curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookiejar.txt');
        curl_setopt ($ch, CURLOPT_COOKIEFILE, 'cookiejar.txt');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        $response = curl_exec($ch);
        if (preg_match('/^Waiting for (.*) seconds lagged/', $response)) {
                echo "Maxlag hit, not posted\r\n";
                $returnval = false;
        } else {
                $returnval = true;
        }
        if(curl_errno($ch))
          {
            print curl_error($ch);
          }
        curl_close($ch);
	sleep($epm);
        return($returnval);
}

function sxIsBlocked($user) {
	$bturl = '/w/index.php?title=Special%3AIpblocklist&ip=';
	$user = rtrim($user);
	$user = ltrim($user);
	$test_url = $bturl . $user;
	$fp = fsockopen("en.wikipedia.org", 80, $errno, $errstr, 30);
	$out = "GET $test_url HTTP/1.1\r\n";
	$out .= "Host: en.wikipedia.org\r\n";
	$out .= "User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20021204\r\n";
	$out .= "Connection: Close\r\n\r\n";
	fwrite($fp, $out);
	$f_input = "";
	while (!feof($fp)) {
		$f_input = fgets($fp, 512);
		$cpage = $cpage . $f_input;
	}
	fclose($fp);
	$findblock = '/The requested IP address or username is not blocked\./';
	$is_blocked = preg_match($findblock, $cpage);
	if($is_blocked != 0) {
		return(TRUE);
	} else {
		return(FALSE);
	}
}

function getit ($query) {
        $result = file($query);
        $result = unserialize($result[0]);
        return($result);
}
 
?>