25use Psr\Log\LoggerInterface;
28use Wikimedia\ScopedCallback;
76 private $haveSourceLock =
false;
79 private $haveTargetLock =
false;
91 $this->oldHash = $this->file->repo->getHashPath( $this->file->getName() );
92 $this->newHash = $this->file->repo->getHashPath( $this->target->getDBkey() );
93 $this->oldName = $this->file->getName();
94 $this->newName = $this->file->repo->getNameFromTitle( $this->target );
99 $this->logger = LoggerFactory::getInstance(
'imagemove' );
109 if ( $status->isOK() ) {
120 $archiveBase =
'archive';
125 $result = $this->db->newSelectQueryBuilder()
126 ->select( [
'oi_archive_name',
'oi_deleted' ] )
129 ->where( [
'oi_name' => $this->oldName ] )
130 ->caller( __METHOD__ )->fetchResultSet();
132 foreach ( $result as $row ) {
133 $archiveNames[] = $row->oi_archive_name;
135 $bits = explode(
'!',
$oldName, 2 );
137 if ( count( $bits ) != 2 ) {
138 $this->logger->debug(
139 'Old file name missing !: {oldName}',
145 [ $timestamp, $filename ] = $bits;
147 if ( $this->oldName != $filename ) {
148 $this->logger->debug(
149 'Old file name does not match: {oldName}',
158 if ( $row->oi_deleted & File::DELETED_FILE ) {
163 "{$archiveBase}/{$this->oldHash}{$oldName}",
164 "{$archiveBase}/{$this->newHash}{$timestamp}!{$this->newName}"
168 return $archiveNames;
177 if ( $this->haveSourceLock ) {
178 return Status::newGood();
180 $status = $this->file->acquireFileLock();
181 if ( $status->isOK() ) {
182 $this->haveSourceLock =
true;
193 if ( $this->haveTargetLock ) {
194 return Status::newGood();
197 if ( $status->isOK() ) {
198 $this->haveTargetLock =
true;
207 if ( $this->haveSourceLock ) {
208 $this->file->releaseFileLock();
209 $this->haveSourceLock =
false;
211 if ( $this->haveTargetLock ) {
213 $this->haveTargetLock =
false;
223 if ( $this->targetFile ===
null ) {
224 $this->targetFile = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
225 ->newFile( $this->target );
227 return $this->targetFile;
235 $repo = $this->file->repo;
236 $status = $repo->newGood();
239 if ( !$status->isOK() ) {
243 if ( !$status->isOK() ) {
247 $unlockScope =
new ScopedCallback(
function () {
253 if ( !$checkStatus->isGood() ) {
254 $status->merge( $checkStatus );
257 $triplets = $checkStatus->value;
261 if ( !$statusDb->isGood() ) {
262 $statusDb->setOK(
false );
267 if ( !$repo->hasSha1Storage() ) {
273 $this->logger->debug(
274 'Moved files for {fileName}: {successCount} successes, {failCount} failures',
276 'fileName' => $this->file->getName(),
277 'successCount' => $statusMove->successCount,
278 'failCount' => $statusMove->failCount,
282 if ( !$statusMove->isGood() ) {
286 $this->logger->debug(
287 'Error in moving files: {error}',
288 [
'error' => $statusMove->getWikiText(
false,
false,
'en' ) ]
291 $statusMove->setOK(
false );
295 $status->merge( $statusMove );
301 $this->logger->debug(
302 'Renamed {fileName} in database: {successCount} successes, {failCount} failures',
304 'fileName' => $this->file->getName(),
305 'successCount' => $statusDb->successCount,
306 'failCount' => $statusDb->failCount,
314 if ( $this->db->trxLevel() ) {
315 $unlockScope->cancel();
316 $this->db->onTransactionResolution(
function () {
320 ScopedCallback::consume( $unlockScope );
323 $status->merge( $statusDb );
335 $repo = $this->file->repo;
336 $status = $repo->newGood();
342 ->where( [
'img_name' => $this->oldName ] )
344 ->caller( __METHOD__ )
348 $oldRowCount = $dbw->newSelectQueryBuilder()
350 ->where( [
'oi_name' => $this->oldName ] )
352 ->caller( __METHOD__ )
356 $status->successCount++;
358 $status->failCount++;
360 $status->successCount += $oldRowCount;
364 $status->failCount += max( 0, $this->oldCount - $oldRowCount );
365 if ( $status->failCount ) {
366 $status->error(
'imageinvalidfilename' );
382 ->set( [
'img_name' => $this->newName ] )
383 ->where( [
'img_name' => $this->oldName ] )
384 ->caller( __METHOD__ )->execute();
387 $dbw->newUpdateQueryBuilder()
388 ->update(
'oldimage' )
390 'oi_name' => $this->newName,
391 'oi_archive_name' =>
new RawSQLValue( $dbw->strreplace(
393 $dbw->addQuotes( $this->oldName ),
394 $dbw->addQuotes( $this->newName )
397 ->where( [
'oi_name' => $this->oldName ] )
398 ->caller( __METHOD__ )->execute();
406 $moves = array_merge( [ $this->cur ], $this->olds );
409 foreach ( $moves as $move ) {
411 $srcUrl = $this->file->repo->getVirtualUrl() .
'/public/' . rawurlencode( $move[0] );
412 $triplets[] = [ $srcUrl,
'public', $move[1] ];
414 $this->logger->debug(
415 'Generated move triplet for {fileName}: {srcUrl} :: public :: {move1}',
417 'fileName' => $this->file->getName(),
435 foreach ( $triplets as
$file ) {
439 $result = $this->file->repo->fileExistsBatch( $files );
440 if ( in_array(
null, $result,
true ) ) {
441 return Status::newFatal(
'backend-fail-internal',
442 $this->file->repo->getBackend()->getName() );
445 $filteredTriplets = [];
446 foreach ( $triplets as
$file ) {
447 if ( $result[
$file[0]] ) {
448 $filteredTriplets[] =
$file;
450 $this->logger->debug(
451 'File {file} does not exist',
452 [
'file' =>
$file[0] ]
457 return Status::newGood( $filteredTriplets );
468 foreach ( $triplets as $triplet ) {
470 $pairs[] = [ $triplet[1], $triplet[2] ];
473 $this->file->repo->cleanupBatch( $pairs );
484 foreach ( $triplets as $triplet ) {
485 $files[] = $triplet[0];
488 $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.