Daniel Kinzler has uploaded a new change for review. https://gerrit.wikimedia.org/r/116763
Change subject: Formatter showing details of GlobeCoordinateValue ...................................................................... Formatter showing details of GlobeCoordinateValue Change-Id: Iad4fa6227a5ff39ee49dbbe03d57c593bbb91ddc --- M lib/WikibaseLib.i18n.php A lib/includes/formatters/GlobeCoordinateDetailsFormatter.php M lib/includes/formatters/WikibaseValueFormatterBuilders.php A lib/tests/phpunit/formatters/GlobeCoordinateDetailsFormatterTest.php 4 files changed, 170 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/63/116763/1 diff --git a/lib/WikibaseLib.i18n.php b/lib/WikibaseLib.i18n.php index d673aa2..bb879bb 100644 --- a/lib/WikibaseLib.i18n.php +++ b/lib/WikibaseLib.i18n.php @@ -59,6 +59,10 @@ 'wikibase-timedetails-precision' => 'Amount', 'wikibase-timedetails-before' => 'Before', 'wikibase-timedetails-after' => 'After', + 'wikibase-globedetails-longitude' => 'Longitude', + 'wikibase-globedetails-latitude' => 'Latitude', + 'wikibase-globedetails-precision' => 'precision', + 'wikibase-globedetails-globe' => 'Globe', 'wikibase-replicationnote' => 'Please notice that it can take several minutes until the changes are visible on all wikis.', 'wikibase-sitelinks-wikipedia' => 'Wikipedia pages linked to this item', 'wikibase-sitelinks-sitename-columnheading' => 'Language', @@ -173,6 +177,10 @@ 'wikibase-timedetails-precision' => 'Label used for the "precision" field of a time value when showing a detailed representation of the time, e.g. in a diff.', 'wikibase-timedetails-before' => 'Label used for the "before" field of a time value when showing a detailed representation of the time, e.g. in a diff.', 'wikibase-timedetails-after' => 'Label used for the "after" field of a time value when showing a detailed representation of the time, e.g. in a diff.', + 'wikibase-globedetails-longitude' => 'Label used for the "longitude" field of a globecoordinate value when showing a detailed representation of the time, e.g. in a diff.', + 'wikibase-globedetails-latitude' => 'Label used for the "latitude" field of a globecoordinate value when showing a detailed representation of the time, e.g. in a diff.', + 'wikibase-globedetails-precision' => 'Label used for the "precision" field of a globecoordinate value when showing a detailed representation of the time, e.g. in a diff.', + 'wikibase-globedetails-globe' => 'Label used for the "globe" field of a globecoordinate value when showing a detailed representation of the time, e.g. in a diff.', 'wikibase-replicationnote' => 'Note telling the user that it can take a few minutes until the made changes are visible on all wikis. Preceded by message {{msg-mw|Wikibase-linkitem-success-link}}', 'wikibase-sitelinks-wikipedia' => '[[File:Screenshot WikidataRepo 2012-05-13 A.png|right|0x150px]] diff --git a/lib/includes/formatters/GlobeCoordinateDetailsFormatter.php b/lib/includes/formatters/GlobeCoordinateDetailsFormatter.php new file mode 100644 index 0000000..8c5a666 --- /dev/null +++ b/lib/includes/formatters/GlobeCoordinateDetailsFormatter.php @@ -0,0 +1,91 @@ +<?php + +namespace Wikibase\Lib; + +use DataValues\GlobeCoordinateValue; +use Html; +use InvalidArgumentException; +use Message; +use NumberFormatter; +use ValueFormatters\DecimalFormatter; +use ValueFormatters\FormatterOptions; +use ValueFormatters\ValueFormatter; +use ValueFormatters\ValueFormatterBase; + +/** + * Formatter for rendering the details of a GlobeCoordinateValue (most useful for diffs) in HTML. + * + * @since 0.5 + * + * @licence GNU GPL v2+ + * @author Daniel Kinzler + */ +class GlobeCoordinateDetailsFormatter extends ValueFormatterBase { + + /** + * @param FormatterOptions $options + */ + public function __construct( FormatterOptions $options ) { + parent::__construct( $options ); + } + + /** + * Generates HTML representing the details of a GlobeCoordinateValue, + * as an itemized list. + * + * @since 0.5 + * + * @param GlobeCoordinateValue $value The ID to format + * + * @return string + * @throws InvalidArgumentException + */ + public function format( $value ) { + if ( !( $value instanceof GlobeCoordinateValue ) ) { + throw new InvalidArgumentException( 'Data value type mismatch. Expected an GlobeCoordinateValue.' ); + } + + $html = ''; + $html .= Html::openElement( 'dl', array( 'class' => 'wikibase-details wikibase-globe-details' ) ); + + //TODO: nicer formatting and localization of numbers. + $html .= $this->renderLabelValuePair( 'latitude', htmlspecialchars( strval( $value->getLatitude() ) ) ); + $html .= $this->renderLabelValuePair( 'longitude', htmlspecialchars( strval( $value->getLongitude() ) ) ); + $html .= $this->renderLabelValuePair( 'precision', htmlspecialchars( strval( $value->getPrecision() ) ) ); + $html .= $this->renderLabelValuePair( 'globe', htmlspecialchars( strval( $value->getGlobe() ) ) ); + + $html .= Html::closeElement( 'dl' ); + + return $html; + } + + /** + * @param string $fieldName + * @param string $valueHtml + * + * @return string HTML for the label/value pair + */ + public function renderLabelValuePair( $fieldName, $valueHtml ) { + $html = ''; + $html .= Html::element( 'dt', array( 'class' => 'wikibase-globe-' . $fieldName ), $this->getFieldLabel( $fieldName )->text() ); + $html .= Html::element( 'dd', array( 'class' => 'wikibase-globe-' . $fieldName ), $valueHtml ); + + return $html; + } + + /** + * @param string $fieldName + * + * @return Message + */ + protected function getFieldLabel( $fieldName ) { + $lang = $this->getOption( ValueFormatter::OPT_LANG ); + + // Messages: wikibase-globedetails-amount, wikibase-globedetails-upperbound, + // wikibase-globedetails-lowerbound, wikibase-globedetails-unit + $key = 'wikibase-globedetails-' . strtolower( $fieldName ); + $msg = wfMessage( $key )->inLanguage( $lang ); + + return $msg; + } +} diff --git a/lib/includes/formatters/WikibaseValueFormatterBuilders.php b/lib/includes/formatters/WikibaseValueFormatterBuilders.php index a7f2fd5..d0b4832 100644 --- a/lib/includes/formatters/WikibaseValueFormatterBuilders.php +++ b/lib/includes/formatters/WikibaseValueFormatterBuilders.php @@ -90,6 +90,7 @@ SnakFormatter::FORMAT_HTML_DIFF => array( 'PT:quantity' => 'Wikibase\Lib\QuantityDetailsFormatter', 'PT:time' => 'Wikibase\Lib\TimeDetailsFormatter', + 'PT:globe-coordinate' => 'Wikibase\Lib\GlobeCoordinateDetailsFormatter', ), ); diff --git a/lib/tests/phpunit/formatters/GlobeCoordinateDetailsFormatterTest.php b/lib/tests/phpunit/formatters/GlobeCoordinateDetailsFormatterTest.php new file mode 100644 index 0000000..e2dda28 --- /dev/null +++ b/lib/tests/phpunit/formatters/GlobeCoordinateDetailsFormatterTest.php @@ -0,0 +1,70 @@ +<?php + +namespace Wikibase\Lib\Test; + +use DataValues\LatLongValue; +use DataValues\NumberValue; +use DataValues\GlobeCoordinateValue; +use ValueFormatters\FormatterOptions; +use ValueFormatters\GlobeCoordinateFormatter; +use ValueFormatters\ValueFormatter; +use Wikibase\Lib\GlobeCoordinateDetailsFormatter; + +/** + * @covers Wikibase\Lib\GlobeCoordinateDetailsFormatter + * + * @since 0.5 + * + * @group ValueFormatters + * @group WikibaseLib + * @group Wikibase + * + * @licence GNU GPL v2+ + * @author Daniel Kinzler + */ +class GlobeCoordinateDetailsFormatterTest extends \PHPUnit_Framework_TestCase { + + /** + * @dataProvider quantityFormatProvider + * + * @covers GlobeCoordinateDetailsFormatterTest::format() + */ + public function testFormat( $value, $options, $pattern ) { + $formatter = new GlobeCoordinateDetailsFormatter( $options ); + + $html = $formatter->format( $value ); + $this->assertRegExp( $pattern, $html ); + } + + public function quantityFormatProvider() { + $options = new FormatterOptions( array( + ValueFormatter::OPT_LANG => 'en' + ) ); + + return array( + array( + new GlobeCoordinateValue( new LatLongValue( 50, 11 ), 1, GlobeCoordinateValue::GLOBE_EARTH ), + $options, + '@' . implode( '.*', + array( + '<dd[^<>]*>[^<>]*50[^<>]*</dd>', + '<dd[^<>]*>[^<>]*11[^<>]*</dd>', + '<dd[^<>]*>[^<>]*1[^<>]*</dd>', + '<dd[^<>]*>[^<>]*.*Q2[^<>]*</dd>', + ) + ) . '@s' + ), + ); + } + + /** + * @covers GlobeCoordinateDetailsFormatterTest::format() + */ + public function testFormatError() { + $formatter = new GlobeCoordinateDetailsFormatter( new FormatterOptions() ); + $value = new NumberValue( 23 ); + + $this->setExpectedException( 'InvalidArgumentException' ); + $formatter->format( $value ); + } +} -- To view, visit https://gerrit.wikimedia.org/r/116763 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Iad4fa6227a5ff39ee49dbbe03d57c593bbb91ddc Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Wikibase Gerrit-Branch: master Gerrit-Owner: Daniel Kinzler <daniel.kinz...@wikimedia.de> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits