Merge "[FileBackend] Use the new CloudFiles metadata functions."
authorDemon <chadh@wikimedia.org>
Tue, 30 Oct 2012 18:30:24 +0000 (18:30 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Tue, 30 Oct 2012 18:30:24 +0000 (18:30 +0000)
1  2 
includes/filebackend/SwiftFileBackend.php

@@@ -201,6 -201,12 +201,6 @@@ class SwiftFileBackend extends FileBack
                // (a) Check the destination container and object
                try {
                        $dContObj = $this->getContainer( $dstCont );
 -                      if ( empty( $params['overwrite'] ) &&
 -                              $this->fileExists( array( 'src' => $params['dst'], 'latest' => 1 ) ) )
 -                      {
 -                              $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
 -                              return $status;
 -                      }
                } catch ( NoSuchContainerException $e ) {
                        $status->fatal( 'backend-fail-create', $params['dst'] );
                        return $status;
                        // Create a fresh CF_Object with no fields preloaded.
                        // We don't want to preserve headers, metadata, and such.
                        $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
-                       // Note: metadata keys stored as [Upper case char][[Lower case char]...]
-                       $obj->metadata = array( 'Sha1base36' => $sha1Hash );
+                       $obj->setMetadataValues( array( 'Sha1base36' => $sha1Hash ) );
                        // Manually set the ETag (https://github.com/rackspace/php-cloudfiles/issues/59).
                        // The MD5 here will be checked within Swift against its own MD5.
                        $obj->set_etag( md5( $params['content'] ) );
                        if ( !empty( $params['async'] ) ) { // deferred
                                $op = $obj->write_async( $params['content'] );
                                $status->value = new SwiftFileOpHandle( $this, $params, 'Create', $op );
 -                              if ( !empty( $params['overwrite'] ) ) { // file possibly mutated
 -                                      $status->value->affectedObjects[] = $obj;
 -                              }
 +                              $status->value->affectedObjects[] = $obj;
                        } else { // actually write the object in Swift
                                $obj->write( $params['content'] );
 -                              if ( !empty( $params['overwrite'] ) ) { // file possibly mutated
 -                                      $this->purgeCDNCache( array( $obj ) );
 -                              }
 +                              $this->purgeCDNCache( array( $obj ) );
                        }
                } catch ( CDNNotEnabledException $e ) {
                        // CDN not enabled; nothing to see here
                // (a) Check the destination container and object
                try {
                        $dContObj = $this->getContainer( $dstCont );
 -                      if ( empty( $params['overwrite'] ) &&
 -                              $this->fileExists( array( 'src' => $params['dst'], 'latest' => 1 ) ) )
 -                      {
 -                              $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
 -                              return $status;
 -                      }
                } catch ( NoSuchContainerException $e ) {
                        $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
                        return $status;
                        // Create a fresh CF_Object with no fields preloaded.
                        // We don't want to preserve headers, metadata, and such.
                        $obj = new CF_Object( $dContObj, $dstRel, false, false ); // skip HEAD
-                       // Note: metadata keys stored as [Upper case char][[Lower case char]...]
-                       $obj->metadata = array( 'Sha1base36' => $sha1Hash );
+                       $obj->setMetadataValues( array( 'Sha1base36' => $sha1Hash ) );
                        // The MD5 here will be checked within Swift against its own MD5.
                        $obj->set_etag( md5_file( $params['src'] ) );
                        // Use the same content type as StreamFile for security
                                        $op = $obj->write_async( $fp, filesize( $params['src'] ), true );
                                        $status->value = new SwiftFileOpHandle( $this, $params, 'Store', $op );
                                        $status->value->resourcesToClose[] = $fp;
 -                                      if ( !empty( $params['overwrite'] ) ) { // file possibly mutated
 -                                              $status->value->affectedObjects[] = $obj;
 -                                      }
 +                                      $status->value->affectedObjects[] = $obj;
                                }
                        } else { // actually write the object in Swift
                                $obj->load_from_filename( $params['src'], true ); // calls $obj->write()
 -                              if ( !empty( $params['overwrite'] ) ) { // file possibly mutated
 -                                      $this->purgeCDNCache( array( $obj ) );
 -                              }
 +                              $this->purgeCDNCache( array( $obj ) );
                        }
                } catch ( CDNNotEnabledException $e ) {
                        // CDN not enabled; nothing to see here
                try {
                        $sContObj = $this->getContainer( $srcCont );
                        $dContObj = $this->getContainer( $dstCont );
 -                      if ( empty( $params['overwrite'] ) &&
 -                              $this->fileExists( array( 'src' => $params['dst'], 'latest' => 1 ) ) )
 -                      {
 -                              $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
 -                              return $status;
 -                      }
                } catch ( NoSuchContainerException $e ) {
                        $status->fatal( 'backend-fail-copy', $params['src'], $params['dst'] );
                        return $status;
                        if ( !empty( $params['async'] ) ) { // deferred
                                $op = $sContObj->copy_object_to_async( $srcRel, $dContObj, $dstRel, null, $hdrs );
                                $status->value = new SwiftFileOpHandle( $this, $params, 'Copy', $op );
 -                              if ( !empty( $params['overwrite'] ) ) { // file possibly mutated
 -                                      $status->value->affectedObjects[] = $dstObj;
 -                              }
 +                              $status->value->affectedObjects[] = $dstObj;
                        } else { // actually write the object in Swift
                                $sContObj->copy_object_to( $srcRel, $dContObj, $dstRel, null, $hdrs );
 -                              if ( !empty( $params['overwrite'] ) ) { // file possibly mutated
 -                                      $this->purgeCDNCache( array( $dstObj ) );
 -                              }
 +                              $this->purgeCDNCache( array( $dstObj ) );
                        }
                } catch ( CDNNotEnabledException $e ) {
                        // CDN not enabled; nothing to see here
                try {
                        $sContObj = $this->getContainer( $srcCont );
                        $dContObj = $this->getContainer( $dstCont );
 -                      if ( empty( $params['overwrite'] ) &&
 -                              $this->fileExists( array( 'src' => $params['dst'], 'latest' => 1 ) ) )
 -                      {
 -                              $status->fatal( 'backend-fail-alreadyexists', $params['dst'] );
 -                              return $status;
 -                      }
                } catch ( NoSuchContainerException $e ) {
                        $status->fatal( 'backend-fail-move', $params['src'], $params['dst'] );
                        return $status;
                                $op = $sContObj->move_object_to_async( $srcRel, $dContObj, $dstRel, null, $hdrs );
                                $status->value = new SwiftFileOpHandle( $this, $params, 'Move', $op );
                                $status->value->affectedObjects[] = $srcObj;
 -                              if ( !empty( $params['overwrite'] ) ) { // file possibly mutated
 -                                      $status->value->affectedObjects[] = $dstObj;
 -                              }
 +                              $status->value->affectedObjects[] = $dstObj;
                        } else { // actually write the object in Swift
                                $sContObj->move_object_to( $srcRel, $dContObj, $dstRel, null, $hdrs );
                                $this->purgeCDNCache( array( $srcObj ) );
 -                              if ( !empty( $params['overwrite'] ) ) { // file possibly mutated
 -                                      $this->purgeCDNCache( array( $dstObj ) );
 -                              }
 +                              $this->purgeCDNCache( array( $dstObj ) );
                        }
                } catch ( CDNNotEnabledException $e ) {
                        // CDN not enabled; nothing to see here
                                // Convert dates like "Tue, 03 Jan 2012 22:01:04 GMT" to TS_MW
                                'mtime' => wfTimestamp( TS_MW, $srcObj->last_modified ),
                                'size'  => (int)$srcObj->content_length,
-                               'sha1'  => $srcObj->metadata['Sha1base36']
+                               'sha1'  => $srcObj->getMetadataValue( 'Sha1base36' )
                        );
                } catch ( NoSuchContainerException $e ) {
                } catch ( NoSuchObjectException $e ) {
         * @throws Exception cloudfiles exceptions
         */
        protected function addMissingMetadata( CF_Object $obj, $path ) {
-               if ( isset( $obj->metadata['Sha1base36'] ) ) {
+               if ( $obj->getMetadataValue( 'Sha1base36' ) !== null ) {
                        return true; // nothing to do
                }
                wfProfileIn( __METHOD__ );
                        if ( $tmpFile ) {
                                $hash = $tmpFile->getSha1Base36();
                                if ( $hash !== false ) {
-                                       $obj->metadata['Sha1base36'] = $hash;
+                                       $obj->setMetadataValues( array( 'Sha1base36' => $hash ) );
                                        $obj->sync_metadata(); // save to Swift
                                        wfProfileOut( __METHOD__ );
                                        return true; // success
                                }
                        }
                }
-               $obj->metadata['Sha1base36'] = false;
+               $obj->setMetadataValues( array( 'Sha1base36' => false ) );
                wfProfileOut( __METHOD__ );
                return false; // failed
        }