<?php
/**
+ * Local file in the wiki's own database
+ *
+ * @file
+ * @ingroup FileRepo
*/
/**
/**#@+
* @private
*/
- var $fileExists, # does the file file exist on disk? (loadFromXxx)
- $historyLine, # Number of line to return by nextHistoryLine() (constructor)
- $historyRes, # result of the query for the file's history (nextHistoryLine)
+ var
+ $fileExists, # does the file file exist on disk? (loadFromXxx)
+ $historyLine, # Number of line to return by nextHistoryLine() (constructor)
+ $historyRes, # result of the query for the file's history (nextHistoryLine)
$width, # \
$height, # |
$bits, # --- returned by getimagesize (loadFromXxx)
$upgraded, # Whether the row was upgraded on load
$locked, # True if the image row is locked
$missing, # True if file is not present in file system. Not to be cached in memcached
- $deleted; # Bitfield akin to rev_deleted
+ $deleted; # Bitfield akin to rev_deleted
/**#@-*/
$title = Title::makeTitle( NS_FILE, $row->img_name );
$file = new self( $title, $repo );
$file->loadFromRow( $row );
+
return $file;
}
-
+
/**
* Create a LocalFile from a SHA-1 key
* Do not call this except from inside a repo class.
+ * @param $sha1
+ * @param $repo LocalRepo
+ * @param $timestamp
*/
static function newFromKey( $sha1, $repo, $timestamp = false ) {
$conds = array( 'img_sha1' => $sha1 );
- if( $timestamp ) {
+
+ if ( $timestamp ) {
$conds['img_timestamp'] = $timestamp;
}
+
$dbr = $repo->getSlaveDB();
$row = $dbr->selectRow( 'image', self::selectFields(), $conds, __METHOD__ );
- if( $row ) {
+
+ if ( $row ) {
return self::newFromRow( $row, $repo );
} else {
return false;
}
}
-
+
/**
* Fields in the image table
*/
* Do not call this except from inside a repo class.
*/
function __construct( $title, $repo ) {
- if( !is_object( $title ) ) {
+ if ( !is_object( $title ) ) {
throw new MWException( __CLASS__ . ' constructor given bogus title.' );
}
+
parent::__construct( $title, $repo );
+
$this->metadata = '';
$this->historyLine = 0;
$this->historyRes = null;
}
/**
- * Get the memcached key for the main data for this file, or false if
+ * Get the memcached key for the main data for this file, or false if
* there is no access to the shared cache.
*/
function getCacheKey() {
$hashedName = md5( $this->getName() );
+
return $this->repo->getSharedCacheKey( 'file', $hashedName );
}
*/
function loadFromCache() {
global $wgMemc;
+
wfProfileIn( __METHOD__ );
$this->dataLoaded = false;
$key = $this->getCacheKey();
+
if ( !$key ) {
wfProfileOut( __METHOD__ );
return false;
}
+
$cachedValues = $wgMemc->get( $key );
// Check if the key existed and belongs to this version of MediaWiki
}
$this->dataLoaded = true;
}
+
if ( $this->dataLoaded ) {
wfIncrStats( 'image_cache_hit' );
} else {
*/
function saveToCache() {
global $wgMemc;
+
$this->load();
$key = $this->getCacheKey();
+
if ( !$key ) {
return;
}
+
$fields = $this->getCacheFields( '' );
$cache = array( 'version' => MW_FILE_VERSION );
$cache['fileExists'] = $this->fileExists;
+
if ( $this->fileExists ) {
foreach ( $fields as $field ) {
$cache[$field] = $this->$field;
static $fields = array( 'size', 'width', 'height', 'bits', 'media_type',
'major_mime', 'minor_mime', 'metadata', 'timestamp', 'sha1', 'user', 'user_text', 'description' );
static $results = array();
+
if ( $prefix == '' ) {
return $fields;
}
+
if ( !isset( $results[$prefix] ) ) {
$prefixedFields = array();
foreach ( $fields as $field ) {
}
$results[$prefix] = $prefixedFields;
}
+
return $results[$prefix];
}
$row = $dbr->selectRow( 'image', $this->getCacheFields( 'img_' ),
array( 'img_name' => $this->getName() ), $fname );
+
if ( $row ) {
$this->loadFromRow( $row );
} else {
function decodeRow( $row, $prefix = 'img_' ) {
$array = (array)$row;
$prefixLength = strlen( $prefix );
+
// Sanity check prefix once
if ( substr( key( $array ), 0, $prefixLength ) !== $prefix ) {
throw new MWException( __METHOD__ . ': incorrect $prefix parameter' );
}
+
$decoded = array();
+
foreach ( $array as $name => $value ) {
$decoded[substr( $name, $prefixLength )] = $value;
}
+
$decoded['timestamp'] = wfTimestamp( TS_MW, $decoded['timestamp'] );
+
if ( empty( $decoded['major_mime'] ) ) {
$decoded['mime'] = 'unknown/unknown';
} else {
}
$decoded['mime'] = $decoded['major_mime'] . '/' . $decoded['minor_mime'];
}
+
# Trim zero padding from char/binary field
$decoded['sha1'] = rtrim( $decoded['sha1'], "\0" );
+
return $decoded;
}
function loadFromRow( $row, $prefix = 'img_' ) {
$this->dataLoaded = true;
$array = $this->decodeRow( $row, $prefix );
+
foreach ( $array as $name => $value ) {
$this->$name = $value;
}
+
$this->fileExists = true;
$this->maybeUpgradeRow();
}
if ( wfReadOnly() ) {
return;
}
+
if ( is_null( $this->media_type ) ||
$this->mime == 'image/svg'
) {
wfProfileOut( __METHOD__ );
return;
}
+
$dbw = $this->repo->getMasterDB();
list( $major, $minor ) = self::splitMime( $this->mime );
), array( 'img_name' => $this->getName() ),
__METHOD__
);
+
$this->saveToCache();
wfProfileOut( __METHOD__ );
}
$this->dataLoaded = true;
$fields = $this->getCacheFields( '' );
$fields[] = 'fileExists';
+
foreach ( $fields as $field ) {
if ( isset( $info[$field] ) ) {
$this->$field = $info[$field];
}
}
+
// Fix up mime fields
if ( isset( $info['major_mime'] ) ) {
$this->mime = "{$info['major_mime']}/{$info['minor_mime']}";
} elseif ( isset( $info['mime'] ) ) {
+ $this->mime = $info['mime'];
list( $this->major_mime, $this->minor_mime ) = self::splitMime( $this->mime );
}
}
/** isVisible inhereted */
function isMissing() {
- if( $this->missing === null ) {
+ if ( $this->missing === null ) {
list( $fileExists ) = $this->repo->fileExistsBatch( array( $this->getVirtualUrl() ), FileRepo::FILES_ONLY );
$this->missing = !$fileExists;
}
*/
public function getWidth( $page = 1 ) {
$this->load();
+
if ( $this->isMultipage() ) {
$dim = $this->getHandler()->getPageDimensions( $this, $page );
if ( $dim ) {
*/
public function getHeight( $page = 1 ) {
$this->load();
+
if ( $this->isMultipage() ) {
$dim = $this->getHandler()->getPageDimensions( $this, $page );
if ( $dim ) {
*/
function getUser( $type = 'text' ) {
$this->load();
- if( $type == 'text' ) {
+
+ if ( $type == 'text' ) {
return $this->user_text;
- } elseif( $type == 'id' ) {
+ } elseif ( $type == 'id' ) {
return $this->user;
}
}
function migrateThumbFile( $thumbName ) {
$thumbDir = $this->getThumbPath();
$thumbPath = "$thumbDir/$thumbName";
+
if ( is_dir( $thumbPath ) ) {
// Directory where file should be
// This happened occasionally due to broken migration code in 1.5
// Doesn't exist anymore
clearstatcache();
}
+
if ( is_file( $thumbDir ) ) {
// File where directory should be
unlink( $thumbDir );
*/
function getThumbnails() {
$this->load();
+
$files = array();
$dir = $this->getThumbPath();
if ( $handle ) {
while ( false !== ( $file = readdir( $handle ) ) ) {
- if ( $file{0} != '.' ) {
+ if ( $file { 0 } != '.' ) {
$files[] = $file;
}
}
+
closedir( $handle );
}
}
*/
function purgeHistory() {
global $wgMemc;
+
$hashedName = md5( $this->getName() );
$oldKey = $this->repo->getSharedCacheKey( 'oldfile', $hashedName );
+
if ( $oldKey ) {
$wgMemc->delete( $oldKey );
}
* Delete cached transformed files
*/
function purgeThumbnails() {
- global $wgUseSquid;
+ global $wgUseSquid, $wgExcludeFromThumbnailPurge;
+
// Delete thumbnails
$files = $this->getThumbnails();
$dir = $this->getThumbPath();
$urls = array();
+
foreach ( $files as $file ) {
+ // Only remove files not in the $wgExcludeFromThumbnailPurge configuration variable
+ $ext = pathinfo( "$dir/$file", PATHINFO_EXTENSION );
+ if ( in_array( $ext, $wgExcludeFromThumbnailPurge ) ) {
+ continue;
+ }
+
# Check that the base file name is part of the thumb name
# This is a basic sanity check to avoid erasing unrelated directories
if ( strpos( $file, $this->getName() ) !== false ) {
$conds = $opts = $join_conds = array();
$eq = $inc ? '=' : '';
$conds[] = "oi_name = " . $dbr->addQuotes( $this->title->getDBkey() );
- if( $start ) {
+
+ if ( $start ) {
$conds[] = "oi_timestamp <$eq " . $dbr->addQuotes( $dbr->timestamp( $start ) );
}
- if( $end ) {
+
+ if ( $end ) {
$conds[] = "oi_timestamp >$eq " . $dbr->addQuotes( $dbr->timestamp( $end ) );
}
- if( $limit ) {
+
+ if ( $limit ) {
$opts['LIMIT'] = $limit;
}
+
// Search backwards for time > x queries
$order = ( !$start && $end !== null ) ? 'ASC' : 'DESC';
$opts['ORDER BY'] = "oi_timestamp $order";
$opts['USE INDEX'] = array( 'oldimage' => 'oi_name_timestamp' );
- wfRunHooks( 'LocalFile::getHistory', array( &$this, &$tables, &$fields,
+ wfRunHooks( 'LocalFile::getHistory', array( &$this, &$tables, &$fields,
&$conds, &$opts, &$join_conds ) );
$res = $dbr->select( $tables, $fields, $conds, __METHOD__, $opts, $join_conds );
$r = array();
- while( $row = $dbr->fetchObject( $res ) ) {
+
+ foreach ( $res as $row ) {
if ( $this->repo->oldFileFromRowFactory ) {
$r[] = call_user_func( $this->repo->oldFileFromRowFactory, $row, $this->repo );
} else {
$r[] = OldLocalFile::newFromRow( $row, $this->repo );
}
}
- if( $order == 'ASC' ) {
+
+ if ( $order == 'ASC' ) {
$r = array_reverse( $r ); // make sure it ends up descending
}
+
return $r;
}
array( 'img_name' => $this->title->getDBkey() ),
$fname
);
+
if ( 0 == $dbr->numRows( $this->historyRes ) ) {
$this->historyRes = null;
return false;
*/
public function resetHistory() {
$this->historyLine = 0;
+
if ( !is_null( $this->historyRes ) ) {
$this->historyRes = null;
}
function upload( $srcPath, $comment, $pageText, $flags = 0, $props = false, $timestamp = false, $user = null ) {
$this->lock();
$status = $this->publish( $srcPath, $flags );
+
if ( $status->ok ) {
if ( !$this->recordUpload2( $status->value, $comment, $pageText, $props, $timestamp, $user ) ) {
$status->fatal( 'filenotfound', $srcPath );
}
}
+
$this->unlock();
+
return $status;
}
$watch = false, $timestamp = false )
{
$pageText = SpecialUpload::getInitialPageText( $desc, $license, $copyStatus, $source );
+
if ( !$this->recordUpload2( $oldver, $desc, $pageText ) ) {
return false;
}
+
if ( $watch ) {
global $wgUser;
$wgUser->addWatch( $this->getTitle() );
*/
function recordUpload2( $oldver, $comment, $pageText, $props = false, $timestamp = false, $user = null )
{
- if( is_null( $user ) ) {
+ if ( is_null( $user ) ) {
global $wgUser;
- $user = $wgUser;
+ $user = $wgUser;
}
$dbw = $this->repo->getMasterDB();
if ( !$props ) {
$props = $this->repo->getFileProps( $this->getVirtualUrl() );
}
+
$props['description'] = $comment;
$props['user'] = $user->getId();
$props['user_text'] = $user->getName();
$props['timestamp'] = wfTimestamp( TS_MW );
$this->setProps( $props );
- // Delete thumbnails and refresh the metadata cache
+ # Delete thumbnails
$this->purgeThumbnails();
- $this->saveToCache();
+
+ # 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
+ # Fail now if the file isn't there
if ( !$this->fileExists ) {
wfDebug( __METHOD__ . ": File " . $this->getPath() . " went missing!\n" );
return false;
}
$reupload = false;
+
if ( $timestamp === false ) {
$timestamp = $dbw->timestamp();
}
$dbw->insert( 'image',
array(
'img_name' => $this->getName(),
- 'img_size'=> $this->size,
+ 'img_size' => $this->size,
'img_width' => intval( $this->width ),
'img_height' => intval( $this->height ),
'img_bits' => $this->bits,
'IGNORE'
);
- if( $dbw->affectedRows() == 0 ) {
+ if ( $dbw->affectedRows() == 0 ) {
$reupload = true;
# Collision, this is an update of a file
$action = $reupload ? 'overwrite' : 'upload';
$log->addEntry( $action, $descTitle, $comment, array(), $user );
- if( $descTitle->exists() ) {
+ if ( $descTitle->exists() ) {
# Create a null revision
$latest = $descTitle->getLatestRevID();
- $nullRevision = Revision::newNullRevision( $dbw, $descTitle->getArticleId(),
- $log->getRcComment(), false );
+ $nullRevision = Revision::newNullRevision(
+ $dbw,
+ $descTitle->getArticleId(),
+ $log->getRcComment(),
+ false
+ );
$nullRevision->insertOn( $dbw );
-
+
wfRunHooks( 'NewRevisionFromEditComplete', array( $article, $nullRevision, $latest, $user ) );
$article->updateRevisionOn( $dbw, $nullRevision );
$descTitle->invalidateCache();
$descTitle->purgeSquid();
} else {
- // New file; create the description page.
- // There's already a log entry, so don't make a second RC entry
+ # 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.
$article->doEdit( $pageText, $comment, EDIT_NEW | EDIT_SUPPRESS_RC );
}
- # Hooks, hooks, the magic of hooks...
- wfRunHooks( 'FileUpload', array( $this ) );
-
# 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();
+ # Save to cache and purge the squid
+ # We shall not saveToCache before the commit since otherwise
+ # in case of a rollback there is an usable file from memcached
+ # which in fact doesn't really exist (bug 24978)
+ $this->saveToCache();
+
+ # Hooks, hooks, the magic of hooks...
+ wfRunHooks( 'FileUpload', array( $this, $reupload, $descTitle->exists() ) );
+
# Invalidate cache for all pages using this file
$update = new HTMLCacheUpdate( $this->getTitle(), 'imagelinks' );
$update->doUpdate();
+
# Invalidate cache for all pages that redirects on this page
$redirs = $this->getTitle()->getRedirectsHere();
- foreach( $redirs as $redir ) {
+
+ foreach ( $redirs as $redir ) {
$update = new HTMLCacheUpdate( $redir, 'imagelinks' );
$update->doUpdate();
}
*/
function publish( $srcPath, $flags = 0 ) {
$this->lock();
+
$dstRel = $this->getRel();
- $archiveName = gmdate( 'YmdHis' ) . '!'. $this->getName();
+ $archiveName = gmdate( 'YmdHis' ) . '!' . $this->getName();
$archiveRel = 'archive/' . $this->getHashPath() . $archiveName;
$flags = $flags & File::DELETE_SOURCE ? LocalRepo::DELETE_SOURCE : 0;
$status = $this->repo->publish( $srcPath, $dstRel, $archiveRel, $flags );
+
if ( $status->value == 'new' ) {
$status->value = '';
} else {
$status->value = $archiveName;
}
+
$this->unlock();
+
return $status;
}
function move( $target ) {
wfDebugLog( 'imagemove', "Got request to move {$this->name} to " . $target->getText() );
$this->lock();
+
$batch = new LocalFileMoveBatch( $this, $target );
$batch->addCurrent();
$batch->addOlds();
$status = $batch->execute();
wfDebugLog( 'imagemove', "Finished moving {$this->name}" );
+
$this->purgeEverything();
$this->unlock();
// Purge the new image
$this->purgeEverything();
}
-
+
return $status;
}
*/
function delete( $reason, $suppress = false ) {
$this->lock();
+
$batch = new LocalFileDeleteBatch( $this, $reason, $suppress );
$batch->addCurrent();
$result = $dbw->select( 'oldimage',
array( 'oi_archive_name' ),
array( 'oi_name' => $this->getName() ) );
- while ( $row = $dbw->fetchObject( $result ) ) {
+ foreach ( $result as $row ) {
$batch->addOld( $row->oi_archive_name );
}
$status = $batch->execute();
}
$this->unlock();
+
return $status;
}
* @throws MWException or FSException on database or file store failure
* @return FileRepoStatus object.
*/
- function deleteOld( $archiveName, $reason, $suppress=false ) {
+ function deleteOld( $archiveName, $reason, $suppress = false ) {
$this->lock();
+
$batch = new LocalFileDeleteBatch( $this, $reason, $suppress );
$batch->addOld( $archiveName );
$status = $batch->execute();
+
$this->unlock();
+
if ( $status->ok ) {
$this->purgeDescription();
$this->purgeHistory();
}
+
return $status;
}
*/
function restore( $versions = array(), $unsuppress = false ) {
$batch = new LocalFileRestoreBatch( $this, $unsuppress );
+
if ( !$versions ) {
$batch->addAll();
} else {
$batch->addIds( $versions );
}
+
$status = $batch->execute();
- if ( !$status->ok ) {
+
+ if ( !$status->isGood() ) {
return $status;
}
$cleanupStatus->successCount = 0;
$cleanupStatus->failCount = 0;
$status->merge( $cleanupStatus );
+
return $status;
}
/** scaleHeight inherited */
/** getImageSize inherited */
- /** getDescriptionUrl inherited */
- /** getDescriptionText inherited */
+ /**
+ * Get the URL of the file description page.
+ */
+ function getDescriptionUrl() {
+ return $this->title->getLocalUrl();
+ }
+
+ /**
+ * 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.
+ */
+ function getDescriptionText() {
+ global $wgParser;
+ $revision = Revision::newFromTitle( $this->title );
+ if ( !$revision ) return false;
+ $text = $revision->getText();
+ if ( !$text ) return false;
+ $pout = $wgParser->parse( $text, $this->title, new ParserOptions() );
+ return $pout->getText();
+ }
function getDescription() {
$this->load();
*/
function lock() {
$dbw = $this->repo->getMasterDB();
+
if ( !$this->locked ) {
$dbw->begin();
$this->locked++;
}
+
return $dbw->selectField( 'image', '1', array( 'img_name' => $this->getName() ), __METHOD__ );
}
}
} // LocalFile class
-#------------------------------------------------------------------------------
+# ------------------------------------------------------------------------------
/**
* Helper class for file deletion
* @ingroup FileRepo
*/
class LocalFileDeleteBatch {
- var $file, $reason, $srcRels = array(), $archiveUrls = array(), $deletionBatch, $suppress;
+
+ /**
+ * @var LocalFile
+ */
+ var $file;
+
+ var $reason, $srcRels = array(), $archiveUrls = array(), $deletionBatch, $suppress;
var $status;
function __construct( File $file, $reason = '', $suppress = false ) {
unset( $oldRels['.'] );
$deleteCurrent = true;
}
+
return array( $oldRels, $deleteCurrent );
}
/*protected*/ function getHashes() {
$hashes = array();
list( $oldRels, $deleteCurrent ) = $this->getOldRels();
+
if ( $deleteCurrent ) {
$hashes['.'] = $this->file->getSha1();
}
+
if ( count( $oldRels ) ) {
$dbw = $this->file->repo->getMasterDB();
- $res = $dbw->select( 'oldimage', array( 'oi_archive_name', 'oi_sha1' ),
- 'oi_archive_name IN(' . $dbw->makeList( array_keys( $oldRels ) ) . ')',
- __METHOD__ );
- while ( $row = $dbw->fetchObject( $res ) ) {
+ $res = $dbw->select(
+ 'oldimage',
+ array( 'oi_archive_name', 'oi_sha1' ),
+ 'oi_archive_name IN (' . $dbw->makeList( array_keys( $oldRels ) ) . ')',
+ __METHOD__
+ );
+
+ foreach ( $res as $row ) {
if ( rtrim( $row->oi_sha1, "\0" ) === '' ) {
// Get the hash from the file
$oldUrl = $this->file->getArchiveVirtualUrl( $row->oi_archive_name );
$props = $this->file->repo->getFileProps( $oldUrl );
+
if ( $props['fileExists'] ) {
// Upgrade the oldimage row
$dbw->update( 'oldimage',
}
}
}
+
$missing = array_diff_key( $this->srcRels, $hashes );
+
foreach ( $missing as $name => $rel ) {
$this->status->error( 'filedelete-old-unregistered', $name );
}
+
foreach ( $hashes as $name => $hash ) {
if ( !$hash ) {
$this->status->error( 'filedelete-missing', $this->srcRels[$name] );
function doDBInserts() {
global $wgUser;
+
$dbw = $this->file->repo->getMasterDB();
$encTimestamp = $dbw->addQuotes( $dbw->timestamp() );
$encUserId = $dbw->addQuotes( $wgUser->getId() );
function doDBDeletes() {
$dbw = $this->file->repo->getMasterDB();
list( $oldRels, $deleteCurrent ) = $this->getOldRels();
+
if ( count( $oldRels ) ) {
$dbw->delete( 'oldimage',
array(
'oi_archive_name' => array_keys( $oldRels )
), __METHOD__ );
}
+
if ( $deleteCurrent ) {
$dbw->delete( 'image', array( 'img_name' => $this->file->getName() ), __METHOD__ );
}
$privateFiles = array();
list( $oldRels, $deleteCurrent ) = $this->getOldRels();
$dbw = $this->file->repo->getMasterDB();
- if( !empty( $oldRels ) ) {
+
+ if ( !empty( $oldRels ) ) {
$res = $dbw->select( 'oldimage',
array( 'oi_archive_name' ),
array( 'oi_name' => $this->file->getName(),
- 'oi_archive_name IN (' . $dbw->makeList( array_keys($oldRels) ) . ')',
- $dbw->bitAnd('oi_deleted', File::DELETED_FILE) => File::DELETED_FILE ),
+ 'oi_archive_name IN (' . $dbw->makeList( array_keys( $oldRels ) ) . ')',
+ $dbw->bitAnd( 'oi_deleted', File::DELETED_FILE ) => File::DELETED_FILE ),
__METHOD__ );
- while( $row = $dbw->fetchObject( $res ) ) {
+
+ foreach ( $res as $row ) {
$privateFiles[$row->oi_archive_name] = 1;
}
}
$this->deletionBatch = array();
$ext = $this->file->getExtension();
$dotExt = $ext === '' ? '' : ".$ext";
+
foreach ( $this->srcRels as $name => $srcRel ) {
// Skip files that have no hash (missing source).
// Keep private files where they are.
// Execute the file deletion batch
$status = $this->file->repo->deleteBatch( $this->deletionBatch );
+
if ( !$status->isGood() ) {
$this->status->merge( $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;
// Commit and return
$this->file->unlock();
wfProfileOut( __METHOD__ );
+
return $this->status;
}
*/
function removeNonexistentFiles( $batch ) {
$files = $newBatch = array();
- foreach( $batch as $batchItem ) {
+
+ foreach ( $batch as $batchItem ) {
list( $src, $dest ) = $batchItem;
$files[$src] = $this->file->repo->getVirtualUrl( 'public' ) . '/' . rawurlencode( $src );
}
+
$result = $this->file->repo->fileExistsBatch( $files, FSRepo::FILES_ONLY );
- foreach( $batch as $batchItem )
- if( $result[$batchItem[0]] )
+
+ foreach ( $batch as $batchItem ) {
+ if ( $result[$batchItem[0]] ) {
$newBatch[] = $batchItem;
+ }
+ }
+
return $newBatch;
}
}
-#------------------------------------------------------------------------------
+# ------------------------------------------------------------------------------
/**
* Helper class for file undeletion
* @ingroup FileRepo
*/
class LocalFileRestoreBatch {
- var $file, $cleanupBatch, $ids, $all, $unsuppress = false;
+ /**
+ * @var LocalFile
+ */
+ var $file;
+
+ var $cleanupBatch, $ids, $all, $unsuppress = false;
function __construct( File $file, $unsuppress = false ) {
$this->file = $file;
*/
function execute() {
global $wgLang;
+
if ( !$this->all && !$this->ids ) {
// Do nothing
return $this->file->repo->newGood();
// Fetch all or selected archived revisions for the file,
// sorted from the most recent to the oldest.
$conditions = array( 'fa_name' => $this->file->getName() );
- if( !$this->all ) {
+
+ if ( !$this->all ) {
$conditions[] = 'fa_id IN (' . $dbw->makeList( $this->ids ) . ')';
}
$deleteIds = array();
$first = true;
$archiveNames = array();
- while( $row = $dbw->fetchObject( $result ) ) {
+
+ foreach ( $result as $row ) {
$idsPresent[] = $row->fa_id;
if ( $row->fa_name != $this->file->getName() ) {
$status->failCount++;
continue;
}
+
if ( $row->fa_storage_key == '' ) {
// Revision was missing pre-deletion
$status->error( 'undelete-bad-store-key', $wgLang->timeanddate( $row->fa_timestamp ) );
$deletedUrl = $this->file->repo->getVirtualUrl() . '/deleted/' . $deletedRel;
$sha1 = substr( $row->fa_storage_key, 0, strcspn( $row->fa_storage_key, '.' ) );
+
# Fix leading zero
if ( strlen( $sha1 ) == 32 && $sha1[0] == '0' ) {
$sha1 = substr( $sha1, 1 );
}
- if( is_null( $row->fa_major_mime ) || $row->fa_major_mime == 'unknown'
+ if ( is_null( $row->fa_major_mime ) || $row->fa_major_mime == 'unknown'
|| is_null( $row->fa_minor_mime ) || $row->fa_minor_mime == 'unknown'
|| is_null( $row->fa_media_type ) || $row->fa_media_type == 'UNKNOWN'
|| is_null( $row->fa_metadata ) ) {
'img_timestamp' => $row->fa_timestamp,
'img_sha1' => $sha1
);
+
// The live (current) version cannot be hidden!
- if( !$this->unsuppress && $row->fa_deleted ) {
+ if ( !$this->unsuppress && $row->fa_deleted ) {
$storeBatch[] = array( $deletedUrl, 'public', $destRel );
$this->cleanupBatch[] = $row->fa_storage_key;
}
} else {
$archiveName = $row->fa_archive_name;
- if( $archiveName == '' ) {
+
+ if ( $archiveName == '' ) {
// This was originally a current version; we
// have to devise a new archive name for it.
// Format is <timestamp of archiving>!<name>
$timestamp = wfTimestamp( TS_UNIX, $row->fa_deleted_timestamp );
+
do {
$archiveName = wfTimestamp( TS_MW, $timestamp ) . '!' . $row->fa_name;
$timestamp++;
} while ( isset( $archiveNames[$archiveName] ) );
}
+
$archiveNames[$archiveName] = true;
$destRel = $this->file->getArchiveRel( $archiveName );
$insertBatch[] = array(
}
$deleteIds[] = $row->fa_id;
- if( !$this->unsuppress && $row->fa_deleted & File::DELETED_FILE ) {
+
+ if ( !$this->unsuppress && $row->fa_deleted & File::DELETED_FILE ) {
// private files can stay where they are
$status->successCount++;
} else {
$storeBatch[] = array( $deletedUrl, 'public', $destRel );
$this->cleanupBatch[] = $row->fa_storage_key;
}
+
$first = false;
}
+
unset( $result );
// Add a warning to the status object for missing IDs
$missingIds = array_diff( $this->ids, $idsPresent );
+
foreach ( $missingIds as $id ) {
$status->error( 'undelete-missing-filearchive', $id );
}
$storeStatus = $this->file->repo->storeBatch( $storeBatch, FileRepo::OVERWRITE_SAME );
$status->merge( $storeStatus );
- if ( !$status->ok ) {
- // Store batch returned a critical error -- this usually means nothing was stored
- // Stop now and return an error
+ if ( !$status->isGood() ) {
+ // Even if some files could be copied, fail entirely as that is the
+ // easiest thing to do without data loss
+ $this->cleanupFailedBatch( $storeStatus, $storeBatch );
+ $status->ok = false;
$this->file->unlock();
+
return $status;
}
if ( $insertCurrent ) {
$dbw->insert( 'image', $insertCurrent, __METHOD__ );
}
+
if ( $insertBatch ) {
$dbw->insert( 'oldimage', $insertBatch, __METHOD__ );
}
+
if ( $deleteIds ) {
$dbw->delete( 'filearchive',
array( 'fa_id IN (' . $dbw->makeList( $deleteIds ) . ')' ),
}
// If store batch is empty (all files are missing), deletion is to be considered successful
- if( $status->successCount > 0 || !$storeBatch ) {
- if( !$exists ) {
+ if ( $status->successCount > 0 || !$storeBatch ) {
+ if ( !$exists ) {
wfDebug( __METHOD__ . " restored {$status->successCount} items, creating a new current\n" );
// Update site_stats
$this->file->purgeHistory();
}
}
+
$this->file->unlock();
+
return $status;
}
*/
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, FSRepo::FILES_ONLY );
- foreach( $triplets as $file )
- if( $result[$file[0]] )
+
+ foreach ( $triplets as $file ) {
+ if ( $result[$file[0]] ) {
$filteredTriplets[] = $file;
+ }
+ }
+
return $filteredTriplets;
}
function removeNonexistentFromCleanup( $batch ) {
$files = $newBatch = array();
$repo = $this->file->repo;
- foreach( $batch as $file ) {
+
+ foreach ( $batch as $file ) {
$files[$file] = $repo->getVirtualUrl( 'deleted' ) . '/' .
rawurlencode( $repo->getDeletedHashPath( $file ) . $file );
}
$result = $repo->fileExistsBatch( $files, FSRepo::FILES_ONLY );
- foreach( $batch as $file )
- if( $result[$file] )
+
+ foreach ( $batch as $file ) {
+ if ( $result[$file] ) {
$newBatch[] = $file;
+ }
+ }
+
return $newBatch;
}
if ( !$this->cleanupBatch ) {
return $this->file->repo->newGood();
}
+
$this->cleanupBatch = $this->removeNonexistentFromCleanup( $this->cleanupBatch );
+
$status = $this->file->repo->cleanupDeletedBatch( $this->cleanupBatch );
+
return $status;
}
+
+ function cleanupFailedBatch( $storeStatus, $storeBatch ) {
+ $cleanupBatch = array();
+
+ foreach ( $storeStatus->success as $i => $success ) {
+ if ( $success ) {
+ $cleanupBatch[] = array( $storeBatch[$i][1], $storeBatch[$i][1] );
+ }
+ }
+ $this->file->repo->cleanupBatch( $cleanupBatch );
+ }
}
-#------------------------------------------------------------------------------
+# ------------------------------------------------------------------------------
/**
* Helper class for file movement
array( 'oi_name' => $this->oldName ),
__METHOD__
);
- while( $row = $this->db->fetchObject( $result ) ) {
+
+ foreach ( $result as $row ) {
$oldName = $row->oi_archive_name;
$bits = explode( '!', $oldName, 2 );
- if( count( $bits ) != 2 ) {
+
+ if ( count( $bits ) != 2 ) {
wfDebug( "Invalid old file name: $oldName \n" );
continue;
}
+
list( $timestamp, $filename ) = $bits;
- if( $this->oldName != $filename ) {
+
+ if ( $this->oldName != $filename ) {
wfDebug( "Invalid old file name: $oldName \n" );
continue;
}
+
$this->oldCount++;
+
// Do we want to add those to oldCount?
- if( $row->oi_deleted & File::DELETED_FILE ) {
+ if ( $row->oi_deleted & File::DELETED_FILE ) {
continue;
}
+
$this->olds[] = array(
"{$archiveBase}/{$this->oldHash}{$oldName}",
"{$archiveBase}/{$this->newHash}{$timestamp}!{$this->newName}"
$triplets = $this->getMoveTriplets();
$triplets = $this->removeNonexistentFiles( $triplets );
- $statusDb = $this->doDBUpdates();
- wfDebugLog( 'imagemove', "Renamed {$this->file->name} in database: {$statusDb->successCount} successes, {$statusDb->failCount} failures" );
- $statusMove = $repo->storeBatch( $triplets, FSRepo::DELETE_SOURCE );
+
+ // Copy the files into their new location
+ $statusMove = $repo->storeBatch( $triplets );
wfDebugLog( 'imagemove', "Moved files for {$this->file->name}: {$statusMove->successCount} successes, {$statusMove->failCount} failures" );
- if( !$statusMove->isOk() ) {
+ if ( !$statusMove->isGood() ) {
wfDebugLog( 'imagemove', "Error in moving files: " . $statusMove->getWikiText() );
- $this->db->rollback();
+ $this->cleanupTarget( $triplets );
+ $statusMove->ok = false;
+ return $statusMove;
}
+ $this->db->begin();
+ $statusDb = $this->doDBUpdates();
+ wfDebugLog( 'imagemove', "Renamed {$this->file->name} 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();
+
+ // Everything went ok, remove the source files
+ $this->cleanupSource( $triplets );
+
$status->merge( $statusDb );
$status->merge( $statusMove );
+
return $status;
}
$dbw = $this->db;
// Update current image
- $dbw->update(
+ $dbw->update(
'image',
array( 'img_name' => $this->newName ),
array( 'img_name' => $this->oldName ),
__METHOD__
);
- if( $dbw->affectedRows() ) {
+
+ if ( $dbw->affectedRows() ) {
$status->successCount++;
} else {
$status->failCount++;
+ $status->fatal( 'imageinvalidfilename' );
+ return $status;
}
// Update old images
'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__
);
+
$affected = $dbw->affectedRows();
$total = $this->oldCount;
$status->successCount += $affected;
$status->failCount += $total - $affected;
+ if ( $status->failCount ) {
+ $status->error( 'imageinvalidfilename' );
+ }
return $status;
}
/**
* Generate triplets for FSRepo::storeBatch().
- */
+ */
function getMoveTriplets() {
$moves = array_merge( array( $this->cur ), $this->olds );
$triplets = array(); // The format is: (srcUrl, destZone, destUrl)
- foreach( $moves as $move ) {
+
+ foreach ( $moves as $move ) {
// $move: (oldRelativePath, newRelativePath)
$srcUrl = $this->file->repo->getVirtualUrl() . '/public/' . rawurlencode( $move[0] );
$triplets[] = array( $srcUrl, 'public', $move[1] );
wfDebugLog( 'imagemove', "Generated move triplet for {$this->file->name}: {$srcUrl} :: public :: {$move[1]}" );
}
+
return $triplets;
}
/**
* Removes non-existent files from move batch.
- */
+ */
function removeNonexistentFiles( $triplets ) {
$files = array();
- foreach( $triplets as $file )
+
+ foreach ( $triplets as $file ) {
$files[$file[0]] = $file[0];
+ }
+
$result = $this->file->repo->fileExistsBatch( $files, FSRepo::FILES_ONLY );
$filteredTriplets = array();
- foreach( $triplets as $file )
- if( $result[$file[0]] ) {
+
+ foreach ( $triplets as $file ) {
+ if ( $result[$file[0]] ) {
$filteredTriplets[] = $file;
} else {
wfDebugLog( 'imagemove', "File {$file[0]} does not exist" );
}
+ }
+
return $filteredTriplets;
}
+
+ /**
+ * Cleanup a partially moved array of triplets by deleting the target
+ * files. Called if something went wrong half way.
+ */
+ function cleanupTarget( $triplets ) {
+ // Create dest pairs from the triplets
+ $pairs = array();
+ foreach ( $triplets as $triplet ) {
+ $pairs[] = array( $triplet[1], $triplet[2] );
+ }
+
+ $this->file->repo->cleanupBatch( $pairs );
+ }
+
+ /**
+ * Cleanup a fully moved array of triplets by deleting the source files.
+ * Called at the end of the move process if everything else went ok.
+ */
+ function cleanupSource( $triplets ) {
+ // Create source file names from the triplets
+ $files = array();
+ foreach ( $triplets as $triplet ) {
+ $files[] = $triplet[0];
+ }
+
+ $this->file->repo->cleanupBatch( $files );
+ }
}