Thiemo Mättig (WMDE) has uploaded a new change for review.
https://gerrit.wikimedia.org/r/188551
Change subject: Show before and after time values in human readable form
......................................................................
Show before and after time values in human readable form
This reuses existing code. This is build on the convention that the
"before" and "after" values are meant to be read as "how many
precision intervals".
Change-Id: I0bddb681840a911dfc6eef646dcb27f567999d38
---
M lib/includes/formatters/TimeDetailsFormatter.php
M lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php
2 files changed, 22 insertions(+), 12 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/51/188551/1
diff --git a/lib/includes/formatters/TimeDetailsFormatter.php
b/lib/includes/formatters/TimeDetailsFormatter.php
index 624f20b..5262b45 100644
--- a/lib/includes/formatters/TimeDetailsFormatter.php
+++ b/lib/includes/formatters/TimeDetailsFormatter.php
@@ -86,15 +86,15 @@
// TODO: Provide "nice" rendering of precision, etc.
$html .= $this->renderLabelValuePair(
'precision',
- $this->getPrecisionHtml( $value->getPrecision() )
+ $this->getAmountAndPrecisionHtml(
$value->getPrecision() )
);
$html .= $this->renderLabelValuePair(
'before',
- htmlspecialchars( $value->getBefore() )
+ $this->getAmountAndPrecisionHtml(
$value->getPrecision(), $value->getBefore() )
);
$html .= $this->renderLabelValuePair(
'after',
- htmlspecialchars( $value->getAfter() )
+ $this->getAmountAndPrecisionHtml(
$value->getPrecision(), $value->getAfter() )
);
$html .= Html::closeElement( 'table' );
@@ -130,16 +130,12 @@
/**
* @param int $precision
+ * @param int $amount
*
* @return string HTML
*/
- private function getPrecisionHtml( $precision ) {
- if ( $precision > TimeValue::PRECISION_SECOND ) {
- return htmlspecialchars( $precision );
- }
-
+ private function getAmountAndPrecisionHtml( $precision, $amount = 1 ) {
$key = 'years';
- $amount = 1;
switch ( $precision ) {
case TimeValue::PRECISION_MONTH: $key = 'months'; break;
@@ -150,7 +146,9 @@
}
if ( $precision < TimeValue::PRECISION_YEAR ) {
- $amount = pow( 10, TimeValue::PRECISION_YEAR -
$precision );
+ $amount *= pow( 10, TimeValue::PRECISION_YEAR -
$precision );
+ } elseif ( $precision > TimeValue::PRECISION_SECOND ) {
+ $amount /= pow( 10, $precision -
TimeValue::PRECISION_SECOND );
}
$lang = $this->getOption( ValueFormatter::OPT_LANG );
diff --git a/lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php
b/lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php
index 330d726..715eddb 100644
--- a/lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php
+++ b/lib/tests/phpunit/formatters/TimeDetailsFormatterTest.php
@@ -50,8 +50,8 @@
'<td[^<>]*>\+01:00</td>',
'<td[^<>]*>Gregorian</td>',
'<td[^<>]*>\(months: 1\)</td>',
- '<td[^<>]*>0</td>',
- '<td[^<>]*>1</td>',
+ '<td[^<>]*>\(months: 0\)</td>',
+ '<td[^<>]*>\(months: 1\)</td>',
)
) . '@s'
),
@@ -75,6 +75,18 @@
new TimeValue( '+2001-01-01T00:00:00Z', 0, 0,
0, TimeValue::PRECISION_Ga, $gregorian ),
'@.*<td[^<>]*precision">\(years:
1000000000\)</td>.*@s'
),
+ array(
+ new TimeValue( '+2001-01-01T00:00:00Z', 0, 2,
0, TimeValue::PRECISION_YEAR, $gregorian ),
+ '@.*<td[^<>]*before">\(years: 2\)</td>.*@s'
+ ),
+ array(
+ new TimeValue( '+2001-01-01T00:00:00Z', 0, 0,
5, TimeValue::PRECISION_10a, $gregorian ),
+ '@.*<td[^<>]*after">\(years: 50\)</td>.*@s'
+ ),
+ array(
+ new TimeValue( '+2001-01-01T00:00:00Z', 0, 0,
125, $day, $gregorian ),
+ '@.*<td[^<>]*after">\(days: 125\)</td>.*@s'
+ ),
);
}
--
To view, visit https://gerrit.wikimedia.org/r/188551
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I0bddb681840a911dfc6eef646dcb27f567999d38
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits