25use Psr\Log\LoggerInterface;
27use Wikimedia\ScopedCallback;
74 private $haveSourceLock =
false;
77 private $haveTargetLock =
false;
89 $this->oldHash = $this->file->repo->getHashPath( $this->file->getName() );
90 $this->newHash = $this->file->repo->getHashPath( $this->target->getDBkey() );
91 $this->oldName = $this->file->getName();
92 $this->newName = $this->file->repo->getNameFromTitle( $this->target );
97 $this->logger = LoggerFactory::getInstance(
'imagemove' );
107 if ( $status->isOK() ) {
118 $archiveBase =
'archive';
123 $result = $this->db->newSelectQueryBuilder()
124 ->select( [
'oi_archive_name',
'oi_deleted' ] )
127 ->where( [
'oi_name' => $this->oldName ] )
128 ->caller( __METHOD__ )->fetchResultSet();
130 foreach ( $result as $row ) {
131 $archiveNames[] = $row->oi_archive_name;
133 $bits = explode(
'!',
$oldName, 2 );
135 if ( count( $bits ) != 2 ) {
136 $this->logger->debug(
137 'Old file name missing !: {oldName}',
143 [ $timestamp, $filename ] = $bits;
145 if ( $this->oldName != $filename ) {
146 $this->logger->debug(
147 'Old file name does not match: {oldName}',
156 if ( $row->oi_deleted & File::DELETED_FILE ) {
161 "{$archiveBase}/{$this->oldHash}{$oldName}",
162 "{$archiveBase}/{$this->newHash}{$timestamp}!{$this->newName}"
166 return $archiveNames;
175 if ( $this->haveSourceLock ) {
176 return Status::newGood();
178 $status = $this->file->acquireFileLock();
179 if ( $status->isOK() ) {
180 $this->haveSourceLock =
true;
191 if ( $this->haveTargetLock ) {
192 return Status::newGood();
195 if ( $status->isOK() ) {
196 $this->haveTargetLock =
true;
205 if ( $this->haveSourceLock ) {
206 $this->file->releaseFileLock();
207 $this->haveSourceLock =
false;
209 if ( $this->haveTargetLock ) {
211 $this->haveTargetLock =
false;
221 if ( $this->targetFile ===
null ) {
222 $this->targetFile = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
223 ->newFile( $this->target );
225 return $this->targetFile;
233 $repo = $this->file->repo;
234 $status = $repo->newGood();
237 if ( !$status->isOK() ) {
241 if ( !$status->isOK() ) {
245 $unlockScope =
new ScopedCallback(
function () {
251 if ( !$checkStatus->isGood() ) {
252 $status->merge( $checkStatus );
255 $triplets = $checkStatus->value;
259 if ( !$statusDb->isGood() ) {
260 $statusDb->setOK(
false );
265 if ( !$repo->hasSha1Storage() ) {
271 $this->logger->debug(
272 'Moved files for {fileName}: {successCount} successes, {failCount} failures',
274 'fileName' => $this->file->getName(),
275 'successCount' => $statusMove->successCount,
276 'failCount' => $statusMove->failCount,
280 if ( !$statusMove->isGood() ) {
284 $this->logger->debug(
285 'Error in moving files: {error}',
286 [
'error' => $statusMove->getWikiText(
false,
false,
'en' ) ]
289 $statusMove->setOK(
false );
293 $status->merge( $statusMove );
299 $this->logger->debug(
300 'Renamed {fileName} in database: {successCount} successes, {failCount} failures',
302 'fileName' => $this->file->getName(),
303 'successCount' => $statusDb->successCount,
304 'failCount' => $statusDb->failCount,
312 if ( $this->db->trxLevel() ) {
313 $unlockScope->cancel();
314 $this->db->onTransactionResolution(
function () {
318 ScopedCallback::consume( $unlockScope );
321 $status->merge( $statusDb );
333 $repo = $this->file->repo;
334 $status = $repo->newGood();
340 ->where( [
'img_name' => $this->oldName ] )
342 ->caller( __METHOD__ )
346 $oldRowCount = $dbw->newSelectQueryBuilder()
348 ->where( [
'oi_name' => $this->oldName ] )
350 ->caller( __METHOD__ )
354 $status->successCount++;
356 $status->failCount++;
358 $status->successCount += $oldRowCount;
362 $status->failCount += max( 0, $this->oldCount - $oldRowCount );
363 if ( $status->failCount ) {
364 $status->error(
'imageinvalidfilename' );
380 ->set( [
'img_name' => $this->newName ] )
381 ->where( [
'img_name' => $this->oldName ] )
382 ->caller( __METHOD__ )->execute();
385 $dbw->newUpdateQueryBuilder()
386 ->update(
'oldimage' )
388 'oi_name' => $this->newName,
389 'oi_archive_name = ' . $dbw->strreplace(
391 $dbw->addQuotes( $this->oldName ),
392 $dbw->addQuotes( $this->newName )
395 ->where( [
'oi_name' => $this->oldName ] )
396 ->caller( __METHOD__ )->execute();
404 $moves = array_merge( [ $this->cur ], $this->olds );
407 foreach ( $moves as $move ) {
409 $srcUrl = $this->file->repo->getVirtualUrl() .
'/public/' . rawurlencode( $move[0] );
410 $triplets[] = [ $srcUrl,
'public', $move[1] ];
412 $this->logger->debug(
413 'Generated move triplet for {fileName}: {srcUrl} :: public :: {move1}',
415 'fileName' => $this->file->getName(),
433 foreach ( $triplets as
$file ) {
437 $result = $this->file->repo->fileExistsBatch( $files );
438 if ( in_array(
null, $result,
true ) ) {
439 return Status::newFatal(
'backend-fail-internal',
440 $this->file->repo->getBackend()->getName() );
443 $filteredTriplets = [];
444 foreach ( $triplets as
$file ) {
445 if ( $result[
$file[0]] ) {
446 $filteredTriplets[] =
$file;
448 $this->logger->debug(
449 'File {file} does not exist',
450 [
'file' =>
$file[0] ]
455 return Status::newGood( $filteredTriplets );
466 foreach ( $triplets as $triplet ) {
468 $pairs[] = [ $triplet[1], $triplet[2] ];
471 $this->file->repo->cleanupBatch( $pairs );
482 foreach ( $triplets as $triplet ) {
483 $files[] = $triplet[0];
486 $this->file->repo->cleanupBatch( $files );
Helper class for file movement.
getTargetFile()
Get the target file.
releaseLocks()
Release both file locks.
cleanupTarget( $triplets)
Cleanup a partially moved array of triplets by deleting the target files.
addOlds()
Add the old versions of the image to the batch.
doDBUpdates()
Do the database updates and return a new Status indicating how many rows where updated.
acquireSourceLock()
Acquire the source file lock, if it has not been acquired already.
getMoveTriplets()
Generate triplets for FileRepo::storeBatch().
execute()
Perform the move.
verifyDBUpdates()
Verify the database updates and return a new Status indicating how many rows would be updated.
removeNonexistentFiles( $triplets)
Removes non-existent files from move batch.
__construct(LocalFile $file, Title $target)
acquireTargetLock()
Acquire the target file lock, if it has not been acquired already.
addCurrent()
Add the current image to the batch.
cleanupSource( $triplets)
Cleanup a fully moved array of triplets by deleting the source files.
Local file in the wiki's own database.