messages and CR remark

This commit is contained in:
jacek 2015-06-03 14:37:12 +02:00
parent c7b5e9345c
commit a8e11a41bd
3 changed files with 70 additions and 52 deletions

View file

@ -4,32 +4,32 @@ $messages = [];
$messages[ 'en' ] = [
'portable-infobox-desc' => 'Create portable infoboxes which can be rendered using clean semantic HTML markup on
any skin / platform using using easy to understand powerful XML-like markup',
'unimplemented-infobox-tag' => 'Unimplemented infobox tag: <$1>',
'xml-parse-error-info' => 'There is a problem with parsing the infobox:',
'xml-parse-error' => 'Incorrect XML markup. Please validate your XML',
'xml_err_document_end' => 'Extra content at the end of the document',
'xml_err_undeclared_entity' => 'Undeclared entity (for example: &nbsp;)',
'xml_err_attribute_not_started' => 'Attribute value should be inside quotation marks',
'xml_err_attribute_without_value' => 'Tag attribute needs to have value',
'xml_err_space_required' => 'Attributes construct error',
'xml_err_name_required' => 'Error parsing attribute name',
'xml_err_gt_required' => "Tag definition is not properly terminated",
'xml_err_tag_name_mismatch' => 'Opening and ending tag mismatch',
'xml_err_tag_not_finished' => 'Premature end of tag'
'portable-infobox-unimplemented-infobox-tag' => 'Unimplemented infobox tag: <$1>',
'portable-infobox-xml-parse-error-info' => 'There is a problem with parsing the infobox',
'portable-infobox-xml-parse-error' => 'Incorrect XML markup. Please validate your XML',
'portable-infobox-xml-parse-error-document-end' => 'Extra content at the end of the document',
'portable-infobox-xml-parse-error-undeclared-entity' => 'Undeclared entity (for example: &nbsp;)',
'portable-infobox-xml-parse-error-attribute-not-started' => 'Attribute value should be inside quotation marks',
'portable-infobox-xml-parse-error-attribute-without-value' => 'Tag attribute needs to have value',
'portable-infobox-xml-parse-error-space-required' => 'Attributes construct error',
'portable-infobox-xml-parse-error-name-required' => 'Error parsing attribute name',
'portable-infobox-xml-parse-error-gt-required' => "Tag definition is not properly terminated",
'portable-infobox-xml-parse-error-tag-name-mismatch' => 'Opening and ending tag mismatch',
'portable-infobox-xml-parse-error-tag-not-finished' => 'Premature end of tag'
];
$messages[ 'qqq' ] = [
'portable-infobox-desc' => 'Portable Infobox extension description',
'unimplemented-infobox-tag' => 'Error message for using unimplemented infobox tag; $1 is the tag name without pointy braces',
'xml-parse-error-info' => 'General message for parsing problem',
'xml-parse-error' => 'Error message for providing incorrect XML markup',
'xml_err_document_end' => 'XML Error: extra content at the end',
'xml_err_undeclared_entity' => 'XML Error: undeclared entity (for example: &nbsp;). This typically comes from HTML.',
'xml_err_attribute_not_started' => 'XML Error: ending tag without start tag',
'xml_err_attribute_without_value' => 'XML Error: tag attribute was provided without value',
'xml_err_space_required' => 'XML Error: issue with parsing tag attributes',
'xml_err_name_required' => 'XML Error: error parsing name attribute',
'xml_err_gt_required' => 'XML Error: could not find end of tag definition (for example: <data><label </data>',
'xml_err_tag_name_mismatch' => 'XML Error: Opening and ending tag mismatch (for example: <data></label>)',
'xml_err_tag_not_finished' => 'XML Error: premature end of tag'
'portable-infobox-unimplemented-infobox-tag' => 'Error message for using unimplemented infobox tag; $1 is the tag name without pointy braces',
'portable-infobox-xml-parse-error-info' => 'General message for parsing problem',
'portable-infobox-xml-parse-error' => 'Error message for providing incorrect XML markup',
'portable-infobox-xml-parse-error-document-end' => 'XML Error: extra content at the end',
'portable-infobox-xml-parse-error-undeclared-entity' => 'XML Error: undeclared entity (for example: &nbsp;). This typically comes from HTML.',
'portable-infobox-xml-parse-error-attribute-not-started' => 'XML Error: ending tag without start tag',
'portable-infobox-xml-parse-error-attribute-without-value' => 'XML Error: tag attribute was provided without value',
'portable-infobox-xml-parse-error-space-required' => 'XML Error: issue with parsing tag attributes',
'portable-infobox-xml-parse-error-name-required' => 'XML Error: error parsing name attribute',
'portable-infobox-xml-parse-error-gt-required' => 'XML Error: could not find end of tag definition (for example: <data><label </data>',
'portable-infobox-xml-parse-error-tag-name-mismatch' => 'XML Error: Opening and ending tag mismatch (for example: <data></label>)',
'portable-infobox-xml-parse-error-tag-not-finished' => 'XML Error: premature end of tag'
];

