Daniel Kinzler has uploaded a new change for review.
https://gerrit.wikimedia.org/r/67432
Change subject: (bug 49241) Handle failures in {{#property}} gracefully.
......................................................................
(bug 49241) Handle failures in {{#property}} gracefully.
This catches any exception that ocurr while rendering the property,
and remembers them in the ParserOutput object to show them on
the finished page.
The parser function itself returns an empty string on failure.
Change-Id: I8cf39fd8f34d36dfdd90710c5fbd3337b7059df2
---
M client/WikibaseClient.hooks.php
M client/WikibaseClient.i18n.php
M client/includes/parserhooks/PropertyParserFunction.php
M lib/includes/SnakFormatter.php
4 files changed, 64 insertions(+), 28 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/32/67432/1
diff --git a/client/WikibaseClient.hooks.php b/client/WikibaseClient.hooks.php
index f237eec..2b71ecc 100644
--- a/client/WikibaseClient.hooks.php
+++ b/client/WikibaseClient.hooks.php
@@ -472,6 +472,16 @@
$out->setProperty( 'wikibase_item', $itemId );
}
+ $errors = $pout->getExtensionData(
'wikibase-property-render-errors' );
+
+ if ( !empty( $errors ) ) {
+ foreach ( $errors as $errorMessage ) {
+ $out->addWikiText(
+ '<p class="error">' . $errorMessage .
'</p>'
+ );
+ }
+ }
+
return true;
}
diff --git a/client/WikibaseClient.i18n.php b/client/WikibaseClient.i18n.php
index 8e684af..2b4c90a 100644
--- a/client/WikibaseClient.i18n.php
+++ b/client/WikibaseClient.i18n.php
@@ -67,6 +67,7 @@
'wikibase-unconnectedpages-format-row' => '($1
{{PLURAL:$1|interlanguagelink|interlanguagelinks}} on the page)',
'wikibase-pageinfo-entity-id' => 'Wikidata Item ID',
'wikibase-pageinfo-entity-id-none' => 'None',
+ 'wikibase-property-render-error' => 'Failed to render property $1: $2'
);
/** Message documentation (Message documentation)
@@ -174,6 +175,13 @@
'wikibase-pageinfo-entity-id' => 'A link to the corresponding Wikibase
Item',
'wikibase-pageinfo-entity-id-none' => 'The page is not linked with a
wikibase item.
{{Identical|None}}',
+
+ 'wikibase-property-render-error' => 'Error message shown when the
#property parser function fails to render a property value.
+
+Parameters:
+* $1 - the property ID or name
+* $2 - the original error message (this is typically in English and may be
rather technical)
+'
);
/** Afrikaans (Afrikaans)
diff --git a/client/includes/parserhooks/PropertyParserFunction.php
b/client/includes/parserhooks/PropertyParserFunction.php
index bd1a9e4..15ae867 100644
--- a/client/includes/parserhooks/PropertyParserFunction.php
+++ b/client/includes/parserhooks/PropertyParserFunction.php
@@ -123,30 +123,37 @@
* @param EntityId $entityId
* @param string $propertyLabel
*
- * @return string - wikitext format
+ * @return \Status a status object wrapping a wikitext string
*/
public function renderForEntityId( EntityId $entityId, $propertyLabel )
{
wfProfileIn( __METHOD__ );
- $entity = $this->entityLookup->getEntity( $entityId );
+ try {
+ $entity = $this->entityLookup->getEntity( $entityId );
- if ( !$entity ) {
- wfProfileOut( __METHOD__ );
- return '';
+ if ( !$entity ) {
+ wfProfileOut( __METHOD__ );
+ return \Status::newGood( '' );
+ }
+
+ $claims = $this->getClaimsForProperty( $entity,
$propertyLabel );
+
+ if ( $claims->isEmpty() ) {
+ wfProfileOut( __METHOD__ );
+ return \Status::newGood( '' );
+ }
+
+ $snakList = $claims->getMainSnaks();
+ $text = $this->formatSnakList( $snakList,
$propertyLabel );
+ $status = \Status::newGood( $text );
+ } catch ( \Exception $ex ) {
+ wfDebugLog( __CLASS__, __FUNCTION__ . ': ' .
$ex->getMessage() );
+
+ $status = \Status::newFatal(
'wikibase-property-render-error', $propertyLabel, $ex->getMessage() );
}
-
- $claims = $this->getClaimsForProperty( $entity, $propertyLabel
);
-
- if ( $claims->isEmpty() ) {
- wfProfileOut( __METHOD__ );
- return '';
- }
-
- $snakList = $claims->getMainSnaks();
- $text = $this->formatSnakList( $snakList, $propertyLabel );
wfProfileOut( __METHOD__ );
- return $text;
+ return $status;
}
/**
@@ -184,8 +191,26 @@
$entityLookup, $propertyLabelResolver,
$errorFormatter, $formatter );
+ $status = $instance->renderForEntityId( $entityId,
$propertyLabel );
+
+ if ( !$status->isGood() ) {
+ // stuff the error messages into the ParserOutput, so
we can render them later somewhere
+
+ $errors = $parser->getOutput()->getExtensionData(
'wikibase-property-render-errors' );
+ if ( $errors === null ) {
+ $errors = array();
+ }
+
+ //XXX: if Status sucked less, we'd could get an array
of Message objects
+ $errors[] = $status->getWikiText();
+
+ $parser->getOutput()->setExtensionData(
'wikibase-property-render-errors', $errors );
+ }
+
+ $text = $status->isOK() ? $status->getValue() : '';
+
$result = array(
- $instance->renderForEntityId( $entityId, $propertyLabel
),
+ $text,
'noparse' => false,
'nowiki' => true,
);
diff --git a/lib/includes/SnakFormatter.php b/lib/includes/SnakFormatter.php
index 1c29ab6..4ebe82b 100644
--- a/lib/includes/SnakFormatter.php
+++ b/lib/includes/SnakFormatter.php
@@ -2,6 +2,7 @@
namespace Wikibase\Lib;
+use DataTypes\DataType;
use DataTypes\DataTypeFactory;
use RuntimeException;
use Wikibase\EntityId;
@@ -90,18 +91,10 @@
}
private function formatPropertyValueSnak( PropertyValueSnak $snak,
$languageCode ) {
- try {
- $dataValue = $snak->getDataValue();
- $dataTypeId = $this->getDataTypeForProperty(
$snak->getPropertyId() );
+ $dataValue = $snak->getDataValue();
+ $dataTypeId = $this->getDataTypeForProperty(
$snak->getPropertyId() );
- return $this->typedValueFormatter->formatToString(
$dataValue, $dataTypeId, $languageCode );
- } catch ( PropertyNotFoundException $e ) {
- // @todo nicer error handling!
- wfDebugLog( __CLASS__, __METHOD__ . ': Property '
- . $snak->getPropertyId()->getPrefixedId() . '
not found.' );
- }
-
- return '';
+ return $this->typedValueFormatter->formatToString( $dataValue,
$dataTypeId, $languageCode );
}
private function getDataTypeForProperty( EntityId $propertyId ) {
--
To view, visit https://gerrit.wikimedia.org/r/67432
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I8cf39fd8f34d36dfdd90710c5fbd3337b7059df2
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits