25use Psr\Log\LoggerInterface;
28use Wikimedia\ScopedCallback;
75 private $haveSourceLock =
false;
78 private $haveTargetLock =
false;
90 $this->oldHash = $this->file->repo->getHashPath( $this->file->getName() );
91 $this->newHash = $this->file->repo->getHashPath( $this->target->getDBkey() );
92 $this->oldName = $this->file->getName();
93 $this->newName = $this->file->repo->getNameFromTitle( $this->target );
98 $this->logger = LoggerFactory::getInstance(
'imagemove' );
108 if ( $status->isOK() ) {
119 $archiveBase =
'archive';
124 $result = $this->db->newSelectQueryBuilder()
125 ->select( [
'oi_archive_name',
'oi_deleted' ] )
128 ->where( [
'oi_name' => $this->oldName ] )
129 ->caller( __METHOD__ )->fetchResultSet();
131 foreach ( $result as $row ) {
132 $archiveNames[] = $row->oi_archive_name;
134 $bits = explode(
'!',
$oldName, 2 );
136 if ( count( $bits ) != 2 ) {
137 $this->logger->debug(
138 'Old file name missing !: {oldName}',
144 [ $timestamp, $filename ] = $bits;
146 if ( $this->oldName != $filename ) {
147 $this->logger->debug(
148 'Old file name does not match: {oldName}',
157 if ( $row->oi_deleted & File::DELETED_FILE ) {
162 "{$archiveBase}/{$this->oldHash}{$oldName}",
163 "{$archiveBase}/{$this->newHash}{$timestamp}!{$this->newName}"
167 return $archiveNames;
176 if ( $this->haveSourceLock ) {
177 return Status::newGood();
179 $status = $this->file->acquireFileLock();
180 if ( $status->isOK() ) {
181 $this->haveSourceLock =
true;
192 if ( $this->haveTargetLock ) {
193 return Status::newGood();
196 if ( $status->isOK() ) {
197 $this->haveTargetLock =
true;
206 if ( $this->haveSourceLock ) {
207 $this->file->releaseFileLock();
208 $this->haveSourceLock =
false;
210 if ( $this->haveTargetLock ) {
212 $this->haveTargetLock =
false;
222 if ( $this->targetFile ===
null ) {
223 $this->targetFile = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
224 ->newFile( $this->target );
226 return $this->targetFile;
234 $repo = $this->file->repo;
235 $status = $repo->newGood();
238 if ( !$status->isOK() ) {
242 if ( !$status->isOK() ) {
246 $unlockScope =
new ScopedCallback(
function () {
252 if ( !$checkStatus->isGood() ) {
253 $status->merge( $checkStatus );
256 $triplets = $checkStatus->value;
260 if ( !$statusDb->isGood() ) {
261 $statusDb->setOK(
false );
266 if ( !$repo->hasSha1Storage() ) {
272 $this->logger->debug(
273 'Moved files for {fileName}: {successCount} successes, {failCount} failures',
275 'fileName' => $this->file->getName(),
276 'successCount' => $statusMove->successCount,
277 'failCount' => $statusMove->failCount,
281 if ( !$statusMove->isGood() ) {
285 $this->logger->debug(
286 'Error in moving files: {error}',
287 [
'error' => $statusMove->getWikiText(
false,
false,
'en' ) ]
290 $statusMove->setOK(
false );
294 $status->merge( $statusMove );
300 $this->logger->debug(
301 'Renamed {fileName} in database: {successCount} successes, {failCount} failures',
303 'fileName' => $this->file->getName(),
304 'successCount' => $statusDb->successCount,
305 'failCount' => $statusDb->failCount,
313 if ( $this->db->trxLevel() ) {
314 $unlockScope->cancel();
315 $this->db->onTransactionResolution(
function () {
319 ScopedCallback::consume( $unlockScope );
322 $status->merge( $statusDb );
334 $repo = $this->file->repo;
335 $status = $repo->newGood();
341 ->where( [
'img_name' => $this->oldName ] )
343 ->caller( __METHOD__ )
347 $oldRowCount = $dbw->newSelectQueryBuilder()
349 ->where( [
'oi_name' => $this->oldName ] )
351 ->caller( __METHOD__ )
355 $status->successCount++;
357 $status->failCount++;
359 $status->successCount += $oldRowCount;
363 $status->failCount += max( 0, $this->oldCount - $oldRowCount );
364 if ( $status->failCount ) {
365 $status->error(
'imageinvalidfilename' );
381 ->set( [
'img_name' => $this->newName ] )
382 ->where( [
'img_name' => $this->oldName ] )
383 ->caller( __METHOD__ )->execute();
386 $dbw->newUpdateQueryBuilder()
387 ->update(
'oldimage' )
389 'oi_name' => $this->newName,
390 'oi_archive_name' =>
new RawSQLValue( $dbw->strreplace(
392 $dbw->addQuotes( $this->oldName ),
393 $dbw->addQuotes( $this->newName )
396 ->where( [
'oi_name' => $this->oldName ] )
397 ->caller( __METHOD__ )->execute();
405 $moves = array_merge( [ $this->cur ], $this->olds );
408 foreach ( $moves as $move ) {
410 $srcUrl = $this->file->repo->getVirtualUrl() .
'/public/' . rawurlencode( $move[0] );
411 $triplets[] = [ $srcUrl,
'public', $move[1] ];
413 $this->logger->debug(
414 'Generated move triplet for {fileName}: {srcUrl} :: public :: {move1}',
416 'fileName' => $this->file->getName(),
434 foreach ( $triplets as
$file ) {
438 $result = $this->file->repo->fileExistsBatch( $files );
439 if ( in_array(
null, $result,
true ) ) {
440 return Status::newFatal(
'backend-fail-internal',
441 $this->file->repo->getBackend()->getName() );
444 $filteredTriplets = [];
445 foreach ( $triplets as
$file ) {
446 if ( $result[
$file[0]] ) {
447 $filteredTriplets[] =
$file;
449 $this->logger->debug(
450 'File {file} does not exist',
451 [
'file' =>
$file[0] ]
456 return Status::newGood( $filteredTriplets );
467 foreach ( $triplets as $triplet ) {
469 $pairs[] = [ $triplet[1], $triplet[2] ];
472 $this->file->repo->cleanupBatch( $pairs );
483 foreach ( $triplets as $triplet ) {
484 $files[] = $triplet[0];
487 $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.