View file

@ -62,7 +62,7 @@ class PortableInfoboxParserTagController extends WikiaController {
try {
$data = $infoboxParser->getDataFromXmlString( $markup );
} catch ( \Wikia\PortableInfobox\Parser\Nodes\UnimplementedNodeException $e ) {
return $this->handleError( wfMessage( 'unimplemented-infobox-tag', [ $e->getMessage() ] )->escaped() );
return $this->handleError( wfMessage( 'portable-infobox-unimplemented-infobox-tag', [ $e->getMessage() ] )->escaped() );
} catch ( \Wikia\PortableInfobox\Parser\XmlMarkupParseErrorException $e ) {
return $this->handleXmlParseError( $infoboxParser->getXmlParseErrors(), $text );
}
@ -102,7 +102,7 @@ class PortableInfoboxParserTagController extends WikiaController {
private function handleXmlParseError( $errors, $xmlMarkup ) {
$errorRenderer = new PortableInfoboxErrorRenderService( $errors );
if ( $this->wg->Title->getNamespace() == NS_TEMPLATE ) {
if ( $this->wg->Title && $this->wg->Title->getNamespace() == NS_TEMPLATE ) {
$renderedValue = $errorRenderer->renderMarkupDebugView( $xmlMarkup );
} else {
$renderedValue = $errorRenderer->renderArticleMsgView();

View file

@ -6,6 +6,9 @@ class PortableInfoboxErrorRenderService extends WikiaService {
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',
@ -29,39 +32,48 @@ class PortableInfoboxErrorRenderService extends WikiaService {
public function renderMarkupDebugView( $sourceCode ) {
if ( count( $this->errorList ) ) {
$errorsByLine = [];
foreach ( $this->errorList as $error ) {
$errorsByLine[ $error->line - 1 ][ $error->code ] = $error;
}
$sourceCodeByLines = explode( "\n", ( $sourceCode ) );
$templateData = [];
$templateData['code'] = [];
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 ) ];
}
}
$templateData['code'][] = $line;
}
$templateData['info'] = wfMessage( 'xml-parse-error-info' )->escaped();
$templateData['code'] = $this->getLinesWithErrors( $sourceCode );
$templateData['info'] = wfMessage( 'portable-infobox-xml-parse-error-info' )->escaped();
return $this->templateEngine->clearData()->setData( $templateData )->render( self::XML_DEBUG_TEMPLATE );
}
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( 'xml-parse-error-info' )->escaped();
return wfMessage( 'portable-infobox-xml-parse-error-info' )->escaped();
}
public function getErrorMessage( libXMLError $error ) {
@ -70,7 +82,13 @@ class PortableInfoboxErrorRenderService extends WikiaService {
} else {
$key = self::XML_ERR_GENERAL;
}
return wfMessage( strtolower( $key ) )->escaped();
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;
}
}