| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Logger\LoggerFactory; |
| 22 |
use MediaWiki\MediaWikiServices; |
| 23 |
use MediaWiki\Status\Status; |
| 24 |
use MediaWiki\Title\Title; |
| 25 |
use Psr\Log\LoggerInterface; |
| 26 |
use Wikimedia\Rdbms\IDatabase; |
| 27 |
use Wikimedia\Rdbms\RawSQLValue; |
| 28 |
use Wikimedia\ScopedCallback; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class LocalFileMoveBatch { |
| 36 |
|
| 37 |
protected $file; |
| 38 |
|
| 39 |
|
| 40 |
protected $target; |
| 41 |
|
| 42 |
|
| 43 |
protected $cur; |
| 44 |
|
| 45 |
|
| 46 |
protected $olds; |
| 47 |
|
| 48 |
|
| 49 |
protected $oldCount; |
| 50 |
|
| 51 |
|
| 52 |
protected $db; |
| 53 |
|
| 54 |
|
| 55 |
protected $oldHash; |
| 56 |
|
| 57 |
|
| 58 |
protected $newHash; |
| 59 |
|
| 60 |
|
| 61 |
protected $oldName; |
| 62 |
|
| 63 |
|
| 64 |
protected $newName; |
| 65 |
|
| 66 |
|
| 67 |
protected $oldRel; |
| 68 |
|
| 69 |
|
| 70 |
protected $newRel; |
| 71 |
|
| 72 |
|
| 73 |
private $logger; |
| 74 |
|
| 75 |
|
| 76 |
private $haveSourceLock = false; |
| 77 |
|
| 78 |
|
| 79 |
private $haveTargetLock = false; |
| 80 |
|
| 81 |
|
| 82 |
private $targetFile; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function __construct( LocalFile $file, Title $target ) { |
| 89 |
$this->file = $file; |
| 90 |
$this->target = $target; |
| 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 ); |
| 95 |
$this->oldRel = $this->oldHash . $this->oldName; |
| 96 |
$this->newRel = $this->newHash . $this->newName; |
| 97 |
$this->db = $file->getRepo()->getPrimaryDB(); |
| 98 |
|
| 99 |
$this->logger = LoggerFactory::getInstance( 'imagemove' ); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public function addCurrent() { |
| 108 |
$status = $this->acquireSourceLock(); |
| 109 |
if ( $status->isOK() ) { |
| 110 |
$this->cur = [ $this->oldRel, $this->newRel ]; |
| 111 |
} |
| 112 |
return $status; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function addOlds() { |
| 120 |
$archiveBase = 'archive'; |
| 121 |
$this->olds = []; |
| 122 |
$this->oldCount = 0; |
| 123 |
$archiveNames = []; |
| 124 |
|
| 125 |
$result = $this->db->newSelectQueryBuilder() |
| 126 |
->select( [ 'oi_archive_name', 'oi_deleted' ] ) |
| 127 |
->forUpdate() |
| 128 |
->from( 'oldimage' ) |
| 129 |
->where( [ 'oi_name' => $this->oldName ] ) |
| 130 |
->caller( __METHOD__ )->fetchResultSet(); |
| 131 |
|
| 132 |
foreach ( $result as $row ) { |
| 133 |
$archiveNames[] = $row->oi_archive_name; |
| 134 |
$oldName = $row->oi_archive_name; |
| 135 |
$bits = explode( '!', $oldName, 2 ); |
| 136 |
|
| 137 |
if ( count( $bits ) != 2 ) { |
| 138 |
$this->logger->debug( |
| 139 |
'Old file name missing !: {oldName}', |
| 140 |
[ 'oldName' => $oldName ] |
| 141 |
); |
| 142 |
continue; |
| 143 |
} |
| 144 |
|
| 145 |
[ $timestamp, $filename ] = $bits; |
| 146 |
|
| 147 |
if ( $this->oldName != $filename ) { |
| 148 |
$this->logger->debug( |
| 149 |
'Old file name does not match: {oldName}', |
| 150 |
[ 'oldName' => $oldName ] |
| 151 |
); |
| 152 |
continue; |
| 153 |
} |
| 154 |
|
| 155 |
$this->oldCount++; |
| 156 |
|
| 157 |
|
| 158 |
if ( $row->oi_deleted & File::DELETED_FILE ) { |
| 159 |
continue; |
| 160 |
} |
| 161 |
|
| 162 |
$this->olds[] = [ |
| 163 |
"{$archiveBase}/{$this->oldHash}{$oldName}", |
| 164 |
"{$archiveBase}/{$this->newHash}{$timestamp}!{$this->newName}" |
| 165 |
]; |
| 166 |
} |
| 167 |
|
| 168 |
return $archiveNames; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
protected function acquireSourceLock() { |
| 177 |
if ( $this->haveSourceLock ) { |
| 178 |
return Status::newGood(); |
| 179 |
} |
| 180 |
$status = $this->file->acquireFileLock(); |
| 181 |
if ( $status->isOK() ) { |
| 182 |
$this->haveSourceLock = true; |
| 183 |
} |
| 184 |
return $status; |
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
protected function acquireTargetLock() { |
| 193 |
if ( $this->haveTargetLock ) { |
| 194 |
return Status::newGood(); |
| 195 |
} |
| 196 |
$status = $this->getTargetFile()->acquireFileLock(); |
| 197 |
if ( $status->isOK() ) { |
| 198 |
$this->haveTargetLock = true; |
| 199 |
} |
| 200 |
return $status; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
protected function releaseLocks() { |
| 207 |
if ( $this->haveSourceLock ) { |
| 208 |
$this->file->releaseFileLock(); |
| 209 |
$this->haveSourceLock = false; |
| 210 |
} |
| 211 |
if ( $this->haveTargetLock ) { |
| 212 |
$this->getTargetFile()->releaseFileLock(); |
| 213 |
$this->haveTargetLock = false; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
protected function getTargetFile() { |
| 223 |
if ( $this->targetFile === null ) { |
| 224 |
$this->targetFile = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo() |
| 225 |
->newFile( $this->target ); |
| 226 |
} |
| 227 |
return $this->targetFile; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
public function execute() { |
| 235 |
$repo = $this->file->repo; |
| 236 |
$status = $repo->newGood(); |
| 237 |
|
| 238 |
$status->merge( $this->acquireSourceLock() ); |
| 239 |
if ( !$status->isOK() ) { |
| 240 |
return $status; |
| 241 |
} |
| 242 |
$status->merge( $this->acquireTargetLock() ); |
| 243 |
if ( !$status->isOK() ) { |
| 244 |
$this->releaseLocks(); |
| 245 |
return $status; |
| 246 |
} |
| 247 |
$unlockScope = new ScopedCallback( function () { |
| 248 |
$this->releaseLocks(); |
| 249 |
} ); |
| 250 |
|
| 251 |
$triplets = $this->getMoveTriplets(); |
| 252 |
$checkStatus = $this->removeNonexistentFiles( $triplets ); |
| 253 |
if ( !$checkStatus->isGood() ) { |
| 254 |
$status->merge( $checkStatus ); |
| 255 |
return $status; |
| 256 |
} |
| 257 |
$triplets = $checkStatus->value; |
| 258 |
|
| 259 |
|
| 260 |
$statusDb = $this->verifyDBUpdates(); |
| 261 |
if ( !$statusDb->isGood() ) { |
| 262 |
$statusDb->setOK( false ); |
| 263 |
|
| 264 |
return $statusDb; |
| 265 |
} |
| 266 |
|
| 267 |
if ( !$repo->hasSha1Storage() ) { |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
$statusMove = $repo->storeBatch( $triplets, FileRepo::OVERWRITE_SAME ); |
| 272 |
|
| 273 |
$this->logger->debug( |
| 274 |
'Moved files for {fileName}: {successCount} successes, {failCount} failures', |
| 275 |
[ |
| 276 |
'fileName' => $this->file->getName(), |
| 277 |
'successCount' => $statusMove->successCount, |
| 278 |
'failCount' => $statusMove->failCount, |
| 279 |
] |
| 280 |
); |
| 281 |
|
| 282 |
if ( !$statusMove->isGood() ) { |
| 283 |
|
| 284 |
$this->cleanupTarget( $triplets ); |
| 285 |
|
| 286 |
$this->logger->debug( |
| 287 |
'Error in moving files: {error}', |
| 288 |
[ 'error' => $statusMove->getWikiText( false, false, 'en' ) ] |
| 289 |
); |
| 290 |
|
| 291 |
$statusMove->setOK( false ); |
| 292 |
|
| 293 |
return $statusMove; |
| 294 |
} |
| 295 |
$status->merge( $statusMove ); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
$this->doDBUpdates(); |
| 300 |
|
| 301 |
$this->logger->debug( |
| 302 |
'Renamed {fileName} in database: {successCount} successes, {failCount} failures', |
| 303 |
[ |
| 304 |
'fileName' => $this->file->getName(), |
| 305 |
'successCount' => $statusDb->successCount, |
| 306 |
'failCount' => $statusDb->failCount, |
| 307 |
] |
| 308 |
); |
| 309 |
|
| 310 |
|
| 311 |
$this->cleanupSource( $triplets ); |
| 312 |
|
| 313 |
|
| 314 |
if ( $this->db->trxLevel() ) { |
| 315 |
ScopedCallback::cancel( $unlockScope ); |
| 316 |
$this->db->onTransactionResolution( function () { |
| 317 |
$this->releaseLocks(); |
| 318 |
}, __METHOD__ ); |
| 319 |
} else { |
| 320 |
ScopedCallback::consume( $unlockScope ); |
| 321 |
} |
| 322 |
|
| 323 |
$status->merge( $statusDb ); |
| 324 |
|
| 325 |
return $status; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
protected function verifyDBUpdates() { |
| 335 |
$repo = $this->file->repo; |
| 336 |
$status = $repo->newGood(); |
| 337 |
$dbw = $this->db; |
| 338 |
|
| 339 |
|
| 340 |
$hasCurrent = $dbw->newSelectQueryBuilder() |
| 341 |
->from( 'image' ) |
| 342 |
->where( [ 'img_name' => $this->oldName ] ) |
| 343 |
->forUpdate() |
| 344 |
->caller( __METHOD__ ) |
| 345 |
->fetchRowCount(); |
| 346 |
|
| 347 |
|
| 348 |
$oldRowCount = $dbw->newSelectQueryBuilder() |
| 349 |
->from( 'oldimage' ) |
| 350 |
->where( [ 'oi_name' => $this->oldName ] ) |
| 351 |
->forUpdate() |
| 352 |
->caller( __METHOD__ ) |
| 353 |
->fetchRowCount(); |
| 354 |
|
| 355 |
if ( $hasCurrent ) { |
| 356 |
$status->successCount++; |
| 357 |
} else { |
| 358 |
$status->failCount++; |
| 359 |
} |
| 360 |
$status->successCount += $oldRowCount; |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
$status->failCount += max( 0, $this->oldCount - $oldRowCount ); |
| 365 |
if ( $status->failCount ) { |
| 366 |
$status->error( 'imageinvalidfilename' ); |
| 367 |
} |
| 368 |
|
| 369 |
return $status; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
protected function doDBUpdates() { |
| 377 |
$dbw = $this->db; |
| 378 |
|
| 379 |
|
| 380 |
$dbw->newUpdateQueryBuilder() |
| 381 |
->update( 'image' ) |
| 382 |
->set( [ 'img_name' => $this->newName ] ) |
| 383 |
->where( [ 'img_name' => $this->oldName ] ) |
| 384 |
->caller( __METHOD__ )->execute(); |
| 385 |
|
| 386 |
|
| 387 |
$dbw->newUpdateQueryBuilder() |
| 388 |
->update( 'oldimage' ) |
| 389 |
->set( [ |
| 390 |
'oi_name' => $this->newName, |
| 391 |
'oi_archive_name' => new RawSQLValue( $dbw->strreplace( |
| 392 |
'oi_archive_name', |
| 393 |
$dbw->addQuotes( $this->oldName ), |
| 394 |
$dbw->addQuotes( $this->newName ) |
| 395 |
) ), |
| 396 |
] ) |
| 397 |
->where( [ 'oi_name' => $this->oldName ] ) |
| 398 |
->caller( __METHOD__ )->execute(); |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
protected function getMoveTriplets() { |
| 406 |
$moves = array_merge( [ $this->cur ], $this->olds ); |
| 407 |
$triplets = []; |
| 408 |
|
| 409 |
foreach ( $moves as $move ) { |
| 410 |
|
| 411 |
$srcUrl = $this->file->repo->getVirtualUrl() . '/public/' . rawurlencode( $move[0] ); |
| 412 |
$triplets[] = [ $srcUrl, 'public', $move[1] ]; |
| 413 |
|
| 414 |
$this->logger->debug( |
| 415 |
'Generated move triplet for {fileName}: {srcUrl} :: public :: {move1}', |
| 416 |
[ |
| 417 |
'fileName' => $this->file->getName(), |
| 418 |
'srcUrl' => $srcUrl, |
| 419 |
'move1' => $move[1], |
| 420 |
] |
| 421 |
); |
| 422 |
} |
| 423 |
|
| 424 |
return $triplets; |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
protected function removeNonexistentFiles( $triplets ) { |
| 433 |
$files = []; |
| 434 |
|
| 435 |
foreach ( $triplets as $file ) { |
| 436 |
$files[$file[0]] = $file[0]; |
| 437 |
} |
| 438 |
|
| 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() ); |
| 443 |
} |
| 444 |
|
| 445 |
$filteredTriplets = []; |
| 446 |
foreach ( $triplets as $file ) { |
| 447 |
if ( $result[$file[0]] ) { |
| 448 |
$filteredTriplets[] = $file; |
| 449 |
} else { |
| 450 |
$this->logger->debug( |
| 451 |
'File {file} does not exist', |
| 452 |
[ 'file' => $file[0] ] |
| 453 |
); |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
return Status::newGood( $filteredTriplets ); |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
protected function cleanupTarget( $triplets ) { |
| 466 |
|
| 467 |
$pairs = []; |
| 468 |
foreach ( $triplets as $triplet ) { |
| 469 |
|
| 470 |
$pairs[] = [ $triplet[1], $triplet[2] ]; |
| 471 |
} |
| 472 |
|
| 473 |
$this->file->repo->cleanupBatch( $pairs ); |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
protected function cleanupSource( $triplets ) { |
| 482 |
|
| 483 |
$files = []; |
| 484 |
foreach ( $triplets as $triplet ) { |
| 485 |
$files[] = $triplet[0]; |
| 486 |
} |
| 487 |
|
| 488 |
$this->file->repo->cleanupBatch( $files ); |
| 489 |
} |
| 490 |
} |