Addshore has submitted this change and it was merged.

Change subject: Implement rendering item links as HTML.
......................................................................


Implement rendering item links as HTML.

Change-Id: I2da16e594d8d9fc687be267c62ae52308df052da
---
A lib/includes/formatters/EntityIdHtmlLinkFormatter.php
M lib/includes/formatters/EntityIdLabelFormatter.php
M lib/includes/formatters/WikibaseValueFormatterBuilders.php
M lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php
M lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
M repo/tests/phpunit/includes/api/FormatSnakValueTest.php
6 files changed, 112 insertions(+), 24 deletions(-)

Approvals:
  Addshore: Verified; Looks good to me, approved



diff --git a/lib/includes/formatters/EntityIdHtmlLinkFormatter.php 
b/lib/includes/formatters/EntityIdHtmlLinkFormatter.php
new file mode 100644
index 0000000..4b70895
--- /dev/null
+++ b/lib/includes/formatters/EntityIdHtmlLinkFormatter.php
@@ -0,0 +1,43 @@
+<?php
+
+namespace Wikibase\Lib;
+
+use InvalidArgumentException;
+use Html;
+use Title;
+use Wikibase\DataModel\Entity\EntityId;
+use Wikibase\DataModel\Entity\EntityIdValue;
+
+/**
+ * Formats entity IDs by generating an html link to the corresponding page 
title.
+ *
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Adrian Lang
+ */
+class EntityIdHtmlLinkFormatter extends EntityIdLabelFormatter {
+
+       /**
+        * Format an EntityId data value
+        *
+        * @param EntityId|EntityIdValue $value The value to format
+        *
+        * @return string
+        *
+        * @throws InvalidArgumentException
+        */
+       public function format( $value ) {
+               $value = $this->unwrapEntityId( $value );
+
+               $title = parent::format( $value );
+
+               $attributes = array(
+                       'href' => Title::newFromText( $value->getPrefixedId() 
)->getLocalUrl()
+               );
+
+               $html = Html::element( 'a', $attributes, $title );
+
+               return $html;
+       }
+}
diff --git a/lib/includes/formatters/EntityIdLabelFormatter.php 
b/lib/includes/formatters/EntityIdLabelFormatter.php
index 28bd6ea..1941488 100644
--- a/lib/includes/formatters/EntityIdLabelFormatter.php
+++ b/lib/includes/formatters/EntityIdLabelFormatter.php
@@ -89,15 +89,9 @@
         * @throws InvalidArgumentException
         */
        public function format( $value ) {
-               if ( $value instanceof EntityIdValue ) {
-                       $value = $value->getEntityId();
-               }
+               $value = $this->unwrapEntityId( $value );
 
-               if ( !( $value instanceof EntityId ) ) {
-                       throw new InvalidArgumentException( 'Data value type 
mismatch. Expected an EntityId or EntityIdValue.' );
-               }
-
-               if ( $this->getOption( self::OPT_RESOLVE_ID )  ) {
+               if ( $this->getOption( self::OPT_RESOLVE_ID ) ) {
                        $label = $this->lookupItemLabel( $value );
                } else {
                        $label = false;
@@ -121,6 +115,28 @@
        }
 
        /**
+        * Unwrap an EntityId value which might be wrapped in an EntityIdValue
+        *
+        * @param EntityId|EntityIdValue $value The value to format
+        *
+        * @return EntityId
+        *
+        * @throws InvalidArgumentException
+        */
+
+       protected function unwrapEntityId( $value ) {
+               if ( $value instanceof EntityIdValue ) {
+                       $value = $value->getEntityId();
+               }
+
+               if ( !( $value instanceof EntityId ) ) {
+                       throw new InvalidArgumentException( 'Data value type 
mismatch. Expected an EntityId or EntityIdValue.' );
+               }
+
+               return $value;
+       }
+
+       /**
         * Lookup a label for an entity
         *
         * @since 0.4
diff --git a/lib/includes/formatters/WikibaseValueFormatterBuilders.php 
b/lib/includes/formatters/WikibaseValueFormatterBuilders.php
index 939b376..7fe9fb8 100644
--- a/lib/includes/formatters/WikibaseValueFormatterBuilders.php
+++ b/lib/includes/formatters/WikibaseValueFormatterBuilders.php
@@ -77,7 +77,7 @@
                SnakFormatter::FORMAT_HTML => array(
                        'PT:url' => 'Wikibase\Lib\HtmlUrlFormatter',
                        'PT:commonsMedia' => 
'Wikibase\Lib\CommonsLinkFormatter',
-                       //'PT:wikibase-item' => 
'Wikibase\Lib\ItemLinkFormatter', // TODO
+                       'PT:wikibase-item' =>  array( 
'Wikibase\Lib\WikibaseValueFormatterBuilders', 'newEntityIdHtmlLinkFormatter' ),
                ),
 
                // Formatters to use for HTML widgets.
@@ -307,7 +307,7 @@
         * @return ValueFormatter[] A map from prefixed type IDs to 
ValueFormatter instances.
         */
        public function getWikiTextFormatters( FormatterOptions $options, array 
$skip = array() ) {
-               $wikiFormatters = $this->buildDefinedFormatters( 
SnakFormatter::FORMAT_WIKI, $options );
+               $wikiFormatters = $this->buildDefinedFormatters( 
SnakFormatter::FORMAT_WIKI, $options, $skip );
                $plainFormatters = $this->getPlainTextFormatters( $options, 
array_merge( $skip, array_keys( $wikiFormatters ) ) );
 
                $wikiFormatters = array_merge(
@@ -330,7 +330,7 @@
         * @return ValueFormatter[] A map from prefixed type IDs to 
ValueFormatter instances.
         */
        public function getHtmlFormatters( FormatterOptions $options, array 
$skip = array() ) {
-               $htmlFormatters = $this->buildDefinedFormatters( 
SnakFormatter::FORMAT_HTML, $options );
+               $htmlFormatters = $this->buildDefinedFormatters( 
SnakFormatter::FORMAT_HTML, $options, $skip );
                $plainFormatters = $this->getPlainTextFormatters( $options, 
array_merge( $skip, array_keys( $htmlFormatters ) ) );
 
                $htmlFormatters = array_merge(
@@ -354,7 +354,7 @@
         * @return ValueFormatter[] A map from prefixed type IDs to 
ValueFormatter instances.
         */
        public function getWidgetFormatters( FormatterOptions $options, array 
$skip = array() ) {
-               $widgetFormatters = $this->buildDefinedFormatters( 
SnakFormatter::FORMAT_HTML_WIDGET, $options );
+               $widgetFormatters = $this->buildDefinedFormatters( 
SnakFormatter::FORMAT_HTML_WIDGET, $options, $skip );
                $htmlFormatters = $this->getHtmlFormatters( $options, 
array_merge( $skip, array_keys( $widgetFormatters ) ) );
 
                $widgetFormatters = array_merge(
@@ -378,7 +378,7 @@
         * @return ValueFormatter[] A map from prefixed type IDs to 
ValueFormatter instances.
         */
        public function getDiffFormatters( FormatterOptions $options, array 
$skip = array() ) {
-               $diffFormatters = $this->buildDefinedFormatters( 
SnakFormatter::FORMAT_HTML_DIFF, $options );
+               $diffFormatters = $this->buildDefinedFormatters( 
SnakFormatter::FORMAT_HTML_DIFF, $options, $skip );
                $htmlFormatters = $this->getHtmlFormatters( $options, 
array_merge( $skip, array_keys( $diffFormatters ) ) );
 
                $diffFormatters = array_merge(
@@ -465,6 +465,19 @@
 
        /**
         * Builder callback for use in 
WikibaseValueFormatterBuilders::$valueFormatterSpecs.
+        * Used to inject services into the EntityIdHtmlLinkFormatter.
+        *
+        * @param FormatterOptions $options
+        * @param WikibaseValueFormatterBuilders $builders
+        *
+        * @return EntityIdHtmlLinkFormatter
+        */
+       protected static function newEntityIdHtmlLinkFormatter( 
FormatterOptions $options, $builders ) {
+               return new EntityIdHtmlLinkFormatter( $options, 
$builders->entityLookup );
+       }
+
+       /**
+        * Builder callback for use in 
WikibaseValueFormatterBuilders::$valueFormatterSpecs.
         * Used to compose the QuantityFormatter.
         *
         * @param FormatterOptions $options
diff --git a/lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php 
b/lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php
index 81c8d97..4d940f7 100644
--- a/lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php
+++ b/lib/tests/phpunit/formatters/WikibaseSnakFormatterBuildersTest.php
@@ -135,8 +135,8 @@
                                new PropertyValueSnak( 7, new StringValue( 'I 
<3 Wikibase' ) ),
                                'I &lt;3 Wikibase'
                        ),
-                       'widget item label (with entity lookup)' => array(
-                               SnakFormatter::FORMAT_HTML_WIDGET,
+                       'plain item label (with entity lookup)' => array(
+                               SnakFormatter::FORMAT_PLAIN,
                                $options,
                                'wikibase-item',
                                new PropertyValueSnak( 7, new EntityIdValue( 
new ItemId( 'Q5' ) ) ),
diff --git 
a/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php 
b/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
index 2e5c507..ccfde58 100644
--- a/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
+++ b/lib/tests/phpunit/formatters/WikibaseValueFormatterBuildersTest.php
@@ -66,7 +66,7 @@
                $formatter = $builders->buildDispatchingValueFormatter( 
$factory, $format, $options );
 
                $text = $formatter->formatValue( $snak, $dataType );
-               $this->assertEquals( $expected, $text );
+               $this->assertRegExp( $expected, $text );
        }
 
        public function buildDispatchingValueFormatterProvider() {
@@ -83,43 +83,50 @@
                                SnakFormatter::FORMAT_PLAIN,
                                $options,
                                new StringValue( 'http://acme.com/' ),
-                               'http://acme.com/'
+                               '@^http://acme\\.com/$@'
                        ),
                        'wikitext string' => array(
                                SnakFormatter::FORMAT_WIKI,
                                $options,
                                new StringValue( '{Wikibase}' ),
-                               '&#123;Wikibase&#125;'
+                               '@^&#123;Wikibase&#125;$@'
                        ),
                        'html string' => array(
                                SnakFormatter::FORMAT_HTML,
                                $options,
                                new StringValue( 'I <3 Wikibase & stuff' ),
-                               'I &lt;3 Wikibase &amp; stuff'
+                               '@^I &lt;3 Wikibase &amp; stuff$@'
                        ),
-                       'widget item label (with entity lookup)' => array(
+                       'plain item label (with entity lookup)' => array(
+                               SnakFormatter::FORMAT_PLAIN,
+                               $options,
+                               new EntityIdValue( new ItemId( 'Q5' ) ),
+                               '@^Label for Q5$@' // compare mock object 
created in newBuilders()
+                       ),
+                       'widget item link (with entity lookup)' => array(
                                SnakFormatter::FORMAT_HTML_WIDGET,
                                $options,
                                new EntityIdValue( new ItemId( 'Q5' ) ),
-                               'Label for Q5' // compare mock object created 
in newBuilders()
+                               '@^<a href=".*/index.php/Q5">Label for 
Q5</a>$@', // compare mock object created in newBuilders()
+                               'wikibase-item'
                        ),
                        'diff <url>' => array(
                                SnakFormatter::FORMAT_HTML_DIFF,
                                $options,
                                new StringValue( '<http://acme.com/>' ),
-                               '&lt;http://acme.com/&gt;'
+                               '@^&lt;http://acme\\.com/&gt;$@'
                        ),
                        'localized quantity' => array(
                                SnakFormatter::FORMAT_WIKI,
                                $optionsDe,
                                QuantityValue::newFromNumber( '+123456.789' ),
-                               '123.456,789'
+                               '@^123\\.456,789$@'
                        ),
                        'commons link' => array(
                                SnakFormatter::FORMAT_HTML,
                                $options,
                                new StringValue( 'example.jpg' ),
-                               '<a class="extiw" 
href="//commons.wikimedia.org/wiki/File:example.jpg">example.jpg</a>',
+                               '@^<a class="extiw" 
href="//commons\\.wikimedia\\.org/wiki/File:example\\.jpg">example\\.jpg</a>$@',
                                'commonsMedia'
                        ),
                );
diff --git a/repo/tests/phpunit/includes/api/FormatSnakValueTest.php 
b/repo/tests/phpunit/includes/api/FormatSnakValueTest.php
index 37b1344..7430df2 100644
--- a/repo/tests/phpunit/includes/api/FormatSnakValueTest.php
+++ b/repo/tests/phpunit/includes/api/FormatSnakValueTest.php
@@ -6,6 +6,8 @@
 use DataValues\StringValue;
 use DataValues\TimeValue;
 use ValueFormatters\TimeFormatter;
+use Wikibase\DataModel\Entity\EntityIdValue;
+use Wikibase\DataModel\Entity\ItemId;
 use Wikibase\Lib\SnakFormatter;
 
 /**
@@ -83,6 +85,13 @@
                                null,
                                
'@commons\.wikimedia\.org\/wiki\/File:example\.jpg@' ),
 
+                       // FIXME: This test uses the production environment, 
but it should have its own mock data
+                       array( new EntityIdValue( new ItemId( 'Q200000' ) ),
+                               'wikibase-item',
+                               SnakFormatter::FORMAT_HTML,
+                               null,
+                               '@^<a href=".*index\.php/Q200000">.*</a>$@' ),
+
                        //TODO: test HTML output
                );
        }

-- 
To view, visit https://gerrit.wikimedia.org/r/109654
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I2da16e594d8d9fc687be267c62ae52308df052da
Gerrit-PatchSet: 8
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Adrian Lang <adrian.l...@wikimedia.de>
Gerrit-Reviewer: Addshore <addshorew...@gmail.com>
Gerrit-Reviewer: Adrian Lang <adrian.l...@wikimedia.de>
Gerrit-Reviewer: WikidataJenkins <wikidata-servi...@wikimedia.de>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to