API: Don't dump Title object into API result
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index 294407a..2025e17 100644 (file)
@@ -160,6 +160,80 @@ if ( !function_exists( 'hash_equals' ) ) {
 }
 /// @endcond
 
+/**
+ * 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.
+ *
+ * @param string $name 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 ) {
+       if ( !$path ) {
+               global $IP;
+               $path = "$IP/extensions/$name/extension.json";
+       }
+       ExtensionRegistry::getInstance()->load( $path );
+}
+
+/**
+ * Load multiple extensions at once
+ *
+ * Same as wfLoadExtension, but more efficient if you
+ * are loading multiple extensions.
+ *
+ * If you want to specify custom paths, you should interact with
+ * ExtensionRegistry directly.
+ *
+ * @see wfLoadExtension
+ * @param string[] $exts Array of extension names to load
+ */
+function wfLoadExtensions( array $exts ) {
+       global $IP;
+       $registry = ExtensionRegistry::getInstance();
+       foreach ( $exts as $ext ) {
+               $registry->queue( "$IP/extensions/$ext/extension.json" );
+       }
+
+       $registry->loadFromQueue();
+}
+
+/**
+ * Load a skin
+ *
+ * @see wfLoadExtension
+ * @param string $name 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 ) {
+       if ( !$path ) {
+               global $IP;
+               $path = "$IP/skins/$name/skin.json";
+       }
+       ExtensionRegistry::getInstance()->load( $path );
+}
+
+/**
+ * Load multiple skins at once
+ *
+ * @see wfLoadExtensions
+ * @param string[] $skins Array of extension names to load
+ */
+function wfLoadSkins( array $skins ) {
+       global $IP;
+       $registry = ExtensionRegistry::getInstance();
+       foreach ( $skins as $skin ) {
+               $registry->queue( "$IP/skins/$skin/skin.json" );
+       }
+
+       $registry->loadFromQueue();
+}
+
 /**
  * Like array_diff( $a, $b ) except that it works with two-dimensional arrays.
  * @param array $a
@@ -1004,7 +1078,7 @@ function wfDebug( $text, $dest = 'all', array $context = array() ) {
                $context['prefix'] = $wgDebugLogPrefix;
        }
 
-       $logger = MWLogger::getInstance( 'wfDebug' );
+       $logger = MWLoggerFactory::getInstance( 'wfDebug' );
        $logger->debug( $text, $context );
 }
 
@@ -1067,14 +1141,18 @@ function wfDebugMem( $exact = false ) {
 }
 
 /**
- * Send a line to a supplementary debug log file, if configured, or main debug log if not.
- * To configure a supplementary log file, set $wgDebugLogGroups[$logGroup] to a string
- * filename or an associative array mapping 'destination' to the desired filename. The
- * associative array may also contain a 'sample' key with an integer value, specifying
- * a sampling factor.
+ * Send a line to a supplementary debug log file, if configured, or main debug
+ * log if not.
+ *
+ * To configure a supplementary log file, set $wgDebugLogGroups[$logGroup] to
+ * a string filename or an associative array mapping 'destination' to the
+ * desired filename. The associative array may also contain a 'sample' key
+ * with an integer value, specifying a sampling factor. Sampled log events
+ * will be emitted with a 1 in N random chance.
  *
  * @since 1.23 support for sampling log messages via $wgDebugLogGroups.
  * @since 1.25 support for additional context data
+ * @since 1.25 sample behavior dependent on configured $wgMWLoggerDefaultSpi
  *
  * @param string $logGroup
  * @param string $text
@@ -1104,9 +1182,9 @@ function wfDebugLog(
                MWDebug::debugMsg( "[{$logGroup}] {$text}\n" );
        }
 
-       $logger = MWLogger::getInstance( $logGroup );
+       $logger = MWLoggerFactory::getInstance( $logGroup );
        $context['private'] = ( $dest === 'private' );
-       $logger->debug( $text, $context );
+       $logger->info( $text, $context );
 }
 
 /**
@@ -1118,7 +1196,7 @@ function wfDebugLog(
  * @param array $context Additional logging context data
  */
 function wfLogDBError( $text, array $context = array() ) {
-       $logger = MWLogger::getInstance( 'wfLogDBError' );
+       $logger = MWLoggerFactory::getInstance( 'wfLogDBError' );
        $logger->error( trim( $text ), $context );
 }
 
