Merge "[FileBackend] Added getFileContentsMulti() and improved it for Swift."
[lhc/web/wiklou.git] / includes / filebackend / FileBackend.php
index e59a13b..561ea33 100644 (file)
@@ -50,7 +50,7 @@
  * backends with virtual directories may not have this limitation. Callers should
  * store files in such a way that no files and directories are under the same path.
  *
- * Methods should avoid throwing exceptions at all costs.
+ * Methods of subclasses should avoid throwing exceptions at all costs.
  * As a corollary, external dependencies should be kept to a minimum.
  *
  * @ingroup FileBackend
@@ -128,6 +128,17 @@ abstract class FileBackend {
                return $this->name;
        }
 
+       /**
+        * Get the wiki identifier used for this backend (possibly empty).
+        * Note that this might *not* be in the same format as wfWikiID().
+        *
+        * @return string
+        * @since 1.20
+        */
+       final public function getWikiId() {
+               return $this->wikiId;
+       }
+
        /**
         * Check if this backend is read-only
         *
@@ -249,13 +260,13 @@ abstract class FileBackend {
         *   - allowStale          : Don't require the latest available data.
         *                           This can increase performance for non-critical writes.
         *                           This has no effect unless the 'force' flag is set.
-        *   - preserveCache       : Don't clear the process cache before checking files.
-        *                           This should only be used if all entries in the process
-        *                           cache were added after the files were already locked.
         *   - nonJournaled        : Don't log this operation batch in the file journal.
         *                           This limits the ability of recovery scripts.
         *   - parallelize         : Try to do operations in parallel when possible.
         *   - bypassReadOnly      : Allow writes in read-only mode (since 1.20).
+        *   - preserveCache       : Don't clear the process cache before checking files.
+        *                           This should only be used if all entries in the process
+        *                           cache were added after the files were already locked (since 1.20).
         *
         * @remarks Remarks on locking:
         * File system paths given to operations should refer to files that are
@@ -284,16 +295,6 @@ abstract class FileBackend {
                        unset( $opts['nonLocking'] );
                        unset( $opts['allowStale'] );
                }
-               $opts['concurrency'] = 1; // off
-               if ( $this->parallelize === 'implicit' ) {
-                       if ( !isset( $opts['parallelize'] ) || $opts['parallelize'] ) {
-                               $opts['concurrency'] = $this->concurrency;
-                       }
-               } elseif ( $this->parallelize === 'explicit' ) {
-                       if ( !empty( $opts['parallelize'] ) ) {
-                               $opts['concurrency'] = $this->concurrency;
-                       }
-               }
                return $this->doOperationsInternal( $ops, $opts );
        }
 
@@ -676,7 +677,7 @@ abstract class FileBackend {
        /**
         * Delete a storage directory if it is empty.
         * Backends using key/value stores may do nothing unless the directory
-        * is that of an empty container, in which case it should be deleted.
+        * is that of an empty container, in which case it will be deleted.
         *
         * @param $params Array
         * $params include:
@@ -730,7 +731,29 @@ abstract class FileBackend {
         *   - latest : use the latest available data
         * @return string|bool Returns false on failure
         */
-       abstract public function getFileContents( array $params );
+       final public function getFileContents( array $params ) {
+               $contents = $this->getFileContentsMulti(
+                       array( 'srcs' => array( $params['src'] ) ) + $params );
+
+               return $contents[$params['src']];
+       }
+
+       /**
+        * Like getFileContents() except it takes an array of storage paths
+        * and returns a map of storage paths to strings (or null on failure).
+        * The map keys (paths) are in the same order as the provided list of paths.
+        *
+        * @see FileBackend::getFileContents()
+        *
+        * @param $params Array
+        * $params include:
+        *   - srcs        : list of source storage paths
+        *   - latest      : use the latest available data
+        *   - parallelize : try to do operations in parallel when possible
+        * @return Array Map of (path name => string or false on failure)
+        * @since 1.20
+        */
+       abstract public function getFileContentsMulti( array $params );
 
        /**
         * Get the size (bytes) of a file at a storage path in the backend.
@@ -784,15 +807,15 @@ abstract class FileBackend {
 
        /**
         * Stream the file at a storage path in the backend.
-        * If the file does not exists, a 404 error will be given.
+        * If the file does not exists, an HTTP 404 error will be given.
         * Appropriate HTTP headers (Status, Content-Type, Content-Length)
-        * must be sent if streaming began, while none should be sent otherwise.
+        * will be sent if streaming began, while none will be sent otherwise.
         * Implementations should flush the output buffer before sending data.
         *
         * @param $params Array
         * $params include:
         *   - src     : source storage path
-        *   - headers : additional HTTP headers to send on success
+        *   - headers : list of additional HTTP headers to send on success
         *   - latest  : use the latest available data
         * @return Status
         */
