<?php

// Directory to output your wiki pages.
// Must be writeable by the Webserver.
DEFINE ('WIKID_DIR', '/home/ian/public_html/test/wiki.d/');

// Newline character (do we really need this?).
DEFINE('NEW_LINE' , '%0a');

// Location of your exported Wordpress entries.
$xml_file = '/home/ian/public_html/test/wordpress_export.xml';

// These correspond to the tags you defined in your
// xml export file.
$open_tags = array(
    'POSTID' => '<POSTID>',
	'POSTDATE' => '<POSTDATE>',
	'POSTTITLE' => '<POSTTITLE>',
	'POSTSTATUS' => '<POSTSTATUS>',
	'POSTMODIFIED' => '<POSTMODIFIED>',
	'BODY' => '<BODY>',
	'ENTRY' => '<ENTRY>');


$close_tags = array(
    'POSTID' => '</POSTID>',
	'POSTDATE' => '</POSTDATE>',
	'POSTTITLE' => '</POSTTITLE>',
	'POSTSTATUS' => '</POSTSTATUS>',
	'POSTMODIFIED' => '</POSTMODIFIED>',
	'BODY' => '</BODY>',
	'ENTRY' => '</ENTRY>');



// This called for each opening tag. I don't need to
// use it, but it's included here in case you do.
function startElement($parser, $name, $attrs=''){
    global $open_tags, $temp, $current_tag;
    //global $open_tags, $current_tag;
    $current_tag = $name;
    if ($format = $open_tags[$name]){
	    switch($name){
		    case 'POSTID':
		    	break;
		    case 'POSTDATE':
		    	break;
		    break;
		    case 'BODY':
		    	break;
		    default:
		    break;
	    }
    }
}


// When we see an ENTRY tag we have read all the data for
// an entry, so we can flush our temp variables.
function endElement($parser, $name, $attrs=''){
    global $close_tags, $temp, $current_tag;
    //global $close_tags, $current_tag;
    if ($format = $close_tags[$name]){

    switch($name){
	    //End of data return the array.
		CASE 'ENTRY':
			// Call the function to create our wiki page.
                        create_wiki_page($temp);
			
                        // Reset variable.
                        $temp = '';
                        break;
        default:
        break;
    }
    }
}

// Process the data between the elements.
function characterData($parser, $data){
    global $current_tag, $temp, $catID;
    //global $current_tag, $catID;
    switch($current_tag){
	    case 'POSTID':
	    	$temp['postid'] = $data;
		$current_tag = '';
   	        break;
	    case 'POSTDATE':
	   	 $temp['postdate'] = $data;
		 $current_tag = '';
	         break;
	    case 'POSTTITLE':
	    	$temp['posttitle'] = $data;
		$current_tag = '';
	        break;

	    case 'BODY':
	   	 $temp['body'] = $data;
		 $current_tag = '';
	         break;
    default:
        break;
    }
}


function create_wiki_page() {
	global $temp;
	
    $content = $content . $temp['body'];

        // Extract the yyy-mm-dd part of the date.
	$tempdate = substr($temp['postdate'], 0, 10);
	
        // Remove the "-" character.
	$tempdate = preg_replace('/-/', '', $tempdate);
	
        // Check to see if the iki page already exists.
        // If it does, append this entry to the bottom of the page.
        // This consolidates multiple blog entries for a day
        // onto a single wiki page.

        // I am creating pages in a group called Journal. You may
        // wish to alter this.
	if (file_exists(WIKID_DIR . 'Journal.' . $tempdate)) {
		//Open for read/write.
		if ($content = file_get_contents(WIKID_DIR . 'Journal.' . $tempdate)); {
			//Open and replace contents.
			$handle = fopen(WIKID_DIR . 'Journal.' . $tempdate, 'w');
			if (! $handle) {
				die("Could not open file.");
			} else {
				//Insert a paragraph to separate the new entry.
				$content = $content . NEW_LINE . NEW_LINE;
				$content = $content . "!" . $temp['posttitle'] . NEW_LINE;
				$content = $content . $temp['body'];
				fwrite($handle, $content);

			}

		}
	} else {
                // Start a new wiki page.
		$content = "newline=" . NEWLINE;
		$content = $content . "\n" . "text=";
		$content = $content . "!" . $temp['posttitle'] . NEW_LINE;
		$content = $content . $temp['body'];

		
		$handle = fopen(WIKID_DIR . 'Journal.' . $tempdate, 'w');
		if (! $handle) {
			die("Could not open file.");
		} else {
			fwrite($handle, $content);

		}
	}
	fclose($handle);

}


// Declare the character set - UTF-8 is the default
$type = 'UTF-8';

// Create our parser
$xml_parser = xml_parser_create($type);

// Set parser options
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');

// This tells PHP what functions to call when it finds an element.
// These functions also handle the element's attributes
xml_set_element_handler($xml_parser, 'startElement','endElement');

// This tells PHP what function to use on the character data
xml_set_character_data_handler($xml_parser, 'characterData');

if (!($fp = fopen($xml_file, 'r'))) {
    die("Could not open $xml_file for parsing!\n");
}

// Start reading through the file and parsing it.
while ($data = fread($fp, 4096)) {
    if (!($data = utf8_encode($data))) {
        echo 'ERROR'."\n";
    }
    if (!xml_parse($xml_parser, $data, feof($fp))) {
        die(sprintf( "XML error: %s at line %d\n\n",
        xml_error_string(xml_get_error_code($xml_parser)),
        xml_get_current_line_number($xml_parser)));
    }
}

xml_parser_free($xml_parser);

?>