@@ -1177,9 +1255,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
  */
 function wfErrorLog( $text, $file, array $context = array() ) {
-       $logger = MWLogger::getInstance( 'wfErrorLog' );
+       wfDeprecated( __METHOD__, '1.25' );
+       $logger = MWLoggerFactory::getInstance( 'wfErrorLog' );
        $context['destination'] = $file;
        $logger->info( trim( $text ), $context );
 }
@@ -1248,13 +1328,13 @@ function wfLogProfilingData() {
        // any knowledge about an URL and throw an exception instead.
        try {
                $ctx['url'] = urldecode( $wgRequest->getRequestURL() );
-       } catch ( MWException $ignored ) {
+       } catch ( Exception $ignored ) {
                // no-op
        }
 
        $ctx['output'] = $profiler->getOutput();
 
-       $log = MWLogger::getInstance( 'profileoutput' );
+       $log = MWLoggerFactory::getInstance( 'profileoutput' );
        $log->info( "Elapsed: {elapsed}; URL: <{url}>\n{output}", $ctx );
 }
 
@@ -1509,10 +1589,8 @@ function wfMsgForContentNoTrans( $key ) {
 function wfMsgReal( $key, $args, $useDB = true, $forContent = false, $transform = true ) {
        wfDeprecated( __METHOD__, '1.21' );
 
-       wfProfileIn( __METHOD__ );
        $message = wfMsgGetKey( $key, $useDB, $forContent, $transform );
        $message = wfMsgReplaceArgs( $message, $args );
-       wfProfileOut( __METHOD__ );
        return $message;
 }
 
@@ -2069,10 +2147,12 @@ function wfVarDump( $var ) {
  */
 function wfHttpError( $code, $label, $desc ) {
        global $wgOut;
-       $wgOut->disable();
        header( "HTTP/1.0 $code $label" );
        header( "Status: $code $label" );
-       $wgOut->sendCacheControl();
+       if ( $wgOut ) {
+               $wgOut->disable();
+               $wgOut->sendCacheControl();
+       }
 
        header( 'Content-type: text/html; charset=utf-8' );
        print "<!doctype html>" .
@@ -3946,7 +4026,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
+ * @deprecated 1.25 - use Hooks::run
  */
 function wfRunHooks( $event, array $args = array(), $deprecatedVersion = null ) {
        return Hooks::run( $event, $args, $deprecatedVersion );
@@ -4004,7 +4084,6 @@ function wfUnpack( $format, $data, $length = false ) {
  */
 function wfIsBadImage( $name, $contextTitle = false, $blacklist = null ) {
        static $badImageCache = null; // based on bad_image_list msg
-       wfProfileIn( __METHOD__ );
 
        # Handle redirects
        $redirectTitle = RepoGroup::singleton()->checkRedirect( Title::makeTitle( NS_FILE, $name ) );
@@ -4015,7 +4094,6 @@ function wfIsBadImage( $name, $contextTitle = false, $blacklist = null ) {
        # Run the extension hook
        $bad = false;
        if ( !Hooks::run( 'BadImage', array( $name, &$bad ) ) ) {
-               wfProfileOut( __METHOD__ );
                return $bad;
        }
 
@@ -4065,7 +4143,6 @@ function wfIsBadImage( $name, $contextTitle = false, $blacklist = null ) {
 
        $contextKey = $contextTitle ? $contextTitle->getPrefixedDBkey() : false;
        $bad = isset( $badImages[$name] ) && !isset( $badImages[$name][$contextKey] );
-       wfProfileOut( __METHOD__ );
        return $bad;
 }