<?php

//error_reporting(E_ALL);
ini_set('display_errors', '0');

$url = "http://meta.wikimedia.org/wiki/Special:SiteMatrix";
$userAgent = "Wiki3D-experiment http://www.world.eu.org/~lucky/wiki_browser/ - by <lukas.goldschmidt@googlemail.com> ";

function get_content($url) {
	$userAgent = "Wiki3D-experiment http://www.world.eu.org/~lucky/wiki_browser/ - by <lukas.goldschmidt@googlemail.com> ";
	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);

	ob_start();

	curl_exec($ch);
	curl_close($ch);
	$string = ob_get_contents();

	ob_end_clean();

	return $string;

}

$raw = get_content($url);
$newlines = array("\t", "\n", "\r", "\x20\x20", "\0", "\x0B");
$content = str_replace($newlines, "", html_entity_decode($raw));

$start = strpos($content, "<table class=\"wikitable\" id=\"mw-sitematrix-table\">");
$end = strpos($content, "</td></tr></table>", $start) + 18;

$table = substr($content, $start, $end - $start);

preg_match_all("|<tr(.*)</tr>|U", $table, $rows);
header("Content-type: text/javascript");

echo <<<TEXT
/**
 * Wikipedia SiteMatrix  \n
TEXT;
echo " *\n";
echo " * Automatically generated ". date("g:i:s A D, F jS Y",time ()). "\n";
echo " *\n";
echo <<<TEXT
 */

 var wp_languages = new Array(\n
TEXT;
$first = true;
foreach ($rows[0] as $row) {
	if (strpos($row, "<th") === false) {
		preg_match_all("|<td>(.*)</td>|U", $row, $cells);
		$name = json_encode(trim(strip_tags($cells[1][0])));
		$id = $cells[1][1];
		$url_ = $cells[1][1];
		preg_match_all("|\"(.*)\"|U", $url_, $tmp);
		if (strlen($id) > 0 && strpos($id, "<del>") === false && strpos($name, "Total") === false) {
			$id = strip_tags($id);
			if(strlen($name) < 3 ){
				$name = json_encode($id);
			} 
			$url_ = json_encode($tmp[1][0]."/");
			if($first) $comma = ""; else $comma=",\n";
			
			echo $comma."{\"lang\": \"{$id}\", \"name\": {$name},  \"url\":  {$url_} }";
			$first = false;

		}
	}
}
echo <<<TEXT
\n);

TEXT;
echo "// Finished \n";
?>