Aude has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/130376

Change subject: Disallow DateTime parsing of strings without digits
......................................................................

Disallow DateTime parsing of strings without digits

plus more test cases

Something like 'x' will be parsed as current time, with
precision of seconds which we don't support. Users then see
error "Precision higher than 'DAY' is not supported'.

Change-Id: I1650db7bd28b18835e5fe6f6dba520fff68c3c82
---
M lib/includes/parsers/DateTimeParser.php
M lib/tests/phpunit/parsers/DateTimeParserTest.php
2 files changed, 27 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/76/130376/1

diff --git a/lib/includes/parsers/DateTimeParser.php 
b/lib/includes/parsers/DateTimeParser.php
index c03fec4..8c012a3 100644
--- a/lib/includes/parsers/DateTimeParser.php
+++ b/lib/includes/parsers/DateTimeParser.php
@@ -72,6 +72,8 @@
                                )
                        );
 
+                       $value = trim( $value );
+
                        // PHP's DateTime object also can't handle larger than 
4 digit years,
                        // e.g. 1 June 202020
                        if( preg_match( '/^(.*\D)?(\d{5,})(.*)$/', $value, 
$matches ) ) {
@@ -79,9 +81,7 @@
                                $largeYear = $matches[2];
                        }
 
-                       if ( preg_match( '/^\d{1,7}(\+\d*|\D*)$/', $value, 
$matches ) ) {
-                               throw new ParseException( $value . ' is not a 
valid date' );
-                       }
+                       $this->validateDateTimeInput( $value );
 
                        // Parse using the DateTime object (this will allow us 
to format the date in a nicer way)
                        $dateTime = new DateTime( $value );
@@ -101,6 +101,24 @@
        }
 
        /**
+        * @param string $value
+        *
+        * @throws ParseException
+        */
+       private function validateDateTimeInput( $value ) {
+               // @todo i18n support for these exceptions
+               // we don't support dates in format of year + timezone
+               if ( preg_match( '/^\d{1,7}(\+\d*|\D*)$/', $value, $matches ) ) 
{
+                       throw new ParseException( $value . ' is not a valid 
date.' );
+               }
+
+               // we don't support input of non-digits only, such as 'x'.
+               if ( preg_match( '/^(\D+)$/', $value, $matches ) ) {
+                       throw new ParseException( $value . ' is not a valid 
date.' );
+               }
+       }
+
+       /**
         * PHP's DateTime object does not accept spaces as separators between 
year, month and day,
         * e.g. dates like 20 12 2012, but we want to support them.
         * See http://de1.php.net/manual/en/datetime.formats.date.php
diff --git a/lib/tests/phpunit/parsers/DateTimeParserTest.php 
b/lib/tests/phpunit/parsers/DateTimeParserTest.php
index a03c6bb..fd547c2 100644
--- a/lib/tests/phpunit/parsers/DateTimeParserTest.php
+++ b/lib/tests/phpunit/parsers/DateTimeParserTest.php
@@ -134,6 +134,8 @@
                                array( '+0000000000000111-07-04T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ),
                        '4th July 1' =>
                                array( '+0000000000000001-07-04T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ),
+                       '4th July 1 ' =>
+                               array( '+0000000000000001-07-04T00:00:00Z', 0 , 
0 , 0 , TimeValue::PRECISION_DAY , TimeFormatter::CALENDAR_GREGORIAN ),
 
                        //More than 4 digit years
                        '4th July 10000' =>
@@ -177,8 +179,7 @@
                        'Jann 2014',
                        '1980x',
                        '1980s', // supported by MWTimeIsoParser
-                       '1980s ',
-                       '1980 ',
+                       '1980',
                        '1980ss',
                        '1980er',
                        '1980UTC', // we don't support year + timezone here
@@ -186,6 +187,9 @@
                        '1980 America/New_York',
                        '1980+3',
                        '1980+x'
+                       'x',
+                       'zz',
+                       'America/New_York'
                );
 
                foreach ( $invalid as $value ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/130376
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1650db7bd28b18835e5fe6f6dba520fff68c3c82
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Aude <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to