Merge "CologneBlue rewrite: kill mWatchLinkNum, watchThisPage() is only called once...
[lhc/web/wiklou.git] / includes / filerepo / FileRepo.php
index 32ef5a9..5f24fed 100644 (file)
@@ -51,6 +51,7 @@ class FileRepo {
        var $pathDisclosureProtection = 'simple'; // 'paranoid'
        var $descriptionCacheExpiry, $url, $thumbUrl;
        var $hashLevels, $deletedHashLevels;
+       protected $abbrvThreshold;
 
        /**
         * Factory functions for creating new files
@@ -113,10 +114,12 @@ class FileRepo {
                        ? $info['deletedHashLevels']
                        : $this->hashLevels;
                $this->transformVia404 = !empty( $info['transformVia404'] );
-               $this->zones = isset( $info['zones'] )
-                       ? $info['zones']
-                       : array();
+               $this->abbrvThreshold = isset( $info['abbrvThreshold'] )
+                       ? $info['abbrvThreshold']
+                       : 255;
+               $this->isPrivate = !empty( $info['isPrivate'] );
                // Give defaults for the basic zones...
+               $this->zones = isset( $info['zones'] ) ? $info['zones'] : array();
                foreach ( array( 'public', 'thumb', 'temp', 'deleted' ) as $zone ) {
                        if ( !isset( $this->zones[$zone]['container'] ) ) {
                                $this->zones[$zone]['container'] = "{$this->name}-{$zone}";
@@ -693,7 +696,7 @@ class FileRepo {
        /**
         * Store a file to a given destination.
         *
-        * @param $srcPath String: source FS path, storage path, or virtual URL
+        * @param $srcPath String: source file system path, storage path, or virtual URL
         * @param $dstZone String: destination zone
         * @param $dstRel String: destination relative path
         * @param $flags Integer: bitwise combination of the following flags:
@@ -838,12 +841,13 @@ class FileRepo {
         * This function can be used to write to otherwise read-only foreign repos.
         * This is intended for copying generated thumbnails into the repo.
         *
-        * @param $src string File system path
+        * @param $src string Source file system path, storage path, or virtual URL
         * @param $dst string Virtual URL or storage path
+        * @param $disposition string|null Content-Disposition if given and supported
         * @return FileRepoStatus
         */
-       final public function quickImport( $src, $dst ) {
-               return $this->quickImportBatch( array( array( $src, $dst ) ) );
+       final public function quickImport( $src, $dst, $disposition = null ) {
+               return $this->quickImportBatch( array( array( $src, $dst, $disposition ) ) );
        }
 
        /**
@@ -879,19 +883,24 @@ class FileRepo {
         * This function can be used to write to otherwise read-only foreign repos.
         * This is intended for copying generated thumbnails into the repo.
         *
-        * @param $pairs Array List of tuples (file system path, virtual URL or storage path)
+        * All path parameters may be a file system path, storage path, or virtual URL.
+        * When "dispositions" are given they are used as Content-Disposition if supported.
+        *
+        * @param $triples Array List of (source path, destination path, disposition)
         * @return FileRepoStatus
         */
-       public function quickImportBatch( array $pairs ) {
+       public function quickImportBatch( array $triples ) {
                $status = $this->newGood();
                $operations = array();
-               foreach ( $pairs as $pair ) {
-                       list ( $src, $dst ) = $pair;
+               foreach ( $triples as $triple ) {
+                       list( $src, $dst ) = $triple;
+                       $src = $this->resolveToStoragePath( $src );
                        $dst = $this->resolveToStoragePath( $dst );
                        $operations[] = array(
-                               'op'        => 'store',
-                               'src'       => $src,
-                               'dst'       => $dst
+                               'op'          => FileBackend::isStoragePath( $src ) ? 'copy' : 'store',
+                               'src'         => $src,
+                               'dst'         => $dst,
+                               'disposition' => isset( $triple[2] ) ? $triple[2] : null
                        );
                        $status->merge( $this->initDirectory( dirname( $dst ) ) );
                }
@@ -936,19 +945,38 @@ class FileRepo {
        public function storeTemp( $originalName, $srcPath ) {
                $this->assertWritableRepo(); // fail out if read-only
 
-               $date      = gmdate( "YmdHis" );
-               $hashPath  = $this->getHashPath( $originalName );
-               $dstRel    = "{$hashPath}{$date}!{$originalName}";
-               $dstUrlRel = $hashPath . $date . '!' . rawurlencode( $originalName );
+               $date       = gmdate( "YmdHis" );
+               $hashPath   = $this->getHashPath( $originalName );
+               $dstRel     = "{$hashPath}{$date}!{$originalName}";
+               $dstUrlRel  = $hashPath . $date . '!' . rawurlencode( $originalName );
+               $virtualUrl = $this->getVirtualUrl( 'temp' )  . '/' . $dstUrlRel;
 
-               $result = $this->store( $srcPath, 'temp', $dstRel, self::SKIP_LOCKING );
-               $result->value = $this->getVirtualUrl( 'temp' ) . '/' . $dstUrlRel;
+               $result = $this->quickImport( $srcPath, $virtualUrl );
+               $result->value = $virtualUrl;
 
                return $result;
        }
 
        /**
-        * Concatenate a list of files into a target file location.
+        * Remove a temporary file or mark it for garbage collection
+        *
+        * @param $virtualUrl String: the virtual URL returned by FileRepo::storeTemp()
+        * @return Boolean: true on success, false on failure
+        */
+       public function freeTemp( $virtualUrl ) {
+               $this->assertWritableRepo(); // fail out if read-only
+
+               $temp = $this->getVirtualUrl( 'temp' );
+               if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
+                       wfDebug( __METHOD__.": Invalid temp virtual URL\n" );
+                       return false;
+               }
+
+               return $this->quickPurge( $virtualUrl )->isOK();
+       }
+
+       /**
+        * Concatenate a list of temporary files into a target file location.
         *
         * @param $srcPaths Array Ordered list of source virtual URLs/storage paths
         * @param $dstPath String Target file system path
@@ -962,14 +990,10 @@ class FileRepo {
                $status = $this->newGood();
 
                $sources = array();
-               $deleteOperations = array(); // post-concatenate ops
                foreach ( $srcPaths as $srcPath ) {
                        // Resolve source to a storage path if virtual
                        $source = $this->resolveToStoragePath( $srcPath );
                        $sources[] = $source; // chunk to merge
-                       if ( $flags & self::DELETE_SOURCE ) {
-                               $deleteOperations[] = array( 'op' => 'delete', 'src' => $source );
-                       }
                }
 
                // Concatenate the chunks into one FS file
@@ -980,44 +1004,24 @@ class FileRepo {
                }
 
                // Delete the sources if required
-               if ( $deleteOperations ) {
-                       $opts = array( 'force' => true );
-                       $status->merge( $this->backend->doOperations( $deleteOperations, $opts ) );
+               if ( $flags & self::DELETE_SOURCE ) {
+                       $status->merge( $this->quickPurgeBatch( $srcPaths ) );
                }
 
-               // Make sure status is OK, despite any $deleteOperations fatals
+               // Make sure status is OK, despite any quickPurgeBatch() fatals
                $status->setResult( true );
 
                return $status;
        }
 
-       /**
-        * Remove a temporary file or mark it for garbage collection
-        *
-        * @param $virtualUrl String: the virtual URL returned by FileRepo::storeTemp()
-        * @return Boolean: true on success, false on failure
-        */
-       public function freeTemp( $virtualUrl ) {
-               $this->assertWritableRepo(); // fail out if read-only
-
-               $temp = "mwrepo://{$this->name}/temp";
-               if ( substr( $virtualUrl, 0, strlen( $temp ) ) != $temp ) {
-                       wfDebug( __METHOD__.": Invalid temp virtual URL\n" );
-                       return false;
-               }
-               $path = $this->resolveVirtualUrl( $virtualUrl );
-
-               return $this->cleanupBatch( array( $path ), self::SKIP_LOCKING )->isOK();
-       }
-
        /**
         * Copy or move a file either from a storage path, virtual URL,
-        * or FS path, into this repository at the specified destination location.
+        * or file system path, into this repository at the specified destination location.
         *
         * Returns a FileRepoStatus object. On success, the value contains "new" or
         * "archived", to indicate whether the file was new with that name.
         *
-        * @param $srcPath String: the source FS path, storage path, or URL
+        * @param $srcPath String: the source file system path, storage path, or URL
         * @param $dstRel String: the destination relative path
         * @param $archiveRel String: the relative path where the existing file is to
         *        be archived, if there is one. Relative to the public zone root.
@@ -1163,7 +1167,7 @@ class FileRepo {
                list( $b, $container, $r ) = FileBackend::splitStoragePath( $path );
 
                $params = array( 'dir' => $path );
-               if ( $container === $this->zones['deleted']['container'] ) {
+               if ( $this->isPrivate || $container === $this->zones['deleted']['container'] ) {
                        # Take all available measures to prevent web accessibility of new deleted
                        # directories, in case the user has not configured offline storage
                        $params = array( 'noAccess' => true, 'noListing' => true ) + $params;
@@ -1317,6 +1321,9 @@ class FileRepo {
         * @return string
         */
        public function getDeletedHashPath( $key ) {
+               if ( strlen( $key ) < 31 ) {
+                       throw new MWException( "Invalid storage key '$key'." );
+               }
                $path = '';
                for ( $i = 0; $i < $this->deletedHashLevels; $i++ ) {
                        $path .= $key[$i] . '/';
@@ -1388,18 +1395,14 @@ class FileRepo {
        }
 
        /**
-        * Get the sha1 of a file with a given virtual URL/storage path
+        * Get the sha1 (base 36) of a file with a given virtual URL/storage path
         *
         * @param $virtualUrl string
         * @return string|bool
         */
        public function getFileSha1( $virtualUrl ) {
                $path = $this->resolveToStoragePath( $virtualUrl );
-               $tmpFile = $this->backend->getLocalReference( array( 'src' => $path ) );
-               if ( !$tmpFile ) {
-                       return false;
-               }
-               return $tmpFile->getSha1Base36();
+               return $this->backend->getFileSha1Base36( array( 'src' => $path ) );
        }
 
        /**
@@ -1559,6 +1562,21 @@ class FileRepo {
                return wfMessageFallback( 'shared-repo-name-' . $this->name, 'shared-repo' )->text();
        }
 
+       /**
+        * Get the portion of the file that contains the origin file name.
+        * If that name is too long, then the name "thumbnail.<ext>" will be given.
+        *
+        * @param $name string
+        * @return string
+        */
+       public function nameForThumb( $name ) {
+               if ( strlen( $name ) > $this->abbrvThreshold ) {
+                       $ext  = FileBackend::extensionFromPath( $name );
+                       $name = ( $ext == '' ) ? 'thumbnail' : "thumbnail.$ext";
+               }
+               return $name;
+       }
+
        /**
         * Returns true if this the local file repository.
         *