Location: PHPKode > projects > Open ShareLive > F3-OSL-pre_alpha-2005_05_30/xmlify.php
<?php
/******************************************************************************
*
*    Open ShareLive (main site code)
*    Copyright (C) 2002-2005  ShareLive
*
*    This program is free software; you can redistribute it and/or
*    modify it under the terms of the GNU General Public License
*    as published by the Free Software Foundation; either version 2
*    of the License, or (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program; if not, write to the Free Software
*    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*
******************************************************************************/

	// ShareLive Database XML Export Functions
	
	require('Connections/Default.php');
	
	// dbprefix makes writing SQL a PITA so i've defined table names again here to make things easier.
	// was this really necessary? doing a F&R sl_ in code would have been MUCCHHH easier and *less* work!
	// 'tis not like would ever need modifying. hmm.. oh well
	
	$td = $dbprefix . "tag_data";
	$ts = $dbprefix . "tags";
	$ur = $dbprefix . "users";
	$sh = $dbprefix . "shares";
	$li = $dbprefix . "links";
	$nw = $dbprefix . "networks";
	
	// Produces the XML header and DTD reference lines and the
	// start of the document.
	function GenerateHeader($count) {
		echo "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?" . ">\n";
		echo "<!DOCTYPE releases SYSTEM \"http://www.sharelive.com/xml/sl_rel.dtd\" >\n\n";
		echo "<releases count=\"$count\">\n";
	}
	
	function CreateReleaseBlock($prow) {
		global $ts, $td, $li;
		echo "\t<release oid=\"" . $prow['rel_id'] . "\" existing=\"" . ($prow['notmine'] ? 'true' : 'false') . "\" counter=\"" . $prow['dl_counter'] . "\">\n";
		echo "\t\t<fullname>" . htmlspecialchars($prow['str_full']) . "</fullname>\n";
		echo "\t\t<shortname>" . htmlspecialchars($prow['str_short']) . "</shortname>\n";
		
		$q = mysql_query("SELECT *, $ts.display, $ts.type FROM $td INNER JOIN $ts ON ($ts.tag_id = $td.tag_id) WHERE rel_id = " . $prow['rel_id'] . " AND data <> \"\" ORDER BY $ts.sort_value");
		
		$res = "";
				
		if ($q && mysql_num_rows($q)) {
			echo "\t\t<tags>\n";
			while ($row = mysql_fetch_array($q)) {				
				$type = explode(',', $row['type']);
				$lastres = $res;
				switch (strtoupper($type[0])) {
				case "STR":
				case "NUM":
					$res = $row['data'];				
					break;
				case "BOOL":
					$res = ($row['data']=='1' ? 'Yes' : 'No');				
					break;
				case "LIST":
					$paramlist = explode('|', $type[1]);
					foreach ($paramlist as $paramitem) {
						$pdata = explode('=', $paramitem);
						$res = 'Unknown Value';
						if ($pdata[1] == $row['data']) {
							$res = $pdata[0];
							break;
						}						
					}									
				}
				
				$res = htmlspecialchars($res);
				// a bit of a hack to get round some tag clean-up problems that exist at least
				// in this test database (problem probably doesn't exist in the real data)
				if ($lastres != $res) echo "\t\t\t<tag name=\"" . htmlspecialchars($row['display']) . "\">$res</tag>\n";
			}
			echo "\t\t</tags>\n";			
		}	
		
		if ($prow['website_url']) echo "\t\t<url>" . htmlspecialchars($prow['website_url']) . "</url>\n";		
		echo "\t\t<date unixtimestamp=\"" . $prow['reqrel_date'] . "\">" . date("d F Y H:i", $prow['reqrel_date']) . "</date>\n";
		echo "\t\t<releaser>" . $prow['username'] . "</releaser>\n";
		echo "\t\t<network iid=\"" . $prow['nid'] . "\">" . $prow['display'] . "</network>\n";
		$q = mysql_query("SELECT * FROM $li WHERE rel_id = " . $prow['rel_id']);
		if ($q && mysql_num_rows($q)) {
			echo "\t\t<links>\n";
			while ($row = mysql_fetch_array($q)) {
				$l = htmlspecialchars($row['link']);
				echo "\t\t\t<link downloads=\"" . $row['counter'] . "\">" . $l . "</link>\n";
			}
			echo "\t\t</links>\n";
		}
		echo "\t\t<description>" . htmlspecialchars($prow['description']) . "</description>\n";
		
		echo "\t</release>\n";
	}
	
	function GenerateFooter() {
		echo "</releases>";
		// insert additional footer commentary here :)
	}
	
	// Produces XML for the entire database
	function DoFullXMLExport() {
		DoExportForNetwork();
	}
	
	// Produces XML for only a given network ID
	function DoExportForNetwork($nid = "") {
		global $ur, $sh, $nw;
		if (strlen($nid)) $nid = "AND network = '$nid'";
		$q = mysql_query("SELECT *, $ur.username, $nw.display, $nw.nid FROM $sh INNER JOIN $ur ON ($ur.user_id = $sh.rel_userid) INNER JOIN $nw ON ($nw.nid = $sh.network) WHERE req_state = 2 $nid ORDER BY rel_id");
		echo mysql_error();
		if ($q && mysql_num_rows($q)) {
			GenerateHeader(mysql_num_rows($q));
			while ($row = mysql_fetch_array($q)) CreateReleaseBlock($row);
			GenerateFooter();
		}
	}
	
	if ($_GET['get']) {
		header('Content-Type: text/xml');
		DoExportForNetwork($_GET['get']);
		exit();
	}
		
?>	
<html><head><title>ShareLive XML Export</title></head>
<body>
<pre>
SHARELIVE LIVE DB XML GENERATOR

Select a network to generate XML data for:
 - <a href="xmlify.php?get=1/sl_export_edonkey.xml">eDonkey</a>
 - <a href="xmlify.php?get=2/sl_export_overnet.xml">Overnet</a>
 - <a href="xmlify.php?get=3/sl_export_g2.xml">Gnutella2</a>
 - <a href="xmlify.php?get=4/sl_export_piolet.xml">Piolet</a>
 
Please do not download the XML unless you need it for something. It's a bit big
and takes a while to generate/transfer (well, on my 56k connection anyway...) :)


</pre>
</body>
</html>
Return current item: Open ShareLive