r99863 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99862‎ | r99863 | r99864 >
Date:09:32, 15 October 2011
Author:ialex
Status:resolved (Comments)
Tags:
Comment:
* Added Language::userDate(), Language::userTime() and Language::userTimeAndDate() as new versions of respectively Language::date(), Language::time() and Language::timeanddate(), which take a User object in the second parameter to get time correction and format instead of having to do this when calling the methods and not wanting to user $wgUser's parameters.
* Moved Language::userAdjust() near other related function
* Updated ENotif stuff to use that functions so that the date format is not taken from the sending user but the receiver
* Removed $PAGEEDITDATEANDTIME parameter, unsed and not present in impersonal notification
* Also fixed the debug line in UserMailer::send()
Modified paths:
  • /trunk/phase3/includes/UserMailer.php (modified) (history)
  • /trunk/phase3/languages/Language.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/UserMailer.php
@@ -158,7 +158,7 @@
159159 global $wgEnotifMaxRecips, $wgAdditionalMailParams;
160160
161161 $emails = '';
162 - wfDebug( __METHOD__ . ': sending mail to ' . implode( ',', array( $to->toString() ) ) . "\n" );
 162+ wfDebug( __METHOD__ . ': sending mail to ' . is_array( $to ) ? implode( ', ', $to ) : $to . "\n" );
163163
164164 $headers['From'] = $from->toString();
165165 $headers['Return-Path'] = $from->toString();
@@ -571,8 +571,14 @@
572572 $keys = array();
573573
574574 if ( $this->oldid ) {
575 - $difflink = $this->title->getCanonicalUrl( 'diff=0&oldid=' . $this->oldid );
576 - $keys['$NEWPAGE'] = wfMsgForContent( 'enotif_lastvisited', $difflink );
 575+ if ( $wgEnotifImpersonal ) {
 576+ // For impersonal mail, show a diff link to the last revision.
 577+ $keys['$NEWPAGE'] = wfMsgForContent( 'enotif_lastdiff',
 578+ $this->title->getCanonicalUrl( 'diff=next&oldid=' . $this->oldid ) );
 579+ } else {
 580+ $keys['$NEWPAGE'] = wfMsgForContent( 'enotif_lastvisited',
 581+ $this->title->getCanonicalUrl( 'diff=0&oldid=' . $this->oldid ) );
 582+ }
577583 $keys['$OLDID'] = $this->oldid;
578584 $keys['$CHANGEDORCREATED'] = wfMsgForContent( 'changed' );
579585 } else {
@@ -582,15 +588,6 @@
583589 $keys['$CHANGEDORCREATED'] = wfMsgForContent( 'created' );
584590 }
585591
586 - if ( $wgEnotifImpersonal && $this->oldid ) {
587 - /**
588 - * For impersonal mail, show a diff link to the last
589 - * revision.
590 - */
591 - $keys['$NEWPAGE'] = wfMsgForContent( 'enotif_lastdiff',
592 - $this->title->getCanonicalUrl( "oldid={$this->oldid}&diff=next" ) );
593 - }
594 -
595592 $body = strtr( $body, $keys );
596593 $pagetitle = $this->title->getPrefixedText();
597594 $keys['$PAGETITLE'] = $pagetitle;
@@ -691,22 +688,18 @@
692689 // Note: The to parameter cannot be an address in the form of "Something <someone@example.com>".
693690 // The mail command will not parse this properly while talking with the MTA.
694691 $to = new MailAddress( $watchingUser );
695 - $name = $wgEnotifUseRealName ? $watchingUser->getRealName() : $watchingUser->getName();
696 - $body = str_replace( '$WATCHINGUSERNAME', $name, $this->body );
697692
698 - $timecorrection = $watchingUser->getOption( 'timecorrection' );
699 -
700693 # $PAGEEDITDATE is the time and date of the page change
701694 # expressed in terms of individual local time of the notification
702695 # recipient, i.e. watching user
703696 $body = str_replace(
704 - array( '$PAGEEDITDATEANDTIME',
 697+ array( '$WATCHINGUSERNAME',
705698 '$PAGEEDITDATE',
706699 '$PAGEEDITTIME' ),
707 - array( $wgContLang->timeanddate( $this->timestamp, true, false, $timecorrection ),
708 - $wgContLang->date( $this->timestamp, true, false, $timecorrection ),
709 - $wgContLang->time( $this->timestamp, true, false, $timecorrection ) ),
710 - $body );
 700+ array( $wgEnotifUseRealName ? $watchingUser->getRealName() : $watchingUser->getName(),
 701+ $wgContLang->userDate( $this->timestamp, $watchingUser ),
 702+ $wgContLang->userTime( $this->timestamp, $watchingUser ) ),
 703+ $this->body );
