Daniel Kinzler has uploaded a new change for review. https://gerrit.wikimedia.org/r/116761
Change subject: Formatter showing details of TimeValue in diffs. ...................................................................... Formatter showing details of TimeValue in diffs. Change-Id: I29c083b31c6bf269f0e7bb9c211c266489aef9a7 --- M lib/WikibaseLib.i18n.php A lib/includes/formatters/TimeDetailsFormatter.php M lib/includes/formatters/WikibaseValueFormatterBuilders.php A lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php 4 files changed, 191 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase refs/changes/61/116761/1 diff --git a/lib/WikibaseLib.i18n.php b/lib/WikibaseLib.i18n.php index e584eed..d673aa2 100644 --- a/lib/WikibaseLib.i18n.php +++ b/lib/WikibaseLib.i18n.php @@ -52,6 +52,13 @@ 'wikibase-quantitydetails-upperbound' => 'Upper bound', 'wikibase-quantitydetails-lowerbound' => 'Lower bound', 'wikibase-quantitydetails-unit' => 'Unit', + 'wikibase-timedetails-time' => 'Time', + 'wikibase-timedetails-isotime' => 'ISO Timestamp', + 'wikibase-timedetails-timezone' => 'Timezone', + 'wikibase-timedetails-calendar' => 'Calendar', + 'wikibase-timedetails-precision' => 'Amount', + 'wikibase-timedetails-before' => 'Before', + 'wikibase-timedetails-after' => 'After', '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', @@ -159,6 +166,13 @@ 'wikibase-quantitydetails-lowerbound' => 'Label used for the "lower bound" field of a quantity value when showing a detailed representation of the quantity, e.g. in a diff.', 'wikibase-quantitydetails-unit' => 'Label used for the "unit" field of a quantity value when showing a detailed representation of the quantity, e.g. in a diff. {{Identical|Unit}}', + 'wikibase-timedetails-time' => 'Label used for the rendered version of a time value when showing a detailed representation of the time, e.g. in a diff.', + 'wikibase-timedetails-isotime' => 'Label used for the "isotime" field of a time value when showing a detailed representation of the time, e.g. in a diff.', + 'wikibase-timedetails-timezone' => 'Label used for the "timezone" field of a time value when showing a detailed representation of the time, e.g. in a diff.', + 'wikibase-timedetails-calendar' => 'Label used for the "calendar" field of a time value when showing a detailed representation of the time, e.g. in a diff.', + '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-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/TimeDetailsFormatter.php b/lib/includes/formatters/TimeDetailsFormatter.php new file mode 100644 index 0000000..6fa5a86 --- /dev/null +++ b/lib/includes/formatters/TimeDetailsFormatter.php @@ -0,0 +1,104 @@ +<?php + +namespace Wikibase\Lib; + +use DataValues\TimeValue; +use Html; +use InvalidArgumentException; +use Message; +use ValueFormatters\FormatterOptions; +use ValueFormatters\Test\MwTimeIsoFormatterTest; +use ValueFormatters\TimeFormatter; +use ValueFormatters\ValueFormatter; +use ValueFormatters\ValueFormatterBase; + +/** + * Formatter for rendering the details of a TimeValue (most useful for diffs) in HTML. + * + * @since 0.5 + * + * @licence GNU GPL v2+ + * @author Daniel Kinzler + */ +class TimeDetailsFormatter extends ValueFormatterBase { + + /** + * @var MwTimeIsoFormatter + */ + protected $isoTimeFormatter; + + /** + * @param FormatterOptions $options + */ + public function __construct( FormatterOptions $options ) { + parent::__construct( $options ); + + $this->isoTimeFormatter = new MwTimeIsoFormatter( $options ); + } + + /** + * Generates HTML representing the details of a TimeValue, + * as an itemized list. + * + * @since 0.5 + * + * @param TimeValue $value The ID to format + * + * @return string + * @throws InvalidArgumentException + */ + public function format( $value ) { + if ( !( $value instanceof TimeValue ) ) { + throw new InvalidArgumentException( 'Data value type mismatch. Expected an TimeValue.' ); + } + + $html = ''; + $html .= Html::openElement( 'dl', array( 'class' => 'wikibase-details wikibase-time-details' ) ); + + $html .= $this->renderLabelValuePair( 'time', htmlspecialchars( $this->isoTimeFormatter->formatDate( $value->getTime(), $value->getPrecision() ) ) ); + + $html .= $this->renderLabelValuePair( 'isotime', htmlspecialchars( strval( $value->getTime() ) ) ); + + //TODO: provide "nice" rendering of timezone, calendar, precision, etc. + $html .= $this->renderLabelValuePair( 'timezone', htmlspecialchars( strval( $value->getTimezone() ) ) ); + $html .= $this->renderLabelValuePair( 'calendar', htmlspecialchars( strval( $value->getCalendarModel() ) ) ); + $html .= $this->renderLabelValuePair( 'precision', htmlspecialchars( strval( $value->getPrecision() ) ) ); + + $html .= $this->renderLabelValuePair( 'before', htmlspecialchars( strval( $value->getBefore() ) ) ); + $html .= $this->renderLabelValuePair( 'after', htmlspecialchars( strval( $value->getAfter() ) ) ); + + $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-time-' . $fieldName ), $this->getFieldLabel( $fieldName )->text() ); + $html .= Html::element( 'dd', array( 'class' => 'wikibase-time-' . $fieldName ), $valueHtml ); + + return $html; + } + + /** + * @param string $fieldName + * + * @return Message + */ + protected function getFieldLabel( $fieldName ) { + $lang = $this->getOption( ValueFormatter::OPT_LANG ); + + // Messages: wikibase-timedetails-amount, wikibase-timedetails-upperbound, + // wikibase-timedetails-lowerbound, wikibase-timedetails-unit + $key = 'wikibase-timedetails-' . strtolower( $fieldName ); + $msg = wfMessage( $key )->inLanguage( $lang ); + + return $msg; + } +} diff --git a/lib/includes/formatters/WikibaseValueFormatterBuilders.php b/lib/includes/formatters/WikibaseValueFormatterBuilders.php index 7fe9fb8..a7f2fd5 100644 --- a/lib/includes/formatters/WikibaseValueFormatterBuilders.php +++ b/lib/includes/formatters/WikibaseValueFormatterBuilders.php @@ -89,6 +89,7 @@ // Falls back to HTML display formatters. SnakFormatter::FORMAT_HTML_DIFF => array( 'PT:quantity' => 'Wikibase\Lib\QuantityDetailsFormatter', + 'PT:time' => 'Wikibase\Lib\TimeDetailsFormatter', ), ); diff --git a/lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php b/lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php new file mode 100644 index 0000000..7b0ee9e --- /dev/null +++ b/lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php @@ -0,0 +1,72 @@ +<?php + +namespace Wikibase\Lib\Test; + +use DataValues\NumberValue; +use DataValues\TimeValue; +use ValueFormatters\FormatterOptions; +use ValueFormatters\TimeFormatter; +use ValueFormatters\ValueFormatter; +use Wikibase\Lib\TimeDetailsFormatter; + +/** + * @covers Wikibase\Lib\TimeDetailsFormatter + * + * @since 0.5 + * + * @group ValueFormatters + * @group WikibaseLib + * @group Wikibase + * + * @licence GNU GPL v2+ + * @author Daniel Kinzler + */ +class TimeDetailsFormatterTest extends \PHPUnit_Framework_TestCase { + + /** + * @dataProvider quantityFormatProvider + * + * @covers TimeDetailsFormatterTest::format() + */ + public function testFormat( $value, $options, $pattern ) { + $formatter = new TimeDetailsFormatter( $options ); + + $html = $formatter->format( $value ); + $this->assertRegExp( $pattern, $html ); + } + + public function quantityFormatProvider() { + $options = new FormatterOptions( array( + ValueFormatter::OPT_LANG => 'en' + ) ); + + return array( + array( + new TimeValue( '+00000002001-01-01T00:00:00Z', 60, 0, 1, 10, TimeFormatter::CALENDAR_GREGORIAN ), + $options, + '@' . implode( '.*', + array( + '<dd[^<>]*>[^<>]*.*2001.*[^<>]*</dd>', + '<dd[^<>]*>[^<>]*\+00000002001-01-01T00:00:00Z[^<>]*</dd>', + '<dd[^<>]*>[^<>]*60[^<>]*</dd>', + '<dd[^<>]*>[^<>]*.*Q1985727[^<>]*</dd>', + '<dd[^<>]*>[^<>]*10[^<>]*</dd>', + '<dd[^<>]*>[^<>]*0[^<>]*</dd>', + '<dd[^<>]*>[^<>]*1[^<>]*</dd>', + ) + ) . '@s' + ), + ); + } + + /** + * @covers TimeDetailsFormatterTest::format() + */ + public function testFormatError() { + $formatter = new TimeDetailsFormatter( new FormatterOptions() ); + $value = new NumberValue( 23 ); + + $this->setExpectedException( 'InvalidArgumentException' ); + $formatter->format( $value ); + } +} -- To view, visit https://gerrit.wikimedia.org/r/116761 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I29c083b31c6bf269f0e7bb9c211c266489aef9a7 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