ast
1 month ago
XmlImportConfig.php
1 month ago
XmlImportCsvParse.php
1 month ago
XmlImportException.php
1 month ago
XmlImportParser.php
1 month ago
XmlImportReaderInterface.php
1 month ago
XmlImportSQLParse.php
1 month ago
XmlImportStringReader.php
1 month ago
XmlImportTemplate.php
1 month ago
XmlImportTemplateCodeGenerator.php
1 month ago
XmlImportTemplateParser.php
1 month ago
XmlImportTemplateScanner.php
3 days ago
XmlImportToken.php
1 month ago
XmlImportXLSParse.php
1 month ago
wpaipclzip.lib.php
1 month ago
XmlImportParser.php
116 lines
| 1 | <?php |
| 2 | // phpcs:disable WordPress.NamingConventions.PrefixAllGlobals |
| 3 | /** |
| 4 | * @author Olexandr Zanichkovsky <olexandr.zanichkovsky@zophiatech.com> |
| 5 | * @author Pavel Kulbakin <p.kulbakin@gmail.com> |
| 6 | * @package General |
| 7 | */ |
| 8 | |
| 9 | require_once dirname(__FILE__) . '/XmlImportTemplate.php'; |
| 10 | |
| 11 | /** |
| 12 | * Is used to parse XML using specified template and root node |
| 13 | */ |
| 14 | class XmlImportParser { |
| 15 | /** |
| 16 | * SimpleXmlElement instance for the xml file |
| 17 | * |
| 18 | * @var SimpleXMLElement |
| 19 | */ |
| 20 | protected $xml; |
| 21 | |
| 22 | /** |
| 23 | * XPath expression for selecting the root node of the record |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | protected $rootNodeXPath; |
| 28 | |
| 29 | /** |
| 30 | * Path to cached template |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | protected $cachedTemplate; |
| 35 | |
| 36 | /** |
| 37 | * Creates new parser instance |
| 38 | * |
| 39 | * @param string $xml |
| 40 | * @param string $rootNodeXPath XPath for the record root node |
| 41 | * @param string $cachedTemplate path to cached template |
| 42 | * @param bool $isURL whether xml is URL or path |
| 43 | */ |
| 44 | public function __construct($xml, $rootNodeXPath, $cachedTemplate, $isURL = false) |
| 45 | { |
| 46 | if ($isURL) { |
| 47 | $xml = file_get_contents($xml); |
| 48 | } |
| 49 | |
| 50 | // FIX: remove default namespace because libxml xpath implementation doesn't handle it properly |
| 51 | $xml and $xml = preg_replace('%xmlns\s*=\s*([\'"]).*\1%sU', '', $xml); |
| 52 | |
| 53 | libxml_use_internal_errors(true); |
| 54 | try{ |
| 55 | $this->xml = new SimpleXMLElement($xml); |
| 56 | } catch (Exception $e){ |
| 57 | try{ |
| 58 | $this->xml = new SimpleXMLElement(mb_convert_encoding( $xml, 'UTF-8', 'ISO-8859-1' )); |
| 59 | } catch (Exception $e){ |
| 60 | throw new XmlImportException(esc_html($e->getMessage())); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | $this->rootNodeXPath = $rootNodeXPath; |
| 65 | $this->cachedTemplate = $cachedTemplate; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Gets the parser results for all records or |
| 70 | * the part of them if $start and $count parameters are passed |
| 71 | * |
| 72 | * @param array[optional] $records Sequence numbers of records to import (first record corresponds to 1) |
| 73 | * @return array |
| 74 | */ |
| 75 | public function parse($records = array()) |
| 76 | { |
| 77 | empty($records) or is_array($records) or $records = array($records); |
| 78 | |
| 79 | $result = array(); |
| 80 | |
| 81 | $rootNodes = $this->xml->xpath($this->rootNodeXPath); |
| 82 | if ($rootNodes === false) |
| 83 | throw new XmlImportException(esc_html('Invalid root node XPath \'' . $this->rootNodeXPath . '\' specified')); |
| 84 | |
| 85 | for ($i = 0; $i < count($rootNodes); $i++) { |
| 86 | if (empty($records) or in_array($i + 1, $records)) { |
| 87 | $rootNode = apply_filters('wpallimport_xml_row', $rootNodes[$i]); |
| 88 | $template = new XmlImportTemplate($rootNode, $this->cachedTemplate); |
| 89 | $result[] = $template->parse(); |
| 90 | } |
| 91 | } |
| 92 | return $result; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Creates new parser instance for text template specified |
| 97 | * |
| 98 | * @param string $xml |
| 99 | * @param string $rootNodeXPath XPath for the record root node |
| 100 | * @param string $template template |
| 101 | * @param string &$file path to the cached template |
| 102 | * @return XmlImportParser |
| 103 | */ |
| 104 | public static function factory($xml, $rootNodeXPath, $template, &$file = NULL) |
| 105 | { |
| 106 | |
| 107 | $scanner = new XmlImportTemplateScanner(); |
| 108 | $tokens = $scanner->scan(new XmlImportStringReader($template)); |
| 109 | $t_parser = new XmlImportTemplateParser($tokens); |
| 110 | $tree = $t_parser->parse(); |
| 111 | $codegenerator = new XmlImportTemplateCodeGenerator($tree); |
| 112 | $file = $codegenerator->generate(); |
| 113 | |
| 114 | return new self($xml, $rootNodeXPath, $file); |
| 115 | } |
| 116 | } |