711704
712705 return UserMailer::send( $to, $this->from, $this->subject, $body, $this->replyto );
713706 }
@@ -726,8 +719,8 @@
727720 '$PAGEEDITDATE',
728721 '$PAGEEDITTIME' ),
729722 array( wfMsgForContent( 'enotif_impersonal_salutation' ),
730 - $wgContLang->date( $this->timestamp, true, false, false ),
731 - $wgContLang->time( $this->timestamp, true, false, false ) ),
 723+ $wgContLang->date( $this->timestamp, false, false ),
 724+ $wgContLang->time( $this->timestamp, false, false ) ),
732725 $this->body );
733726
734727 return UserMailer::send( $addresses, $this->from, $this->subject, $body, $this->replyto );
Index: trunk/phase3/languages/Language.php
@@ -806,82 +806,6 @@
807807 }
808808
809809 /**
810 - * Used by date() and time() to adjust the time output.
811 - *
812 - * @param $ts Int the time in date('YmdHis') format
813 - * @param $tz Mixed: adjust the time by this amount (default false, mean we
814 - * get user timecorrection setting)
815 - * @return int
816 - */
817 - function userAdjust( $ts, $tz = false ) {
818 - global $wgUser, $wgLocalTZoffset;
819 -
820 - if ( $tz === false ) {
821 - $tz = $wgUser->getOption( 'timecorrection' );
822 - }
823 -
824 - $data = explode( '|', $tz, 3 );
825 -
826 - if ( $data[0] == 'ZoneInfo' ) {
827 - wfSuppressWarnings();
828 - $userTZ = timezone_open( $data[2] );
829 - wfRestoreWarnings();
830 - if ( $userTZ !== false ) {
831 - $date = date_create( $ts, timezone_open( 'UTC' ) );
832 - date_timezone_set( $date, $userTZ );
833 - $date = date_format( $date, 'YmdHis' );
834 - return $date;
835 - }
836 - # Unrecognized timezone, default to 'Offset' with the stored offset.
837 - $data[0] = 'Offset';
838 - }
839 -
840 - $minDiff = 0;
841 - if ( $data[0] == 'System' || $tz == '' ) {
842 - #  Global offset in minutes.
843 - if ( isset( $wgLocalTZoffset ) ) {
844 - $minDiff = $wgLocalTZoffset;
845 - }
846 - } elseif ( $data[0] == 'Offset' ) {
847 - $minDiff = intval( $data[1] );
848 - } else {
849 - $data = explode( ':', $tz );
850 - if ( count( $data ) == 2 ) {
851 - $data[0] = intval( $data[0] );
852 - $data[1] = intval( $data[1] );
853 - $minDiff = abs( $data[0] ) * 60 + $data[1];
854 - if ( $data[0] < 0 ) {
855 - $minDiff = -$minDiff;
856 - }
857 - } else {
858 - $minDiff = intval( $data[0] ) * 60;
859 - }
860 - }
861 -
862 - # No difference ? Return time unchanged
863 - if ( 0 == $minDiff ) {
864 - return $ts;
865 - }
866 -
867 - wfSuppressWarnings(); // E_STRICT system time bitching
868 - # Generate an adjusted date; take advantage of the fact that mktime
869 - # will normalize out-of-range values so we don't have to split $minDiff
870 - # into hours and minutes.
871 - $t = mktime( (
872 - (int)substr( $ts, 8, 2 ) ), # Hours
873 - (int)substr( $ts, 10, 2 ) + $minDiff, # Minutes
874 - (int)substr( $ts, 12, 2 ), # Seconds
875 - (int)substr( $ts, 4, 2 ), # Month
876 - (int)substr( $ts, 6, 2 ), # Day
877 - (int)substr( $ts, 0, 4 ) ); # Year
878 -
879 - $date = date( 'YmdHis', $t );
880 - wfRestoreWarnings();
881 -
882 - return $date;
883 - }
884 -
885 - /**
886810 * This is a workalike of PHP's date() function, but with better
887811 * internationalisation, a reduced set of format characters, and a better
888812 * escaping format.
@@ -1720,6 +1644,82 @@
17211645 }
17221646
17231647 /**
 1648+ * Used by date() and time() to adjust the time output.
 1649+ *
 1650+ * @param $ts Int the time in date('YmdHis') format
 1651+ * @param $tz Mixed: adjust the time by this amount (default false, mean we
 1652+ * get user timecorrection setting)
 1653+ * @return int
 1654+ */
 1655+ function userAdjust( $ts, $tz = false ) {
 1656+ global $wgUser, $wgLocalTZoffset;
 1657+
 1658+ if ( $tz === false ) {
 1659+ $tz = $wgUser->getOption( 'timecorrection' );
 1660+ }
 1661+
 1662+ $data = explode( '|', $tz, 3 );
 1663+
 1664+ if ( $data[0] == 'ZoneInfo' ) {
 1665+ wfSuppressWarnings();
 1666+ $userTZ = timezone_open( $data[2] );
 1667+ wfRestoreWarnings();
 1668+ if ( $userTZ !== false ) {
 1669+ $date = date_create( $ts, timezone_open( 'UTC' ) );
 1670+ date_timezone_set( $date, $userTZ );
 1671+ $date = date_format( $date, 'YmdHis' );
 1672+ return $date;
 1673+ }
 1674+ # Unrecognized timezone, default to 'Offset' with the stored offset.
 1675+ $data[0] = 'Offset';
 1676+ }
 1677+
 1678+ $minDiff = 0;
 1679+ if ( $data[0] == 'System' || $tz == '' ) {
 1680+ #  Global offset in minutes.
 1681+ if ( isset( $wgLocalTZoffset ) ) {
 1682+ $minDiff = $wgLocalTZoffset;
 1683+ }
 1684+ } elseif ( $data[0] == 'Offset' ) {
 1685+ $minDiff = intval( $data[1] );
 1686+ } else {
 1687+ $data = explode( ':', $tz );
 1688+ if ( count( $data ) == 2 ) {
 1689+ $data[0] = intval( $data[0] );
 1690+ $data[1] = intval( $data[1] );
 1691+ $minDiff = abs( $data[0] ) * 60 + $data[1];
 1692+ if ( $data[0] < 0 ) {
 1693+ $minDiff = -$minDiff;
 1694+ }
 1695+ } else {
 1696+ $minDiff = intval( $data[0] ) * 60;
 1697+ }
 1698+ }
 1699+
 1700+ # No difference ? Return time unchanged
 1701+ if ( 0 == $minDiff ) {
 1702+ return $ts;
 1703+ }
 1704+
 1705+ wfSuppressWarnings(); // E_STRICT system time bitching
 1706+ # Generate an adjusted date; take advantage of the fact that mktime
 1707+ # will normalize out-of-range values so we don't have to split $minDiff
 1708+ # into hours and minutes.
 1709+ $t = mktime( (
 1710+ (int)substr( $ts, 8, 2 ) ), # Hours
 1711+ (int)substr( $ts, 10, 2 ) + $minDiff, # Minutes
 1712+ (int)substr( $ts, 12, 2 ), # Seconds
 1713+ (int)substr( $ts, 4, 2 ), # Month
 1714+ (int)substr( $ts, 6, 2 ), # Day
 1715+ (int)substr( $ts, 0, 4 ) ); # Year
 1716+
 1717+ $date = date( 'YmdHis', $t );
 1718+ wfRestoreWarnings();
 1719+
 1720+ return $date;
 1721+ }
 1722+
 1723+ /**
17241724 * This is meant to be used by time(), date(), and timeanddate() to get
17251725 * the date preference they're supposed to use, it should be used in
17261726 * all children.
@@ -1840,6 +1840,113 @@
18411841 }
18421842
18431843 /**
 1844+ * Internal helper function for userDate(), userTime() and userTimeAndDate()
 1845+ *
 1846+ * @param $type String: can be 'date', 'time' or 'both'
 1847+ * @param $ts Mixed: the time format which needs to be turned into a
 1848+ * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
 1849+ * @param $user User object used to get preferences for timezone and format
 1850+ * @param $options Array, can contain the following keys:
 1851+ * - 'timecorrection': time correction, can have the following values:
 1852+ * - true: use user's preference
 1853+ * - false: don't use time correction
 1854+ * - integer: value of time correction in minutes
 1855+ * - 'format': format to use, can have the following values:
 1856+ * - true: use user's preference
 1857+ * - false: use default preference
 1858+ * - string: format to use
 1859+ * @return String
 1860+ */
 1861+ private function internalUserTimeAndDate( $type, $ts, User $user, array $options ) {
 1862+ $ts = wfTimestamp( TS_MW, $ts );
 1863+ $options += array( 'timecorrection' => true, 'format' => true );
 1864+ if ( $options['timecorrection'] !== false ) {
 1865+ if ( $options['timecorrection'] === true ) {
 1866+ $offset = $user->getOption( 'timecorrection' );
 1867+ } else {
 1868+ $offset = $options['timecorrection'];
 1869+ }
 1870+ $ts = $this->userAdjust( $ts, $offset );
 1871+ }
 1872+ if ( $options['format'] === true ) {
 1873+ $format = $user->getDatePreference();
 1874+ } else {
 1875+ $format = $options['format'];
 1876+ }
 1877+ $df = $this->getDateFormatString( $type, $this->dateFormat( $format ) );
 1878+ return $this->sprintfDate( $df, $ts );
 1879+ }
 1880+
 1881+ /**
 1882+ * Get the formatted date for the given timestamp and formatted for
 1883+ * the given user.
 1884+ *
 1885+ * @param $type String: can be 'date', 'time' or 'both'
 1886+ * @param $ts Mixed: the time format which needs to be turned into a
 1887+ * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
 1888+ * @param $user User object used to get preferences for timezone and format
 1889+ * @param $options Array, can contain the following keys:
 1890+ * - 'timecorrection': time correction, can have the following values:
 1891+ * - true: use user's preference
 1892+ * - false: don't use time correction
 1893+ * - integer: value of time correction in minutes
 1894+ * - 'format': format to use, can have the following values:
 1895+ * - true: use user's preference
 1896+ * - false: use default preference
 1897+ * - string: format to use
 1898+ * @return String
 1899+ */
 1900+ public function userDate( $ts, User $user, array $options = array() ) {
 1901+ return $this->internalUserTimeAndDate( 'date', $ts, $user, $options );
 1902+ }
 1903+
 1904+ /**
 1905+ * Get the formatted time for the given timestamp and formatted for
 1906+ * the given user.
 1907+ *
 1908+ * @param $type String: can be 'date', 'time' or 'both'
 1909+ * @param $ts Mixed: the time format which needs to be turned into a
 1910+ * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
 1911+ * @param $user User object used to get preferences for timezone and format
 1912+ * @param $options Array, can contain the following keys:
 1913+ * - 'timecorrection': time correction, can have the following values:
 1914+ * - true: use user's preference
 1915+ * - false: don't use time correction
 1916+ * - integer: value of time correction in minutes
 1917+ * - 'format': format to use, can have the following values:
 1918+ * - true: use user's preference
 1919+ * - false: use default preference
 1920+ * - string: format to use
 1921+ * @return String
 1922+ */
 1923+ public function userTime( $ts, User $user, array $options = array() ) {
 1924+ return $this->internalUserTimeAndDate( 'time', $ts, $user, $options );
 1925+ }
 1926+
 1927+ /**
 1928+ * Get the formatted date and time for the given timestamp and formatted for
 1929+ * the given user.
 1930+ *
 1931+ * @param $type String: can be 'date', 'time' or 'both'
 1932+ * @param $ts Mixed: the time format which needs to be turned into a
 1933+ * date('YmdHis') format with wfTimestamp(TS_MW,$ts)
 1934+ * @param $user User object used to get preferences for timezone and format
 1935+ * @param $options Array, can contain the following keys:
 1936+ * - 'timecorrection': time correction, can have the following values:
 1937+ * - true: use user's preference
 1938+ * - false: don't use time correction
 1939+ * - integer: value of time correction in minutes
 1940+ * - 'format': format to use, can have the following values:
 1941+ * - true: use user's preference
 1942+ * - false: use default preference
 1943+ * - string: format to use
 1944+ * @return String
 1945+ */
 1946+ public function userTimeAndDate( $ts, User $user, array $options = array() ) {
 1947+ return $this->internalUserTimeAndDate( 'both', $ts, $user, $options );
 1948+ }
 1949+
 1950+ /**
18441951 * @param $key string
18451952 * @return array|null
18461953 */

