Add array type hints to minor methods in the Html class
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 47e7a15..3be43b3 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
@@ -166,12 +167,8 @@ if ( !function_exists( 'hash_equals' ) ) {
 /**
  * Load an extension
  *
- * This is the closest equivalent to:
- *   require_once "$IP/extensions/$name/$name.php";
- * as it will process and load the extension immediately.
- *
- * However, batch loading with wfLoadExtensions will
- * be more performant.
+ * This queues an extension to be loaded through
+ * the ExtensionRegistry system.
  *
  * @param string $name Name of the extension to load
  * @param string|null $path Absolute path of where to find the extension.json file
@@ -181,7 +178,7 @@ function wfLoadExtension( $name, $path = null ) {
                global $IP;
                $path = "$IP/extensions/$name/extension.json";
        }
-       ExtensionRegistry::getInstance()->load( $path );
+       ExtensionRegistry::getInstance()->queue( $path );
 }
 
 /**
@@ -202,8 +199,6 @@ function wfLoadExtensions( array $exts ) {
        foreach ( $exts as $ext ) {
                $registry->queue( "$IP/extensions/$ext/extension.json" );
        }
-
-       $registry->loadFromQueue();
 }
 
 /**
@@ -218,7 +213,7 @@ function wfLoadSkin( $name, $path = null ) {
                global $IP;
                $path = "$IP/skins/$name/skin.json";
        }
-       ExtensionRegistry::getInstance()->load( $path );
+       ExtensionRegistry::getInstance()->queue( $path );
 }
 
 /**
@@ -233,8 +228,6 @@ function wfLoadSkins( array $skins ) {
        foreach ( $skins as $skin ) {
                $registry->queue( "$IP/skins/$skin/skin.json" );
        }
-
-       $registry->loadFromQueue();
 }
 
 /**
@@ -1059,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 );
 }
 
@@ -1159,7 +1152,7 @@ function wfDebugLog(
 
        $text = trim( $text );
 
-       $logger = MWLoggerFactory::getInstance( $logGroup );
+       $logger = LoggerFactory::getInstance( $logGroup );
        $context['private'] = ( $dest === 'private' );
        $logger->info( $text, $context );
 }
@@ -1173,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 );
 }
 
@@ -1232,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 );
 }
@@ -1245,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' ) );
@@ -1259,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
        ) {
@@ -1285,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'];
        }
@@ -1304,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 );
 }
 
@@ -1360,6 +1344,12 @@ 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.
+               if ( $wgReadOnly === false && wfGetLB()->getLaggedSlaveMode() ) {
+                       $wgReadOnly = 'The database has been automatically locked ' .
+                               'while the slave database servers catch up to the master';
+               }
        }
 
        return $wgReadOnly;
@@ -3080,7 +3070,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 ) ) {
@@ -3091,7 +3082,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 {
@@ -3115,7 +3107,9 @@ function wfMerge( $old, $mine, $yours, &$result ) {
 
 /**
  * Returns unified plain-text diff of two texts.
- * Useful for machine processing of diffs.
+ * "Useful" for machine processing of diffs.
+ *
+ * @deprecated since 1.25, use DiffEngine/UnifiedDiffFormatter directly
  *
  * @param string $before The text before the changes.
  * @param string $after The text after the changes.
@@ -3155,6 +3149,11 @@ function wfDiff( $before, $after, $params = '-u' ) {
        $cmd = "$wgDiff " . $params . ' ' . wfEscapeShellArg( $oldtextName, $newtextName );
 
        $h = popen( $cmd, 'r' );
+       if ( !$h ) {
+               unlink( $oldtextName );
+               unlink( $newtextName );
+               throw new Exception( __METHOD__ . '(): popen() failed' );
+       }
 
        $diff = '';