Daniel Kinzler has uploaded a new change for review.
https://gerrit.wikimedia.org/r/68152
Change subject: (bug 49264, bug 48965) validate time format.
......................................................................
(bug 49264, bug 48965) validate time format.
This provides validation of the provided date/time string
when constructing a TimeValue object.
It does not however validate the calendar model, since the
DataValue does not (and should not) know which models would be
valid. This needs to be done by a Validator based on the
proiperty's data type (not the data value type).
Note that this contributes to the solution of the mentioned bugs,
but is not a complete fix.
NOTE: this was originally submitted as I6990983, but was reverted
because we do not currently have graceful handling of the failures
caused when encountering invalid data in the database (like we have
on wikidata.org).
Before merging this, we need to at least have a clear plan to handle the
resulting failures gracefully when rendering, diffing, serializing,
importing, etc.
Change-Id: I72d6b6d890718c07006c306c254479ce0789067e
---
M DataValues/includes/values/TimeValue.php
M DataValues/tests/phpunit/includes/values/TimeValueTest.php
2 files changed, 109 insertions(+), 4 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/DataValues
refs/changes/52/68152/1
diff --git a/DataValues/includes/values/TimeValue.php
b/DataValues/includes/values/TimeValue.php
index 8b71731..e51acd4 100644
--- a/DataValues/includes/values/TimeValue.php
+++ b/DataValues/includes/values/TimeValue.php
@@ -91,7 +91,7 @@
*
* @since 0.1
*
- * @var integer
+ * @var int
*/
protected $timezone;
@@ -111,6 +111,7 @@
* @param string $time
* @param integer $timezone
* @param integer $before
+ * @param integer $after
* @param integer $precision
* @param string $calendarModel
*
@@ -119,6 +120,10 @@
public function __construct( $time, $timezone, $before, $after,
$precision, $calendarModel ) {
if ( !is_string( $time ) ) {
throw new IllegalValueException( '$time needs to be a
string' );
+ }
+
+ if ( !preg_match(
'!^[-+]\d{1,16}-(0\d|1[012])-([012]\d|3[01])T([01]\d|2[0123]):[0-5]\d:([0-5]\d|6[012])Z$!',
$time ) ) {
+ throw new IllegalValueException( '$time needs to be a
valid ISO 8601 date' );
}
if ( !is_integer( $timezone ) ) {
@@ -145,7 +150,7 @@
throw new IllegalValueException( '$precision out of
allowed bounds' );
}
- if ( !is_string( $calendarModel ) ) {
+ if ( !is_string( $calendarModel ) ) { //XXX: enforce IRI? Or at
least a size limit?
throw new IllegalValueException( '$calendarModel needs
to be a string' );
}
diff --git a/DataValues/tests/phpunit/includes/values/TimeValueTest.php
b/DataValues/tests/phpunit/includes/values/TimeValueTest.php
index e698eea..dd06062 100644
--- a/DataValues/tests/phpunit/includes/values/TimeValueTest.php
+++ b/DataValues/tests/phpunit/includes/values/TimeValueTest.php
@@ -117,12 +117,12 @@
);
$argLists[] = array(
- true,
+ 'DataValues\IllegalValueException',
'+00000002013-01-01T00:00:00Z',
0,
0,
0,
- 0, // 0 == TimeValue::PRECISION_GIGAYEAR
+ 333,
'http://nyan.cat/original.php',
);
@@ -166,6 +166,106 @@
'http://nyan.cat/original.php',
);
+ $argLists[] = array(
+ 'DataValues\IllegalValueException',
+ 'bla',
+ 0,
+ 0,
+ 0,
+ TimeValue::PRECISION_SECOND,
+ 'http://nyan.cat/original.php',
+ );
+
+ $argLists[] = array(
+ 'DataValues\IllegalValueException',
+ '+00000002013/01/01 00:00:00',
+ 0,
+ 0,
+ 0,
+ TimeValue::PRECISION_SECOND,
+ 'http://nyan.cat/original.php',
+ );
+
+ $argLists[] = array(
+ 'DataValues\IllegalValueException',
+ '+00000002013-22-01T00:00:00Z',
+ 0,
+ 0,
+ 0,
+ TimeValue::PRECISION_SECOND,
+ 'http://nyan.cat/original.php',
+ );
+
+ $argLists[] = array(
+ 'DataValues\IllegalValueException',
+ '+00000002013-01-35T00:00:00Z',
+ 0,
+ 0,
+ 0,
+ TimeValue::PRECISION_SECOND,
+ 'http://nyan.cat/original.php',
+ );
+
+ $argLists[] = array(
+ 'DataValues\IllegalValueException',
+ '+00000002013-01-01T27:00:00Z',
+ 0,
+ 0,
+ 0,
+ TimeValue::PRECISION_SECOND,
+ 'http://nyan.cat/original.php',
+ );
+
+ $argLists[] = array(
+ 'DataValues\IllegalValueException',
+ '+00000002013-01-01T00:66:00Z',
+ 0,
+ 0,
+ 0,
+ TimeValue::PRECISION_SECOND,
+ 'http://nyan.cat/original.php',
+ );
+
+ $argLists[] = array(
+ 'DataValues\IllegalValueException',
+ '+00000002013-01-01T00:00:66Z',
+ 0,
+ 0,
+ 0,
+ TimeValue::PRECISION_SECOND,
+ 'http://nyan.cat/original.php',
+ );
+
+ $argLists[] = array(
+ 'DataValues\IllegalValueException',
+ '+00000002013-01-01T00:00:00+60',
+ 0,
+ 0,
+ 0,
+ TimeValue::PRECISION_SECOND,
+ 'http://nyan.cat/original.php',
+ );
+
+ $argLists[] = array(
+ true,
+ '+2013-01-01T00:00:00Z',
+ 0,
+ 0,
+ 0,
+ TimeValue::PRECISION_SECOND,
+ 'http://nyan.cat/original.php',
+ );
+
+ $argLists[] = array(
+ true,
+ '-5-01-01T00:00:00Z',
+ 0,
+ 0,
+ 0,
+ TimeValue::PRECISION_SECOND,
+ 'http://nyan.cat/original.php',
+ );
+
return $argLists;
}
--
To view, visit https://gerrit.wikimedia.org/r/68152
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I72d6b6d890718c07006c306c254479ce0789067e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/DataValues
Gerrit-Branch: master
Gerrit-Owner: Daniel Kinzler <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits