Thiemo Mättig (WMDE) has uploaded a new change for review.

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

Change subject: Optimize time parsers regular expressions
......................................................................

Optimize time parsers regular expressions

* Removed one matching bracket that's not needed.
* \D is short for [^\d] (and in my opinion better readable).

Change-Id: I68e07db23ad83edc1d276f7ab32e7107eeb5bcd0
---
M lib/includes/parsers/DateTimeParser.php
M lib/includes/parsers/EraParser.php
2 files changed, 12 insertions(+), 11 deletions(-)


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

diff --git a/lib/includes/parsers/DateTimeParser.php 
b/lib/includes/parsers/DateTimeParser.php
index 5479f00..1f41aad 100644
--- a/lib/includes/parsers/DateTimeParser.php
+++ b/lib/includes/parsers/DateTimeParser.php
@@ -52,7 +52,7 @@
                $calendarModelParser = new CalendarModelParser();
                $options = $this->getOptions();
 
-               //Place to put large years when they are found
+               // Place to put large years when they are found
                $largeYear = null;
 
                try{
@@ -68,14 +68,14 @@
                                )
                        );
 
-                       //PHP's DateTime object also cant handel larger than 4 
digit years
-                       //e.g. 1 June 202020
-                       if( preg_match( '/^(.*[^\d]|)(\d{5,})(.*|)$/', $value, 
$matches ) ) {
+                       // 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 ) ) {
                                $value = $matches[1] . substr( $matches[2], -4 
) . $matches[3];
                                $largeYear = $matches[2];
                        }
 
-                       //Parse using the DateTime object (this will allow us 
to format the date in a nicer way)
+                       // Parse using the DateTime object (this will allow us 
to format the date in a nicer way)
                        $dateTime = new DateTime( $value );
                        if( $largeYear === null ) {
                                $timeString = $sign . $dateTime->format( 
'Y-m-d\TH:i:s\Z' );
@@ -83,7 +83,7 @@
                                $timeString = $sign . $largeYear . 
$dateTime->format( '-m-d\TH:i:s\Z' );
                        }
 
-                       //Pass the reformatted string into a base parser that 
parses this +/-Y-m-d\TH:i:s\Z format with a precision
+                       // Pass the reformatted string into a base parser that 
parses this +/-Y-m-d\TH:i:s\Z format with a precision
                        $valueParser = new \ValueParsers\TimeParser( 
$calendarModelParser, $options );
                        return $valueParser->parse( $timeString );
                }
@@ -106,7 +106,7 @@
        }
 
        /**
-        * PHP's DateTime object also cant handel smaller than 4 digit years
+        * PHP's DateTime object also can't handle smaller than 4 digit years
         * e.g. instead of 12 it needs 0012 etc.
         *
         * @param string $value
@@ -114,7 +114,7 @@
         * @return string
         */
        private function getValueWithFixedYearLengths( $value ) {
-               if( preg_match( '/^(\d+)([^\d])(\d+)([^\d])(\d+)$/', $value, 
$dateParts ) ) {
+               if( preg_match( '/^(\d+)(\D)(\d+)(\D)(\d+)$/', $value, 
$dateParts ) ) {
                        if( $dateParts[1] > 31 && $dateParts[5] <= 31 ) {
                                // the year looks like it is at the front
                                if( strlen( $dateParts[1] ) < 4 ) {
@@ -129,7 +129,7 @@
                                }
                        }
                } else {
-                       if( preg_match( '/^(.*[^\d])(\d{1,3})$/', $value, 
$matches ) ) {
+                       if( preg_match( '/^(.*\D)(\d{1,3})$/', $value, $matches 
) ) {
                                $value = $matches[1] . str_pad( $matches[2], 4, 
'0', STR_PAD_LEFT );
                        }
                }
diff --git a/lib/includes/parsers/EraParser.php 
b/lib/includes/parsers/EraParser.php
index 91fb221..88011c4 100644
--- a/lib/includes/parsers/EraParser.php
+++ b/lib/includes/parsers/EraParser.php
@@ -81,8 +81,9 @@
                        $value = substr( $value, 1 );
                }
 
-               $value = preg_replace( '/(\s*(' . $this->CEregex . '|' .  
$this->BCEregex . '))$/i', '', $value );
+               $value = preg_replace( '/\s*(' . $this->CEregex . '|' .  
$this->BCEregex . ')$/i', '', $value );
 
                return trim( $value );
        }
-}
\ No newline at end of file
+
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I68e07db23ad83edc1d276f7ab32e7107eeb5bcd0
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to