PortableInfobox/includes/services/PortableInfoboxErrorRenderService.php

94 lines
2.6 KiB
PHP
Raw Normal View History

2015-05-29 14:37:35 +00:00
<?php
use PortableInfobox\Helpers\PortableInfoboxTemplateEngine;
class PortableInfoboxErrorRenderService {
2015-05-29 14:37:35 +00:00
2021-09-10 02:52:19 +00:00
private const XML_ERR_GENERAL = 'xml-parse-error';
2015-06-01 12:11:07 +00:00
2015-05-29 14:37:35 +00:00
private $errorList = [];
2015-06-03 12:37:12 +00:00
// For full list of possible errors, see:
// http://www.xmlsoft.org/html/libxml-xmlerror.html#xmlParserErrors
2015-06-01 12:11:07 +00:00
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'
];
2015-05-29 14:37:35 +00:00
2022-03-11 20:35:51 +00:00
/* @var $templateEngine PortableInfoboxTemplateEngine */
2015-05-29 14:37:35 +00:00
private $templateEngine;
public function __construct( $errorList ) {
$this->templateEngine = new PortableInfoboxTemplateEngine();
2015-05-29 14:37:35 +00:00
$this->errorList = $errorList;
}
public function renderMarkupDebugView( $sourceCode ) {
if ( count( $this->errorList ) ) {
$templateData = [];
2015-06-03 12:37:12 +00:00
$templateData['code'] = $this->getLinesWithErrors( $sourceCode );
$templateData['info'] = wfMessage( 'portable-infobox-xml-parse-error-info' )->text();
return $this->templateEngine->render( self::XML_ERR_GENERAL, $templateData );
2015-06-03 12:37:12 +00:00
}
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,
];
2018-08-16 09:25:53 +00:00
if ( isset( $errorsByLine[$i] ) ) {
2015-06-03 12:37:12 +00:00
$line['error'] = true;
2018-08-16 09:25:53 +00:00
foreach ( $errorsByLine[$i] as $error ) {
2015-06-03 12:37:12 +00:00
$line['error_messages'][] = [ 'message' => $this->getErrorMessage( $error ) ];
2015-06-02 11:09:25 +00:00
}
2015-05-29 14:37:35 +00:00
}
2015-06-03 12:37:12 +00:00
$lines[] = $line;
}
return $lines;
}
2015-05-29 14:37:35 +00:00
2015-06-03 12:37:12 +00:00
protected function getErrorsByLine( $errorList ) {
$errorsByLine = [];
foreach ( $errorList as $error ) {
$errorsByLine[ $error->line - 1 ][ $error->code ] = $error;
2015-05-29 14:37:35 +00:00
}
2015-06-03 12:37:12 +00:00
return $errorsByLine;
2015-05-29 14:37:35 +00:00
}
2015-06-02 11:09:25 +00:00
public function renderArticleMsgView() {
2015-06-03 12:37:12 +00:00
return wfMessage( 'portable-infobox-xml-parse-error-info' )->escaped();
2015-06-02 11:09:25 +00:00
}
2015-06-01 12:11:07 +00:00
2015-06-02 11:09:25 +00:00
public function getErrorMessage( libXMLError $error ) {
2015-06-01 12:11:07 +00:00
if ( isset( $this->supportedErrors[ $error->code ] ) ) {
$key = $this->supportedErrors[ $error->code ];
} else {
$key = self::XML_ERR_GENERAL;
}
2015-06-03 12:37:12 +00:00
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;
2015-05-29 14:37:35 +00:00
}
}