jenkins-bot has submitted this change and it was merged.
Change subject: Introducing EntityIdLinkFormatter
......................................................................
Introducing EntityIdLinkFormatter
Change-Id: If9368d42acdd4771f601898fe8ebaaad70042bdf
---
M lib/WikibaseLib.classes.php
M lib/includes/formatters/EntityIdLabelFormatter.php
A lib/includes/formatters/EntityIdLinkFormatter.php
A lib/includes/formatters/EntityIdTitleFormatter.php
A lib/tests/phpunit/formatters/EntityIdLinkFormatterTest.php
A lib/tests/phpunit/formatters/EntityIdTitleFormatterTest.php
6 files changed, 267 insertions(+), 5 deletions(-)
Approvals:
Addshore: Looks good to me, approved
jenkins-bot: Verified
diff --git a/lib/WikibaseLib.classes.php b/lib/WikibaseLib.classes.php
index ca402ab..71345c5 100644
--- a/lib/WikibaseLib.classes.php
+++ b/lib/WikibaseLib.classes.php
@@ -95,6 +95,8 @@
'Wikibase\Lib\DispatchingValueFormatter' =>
'includes/formatters/DispatchingValueFormatter.php',
'Wikibase\Lib\EntityIdFormatter' =>
'includes/formatters/EntityIdFormatter.php',
'Wikibase\Lib\EntityIdLabelFormatter' =>
'includes/formatters/EntityIdLabelFormatter.php',
+ 'Wikibase\Lib\EntityIdTitleFormatter' =>
'includes/formatters/EntityIdTitleFormatter.php',
+ 'Wikibase\Lib\EntityIdLinkFormatter' =>
'includes/formatters/EntityIdLinkFormatter.php',
'Wikibase\Lib\MwTimeIsoFormatter' =>
'includes/formatters/MwTimeIsoFormatter.php',
'Wikibase\Lib\EscapingValueFormatter' =>
'includes/formatters/EscapingValueFormatter.php',
'Wikibase\Lib\FormattingException' =>
'includes/formatters/FormattingException.php',
diff --git a/lib/includes/formatters/EntityIdLabelFormatter.php
b/lib/includes/formatters/EntityIdLabelFormatter.php
index 9fda460..6b98b9f 100644
--- a/lib/includes/formatters/EntityIdLabelFormatter.php
+++ b/lib/includes/formatters/EntityIdLabelFormatter.php
@@ -14,18 +14,14 @@
/**
* @since 0.4
*
- * @file
- * @ingroup WikibaseLib
- *
* @licence GNU GPL v2+
* @author Jeroen De Dauw < [email protected] >
* @author Katie Filbert < [email protected] >
* @author Daniel Kinzler
*
- * @todo: once this is no longer used directly, rename it to
EntityIdValueFormatter
* @todo: add support for language fallback chains
*/
-class EntityIdLabelFormatter extends ValueFormatterBase {
+class EntityIdLabelFormatter extends EntityIdFormatter {
/**
* Whether we should try to find the label of the entity
diff --git a/lib/includes/formatters/EntityIdLinkFormatter.php
b/lib/includes/formatters/EntityIdLinkFormatter.php
new file mode 100644
index 0000000..b8f3463
--- /dev/null
+++ b/lib/includes/formatters/EntityIdLinkFormatter.php
@@ -0,0 +1,35 @@
+<?php
+
+namespace Wikibase\Lib;
+use InvalidArgumentException;
+use RuntimeException;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\EntityIdValue;
+
+/**
+ * Formats entity IDs by generating a wiki link to the corresponding page
title.
+ *
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ */
+class EntityIdLinkFormatter extends EntityIdTitleFormatter {
+
+ /**
+ * Format an EntityId data value
+ *
+ * @param EntityId|EntityIdValue $value The value to format
+ *
+ * @return string
+ *
+ * @throws InvalidArgumentException
+ */
+ public function format( $value ) {
+ $title = parent::format( $value );
+
+ return "[[$title]]";
+ }
+
+}
+
diff --git a/lib/includes/formatters/EntityIdTitleFormatter.php
b/lib/includes/formatters/EntityIdTitleFormatter.php
new file mode 100644
index 0000000..fa3fe93
--- /dev/null
+++ b/lib/includes/formatters/EntityIdTitleFormatter.php
@@ -0,0 +1,59 @@
+<?php
+
+namespace Wikibase\Lib;
+
+use InvalidArgumentException;
+use ValueFormatters\FormatterOptions;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\EntityIdValue;
+use Wikibase\EntityTitleLookup;
+
+/**
+ * Formats entity IDs by generating the corresponding page title.
+ *
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ */
+class EntityIdTitleFormatter extends EntityIdFormatter {
+
+ /**
+ * @var EntityTitleLookup
+ */
+ protected $titleLookup;
+
+ /**
+ * @param FormatterOptions $options
+ * @param EntityTitleLookup $titleLookup
+ */
+ public function __construct( FormatterOptions $options,
EntityTitleLookup $titleLookup ) {
+ parent::__construct( $options );
+
+ $this->titleLookup = $titleLookup;
+ }
+
+ /**
+ * Format an EntityId data value
+ *
+ * @param EntityId|EntityIdValue $value The value to format
+ *
+ * @return string
+ *
+ * @throws InvalidArgumentException
+ */
+ public function format( $value ) {
+ if ( $value instanceof EntityIdValue ) {
+ $value = $value->getEntityId();
+ }
+
+ if ( !( $value instanceof EntityId ) ) {
+ throw new InvalidArgumentException( 'Data value type
mismatch. Expected an EntityId or EntityIdValue.' );
+ }
+
+ $title = $this->titleLookup->getTitleForId( $value );
+ return $title->getFullText();
+ }
+
+}
+
diff --git a/lib/tests/phpunit/formatters/EntityIdLinkFormatterTest.php
b/lib/tests/phpunit/formatters/EntityIdLinkFormatterTest.php
new file mode 100644
index 0000000..487f4ec
--- /dev/null
+++ b/lib/tests/phpunit/formatters/EntityIdLinkFormatterTest.php
@@ -0,0 +1,85 @@
+<?php
+
+namespace Wikibase\Test;
+
+use Language;
+use LogicException;
+use Title;
+use ValueFormatters\FormatterOptions;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\EntityIdValue;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\Lib\EntityIdLinkFormatter;
+use Wikibase\Item;
+use Wikibase\LanguageFallbackChainFactory;
+use Wikibase\Property;
+
+/**
+ * @covers Wikibase\Lib\EntityIdLinkFormatter
+ *
+ * @since 0.4
+ *
+ * @group ValueFormatters
+ * @group DataValueExtensions
+ * @group WikibaseLib
+ * @group EntityIdFormatterTest
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ */
+class EntityIdLinkFormatterTest extends \PHPUnit_Framework_TestCase {
+
+ public function provideFormat() {
+ return array(
+ 'ItemId' => array(
+ new ItemId( 'Q23' ),
+ '[[ITEM-TEST--Q23]]'
+ ),
+ 'PropertyId' => array(
+ new PropertyId( 'P23' ),
+ '[[PROPERTY-TEST--P23]]'
+ ),
+ 'EntityId' => array(
+ new EntityId( Item::ENTITY_TYPE, 23 ),
+ '[[ITEM-TEST--Q23]]'
+ ),
+ 'EntityIdValue' => array(
+ new EntityIdValue( new ItemId( "Q23" ) ),
+ '[[ITEM-TEST--Q23]]'
+ ),
+ );
+ }
+
+ /**
+ * @dataProvider provideFormat
+ */
+ public function testFormat( $id, $expected ) {
+ $formatter = $this->newEntityIdLinkFormatter();
+
+ $actual = $formatter->format( $id );
+ $this->assertEquals( $expected, $actual );
+ }
+
+ public function getTitleForId( EntityId $id ) {
+ if ( $id->getEntityType() === Item::ENTITY_TYPE ) {
+ $name = 'ITEM-TEST--' . $id->getPrefixedId();
+ } elseif ( $id->getEntityType() === Property::ENTITY_TYPE ) {
+ $name = 'PROPERTY-TEST--' . $id->getPrefixedId();
+ } else {
+ throw new LogicException( "oops!" );
+ }
+
+ return Title::makeTitle( NS_MAIN, $name );
+ }
+
+ protected function newEntityIdLinkFormatter() {
+ $options = new FormatterOptions();
+ $titleLookup = $this->getMock( 'Wikibase\EntityTitleLookup' );
+ $titleLookup->expects( $this->any() )->method( 'getTitleForId' )
+ ->will( $this->returnCallback( array( $this,
'getTitleForId' ) ) );
+
+ $formatter = new EntityIdLinkFormatter( $options, $titleLookup
);
+ return $formatter;
+ }
+}
diff --git a/lib/tests/phpunit/formatters/EntityIdTitleFormatterTest.php
b/lib/tests/phpunit/formatters/EntityIdTitleFormatterTest.php
new file mode 100644
index 0000000..d67eaf4
--- /dev/null
+++ b/lib/tests/phpunit/formatters/EntityIdTitleFormatterTest.php
@@ -0,0 +1,85 @@
+<?php
+
+namespace Wikibase\Test;
+
+use Language;
+use LogicException;
+use Title;
+use ValueFormatters\FormatterOptions;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\EntityIdValue;
+use Wikibase\DataModel\Entity\ItemId;
+use Wikibase\DataModel\Entity\PropertyId;
+use Wikibase\Lib\EntityIdTitleFormatter;
+use Wikibase\Item;
+use Wikibase\LanguageFallbackChainFactory;
+use Wikibase\Property;
+
+/**
+ * @covers Wikibase\Lib\EntityIdTitleFormatter
+ *
+ * @since 0.4
+ *
+ * @group ValueFormatters
+ * @group DataValueExtensions
+ * @group WikibaseLib
+ * @group EntityIdFormatterTest
+ *
+ * @licence GNU GPL v2+
+ * @author Daniel Kinzler
+ */
+class EntityIdTitleFormatterTest extends \PHPUnit_Framework_TestCase {
+
+ public function provideFormat() {
+ return array(
+ 'ItemId' => array(
+ new ItemId( 'Q23' ),
+ 'ITEM-TEST--Q23'
+ ),
+ 'PropertyId' => array(
+ new PropertyId( 'P23' ),
+ 'PROPERTY-TEST--P23'
+ ),
+ 'EntityId' => array(
+ new EntityId( Item::ENTITY_TYPE, 23 ),
+ 'ITEM-TEST--Q23'
+ ),
+ 'EntityIdValue' => array(
+ new EntityIdValue( new ItemId( "Q23" ) ),
+ 'ITEM-TEST--Q23'
+ ),
+ );
+ }
+
+ /**
+ * @dataProvider provideFormat
+ */
+ public function testFormat( $id, $expected ) {
+ $formatter = $this->newEntityIdTitleFormatter();
+
+ $actual = $formatter->format( $id );
+ $this->assertEquals( $expected, $actual );
+ }
+
+ public function getTitleForId( EntityId $id ) {
+ if ( $id->getEntityType() === Item::ENTITY_TYPE ) {
+ $name = 'ITEM-TEST--' . $id->getPrefixedId();
+ } elseif ( $id->getEntityType() === Property::ENTITY_TYPE ) {
+ $name = 'PROPERTY-TEST--' . $id->getPrefixedId();
+ } else {
+ throw new LogicException( "oops!" );
+ }
+
+ return Title::makeTitle( NS_MAIN, $name );
+ }
+
+ protected function newEntityIdTitleFormatter() {
+ $options = new FormatterOptions();
+ $titleLookup = $this->getMock( 'Wikibase\EntityTitleLookup' );
+ $titleLookup->expects( $this->any() )->method( 'getTitleForId' )
+ ->will( $this->returnCallback( array( $this,
'getTitleForId' ) ) );
+
+ $formatter = new EntityIdTitleFormatter( $options, $titleLookup
);
+ return $formatter;
+ }
+}
--
To view, visit https://gerrit.wikimedia.org/r/88014
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: If9368d42acdd4771f601898fe8ebaaad70042bdf
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
Gerrit-Reviewer: Addshore <[email protected]>
Gerrit-Reviewer: Aude <[email protected]>
Gerrit-Reviewer: Jeroen De Dauw <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits