From: Aaron Date: Mon, 16 Apr 2012 23:51:55 +0000 (-0700) Subject: [FileRepo] Split out store/purge functions for thumbnails and made them skip file... X-Git-Tag: 1.31.0-rc.0~23896^2 X-Git-Url: http://git.cyclocoop.org/%24dirpuce/puce%24spip_lang_rtl.gif?a=commitdiff_plain;h=ffb1b38ad83927606c539ac941e9f3eb2653a840;p=lhc%2Fweb%2Fwiklou.git [FileRepo] Split out store/purge functions for thumbnails and made them skip file journals. * Added quickImport()/quickPurge() functions to store and delete files as fast as possible. * Also added a cleanDir() function to avoid having File directly use FileBackend functions. Change-Id: I9a19862f1a720c5a464bd37f79b58a505c5961f9 --- diff --git a/includes/filerepo/FileRepo.php b/includes/filerepo/FileRepo.php index dcc9610944..8fe786ab3f 100644 --- a/includes/filerepo/FileRepo.php +++ b/includes/filerepo/FileRepo.php @@ -783,6 +783,86 @@ class FileRepo { return $status; } + /** + * Import a file from the local file system into the repo. + * This does no locking nor journaling and overrides existing files. + * This is intended for copying generated thumbnails into the repo. + * + * @param $src string File system path + * @param $dst string Virtual URL or storage path + * @return FileRepoStatus + */ + final public function quickImport( $src, $dst ) { + return $this->quickImportBatch( array( array( $src, $dst ) ) ); + } + + /** + * Purge a file from the repo. This does no locking nor journaling. + * This is intended for purging thumbnail. + * + * @param $path string Virtual URL or storage path + * @return FileRepoStatus + */ + final public function quickPurge( $path ) { + return $this->quickPurgeBatch( array( $path ) ); + } + + /** + * Import a batch of files from the local file system into the repo. + * This does no locking nor journaling and overrides existing files. + * This is intended for copying generated thumbnails into the repo. + * + * @param $src Array List of tuples (file system path, virtual URL or storage path) + * @return FileRepoStatus + */ + public function quickImportBatch( array $pairs ) { + $this->assertWritableRepo(); // fail out if read-only + + $status = $this->newGood(); + $operations = array(); + foreach ( $pairs as $pair ) { + list ( $src, $dst ) = $pair; + $operations[] = array( + 'op' => 'store', + 'src' => $src, + 'dst' => $this->resolveToStoragePath( $dst ), + 'overwrite' => true + ); + $this->backend->prepare( array( 'dir' => dirname( $dst ) ) ); + } + $status->merge( $this->backend->doOperations( $operations, + array( 'force' => 1, 'nonLocking' => 1, 'allowStale' => 1, 'nonJournaled' => 1 ) + ) ); + + return $status; + } + + /** + * Purge a batch of files from the repo. This does no locking nor journaling. + * This is intended for purging thumbnails. + * + * @param $path Array List of virtual URLs or storage paths + * @return FileRepoStatus + */ + public function quickPurgeBatch( array $paths ) { + $this->assertWritableRepo(); // fail out if read-only + + $status = $this->newGood(); + $operations = array(); + foreach ( $paths as $path ) { + $operations[] = array( + 'op' => 'delete', + 'src' => $this->resolveToStoragePath( $path ), + 'ignoreMissingSource' => true + ); + } + $status->merge( $this->backend->doOperations( $operations, + array( 'force' => 1, 'nonLocking' => 1, 'allowStale' => 1, 'nonJournaled' => 1 ) + ) ); + + return $status; + } + /** * Pick a random name in the temp zone and store a file to it. * Returns a FileRepoStatus object with the file Virtual URL in the value, @@ -927,7 +1007,7 @@ class FileRepo { foreach ( $triplets as $i => $triplet ) { list( $srcPath, $dstRel, $archiveRel ) = $triplet; // Resolve source to a storage path if virtual - if ( substr( $srcPath, 0, 9 ) == 'mwrepo://' ) { + if ( $this->isVirtualUrl( $srcPath ) ) { $srcPath = $this->resolveVirtualUrl( $srcPath ); } if ( !$this->validateFilename( $dstRel ) ) { @@ -1012,6 +1092,22 @@ class FileRepo { return $status; } + /** + * Deletes a directory if empty + * + * @param $dir string Virtual URL (or storage path) of directory to clean + * @return Status + */ + public function cleanDir( $dir ) { + $this->assertWritableRepo(); // fail out if read-only + + $status = $this->newGood(); + $status->merge( $this->backend->clean( + array( 'dir' => $this->resolveToStoragePath( $dir ) ) ) ); + + return $status; + } + /** * Checks existence of a a file * diff --git a/includes/filerepo/file/File.php b/includes/filerepo/file/File.php index ce69a03a54..de9efb5afb 100644 --- a/includes/filerepo/file/File.php +++ b/includes/filerepo/file/File.php @@ -865,13 +865,8 @@ abstract class File { } } elseif ( $this->repo && $thumb->hasFile() && !$thumb->fileIsSource() ) { $backend = $this->repo->getBackend(); - // Copy the thumbnail from the file system into storage. This avoids using - // FileRepo::store(); getThumbPath() uses a different zone in some subclasses. - $backend->prepare( array( 'dir' => dirname( $thumbPath ) ) ); - $status = $backend->store( - array( 'src' => $tmpThumbPath, 'dst' => $thumbPath, 'overwrite' => 1 ), - array( 'force' => 1, 'nonLocking' => 1, 'allowStale' => 1 ) - ); + // Copy the thumbnail from the file system into storage... + $status = $this->repo->quickImport( $tmpThumbPath, $thumbPath ); if ( $status->isOK() ) { $thumb->setStoragePath( $thumbPath ); } else { diff --git a/includes/filerepo/file/ForeignAPIFile.php b/includes/filerepo/file/ForeignAPIFile.php index 16828fa650..9ae501a43d 100644 --- a/includes/filerepo/file/ForeignAPIFile.php +++ b/includes/filerepo/file/ForeignAPIFile.php @@ -247,8 +247,8 @@ class ForeignAPIFile extends File { } # Delete the thumbnails - $this->repo->cleanupBatch( $purgeList, FileRepo::SKIP_LOCKING ); + $this->repo->quickPurgeBatch( $purgeList ); # Clear out the thumbnail directory if empty - $this->repo->getBackend()->clean( array( 'dir' => $dir ) ); + $this->repo->cleanDir( $dir ); } } diff --git a/includes/filerepo/file/LocalFile.php b/includes/filerepo/file/LocalFile.php index 04d7a47aac..af1b7cd999 100644 --- a/includes/filerepo/file/LocalFile.php +++ b/includes/filerepo/file/LocalFile.php @@ -777,9 +777,9 @@ class LocalFile extends File { } # Delete the thumbnails - $this->repo->cleanupBatch( $purgeList, FileRepo::SKIP_LOCKING ); + $this->repo->quickPurgeBatch( $purgeList ); # Clear out the thumbnail directory if empty - $this->repo->getBackend()->clean( array( 'dir' => $dir ) ); + $this->repo->cleanDir( $dir ); } /** purgeDescription inherited */