[FileRepo] Added support for temp file repos (to preserve our thumb.php hack).
authorAaron Schulz <aschulz@wikimedia.org>
Fri, 6 Apr 2012 17:11:13 +0000 (10:11 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Fri, 6 Apr 2012 22:55:44 +0000 (15:55 -0700)
* Added FileRepo::getTempRepo() to get a temporary repo similiar to the one used in wmf1.19 thumb.php.
* Merged and cleaned up wmf1.19 code into thumb.php to handle temp files and thumbnails.

Change 1: prevent FileRepo::getTempRepo() from being called on the result of FileRepo::getTempRepo().

Change 2: made FileRepo::getTempRepo() account for cases when the 'directory' param is not the empty string.

Change 3:
* Tweaked TempFileRepo to override getTempRepo() to enforce restriction.
* Added FileRepo::getTempHashPath() and used in thumb.php to get the correct path.

Change-Id: I2937b5ffb3ec9a7e2741b42bc28dc76833053fcd

includes/filerepo/FileRepo.php
thumb.php

index d121454..dcc9610 100644 (file)
@@ -483,13 +483,26 @@ class FileRepo {
         * Get a relative path including trailing slash, e.g. f/fa/
         * If the repo is not hashed, returns an empty string
         *
-        * @param $name string
+        * @param $name string Name of file
         * @return string
         */
        public function getHashPath( $name ) {
                return self::getHashPathForLevel( $name, $this->hashLevels );
        }
 
+       /**
+        * Get a relative path including trailing slash, e.g. f/fa/
+        * If the repo is not hashed, returns an empty string
+        *
+        * @param $suffix string Basename of file from FileRepo::storeTemp()
+        * @return string
+        */
+       public function getTempHashPath( $suffix ) {
+               $parts = explode( '!', $suffix, 2 ); // format is <timestamp>!<name> or just <name>
+               $name = isset( $parts[1] ) ? $parts[1] : $suffix; // hash path is not based on timestamp
+               return self::getHashPathForLevel( $name, $this->hashLevels );
+       }
+
        /**
         * @param $name
         * @param $levels
@@ -1404,6 +1417,36 @@ class FileRepo {
                return call_user_func_array( 'wfMemcKey', $args );
        }
 
+       /**
+        * Get an temporary FileRepo associated with this repo.
+        * Files will be created in the temp zone of this repo and
+        * thumbnails in a /temp subdirectory in thumb zone of this repo.
+        * It will have the same backend as this repo.
+        *
+        * @return TempFileRepo
+        */
+       public function getTempRepo() {
+               return new TempFileRepo( array(
+                       'name'      => "{$this->name}-temp",
+                       'backend'   => $this->backend,
+                       'zones'     => array(
+                               'public' => array(
+                                       'container' => $this->zones['temp']['container'],
+                                       'directory' => $this->zones['temp']['directory']
+                               ),
+                               'thumb'  => array(
+                                       'container' => $this->zones['thumb']['container'],
+                                       'directory' => ( $this->zones['thumb']['directory'] == '' )
+                                               ? 'temp'
+                                               : $this->zones['thumb']['directory'] . '/temp'
+                               )
+                       ),
+                       'url'        => $this->getZoneUrl( 'temp' ),
+                       'thumbUrl'   => $this->getZoneUrl( 'thumb' ) . '/temp',
+                       'hashLevels' => $this->hashLevels // performance
+               ) );
+       }
+
        /**
         * Get an UploadStash associated with this repo.
         *
@@ -1422,3 +1465,12 @@ class FileRepo {
         */
        protected function assertWritableRepo() {}
 }
+
+/**
+ * FileRepo for temporary files created via FileRepo::getTempRepo()
+ */
+class TempFileRepo extends FileRepo {
+       public function getTempRepo() {
+               throw new MWException( "Cannot get a temp repo from a temp repo." );
+       }
+}
index 6afc7e5..6855581 100644 (file)
--- a/thumb.php
+++ b/thumb.php
@@ -97,7 +97,11 @@ function wfStreamThumb( array $params ) {
 
        // Is this a thumb of an archived file?
        $isOld = ( isset( $params['archived'] ) && $params['archived'] );
-       unset( $params['archived'] );
+       unset( $params['archived'] ); // handlers don't care
+
+       // Is this a thumb of a temp file?
+       $isTemp = ( isset( $params['temp'] ) && $params['temp'] );
+       unset( $params['temp'] ); // handlers don't care
 
        // Some basic input validation
        $fileName = strtr( $fileName, '\\/', '__' );
@@ -118,6 +122,20 @@ function wfStreamThumb( array $params ) {
                        return;
                }
                $img = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $fileName );
+       } elseif ( $isTemp ) {
+               $repo = RepoGroup::singleton()->getLocalRepo()->getTempRepo();
+               // Format is <timestamp>!<name> or just <name>
+               $bits = explode( '!', $fileName, 2 );
+               // Get the name without the timestamp so hash paths are correctly computed
+               $title = Title::makeTitleSafe( NS_FILE, isset( $bits[1] ) ? $bits[1] : $fileName );
+               if ( !$title ) {
+                       wfThumbError( 404, wfMsg( 'badtitletext' ) );
+                       wfProfileOut( __METHOD__ );
+                       return;
+               }
+               $img = new UnregisteredLocalFile( $title, $repo,
+                       $repo->getZonePath( 'public' ) . '/' . $repo->getTempHashPath( $fileName ) . $fileName
+               );
        } else {
                $img = wfLocalFile( $fileName );
        }
@@ -178,7 +196,7 @@ function wfStreamThumb( array $params ) {
                        // Check that the zone relative path matches up so squid caches won't pick
                        // up thumbs that would not be purged on source file deletion (bug 34231).
                        if ( isset( $params['rel404'] ) // thumbnail was handled via 404
-                               && urldecode( $params['rel404'] ) !== $img->getThumbRel( $thumbName ) ) 
+                               && urldecode( $params['rel404'] ) !== $img->getThumbRel( $thumbName ) )
                        {
                                wfThumbError( 404, 'The source file for the specified thumbnail does not exist.' );
                                wfProfileOut( __METHOD__ );