Umherirrender has uploaded a new change for review.

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


Change subject: Added wf[Local]DateTime to create DateTime objects
......................................................................

Added wf[Local]DateTime to create DateTime objects

This avoids use of date(), which needs the local time zone corretly set
on the server, which is assumed at the moment in Setup.php

Change-Id: I812aa013be2f4380e0cf10dc465202756fe8347b
---
M includes/GlobalFunctions.php
M includes/Pager.php
M includes/Preferences.php
M includes/UserMailer.php
M includes/Xml.php
M includes/actions/HistoryAction.php
M includes/db/DatabaseOracle.php
M includes/filerepo/FileRepo.php
M includes/parser/Parser.php
M includes/specials/SpecialContributions.php
M includes/specials/SpecialVersion.php
A tests/phpunit/includes/GlobalFunctions/wfDateTimeTest.php
12 files changed, 105 insertions(+), 47 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/21/71121/1

diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php
index a44e45b..79f8e1f 100644
--- a/includes/GlobalFunctions.php
+++ b/includes/GlobalFunctions.php
@@ -2413,6 +2413,44 @@
 }
 
 /**
+ * Get a DateTime object with the local timezone
+ *
+ * @param $ts int: unix timestamp, default 0 for the current time
+ * @return Mixed: DateTime / false A DateTime object or false
+ */
+function wfLocalDateTime( $ts = 0 ) {
+       global $wgLocaltimezone;
+
+       try {
+               $localDateTime = new DateTime( null, new DateTimeZone( 
$wgLocaltimezone ) );
+               $localDateTime->setTimestamp( $ts );
+               return $localDateTime;
+       } catch ( Exception $e ) {
+               wfDebug( "wfLocalDateTime() fed bogus time value: " .
+                       "VALUE=" . $ts . "; MESSAGE=" . $e->getMessage() . "\n" 
);
+               return false;
+       }
+}
+
+/**
+ * Get a DateTime object in UTC
+ *
+ * @param $ts int: unix timestamp, default 0 for the current time
+ * @return Mixed: DateTime / false A DateTime object or false
+ */
+function wfDateTime( $ts = 0 ) {
+       try {
+               $dateTime = new DateTime( null, new DateTimeZone( 'UTC' ) );
+               $dateTime->setTimestamp( $ts );
+               return $dateTime;
+       } catch ( Exception $e ) {
+               wfDebug( "wfDateTime() fed bogus time value: " .
+                       "VALUE=" . $ts . "; MESSAGE=" . $e->getMessage() . "\n" 
);
+               return false;
+       }
+}
+
+/**
  * Check if the operating system is Windows
  *
  * @return Bool: true if it's Windows, False otherwise.
diff --git a/includes/Pager.php b/includes/Pager.php
index c2bd364..8fa20d0 100644
--- a/includes/Pager.php
+++ b/includes/Pager.php
@@ -872,9 +872,10 @@
                        $year = $this->mYear;
                } else {
                        // If no year given, assume the current one
-                       $year = gmdate( 'Y' );
+                       $dateTime = wfDateTime();
+                       $year = $dateTime->format( 'Y' );
                        // If this month hasn't happened yet this year, go back 
to last year's month
-                       if ( $this->mMonth > gmdate( 'n' ) ) {
+                       if ( $this->mMonth > $dateTime->format( 'n' ) ) {
                                $year--;
                        }
                }
diff --git a/includes/Preferences.php b/includes/Preferences.php
index 848cd32..5481042 100644
--- a/includes/Preferences.php
+++ b/includes/Preferences.php
@@ -635,7 +635,7 @@
                        # Timezone offset can vary with DST
                        $userTZ = timezone_open( $tz[2] );
                        if ( $userTZ !== false ) {
-                               $minDiff = floor( timezone_offset_get( $userTZ, 
date_create( 'now' ) ) / 60 );
+                               $minDiff = floor( timezone_offset_get( $userTZ, 
wfLocalDateTime() ) / 60 );
                                $tzSetting = "ZoneInfo|$minDiff|{$tz[2]}";
                        }
                }
@@ -1275,10 +1275,11 @@
        static function getTimezoneOptions( IContextSource $context ) {
                $opt = array();
 
-               global $wgLocalTZoffset, $wgLocaltimezone;
-               // Check that $wgLocalTZoffset is the same as $wgLocaltimezone
-               if ( $wgLocalTZoffset == date( 'Z' ) / 60 ) {
-                       $server_tz_msg = $context->msg( 
'timezoneuseserverdefault', $wgLocaltimezone )->text();
+               global $wgLocalTZoffset;
+               $localDateTime = wfLocalDateTime();
+               // Check that $wgLocalTZoffset is the same as the local time 
zone offset
+               if ( $wgLocalTZoffset == $localDateTime->format( 'Z' ) / 60 ) {
+                       $server_tz_msg = $context->msg( 
'timezoneuseserverdefault', $localDateTime->getTimezone()->getName() )->text();
                } else {
                        $tzstring = sprintf( '%+03d:%02d', floor( 
$wgLocalTZoffset / 60 ), abs( $wgLocalTZoffset ) % 60 );
                        $server_tz_msg = $context->msg( 
'timezoneuseserverdefault', $tzstring )->text();
@@ -1308,8 +1309,6 @@
                        $prefill = array_fill_keys( array_values( $tzRegions ), 
array() );
                        $opt = array_merge( $opt, $prefill );
 
-                       $now = date_create( 'now' );
-
                        foreach ( $tzs as $tz ) {
                                $z = explode( '/', $tz, 2 );
 
@@ -1323,7 +1322,7 @@
                                # Localize region
                                $z[0] = $tzRegions[$z[0]];
 
-                               $minDiff = floor( timezone_offset_get( 
timezone_open( $tz ), $now ) / 60 );
+                               $minDiff = floor( timezone_offset_get( 
timezone_open( $tz ), $localDateTime ) / 60 );
 
                                $display = str_replace( '_', ' ', $z[0] . '/' . 
$z[1] );
                                $value = "ZoneInfo|$minDiff|$tz";
diff --git a/includes/UserMailer.php b/includes/UserMailer.php
index 2a549ac..54ef41f 100644
--- a/includes/UserMailer.php
+++ b/includes/UserMailer.php
@@ -241,7 +241,7 @@
                        $headers['Reply-To'] = $replyto->toString();
                }
 
-               $headers['Date'] = date( 'r' );
+               $headers['Date'] = wfLocalDateTime()->format( 'r' );
                $headers['Message-ID'] = self::makeMsgId();
                $headers['X-Mailer'] = 'MediaWiki mailer';
 
diff --git a/includes/Xml.php b/includes/Xml.php
index 223061a..11e5f4c 100644
--- a/includes/Xml.php
+++ b/includes/Xml.php
@@ -195,8 +195,9 @@
                if ( $year ) {
                        $encYear = intval( $year );
                } elseif ( $encMonth ) {
-                       $thisMonth = intval( gmdate( 'n' ) );
-                       $thisYear = intval( gmdate( 'Y' ) );
+                       $dateTime = wfDateTime();
+                       $thisMonth = intval( $dateTime->format( 'n' ) );
+                       $thisYear = intval( $dateTime->format( 'Y' ) );
                        if ( intval( $encMonth ) > $thisMonth ) {
                                $thisYear--;
                        }
diff --git a/includes/actions/HistoryAction.php 
b/includes/actions/HistoryAction.php
index f43736b..964f51d 100644
--- a/includes/actions/HistoryAction.php
+++ b/includes/actions/HistoryAction.php
@@ -178,7 +178,7 @@
                        ) .
                        Html::hidden( 'title', 
$this->getTitle()->getPrefixedDBkey() ) . "\n" .
                        Html::hidden( 'action', 'history' ) . "\n" .
-                       Xml::dateMenu( ( $year == null ? date( "Y" ) : $year ), 
$month ) . ' ' .
+                       Xml::dateMenu( ( $year == null ? 
wfLocalDateTime()->format( 'Y' ) : $year ), $month ) . ' ' .
                        ( $tagSelector ? ( implode( ' ', $tagSelector ) . 
' ' ) : '' ) .
                        $checkDeleted .
                        Xml::submitButton( $this->msg( 'allpagessubmit' 
)->text() ) . "\n" .
diff --git a/includes/db/DatabaseOracle.php b/includes/db/DatabaseOracle.php
index c0d3805..f40d88c 100644
--- a/includes/db/DatabaseOracle.php
+++ b/includes/db/DatabaseOracle.php
@@ -341,7 +341,7 @@
                $union_unique = ( preg_match( '/\/\* UNION_UNIQUE \*\/ /', $sql 
) != 0 );
                // EXPLAIN syntax in Oracle is EXPLAIN PLAN FOR and it return 
nothing
                // you have to select data from plan table after explain
-               $explain_id = date( 'dmYHis' );
+               $explain_id = wfLocalDateTime()->format( 'dmYHis' );
 
                $sql = preg_replace( '/^EXPLAIN /', 'EXPLAIN PLAN SET 
STATEMENT_ID = \'' . $explain_id . '\' FOR', $sql, 1, $explain_count );
 
diff --git a/includes/filerepo/FileRepo.php b/includes/filerepo/FileRepo.php
index b82a0b8..e63c246 100644
--- a/includes/filerepo/FileRepo.php
+++ b/includes/filerepo/FileRepo.php
@@ -965,7 +965,7 @@
        public function storeTemp( $originalName, $srcPath ) {
                $this->assertWritableRepo(); // fail out if read-only
 
-               $date = gmdate( "YmdHis" );
+               $date = wfDateTime()->format( 'YmdHis' );
                $hashPath = $this->getHashPath( $originalName );
                $dstUrlRel = $hashPath . $date . '!' . rawurlencode( 
$originalName );
                $virtualUrl = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php
index 1c69208..abbbb9c 100644
--- a/includes/parser/Parser.php
+++ b/includes/parser/Parser.php
@@ -2685,46 +2685,46 @@
 
                switch ( $index ) {
                        case 'currentmonth':
-                               $value = $pageLang->formatNum( gmdate( 'm', $ts 
) );
+                               $value = $pageLang->formatNum( wfDateTime( $ts 
)->format( 'm' ) );
                                break;
                        case 'currentmonth1':
-                               $value = $pageLang->formatNum( gmdate( 'n', $ts 
) );
+                               $value = $pageLang->formatNum( wfDateTime( $ts 
)->format( 'n' ) );
                                break;
                        case 'currentmonthname':
-                               $value = $pageLang->getMonthName( gmdate( 'n', 
$ts ) );
+                               $value = $pageLang->getMonthName( wfDateTime( 
$ts )->format( 'n' ) );
                                break;
                        case 'currentmonthnamegen':
-                               $value = $pageLang->getMonthNameGen( gmdate( 
'n', $ts ) );
+                               $value = $pageLang->getMonthNameGen( 
wfDateTime( $ts )->format( 'n' ) );
                                break;
                        case 'currentmonthabbrev':
-                               $value = $pageLang->getMonthAbbreviation( 
gmdate( 'n', $ts ) );
+                               $value = $pageLang->getMonthAbbreviation( 
wfDateTime( $ts )->format( 'n' ) );
                                break;
                        case 'currentday':
-                               $value = $pageLang->formatNum( gmdate( 'j', $ts 
) );
+                               $value = $pageLang->formatNum( wfDateTime( $ts 
)->format( 'j' ) );
                                break;
                        case 'currentday2':
-                               $value = $pageLang->formatNum( gmdate( 'd', $ts 
) );
+                               $value = $pageLang->formatNum( wfDateTime( $ts 
)->format( 'd' ) );
                                break;
                        case 'localmonth':
-                               $value = $pageLang->formatNum( date( 'm', $ts ) 
);
+                               $value = $pageLang->formatNum( wfLocalDateTime( 
$ts )->format( 'm' ) );
                                break;
                        case 'localmonth1':
-                               $value = $pageLang->formatNum( date( 'n', $ts ) 
);
+                               $value = $pageLang->formatNum( wfLocalDateTime( 
$ts )->format( 'n' ) );
                                break;
                        case 'localmonthname':
-                               $value = $pageLang->getMonthName( date( 'n', 
$ts ) );
+                               $value = $pageLang->getMonthName( 
wfLocalDateTime( $ts )->format( 'n' ) );
                                break;
                        case 'localmonthnamegen':
-                               $value = $pageLang->getMonthNameGen( date( 'n', 
$ts ) );
+                               $value = $pageLang->getMonthNameGen( 
wfLocalDateTime( $ts )->format( 'n' ) );
                                break;
                        case 'localmonthabbrev':
-                               $value = $pageLang->getMonthAbbreviation( date( 
'n', $ts ) );
+                               $value = $pageLang->getMonthAbbreviation( 
wfLocalDateTime( $ts )->format( 'n' ) );
                                break;
                        case 'localday':
-                               $value = $pageLang->formatNum( date( 'j', $ts ) 
);
+                               $value = $pageLang->formatNum( wfLocalDateTime( 
$ts )->format( 'j' ) );
                                break;
                        case 'localday2':
-                               $value = $pageLang->formatNum( date( 'd', $ts ) 
);
+                               $value = $pageLang->formatNum( wfLocalDateTime( 
$ts )->format( 'd' ) );
                                break;
                        case 'pagename':
                                $value = wfEscapeWikiText( 
$this->mTitle->getText() );
@@ -2870,44 +2870,44 @@
                                $value = ( wfUrlencode( 
$this->mTitle->getSubjectNsText() ) );
                                break;
                        case 'currentdayname':
-                               $value = $pageLang->getWeekdayName( gmdate( 
'w', $ts ) + 1 );
+                               $value = $pageLang->getWeekdayName( wfDateTime( 
$ts )->format( 'w' ) + 1 );
                                break;
                        case 'currentyear':
-                               $value = $pageLang->formatNum( gmdate( 'Y', $ts 
), true );
+                               $value = $pageLang->formatNum( wfDateTime( $ts 
)->format( 'Y' ), true );
                                break;
                        case 'currenttime':
                                $value = $pageLang->time( wfTimestamp( TS_MW, 
$ts ), false, false );
                                break;
                        case 'currenthour':
-                               $value = $pageLang->formatNum( gmdate( 'H', $ts 
), true );
+                               $value = $pageLang->formatNum( wfDateTime( $ts 
)->format( 'H' ), true );
                                break;
                        case 'currentweek':
                                # @bug 4594 PHP5 has it zero padded, PHP4 does 
not, cast to
                                # int to remove the padding
-                               $value = $pageLang->formatNum( (int)gmdate( 
'W', $ts ) );
+                               $value = $pageLang->formatNum( (int)wfDateTime( 
$ts )->format( 'W' ) );
                                break;
                        case 'currentdow':
-                               $value = $pageLang->formatNum( gmdate( 'w', $ts 
) );
+                               $value = $pageLang->formatNum( wfDateTime( $ts 
)->format( 'w' ) );
                                break;
                        case 'localdayname':
-                               $value = $pageLang->getWeekdayName( date( 'w', 
$ts ) + 1 );
+                               $value = $pageLang->getWeekdayName( 
wfLocalDateTime( $ts )->format( 'w' ) + 1 );
                                break;
                        case 'localyear':
-                               $value = $pageLang->formatNum( date( 'Y', $ts 
), true );
+                               $value = $pageLang->formatNum( wfLocalDateTime( 
$ts )->format( 'Y' ), true );
                                break;
                        case 'localtime':
-                               $value = $pageLang->time( date( 'YmdHis', $ts 
), false, false );
+                               $value = $pageLang->time( wfLocalDateTime( $ts 
)->format( 'YmdHis' ), false, false );
                                break;
                        case 'localhour':
-                               $value = $pageLang->formatNum( date( 'H', $ts 
), true );
+                               $value = $pageLang->formatNum( wfLocalDateTime( 
$ts )->format( 'H' ), true );
                                break;
                        case 'localweek':
                                # @bug 4594 PHP5 has it zero padded, PHP4 does 
not, cast to
                                # int to remove the padding
-                               $value = $pageLang->formatNum( (int)date( 'W', 
$ts ) );
+                               $value = $pageLang->formatNum( 
(int)wfLocalDateTime( $ts )->format( 'W' ) );
                                break;
                        case 'localdow':
-                               $value = $pageLang->formatNum( date( 'w', $ts ) 
);
+                               $value = $pageLang->formatNum( wfLocalDateTime( 
$ts )->format( 'w' ) );
                                break;
                        case 'numberofarticles':
                                $value = $pageLang->formatNum( 
SiteStats::articles() );
@@ -2938,7 +2938,7 @@
                                $value = wfTimestamp( TS_MW, $ts );
                                break;
                        case 'localtimestamp':
-                               $value = date( 'YmdHis', $ts );
+                               $value = wfLocalDateTime( $ts )->format( 
'YmdHis' );
                                break;
                        case 'currentversion':
                                $value = SpecialVersion::getVersion();
@@ -4524,10 +4524,11 @@
                # (see also bug 12815)
                $ts = $this->mOptions->getTimestamp();
                $unixts = wfTimestamp( TS_UNIX, $ts );
-               $ts = date( 'YmdHis', $unixts );
-               $tzMsg = date( 'T', $unixts );  # might vary on DST changeover!
+               $localDateTime = wfLocalDateTime( $unixts );
+               $ts = $localDateTime->format( 'YmdHis' );
+               $tzMsg = $localDateTime->format( 'T' );  # might vary on DST 
changeover!
 
-               # Allow translation of timezones through wiki. date() can return
+               # Allow translation of timezones through wiki. format() can 
return
                # whatever crap the system uses, localised or not, so we cannot
                # ship premade translations.
                $key = 'timezone-' . strtolower( trim( $tzMsg ) );
diff --git a/includes/specials/SpecialContributions.php 
b/includes/specials/SpecialContributions.php
index 2f37cb4..d9884c4 100644
--- a/includes/specials/SpecialContributions.php
+++ b/includes/specials/SpecialContributions.php
@@ -567,7 +567,7 @@
 
                $dateSelectionAndSubmit = Xml::tags( 'td', array( 'colspan' => 
2 ),
                        Xml::dateMenu(
-                               $this->opts['year'] === '' ? gmdate( 'Y' ) : 
$this->opts['year'],
+                               $this->opts['year'] === '' ? 
wfDateTime()->format( 'Y' ) : $this->opts['year'],
                                $this->opts['month']
                        ) . ' ' .
                                Xml::submitButton(
diff --git a/includes/specials/SpecialVersion.php 
b/includes/specials/SpecialVersion.php
index c257dd4..506e9ad 100644
--- a/includes/specials/SpecialVersion.php
+++ b/includes/specials/SpecialVersion.php
@@ -129,7 +129,7 @@
                        'Timo Tijhof', 'Daniel Kinzler', 'Jeroen De Dauw', 
$othersLink
                );
 
-               return wfMessage( 'version-poweredby-credits', date( 'Y' ),
+               return wfMessage( 'version-poweredby-credits', 
wfLocalDateTime()->format( 'Y' ),
                        $wgLang->listToText( $authorList ) )->text();
        }
 
diff --git a/tests/phpunit/includes/GlobalFunctions/wfDateTimeTest.php 
b/tests/phpunit/includes/GlobalFunctions/wfDateTimeTest.php
new file mode 100644
index 0000000..7fa40ee
--- /dev/null
+++ b/tests/phpunit/includes/GlobalFunctions/wfDateTimeTest.php
@@ -0,0 +1,18 @@
+<?php
+/*
+ * Tests for wfDateTime() and wfLocalDateTime();
+ */
+class WfDateTimeTest extends MediaWikiTestCase {
+
+       function testLocalTimezone() {
+               global $wgLocaltimezone;
+               $dateTime = wfLocalDateTime();
+               $this->assertEquals( $wgLocaltimezone, 
$dateTime->getTimezone()->getName(), "wfLocalDateTime returns local time zone" 
);
+               $this->assertEquals( date( 'T' ), 
$dateTime->getTimezone()->getName(), "wfLocalDateTime returns local time zone" 
);
+       }
+
+       function testUTCTimezone() {
+               $dateTime = wfDateTime();
+               $this->assertEquals( 'UTC', 
$dateTime->getTimezone()->getName(), "wfDateTime returns UTC time zone" );
+       }
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I812aa013be2f4380e0cf10dc465202756fe8347b
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Umherirrender <umherirrender_de...@web.de>

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

Reply via email to