ResourceLoaderImageModule for icons
[lhc/web/wiklou.git] / includes / resourceloader / ResourceLoader.php
index 8602fb0..f6f6d1b 100644 (file)
@@ -646,19 +646,32 @@ class ResourceLoader {
                // Generate a response
                $response = $this->makeModuleResponse( $context, $modules, $missing );
 
-               // Prepend comments indicating exceptions
-               $response = $errors . $response;
-
                // Capture any PHP warnings from the output buffer and append them to the
-               // response in a comment if we're in debug mode.
+               // error list if we're in debug mode.
                if ( $context->getDebug() && strlen( $warnings = ob_get_contents() ) ) {
-                       $response = self::makeComment( $warnings ) . $response;
+                       $errors .= self::makeComment( $warnings );
+                       $this->hasErrors = true;
+               }
+
+               if ( $context->getImageObj() && !$response ) {
+                       $errors .= self::makeComment( "Image generation failed." );
                        $this->hasErrors = true;
                }
 
+               if ( $this->hasErrors ) {
+                       if ( $context->getImageObj() ) {
+                               // Bail, we can't show both the error messages and the response when it's not CSS or JS.
+                               // sendResponseHeaders() will handle this sensibly.
+                               $response = $errors;
+                       } else {
+                               // Prepend comments indicating exceptions
+                               $response = $errors . $response;
+                       }
+               }
+
                // Save response to file cache unless there are errors
-               if ( isset( $fileCache ) && !$errors && !count( $missing ) ) {
-                       // Cache single modules...and other requests if there are enough hits
+               if ( isset( $fileCache ) && !$this->hasErrors && !count( $missing ) ) {
+                       // Cache single modules and images...and other requests if there are enough hits
                        if ( ResourceFileCache::useFileCache( $context ) ) {
                                if ( $fileCache->isCacheWorthy() ) {
                                        $fileCache->saveText( $response );
@@ -699,7 +712,13 @@ class ResourceLoader {
                        $maxage = $rlMaxage['versioned']['client'];
                        $smaxage = $rlMaxage['versioned']['server'];
                }
-               if ( $context->getOnly() === 'styles' ) {
+               if ( $context->getImageObj() ) {
+                       if ( $errors ) {
+                               header( 'Content-Type: text/plain; charset=utf-8' );
+                       } else {
+                               $context->getImageObj()->sendResponseHeaders( $context );
+                       }
+               } elseif ( $context->getOnly() === 'styles' ) {
                        header( 'Content-Type: text/css; charset=utf-8' );
                        header( 'Access-Control-Allow-Origin: *' );
                } else {
@@ -858,6 +877,13 @@ class ResourceLoader {
 
                wfProfileIn( __METHOD__ );
 
+               $image = $context->getImageObj();
+               if ( $image ) {
+                       $data = $image->getImageData( $context );
+                       wfProfileOut( __METHOD__ );
+                       return $data;
+               }
+
                // Pre-fetch blobs
                if ( $context->shouldIncludeMessages() ) {
                        try {
@@ -1188,6 +1214,27 @@ class ResourceLoader {
                );
        }
 
+       /**
+        * Remove empty values from the end of an array.
+        *
+        * Values considered empty:
+        *
+        * - null
+        * - empty array
+        *
+        * @param Array $array
+        */
+       private static function trimArray( Array &$array ) {
+               $i = count( $array );
+               while ( $i-- ) {
+                       if ( $array[$i] === null || $array[$i] === array() ) {
+                               unset( $array[$i] );
+                       } else {
+                               break;
+                       }
+               }
+       }
+
        /**
         * Returns JS code which calls mw.loader.register with the given
         * parameters. Has three calling conventions:
@@ -1221,7 +1268,7 @@ class ResourceLoader {
                if ( is_array( $name ) ) {
                        // Build module name index
                        $index = array();
-                       foreach ( $name as $i => $module ) {
+                       foreach ( $name as $i => &$module ) {
                                $index[$module[0]] = $i;
                        }
 
@@ -1237,16 +1284,19 @@ class ResourceLoader {
                                }
                        }
 
+                       array_walk( $name, array( 'self', 'trimArray' ) );
+
                        return Xml::encodeJsCall(
                                'mw.loader.register',
                                array( $name ),
                                ResourceLoader::inDebugMode()
                        );
                } else {
-                       $version = (int) $version;
+                       $registration = array( $name, $version, $dependencies, $group, $source, $skip );
+                       self::trimArray( $registration );
                        return Xml::encodeJsCall(
                                'mw.loader.register',
-                               array( $name, $version, $dependencies, $group, $source, $skip ),
+                               $registration,
                                ResourceLoader::inDebugMode()
                        );
                }