As per my comments on bug 27038 and my comments on r75906, use a /thumb and /file...
authorBryan Tong Minh <btongminh@users.mediawiki.org>
Sun, 30 Jan 2011 16:57:19 +0000 (16:57 +0000)
committerBryan Tong Minh <btongminh@users.mediawiki.org>
Sun, 30 Jan 2011 16:57:19 +0000 (16:57 +0000)
includes/specials/SpecialUploadStash.php
includes/upload/UploadStash.php

index 2f0335b..befc781 100644 (file)
@@ -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 ) );
index 8dd2c76..27e0ec1 100644 (file)
@@ -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;
        }