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

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


function WordPressToQozu($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);


	$x = 0; //For counting all comments uniquely
	foreach ( $xml->channel->item as $item){ //Every item

		if (empty ($item->xpath('.//wp:comment') ) ){ // Only see articles with comments, is not an post import
			continue;
		}
		
		//Search for ID
		if ($item->xpath('.//wp:post_id')){
			$id = $item->xpath('.//wp:post_id')[0];
		}else{
			$id = $item->xpath('.//dsq:thread_identifier')[0];
		}
		$id = trim($id);
		$importarray[$id] = array(
			'ID_UW' => $id,
			'title' => trim($item->xpath('title')[0])
		);


		foreach($item->xpath('.//wp:comment') as $comment){
			$idc = trim($comment->xpath('wp:comment_id')[0]);
			$commentsarray[$x]['ID_C'] = $idc;
			$commentsarray[$x]['ID_UW'] = $id;
			
			if ($comment->xpath('wp:comment_date_gmt')){
				$commentsarray[$x]['timestamp'] = strtotime(trim($comment->xpath('wp:comment_date_gmt')[0]));
			}else{
				$commentsarray[$x]['timestamp'] = strtotime(trim($comment->xpath('wp:comment_date')[0]));
			}
			
			$commentsarray[$x]['username'] = trim($comment->xpath('wp:comment_author')[0]);
			$commentsarray[$x]['visible'] = trim($comment->xpath('wp:comment_approved')[0]);
			$commentsarray[$x]['comment'] = trim($comment->xpath('wp:comment_content')[0]);
			if (trim($comment->xpath('wp:comment_author_email')[0])){
				$commentsarray[$x]['email'] = trim($comment->xpath('wp:comment_author_email')[0]);
			}else{
				$commentsarray[$x]['email'] = NULL;
			}
			
			if (trim($comment->xpath('wp:comment_parent')[0])){ //If its an answer
				$commentsarray[$x]['answerto'] = trim($comment->xpath('wp:comment_parent')[0]);
				$commentsarray[$x]['answerto'] = dbw_escape_string($db_conn,$commentsarray[$x]['answerto']);
			}else{
				$commentsarray[$x]['answerto'] = 'NULL';
			}
			$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['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['comment'] = dbw_escape_string($db_conn,$ia['comment']);
		$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,';'));

	unlink('../cache/imports/'.$ID_W.'.xml'); /* ../ because is running from cron/ 

}
?>