From 586cf9a929e622816799f9b58ddc18568c1c0efc Mon Sep 17 00:00:00 2001 From: Bryan Tong Minh Date: Sun, 30 Jan 2011 16:57:19 +0000 Subject: [PATCH] As per my comments on bug 27038 and my comments on r75906, use a /thumb and /file prefix for files fetched via Special:UploadStash. Make filenames more consistent with regular thumbs by using the /thumb/$fileName/$thumbName scheme. Now uses MediaHandler::parseParamString so that params like page are properly handled. --- includes/specials/SpecialUploadStash.php | 68 ++++++++++++++++-------- includes/upload/UploadStash.php | 4 +- 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/includes/specials/SpecialUploadStash.php b/includes/specials/SpecialUploadStash.php index 2f0335b47d..befc7812fb 100644 --- a/includes/specials/SpecialUploadStash.php +++ b/includes/specials/SpecialUploadStash.php @@ -84,11 +84,11 @@ class SpecialUploadStash extends UnlistedSpecialPage { $wgOut->disable(); try { - if ( preg_match( '/^(\d+)px-(.*)$/', $key, $matches ) ) { - list( /* full match */, $width, $key ) = $matches; - return $this->outputThumbFromStash( $key, $width ); + $params = $this->parseKey( $key ); + if ( $params['type'] === 'thumb' ) { + return $this->outputThumbFromStash( $params['file'], $params['params'] ); } else { - return $this->outputFileFromStash( $key ); + return $this->outputLocalFile( $params['file'] ); } } catch( UploadStashFileNotFoundException $e ) { $code = 404; @@ -110,16 +110,42 @@ class SpecialUploadStash extends UnlistedSpecialPage { wfHttpError( $code, OutputPage::getStatusMessage( $code ), $message ); return false; } - + /** - * Get a file from stash and stream it out. Rely on parent to catch exceptions and transform them into HTTP - * @param String: $key - key of this file in the stash, which probably looks like a filename with extension. - * @return boolean + * Parse the key passed to the SpecialPage. Returns an array containing + * the associated file object, the type ('file' or 'thumb') and if + * application the transform parameters + * + * @param string $key + * @return array */ - private function outputFileFromStash( $key ) { - $file = $this->stash->getFile( $key ); - return $this->outputLocalFile( $file ); + private function parseKey( $key ) { + $type = strtok( $key, '/' ); + + if ( $type !== 'file' && $type !== 'thumb' ) { + throw new UploadStashBadPathException( "Unknown type '$type'" ); + } + $fileName = strtok( '/' ); + $thumbPart = strtok( '/' ); + $file = $this->stash->getFile( $fileName ); + if ( $type === 'thumb' ) { + + $parts = explode( "-{$fileName}", $thumbPart ); + + if ( count( $parts ) != 2 || $parts[1] !== '' ) { + throw new UploadStashBadPathException( 'Invalid suffix' ); + } + + + $handler = $file->getHandler(); + $params = $handler->parseParamString( $parts[0] ); + return array( 'file' => $file, 'type' => $type, 'params' => $params ); + } + + return array( 'file' => $file, 'type' => $type ); } + + /** @@ -128,25 +154,20 @@ class SpecialUploadStash extends UnlistedSpecialPage { * @param int $width: width of desired thumbnail * @return boolean success */ - private function outputThumbFromStash( $key, $width ) { + private function outputThumbFromStash( $file, $params ) { // this global, if it exists, points to a "scaler", as you might find in the Wikimedia Foundation cluster. See outputRemoteScaledThumb() // this is part of our horrible NFS-based system, we create a file on a mount point here, but fetch the scaled file from somewhere else that // happens to share it over NFS global $wgUploadStashScalerBaseUrl; - // let exceptions propagate to caller. - $file = $this->stash->getFile( $key ); - - // OK, we're here and no exception was thrown, - // so the original file must exist. - - // let's get ready to transform the original -- these are standard - $params = array( 'width' => $width ); $flags = 0; + if ( $wgUploadStashScalerBaseUrl ) { + $this->outputRemoteScaledThumb( $file, $params, $flags ); + } else { + $this->outputLocallyScaledThumb( $file, $params, $flags ); + } - return $wgUploadStashScalerBaseUrl ? $this->outputRemoteScaledThumb( $file, $params, $flags ) - : $this->outputLocallyScaledThumb( $file, $params, $flags ); } @@ -353,7 +374,8 @@ class SpecialUploadStash extends UnlistedSpecialPage { $fileListItemsHtml = ''; foreach ( $files as $file ) { $fileListItemsHtml .= Html::rawElement( 'li', array(), - Html::element( 'a', array( 'href' => $this->getTitle( $file )->getLocalURL() ), $file ) + Html::element( 'a', array( 'href' => + $this->getTitle( "file/$file" )->getLocalURL() ), $file ) ); } $wgOut->addHtml( Html::rawElement( 'ul', array(), $fileListItemsHtml ) ); diff --git a/includes/upload/UploadStash.php b/includes/upload/UploadStash.php index 8dd2c766c9..27e0ec1bb7 100644 --- a/includes/upload/UploadStash.php +++ b/includes/upload/UploadStash.php @@ -358,7 +358,7 @@ class UploadStashFile extends UnregisteredLocalFile { */ public function getThumbUrl( $thumbName = false ) { wfDebug( __METHOD__ . " getting for $thumbName \n" ); - return $this->getSpecialUrl( $thumbName ); + return $this->getSpecialUrl( 'thumb/' . $this->getUrlName() . '/' . $thumbName ); } /** @@ -382,7 +382,7 @@ class UploadStashFile extends UnregisteredLocalFile { */ public function getUrl() { if ( !isset( $this->url ) ) { - $this->url = $this->getSpecialUrl( $this->getUrlName() ); + $this->url = $this->getSpecialUrl( 'file/' . $this->getUrlName() ); } return $this->url; } -- 2.20.1