Yaron Koren has submitted this change and it was merged.

Change subject: Fix to allow parsing of dates when SMW is not installed
......................................................................


Fix to allow parsing of dates when SMW is not installed

Change-Id: I0ed868af0a8a6af0c201bc0ab29ad5f4db52d8ef
---
M includes/forminputs/SF_DateInput.php
M includes/forminputs/SF_DateTimeInput.php
2 files changed, 76 insertions(+), 39 deletions(-)

Approvals:
  Yaron Koren: Checked; Looks good to me, approved



diff --git a/includes/forminputs/SF_DateInput.php 
b/includes/forminputs/SF_DateInput.php
index ade9656..12227d2 100644
--- a/includes/forminputs/SF_DateInput.php
+++ b/includes/forminputs/SF_DateInput.php
@@ -52,6 +52,77 @@
                return $text;
        }
 
+       static function parseDate( $date ) {
+               // Special handling for 'default=now'.
+               if ( $date == 'now' ) {
+                       global $wgLocaltimezone;
+                       if ( isset( $wgLocaltimezone ) ) {
+                               $serverTimezone = date_default_timezone_get();
+                               date_default_timezone_set( $wgLocaltimezone );
+                       }
+                       $year = date( 'Y' );
+                       $month = date( 'm' );
+                       $day = date( 'j' );
+                       if ( isset( $wgLocaltimezone ) ) {
+                               date_default_timezone_set( $serverTimezone );
+                       }
+                       return array( $year, $month, $day );
+               }
+
+               if ( class_exists( 'SMWTimeValue' ) ) {
+                       return self::parseDateSMW( $date );
+               } else {
+                       return self::parseDateNonSMW( $date );
+               }
+       }
+
+       static function parseDateSMW( $date ) {
+               $actual_date = new SMWTimeValue( '_dat' );
+               $actual_date->setUserValue( $date );
+               $year = $actual_date->getYear();
+               // TODO - the code to convert from negative to BC notation
+               // should be in SMW itself.
+               if ( $year < 0 ) {
+                       $year = ( $year * - 1 + 1 ) . ' BC';
+               }
+               // Use precision of the date to determine whether we should
+               // also set the month and day.
+               if ( method_exists( $actual_date->getDataItem(), 'getPrecision' 
) ) {
+                       $precision = 
$actual_date->getDataItem()->getPrecision();
+                       if ( $precision > SMWDITime::PREC_Y ) {
+                               $month = $actual_date->getMonth();
+                       }
+                       if ( $precision > SMWDITime::PREC_YM ) {
+                               $day = $actual_date->getDay();
+                       }
+               } else {
+                       // There's some sort of error - make everything blank.
+                       $year = null;
+               }
+               return array( $year, $month, $day );
+       }
+
+       static function parseDateNonSMW( $date ) {
+               if ( ctype_digit( $date ) ) {
+                       return array( $date, null, null );
+               }
+
+               $seconds = strtotime( $date );
+               $year = date( 'Y', $seconds );
+               $month = date( 'm', $seconds );
+               // Determine if there's a month but no day. There's no ideal
+               // way to do this, so: we'll just look for the total
+               // number of spaces, slashes and dashes, and if there's
+               // exactly one altogether, we'll guess that it's a month only.
+               $numSpecialChars = substr_count( $date, ' ' ) + substr_count( 
$date, '/' ) + substr_count( $date, '-' );
+               if ( $numSpecialChars == 1 ) {
+                       return array( $year, $month, null );
+               }
+
+               $day = date( 'j', $seconds );
+               return array( $year, $month, $day );
+       }
+
        public static function getMainHTML( $date, $input_name, $is_mandatory, 
$is_disabled, $other_args ) {
                global $sfgTabIndex, $wgAmericanDates;
 
@@ -66,42 +137,7 @@
                                $month = $date['month'];
                                $day = $date['day'];
                        } else {
-                               // handle 'default=now'
-                               if ( $date == 'now' ) {
-                                       global $wgLocaltimezone;
-                                       if ( isset( $wgLocaltimezone ) ) {
-                                               $serverTimezone = 
date_default_timezone_get();
-                                               date_default_timezone_set( 
$wgLocaltimezone );
-                                       }
-                                       $date = date( 'Y/m/d' );
-                                       if ( isset( $wgLocaltimezone ) ) {
-                                               date_default_timezone_set( 
$serverTimezone );
-                                       }
-                               }
-                               $actual_date = new SMWTimeValue( '_dat' );
-                               $actual_date->setUserValue( $date );
-                               $year = $actual_date->getYear();
-                               // TODO - the code to convert from negative to
-                               // BC notation should be in SMW itself.
-                               if ( $year < 0 ) {
-                                       $year = ( $year * - 1 + 1 ) . ' BC';
-                               }
-                               // Use precision of the date to determine
-                               // whether we should also set the month and
-                               // day.
-                               if ( method_exists( 
$actual_date->getDataItem(), 'getPrecision' ) ) {
-                                       $precision = 
$actual_date->getDataItem()->getPrecision();
-                                       if ( $precision > SMWDITime::PREC_Y ) {
-                                               $month = 
$actual_date->getMonth();
-                                       }
-                                       if ( $precision > SMWDITime::PREC_YM ) {
-                                               $day = $actual_date->getDay();
-                                       }
-                               } else {
-                                       // There's some sort of error - make
-                                       // everything blank.
-                                       $year = null;
-                               }
+                               list( $year, $month, $day ) = self::parseDate( 
$date );
                        }
                } else {
                        // Just keep everything at null.
diff --git a/includes/forminputs/SF_DateTimeInput.php 
b/includes/forminputs/SF_DateTimeInput.php
index b910502..b2205b8 100644
--- a/includes/forminputs/SF_DateTimeInput.php
+++ b/includes/forminputs/SF_DateTimeInput.php
@@ -52,9 +52,10 @@
                                        $timezone = $datetime['timezone'];
                                }
                        } else {
-                               // TODO - this should change to use SMW's own
-                               // date-handling class, just like
-                               // dateEntryHTML() does.
+                               // Parse the date.
+                               // We get only the time data here - the main
+                               // date data is handled by the call to
+                               // parent::getMainHTML().
 
                                // Handle 'default=now'.
                                if ( $datetime == 'now' ) {

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I0ed868af0a8a6af0c201bc0ab29ad5f4db52d8ef
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SemanticForms
Gerrit-Branch: master
Gerrit-Owner: Yaron Koren <[email protected]>
Gerrit-Reviewer: Yaron Koren <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to