Merge "ApiTestCase: Set correct user for derivate requests"
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index bc3a46b..25876fb 100644 (file)
@@ -26,6 +26,7 @@ if ( !defined( 'MEDIAWIKI' ) ) {
 
 use Liuggio\StatsdClient\StatsdClient;
 use Liuggio\StatsdClient\Sender\SocketSender;
+use MediaWiki\Logger\LoggerFactory;
 
 // Hide compatibility functions from Doxygen
 /// @cond
@@ -169,13 +170,13 @@ if ( !function_exists( 'hash_equals' ) ) {
  * This queues an extension to be loaded through
  * the ExtensionRegistry system.
  *
- * @param string $name Name of the extension to load
+ * @param string $ext Name of the extension to load
  * @param string|null $path Absolute path of where to find the extension.json file
  */
-function wfLoadExtension( $name, $path = null ) {
+function wfLoadExtension( $ext, $path = null ) {
        if ( !$path ) {
-               global $IP;
-               $path = "$IP/extensions/$name/extension.json";
+               global $wgExtensionDirectory;
+               $path = "$wgExtensionDirectory/$ext/extension.json";
        }
        ExtensionRegistry::getInstance()->queue( $path );
 }
@@ -193,10 +194,10 @@ function wfLoadExtension( $name, $path = null ) {
  * @param string[] $exts Array of extension names to load
  */
 function wfLoadExtensions( array $exts ) {
-       global $IP;
+       global $wgExtensionDirectory;
        $registry = ExtensionRegistry::getInstance();
        foreach ( $exts as $ext ) {
-               $registry->queue( "$IP/extensions/$ext/extension.json" );
+               $registry->queue( "$wgExtensionDirectory/$ext/extension.json" );
        }
 }
 
@@ -204,13 +205,13 @@ function wfLoadExtensions( array $exts ) {
  * Load a skin
  *
  * @see wfLoadExtension
- * @param string $name Name of the extension to load
+ * @param string $skin Name of the extension to load
  * @param string|null $path Absolute path of where to find the skin.json file
  */
-function wfLoadSkin( $name, $path = null ) {
+function wfLoadSkin( $skin, $path = null ) {
        if ( !$path ) {
-               global $IP;
-               $path = "$IP/skins/$name/skin.json";
+               global $wgStyleDirectory;
+               $path = "$wgStyleDirectory/$skin/skin.json";
        }
        ExtensionRegistry::getInstance()->queue( $path );
 }
@@ -222,10 +223,10 @@ function wfLoadSkin( $name, $path = null ) {
  * @param string[] $skins Array of extension names to load
  */
 function wfLoadSkins( array $skins ) {
-       global $IP;
+       global $wgStyleDirectory;
        $registry = ExtensionRegistry::getInstance();
        foreach ( $skins as $skin ) {
-               $registry->queue( "$IP/skins/$skin/skin.json" );
+               $registry->queue( "$wgStyleDirectory/$skin/skin.json" );
        }
 }
 
@@ -1051,7 +1052,7 @@ function wfDebug( $text, $dest = 'all', array $context = array() ) {
                $context['prefix'] = $wgDebugLogPrefix;
        }
 
-       $logger = MWLoggerFactory::getInstance( 'wfDebug' );
+       $logger = LoggerFactory::getInstance( 'wfDebug' );
        $logger->debug( $text, $context );
 }
 
@@ -1151,7 +1152,7 @@ function wfDebugLog(
 
        $text = trim( $text );
 
-       $logger = MWLoggerFactory::getInstance( $logGroup );
+       $logger = LoggerFactory::getInstance( $logGroup );
        $context['private'] = ( $dest === 'private' );
        $logger->info( $text, $context );
 }
@@ -1165,7 +1166,7 @@ function wfDebugLog(
  * @param array $context Additional logging context data
  */
 function wfLogDBError( $text, array $context = array() ) {
-       $logger = MWLoggerFactory::getInstance( 'wfLogDBError' );
+       $logger = LoggerFactory::getInstance( 'wfLogDBError' );
        $logger->error( trim( $text ), $context );
 }
 
@@ -1224,11 +1225,11 @@ function wfLogWarning( $msg, $callerOffset = 1, $level = E_USER_WARNING ) {
  * @param string $file Filename
  * @param array $context Additional logging context data
  * @throws MWException
- * @deprecated since 1.25 Use MWLoggerLegacyLogger::emit or UDPTransport
+ * @deprecated since 1.25 Use MediaWiki\Logger\LegacyLogger::emit or UDPTransport
  */
 function wfErrorLog( $text, $file, array $context = array() ) {
        wfDeprecated( __METHOD__, '1.25' );
-       $logger = MWLoggerFactory::getInstance( 'wfErrorLog' );
+       $logger = LoggerFactory::getInstance( 'wfErrorLog' );
        $context['destination'] = $file;
        $logger->info( trim( $text ), $context );
 }
@@ -1237,10 +1238,15 @@ function wfErrorLog( $text, $file, array $context = array() ) {
  * @todo document
  */
 function wfLogProfilingData() {
-       global $wgRequestTime, $wgDebugLogGroups, $wgDebugRawPage;
-       global $wgProfileLimit, $wgUser, $wgRequest;
+       global $wgDebugLogGroups, $wgDebugRawPage;
 
        $context = RequestContext::getMain();
+       $request = $context->getRequest();
+
+       $profiler = Profiler::instance();
+       $profiler->setContext( $context );
+       $profiler->logData();
+
        $config = $context->getConfig();
        if ( $config->has( 'StatsdServer' ) ) {
                $statsdServer = explode( ':', $config->get( 'StatsdServer' ) );
@@ -1251,22 +1257,11 @@ function wfLogProfilingData() {
                $statsdClient->send( $context->getStats()->getBuffer() );
        }
 
-       $profiler = Profiler::instance();
-
        # Profiling must actually be enabled...
        if ( $profiler instanceof ProfilerStub ) {
                return;
        }
 
-       // Get total page request time and only show pages that longer than
-       // $wgProfileLimit time (default is 0)
-       $elapsed = microtime( true ) - $wgRequestTime;
-       if ( $elapsed <= $wgProfileLimit ) {
-               return;
-       }
-
-       $profiler->logData();
-
        if ( isset( $wgDebugLogGroups['profileoutput'] )
                && $wgDebugLogGroups['profileoutput'] === false
        ) {
@@ -1277,7 +1272,7 @@ function wfLogProfilingData() {
                return;
        }
 
-       $ctx = array( 'elapsed' => $elapsed );
+       $ctx = array( 'elapsed' => $request->getElapsedTime() );
        if ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
                $ctx['forwarded_for'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
        }
@@ -1296,23 +1291,20 @@ function wfLogProfilingData() {
        // Don't load $wgUser at this late stage just for statistics purposes
        // @todo FIXME: We can detect some anons even if it is not loaded.
        // See User::getId()
-       if ( $wgUser->isItemLoaded( 'id' ) && $wgUser->isAnon() ) {
-               $ctx['anon'] = true;
-       } else {
-               $ctx['anon'] = false;
-       }
+       $user = $context->getUser();
+       $ctx['anon'] = $user->isItemLoaded( 'id' ) && $user->isAnon();
 
        // Command line script uses a FauxRequest object which does not have
        // any knowledge about an URL and throw an exception instead.
        try {
-               $ctx['url'] = urldecode( $wgRequest->getRequestURL() );
+               $ctx['url'] = urldecode( $request->getRequestURL() );
        } catch ( Exception $ignored ) {
                // no-op
        }
 
        $ctx['output'] = $profiler->getOutput();
 
-       $log = MWLoggerFactory::getInstance( 'profileoutput' );
+       $log = LoggerFactory::getInstance( 'profileoutput' );
        $log->info( "Elapsed: {elapsed}; URL: <{url}>\n{output}", $ctx );
 }
 
@@ -1352,6 +1344,17 @@ function wfReadOnlyReason() {
                } else {
                        $wgReadOnly = false;
                }
+               // Callers use this method to be aware that data presented to a user
+               // may be very stale and thus allowing submissions can be problematic.
+               try {
+                       if ( $wgReadOnly === false && wfGetLB()->getLaggedSlaveMode() ) {
+                               $wgReadOnly = 'The database has been automatically locked ' .
+                                       'while the slave database servers catch up to the master';
+                       }
+               } catch ( DBConnectionError $e ) {
+                       $wgReadOnly = 'The database has been automatically locked ' .
+                               'until the slave database servers become available';
+               }
        }
 
        return $wgReadOnly;
@@ -2126,15 +2129,14 @@ function wfVarDump( $var ) {
  */
 function wfHttpError( $code, $label, $desc ) {
        global $wgOut;
-       header( "HTTP/1.0 $code $label" );
-       header( "Status: $code $label" );
+       HttpStatus::header( $code );
        if ( $wgOut ) {
                $wgOut->disable();
                $wgOut->sendCacheControl();
        }
 
        header( 'Content-type: text/html; charset=utf-8' );
-       print "<!doctype html>" .
+       print '<!DOCTYPE html>' .
                '<html><head><title>' .
                htmlspecialchars( $label ) .
                '</title></head><body><h1>' .
@@ -2461,7 +2463,7 @@ function wfTimestampNow() {
 function wfIsWindows() {
        static $isWindows = null;
        if ( $isWindows === null ) {
-               $isWindows = substr( php_uname(), 0, 7 ) == 'Windows';
+               $isWindows = strtoupper( substr( PHP_OS, 0, 3 ) ) === 'WIN';
        }
        return $isWindows;
 }
@@ -2475,20 +2477,6 @@ function wfIsHHVM() {
        return defined( 'HHVM_VERSION' );
 }
 
-/**
- * Swap two variables
- *
- * @deprecated since 1.24
- * @param mixed $x
- * @param mixed $y
- */
-function swap( &$x, &$y ) {
-       wfDeprecated( __FUNCTION__, '1.24' );
-       $z = $x;
-       $x = $y;
-       $y = $z;
-}
-
 /**
  * Tries to get the system directory for temporary files. First
  * $wgTmpDirectory is checked, and then the TMPDIR, TMP, and TEMP
@@ -2537,7 +2525,7 @@ function wfMkdirParents( $dir, $mode = null, $caller = null ) {
                wfDebug( "$caller: called wfMkdirParents($dir)\n" );
        }
 
-       if ( strval( $dir ) === '' || ( file_exists( $dir ) && is_dir( $dir ) ) ) {
+       if ( strval( $dir ) === '' || is_dir( $dir ) ) {
                return true;
        }
 
@@ -3072,7 +3060,8 @@ function wfMerge( $old, $mine, $yours, &$result ) {
        fclose( $yourtextFile );
 
        # Check for a conflict
-       $cmd = wfEscapeShellArg( $wgDiff3, '-a', '--overlap-only', $mytextName, $oldtextName, $yourtextName );
+       $cmd = wfEscapeShellArg( $wgDiff3, '-a', '--overlap-only', $mytextName,
+               $oldtextName, $yourtextName );
        $handle = popen( $cmd, 'r' );
 
        if ( fgets( $handle, 1024 ) ) {
@@ -3083,7 +3072,8 @@ function wfMerge( $old, $mine, $yours, &$result ) {
        pclose( $handle );
 
        # Merge differences
-       $cmd = wfEscapeShellArg( $wgDiff3, '-a', '-e', '--merge', $mytextName, $oldtextName, $yourtextName );
+       $cmd = wfEscapeShellArg( $wgDiff3, '-a', '-e', '--merge', $mytextName,
+               $oldtextName, $yourtextName );
        $handle = popen( $cmd, 'r' );
        $result = '';
        do {
@@ -3362,7 +3352,7 @@ function wfBaseConvert( $input, $sourceBase, $destBase, $pad = 1,
                // Removing leading zeros works around broken base detection code in
                // some PHP versions (see <https://bugs.php.net/bug.php?id=50175> and
                // <https://bugs.php.net/bug.php?id=55398>).
-               $result = gmp_strval( gmp_init( ltrim( $input, '0' ), $sourceBase ), $destBase );
+               $result = gmp_strval( gmp_init( ltrim( $input, '0' ) ?: '0', $sourceBase ), $destBase );
        } elseif ( extension_loaded( 'bcmath' ) && ( $engine == 'auto' || $engine == 'bcmath' ) ) {
                $decimal = '0';
                foreach ( str_split( strtolower( $input ) ) as $char ) {
@@ -3764,6 +3754,7 @@ function wfWaitForSlaves(
        }
 
        // Figure out which clusters need to be checked
+       /** @var LoadBalancer[] $lbs */
        $lbs = array();
        if ( $cluster === '*' ) {
                wfGetLBFactory()->forEachLB( function ( LoadBalancer $lb ) use ( &$lbs ) {
@@ -3780,20 +3771,14 @@ function wfWaitForSlaves(
        // time needed to wait on the next clusters.
        $masterPositions = array_fill( 0, count( $lbs ), false );
        foreach ( $lbs as $i => $lb ) {
-               // bug 27975 - Don't try to wait for slaves if there are none
-               // Prevents permission error when getting master position
-               if ( $lb->getServerCount() > 1 ) {
-                       if ( $ifWritesSince && !$lb->hasMasterConnection() ) {
-                               continue; // assume no writes done
-                       }
-                       // Use the empty string to not trigger selectDB() since the connection
-                       // may have been to a server that does not have a DB for the current wiki.
-                       $dbw = $lb->getConnection( DB_MASTER, array(), '' );
-                       if ( $ifWritesSince && $dbw->lastDoneWrites() < $ifWritesSince ) {
-                               continue; // no writes since the last wait
-                       }
-                       $masterPositions[$i] = $dbw->getMasterPos();
+               if ( $lb->getServerCount() <= 1 ) {
+                       // Bug 27975 - Don't try to wait for slaves if there are none
+                       // Prevents permission error when getting master position
+                       continue;
+               } elseif ( $ifWritesSince && $lb->lastMasterChangeTimestamp() < $ifWritesSince ) {
+                       continue; // no writes since the last wait
                }
+               $masterPositions[$i] = $lb->getMasterPos();
        }
 
        $ok = true;