r108111 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108110‎ | r108111 | r108112 >
Date:01:58, 5 January 2012
Author:aaron
Status:ok
Tags:
Comment:
In SpecialUploadStash:
* Updated outputLocallyScaledThumb() and outputLocalFile() to handle changes in r106752.
In MediaTransformOutput:
* Added a storage path field to the transformation output object and set it in File::maybeDoTransform().
In File:
* Fixed maybeDoTransform() handling if repo member is not set and made it fully respect $wgIgnoreImageErrors.
In BitmapHandler:
* Don't set bogus path if TRANSFORM_LATER in doTransform() for deffered renderings.
Modified paths:
  • /trunk/phase3/includes/filerepo/file/File.php (modified) (history)
  • /trunk/phase3/includes/media/Bitmap.php (modified) (history)
  • /trunk/phase3/includes/media/MediaTransformOutput.php (modified) (history)
  • /trunk/phase3/includes/specials/SpecialUploadStash.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/filerepo/file/File.php
@@ -757,57 +757,63 @@
758758 global $wgIgnoreImageErrors, $wgThumbnailEpoch;
759759
760760 $thumbPath = $this->getThumbPath( $thumbName ); // final thumb path
761 - if ( $this->repo && $this->repo->canTransformVia404() && !( $flags & self::RENDER_NOW ) ) {
762 - wfDebug( __METHOD__ . " transformation deferred." );
763 - // XXX: Pass in the storage path even though we are not rendering anything
764 - // and the path is supposed to be an FS path. This is due to getScalerType()
765 - // getting called on the path and clobbering $thumb->getUrl() if it's false.
766 - return $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
767 - }
768 -
769 - wfDebug( __METHOD__.": Doing stat for $thumbPath\n" );
770 - $this->migrateThumbFile( $thumbName );
771 - if ( $this->repo->fileExists( $thumbPath ) && !( $flags & self::RENDER_FORCE ) ) {
772 - $timestamp = $this->repo->getFileTimestamp( $thumbPath );
773 - if ( $timestamp !== false && $timestamp >= $wgThumbnailEpoch ) {
 761+ if ( $this->repo ) {
 762+ // Defer rendering if a 404 handler is set up...
 763+ if ( $this->repo->canTransformVia404() && !( $flags & self::RENDER_NOW ) ) {
 764+ wfDebug( __METHOD__ . " transformation deferred." );
774765 // XXX: Pass in the storage path even though we are not rendering anything
775766 // and the path is supposed to be an FS path. This is due to getScalerType()
776767 // getting called on the path and clobbering $thumb->getUrl() if it's false.
777768 return $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
778769 }
779 - } elseif ( $flags & self::RENDER_FORCE ) {
780 - wfDebug( __METHOD__ . " forcing rendering per flag File::RENDER_FORCE\n" );
 770+ // Clean up broken thumbnails as needed
 771+ $this->migrateThumbFile( $thumbName );
 772+ // Check if an up-to-date thumbnail already exists...
 773+ wfDebug( __METHOD__.": Doing stat for $thumbPath\n" );
 774+ if ( $this->repo->fileExists( $thumbPath ) && !( $flags & self::RENDER_FORCE ) ) {
 775+ $timestamp = $this->repo->getFileTimestamp( $thumbPath );
 776+ if ( $timestamp !== false && $timestamp >= $wgThumbnailEpoch ) {
 777+ // XXX: Pass in the storage path even though we are not rendering anything
 778+ // and the path is supposed to be an FS path. This is due to getScalerType()
 779+ // getting called on the path and clobbering $thumb->getUrl() if it's false.
 780+ $thumb = $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
 781+ $thumb->setStoragePath( $thumbPath );
 782+ return $thumb;
 783+ }
 784+ } elseif ( $flags & self::RENDER_FORCE ) {
 785+ wfDebug( __METHOD__ . " forcing rendering per flag File::RENDER_FORCE\n" );
 786+ }
781787 }
782788
783789 // Create a temp FS file with the same extension and the thumbnail
784790 $thumbExt = FileBackend::extensionFromPath( $thumbPath );
785791 $tmpFile = TempFSFile::factory( 'transform_', $thumbExt );
786792 if ( !$tmpFile ) {
787 - return new MediaTransformError( 'thumbnail_error',
788 - $params['width'], 0, wfMsg( 'thumbnail-temp-create' ) );
 793+ return $this->transformErrorOutput( $thumbPath, $thumbUrl, $params, $flags );
789794 }
790795 $tmpThumbPath = $tmpFile->getPath(); // path of 0-byte temp file
791796
792 - // Actually render the thumbnail
 797+ // Actually render the thumbnail...
793798 $thumb = $this->handler->doTransform( $this, $tmpThumbPath, $thumbUrl, $params );
794799 $tmpFile->bind( $thumb ); // keep alive with $thumb
795800
796 - // Ignore errors if requested
797 - if ( !$thumb ) {
 801+ if ( !$thumb ) { // bad params?
798802 $thumb = null;
799 - } elseif ( $thumb->isError() ) {
 803+ } elseif ( $thumb->isError() ) { // transform error
800804 $this->lastError = $thumb->toText();
 805+ // Ignore errors if requested
801806 if ( $wgIgnoreImageErrors && !( $flags & self::RENDER_NOW ) ) {
802807 $thumb = $this->handler->getTransform( $this, $tmpThumbPath, $thumbUrl, $params );
803808 }
804809 } elseif ( $thumb->hasFile() && !$thumb->fileIsSource() ) {
805 - // Copy any thumbnail from the FS into storage at $dstpath
 810+ // Copy the thumbnail from the file system into storage
806811 $status = $this->repo->store(
807812 $tmpThumbPath, 'thumb', $this->getThumbRel( $thumbName ),
808813 FileRepo::OVERWRITE | FileRepo::SKIP_LOCKING | FileRepo::ALLOW_STALE );
809 - if ( !$status->isOK() ) {
810 - return new MediaTransformError( 'thumbnail_error',
811 - $params['width'], 0, wfMsg( 'thumbnail-dest-create' ) );
 814+ if ( $status->isOK() ) {
 815+ $thumb->setStoragePath( $thumbPath );
 816+ } else {
 817+ $thumb = $this->transformErrorOutput( $thumbPath, $thumbUrl, $params, $flags );
812818 }
813819 }
814820
@@ -815,6 +821,26 @@
816822 }
817823
818824 /**
 825+ * Return either a MediaTransformError or placeholder thumbnail (if $wgIgnoreImageErrors)
 826+ *
 827+ * @param $thumbPath string Thumbnail storage path
 828+ * @param $thumbUrl string Thumbnail URL
 829+ * @param $params Array
 830+ * @param $flags integer
 831+ * @return MediaTransformOutput
 832+ */
 833+ protected function transformErrorOutput( $thumbPath, $thumbUrl, $params, $flags ) {
 834+ global $wgIgnoreImageErrors;
 835+
 836+ if ( $wgIgnoreImageErrors && !( $flags & self::RENDER_NOW ) ) {
 837+ return $this->handler->getTransform( $this, $thumbPath, $thumbUrl, $params );
 838+ } else {
 839+ return new MediaTransformError( 'thumbnail_error',
 840+ $params['width'], 0, wfMsg( 'thumbnail-dest-create' ) );
 841+ }
 842+ }
 843+
 844+ /**
819845 * Transform a media file
820846 *
821847 * @param $params Array: an associative array of handler-specific parameters.
Index: trunk/phase3/includes/media/MediaTransformOutput.php
@@ -18,6 +18,7 @@
1919 var $file;
2020
2121 var $width, $height, $url, $page, $path;
 22+ protected $storagePath = false;
2223
2324 /**
2425 * Get the width of the output box
@@ -41,6 +42,21 @@
4243 }
4344
4445 /**
 46+ * @return string|false The permanent thumbnail storage path
 47+ */
 48+ public function getStoragePath() {
 49+ return $this->storagePath;
 50+ }
 51+
 52+ /**
 53+ * @param $storagePath string The permanent storage path
 54+ * @return void
 55+ */
 56+ public function setStoragePath( $storagePath ) {
 57+ $this->storagePath = $storagePath;
 58+ }
 59+
 60+ /**
4561 * Fetch HTML for this transform output
4662 *
4763 * @param $options array Associative array of options. Boolean options
Index: trunk/phase3/includes/media/Bitmap.php
@@ -154,7 +154,7 @@
155155 if ( $flags & self::TRANSFORM_LATER ) {
156156 wfDebug( __METHOD__ . ": Transforming later per flags.\n" );
157157 return new ThumbnailImage( $image, $dstUrl, $scalerParams['clientWidth'],
158 - $scalerParams['clientHeight'], $dstPath );
 158+ $scalerParams['clientHeight'], false );
159159 }
160160
161161 # Try to make a target path for the thumbnail
Index: trunk/phase3/includes/specials/SpecialUploadStash.php
@@ -166,14 +166,15 @@
167167 }
168168
169169 // we should have just generated it locally
170 - if ( ! $thumbnailImage->getPath() ) {
 170+ if ( !$thumbnailImage->getStoragePath() ) {
171171 throw new UploadStashFileNotFoundException( "no local path for scaled item" );
172172 }
173173
174174 // now we should construct a File, so we can get mime and other such info in a standard way
175175 // n.b. mimetype may be different from original (ogx original -> jpeg thumb)
176 - $thumbFile = new UnregisteredLocalFile( false, $this->stash->repo, $thumbnailImage->getPath(), false );
177 - if ( ! $thumbFile ) {
 176+ $thumbFile = new UnregisteredLocalFile( false,
 177+ $this->stash->repo, $thumbnailImage->getStoragePath(), false );
 178+ if ( !$thumbFile ) {
178179 throw new UploadStashFileNotFoundException( "couldn't create local file object for thumbnail" );
179180 }
180181
@@ -238,18 +239,17 @@
239240 /**
240241 * Output HTTP response for file
241242 * Side effect: writes HTTP response to STDOUT.
242 - * XXX could use wfStreamfile (in includes/Streamfile.php), but for consistency with outputContents() doing it this way.
243 - * XXX is mimeType really enough, or do we need encoding for full Content-Type header?
244243 *
245244 * @param $file File object with a local path (e.g. UnregisteredLocalFile, LocalFile. Oddly these don't share an ancestor!)
246245 */
247 - private function outputLocalFile( $file ) {
 246+ private function outputLocalFile( File $file ) {
248247 if ( $file->getSize() > self::MAX_SERVE_BYTES ) {
249248 throw new SpecialUploadStashTooLargeException();
250249 }
251 - self::outputFileHeaders( $file->getMimeType(), $file->getSize() );
252 - readfile( $file->getPath() );
253 - return true;
 250+ return $file->getRepo()->streamFile( $file->getPath(),
 251+ array( 'Content-Transfer-Encoding: binary',
 252+ 'Expires: Sun, 17-Jan-2038 19:14:07 GMT' )
 253+ );
254254 }
255255
256256 /**

Sign-offs

UserFlagDate
😂inspected21:51, 11 January 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r106752Merged FileBackend branch. Manually avoiding merging the many prop-only chang...aaron03:52, 20 December 2011

Status & tagging log