Follow-up revisions

RevisionCommit summaryAuthorDate
r99899Temporarily reverting r99863 to see if I can pin down test failuresdemon19:02, 15 October 2011
r99902Fix E_WARNING from r99863ialex19:22, 15 October 2011
r99951Per Nikerabbit, fix for r99863: removed copy/paste error in method documentationialex13:16, 16 October 2011
r107255@since for r99863robin02:54, 25 December 2011

Comments

#Comment by Reedy (talk | contribs)   16:21, 15 October 2011
-		wfDebug( __METHOD__ . ': sending mail to ' . implode( ',', array( $to->toString() ) ) . "\n" );
+		wfDebug( __METHOD__ . ': sending mail to ' . is_array( $to ) ? implode( ', ', $to ) : $to . "\n" );



Warning: implode(): Invalid arguments passed in /home/reedy/mediawiki/trunk/phase3/includes/UserMailer.php on line 161

Call Stack:
    0.0012     712536   1. {main}() /home/reedy/mediawiki/trunk/extensions/CodeReview/svnImport.php:0
    0.0302    1324040   2. require_once('/home/reedy/mediawiki/trunk/phase3/maintenance/doMaintenance.php') /home/reedy/mediawiki/trunk/extensions/CodeReview/svnImport.php:183
    0.3533   16843688   3. SvnImport->execute() /home/reedy/mediawiki/trunk/phase3/maintenance/doMaintenance.php:105
   91.9781   17274192   4. SvnImport->importRepo() /home/reedy/mediawiki/trunk/extensions/CodeReview/svnImport.php:41
  233.4468   26561288   5. CodeRevision->save() /home/reedy/mediawiki/trunk/extensions/CodeReview/svnImport.php:125
  233.4731   26571992   6. User->sendMail() /home/reedy/mediawiki/trunk/extensions/CodeReview/backend/CodeRevision.php:514
  233.4732   26574120   7. UserMailer::send() /home/reedy/mediawiki/trunk/phase3/includes/User.php:3281
  233.4732   26574632   8. implode() /home/reedy/mediawiki/trunk/phase3/includes/UserMailer.php:161
#Comment by IAlex (talk | contribs)   19:22, 15 October 2011

Fixed in r99902.

#Comment by Nikerabbit (talk | contribs)   10:06, 16 October 2011

Why private? Looks like the docs for new methods are copy-pasted unchanged from the internal method, and refer to $type that doesn't exist.

#Comment by IAlex (talk | contribs)   13:19, 16 October 2011

internalUserTimeAndDate() is private because it's just an helper function for the other three and should not be called diretcly. Documentation fixed in r99951.

Status & tagging log