OutputPage: Support foreign module sources in makeResourceLoaderLink
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoader.php
index 680bd99..36e3a1b 100644 (file)
@@ -35,6 +35,9 @@ class ResourceLoader {
        /** @var array */
        protected static $requiredSourceProperties = array( 'loadScript' );
 
+       /** @var bool */
+       protected static $debugMode = null;
+
        /** @var array Module name/ResourceLoaderModule object pairs */
        protected $modules = array();
 
@@ -145,9 +148,10 @@ class ResourceLoader {
         *
         * @param string $filter Name of filter to run
         * @param string $data Text to filter, such as JavaScript or CSS text
+        * @param string $cacheReport Whether to include the cache key report
         * @return string Filtered data, or a comment containing an error message
         */
-       protected function filter( $filter, $data ) {
+       public function filter( $filter, $data, $cacheReport = true ) {
                global $wgResourceLoaderMinifierStatementsOnOwnLine, $wgResourceLoaderMinifierMaxLineLength;
                wfProfileIn( __METHOD__ );
 
@@ -179,11 +183,15 @@ class ResourceLoader {
                                                $wgResourceLoaderMinifierStatementsOnOwnLine,
                                                $wgResourceLoaderMinifierMaxLineLength
                                        );
-                                       $result .= "\n/* cache key: $key */";
+                                       if ( $cacheReport ) {
+                                               $result .= "\n/* cache key: $key */";
+                                       }
                                        break;
                                case 'minify-css':
                                        $result = CSSMin::minify( $data );
-                                       $result .= "\n/* cache key: $key */";
+                                       if ( $cacheReport ) {
+                                               $result .= "\n/* cache key: $key */";
+                                       }
                                        break;
                        }
 
@@ -449,6 +457,22 @@ class ResourceLoader {
                return $this->sources;
        }
 
+       /**
+        * Get the URL to the load.php endpoint for the given
+        * ResourceLoader source
+        *
+        * @since 1.24
+        * @param string $source
+        * @throws MWException on an invalid $source name
+        * @return string
+        */
+       public function getLoadScript( $source ) {
+               if ( !isset( $this->sources[$source] ) ) {
+                       throw new MWException( "The $source source was never registered in ResourceLoader." );
+               }
+               return $this->sources[$source]['loadScript'];
+       }
+
        /**
         * Output a response to a load request, including the content-type header.
         *
@@ -814,7 +838,7 @@ class ResourceLoader {
                                        // Don't create empty stylesheets like array( '' => '' ) for modules
                                        // that don't *have* any stylesheets (bug 38024).
                                        $stylePairs = $module->getStyles( $context );
-                                       if ( count ( $stylePairs ) ) {
+                                       if ( count( $stylePairs ) ) {
                                                // If we are in debug mode without &only= set, we'll want to return an array of URLs
                                                // See comment near shouldIncludeScripts() for more details
                                                if ( $context->getDebug() && !$context->getOnly() && $module->supportsURLLoading() ) {
@@ -988,7 +1012,7 @@ class ResourceLoader {
         * @param array $stylePairs Array keyed by media type containing (arrays of) CSS strings
         * @return array
         */
-       private static function makeCombinedStyles( array $stylePairs ) {
+       public static function makeCombinedStyles( array $stylePairs ) {
                $out = array();
                foreach ( $stylePairs as $media => $styles ) {
                        // ResourceLoaderFileModule::getStyle can return the styles
@@ -1074,15 +1098,15 @@ class ResourceLoader {
         * Returns JS code which calls mw.loader.register with the given
         * parameters. Has three calling conventions:
         *
-        *   - ResourceLoader::makeLoaderRegisterScript( $name, $version, $dependencies, $group, $source ):
+        *   - ResourceLoader::makeLoaderRegisterScript( $name, $version, $dependencies, $group, $source, $skip ):
         *       Register a single module.
         *
         *   - ResourceLoader::makeLoaderRegisterScript( array( $name1, $name2 ) ):
         *       Register modules with the given names.
         *
         *   - ResourceLoader::makeLoaderRegisterScript( array(
-        *        array( $name1, $version1, $dependencies1, $group1, $source1 ),
-        *        array( $name2, $version2, $dependencies1, $group2, $source2 ),
+        *        array( $name1, $version1, $dependencies1, $group1, $source1, $skip1 ),
+        *        array( $name2, $version2, $dependencies1, $group2, $source2, $skip2 ),
         *        ...
         *     ) ):
         *        Registers modules with the given names and parameters.
@@ -1092,10 +1116,11 @@ class ResourceLoader {
         * @param array $dependencies List of module names on which this module depends
         * @param string $group Group which the module is in
         * @param string $source Source of the module, or 'local' if not foreign
+        * @param string $skip Script body of the skip function
         * @return string
         */
        public static function makeLoaderRegisterScript( $name, $version = null,
-               $dependencies = null, $group = null, $source = null
+               $dependencies = null, $group = null, $source = null, $skip = null
        ) {
                if ( is_array( $name ) ) {
                        return Xml::encodeJsCall(
@@ -1107,7 +1132,7 @@ class ResourceLoader {
                        $version = (int)$version > 1 ? (int)$version : 1;
                        return Xml::encodeJsCall(
                                'mw.loader.register',
-                               array( $name, $version, $dependencies, $group, $source ),
+                               array( $name, $version, $dependencies, $group, $source, $skip ),
                                ResourceLoader::inDebugMode()
                        );
                }
@@ -1201,17 +1226,49 @@ class ResourceLoader {
         * @return bool
         */
        public static function inDebugMode() {
-               global $wgRequest, $wgResourceLoaderDebug;
-               static $retval = null;
-               if ( is_null( $retval ) ) {
-                       $retval = $wgRequest->getFuzzyBool( 'debug',
-                               $wgRequest->getCookie( 'resourceLoaderDebug', '', $wgResourceLoaderDebug ) );
+               if ( self::$debugMode === null ) {
+                       global $wgRequest, $wgResourceLoaderDebug;
+                       self::$debugMode = $wgRequest->getFuzzyBool( 'debug',
+                               $wgRequest->getCookie( 'resourceLoaderDebug', '', $wgResourceLoaderDebug )
+                       );
                }
-               return $retval;
+               return self::$debugMode;
+       }
+
+       /**
+        * Reset static members used for caching.
+        *
+        * Global state and $wgRequest are evil, but we're using it right
+        * now and sometimes we need to be able to force ResourceLoader to
+        * re-evaluate the context because it has changed (e.g. in the test suite).
+        */
+       public static function clearCache() {
+               self::$debugMode = null;
        }
 
        /**
         * Build a load.php URL
+        *
+        * @since 1.24
+        * @param string $source name of the ResourceLoader source
+        * @param ResourceLoaderContext $context
+        * @param array $extraQuery
+        * @return string URL to load.php. May be protocol-relative (if $wgLoadScript is procol-relative)
+        */
+       public function createLoaderURL( $source, ResourceLoaderContext $context,
+               $extraQuery = array()
+       ) {
+               $query = self::createLoaderQuery( $context, $extraQuery );
+               $script = $this->getLoadScript( $source );
+
+               // Prevent the IE6 extension check from being triggered (bug 28840)
+               // by appending a character that's invalid in Windows extensions ('*')
+               return wfExpandUrl( wfAppendQuery( $script, $query ) . '&*', PROTO_RELATIVE );
+       }
+
+       /**
+        * Build a load.php URL
+        * @deprecated since 1.24, use createLoaderURL instead
         * @param array $modules Array of module names (strings)
         * @param string $lang Language code
         * @param string $skin Skin name
@@ -1239,6 +1296,30 @@ class ResourceLoader {
                return wfExpandUrl( wfAppendQuery( $wgLoadScript, $query ) . '&*', PROTO_RELATIVE );
        }
 
+       /**
+        * Helper for createLoaderURL()
+        *
+        * @since 1.24
+        * @see makeLoaderQuery
+        * @param ResourceLoaderContext $context
+        * @param array $extraQuery
+        * @return array
+        */
+       public static function createLoaderQuery( ResourceLoaderContext $context, $extraQuery = array() ) {
+               return self::makeLoaderQuery(
+                       $context->getModules(),
+                       $context->getLanguage(),
+                       $context->getSkin(),
+                       $context->getUser(),
+                       $context->getVersion(),
+                       $context->getDebug(),
+                       $context->getOnly(),
+                       $context->getRequest()->getBool( 'printable' ),
+                       $context->getRequest()->getBool( 'handheld' ),
+                       $extraQuery
+               );
+       }
+
        /**
         * Build a query array (array representation of query string) for load.php. Helper
         * function for makeLoaderURL().