More useful debug output
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 6fc7f16..8319c36 100644 (file)
@@ -214,7 +214,7 @@ function wfArrayDiff2_cmp( $a, $b ) {
        } else {
                reset( $a );
                reset( $b );
-               while( ( list( $keyA, $valueA ) = each( $a ) ) && ( list( $keyB, $valueB ) = each( $b ) ) ) {
+               while( ( list( , $valueA ) = each( $a ) ) && ( list( , $valueB ) = each( $b ) ) ) {
                        $cmp = strcmp( $valueA, $valueB );
                        if ( $cmp !== 0 ) {
                                return $cmp;
@@ -425,13 +425,11 @@ function wfErrorLog( $text, $file ) {
                # Needs the sockets extension
                if ( preg_match( '!^(tcp|udp):(?://)?\[([0-9a-fA-F:]+)\]:(\d+)(?:/(.*))?$!', $file, $m ) ) {
                        // IPv6 bracketed host
-                       $protocol = $m[1];
                        $host = $m[2];
                        $port = intval( $m[3] );
                        $prefix = isset( $m[4] ) ? $m[4] : false;
                        $domain = AF_INET6;
                } elseif ( preg_match( '!^(tcp|udp):(?://)?([a-zA-Z0-9.-]+):(\d+)(?:/(.*))?$!', $file, $m ) ) {
-                       $protocol = $m[1];
                        $host = $m[2];
                        if ( !IP::isIPv4( $host ) ) {
                                $host = gethostbyname( $host );
@@ -611,6 +609,9 @@ function wfUILang() {
 function wfMessage( $key /*...*/) {
        $params = func_get_args();
        array_shift( $params );
+       if ( isset( $params[0] ) && is_array( $params[0] ) ) {
+               $params = $params[0];
+       }
        return new Message( $key, $params );
 }
 
@@ -853,7 +854,7 @@ function wfMsgWikiHtml( $key ) {
  *   <i>content</i>: fetch message for content language instead of interface
  * Also can accept a single associative argument, of the form 'language' => 'xx':
  *   <i>language</i>: Language object or language code to fetch message for
- *       (overriden by <i>content</i>), its behaviour with parser, parseinline
+ *       (overriden by <i>content</i>), its behaviour with parse, parseinline
  *       and parsemag is undefined.
  * Behavior for conflicting options (e.g., parse+parseinline) is undefined.
  */
@@ -1236,23 +1237,28 @@ function wfNumLink( $offset, $limit, $title, $query = '' ) {
  * @return bool Whereas client accept gzip compression
  */
 function wfClientAcceptsGzip() {
-       if( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
-               # FIXME: we may want to blacklist some broken browsers
-               $m = array();
-               if( preg_match(
-                       '/\bgzip(?:;(q)=([0-9]+(?:\.[0-9]+)))?\b/',
-                       $_SERVER['HTTP_ACCEPT_ENCODING'],
-                       $m )
-               )
-               {
-                       if( isset( $m[2] ) && ( $m[1] == 'q' ) && ( $m[2] == 0 ) ) {
-                               return false;
+       static $result = null;
+       if ( $result === null ) {
+               $result = false;
+               if( isset( $_SERVER['HTTP_ACCEPT_ENCODING'] ) ) {
+                       # FIXME: we may want to blacklist some broken browsers
+                       $m = array();
+                       if( preg_match(
+                               '/\bgzip(?:;(q)=([0-9]+(?:\.[0-9]+)))?\b/',
+                               $_SERVER['HTTP_ACCEPT_ENCODING'],
+                               $m )
+                       )
+                       {
+                               if( isset( $m[2] ) && ( $m[1] == 'q' ) && ( $m[2] == 0 ) ) {
+                                       $result = false;
+                                       return $result;
+                               }
+                               wfDebug( " accepts gzip\n" );
+                               $result = true;
                        }
-                       wfDebug( " accepts gzip\n" );
-                       return true;
                }
        }
-       return false;
+       return $result;
 }
 
 /**
@@ -1281,8 +1287,10 @@ function wfCheckLimits( $deflimit = 50, $optionname = 'rclimit' ) {
  */
 function wfEscapeWikiText( $text ) {
        $text = str_replace(
-               array( '[',     '|',      ']',     '\'',    'ISBN ',     'RFC ',     '://',     "\n=",     '{{' ), # }}
-               array( '&#91;', '&#124;', '&#93;', '&#39;', 'ISBN&#32;', 'RFC&#32;', '&#58;//', "\n&#61;", '&#123;&#123;' ),
+               array( '[',     '|',      ']',     '\'',    'ISBN ',
+                       'RFC ',     '://',     "\n=",     '{{',           '}}' ),
+               array( '&#91;', '&#124;', '&#93;', '&#39;', 'ISBN&#32;',
+                       'RFC&#32;', '&#58;//', "\n&#61;", '&#123;&#123;', '&#125;&#125;' ),
                htmlspecialchars( $text )
        );
        return $text;
@@ -1973,13 +1981,16 @@ define( 'TS_ISO_8601_BASIC', 9 );
  *                    function will autodetect which format is supplied and act
  *                    accordingly.
  * @param $ts Mixed: the timestamp to convert or 0 for the current timestamp
- * @return String: in the format specified in $outputtype
+ * @return Mixed: String / false The same date in the format specified in $outputtype or false
  */
 function wfTimestamp( $outputtype = TS_UNIX, $ts = 0 ) {
        $uts = 0;
        $da = array();
+       $strtime = '';
+
        if ( $ts === 0 ) {
                $uts = time();
+               $strtime = "@$uts";
        } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) {
                # TS_DB
        } elseif ( preg_match( '/^(\d{4}):(\d\d):(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) {
@@ -1989,10 +2000,11 @@ function wfTimestamp( $outputtype = TS_UNIX, $ts = 0 ) {
        } elseif ( preg_match( '/^-?\d{1,13}$/D', $ts ) ) {
                # TS_UNIX
                $uts = $ts;
+               $strtime = "@$ts"; // Undocumented?
        } elseif ( preg_match( '/^\d{2}-\d{2}-\d{4} \d{2}:\d{2}:\d{2}.\d{6}$/', $ts ) ) {
                # TS_ORACLE // session altered to DD-MM-YYYY HH24:MI:SS.FF6
-               $uts = strtotime( preg_replace( '/(\d\d)\.(\d\d)\.(\d\d)(\.(\d+))?/', "$1:$2:$3",
-                               str_replace( '+00:00', 'UTC', $ts ) ) );
+               $strtime = preg_replace( '/(\d\d)\.(\d\d)\.(\d\d)(\.(\d+))?/', "$1:$2:$3",
+                               str_replace( '+00:00', 'UTC', $ts ) );
        } elseif ( preg_match( '/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(?:\.*\d*)?Z$/', $ts, $da ) ) {
                # TS_ISO_8601
        } elseif ( preg_match( '/^(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(?:\.*\d*)?Z$/', $ts, $da ) ) {
@@ -2003,48 +2015,89 @@ function wfTimestamp( $outputtype = TS_UNIX, $ts = 0 ) {
                # TS_POSTGRES
        } elseif (preg_match('/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.\d\d\d$/',$ts,$da)) {
                # TS_DB2
-       } elseif ( preg_match( '/^[A-Z][a-z]{2}, \d\d [A-Z][a-z]{2} \d{4} \d\d:\d\d:\d\d/', $ts ) ) {
-               # TS_RFC2822
-               $uts = strtotime( $ts );
+       } elseif ( preg_match( '/^[ \t\r\n]*([A-Z][a-z]{2},[ \t\r\n]*)?' . # Day of week
+                                                       '\d\d?[ \t\r\n]*[A-Z][a-z]{2}[ \t\r\n]*\d{2}(?:\d{2})?' .  # dd Mon yyyy
+                                                       '[ \t\r\n]*\d\d[ \t\r\n]*:[ \t\r\n]*\d\d[ \t\r\n]*:[ \t\r\n]*\d\d/S', $ts ) ) { # hh:mm:ss
+               # TS_RFC2822, accepting a trailing comment. See http://www.squid-cache.org/mail-archive/squid-users/200307/0122.html / r77171
+               # The regex is a superset of rfc2822 for readability 
+               $strtime = strtok( $ts, ';' );
+       } elseif ( preg_match( '/^[A-Z][a-z]{5,8}, \d\d-[A-Z][a-z]{2}-\d{2} \d\d:\d\d:\d\d/', $ts ) ) {
+               # TS_RFC850
+               $strtime = $ts;
+       } elseif ( preg_match( '/^[A-Z][a-z]{2} [A-Z][a-z]{2} +\d{1,2} \d\d:\d\d:\d\d \d{4}/', $ts ) ) {
+               # asctime
+               $strtime = $ts;
        } else {
                # Bogus value; fall back to the epoch...
                wfDebug("wfTimestamp() fed bogus time value: $outputtype; $ts\n");
-               $uts = 0;
+               
+               return false;
        }
 
-       if (count( $da ) ) {
-               // Warning! gmmktime() acts oddly if the month or day is set to 0
-               // We may want to handle that explicitly at some point
-               $uts = gmmktime( (int)$da[4], (int)$da[5], (int)$da[6],
-                       (int)$da[2], (int)$da[3], (int)$da[1] );
+
+
+       static $formats = array(
+               TS_UNIX => 'U',
+               TS_MW => 'YmdHis',
+               TS_DB => 'Y-m-d H:i:s',
+               TS_ISO_8601 => 'Y-m-d\TH:i:s\Z',
+               TS_ISO_8601_BASIC => 'Ymd\THis\Z',
+               TS_EXIF => 'Y:m:d H:i:s', // This shouldn't ever be used, but is included for completeness
+               TS_RFC2822 => 'D, d M Y H:i:s',
+               TS_ORACLE => 'd-m-Y H:i:s.000000', // Was 'd-M-y h.i.s A' . ' +00:00' before r51500
+               TS_POSTGRES => 'Y-m-d H:i:s',
+               TS_DB2 => 'Y-m-d H:i:s',
+       );
+
+       if ( !isset( $formats[$outputtype] ) ) {
+               throw new MWException( 'wfTimestamp() called with illegal output type.' );
        }
 
-       switch( $outputtype ) {
-               case TS_UNIX:
+       if ( function_exists( "date_create" ) ) {
+               if ( count( $da ) ) {
+                       $ds = sprintf("%04d-%02d-%02dT%02d:%02d:%02d.00+00:00",
+                               (int)$da[1], (int)$da[2], (int)$da[3],
+                               (int)$da[4], (int)$da[5], (int)$da[6]);
+
+                       $d = date_create( $ds, new DateTimeZone( 'GMT' ) );
+               } elseif ( $strtime ) {
+                       $d = date_create( $strtime, new DateTimeZone( 'GMT' ) );
+               } else {
+                       return false;
+               }
+               
+               if ( !$d ) {
+                       wfDebug("wfTimestamp() fed bogus time value: $outputtype; $ts\n");
+                       return false;
+               }
+               
+               $output = $d->format( $formats[$outputtype] );
+       } else {
+               if ( count( $da ) ) {
+                       // Warning! gmmktime() acts oddly if the month or day is set to 0
+                       // We may want to handle that explicitly at some point
+                       $uts = gmmktime( (int)$da[4], (int)$da[5], (int)$da[6],
+                               (int)$da[2], (int)$da[3], (int)$da[1] );
+               } elseif ( $strtime ) {
+                       $uts = strtotime( $strtime );
+               }
+
+               if ( $uts === false ) {
+                       wfDebug("wfTimestamp() can't parse the timestamp (non 32-bit time? Update php): $outputtype; $ts\n");
+                       return false;
+               }
+
+               if ( TS_UNIX == $outputtype ) {
                        return $uts;
-               case TS_MW:
-                       return gmdate( 'YmdHis', $uts );
-               case TS_DB:
-                       return gmdate( 'Y-m-d H:i:s', $uts );
-               case TS_ISO_8601:
-                       return gmdate( 'Y-m-d\TH:i:s\Z', $uts );
-               case TS_ISO_8601_BASIC:
-                       return gmdate( 'Ymd\THis\Z', $uts );
-               // This shouldn't ever be used, but is included for completeness
-               case TS_EXIF:
-                       return gmdate( 'Y:m:d H:i:s', $uts );
-               case TS_RFC2822:
-                       return gmdate( 'D, d M Y H:i:s', $uts ) . ' GMT';
-               case TS_ORACLE:
-                       return gmdate( 'd-m-Y H:i:s.000000', $uts );
-                       //return gmdate( 'd-M-y h.i.s A', $uts ) . ' +00:00';
-               case TS_POSTGRES:
-                       return gmdate( 'Y-m-d H:i:s', $uts ) . ' GMT';
-               case TS_DB2:
-                       return gmdate( 'Y-m-d H:i:s', $uts );
-               default:
-                       throw new MWException( 'wfTimestamp() called with illegal output type.' );
+               }
+               $output = gmdate( $formats[$outputtype], $uts );
+       }
+
+       if ( ( $outputtype == TS_RFC2822 ) || ( $outputtype == TS_POSTGRES ) ) {
+               $output .= ' GMT';
        }
+
+       return $output;
 }
 
 /**
@@ -2187,7 +2240,7 @@ function wfGetSiteNotice() {
 
 /**
  * BC wrapper for MimeMagic::singleton()
- * @deprecated
+ * @deprecated No longer needed as of 1.17 (r68836).
  */
 function &wfGetMimeMagic() {
        wfDeprecated( __FUNCTION__ );
@@ -2457,7 +2510,7 @@ function wfDl( $extension ) {
  * @param $cmd String Command line, properly escaped for shell.
  * @param &$retval optional, will receive the program's exit code.
  *                 (non-zero is usually failure)
- * @param $environ Array optional environment variables which should be 
+ * @param $environ Array optional environment variables which should be
  *                 added to the executed command environment.
  * @return collected stdout as a string (trailing newlines stripped)
  */
@@ -2492,22 +2545,22 @@ function wfShellExec( $cmd, &$retval = null, $environ = array() ) {
        $envcmd = '';
        foreach( $environ as $k => $v ) {
                if ( wfIsWindows() ) {
-                       /* Surrounding a set in quotes (method used by wfEscapeShellArg) makes the quotes themselves 
-                        * appear in the environment variable, so we must use carat escaping as documented in 
-                        * http://technet.microsoft.com/en-us/library/cc723564.aspx 
-                        * Note however that the quote isn't listed there, but is needed, and the parentheses 
+                       /* Surrounding a set in quotes (method used by wfEscapeShellArg) makes the quotes themselves
+                        * appear in the environment variable, so we must use carat escaping as documented in
+                        * http://technet.microsoft.com/en-us/library/cc723564.aspx
+                        * Note however that the quote isn't listed there, but is needed, and the parentheses
                         * are listed there but doesn't appear to need it.
                         */
                        $envcmd .= "set $k=" . preg_replace( '/([&|()<>^"])/', '^\\1', $v ) . ' && ';
                } else {
-                       /* Assume this is a POSIX shell, thus required to accept variable assignments before the command 
+                       /* Assume this is a POSIX shell, thus required to accept variable assignments before the command
                         * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_09_01
                         */
                        $envcmd .= "$k=" . escapeshellarg( $v ) . ' ';
                }
        }
        $cmd = $envcmd . $cmd;
-       
+
        if ( wfIsWindows() ) {
                if ( version_compare( PHP_VERSION, '5.3.0', '<' ) && /* Fixed in 5.3.0 :) */
                        ( version_compare( PHP_VERSION, '5.2.1', '>=' ) || php_uname( 's' ) == 'Windows NT' ) )
@@ -3153,7 +3206,7 @@ function wfFindFile( $title, $options = array() ) {
 /**
  * Get an object referring to a locally registered file.
  * Returns a valid placeholder object if the file does not exist.
- * @param $title Either a string or Title object
+ * @param $title Title or String
  * @return File, or null if passed an invalid Title
  */
 function wfLocalFile( $title ) {
@@ -3443,8 +3496,8 @@ function wfObjectToArray( $object, $recursive = true ) {
 function wfMemoryLimit() {
        global $wgMemoryLimit;
        $memlimit = wfShorthandToInteger( ini_get( 'memory_limit' ) );
-       $conflimit = wfShorthandToInteger( $wgMemoryLimit );
        if( $memlimit != -1 ) {
+               $conflimit = wfShorthandToInteger( $wgMemoryLimit );
                if( $conflimit == -1 ) {
                        wfDebug( "Removing PHP's memory limit\n" );
                        wfSuppressWarnings();
@@ -3469,17 +3522,22 @@ function wfMemoryLimit() {
  */
 function wfShorthandToInteger( $string = '' ) {
        $string = trim( $string );
-       if( empty( $string ) ) {
+       if( $string === '' ) {
                return -1;
        }
-       $last = strtolower( $string[strlen( $string ) - 1] );
+       $last = $string[strlen( $string ) - 1];
        $val = intval( $string );
        switch( $last ) {
                case 'g':
+               case 'G':
                        $val *= 1024;
+                       // break intentionally missing
                case 'm':
+               case 'M':
                        $val *= 1024;
+                       // break intentionally missing
                case 'k':
+               case 'K':
                        $val *= 1024;
        }
 
@@ -3527,20 +3585,20 @@ function wfArrayMap( $function, $input ) {
 
 /**
  * Returns the PackageRepository object for interaction with the package repository.
- * 
- * TODO: Make the repository type also configurable. 
- * 
+ *
+ * TODO: Make the repository type also configurable.
+ *
  * @since 1.17
- * 
+ *
  * @return PackageRepository
  */
 function wfGetRepository() {
        global $wgRepositoryApiLocation;
        static $repository = false;
-       
+
        if ( $repository === false ) {
                $repository = new DistributionRepository( $wgRepositoryApiLocation );
        }
-       
+
        return $repository;
 }