Followup cf5f641: pass $params by reference again
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index bb80770..859b421 100644 (file)
@@ -950,6 +950,8 @@ function wfMatchesDomainList( $url, $domains ) {
  * $wgDebugRawPage - if false, 'action=raw' hits will not result in debug output.
  * $wgDebugComments - if on, some debug items may appear in comments in the HTML output.
  *
+ * @since 1.25 support for additional context data
+ *
  * @param string $text
  * @param string|bool $dest Destination of the message:
  *     - 'all': both to the log and HTML (debug toolbar or HTML comments)
@@ -957,9 +959,11 @@ function wfMatchesDomainList( $url, $domains ) {
  *   For backward compatibility, it can also take a boolean:
  *     - true: same as 'all'
  *     - false: same as 'log'
+ * @param array $context Additional logging context data
  */
-function wfDebug( $text, $dest = 'all' ) {
+function wfDebug( $text, $dest = 'all', array $context = array() ) {
        global $wgDebugRawPage, $wgDebugLogPrefix;
+       global $wgDebugTimestamps, $wgRequestTime;
 
        if ( !$wgDebugRawPage && wfIsDebugRawPage() ) {
                return;
@@ -972,23 +976,36 @@ function wfDebug( $text, $dest = 'all' ) {
                $dest = 'log';
        }
 
-       $timer = wfDebugTimer();
-       if ( $timer !== '' ) {
-               // Prepend elapsed request time and real memory usage to each line
-               $text = preg_replace( '/[^\n]/', $timer . '\0', $text, 1 );
+       $text = trim( $text );
+
+       // Inline logic from deprecated wfDebugTimer()
+       if ( $wgDebugTimestamps ) {
+               $context['seconds_elapsed'] = sprintf(
+                       '%6.4f',
+                       microtime( true ) - $wgRequestTime
+               );
+               $context['memory_used'] = sprintf(
+                       '%5.1fM',
+                       ( memory_get_usage( true ) / ( 1024 * 1024 ) )
+               );
        }
 
        if ( $dest === 'all' ) {
-               MWDebug::debugMsg( $text );
+               $prefix = '';
+               if ( $wgDebugTimestamps ) {
+                       // Prepend elapsed request time and real memory usage with two
+                       // trailing spaces.
+                       $prefix = "{$context['seconds_elapsed']} {$context['memory_used']}  ";
+               }
+               MWDebug::debugMsg( "{$prefix}{$text}" );
        }
 
-       $ctx = array();
        if ( $wgDebugLogPrefix !== '' ) {
-               $ctx['prefix'] = $wgDebugLogPrefix;
+               $context['prefix'] = $wgDebugLogPrefix;
        }
 
        $logger = MWLogger::getInstance( 'wfDebug' );
-       $logger->debug( rtrim( $text, "\n" ), $ctx );
+       $logger->debug( $text, $context );
 }
 
 /**
@@ -1017,11 +1034,14 @@ function wfIsDebugRawPage() {
 /**
  * Get microsecond timestamps for debug logs
  *
+ * @deprecated since 1.25
  * @return string
  */
 function wfDebugTimer() {
        global $wgDebugTimestamps, $wgRequestTime;
 
+       wfDeprecated( __METHOD__, '1.25' );
+
        if ( !$wgDebugTimestamps ) {
                return '';
        }
@@ -1054,19 +1074,23 @@ function wfDebugMem( $exact = false ) {
  * a sampling factor.
  *
  * @since 1.23 support for sampling log messages via $wgDebugLogGroups.
+ * @since 1.25 support for additional context data
  *
  * @param string $logGroup
  * @param string $text
  * @param string|bool $dest Destination of the message:
  *     - 'all': both to the log and HTML (debug toolbar or HTML comments)
  *     - 'log': only to the log and not in HTML
- *     - 'private': only to the specifc log if set in $wgDebugLogGroups and
+ *     - 'private': only to the specific log if set in $wgDebugLogGroups and
  *       discarded otherwise
  *   For backward compatibility, it can also take a boolean:
  *     - true: same as 'all'
  *     - false: same as 'private'
+ * @param array $context Additional logging context data
  */
-function wfDebugLog( $logGroup, $text, $dest = 'all' ) {
+function wfDebugLog(
+       $logGroup, $text, $dest = 'all', array $context = array()
+) {
        // Turn $dest into a string if it's a boolean (for b/c)
        if ( $dest === true ) {
                $dest = 'all';
@@ -1081,19 +1105,21 @@ function wfDebugLog( $logGroup, $text, $dest = 'all' ) {
        }
 
        $logger = MWLogger::getInstance( $logGroup );
-       $logger->debug( $text, array(
-               'private' => ( $dest === 'private' ),
-       ) );
+       $context['private'] = ( $dest === 'private' );
+       $logger->debug( $text, $context );
 }
 
 /**
  * Log for database errors
  *
+ * @since 1.25 support for additional context data
+ *
  * @param string $text Database error message.
+ * @param array $context Additional logging context data
  */
-function wfLogDBError( $text ) {
+function wfLogDBError( $text, array $context = array() ) {
        $logger = MWLogger::getInstance( 'wfLogDBError' );
-       $logger->error( trim( $text ) );
+       $logger->error( trim( $text ), $context );
 }
 
 /**
@@ -1145,23 +1171,24 @@ function wfLogWarning( $msg, $callerOffset = 1, $level = E_USER_WARNING ) {
  *
  * Can also log to TCP or UDP with the syntax udp://host:port/prefix. This will
  * send lines to the specified port, prefixed by the specified prefix and a space.
+ * @since 1.25 support for additional context data
  *
  * @param string $text
  * @param string $file Filename
+ * @param array $context Additional logging context data
  * @throws MWException
  */
-function wfErrorLog( $text, $file ) {
+function wfErrorLog( $text, $file, array $context = array() ) {
        $logger = MWLogger::getInstance( 'wfErrorLog' );
-       $logger->info( trim( $text ), array(
-               'destination' => $file,
-       ) );
+       $context['destination'] = $file;
+       $logger->info( trim( $text ), $context );
 }
 
 /**
  * @todo document
  */
 function wfLogProfilingData() {
-       global $wgRequestTime, $wgDebugLogFile, $wgDebugLogGroups, $wgDebugRawPage;
+       global $wgRequestTime, $wgDebugLogGroups, $wgDebugRawPage;
        global $wgProfileLimit, $wgUser, $wgRequest;
 
        StatCounter::singleton()->flush();
@@ -1169,7 +1196,7 @@ function wfLogProfilingData() {
        $profiler = Profiler::instance();
 
        # Profiling must actually be enabled...
-       if ( $profiler->isStub() ) {
+       if ( $profiler instanceof ProfilerStub ) {
                return;
        }
 
@@ -1226,10 +1253,9 @@ function wfLogProfilingData() {
        }
 
        $ctx['output'] = $profiler->getOutput();
-       $ctx['profile'] = $profiler->getRawData();
 
        $log = MWLogger::getInstance( 'profileoutput' );
-       $log->info( 'Elapsed: {elapsed}', $ctx );
+       $log->info( "Elapsed: {elapsed}; URL: <{url}>\n{output}", $ctx );
 }
 
 /**
@@ -1505,7 +1531,7 @@ function wfMsgReal( $key, $args, $useDB = true, $forContent = false, $transform
 function wfMsgGetKey( $key, $useDB = true, $langCode = false, $transform = true ) {
        wfDeprecated( __METHOD__, '1.21' );
 
-       wfRunHooks( 'NormalizeMessageKey', array( &$key, &$useDB, &$langCode, &$transform ) );
+       Hooks::run( 'NormalizeMessageKey', array( &$key, &$useDB, &$langCode, &$transform ) );
 
        $cache = MessageCache::singleton();
        $message = $cache->get( $key, $useDB, $langCode );
@@ -1620,15 +1646,15 @@ function wfMsgExt( $key, $options ) {
        array_shift( $args );
        array_shift( $args );
        $options = (array)$options;
+       $validOptions = array( 'parse', 'parseinline', 'escape', 'escapenoentities', 'replaceafter',
+               'parsemag', 'content' );
 
        foreach ( $options as $arrayKey => $option ) {
                if ( !preg_match( '/^[0-9]+|language$/', $arrayKey ) ) {
-                       # An unknown index, neither numeric nor "language"
+                       // An unknown index, neither numeric nor "language"
                        wfWarn( "wfMsgExt called with incorrect parameter key $arrayKey", 1, E_USER_WARNING );
-               } elseif ( preg_match( '/^[0-9]+$/', $arrayKey ) && !in_array( $option,
-               array( 'parse', 'parseinline', 'escape', 'escapenoentities',
-               'replaceafter', 'parsemag', 'content' ) ) ) {
-                       # A numeric index with unknown value
+               } elseif ( preg_match( '/^[0-9]+$/', $arrayKey ) && !in_array( $option, $validOptions ) ) {
+                       // A numeric index with unknown value
                        wfWarn( "wfMsgExt called with incorrect parameter $option", 1, E_USER_WARNING );
                }
        }
@@ -1789,52 +1815,37 @@ function wfDebugBacktrace( $limit = 0 ) {
 /**
  * Get a debug backtrace as a string
  *
+ * @param bool|null $raw If true, the return value is plain text. If false, HTML.
+ *   Defaults to $wgCommandLineMode if unset.
  * @return string
+ * @since 1.25 Supports $raw parameter.
  */
-function wfBacktrace() {
+function wfBacktrace( $raw = null ) {
        global $wgCommandLineMode;
 
-       if ( $wgCommandLineMode ) {
-               $msg = '';
-       } else {
-               $msg = "<ul>\n";
+       if ( $raw === null ) {
+               $raw = $wgCommandLineMode;
        }
-       $backtrace = wfDebugBacktrace();
-       foreach ( $backtrace as $call ) {
-               if ( isset( $call['file'] ) ) {
-                       $f = explode( DIRECTORY_SEPARATOR, $call['file'] );
-                       $file = $f[count( $f ) - 1];
-               } else {
-                       $file = '-';
-               }
-               if ( isset( $call['line'] ) ) {
-                       $line = $call['line'];
-               } else {
-                       $line = '-';
-               }
-               if ( $wgCommandLineMode ) {
-                       $msg .= "$file line $line calls ";
-               } else {
-                       $msg .= '<li>' . $file . ' line ' . $line . ' calls ';
-               }
-               if ( !empty( $call['class'] ) ) {
-                       $msg .= $call['class'] . $call['type'];
-               }
-               $msg .= $call['function'] . '()';
 
-               if ( $wgCommandLineMode ) {
-                       $msg .= "\n";
-               } else {
-                       $msg .= "</li>\n";
-               }
-       }
-       if ( $wgCommandLineMode ) {
-               $msg .= "\n";
+       if ( $raw ) {
+               $frameFormat = "%s line %s calls %s()\n";
+               $traceFormat = "%s";
        } else {
-               $msg .= "</ul>\n";
+               $frameFormat = "<li>%s line %s calls %s()</li>\n";
+               $traceFormat = "<ul>\n%s</ul>\n";
        }
 
-       return $msg;
+       $frames = array_map( function ( $frame ) use ( $frameFormat ) {
+               $file = !empty( $frame['file'] ) ? basename( $frame['file'] ) : '-';
+               $line = isset( $frame['line'] ) ? $frame['line'] : '-';
+               $call = $frame['function'];
+               if ( !empty( $frame['class'] ) ) {
+                       $call = $frame['class'] . $frame['type'] . $call;
+               }
+               return sprintf( $frameFormat, $file, $line, $call );
+       }, wfDebugBacktrace() );
+
+       return sprintf( $traceFormat, implode( '', $frames ) );
 }
 
 /**
@@ -2953,7 +2964,7 @@ function wfShellWikiCmd( $script, array $parameters = array(), array $options =
        global $wgPhpCli;
        // Give site config file a chance to run the script in a wrapper.
        // The caller may likely want to call wfBasename() on $script.
-       wfRunHooks( 'wfShellWikiCmd', array( &$script, &$parameters, &$options ) );
+       Hooks::run( 'wfShellWikiCmd', array( &$script, &$parameters, &$options ) );
        $cmd = isset( $options['php'] ) ? array( $options['php'] ) : array( $wgPhpCli );
        if ( isset( $options['wrapper'] ) ) {
                $cmd[] = $options['wrapper'];
@@ -3405,7 +3416,7 @@ function wfResetSessionID() {
                $_SESSION = $tmp;
        }
        $newSessionId = session_id();
-       wfRunHooks( 'ResetSessionID', array( $oldSessionId, $newSessionId ) );
+       Hooks::run( 'ResetSessionID', array( $oldSessionId, $newSessionId ) );
 }
 
 /**
@@ -3540,7 +3551,7 @@ function wfSplitWikiID( $wiki ) {
  *
  * @return DatabaseBase
  */
-function &wfGetDB( $db, $groups = array(), $wiki = false ) {
+function wfGetDB( $db, $groups = array(), $wiki = false ) {
        return wfGetLB( $wiki )->getConnection( $db, $groups, $wiki );
 }
 
@@ -3559,7 +3570,7 @@ function wfGetLB( $wiki = false ) {
  *
  * @return LBFactory
  */
-function &wfGetLBFactory() {
+function wfGetLBFactory() {
        return LBFactory::singleton();
 }
 
@@ -3935,6 +3946,7 @@ function wfGetLangConverterCacheStorage() {
  * @param string|null $deprecatedVersion Optionally mark hook as deprecated with version number
  *
  * @return bool True if no handler aborted the hook
+ * @deprecated 1.25
  */
 function wfRunHooks( $event, array $args = array(), $deprecatedVersion = null ) {
        return Hooks::run( $event, $args, $deprecatedVersion );
@@ -4002,7 +4014,7 @@ function wfIsBadImage( $name, $contextTitle = false, $blacklist = null ) {
 
        # Run the extension hook
        $bad = false;
-       if ( !wfRunHooks( 'BadImage', array( $name, &$bad ) ) ) {
+       if ( !Hooks::run( 'BadImage', array( $name, &$bad ) ) ) {
                wfProfileOut( __METHOD__ );
                return $bad;
        }
@@ -4066,7 +4078,7 @@ function wfIsBadImage( $name, $contextTitle = false, $blacklist = null ) {
  */
 function wfCanIPUseHTTPS( $ip ) {
        $canDo = true;
-       wfRunHooks( 'CanIPUseHTTPS', array( $ip, &$canDo ) );
+       Hooks::run( 'CanIPUseHTTPS', array( $ip, &$canDo ) );
        return !!$canDo;
 }
 
@@ -4093,6 +4105,7 @@ function wfGetIP() {
  * @return bool
  */
 function wfIsTrustedProxy( $ip ) {
+       wfDeprecated( __METHOD__, '1.24' );
        return IP::isTrustedProxy( $ip );
 }
 
@@ -4105,5 +4118,6 @@ function wfIsTrustedProxy( $ip ) {
  * @since 1.23 Supports CIDR ranges in $wgSquidServersNoPurge
  */
 function wfIsConfiguredProxy( $ip ) {
+       wfDeprecated( __METHOD__, '1.24' );
        return IP::isConfiguredProxy( $ip );
 }