Merge "CologneBlue rewrite: kill mWatchLinkNum, watchThisPage() is only called once...
[lhc/web/wiklou.git] / includes / filerepo / FileRepo.php
index bef2642..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,6 +114,9 @@ class FileRepo {
                        ? $info['deletedHashLevels']
                        : $this->hashLevels;
                $this->transformVia404 = !empty( $info['transformVia404'] );
+               $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();
@@ -692,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:
@@ -837,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 ) ) );
        }
 
        /**
@@ -878,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 ) ) );
                }
@@ -1006,12 +1016,12 @@ class FileRepo {
 
        /**
         * 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.
@@ -1311,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] . '/';
@@ -1549,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.
         *