@@ -817,7 +840,29 @@ abstract class FileBackend {
         *   - latest : use the latest available data
         * @return FSFile|null Returns null on failure
         */
-       abstract public function getLocalReference( array $params );
+       final public function getLocalReference( array $params ) {
+               $fsFiles = $this->getLocalReferenceMulti(
+                       array( 'srcs' => array( $params['src'] ) ) + $params );
+
+               return $fsFiles[$params['src']];
+       }
+
+       /**
+        * Like getLocalReference() except it takes an array of storage paths
+        * and returns a map of storage paths to FSFile objects (or null on failure).
+        * The map keys (paths) are in the same order as the provided list of paths.
+        *
+        * @see FileBackend::getLocalReference()
+        *
+        * @param $params Array
+        * $params include:
+        *   - srcs        : list of source storage paths
+        *   - latest      : use the latest available data
+        *   - parallelize : try to do operations in parallel when possible
+        * @return Array Map of (path name => FSFile or null on failure)
+        * @since 1.20
+        */
+       abstract public function getLocalReferenceMulti( array $params );
 
        /**
         * Get a local copy on disk of the file at a storage path in the backend.
@@ -830,7 +875,29 @@ abstract class FileBackend {
         *   - latest : use the latest available data
         * @return TempFSFile|null Returns null on failure
         */
-       abstract public function getLocalCopy( array $params );
+       final public function getLocalCopy( array $params ) {
+               $tmpFiles = $this->getLocalCopyMulti(
+                       array( 'srcs' => array( $params['src'] ) ) + $params );
+
+               return $tmpFiles[$params['src']];
+       }
+
+       /**
+        * Like getLocalCopy() except it takes an array of storage paths and
+        * returns a map of storage paths to TempFSFile objects (or null on failure).
+        * The map keys (paths) are in the same order as the provided list of paths.
+        *
+        * @see FileBackend::getLocalCopy()
+        *
+        * @param $params Array
+        * $params include:
+        *   - srcs        : list of source storage paths
+        *   - latest      : use the latest available data
+        *   - parallelize : try to do operations in parallel when possible
+        * @return Array Map of (path name => TempFSFile or null on failure)
+        * @since 1.20
+        */
+       abstract public function getLocalCopyMulti( array $params );
 
        /**
         * Check if a directory exists at a given storage path.
@@ -850,10 +917,10 @@ abstract class FileBackend {
        /**
         * Get an iterator to list *all* directories under a storage directory.
         * If the directory is of the form "mwstore://backend/container",
-        * then all directories in the container should be listed.
+        * then all directories in the container will be listed.
         * If the directory is of form "mwstore://backend/container/dir",
-        * then all directories directly under that directory should be listed.
-        * Results should be storage directories relative to the given directory.
+        * then all directories directly under that directory will be listed.
+        * Results will be storage directories relative to the given directory.
         *
         * Storage backends with eventual consistency might return stale data.
         *
@@ -885,10 +952,10 @@ abstract class FileBackend {
        /**
         * Get an iterator to list *all* stored files under a storage directory.
         * If the directory is of the form "mwstore://backend/container",
-        * then all files in the container should be listed.
+        * then all files in the container will be listed.
         * If the directory is of form "mwstore://backend/container/dir",
-        * then all files under that directory should be listed.
-        * Results should be storage paths relative to the given directory.
+        * then all files under that directory will be listed.
+        * Results will be storage paths relative to the given directory.
         *
         * Storage backends with eventual consistency might return stale data.
         *
@@ -1105,17 +1172,27 @@ abstract class FileBackend {
        }
 
        /**
-        * Build a Content-Disposition header value per RFC 6266
+        * Build a Content-Disposition header value per RFC 6266.
         *
         * @param $type string One of (attachment, inline)
         * @param $filename string Suggested file name (should not contain slashes)
         * @return string
         * @since 1.20
         */
-       final public static function makeContentDisposition( $type, $filename ) {
+       final public static function makeContentDisposition( $type, $filename = '' ) {
+               $parts = array();
+
                $type = strtolower( $type );
-               $type = in_array( $type, array( 'inline', 'attachment' ) ) ? $type : 'inline';
-               return "$type; filename*=UTF-8''" . rawurlencode( basename( $filename ) );
+               if ( !in_array( $type, array( 'inline', 'attachment' ) ) ) {
+                       throw new MWException( "Invalid Content-Disposition type '$type'." );
+               }
+               $parts[] = $type;
+
+               if ( strlen( $filename ) ) {
+                       $parts[] = "filename*=UTF-8''" . rawurlencode( basename( $filename ) );
+               }
+
+               return implode( ';', $parts );
        }
 
        /**