Bartosz Dziewoński has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/200483

Change subject: ResourceLoaderImage: Throw exception early if files don't exist
......................................................................

ResourceLoaderImage: Throw exception early if files don't exist

Also some cleanup, typo fixes and tweaks.

Change-Id: I67eb795f7fc4d35d15feb0717b1b781cbc45f78a
---
M includes/resourceloader/ResourceLoaderImage.php
1 file changed, 18 insertions(+), 14 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/83/200483/1

diff --git a/includes/resourceloader/ResourceLoaderImage.php 
b/includes/resourceloader/ResourceLoaderImage.php
index 4b16a12..6ecfa4f 100644
--- a/includes/resourceloader/ResourceLoaderImage.php
+++ b/includes/resourceloader/ResourceLoaderImage.php
@@ -53,19 +53,22 @@
                $this->basePath = $basePath;
                $this->variants = $variants;
 
-               // Ensure that all files have common extension.
+               // Ensure that all files exist and have common extension.
                $extensions = array();
                $descriptor = (array)$descriptor;
-               array_walk_recursive( $descriptor, function ( $path ) use ( 
&$extensions ) {
+               array_walk_recursive( $descriptor, function ( $path ) use ( 
&$extensions, $basePath, $name ) {
+                       if ( !file_exists( $basePath . '/' . $path ) ) {
+                               throw new MWException( "File '$path' of '$name' 
does not exist" );
+                       }
                        $extensions[] = pathinfo( $path, PATHINFO_EXTENSION );
                } );
                $extensions = array_unique( $extensions );
                if ( count( $extensions ) !== 1 ) {
-                       throw new MWException( "File type for different image 
files of '$name' not the same." );
+                       throw new MWException( "File type for different image 
files of '$name' not the same" );
                }
                $ext = $extensions[0];
                if ( !isset( self::$fileTypes[$ext] ) ) {
-                       throw new MWException( "Invalid file type for image 
files of '$name' (valid: svg, png, gif, jpg)." );
+                       throw new MWException( "Invalid file type for image 
files of '$name' (valid: svg, png, gif, jpg)" );
                }
                $this->extension = $ext;
        }
@@ -184,11 +187,11 @@
         * Call getExtension() or getMimeType() with the same $format argument 
to learn what file type the
         * returned data uses.
         *
-        * @param ResourceLoaderContext $context Image context, or any context 
of $variant and $format
+        * @param ResourceLoaderContext $context Image context, or any context 
if $variant and $format
         *     given.
-        * @param string|null $variant Variant to get the data for. Optional, 
if given, overrides the data
+        * @param string|null $variant Variant to get the data for. Optional; 
if given, overrides the data
         *     from $context.
-        * @param string $format Format to get the data for, 'original' or 
'rasterized'. Optional, if
+        * @param string $format Format to get the data for, 'original' or 
'rasterized'. Optional; if
         *     given, overrides the data from $context.
         * @return string|false Possibly binary image data, or false on failure
         */
@@ -201,6 +204,7 @@
                }
 
                $path = $this->getPath( $context );
+
                if ( $this->getExtension() !== 'svg' ) {
                        return file_get_contents( $path );
                }
@@ -214,7 +218,7 @@
                if ( $format === 'rasterized' ) {
                        $data = $this->rasterize( $data );
                        if ( !$data ) {
-                               wfDebugLog( 'ResourceLoaderImage', __METHOD__  
. " failed to rasterize for $path" );
+                               wfDebugLog( 'ResourceLoaderImage', __METHOD__ . 
" failed to rasterize for $path" );
                        }
                }
 
@@ -260,7 +264,7 @@
        }
 
        /**
-        * Massage the SVG image data for converters which doesn't understand 
some path data syntax.
+        * Massage the SVG image data for converters which don't understand 
some path data syntax.
         *
         * This is necessary for rsvg and ImageMagick when compiled with rsvg 
support.
         * Upstream bug is https://bugzilla.gnome.org/show_bug.cgi?id=620923, 
fixed 2014-11-10, so
@@ -295,11 +299,11 @@
                // class, with a separate set of configuration settings.
                //
                // This is a distinct use case from regular SVG rasterization:
-               // * we can skip many sanity and security checks (as the images 
come from a trusted source,
-               //   rather than from the user)
-               // * we need to provide extra options to some converters to 
achieve acceptable quality for very
-               //   small images, which might cause performance issues in the 
general case
-               // * we need to directly pass image data to the converter 
instead of a file path
+               // * We can skip many sanity and security checks (as the images 
come from a trusted source,
+               //   rather than from the user).
+               // * We need to provide extra options to some converters to 
achieve acceptable quality for very
+               //   small images, which might cause performance issues in the 
general case.
+               // * We want to directly pass image data to the converter, 
rather than a file path.
                //
                // See https://phabricator.wikimedia.org/T76473#801446 for 
examples of what happens with the
                // default settings.

-- 
To view, visit https://gerrit.wikimedia.org/r/200483
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I67eb795f7fc4d35d15feb0717b1b781cbc45f78a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Bartosz Dziewoński <matma....@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to