Add a version number and user-agent string to ForeignAPIRepo.
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 08b8c60..78def7d 100644 (file)
@@ -16,7 +16,7 @@ require_once dirname( __FILE__ ) . '/normal/UtfNormalUtil.php';
 /**
  * Compatibility functions
  *
- * We more or less support PHP 5.0.x and up.
+ * We support PHP 5.1.x and up.
  * Re-implementations of newer functions or functions in non-standard
  * PHP extensions may be included here.
  */
@@ -227,9 +227,10 @@ function wfArrayDiff2_cmp( $a, $b ) {
 /**
  * Seed Mersenne Twister
  * No-op for compatibility; only necessary in PHP < 4.2.0
+ * @deprecated. Remove in 1.18
  */
 function wfSeedRandom() {
-       /* No-op */
+       wfDeprecated(__FUNCTION__);
 }
 
 /**
@@ -424,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 );
@@ -498,7 +497,8 @@ function wfLogProfilingData() {
                $forward = "\t(proxied via {$_SERVER['REMOTE_ADDR']}{$forward})";
        }
        // Don't unstub $wgUser at this late stage just for statistics purposes
-       if( StubObject::isRealObject( $wgUser ) && $wgUser->isAnon() ) {
+       // FIXME: We can detect some anons even if it is not loaded. See User::getId()
+       if( $wgUser->mDataLoaded && $wgUser->isAnon() ) {
                $forward .= ' anon';
        }
        $log = sprintf( "%s\t%04.3f\t%s\n",
@@ -597,6 +597,21 @@ function wfUILang() {
        return wfGetLangObj( !$wgBetterDirectionality );
 }
 
+/**
+ * This is the new function for getting translated interface messages.
+ * See the Message class for documentation how to use them.
+ * The intention is that this function replaces all old wfMsg* functions.
+ * @param $key \string Message key.
+ * Varargs: normal message parameters.
+ * @return \type{Message}
+ * @since 1.17
+ */
+function wfMessage( $key /*...*/) {
+       $params = func_get_args();
+       array_shift( $params );
+       return new Message( $key, $params );
+}
+
 /**
  * Get a message from anywhere, for the current user language.
  *
@@ -912,7 +927,8 @@ function wfMsgExt( $key, $options ) {
  * Just like exit() but makes a note of it.
  * Commits open transactions except if the error parameter is set
  *
- * @deprecated Please return control to the caller or throw an exception
+ * @deprecated Please return control to the caller or throw an exception. Will
+ *             be removed in 1.19.
  */
 function wfAbruptExit( $error = false ) {
        static $called = false;
@@ -921,6 +937,7 @@ function wfAbruptExit( $error = false ) {
        }
        $called = true;
 
+       wfDeprecated( __FUNCTION__ );
        $bt = wfDebugBacktrace();
        if( $bt ) {
                for( $i = 0; $i < count( $bt ); $i++ ) {
@@ -941,9 +958,11 @@ function wfAbruptExit( $error = false ) {
 }
 
 /**
- * @deprecated Please return control the caller or throw an exception
+ * @deprecated Please return control the caller or throw an exception. Will
+ *             be removed in 1.19.
  */
 function wfErrorExit() {
+       wfDeprecated( __FUNCTION__ );
        wfAbruptExit( true );
 }
 
@@ -1435,14 +1454,6 @@ function wfExpandUrl( $url ) {
        }
 }
 
-/**
- * This is obsolete, use SquidUpdate::purge()
- * @deprecated
- */
-function wfPurgeSquidServers( $urlArr ) {
-       SquidUpdate::purge( $urlArr );
-}
-
 /**
  * Windows-compatible version of escapeshellarg()
  * Windows doesn't recognise single-quotes in the shell, but the escapeshellarg()
@@ -1509,7 +1520,11 @@ function wfMerge( $old, $mine, $yours, &$result ) {
 
        # This check may also protect against code injection in
        # case of broken installations.
-       if( !$wgDiff3 || !file_exists( $wgDiff3 ) ) {
+       wfSuppressWarnings();
+       $haveDiff3 = $wgDiff3 && file_exists( $wgDiff3 );
+       wfRestoreWarnings();
+
+       if( !$haveDiff3 ) {
                wfDebug( "diff3 not found\n" );
                return false;
        }
@@ -1579,10 +1594,13 @@ function wfDiff( $before, $after, $params = '-u' ) {
        }
 
        global $wgDiff;
+       wfSuppressWarnings();
+       $haveDiff = $wgDiff && file_exists( $wgDiff );
+       wfRestoreWarnings();
 
        # This check may also protect against code injection in
        # case of broken installations.
-       if( !file_exists( $wgDiff ) ) {
+       if( !$haveDiff ) {
                wfDebug( "diff executable not found\n" );
                $diffs = new Diff( explode( "\n", $before ), explode( "\n", $after ) );
                $format = new UnifiedDiffFormatter();
@@ -1941,6 +1959,13 @@ define( 'TS_POSTGRES', 7 );
  */
 define( 'TS_DB2', 8 );
 
+/**
+ * ISO 8601 basic format with no timezone: 19860209T200000Z
+ *
+ * This is used by ResourceLoader
+ */
+define( 'TS_ISO_8601_BASIC', 9 );
+
 /**
  * @param $outputtype Mixed: A timestamp in one of the supported formats, the
  *                    function will autodetect which format is supplied and act
@@ -1951,7 +1976,7 @@ define( 'TS_DB2', 8 );
 function wfTimestamp( $outputtype = TS_UNIX, $ts = 0 ) {
        $uts = 0;
        $da = array();
-       if ( $ts == 0 ) {
+       if ( $ts === 0 ) {
                $uts = time();
        } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)$/D', $ts, $da ) ) {
                # TS_DB
@@ -1968,10 +1993,17 @@ function wfTimestamp( $outputtype = TS_UNIX, $ts = 0 ) {
                                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 ) ) {
+               #TS_ISO_8601_BASIC
        } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d*[\+\- ](\d\d)$/', $ts, $da ) ) {
                # TS_POSTGRES
        } elseif ( preg_match( '/^(\d{4})\-(\d\d)\-(\d\d) (\d\d):(\d\d):(\d\d)\.*\d* GMT$/', $ts, $da ) ) {
                # 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 );
        } else {
                # Bogus value; fall back to the epoch...
                wfDebug("wfTimestamp() fed bogus time value: $outputtype; $ts\n");
@@ -1985,30 +2017,34 @@ function wfTimestamp( $outputtype = TS_UNIX, $ts = 0 ) {
                        (int)$da[2], (int)$da[3], (int)$da[1] );
        }
 
-       switch( $outputtype ) {
-               case TS_UNIX:
-                       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 );
-               // 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.' );
+       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.' );
        }
+
+       if ( TS_UNIX == $outputtype ) {
+               return $uts;
+       }
+
+       $output = gmdate( $formats[$outputtype], $uts );
+
+       if ( ( $outputtype == TS_RFC2822 ) || ( $outputtype == TS_POSTGRES ) ) {
+               $output .= ' GMT';
+       }
+
+       return $output;
 }
 
 /**
@@ -2032,11 +2068,11 @@ function wfTimestampOrNull( $outputtype = TS_UNIX, $ts = null ) {
  * @return Bool: true if it's Windows, False otherwise.
  */
 function wfIsWindows() {
-       if ( substr( php_uname(), 0, 7 ) == 'Windows' ) {
-               return true;
-       } else {
-               return false;
+       static $isWindows = null;
+       if ( $isWindows === null ) {
+               $isWindows = substr( php_uname(), 0, 7 ) == 'Windows';
        }
+       return $isWindows;
 }
 
 /**
@@ -2159,10 +2195,10 @@ function &wfGetMimeMagic() {
 }
 
 /**
- * Tries to get the system directory for temporary files. For PHP >= 5.2.1,
- * we'll use sys_get_temp_dir(). The TMPDIR, TMP, and TEMP environment
- * variables are then checked in sequence, and if none are set /tmp is
- * returned as the generic Unix default.
+ * Tries to get the system directory for temporary files. The TMPDIR, TMP, and
+ * TEMP environment variables are then checked in sequence, and if none are set
+ * try sys_get_temp_dir() for PHP >= 5.2.1. All else fails, return /tmp for Unix
+ * or C:\Windows\Temp for Windows and hope for the best.
  * It is common to call it with tempnam().
  *
  * NOTE: When possible, use instead the tmpfile() function to create
@@ -2171,17 +2207,17 @@ function &wfGetMimeMagic() {
  * @return String
  */
 function wfTempDir() {
-       if( function_exists( 'sys_get_temp_dir' ) ) {
-               return sys_get_temp_dir();
-       }
        foreach( array( 'TMPDIR', 'TMP', 'TEMP' ) as $var ) {
                $tmp = getenv( $var );
                if( $tmp && file_exists( $tmp ) && is_dir( $tmp ) && is_writable( $tmp ) ) {
                        return $tmp;
                }
        }
-       # Hope this is Unix of some kind!
-       return '/tmp';
+       if( function_exists( 'sys_get_temp_dir' ) ) {
+               return sys_get_temp_dir();
+       }
+       # Usual defaults
+       return wfIsWindows() ? 'C:\Windows\Temp' : '/tmp';
 }
 
 /**
@@ -2243,7 +2279,8 @@ function wfIncrStats( $key ) {
                        );
                }
                $statline = "stats/{$wgDBname} - 1 1 1 1 1 {$key}\n";
-               @socket_sendto(
+               wfSuppressWarnings();
+               socket_sendto(
                        $socket,
                        $statline,
                        strlen( $statline ),
@@ -2251,6 +2288,7 @@ function wfIncrStats( $key ) {
                        $wgUDPProfilerHost,
                        $wgUDPProfilerPort
                );
+               wfRestoreWarnings();
        } elseif( $wgStatsMethod == 'cache' ) {
                global $wgMemc;
                $key = wfMemcKey( 'stats', $key );
@@ -2416,12 +2454,14 @@ function wfDl( $extension ) {
 /**
  * Execute a shell command, with time and memory limits mirrored from the PHP
  * configuration if supported.
- * @param $cmd Command line, properly escaped for shell.
+ * @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 
+ *                 added to the executed command environment.
  * @return collected stdout as a string (trailing newlines stripped)
  */
-function wfShellExec( $cmd, &$retval = null ) {
+function wfShellExec( $cmd, &$retval = null, $environ = array() ) {
        global $IP, $wgMaxShellMemory, $wgMaxShellFileSize, $wgMaxShellTime;
 
        static $disabled;
@@ -2429,24 +2469,55 @@ function wfShellExec( $cmd, &$retval = null ) {
                $disabled = false;
                if( wfIniGetBool( 'safe_mode' ) ) {
                        wfDebug( "wfShellExec can't run in safe_mode, PHP's exec functions are too broken.\n" );
-                       $disabled = true;
-               }
-               $functions = explode( ',', ini_get( 'disable_functions' ) );
-               $functions = array_map( 'trim', $functions );
-               $functions = array_map( 'strtolower', $functions );
-               if ( in_array( 'passthru', $functions ) ) {
-                       wfDebug( "passthru is in disabled_functions\n" );
-                       $disabled = true;
+                       $disabled = 'safemode';
+               } else {
+                       $functions = explode( ',', ini_get( 'disable_functions' ) );
+                       $functions = array_map( 'trim', $functions );
+                       $functions = array_map( 'strtolower', $functions );
+                       if ( in_array( 'passthru', $functions ) ) {
+                               wfDebug( "passthru is in disabled_functions\n" );
+                               $disabled = 'passthru';
+                       }
                }
        }
        if ( $disabled ) {
                $retval = 1;
-               return 'Unable to run external programs in safe mode.';
+               return $disabled == 'safemode' ?
+                       'Unable to run external programs in safe mode.' :
+                       'Unable to run external programs, passthru() is disabled.';
        }
 
        wfInitShellLocale();
 
-       if ( php_uname( 's' ) == 'Linux' ) {
+       $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 
+                        * 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 
+                        * 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' ) )
+               {
+                       # Hack to work around PHP's flawed invocation of cmd.exe
+                       # http://news.php.net/php.internals/21796
+                       # Windows 9x doesn't accept any kind of quotes
+                       $cmd = '"' . $cmd . '"';
+               }
+       } elseif ( php_uname( 's' ) == 'Linux' ) {
                $time = intval( $wgMaxShellTime );
                $mem = intval( $wgMaxShellMemory );
                $filesize = intval( $wgMaxShellFileSize );
@@ -2457,13 +2528,6 @@ function wfShellExec( $cmd, &$retval = null ) {
                                $cmd = '/bin/bash ' . escapeshellarg( $script ) . " $time $mem $filesize " . escapeshellarg( $cmd );
                        }
                }
-       } elseif ( php_uname( 's' ) == 'Windows NT' &&
-               version_compare( PHP_VERSION, '5.3.0', '<' ) )
-       {
-               # This is a hack to work around PHP's flawed invocation of cmd.exe
-               # http://news.php.net/php.internals/21796
-               # Which is fixed in 5.3.0 :)
-               $cmd = '"' . $cmd . '"';
        }
        wfDebug( "wfShellExec: $cmd\n" );
 
@@ -2890,7 +2954,7 @@ function wfHttpOnlySafe() {
 /**
  * Initialise php session
  */
-function wfSetupSession() {
+function wfSetupSession( $sessionId = false ) {
        global $wgSessionsInMemcached, $wgCookiePath, $wgCookieDomain,
                        $wgCookieSecure, $wgCookieHttpOnly, $wgSessionHandler;
        if( $wgSessionsInMemcached ) {
@@ -2916,6 +2980,9 @@ function wfSetupSession() {
                session_set_cookie_params( 0, $wgCookiePath, $wgCookieDomain, $wgCookieSecure );
        }
        session_cache_limiter( 'private, must-revalidate' );
+       if ( $sessionId ) {
+               session_id( $sessionId );
+       }
        wfSuppressWarnings();
        session_start();
        wfRestoreWarnings();
@@ -3063,7 +3130,7 @@ function &wfGetLBFactory() {
 /**
  * Find a file.
  * Shortcut for RepoGroup::singleton()->findFile()
- * @param $title Either a string or Title object
+ * @param $title String or Title object
  * @param $options Associative array of options:
  *     time:           requested time for an archived image, or false for the
  *                     current version. An image object will be returned which was
@@ -3228,7 +3295,9 @@ function wfWarn( $msg, $callerOffset = 1, $level = E_USER_NOTICE ) {
                if( isset( $callerfunc['class'] ) ) {
                        $func .= $callerfunc['class'] . '::';
                }
-               $func .= @$callerfunc['function'];
+               if( isset( $callerfunc['function'] ) ) {
+                       $func .= $callerfunc['function'];
+               }
                $msg .= " [Called from $func in $file]";
        }
 
@@ -3259,7 +3328,9 @@ function wfWaitForSlaves( $maxLag, $wiki = false ) {
                $lb = wfGetLB( $wiki );
                list( $host, $lag ) = $lb->getMaxLag( $wiki );
                while( $lag > $maxLag ) {
-                       $name = @gethostbyaddr( $host );
+                       wfSuppressWarnings();
+                       $name = gethostbyaddr( $host );
+                       wfRestoreWarnings();
                        if( $name !== false ) {
                                $host = $name;
                        }
@@ -3308,9 +3379,8 @@ function wfCountDown( $n ) {
  *              characters before hashing.
  */
 function wfGenerateToken( $salt = '' ) {
-       $salt = serialize( $salt );
-
-       return md5( mt_rand( 0, 0x7fffffff ) . $salt );
+       $salt = serialize( $salt );
+       return md5( mt_rand( 0, 0x7fffffff ) . $salt );
 }
 
 /**
@@ -3465,11 +3535,12 @@ function wfArrayMap( $function, $input ) {
  * @return PackageRepository
  */
 function wfGetRepository() {
-       global $wgRepository, $wgRepositoryApiLocation;
+       global $wgRepositoryApiLocation;
+       static $repository = false;
        
-       if ( !isset( $wgRepository ) ) {
-               $wgRepository = new DistributionRepository( $wgRepositoryApiLocation );
+       if ( $repository === false ) {
+               $repository = new DistributionRepository( $wgRepositoryApiLocation );
        }
        
-       return $wgRepository;
+       return $repository;
 }