2015-04-27 14:05:31 +00:00
|
|
|
<?php
|
|
|
|
namespace Wikia\PortableInfobox\Parser;
|
|
|
|
|
2015-06-10 15:19:40 +00:00
|
|
|
use Wikia\Logger\WikiaLogger;
|
2015-04-27 14:05:31 +00:00
|
|
|
|
2015-06-10 15:19:40 +00:00
|
|
|
class XmlParser {
|
2015-05-29 13:04:04 +00:00
|
|
|
/**
|
2015-06-10 15:19:40 +00:00
|
|
|
* @param string $xmlString XML to parse
|
|
|
|
*
|
|
|
|
* @param array $errors this array will be filled with errors if any found
|
|
|
|
*
|
2015-05-29 13:04:04 +00:00
|
|
|
* @return \SimpleXMLElement
|
|
|
|
* @throws XmlMarkupParseErrorException
|
|
|
|
*/
|
2015-06-10 15:19:40 +00:00
|
|
|
public static function parseXmlString( $xmlString, &$errors = [ ] ) {
|
2015-05-29 13:04:04 +00:00
|
|
|
$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 ) {
|
2015-06-10 15:19:40 +00:00
|
|
|
self::logXmlParseError( $xmlerror->level, $xmlerror->code, trim( $xmlerror->message ) );
|
2015-05-29 13:04:04 +00:00
|
|
|
}
|
|
|
|
libxml_clear_errors();
|
2015-06-19 12:43:40 +00:00
|
|
|
throw new XmlMarkupParseErrorException( $errors );
|
2015-05-29 13:04:04 +00:00
|
|
|
}
|
2015-06-10 15:19:40 +00:00
|
|
|
|
2015-05-29 13:04:04 +00:00
|
|
|
return $xml;
|
|
|
|
}
|
|
|
|
|
2015-06-10 15:19:40 +00:00
|
|
|
protected static function logXmlParseError( $level, $code, $message ) {
|
|
|
|
WikiaLogger::instance()->info( "PortableInfobox XML Parser problem", [
|
|
|
|
"level" => $level,
|
|
|
|
"code" => $code,
|
|
|
|
"message" => $message ] );
|
|
|
|
}
|
|
|
|
|
2015-04-27 14:05:31 +00:00
|
|
|
}
|
2015-05-08 12:04:18 +00:00
|
|
|
|
|
|
|
class XmlMarkupParseErrorException extends \Exception {
|
2015-06-10 12:51:07 +00:00
|
|
|
private $errors;
|
|
|
|
|
|
|
|
public function __construct( $errors ) {
|
|
|
|
$this->errors = $errors;
|
|
|
|
return parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getErrors() {
|
|
|
|
return $this->errors;
|
|
|
|
}
|
2015-05-21 18:38:33 +00:00
|
|
|
}
|