Guard against non-object returns from File::getHander()
authorBryan Davis <bd808@wikimedia.org>
Thu, 5 Sep 2013 20:46:38 +0000 (14:46 -0600)
committerBryanDavis <bdavis@wikimedia.org>
Tue, 17 Sep 2013 06:48:43 +0000 (06:48 +0000)
MediaHandler::getHandler() can return false when a handler cannot be
determined for the given file's derived mime type. This change adds
guards to invocations that I could find that did not properly check for
this potential return result.

Bug: 53820
Change-Id: I8c0165311cc75f9920ac30ce2b38ccd207439198

includes/filerepo/file/File.php
includes/filerepo/file/LocalFile.php
includes/specials/SpecialUploadStash.php

index 9060731..05cb000 100644 (file)
@@ -842,8 +842,9 @@ abstract class File {
        protected function transformErrorOutput( $thumbPath, $thumbUrl, $params, $flags ) {
                global $wgIgnoreImageErrors;
 
-               if ( $wgIgnoreImageErrors && !( $flags & self::RENDER_NOW ) ) {
-                       return $this->getHandler()->getTransform( $this, $thumbPath, $thumbUrl, $params );
+               $handler = $this->getHandler();
+               if ( $handler && $wgIgnoreImageErrors && !( $flags & self::RENDER_NOW ) ) {
+                       return $handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
                } else {
                        return new MediaTransformError( 'thumbnail_error',
                                $params['width'], 0, wfMessage( 'thumbnail-dest-create' )->text() );
@@ -1000,7 +1001,7 @@ abstract class File {
        /**
         * Get a MediaHandler instance for this file
         *
-        * @return MediaHandler
+        * @return MediaHandler|boolean Registered MediaHandler for file's mime type or false if none found
         */
        function getHandler() {
                if ( !isset( $this->handler ) ) {
index 39ef62c..91dacf8 100644 (file)
@@ -610,7 +610,11 @@ class LocalFile extends File {
                $this->load();
 
                if ( $this->isMultipage() ) {
-                       $dim = $this->getHandler()->getPageDimensions( $this, $page );
+                       $handler = $this->getHandler();
+                       if ( !$handler ) {
+                               return 0;
+                       }
+                       $dim = $handler->getPageDimensions( $this, $page );
                        if ( $dim ) {
                                return $dim['width'];
                        } else {
@@ -633,7 +637,11 @@ class LocalFile extends File {
                $this->load();
 
                if ( $this->isMultipage() ) {
-                       $dim = $this->getHandler()->getPageDimensions( $this, $page );
+                       $handler = $this->getHandler();
+                       if ( !$handler ) {
+                               return 0;
+                       }
+                       $dim = $handler->getPageDimensions( $this, $page );
                        if ( $dim ) {
                                return $dim['height'];
                        } else {
index e7f36ee..002e949 100644 (file)
@@ -134,8 +134,13 @@ class SpecialUploadStash extends UnlistedSpecialPage {
                        $paramString = substr( $thumbPart, 0, $srcNamePos - 1 );
 
                        $handler = $file->getHandler();
-                       $params = $handler->parseParamString( $paramString );
-                       return array( 'file' => $file, 'type' => $type, 'params' => $params );
+                       if ( $handler ) {
+                               $params = $handler->parseParamString( $paramString );
+                               return array( 'file' => $file, 'type' => $type, 'params' => $params );
+                       } else {
+                               throw new UploadStashBadPathException( 'No handler found for ' .
+                                               "mime {$file->getMimeType()} of file {$file->getPath()}" );
+                       }
                }
 
                return array( 'file' => $file, 'type' => $type );