Merge "else if -> elseif"
[lhc/web/wiklou.git] / includes / filebackend / FileBackendStore.php
index 3f1d185..8b280e0 100644 (file)
@@ -92,7 +92,6 @@ abstract class FileBackendStore extends FileBackend {
         * $params include:
         *   - content     : the raw file contents
         *   - dst         : destination storage path
-        *   - disposition : Content-Disposition header value for the destination
         *   - headers     : HTTP header name/value map
         *   - async       : Status will be returned immediately if supported.
         *                   If the status is OK, then its value field will be
@@ -135,7 +134,6 @@ abstract class FileBackendStore extends FileBackend {
         * $params include:
         *   - src         : source path on disk
         *   - dst         : destination storage path
-        *   - disposition : Content-Disposition header value for the destination
         *   - headers     : HTTP header name/value map
         *   - async       : Status will be returned immediately if supported.
         *                   If the status is OK, then its value field will be
@@ -179,7 +177,7 @@ abstract class FileBackendStore extends FileBackend {
         *   - src                 : source storage path
         *   - dst                 : destination storage path
         *   - ignoreMissingSource : do nothing if the source file does not exist
-        *   - disposition         : Content-Disposition header value for the destination
+        *   - headers             : HTTP header name/value map
         *   - async               : Status will be returned immediately if supported.
         *                           If the status is OK, then its value field will be
         *                           set to a FileBackendStoreOpHandle object.
@@ -248,7 +246,7 @@ abstract class FileBackendStore extends FileBackend {
         *   - src                 : source storage path
         *   - dst                 : destination storage path
         *   - ignoreMissingSource : do nothing if the source file does not exist
-        *   - disposition         : Content-Disposition header value for the destination
+        *   - headers             : HTTP header name/value map
         *   - async               : Status will be returned immediately if supported.
         *                           If the status is OK, then its value field will be
         *                           set to a FileBackendStoreOpHandle object.
@@ -278,10 +276,12 @@ abstract class FileBackendStore extends FileBackend {
         */
        protected function doMoveInternal( array $params ) {
                unset( $params['async'] ); // two steps, won't work here :)
+               $nsrc = FileBackend::normalizeStoragePath( $params['src'] );
+               $ndst = FileBackend::normalizeStoragePath( $params['dst'] );
                // Copy source to dest
                $status = $this->copyInternal( $params );
-               if ( $status->isOK() ) {
-                       // Delete source (only fails due to races or medium going down)
+               if ( $nsrc !== $ndst && $status->isOK() ) {
+                       // Delete source (only fails due to races or network problems)
                        $status->merge( $this->deleteInternal( array( 'src' => $params['src'] ) ) );
                        $status->setResult( true, $status->value ); // ignore delete() errors
                }
@@ -294,7 +294,6 @@ abstract class FileBackendStore extends FileBackend {
         *
         * $params include:
         *   - src           : source storage path
-        *   - disposition   : Content-Disposition header value for the destination
         *   - headers       : HTTP header name/value map
         *   - async         : Status will be returned immediately if supported.
         *                     If the status is OK, then its value field will be
@@ -306,9 +305,13 @@ abstract class FileBackendStore extends FileBackend {
        final public function describeInternal( array $params ) {
                wfProfileIn( __METHOD__ );
                wfProfileIn( __METHOD__ . '-' . $this->name );
-               $status = $this->doDescribeInternal( $params );
-               $this->clearCache( array( $params['src'] ) );
-               $this->deleteFileCache( $params['src'] ); // persistent cache
+               if ( count( $params['headers'] ) ) {
+                       $status = $this->doDescribeInternal( $params );
+                       $this->clearCache( array( $params['src'] ) );
+                       $this->deleteFileCache( $params['src'] ); // persistent cache
+               } else {
+                       $status = Status::newGood(); // nothing to do
+               }
                wfProfileOut( __METHOD__ . '-' . $this->name );
                wfProfileOut( __METHOD__ );
                return $status;
@@ -1249,8 +1252,12 @@ abstract class FileBackendStore extends FileBackend {
                wfProfileIn( __METHOD__ . '-' . $this->name );
                foreach ( $fileOpHandles as $fileOpHandle ) {
                        if ( !( $fileOpHandle instanceof FileBackendStoreOpHandle ) ) {
+                               wfProfileOut( __METHOD__ . '-' . $this->name );
+                               wfProfileOut( __METHOD__ );
                                throw new MWException( "Given a non-FileBackendStoreOpHandle object." );
                        } elseif ( $fileOpHandle->backend->getName() !== $this->getName() ) {
+                               wfProfileOut( __METHOD__ . '-' . $this->name );
+                               wfProfileOut( __METHOD__ );
                                throw new MWException( "Given a FileBackendStoreOpHandle for the wrong backend." );
                        }
                }
@@ -1277,15 +1284,20 @@ abstract class FileBackendStore extends FileBackend {
        }
 
        /**
-        * Strip long HTTP headers from a file operation
+        * Strip long HTTP headers from a file operation.
+        * Most headers are just numbers, but some are allowed to be long.
+        * This function is useful for cleaning up headers and avoiding backend
+        * specific errors, especially in the middle of batch file operations.
         *
         * @param array $op Same format as doOperation()
         * @return Array
         */
        protected function stripInvalidHeadersFromOp( array $op ) {
-               if ( isset( $op['headers'] ) ) {
+               static $longs = array( 'Content-Disposition' );
+               if ( isset( $op['headers'] ) ) { // op sets HTTP headers
                        foreach ( $op['headers'] as $name => $value ) {
-                               if ( strlen( $name ) > 255 || strlen( $value ) > 255 ) {
+                               $maxHVLen = in_array( $name, $longs ) ? INF : 255;
+                               if ( strlen( $name ) > 255 || strlen( $value ) > $maxHVLen ) {
                                        trigger_error( "Header '$name: $value' is too long." );
                                        unset( $op['headers'][$name] );
                                } elseif ( !strlen( $value ) ) {
@@ -1566,7 +1578,7 @@ abstract class FileBackendStore extends FileBackend {
         * @param $val mixed Information to cache
         */
        final protected function setContainerCache( $container, $val ) {
-               $this->memCache->add( $this->containerCacheKey( $container ), $val, 14*86400 );
+               $this->memCache->add( $this->containerCacheKey( $container ), $val, 14 * 86400 );
        }
 
        /**
@@ -1661,7 +1673,7 @@ abstract class FileBackendStore extends FileBackend {
                if ( $path === null ) {
                        return; // invalid storage path
                }
-               $this->memCache->add( $this->fileCacheKey( $path ), $val, 7*86400 );
+               $this->memCache->add( $this->fileCacheKey( $path ), $val, 7 * 86400 );
        }
 
        /**
@@ -1786,21 +1798,15 @@ abstract class FileBackendStoreOpHandle {
  *
  * @ingroup FileBackend
  */
-abstract class FileBackendStoreShardListIterator implements Iterator {
+abstract class FileBackendStoreShardListIterator extends FilterIterator {
        /** @var FileBackendStore */
        protected $backend;
        /** @var Array */
        protected $params;
-       /** @var Array */
-       protected $shardSuffixes;
+
        protected $container; // string; full container name
        protected $directory; // string; resolved relative path
 
-       /** @var Traversable */
-       protected $iter;
-       protected $curShard = 0; // integer
-       protected $pos = 0; // integer
-
        /** @var Array */
        protected $multiShardPaths = array(); // (rel path => 1)
 
@@ -1817,142 +1823,56 @@ abstract class FileBackendStoreShardListIterator implements Iterator {
                $this->backend = $backend;
                $this->container = $container;
                $this->directory = $dir;
-               $this->shardSuffixes = $suffixes;
                $this->params = $params;
-       }
 
-       /**
-        * @see Iterator::key()
-        * @return integer
-        */
-       public function key() {
-               return $this->pos;
-       }
-
-       /**
-        * @see Iterator::valid()
-        * @return bool
-        */
-       public function valid() {
-               if ( $this->iter instanceof Iterator ) {
-                       return $this->iter->valid();
-               } elseif ( is_array( $this->iter ) ) {
-                       return ( current( $this->iter ) !== false ); // no paths can have this value
+               $iter = new AppendIterator();
+               foreach ( $suffixes as $suffix ) {
+                       $iter->append( $this->listFromShard( $this->container . $suffix ) );
                }
-               return false; // some failure?
-       }
 
-       /**
-        * @see Iterator::current()
-        * @return string|bool String or false
-        */
-       public function current() {
-               return ( $this->iter instanceof Iterator )
-                       ? $this->iter->current()
-                       : current( $this->iter );
+               parent::__construct( $iter );
        }
 
-       /**
-        * @see Iterator::next()
-        * @return void
-        */
-       public function next() {
-               ++$this->pos;
-               ( $this->iter instanceof Iterator ) ? $this->iter->next() : next( $this->iter );
-               do {
-                       $continue = false; // keep scanning shards?
-                       $this->filterViaNext(); // filter out duplicates
-                       // Find the next non-empty shard if no elements are left
-                       if ( !$this->valid() ) {
-                               $this->nextShardIteratorIfNotValid();
-                               $continue = $this->valid(); // re-filter unless we ran out of shards
-                       }
-               } while ( $continue );
-       }
-
-       /**
-        * @see Iterator::rewind()
-        * @return void
-        */
-       public function rewind() {
-               $this->pos = 0;
-               $this->curShard = 0;
-               $this->setIteratorFromCurrentShard();
-               do {
-                       $continue = false; // keep scanning shards?
-                       $this->filterViaNext(); // filter out duplicates
-                       // Find the next non-empty shard if no elements are left
-                       if ( !$this->valid() ) {
-                               $this->nextShardIteratorIfNotValid();
-                               $continue = $this->valid(); // re-filter unless we ran out of shards
-                       }
-               } while ( $continue );
-       }
-
-       /**
-        * Filter out duplicate items by advancing to the next ones
-        */
-       protected function filterViaNext() {
-               while ( $this->valid() ) {
-                       $rel = $this->iter->current(); // path relative to given directory
-                       $path = $this->params['dir'] . "/{$rel}"; // full storage path
-                       if ( $this->backend->isSingleShardPathInternal( $path ) ) {
-                               break; // path is only on one shard; no issue with duplicates
-                       } elseif ( isset( $this->multiShardPaths[$rel] ) ) {
-                               // Don't keep listing paths that are on multiple shards
-                               ( $this->iter instanceof Iterator ) ? $this->iter->next() : next( $this->iter );
-                       } else {
-                               $this->multiShardPaths[$rel] = 1;
-                               break;
-                       }
-               }
-       }
-
-       /**
-        * If the list iterator for this container shard is out of items,
-        * then move on to the next container that has items.
-        * If there are none, then it advances to the last container.
-        */
-       protected function nextShardIteratorIfNotValid() {
-               while ( !$this->valid() && ++$this->curShard < count( $this->shardSuffixes ) ) {
-                       $this->setIteratorFromCurrentShard();
+       public function accept() {
+               $rel = $this->getInnerIterator()->current(); // path relative to given directory
+               $path = $this->params['dir'] . "/{$rel}"; // full storage path
+               if ( $this->backend->isSingleShardPathInternal( $path ) ) {
+                       return true; // path is only on one shard; no issue with duplicates
+               } elseif ( isset( $this->multiShardPaths[$rel] ) ) {
+                       // Don't keep listing paths that are on multiple shards
+                       return false;
+               } else {
+                       $this->multiShardPaths[$rel] = 1;
+                       return true;
                }
        }
 
-       /**
-        * Set the list iterator to that of the current container shard
-        */
-       protected function setIteratorFromCurrentShard() {
-               $this->iter = $this->listFromShard(
-                       $this->container . $this->shardSuffixes[$this->curShard],
-                       $this->directory, $this->params );
-               // Start loading results so that current() works
-               if ( $this->iter ) {
-                       ( $this->iter instanceof Iterator ) ? $this->iter->rewind() : reset( $this->iter );
-               }
+       public function rewind() {
+               parent::rewind();
+               $this->multiShardPaths = array();
        }
 
        /**
         * Get the list for a given container shard
         *
         * @param string $container Resolved container name
-        * @param string $dir Resolved path relative to container
-        * @param array $params
-        * @return Traversable|Array|null
+        * @return Iterator
         */
-       abstract protected function listFromShard( $container, $dir, array $params );
+       abstract protected function listFromShard( $container );
 }
 
 /**
  * Iterator for listing directories
  */
 class FileBackendStoreShardDirIterator extends FileBackendStoreShardListIterator {
-       /**
-        * @see FileBackendStoreShardListIterator::listFromShard()
-        * @return Array|null|Traversable
-        */
-       protected function listFromShard( $container, $dir, array $params ) {
-               return $this->backend->getDirectoryListInternal( $container, $dir, $params );
+       protected function listFromShard( $container ) {
+               $list = $this->backend->getDirectoryListInternal(
+                       $container, $this->directory, $this->params );
+               if ( $list === null ) {
+                       return new ArrayIterator( array() );
+               } else {
+                       return is_array( $list ) ? new ArrayIterator( $list ) : $list;
+               }
        }
 }
 
@@ -1960,11 +1880,13 @@ class FileBackendStoreShardDirIterator extends FileBackendStoreShardListIterator
  * Iterator for listing regular files
  */
 class FileBackendStoreShardFileIterator extends FileBackendStoreShardListIterator {
-       /**
-        * @see FileBackendStoreShardListIterator::listFromShard()
-        * @return Array|null|Traversable
-        */
-       protected function listFromShard( $container, $dir, array $params ) {
-               return $this->backend->getFileListInternal( $container, $dir, $params );
+       protected function listFromShard( $container ) {
+               $list = $this->backend->getFileListInternal(
+                       $container, $this->directory, $this->params );
+               if ( $list === null ) {
+                       return new ArrayIterator( array() );
+               } else {
+                       return is_array( $list ) ? new ArrayIterator( $list ) : $list;
+               }
        }
 }