diff options
author | Pierre Schmitz <pierre@archlinux.de> | 2013-01-18 16:46:04 +0100 |
---|---|---|
committer | Pierre Schmitz <pierre@archlinux.de> | 2013-01-18 16:46:04 +0100 |
commit | 63601400e476c6cf43d985f3e7b9864681695ed4 (patch) | |
tree | f7846203a952e38aaf66989d0a4702779f549962 /includes/filerepo/file/LocalFile.php | |
parent | 8ff01378c9e0207f9169b81966a51def645b6a51 (diff) |
Update to MediaWiki 1.20.2
this update includes:
* adjusted Arch Linux skin
* updated FluxBBAuthPlugin
* patch for https://bugzilla.wikimedia.org/show_bug.cgi?id=44024
Diffstat (limited to 'includes/filerepo/file/LocalFile.php')
-rw-r--r-- | includes/filerepo/file/LocalFile.php | 491 |
1 files changed, 354 insertions, 137 deletions
diff --git a/includes/filerepo/file/LocalFile.php b/includes/filerepo/file/LocalFile.php index 0f8b4754..695c4e9e 100644 --- a/includes/filerepo/file/LocalFile.php +++ b/includes/filerepo/file/LocalFile.php @@ -1,6 +1,21 @@ <?php /** - * Local file in the wiki's own database + * Local file in the wiki's own database. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html * * @file * @ingroup FileAbstraction @@ -29,6 +44,8 @@ define( 'MW_FILE_VERSION', 8 ); * @ingroup FileAbstraction */ class LocalFile extends File { + const CACHE_FIELD_MAX_LEN = 1000; + /**#@+ * @private */ @@ -58,6 +75,11 @@ class LocalFile extends File { /**#@-*/ + /** + * @var LocalRepo + */ + var $repo; + protected $repoClass = 'LocalRepo'; /** @@ -121,6 +143,7 @@ class LocalFile extends File { /** * Fields in the image table + * @return array */ static function selectFields() { return array( @@ -160,6 +183,7 @@ class LocalFile extends File { /** * Get the memcached key for the main data for this file, or false if * there is no access to the shared cache. + * @return bool */ function getCacheKey() { $hashedName = md5( $this->getName() ); @@ -169,6 +193,7 @@ class LocalFile extends File { /** * Try to load file metadata from memcached. Returns true on success. + * @return bool */ function loadFromCache() { global $wgMemc; @@ -238,6 +263,10 @@ class LocalFile extends File { $this->setProps( $props ); } + /** + * @param $prefix string + * @return array + */ function getCacheFields( $prefix = 'img_' ) { static $fields = array( 'size', 'width', 'height', 'bits', 'media_type', 'major_mime', 'minor_mime', 'metadata', 'timestamp', 'sha1', 'user', 'user_text', 'description' ); @@ -286,6 +315,10 @@ class LocalFile extends File { /** * Decode a row from the database (either object or array) to an array * with timestamps and MIME types decoded, and the field prefix removed. + * @param $row + * @param $prefix string + * @throws MWException + * @return array */ function decodeRow( $row, $prefix = 'img_' ) { $array = (array)$row; @@ -407,6 +440,7 @@ class LocalFile extends File { $dbw->update( 'image', array( + 'img_size' => $this->size, // sanity 'img_width' => $this->width, 'img_height' => $this->height, 'img_bits' => $this->bits, @@ -462,9 +496,12 @@ class LocalFile extends File { /** getPath inherited */ /** isVisible inhereted */ + /** + * @return bool + */ function isMissing() { if ( $this->missing === null ) { - list( $fileExists ) = $this->repo->fileExists( $this->getVirtualUrl(), FileRepo::FILES_ONLY ); + list( $fileExists ) = $this->repo->fileExists( $this->getVirtualUrl() ); $this->missing = !$fileExists; } return $this->missing; @@ -473,7 +510,8 @@ class LocalFile extends File { /** * Return the width of the image * - * Returns false on error + * @param $page int + * @return bool|int Returns false on error */ public function getWidth( $page = 1 ) { $this->load(); @@ -493,7 +531,8 @@ class LocalFile extends File { /** * Return the height of the image * - * Returns false on error + * @param $page int + * @return bool|int Returns false on error */ public function getHeight( $page = 1 ) { $this->load(); @@ -514,6 +553,7 @@ class LocalFile extends File { * Returns ID or name of user who uploaded the file * * @param $type string 'text' or 'id' + * @return int|string */ function getUser( $type = 'text' ) { $this->load(); @@ -527,12 +567,16 @@ class LocalFile extends File { /** * Get handler-specific metadata + * @return string */ function getMetadata() { $this->load(); return $this->metadata; } + /** + * @return int + */ function getBitDepth() { $this->load(); return $this->bits; @@ -540,6 +584,7 @@ class LocalFile extends File { /** * Return the size of the image file, in bytes + * @return int */ public function getSize() { $this->load(); @@ -548,6 +593,7 @@ class LocalFile extends File { /** * Returns the mime type of the file. + * @return string */ function getMimeType() { $this->load(); @@ -557,6 +603,7 @@ class LocalFile extends File { /** * Return the type of the media in the file. * Use the value returned by this function with the MEDIATYPE_xxx constants. + * @return string */ function getMediaType() { $this->load(); @@ -586,6 +633,9 @@ class LocalFile extends File { /** * Fix thumbnail files from 1.4 or before, with extreme prejudice + * @todo : do we still care about this? Perhaps a maintenance script + * can be made instead. Enabling this code results in a serious + * RTT regression for wikis without 404 handling. */ function migrateThumbFile( $thumbName ) { $thumbDir = $this->getThumbPath(); @@ -608,10 +658,12 @@ class LocalFile extends File { } */ - if ( $this->repo->fileExists( $thumbDir, FileRepo::FILES_ONLY ) ) { + /* + if ( $this->repo->fileExists( $thumbDir ) ) { // Delete file where directory should be $this->repo->cleanupBatch( array( $thumbDir ) ); } + */ } /** getHandler inherited */ @@ -620,12 +672,10 @@ class LocalFile extends File { /** * Get all thumbnail names previously generated for this file - * @param $archiveName string|false Name of an archive file + * @param $archiveName string|bool Name of an archive file, default false * @return array first element is the base dir, then files in that base dir. */ function getThumbnails( $archiveName = false ) { - $this->load(); - if ( $archiveName ) { $dir = $this->getArchiveThumbPath( $archiveName ); } else { @@ -690,6 +740,8 @@ class LocalFile extends File { */ function purgeOldThumbnails( $archiveName ) { global $wgUseSquid; + wfProfileIn( __METHOD__ ); + // Get a list of old thumbnails and URLs $files = $this->getThumbnails( $archiveName ); $dir = array_shift( $files ); @@ -706,6 +758,8 @@ class LocalFile extends File { } SquidUpdate::purge( $urls ); } + + wfProfileOut( __METHOD__ ); } /** @@ -713,6 +767,7 @@ class LocalFile extends File { */ function purgeThumbnails( $options = array() ) { global $wgUseSquid; + wfProfileIn( __METHOD__ ); // Delete thumbnails $files = $this->getThumbnails(); @@ -739,6 +794,8 @@ class LocalFile extends File { } SquidUpdate::purge( $urls ); } + + wfProfileOut( __METHOD__ ); } /** @@ -763,14 +820,21 @@ 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->quickCleanDir( $dir ); } /** purgeDescription inherited */ /** purgeEverything inherited */ + /** + * @param $limit null + * @param $start null + * @param $end null + * @param $inc bool + * @return array + */ function getHistory( $limit = null, $start = null, $end = null, $inc = true ) { $dbr = $this->repo->getSlaveDB(); $tables = array( 'oldimage' ); @@ -824,6 +888,7 @@ class LocalFile extends File { * 0 return line for current version * 1 query for old versions, return first one * 2, ... return next old version from above query + * @return bool */ public function nextHistoryLine() { # Polymorphic function name to distinguish foreign and local fetches @@ -888,21 +953,25 @@ class LocalFile extends File { * @param $comment String: upload description * @param $pageText String: text to use for the new description page, * if a new description page is created - * @param $flags Integer: flags for publish() - * @param $props Array: File properties, if known. This can be used to reduce the + * @param $flags Integer|bool: flags for publish() + * @param $props Array|bool: File properties, if known. This can be used to reduce the * upload time when uploading virtual URLs for which the file info * is already known - * @param $timestamp String: timestamp for img_timestamp, or false to use the current time - * @param $user Mixed: User object or null to use $wgUser + * @param $timestamp String|bool: timestamp for img_timestamp, or false to use the current time + * @param $user User|null: User object or null to use $wgUser * * @return FileRepoStatus object. On success, the value member contains the * archive name, or an empty string if it was a new file. */ function upload( $srcPath, $comment, $pageText, $flags = 0, $props = false, $timestamp = false, $user = null ) { global $wgContLang; + + if ( $this->getRepo()->getReadOnlyReason() !== false ) { + return $this->readOnlyFatalStatus(); + } + // truncate nicely or the DB will do it for us - // non-nicely (dangling multi-byte chars, non-truncated - // version in cache). + // non-nicely (dangling multi-byte chars, non-truncated version in cache). $comment = $wgContLang->truncate( $comment, 255 ); $this->lock(); // begin $status = $this->publish( $srcPath, $flags ); @@ -923,6 +992,14 @@ class LocalFile extends File { /** * Record a file upload in the upload log and the image table + * @param $oldver + * @param $desc string + * @param $license string + * @param $copyStatus string + * @param $source string + * @param $watch bool + * @param $timestamp string|bool + * @return bool */ function recordUpload( $oldver, $desc, $license = '', $copyStatus = '', $source = '', $watch = false, $timestamp = false ) @@ -942,20 +1019,31 @@ class LocalFile extends File { /** * Record a file upload in the upload log and the image table + * @param $oldver + * @param $comment string + * @param $pageText string + * @param $props bool|array + * @param $timestamp bool|string + * @param $user null|User + * @return bool */ function recordUpload2( $oldver, $comment, $pageText, $props = false, $timestamp = false, $user = null ) { + wfProfileIn( __METHOD__ ); + if ( is_null( $user ) ) { global $wgUser; $user = $wgUser; } $dbw = $this->repo->getMasterDB(); - $dbw->begin(); + $dbw->begin( __METHOD__ ); if ( !$props ) { + wfProfileIn( __METHOD__ . '-getProps' ); $props = $this->repo->getFileProps( $this->getVirtualUrl() ); + wfProfileOut( __METHOD__ . '-getProps' ); } if ( $timestamp === false ) { @@ -968,15 +1056,10 @@ class LocalFile extends File { $props['timestamp'] = wfTimestamp( TS_MW, $timestamp ); // DB -> TS_MW $this->setProps( $props ); - # Delete thumbnails - $this->purgeThumbnails(); - - # The file is already on its final location, remove it from the squid cache - SquidUpdate::purge( array( $this->getURL() ) ); - # Fail now if the file isn't there if ( !$this->fileExists ) { wfDebug( __METHOD__ . ": File " . $this->getRel() . " went missing!\n" ); + wfProfileOut( __METHOD__ ); return false; } @@ -1005,15 +1088,12 @@ class LocalFile extends File { __METHOD__, 'IGNORE' ); - if ( $dbw->affectedRows() == 0 ) { - if ( $oldver == '' ) { // XXX - # (bug 34993) publish() can displace the current file and yet fail to save - # a new one. The next publish attempt will treat the file as a brand new file - # and pass an empty $oldver. Allow this bogus value so we can displace the - # `image` row to `oldimage`, leaving room for the new current file `image` row. - #throw new MWException( "Empty oi_archive_name. Database and storage out of sync?" ); - } + # (bug 34993) Note: $oldver can be empty here, if the previous + # version of the file was broken. Allow registration of the new + # version to continue anyway, because that's better than having + # an image that's not fixable by user operations. + $reupload = true; # Collision, this is an update of a file # Insert previous contents into oldimage @@ -1060,16 +1140,8 @@ class LocalFile extends File { __METHOD__ ); } else { - # This is a new file - # Update the image count - $dbw->begin( __METHOD__ ); - $dbw->update( - 'site_stats', - array( 'ss_images = ss_images+1' ), - '*', - __METHOD__ - ); - $dbw->commit( __METHOD__ ); + # This is a new file, so update the image count + DeferredUpdates::addUpdate( SiteStatsUpdate::factory( array( 'images' => 1 ) ) ); } $descTitle = $this->getTitle(); @@ -1079,14 +1151,17 @@ class LocalFile extends File { # Add the log entry $log = new LogPage( 'upload' ); $action = $reupload ? 'overwrite' : 'upload'; - $log->addEntry( $action, $descTitle, $comment, array(), $user ); + $logId = $log->addEntry( $action, $descTitle, $comment, array(), $user ); + + wfProfileIn( __METHOD__ . '-edit' ); + $exists = $descTitle->exists(); - if ( $descTitle->exists() ) { + if ( $exists ) { # Create a null revision $latest = $descTitle->getLatestRevID(); $nullRevision = Revision::newNullRevision( $dbw, - $descTitle->getArticleId(), + $descTitle->getArticleID(), $log->getRcComment(), false ); @@ -1096,6 +1171,15 @@ class LocalFile extends File { wfRunHooks( 'NewRevisionFromEditComplete', array( $wikiPage, $nullRevision, $latest, $user ) ); $wikiPage->updateRevisionOn( $dbw, $nullRevision ); } + } + + # Commit the transaction now, in case something goes wrong later + # The most important thing is that files don't get lost, especially archives + # NOTE: once we have support for nested transactions, the commit may be moved + # to after $wikiPage->doEdit has been called. + $dbw->commit( __METHOD__ ); + + if ( $exists ) { # Invalidate the cache for the description page $descTitle->invalidateCache(); $descTitle->purgeSquid(); @@ -1103,12 +1187,19 @@ class LocalFile extends File { # New file; create the description page. # There's already a log entry, so don't make a second RC entry # Squid and file cache for the description page are purged by doEdit. - $wikiPage->doEdit( $pageText, $comment, EDIT_NEW | EDIT_SUPPRESS_RC, false, $user ); + $status = $wikiPage->doEdit( $pageText, $comment, EDIT_NEW | EDIT_SUPPRESS_RC, false, $user ); + + if ( isset( $status->value['revision'] ) ) { // XXX; doEdit() uses a transaction + $dbw->begin(); + $dbw->update( 'logging', + array( 'log_page' => $status->value['revision']->getPage() ), + array( 'log_id' => $logId ), + __METHOD__ + ); + $dbw->commit(); // commit before anything bad can happen + } } - - # Commit the transaction now, in case something goes wrong later - # The most important thing is that files don't get lost, especially archives - $dbw->commit(); + wfProfileOut( __METHOD__ . '-edit' ); # Save to cache and purge the squid # We shall not saveToCache before the commit since otherwise @@ -1116,8 +1207,20 @@ class LocalFile extends File { # which in fact doesn't really exist (bug 24978) $this->saveToCache(); + if ( $reupload ) { + # Delete old thumbnails + wfProfileIn( __METHOD__ . '-purge' ); + $this->purgeThumbnails(); + wfProfileOut( __METHOD__ . '-purge' ); + + # Remove the old file from the squid cache + SquidUpdate::purge( array( $this->getURL() ) ); + } + # Hooks, hooks, the magic of hooks... + wfProfileIn( __METHOD__ . '-hooks' ); wfRunHooks( 'FileUpload', array( $this, $reupload, $descTitle->exists() ) ); + wfProfileOut( __METHOD__ . '-hooks' ); # Invalidate cache for all pages using this file $update = new HTMLCacheUpdate( $this->getTitle(), 'imagelinks' ); @@ -1131,6 +1234,7 @@ class LocalFile extends File { $update->doUpdate(); } + wfProfileOut( __METHOD__ ); return true; } @@ -1167,6 +1271,10 @@ class LocalFile extends File { * archive name, or an empty string if it was a new file. */ function publishTo( $srcPath, $dstRel, $flags = 0 ) { + if ( $this->getRepo()->getReadOnlyReason() !== false ) { + return $this->readOnlyFatalStatus(); + } + $this->lock(); // begin $archiveName = wfTimestamp( TS_MW ) . '!'. $this->getName(); @@ -1203,20 +1311,26 @@ class LocalFile extends File { * @return FileRepoStatus object. */ function move( $target ) { - wfDebugLog( 'imagemove', "Got request to move {$this->name} to " . $target->getText() ); - $this->lock(); // begin + if ( $this->getRepo()->getReadOnlyReason() !== false ) { + return $this->readOnlyFatalStatus(); + } + wfDebugLog( 'imagemove', "Got request to move {$this->name} to " . $target->getText() ); $batch = new LocalFileMoveBatch( $this, $target ); - $batch->addCurrent(); - $batch->addOlds(); + $this->lock(); // begin + $batch->addCurrent(); + $archiveNames = $batch->addOlds(); $status = $batch->execute(); + $this->unlock(); // done + wfDebugLog( 'imagemove', "Finished moving {$this->name}" ); $this->purgeEverything(); - $this->unlock(); // done - - if ( $status->isOk() ) { + foreach ( $archiveNames as $archiveName ) { + $this->purgeOldThumbnails( $archiveName ); + } + if ( $status->isOK() ) { // Now switch the object $this->title = $target; // Force regeneration of the name and hashpath @@ -1242,30 +1356,27 @@ class LocalFile extends File { * @return FileRepoStatus object. */ function delete( $reason, $suppress = false ) { - $this->lock(); // begin + if ( $this->getRepo()->getReadOnlyReason() !== false ) { + return $this->readOnlyFatalStatus(); + } $batch = new LocalFileDeleteBatch( $this, $reason, $suppress ); - $batch->addCurrent(); + $this->lock(); // begin + $batch->addCurrent(); # Get old version relative paths - $dbw = $this->repo->getMasterDB(); - $result = $dbw->select( 'oldimage', - array( 'oi_archive_name' ), - array( 'oi_name' => $this->getName() ) ); - foreach ( $result as $row ) { - $batch->addOld( $row->oi_archive_name ); - $this->purgeOldThumbnails( $row->oi_archive_name ); - } + $archiveNames = $batch->addOlds(); $status = $batch->execute(); + $this->unlock(); // done - if ( $status->ok ) { - // Update site_stats - $site_stats = $dbw->tableName( 'site_stats' ); - $dbw->query( "UPDATE $site_stats SET ss_images=ss_images-1", __METHOD__ ); - $this->purgeEverything(); + if ( $status->isOK() ) { + DeferredUpdates::addUpdate( SiteStatsUpdate::factory( array( 'images' => -1 ) ) ); } - $this->unlock(); // done + $this->purgeEverything(); + foreach ( $archiveNames as $archiveName ) { + $this->purgeOldThumbnails( $archiveName ); + } return $status; } @@ -1285,16 +1396,19 @@ class LocalFile extends File { * @return FileRepoStatus object. */ function deleteOld( $archiveName, $reason, $suppress = false ) { - $this->lock(); // begin + if ( $this->getRepo()->getReadOnlyReason() !== false ) { + return $this->readOnlyFatalStatus(); + } $batch = new LocalFileDeleteBatch( $this, $reason, $suppress ); + + $this->lock(); // begin $batch->addOld( $archiveName ); - $this->purgeOldThumbnails( $archiveName ); $status = $batch->execute(); - $this->unlock(); // done - if ( $status->ok ) { + $this->purgeOldThumbnails( $archiveName ); + if ( $status->isOK() ) { $this->purgeDescription(); $this->purgeHistory(); } @@ -1308,30 +1422,32 @@ class LocalFile extends File { * * May throw database exceptions on error. * - * @param $versions set of record ids of deleted items to restore, + * @param $versions array set of record ids of deleted items to restore, * or empty to restore all revisions. * @param $unsuppress Boolean * @return FileRepoStatus */ function restore( $versions = array(), $unsuppress = false ) { + if ( $this->getRepo()->getReadOnlyReason() !== false ) { + return $this->readOnlyFatalStatus(); + } + $batch = new LocalFileRestoreBatch( $this, $unsuppress ); + $this->lock(); // begin if ( !$versions ) { $batch->addAll(); } else { $batch->addIds( $versions ); } - $status = $batch->execute(); - - if ( !$status->isGood() ) { - return $status; + if ( $status->isGood() ) { + $cleanupStatus = $batch->cleanup(); + $cleanupStatus->successCount = 0; + $cleanupStatus->failCount = 0; + $status->merge( $cleanupStatus ); } - - $cleanupStatus = $batch->cleanup(); - $cleanupStatus->successCount = 0; - $cleanupStatus->failCount = 0; - $status->merge( $cleanupStatus ); + $this->unlock(); // done return $status; } @@ -1343,6 +1459,7 @@ class LocalFile extends File { /** * Get the URL of the file description page. + * @return String */ function getDescriptionUrl() { return $this->title->getLocalUrl(); @@ -1352,10 +1469,11 @@ class LocalFile extends File { * Get the HTML text of the description page * This is not used by ImagePage for local files, since (among other things) * it skips the parser cache. + * @return bool|mixed */ function getDescriptionText() { global $wgParser; - $revision = Revision::newFromTitle( $this->title ); + $revision = Revision::newFromTitle( $this->title, false, Revision::READ_NORMAL ); if ( !$revision ) return false; $text = $revision->getText(); if ( !$text ) return false; @@ -1363,16 +1481,33 @@ class LocalFile extends File { return $pout->getText(); } - function getDescription() { + /** + * @return string + */ + function getDescription( $audience = self::FOR_PUBLIC, User $user = null ) { $this->load(); - return $this->description; + if ( $audience == self::FOR_PUBLIC && $this->isDeleted( self::DELETED_COMMENT ) ) { + return ''; + } elseif ( $audience == self::FOR_THIS_USER + && !$this->userCan( self::DELETED_COMMENT, $user ) ) + { + return ''; + } else { + return $this->description; + } } + /** + * @return bool|string + */ function getTimestamp() { $this->load(); return $this->timestamp; } + /** + * @return string + */ function getSha1() { $this->load(); // Initialise now if necessary @@ -1396,6 +1531,14 @@ class LocalFile extends File { } /** + * @return bool + */ + function isCacheable() { + $this->load(); + return strlen( $this->metadata ) <= self::CACHE_FIELD_MAX_LEN; // avoid OOMs + } + + /** * Start a transaction and lock the image for update * Increments a reference counter if the lock is already held * @return boolean True if the image exists, false otherwise @@ -1404,11 +1547,12 @@ class LocalFile extends File { $dbw = $this->repo->getMasterDB(); if ( !$this->locked ) { - $dbw->begin(); + $dbw->begin( __METHOD__ ); $this->locked++; } - return $dbw->selectField( 'image', '1', array( 'img_name' => $this->getName() ), __METHOD__ ); + return $dbw->selectField( 'image', '1', + array( 'img_name' => $this->getName() ), __METHOD__, array( 'FOR UPDATE' ) ); } /** @@ -1420,7 +1564,7 @@ class LocalFile extends File { --$this->locked; if ( !$this->locked ) { $dbw = $this->repo->getMasterDB(); - $dbw->commit(); + $dbw->commit( __METHOD__ ); } } } @@ -1431,7 +1575,15 @@ class LocalFile extends File { function unlockAndRollback() { $this->locked = false; $dbw = $this->repo->getMasterDB(); - $dbw->rollback(); + $dbw->rollback( __METHOD__ ); + } + + /** + * @return Status + */ + protected function readOnlyFatalStatus() { + return $this->getRepo()->newFatal( 'filereadonlyerror', $this->getName(), + $this->getRepo()->getName(), $this->getRepo()->getReadOnlyReason() ); } } // LocalFile class @@ -1451,6 +1603,11 @@ class LocalFileDeleteBatch { var $reason, $srcRels = array(), $archiveUrls = array(), $deletionBatch, $suppress; var $status; + /** + * @param $file File + * @param $reason string + * @param $suppress bool + */ function __construct( File $file, $reason = '', $suppress = false ) { $this->file = $file; $this->reason = $reason; @@ -1462,11 +1619,39 @@ class LocalFileDeleteBatch { $this->srcRels['.'] = $this->file->getRel(); } + /** + * @param $oldName string + */ function addOld( $oldName ) { $this->srcRels[$oldName] = $this->file->getArchiveRel( $oldName ); $this->archiveUrls[] = $this->file->getArchiveUrl( $oldName ); } + /** + * Add the old versions of the image to the batch + * @return Array List of archive names from old versions + */ + function addOlds() { + $archiveNames = array(); + + $dbw = $this->file->repo->getMasterDB(); + $result = $dbw->select( 'oldimage', + array( 'oi_archive_name' ), + array( 'oi_name' => $this->file->getName() ), + __METHOD__ + ); + + foreach ( $result as $row ) { + $this->addOld( $row->oi_archive_name ); + $archiveNames[] = $row->oi_archive_name; + } + + return $archiveNames; + } + + /** + * @return array + */ function getOldRels() { if ( !isset( $this->srcRels['.'] ) ) { $oldRels =& $this->srcRels; @@ -1480,6 +1665,9 @@ class LocalFileDeleteBatch { return array( $oldRels, $deleteCurrent ); } + /** + * @return array + */ protected function getHashes() { $hashes = array(); list( $oldRels, $deleteCurrent ) = $this->getOldRels(); @@ -1601,7 +1789,7 @@ class LocalFileDeleteBatch { 'fa_deleted_user' => $encUserId, 'fa_deleted_timestamp' => $encTimestamp, 'fa_deleted_reason' => $encReason, - 'fa_deleted' => $this->suppress ? $bitfield : 'oi_deleted', + 'fa_deleted' => $this->suppress ? $bitfield : 'oi_deleted', 'fa_name' => 'oi_name', 'fa_archive_name' => 'oi_archive_name', @@ -1617,7 +1805,6 @@ class LocalFileDeleteBatch { 'fa_user' => 'oi_user', 'fa_user_text' => 'oi_user_text', 'fa_timestamp' => 'oi_timestamp', - 'fa_deleted' => $bitfield ), $where, __METHOD__ ); } } @@ -1641,9 +1828,9 @@ class LocalFileDeleteBatch { /** * Run the transaction + * @return FileRepoStatus */ function execute() { - global $wgUseSquid; wfProfileIn( __METHOD__ ); $this->file->lock(); @@ -1699,7 +1886,7 @@ class LocalFileDeleteBatch { $this->status->merge( $status ); } - if ( !$this->status->ok ) { + if ( !$this->status->isOK() ) { // Critical file deletion error // Roll back inserts, release lock and abort // TODO: delete the defunct filearchive rows if we are using a non-transactional DB @@ -1708,17 +1895,6 @@ class LocalFileDeleteBatch { return $this->status; } - // Purge squid - if ( $wgUseSquid ) { - $urls = array(); - - foreach ( $this->srcRels as $srcRel ) { - $urlRel = str_replace( '%2F', '/', rawurlencode( $srcRel ) ); - $urls[] = $this->file->repo->getZoneUrl( 'public' ) . '/' . $urlRel; - } - SquidUpdate::purge( $urls ); - } - // Delete image/oldimage rows $this->doDBDeletes(); @@ -1731,6 +1907,8 @@ class LocalFileDeleteBatch { /** * Removes non-existent files from a deletion batch. + * @param $batch array + * @return array */ function removeNonexistentFiles( $batch ) { $files = $newBatch = array(); @@ -1740,7 +1918,7 @@ class LocalFileDeleteBatch { $files[$src] = $this->file->repo->getVirtualUrl( 'public' ) . '/' . rawurlencode( $src ); } - $result = $this->file->repo->fileExistsBatch( $files, FileRepo::FILES_ONLY ); + $result = $this->file->repo->fileExistsBatch( $files ); foreach ( $batch as $batchItem ) { if ( $result[$batchItem[0]] ) { @@ -1766,6 +1944,10 @@ class LocalFileRestoreBatch { var $cleanupBatch, $ids, $all, $unsuppress = false; + /** + * @param $file File + * @param $unsuppress bool + */ function __construct( File $file, $unsuppress = false ) { $this->file = $file; $this->cleanupBatch = $this->ids = array(); @@ -1800,6 +1982,7 @@ class LocalFileRestoreBatch { * rows and there's no need to keep the image row locked while it's acquiring those locks * The caller may have its own transaction open. * So we save the batch and let the caller call cleanup() + * @return FileRepoStatus */ function execute() { global $wgLang; @@ -2003,9 +2186,7 @@ class LocalFileRestoreBatch { if ( !$exists ) { wfDebug( __METHOD__ . " restored {$status->successCount} items, creating a new current\n" ); - // Update site_stats - $site_stats = $dbw->tableName( 'site_stats' ); - $dbw->query( "UPDATE $site_stats SET ss_images=ss_images+1", __METHOD__ ); + DeferredUpdates::addUpdate( SiteStatsUpdate::factory( array( 'images' => 1 ) ) ); $this->file->purgeEverything(); } else { @@ -2022,13 +2203,16 @@ class LocalFileRestoreBatch { /** * Removes non-existent files from a store batch. + * @param $triplets array + * @return array */ function removeNonexistentFiles( $triplets ) { $files = $filteredTriplets = array(); - foreach ( $triplets as $file ) + foreach ( $triplets as $file ) { $files[$file[0]] = $file[0]; + } - $result = $this->file->repo->fileExistsBatch( $files, FileRepo::FILES_ONLY ); + $result = $this->file->repo->fileExistsBatch( $files ); foreach ( $triplets as $file ) { if ( $result[$file[0]] ) { @@ -2041,6 +2225,8 @@ class LocalFileRestoreBatch { /** * Removes non-existent files from a cleanup batch. + * @param $batch array + * @return array */ function removeNonexistentFromCleanup( $batch ) { $files = $newBatch = array(); @@ -2051,7 +2237,7 @@ class LocalFileRestoreBatch { rawurlencode( $repo->getDeletedHashPath( $file ) . $file ); } - $result = $repo->fileExistsBatch( $files, FileRepo::FILES_ONLY ); + $result = $repo->fileExistsBatch( $files ); foreach ( $batch as $file ) { if ( $result[$file] ) { @@ -2065,6 +2251,7 @@ class LocalFileRestoreBatch { /** * Delete unused files in the deleted zone. * This should be called from outside the transaction in which execute() was called. + * @return FileRepoStatus|void */ function cleanup() { if ( !$this->cleanupBatch ) { @@ -2109,7 +2296,7 @@ class LocalFileRestoreBatch { class LocalFileMoveBatch { /** - * @var File + * @var LocalFile */ var $file; @@ -2118,8 +2305,17 @@ class LocalFileMoveBatch { */ var $target; - var $cur, $olds, $oldCount, $archive, $db; + var $cur, $olds, $oldCount, $archive; + /** + * @var DatabaseBase + */ + var $db; + + /** + * @param File $file + * @param Title $target + */ function __construct( File $file, Title $target ) { $this->file = $file; $this->target = $target; @@ -2129,7 +2325,7 @@ class LocalFileMoveBatch { $this->newName = $this->file->repo->getNameFromTitle( $this->target ); $this->oldRel = $this->oldHash . $this->oldName; $this->newRel = $this->newHash . $this->newName; - $this->db = $file->repo->getMasterDb(); + $this->db = $file->getRepo()->getMasterDb(); } /** @@ -2141,11 +2337,13 @@ class LocalFileMoveBatch { /** * Add the old versions of the image to the batch + * @return Array List of archive names from old versions */ function addOlds() { $archiveBase = 'archive'; $this->olds = array(); $this->oldCount = 0; + $archiveNames = array(); $result = $this->db->select( 'oldimage', array( 'oi_archive_name', 'oi_deleted' ), @@ -2154,6 +2352,7 @@ class LocalFileMoveBatch { ); foreach ( $result as $row ) { + $archiveNames[] = $row->oi_archive_name; $oldName = $row->oi_archive_name; $bits = explode( '!', $oldName, 2 ); @@ -2181,39 +2380,49 @@ class LocalFileMoveBatch { "{$archiveBase}/{$this->newHash}{$timestamp}!{$this->newName}" ); } + + return $archiveNames; } /** * Perform the move. + * @return FileRepoStatus */ function execute() { $repo = $this->file->repo; $status = $repo->newGood(); - $triplets = $this->getMoveTriplets(); + $triplets = $this->getMoveTriplets(); $triplets = $this->removeNonexistentFiles( $triplets ); - // Copy the files into their new location - $statusMove = $repo->storeBatch( $triplets ); + $this->file->lock(); // begin + // Rename the file versions metadata in the DB. + // This implicitly locks the destination file, which avoids race conditions. + // If we moved the files from A -> C before DB updates, another process could + // move files from B -> C at this point, causing storeBatch() to fail and thus + // cleanupTarget() to trigger. It would delete the C files and cause data loss. + $statusDb = $this->doDBUpdates(); + if ( !$statusDb->isGood() ) { + $this->file->unlockAndRollback(); + $statusDb->ok = false; + return $statusDb; + } + wfDebugLog( 'imagemove', "Renamed {$this->file->getName()} in database: {$statusDb->successCount} successes, {$statusDb->failCount} failures" ); + + // Copy the files into their new location. + // If a prior process fataled copying or cleaning up files we tolerate any + // of the existing files if they are identical to the ones being stored. + $statusMove = $repo->storeBatch( $triplets, FileRepo::OVERWRITE_SAME ); wfDebugLog( 'imagemove', "Moved files for {$this->file->getName()}: {$statusMove->successCount} successes, {$statusMove->failCount} failures" ); if ( !$statusMove->isGood() ) { - wfDebugLog( 'imagemove', "Error in moving files: " . $statusMove->getWikiText() ); + // Delete any files copied over (while the destination is still locked) $this->cleanupTarget( $triplets ); + $this->file->unlockAndRollback(); // unlocks the destination + wfDebugLog( 'imagemove', "Error in moving files: " . $statusMove->getWikiText() ); $statusMove->ok = false; return $statusMove; } - - $this->db->begin(); - $statusDb = $this->doDBUpdates(); - wfDebugLog( 'imagemove', "Renamed {$this->file->getName()} in database: {$statusDb->successCount} successes, {$statusDb->failCount} failures" ); - if ( !$statusDb->isGood() ) { - $this->db->rollback(); - // Something went wrong with the DB updates, so remove the target files - $this->cleanupTarget( $triplets ); - $statusDb->ok = false; - return $statusDb; - } - $this->db->commit(); + $this->file->unlock(); // done // Everything went ok, remove the source files $this->cleanupSource( $triplets ); @@ -2256,7 +2465,8 @@ class LocalFileMoveBatch { 'oldimage', array( 'oi_name' => $this->newName, - 'oi_archive_name = ' . $dbw->strreplace( 'oi_archive_name', $dbw->addQuotes( $this->oldName ), $dbw->addQuotes( $this->newName ) ), + 'oi_archive_name = ' . $dbw->strreplace( 'oi_archive_name', + $dbw->addQuotes( $this->oldName ), $dbw->addQuotes( $this->newName ) ), ), array( 'oi_name' => $this->oldName ), __METHOD__ @@ -2265,7 +2475,10 @@ class LocalFileMoveBatch { $affected = $dbw->affectedRows(); $total = $this->oldCount; $status->successCount += $affected; - $status->failCount += $total - $affected; + // Bug 34934: $total is based on files that actually exist. + // There may be more DB rows than such files, in which case $affected + // can be greater than $total. We use max() to avoid negatives here. + $status->failCount += max( 0, $total - $affected ); if ( $status->failCount ) { $status->error( 'imageinvalidfilename' ); } @@ -2275,6 +2488,7 @@ class LocalFileMoveBatch { /** * Generate triplets for FileRepo::storeBatch(). + * @return array */ function getMoveTriplets() { $moves = array_merge( array( $this->cur ), $this->olds ); @@ -2292,6 +2506,8 @@ class LocalFileMoveBatch { /** * Removes non-existent files from move batch. + * @param $triplets array + * @return array */ function removeNonexistentFiles( $triplets ) { $files = array(); @@ -2300,7 +2516,7 @@ class LocalFileMoveBatch { $files[$file[0]] = $file[0]; } - $result = $this->file->repo->fileExistsBatch( $files, FileRepo::FILES_ONLY ); + $result = $this->file->repo->fileExistsBatch( $files ); $filteredTriplets = array(); foreach ( $triplets as $file ) { @@ -2322,6 +2538,7 @@ class LocalFileMoveBatch { // Create dest pairs from the triplets $pairs = array(); foreach ( $triplets as $triplet ) { + // $triplet: (old source virtual URL, dst zone, dest rel) $pairs[] = array( $triplet[1], $triplet[2] ); } |