mirror of
https://github.com/Universal-Omega/PortableInfobox.git
synced 2024-12-04 20:59:08 +00:00
94 lines
2.6 KiB
PHP
94 lines
2.6 KiB
PHP
<?php
|
|
|
|
use PortableInfobox\Helpers\PortableInfoboxTemplateEngine;
|
|
|
|
class PortableInfoboxErrorRenderService {
|
|
|
|
const XML_ERR_GENERAL = 'xml-parse-error';
|
|
|
|
private $errorList = [];
|
|
|
|
// For full list of possible errors, see:
|
|
// http://www.xmlsoft.org/html/libxml-xmlerror.html#xmlParserErrors
|
|
private $supportedErrors = [
|
|
5 => 'XML_ERR_DOCUMENT_END',
|
|
26 => 'XML_ERR_UNDECLARED_ENTITY',
|
|
39 => 'XML_ERR_ATTRIBUTE_NOT_STARTED',
|
|
41 => 'XML_ERR_ATTRIBUTE_WITHOUT_VALUE',
|
|
65 => 'XML_ERR_SPACE_REQUIRED',
|
|
68 => 'XML_ERR_NAME_REQUIRED',
|
|
73 => 'XML_ERR_GT_REQUIRED',
|
|
76 => 'XML_ERR_TAG_NAME_MISMATCH',
|
|
77 => 'XML_ERR_TAG_NOT_FINISHED'
|
|
];
|
|
|
|
/* @var $templateEngine PortableInfobox\Helpers\PortableInfoboxTemplateEngine */
|
|
private $templateEngine;
|
|
|
|
public function __construct( $errorList ) {
|
|
$this->templateEngine = new PortableInfoboxTemplateEngine();
|
|
$this->errorList = $errorList;
|
|
}
|
|
|
|
public function renderMarkupDebugView( $sourceCode ) {
|
|
if ( count( $this->errorList ) ) {
|
|
$templateData = [];
|
|
$templateData['code'] = $this->getLinesWithErrors( $sourceCode );
|
|
$templateData['info'] = wfMessage( 'portable-infobox-xml-parse-error-info' )->text();
|
|
return $this->templateEngine->render( self::XML_ERR_GENERAL, $templateData );
|
|
}
|
|
return false;
|
|
}
|
|
|
|
protected function getLinesWithErrors( $sourceCode ) {
|
|
$lines = [];
|
|
|
|
$errorsByLine = $this->getErrorsByLine( $this->errorList );
|
|
$sourceCodeByLines = explode( "\n", ( $sourceCode ) );
|
|
|
|
foreach ( $sourceCodeByLines as $i => $codeLine ) {
|
|
$line = [
|
|
'line' => $i,
|
|
'codeLine' => $codeLine,
|
|
'error' => false,
|
|
];
|
|
if ( isset( $errorsByLine[$i] ) ) {
|
|
$line['error'] = true;
|
|
foreach ( $errorsByLine[$i] as $error ) {
|
|
$line['error_messages'][] = [ 'message' => $this->getErrorMessage( $error ) ];
|
|
}
|
|
}
|
|
$lines[] = $line;
|
|
}
|
|
return $lines;
|
|
}
|
|
|
|
protected function getErrorsByLine( $errorList ) {
|
|
$errorsByLine = [];
|
|
foreach ( $errorList as $error ) {
|
|
$errorsByLine[ $error->line - 1 ][ $error->code ] = $error;
|
|
}
|
|
return $errorsByLine;
|
|
}
|
|
|
|
public function renderArticleMsgView() {
|
|
return wfMessage( 'portable-infobox-xml-parse-error-info' )->escaped();
|
|
}
|
|
|
|
public function getErrorMessage( libXMLError $error ) {
|
|
if ( isset( $this->supportedErrors[ $error->code ] ) ) {
|
|
$key = $this->supportedErrors[ $error->code ];
|
|
} else {
|
|
$key = self::XML_ERR_GENERAL;
|
|
}
|
|
return wfMessage( $this->convertConstNameToMsgKey( $key ) )->escaped();
|
|
}
|
|
|
|
private function convertConstNameToMsgKey( $name ) {
|
|
$msg = str_replace( 'XML_ERR', 'xml_parse_error', $name );
|
|
$msg = str_replace( '_', '-', strtolower( $msg ) );
|
|
return 'portable-infobox-' . $msg;
|
|
}
|
|
|
|
}
|