<?php
/* For load this funcion, connect.php has to be called before, and not in this file */

/**  This function imports a Disqus export xml comments to Qozu directly
	Only needs the ID_W for the import, var and xml file (Not gzipped)
	Function doesn't have security for knowing if is a trusted user importing it or not, it has to be did before calling
*/


function DisqusToQozu($xml_file,$ID_W){
	global $db_conn;
	// Convert CDATA into xml nodes and load as SimpleXML.
	$xml = simplexml_load_file($xml_file,'SimpleXMLElement', LIBXML_NOCDATA);
	$xml->getNameSpaces(true);


	foreach ($xml->thread as $post) 
		{
			$atr = $post->attributes('dsq',true); //This makes an array with attributes, for use "dsq:id" one
			$id = trim($atr['id']);
			if ($id != ''){
				$atr = $post->attributes('dsq',true);
				$importarray[$id] = array(
					'ID_UW' => trim($post->id),
					'title' => trim($post->title)
				);
				$realID_UW[$id] = trim($post->id); //Correspondence from disqus unique ID the real ID that developers used, needed make Threads and comments "linked" right, used for ID_UW in next foreach
			}
		}

		$x = 0; //Array for count...
	foreach ($xml->post as $comm) { //All comments
		$atr = $comm->attributes('dsq',true); //This makes an array with attributes, for use "dsq:id" one
		$ID_UW = $realID_UW[trim($comm->thread->attributes('dsq',true)['id'])]; //ID_UW, thread correspondence of the comment.

		$idc = retIntFromID_C_UW(trim($atr['id']),$ID_UW); //This will handle id of comments for return an int value, and not a Bigint value
		
		$commentsarray[$x]['ID_C'] = $idc;
		$commentsarray[$x]['ID_UW'] = $ID_UW;
		$commentsarray[$x]['timestamp'] = strtotime(trim($comm->createdAt));
		$commentsarray[$x]['username'] = trim($comm->author->name);
		$commentsarray[$x]['email'] = trim($comm->author->email);
		if (trim($comm->isDeleted) == "false"){
			$commentsarray[$x]['hidden'] = 0;
		}else{
			$commentsarray[$x]['hidden'] = 1;
		}
		$commentsarray[$x]['comment'] = dbw_escape_string($db_conn,trim($comm->message));
		$x++;
	}

	//Delete all comments and titles before inserting new
	dbw_query($db_conn,"DELETE FROM COMMENTS WHERE ID_W='$ID_W'");
	dbw_query($db_conn,"DELETE FROM UWDATA WHERE ID_W='$ID_W'");


	//Now create an array to import all as a transaction
	$transaction[] = 'START TRANSACTION';
	foreach($importarray as $ia){
		//$ia['ID_UW'] = $realID_UW["$ia[ID_UW]"];
		$ia['title'] = dbw_escape_string($db_conn,$ia['title']);
		$transaction[] = "INSERT INTO UWDATA(`ID_W`,`ID_UW`,`title`) VALUES ('$ID_W','$ia[ID_UW]','$ia[title]')";
	}

	foreach($commentsarray as $ia){
		//$ia['ID_UW'] = $realID_UW["$ia[ID_UW]"];
		$ia['comment'] = dbw_escape_string($db_conn,$ia['comment']);
		$ia['answerto'] = 'NULL'; //Answerto not supported in Disqus
		$transaction[] = "INSERT INTO COMMENTS(`ID_C`,`ID_W`,`ID_UW`,`answerto`,`username`,`email`,`timestamp`,`comment`) VALUES ('$ia[ID_C]','$ID_W','$ia[ID_UW]',$ia[answerto],'$ia[username]','$ia[email]','$ia[timestamp]','$ia[comment]')";
	}

	$transaction[] = 'COMMIT';
	dbw_multi_query($db_conn,implode($transaction,';'));
	echo 'importados ',$x+1,'comentarios';
	unlink('../cache/imports/'.$ID_W.'.xml');


}

/** This function creates a numeric Int comment ID and saves the Bigint one for when needing it (Maybe for answerto if I know the way)
*/

function retIntFromID_C_UW($atrIDC,$ID_UW){
	global $bigIntID_C;
	if (isset($bigIntID_C[$ID_UW]) && in_array($atrIDC, $bigIntID_C[$ID_UW])){
		return $bigIntID_C[$ID_UW][$atrIDC];
	}else{
		if (!isset($bigIntID_C[$ID_UW])){ //If array doesn't exists, create empty one.
			$bigIntID_C[$ID_UW] = array();
		}
		$bigIntID_C[$ID_UW][$atrIDC] = count($bigIntID_C[$ID_UW]) + 1;
		return $bigIntID_C[$ID_UW][$atrIDC];
	}
}

?>