PortableInfobox/services/Parser/XmlParser.php

114 lines
2.8 KiB
PHP
Raw Normal View History

<?php
namespace Wikia\PortableInfobox\Parser;
class XmlParser {
protected $infoboxData;
protected $externalParser;
public function __construct( $infoboxData ) {
$this->infoboxData = $infoboxData;
}
/**
* @return mixed
*/
public function getExternalParser() {
return $this->externalParser;
}
/**
* @param mixed $externalParser
*/
public function setExternalParser( ExternalParser $externalParser ) {
$this->externalParser = $externalParser;
}
/**
* @param \SimpleXMLElement $xmlIterable
* @return array
*/
public function getDataFromNodes( \SimpleXMLElement $xmlIterable, $parentNode = null ) {
wfProfileIn(__METHOD__);
$data = [ ];
foreach ( $xmlIterable as $node ) {
$nodeHandler = Nodes\NodeFactory::newFromSimpleXml( $node, $this->infoboxData );
if ( $this->getExternalParser() ) {
$nodeHandler->setExternalParser( $this->getExternalParser() );
}
if ( $parentNode ) {
$nodeHandler->setParent( $parentNode );
}
$nodeData = $nodeHandler->getData();
// add data if node is not empty or - when node can not be ignored when empty
if ( !$nodeHandler->isEmpty() || !$nodeHandler->ignoreNodeWhenEmpty() ) {
2015-05-06 15:55:11 +00:00
$data[ ] = [
'type' => $nodeHandler->getType(),
'data' => $nodeData,
'isEmpty' => $nodeHandler->isEmpty( $nodeData ),
'source' => $nodeHandler->getSource()
2015-05-06 15:55:11 +00:00
];
}
}
wfProfileOut(__METHOD__);
return $data;
}
/**
2015-05-18 15:08:53 +00:00
* @param $xmlString
* @return array
2015-05-18 15:08:53 +00:00
* @throws XmlMarkupParseErrorException
*/
2015-05-08 12:28:56 +00:00
public function getDataFromXmlString( $xmlString ) {
wfProfileIn( __METHOD__ );
2015-05-18 15:08:53 +00:00
2015-05-29 13:04:04 +00:00
$xml = $this->parseXmlString( $xmlString );
2015-05-08 12:10:25 +00:00
$data = $this->getDataFromNodes( $xml );
wfProfileOut( __METHOD__ );
return $data;
}
2015-05-29 13:04:04 +00:00
public function getInfoboxParams( $xmlString ) {
$xml = $this->parseXmlString( $xmlString );
$result = [];
foreach ( $xml->attributes() as $k => $v ) {
$result[$k] = (string) $v;
}
return $result;
}
protected function logXmlParseError( $level, $code, $message ) {
2015-05-19 10:49:35 +00:00
\Wikia\Logger\WikiaLogger::instance()->info( "PortableInfobox XML Parser problem", [
2015-05-19 14:01:53 +00:00
"level" => $level,
"code" => $code,
"message" => $message ] );
2015-05-19 10:49:35 +00:00
}
2015-05-29 13:04:04 +00:00
/**
* @param $xmlString
* @return \SimpleXMLElement
* @throws XmlMarkupParseErrorException
*/
protected function parseXmlString( $xmlString ) {
$global_libxml_setting = libxml_use_internal_errors();
libxml_use_internal_errors( true );
$xml = simplexml_load_string( $xmlString );
$errors = libxml_get_errors();
libxml_use_internal_errors( $global_libxml_setting );
if ( $xml === false ) {
foreach ( $errors as $xmlerror ) {
$this->logXmlParseError( $xmlerror->level, $xmlerror->code, trim( $xmlerror->message ) );
}
libxml_clear_errors();
throw new XmlMarkupParseErrorException();
}
return $xml;
}
}
2015-05-08 12:04:18 +00:00
class XmlMarkupParseErrorException extends \Exception {
2015-05-21 18:38:33 +00:00
}