XmlImportParser.php 3.13 KB
Newer Older
imac's avatar
imac committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
<?php
/**
 * @author Olexandr Zanichkovsky <olexandr.zanichkovsky@zophiatech.com>
 * @author Pavel Kulbakin <p.kulbakin@gmail.com>
 * @package General
 */

require_once dirname(__FILE__) . '/XmlImportTemplate.php';

/**
 * Is used to parse XML using specified template and root node
 */
class XmlImportParser {
	/**
	 * SimpleXmlElement instance for the xml file
	 *
	 * @var SimpleXMLElement
	 */
	protected $xml;

	/**
	 * XPath expression for selecting the root node of the record
	 *
	 * @var string
	 */
	protected $rootNodeXPath;

	/**
	 * Path to cached template
	 *
	 * @var string
	 */
	protected $cachedTemplate;

	/**
	 * Creates new parser instance
	 *
	 * @param string $xml
	 * @param string $rootNodeXPath XPath for the record root node
	 * @param string $cachedTemplate path to cached template
	 * @param bool $isURL whether xml is URL or path
	 */
	public function __construct($xml, $rootNodeXPath, $cachedTemplate, $isURL = false)
	{		
		if ($isURL) {
			$xml = file_get_contents($xml);
		}		
		
		// FIX: remove default namespace because libxml xpath implementation doesn't handle it properly
		$xml and $xml = preg_replace('%xmlns\s*=\s*([\'"]).*\1%sU', '', $xml);
	
		libxml_use_internal_errors(true);
		try{ 			
			$this->xml = new SimpleXMLElement($xml);
		} catch (Exception $e){ 			
			try{ 
				$this->xml = new SimpleXMLElement(utf8_encode($xml));
			} catch (Exception $e){ 
				throw new XmlImportException($e->getMessage());
			}
		}			

		$this->rootNodeXPath = $rootNodeXPath;
		$this->cachedTemplate = $cachedTemplate;
	}

	/**
	 * Gets the parser results for all records or
	 * the part of them if $start and $count parameters are passed
	 *
	 * @param array[optional] $records Sequence numbers of records to import (first record corresponds to 1)
	 * @return array
	 */
	public function parse($records = array())
	{				
		empty($records) or is_array($records) or $records = array($records);
		
		$result = array();
		
		$rootNodes = $this->xml->xpath($this->rootNodeXPath);		
		if ($rootNodes === false)
		throw new XmlImportException('Invalid root node XPath \'' . $this->rootNodeXPath . '\' specified');
	    		
		for ($i = 0; $i < count($rootNodes); $i++) {
			if (empty($records) or in_array($i + 1, $records)) {                
				$rootNode = apply_filters('wpallimport_xml_row', $rootNodes[$i]);
				$template = new XmlImportTemplate($rootNode, $this->cachedTemplate);
				$result[] = $template->parse();
			}
		}				
		return $result;
	}

	/**
	 * Creates new parser instance for text template specified
	 *
	 * @param string $xml
	 * @param string $rootNodeXPath XPath for the record root node
	 * @param string $template template
	 * @param string &$file path to the cached template
	 * @return XmlImportParser
	 */
	public static function factory($xml, $rootNodeXPath, $template, &$file = NULL)
	{		
	
		$scanner = new XmlImportTemplateScanner();		
		$tokens = $scanner->scan(new XmlImportStringReader($template));				
		$t_parser = new XmlImportTemplateParser($tokens);
		$tree = $t_parser->parse();
		$codegenerator = new XmlImportTemplateCodeGenerator($tree);
		$file = $codegenerator->generate();
				
		return new self($xml, $rootNodeXPath, $file);
	}
}