Daniel Kinzler has uploaded a new change for review.
https://gerrit.wikimedia.org/r/84987
Change subject: Use SnakFormatter for HTML output.
......................................................................
Use SnakFormatter for HTML output.
Change-Id: Iadc01fd83de8337b72394d6a6e3ef67b8f2d8fd0
---
M repo/includes/EntityView.php
M repo/includes/actions/ViewEntityAction.php
M repo/includes/content/EntityContent.php
M repo/includes/specials/SpecialItemResolver.php
M repo/tests/phpunit/includes/EntityViewTest.php
M repo/tests/phpunit/includes/ItemViewTest.php
6 files changed, 103 insertions(+), 47 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/87/84987/1
diff --git a/repo/includes/EntityView.php b/repo/includes/EntityView.php
index 1a6a693..00e14c3 100644
--- a/repo/includes/EntityView.php
+++ b/repo/includes/EntityView.php
@@ -12,13 +12,14 @@
use MediaWikiSite;
use MWException;
use FormatJson;
+use ValueParsers\FormattingException;
use Wikibase\Lib\EntityIdFormatter;
use Wikibase\Lib\PropertyDataTypeLookup;
+use Wikibase\Lib\PropertyNotFoundException;
use Wikibase\Lib\Serializers\EntitySerializationOptions;
use Wikibase\Lib\Serializers\SerializerFactory;
+use Wikibase\Lib\SnakFormatter;
use Wikibase\Serializers\FetchedEntityContentSerializer;
-use Wikibase\Serializers\FetchedEntityContentSerializerOptions;
-use ValueFormatters\ValueFormatterFactory;
use ValueFormatters\FormatterOptions;
use ValueFormatters\ValueFormatter;
use ValueFormatters\TimeFormatter;
@@ -63,9 +64,9 @@
/**
* @since 0.4
*
- * @var ValueFormatterFactory
+ * @var SnakFormatter
*/
- protected $valueFormatters;
+ protected $snakFormatter;
/**
* @var EntityIdFormatter
@@ -107,7 +108,7 @@
* @since 0.1
*
* @param IContextSource|null $context
- * @param ValueFormatterFactory $valueFormatters
+ * @param SnakFormatter $snakFormatter
* @param Lib\PropertyDataTypeLookup $dataTypeLookup
* @param EntityLookup $entityLookup
* @param EntityTitleLookup $entityTitleLookup
@@ -115,14 +116,20 @@
*/
public function __construct(
IContextSource $context,
- ValueFormatterFactory $valueFormatters,
+ SnakFormatter $snakFormatter,
PropertyDataTypeLookup $dataTypeLookup,
EntityLookup $entityLookup,
EntityTitleLookup $entityTitleLookup,
EntityIdFormatter $idFormatter
) {
+ if ( $snakFormatter->getFormat() !== SnakFormatter::FORMAT_HTML
+ && $snakFormatter->getFormat() !==
SnakFormatter::FORMAT_HTML_WIDGET ) {
+ throw new \InvalidArgumentException( '$snakFormatter is
expected to return text/html, not '
+ . $snakFormatter->getFormat() );
+ }
+
$this->setContext( $context );
- $this->valueFormatters = $valueFormatters;
+ $this->snakFormatter = $snakFormatter;
$this->dataTypeLookup = $dataTypeLookup;
$this->entityLookup = $entityLookup;
$this->entityTitleLookup = $entityTitleLookup;
@@ -634,30 +641,12 @@
),
) );
- // TODO: display a "placeholder" message for novalue/somevalue
snak
- $value = '';
- if ( $claim->getMainSnak()->getType() === 'value' ) {
- $value = $claim->getMainSnak()->getDataValue();
-
- $valueFormatter = $this->valueFormatters->newFormatter(
- $value->getType(), $valueFormatterOptions
- );
-
- if ( $valueFormatter !== null ) {
- $value = $valueFormatter->format( $value );
- } else {
- // If value representation is a string, just
display that one as a
- // fallback for values not having a formatter
implemented yet.
- if ( is_string( $value->getValue() ) ) {
- $value = $value->getValue();
- } elseif ( $value instanceof
\DataValues\UnDeserializableValue ) {
- $value = $value->getReason();
- } else {
- // TODO: don't fail here, display a
message in the UI instead
- throw new MWException( 'Displaying of
values of type "'
- . $value->getType() . '" not
supported yet' );
- }
- }
+ try {
+ $snakValueHtml = $this->snakFormatter->formatSnak(
$claim->getMainSnak() );
+ } catch ( FormattingException $ex ) {
+ $snakValueHtml = '?'; // XXX: perhaps show error
message?
+ } catch ( PropertyNotFoundException $ex ) {
+ $snakValueHtml = '?'; // XXX: perhaps show error
message?
}
$mainSnakHtml = wfTemplate( 'wb-snak',
@@ -666,7 +655,7 @@
// Claim group level) If this was a public
function, this should be generated
// anyhow since important when displaying a
Claim on its own.
'', // type selector, JS only
- ( $value === '' ) ? ' ' : htmlspecialchars( $value
)
+ ( $snakValueHtml === '' ) ? ' ' : $snakValueHtml
);
// TODO: Use 'wb-claim' or 'wb-statement' template accordingly
@@ -917,7 +906,7 @@
* @since 0.2
*
* @param EntityContent $entity
- * @param ValueFormatterFactory $valueFormatters
+ * @param SnakFormatter $snakFormatter
* @param Lib\PropertyDataTypeLookup $dataTypeLookup
* @param EntityLookup $entityLookup
* @param IContextSource|null $context
@@ -927,7 +916,7 @@
*/
public static function newForEntityContent(
EntityContent $entity,
- ValueFormatterFactory $valueFormatters,
+ SnakFormatter $snakFormatter,
PropertyDataTypeLookup $dataTypeLookup,
EntityLookup $entityLookup,
IContextSource $context = null
@@ -945,7 +934,7 @@
$idFormatter =
WikibaseRepo::getDefaultInstance()->getIdFormatter();
$entityTitleLookup = EntityContentFactory::singleton();
- $instance = new self::$typeMap[ $type ]( $context,
$valueFormatters, $dataTypeLookup, $entityLookup, $entityTitleLookup,
$idFormatter );
+ $instance = new self::$typeMap[ $type ]( $context,
$snakFormatter, $dataTypeLookup, $entityLookup, $entityTitleLookup,
$idFormatter );
return $instance;
}
}
diff --git a/repo/includes/actions/ViewEntityAction.php
b/repo/includes/actions/ViewEntityAction.php
index 1e54ebc..ccc17ef 100644
--- a/repo/includes/actions/ViewEntityAction.php
+++ b/repo/includes/actions/ViewEntityAction.php
@@ -2,6 +2,9 @@
namespace Wikibase;
use Language, Article, \ValueFormatters\ValueFormatterFactory;
+use ValueFormatters\FormatterOptions;
+use ValueFormatters\ValueFormatter;
+use Wikibase\Lib\SnakFormatter;
use Wikibase\Repo\WikibaseRepo;
/**
@@ -155,13 +158,18 @@
$this->displayEntityContent( $content );
- $valueFormatters = new ValueFormatterFactory(
$GLOBALS['wgValueFormatters'] );
+ $formatterOptions = new FormatterOptions(); //TODO:
Language Fallback
+ $formatterOptions->setOption( ValueFormatter::OPT_LANG,
$this->getContext()->getLanguage()->getCode() );
+
+ $snakFormatter =
WikibaseRepo::getDefaultInstance()->getSnakFormatterFactory()
+ ->getFormatter(
SnakFormatter::FORMAT_HTML_WIDGET, $formatterOptions );
+
$dataTypeLookup =
WikibaseRepo::getDefaultInstance()->getPropertyDataTypeLookup();
$entityLoader =
WikibaseRepo::getDefaultInstance()->getStore()->getEntityLookup();
$isEditableView = $this->isPlainView();
- $view = EntityView::newForEntityContent( $content,
$valueFormatters, $dataTypeLookup, $entityLoader );
+ $view = EntityView::newForEntityContent( $content,
$snakFormatter, $dataTypeLookup, $entityLoader );
$view->registerJsConfigVars(
$this->getOutput(),
$content,
diff --git a/repo/includes/content/EntityContent.php
b/repo/includes/content/EntityContent.php
index 1d2769f..f083177 100644
--- a/repo/includes/content/EntityContent.php
+++ b/repo/includes/content/EntityContent.php
@@ -3,6 +3,10 @@
namespace Wikibase;
use ParserOutput;
+use ValueFormatters\FormatterOptions;
+use ValueFormatters\Test\FormatterOptionsTest;
+use ValueFormatters\ValueFormatter;
+use Wikibase\Lib\SnakFormatter;
use WikiPage, Title, User, Status, ParserOptions;
use \ValueFormatters\ValueFormatterFactory;
use Wikibase\Repo\WikibaseRepo;
@@ -121,11 +125,20 @@
* @return ParserOutput
*/
public function getParserOutput( Title $title, $revId = null,
ParserOptions $options = null, $generateHtml = true ) {
- $valueFormatters = new ValueFormatterFactory(
$GLOBALS['wgValueFormatters'] );
+ $formatterOptions = new FormatterOptions(); //TODO: Language
Fallback, etc
+
+ if ( $options !== null ) {
+ $lang = $options->getUserLang(); //XXX: the right
language?!
+ $formatterOptions->setOption( ValueFormatter::OPT_LANG,
$lang );
+ }
+
+ $snakFormatter =
WikibaseRepo::getDefaultInstance()->getSnakFormatterFactory()
+ ->getFormatter( SnakFormatter::FORMAT_HTML_WIDGET,
$formatterOptions );
+
$dataTypeLookup =
WikibaseRepo::getDefaultInstance()->getPropertyDataTypeLookup();
$entityLoader =
WikibaseRepo::getDefaultInstance()->getStore()->getEntityLookup();
- $entityView = EntityView::newForEntityContent( $this,
$valueFormatters, $dataTypeLookup, $entityLoader );
+ $entityView = EntityView::newForEntityContent( $this,
$snakFormatter, $dataTypeLookup, $entityLoader );
return $entityView->getParserOutput( $this, $options,
$generateHtml );
}
diff --git a/repo/includes/specials/SpecialItemResolver.php
b/repo/includes/specials/SpecialItemResolver.php
index ef8ad73..2e328fb 100644
--- a/repo/includes/specials/SpecialItemResolver.php
+++ b/repo/includes/specials/SpecialItemResolver.php
@@ -2,10 +2,14 @@
namespace Wikibase\Repo\Specials;
+use ValueFormatters\FormatterOptions;
+use ValueFormatters\ValueFormatter;
use \ValueFormatters\ValueFormatterFactory;
use Wikibase\EntityView;
use Wikibase\ItemContent;
+use Wikibase\Lib\SnakFormatter;
use Wikibase\Lib\Specials\SpecialWikibasePage;
+use Wikibase\Repo\WikibaseRepo;
/**
* Base for special pages that resolve certain arguments to an item.
@@ -87,9 +91,16 @@
* @param ItemContent $itemContent
*/
protected function displayItem( ItemContent $itemContent ) {
- $valueFormatters = new ValueFormatterFactory(
$GLOBALS['wgValueFormatters'] );
+ $formatterOptions = new FormatterOptions(); //TODO: Language
Fallback
+ $formatterOptions->setOption( ValueFormatter::OPT_LANG,
$this->getContext()->getLanguage()->getCode() );
- $view = EntityView::newForEntityContent( $itemContent,
$valueFormatters, $this->getContext() );
+ $snakFormatter =
WikibaseRepo::getDefaultInstance()->getSnakFormatterFactory()
+ ->getFormatter( SnakFormatter::FORMAT_HTML_WIDGET,
$formatterOptions );
+
+ $dataTypeLookup =
WikibaseRepo::getDefaultInstance()->getPropertyDataTypeLookup();
+ $entityLoader =
WikibaseRepo::getDefaultInstance()->getStore()->getEntityLookup();
+
+ $view = EntityView::newForEntityContent( $itemContent,
$snakFormatter, $dataTypeLookup, $entityLoader );
$view->render( $itemContent );
$this->getOutput()->setPageTitle(
$itemContent->getItem()->getLabel( $this->getLanguage()->getCode() ) );
diff --git a/repo/tests/phpunit/includes/EntityViewTest.php
b/repo/tests/phpunit/includes/EntityViewTest.php
index 05c1465..04df8c3 100644
--- a/repo/tests/phpunit/includes/EntityViewTest.php
+++ b/repo/tests/phpunit/includes/EntityViewTest.php
@@ -15,6 +15,7 @@
use Wikibase\Item;
use Wikibase\ItemContent;
use Wikibase\Lib\InMemoryDataTypeLookup;
+use Wikibase\Lib\SnakFormatter;
use Wikibase\Property;
use Wikibase\PropertyContent;
use Wikibase\PropertyNoValueSnak;
@@ -43,8 +44,25 @@
*/
class EntityViewTest extends \PHPUnit_Framework_TestCase {
+ /**
+ * @return SnakFormatter
+ */
+ protected function newSnakFormatterMock() {
+ $snakFormatter = $this->getMock( 'Wikibase\Lib\SnakFormatter' );
+
+ $snakFormatter->expects( $this->any() )->method( 'formatSnak' )
+ ->will( $this->returnValue( '(value)' ) );
+
+ $snakFormatter->expects( $this->any() )->method( 'getFormat' )
+ ->will( $this->returnValue(
SnakFormatter::FORMAT_HTML_WIDGET ) );
+
+ $snakFormatter->expects( $this->any() )->method(
'canFormatSnak' )
+ ->will( $this->returnValue( true ) );
+
+ return $snakFormatter;
+ }
+
protected function newEntityView( EntityContent $entityContent ) {
- $valueFormatters = new ValueFormatterFactory( array() );
$entityLoader = new MockRepository();
$p11 = new PropertyId( 'p11' );
@@ -61,7 +79,7 @@
$entityView = EntityView::newForEntityContent(
$entityContent,
- $valueFormatters,
+ $this->newSnakFormatterMock(),
$dataTypeLookup,
$entityLoader
);
@@ -254,12 +272,11 @@
* @dataProvider providerNewForEntityContent
*/
public function testNewForEntityContent( EntityContent $entityContent )
{
- $valueFormatters = new ValueFormatterFactory( array() );
$dataTypeLookup = new InMemoryDataTypeLookup( array() );
$entityLoader = new MockRepository();
// test whether we get the right EntityView from an
EntityContent
- $view = EntityView::newForEntityContent( $entityContent,
$valueFormatters, $dataTypeLookup, $entityLoader );
+ $view = EntityView::newForEntityContent( $entityContent,
$this->newSnakFormatterMock(), $dataTypeLookup, $entityLoader );
$this->assertInstanceOf(
EntityView::$typeMap[
$entityContent->getEntity()->getType() ],
$view
diff --git a/repo/tests/phpunit/includes/ItemViewTest.php
b/repo/tests/phpunit/includes/ItemViewTest.php
index d65a49c..8045ac0 100644
--- a/repo/tests/phpunit/includes/ItemViewTest.php
+++ b/repo/tests/phpunit/includes/ItemViewTest.php
@@ -5,6 +5,7 @@
use Wikibase\ItemContent;
use Wikibase\Item;
use Wikibase\Lib\InMemoryDataTypeLookup;
+use Wikibase\Lib\SnakFormatter;
use Wikibase\Utils;
use Wikibase\ItemView;
use ValueFormatters\ValueFormatterFactory;
@@ -51,15 +52,32 @@
class ItemViewTest extends \MediaWikiTestCase {
/**
+ * @return SnakFormatter
+ */
+ protected function newSnakFormatterMock() {
+ $snakFormatter = $this->getMock( 'Wikibase\Lib\SnakFormatter' );
+
+ $snakFormatter->expects( $this->any() )->method( 'formatSnak' )
+ ->will( $this->returnValue( '(value)' ) );
+
+ $snakFormatter->expects( $this->any() )->method( 'getFormat' )
+ ->will( $this->returnValue(
SnakFormatter::FORMAT_HTML_WIDGET ) );
+
+ $snakFormatter->expects( $this->any() )->method(
'canFormatSnak' )
+ ->will( $this->returnValue( true ) );
+
+ return $snakFormatter;
+ }
+
+ /**
* @dataProvider providerNewForEntityContent
*/
public function testNewForEntityContent( $entityContent ) {
- $valueFormatters = new ValueFormatterFactory( array() );
$entityLoader = new MockRepository();
$dataTypeLookup = new InMemoryDataTypeLookup();
// test whether we get the right EntityView from an
EntityContent
- $view = ItemView::newForEntityContent( $entityContent,
$valueFormatters, $dataTypeLookup, $entityLoader );
+ $view = ItemView::newForEntityContent( $entityContent,
$this->newSnakFormatterMock(), $dataTypeLookup, $entityLoader );
$this->assertType(
ItemView::$typeMap[
$entityContent->getEntity()->getType() ],
--
To view, visit https://gerrit.wikimedia.org/r/84987
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Iadc01fd83de8337b72394d6a6e3ef67b8f2d8fd0
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