jenkins-bot has submitted this change and it was merged. Change subject: Show label instead of URI for globes in coordinate diffs ......................................................................
Show label instead of URI for globes in coordinate diffs By using VocabularyUriFormatter in GlobeCoordinateDetailsFormatter as suggested by Thiemo at T110193#1637667. Bug: T110193 Change-Id: I86fe2c7abecfc4fbd750f8d08f8a4da056b7daae --- M lib/includes/Formatters/GlobeCoordinateDetailsFormatter.php M lib/includes/Formatters/WikibaseValueFormatterBuilders.php M lib/tests/phpunit/Formatters/GlobeCoordinateDetailsFormatterTest.php 3 files changed, 49 insertions(+), 7 deletions(-) Approvals: Hoo man: Looks good to me, approved jenkins-bot: Verified diff --git a/lib/includes/Formatters/GlobeCoordinateDetailsFormatter.php b/lib/includes/Formatters/GlobeCoordinateDetailsFormatter.php index a00f4df..714363a 100644 --- a/lib/includes/Formatters/GlobeCoordinateDetailsFormatter.php +++ b/lib/includes/Formatters/GlobeCoordinateDetailsFormatter.php @@ -28,15 +28,26 @@ protected $coordinateFormatter; /** + * @var ValueFormatter + */ + protected $vocabularyUriFormatter; + + /** + * @param ValueFormatter $vocabularyUriFormatter * @param FormatterOptions|null $options */ - public function __construct( FormatterOptions $options = null ) { + public function __construct( + ValueFormatter $vocabularyUriFormatter, + FormatterOptions $options = null + ) { parent::__construct( $options ); // TODO: What's a good default? Should this be locale dependant? Configurable? $this->defaultOption( GeoCoordinateFormatter::OPT_FORMAT, GeoCoordinateFormatter::TYPE_DMS ); $this->coordinateFormatter = new GlobeCoordinateFormatter( $this->options ); + + $this->vocabularyUriFormatter = $vocabularyUriFormatter; } /** @@ -72,7 +83,7 @@ $html .= $this->renderLabelValuePair( 'precision', htmlspecialchars( $value->getPrecision() ) ); $html .= $this->renderLabelValuePair( 'globe', - htmlspecialchars( $value->getGlobe() ) ); + $this->formatGlobe( $value->getGlobe() ) ); $html .= Html::closeElement( 'table' ); @@ -80,6 +91,21 @@ } /** + * @param string $globe URI + * + * @return string HTML + */ + private function formatGlobe( $globe ) { + $formattedGlobe = $this->vocabularyUriFormatter->format( $globe ); + + if ( $formattedGlobe === null || $formattedGlobe === $globe ) { + return htmlspecialchars( $globe ); + } + + return Html::element( 'a', array( 'href' => $globe ), $formattedGlobe ); + } + + /** * @param string $fieldName * @param string $valueHtml HTML * diff --git a/lib/includes/Formatters/WikibaseValueFormatterBuilders.php b/lib/includes/Formatters/WikibaseValueFormatterBuilders.php index 45deded..400efa5 100644 --- a/lib/includes/Formatters/WikibaseValueFormatterBuilders.php +++ b/lib/includes/Formatters/WikibaseValueFormatterBuilders.php @@ -291,7 +291,10 @@ */ public function newGlobeCoordinateFormatter( $format, FormatterOptions $options ) { if ( $format === SnakFormatter::FORMAT_HTML_DIFF ) { - return new GlobeCoordinateDetailsFormatter( $options ); + return new GlobeCoordinateDetailsFormatter( + $this->getVocabularyUriFormatter( $options ), + $options + ); } else { $options->setOption( GeoCoordinateFormatter::OPT_FORMAT, GeoCoordinateFormatter::TYPE_DMS ); $options->setOption( GeoCoordinateFormatter::OPT_SPACING_LEVEL, array( diff --git a/lib/tests/phpunit/Formatters/GlobeCoordinateDetailsFormatterTest.php b/lib/tests/phpunit/Formatters/GlobeCoordinateDetailsFormatterTest.php index e9ca627..28d97ee 100644 --- a/lib/tests/phpunit/Formatters/GlobeCoordinateDetailsFormatterTest.php +++ b/lib/tests/phpunit/Formatters/GlobeCoordinateDetailsFormatterTest.php @@ -22,11 +22,24 @@ */ class GlobeCoordinateDetailsFormatterTest extends \PHPUnit_Framework_TestCase { + private function newFormatter( FormatterOptions $options = null ) { + $vocabularyUriFormatter = $this->getMock( ValueFormatter::class ); + $vocabularyUriFormatter->expects( $this->any() ) + ->method( 'format' ) + ->will( $this->returnCallback( function( $value ) { + return preg_match( '@^http://www\.wikidata\.org/entity/(.*)@', $value, $matches ) + ? "formatted-globe-{$matches[1]}" + : $value; + } ) ); + + return new GlobeCoordinateDetailsFormatter( $vocabularyUriFormatter, $options ); + } + /** * @dataProvider quantityFormatProvider */ public function testFormat( $value, $options, $pattern ) { - $formatter = new GlobeCoordinateDetailsFormatter( $options ); + $formatter = $this->newFormatter( $options ); $html = $formatter->format( $value ); $this->assertRegExp( $pattern, $html ); @@ -47,7 +60,7 @@ '<td[^<>]*>[^<>]*50[^<>]*</td>', '<td[^<>]*>[^<>]*11[^<>]*</td>', '<td[^<>]*>[^<>]*1[^<>]*</td>', - '<td[^<>]*>[^<>]*.*Q2[^<>]*</td>', + '<td[^<>]*>[^<>]*<a[^<>]*>[^<>]*.*formatted-globe-Q2[^<>]*</a>[^<>]*</td>', ) ) . '@s' ), @@ -55,7 +68,7 @@ } public function testFormatError() { - $formatter = new GlobeCoordinateDetailsFormatter(); + $formatter = $this->newFormatter(); $value = new NumberValue( 23 ); $this->setExpectedException( InvalidArgumentException::class ); @@ -78,7 +91,7 @@ ->method( 'getPrecision' ) ->will( $this->returnValue( '<PRECISION>' ) ); - $formatter = new GlobeCoordinateDetailsFormatter(); + $formatter = $this->newFormatter(); $formatted = $formatter->format( $value ); $this->assertContains( '<LAT>', $formatted ); -- To view, visit https://gerrit.wikimedia.org/r/287407 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I86fe2c7abecfc4fbd750f8d08f8a4da056b7daae Gerrit-PatchSet: 6 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Ricordisamoa <ricordisa...@openmailbox.org> Gerrit-Reviewer: Aude <aude.w...@gmail.com> Gerrit-Reviewer: Daniel Kinzler <daniel.kinz...@wikimedia.de> Gerrit-Reviewer: Hoo man <h...@online.de> Gerrit-Reviewer: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits