3 * Local file in the wiki's own database.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup FileAbstraction
24 use MediaWiki\MediaWikiServices
;
25 use Wikimedia\Rdbms\IDatabase
;
28 * Helper class for file movement
29 * @ingroup FileAbstraction
31 class LocalFileMoveBatch
{
69 * @param Title $target
71 function __construct( File
$file, Title
$target ) {
73 $this->target
= $target;
74 $this->oldHash
= $this->file
->repo
->getHashPath( $this->file
->getName() );
75 $this->newHash
= $this->file
->repo
->getHashPath( $this->target
->getDBkey() );
76 $this->oldName
= $this->file
->getName();
77 $this->newName
= $this->file
->repo
->getNameFromTitle( $this->target
);
78 $this->oldRel
= $this->oldHash
. $this->oldName
;
79 $this->newRel
= $this->newHash
. $this->newName
;
80 $this->db
= $file->getRepo()->getMasterDB();
84 * Add the current image to the batch
86 public function addCurrent() {
87 $this->cur
= [ $this->oldRel
, $this->newRel
];
91 * Add the old versions of the image to the batch
92 * @return string[] List of archive names from old versions
94 public function addOlds() {
95 $archiveBase = 'archive';
100 $result = $this->db
->select( 'oldimage',
101 [ 'oi_archive_name', 'oi_deleted' ],
102 [ 'oi_name' => $this->oldName
],
104 [ 'LOCK IN SHARE MODE' ] // ignore snapshot
107 foreach ( $result as $row ) {
108 $archiveNames[] = $row->oi_archive_name
;
109 $oldName = $row->oi_archive_name
;
110 $bits = explode( '!', $oldName, 2 );
112 if ( count( $bits ) != 2 ) {
113 wfDebug( "Old file name missing !: '$oldName' \n" );
117 list( $timestamp, $filename ) = $bits;
119 if ( $this->oldName
!= $filename ) {
120 wfDebug( "Old file name doesn't match: '$oldName' \n" );
126 // Do we want to add those to oldCount?
127 if ( $row->oi_deleted
& File
::DELETED_FILE
) {
132 "{$archiveBase}/{$this->oldHash}{$oldName}",
133 "{$archiveBase}/{$this->newHash}{$timestamp}!{$this->newName}"
137 return $archiveNames;
144 public function execute() {
145 $repo = $this->file
->repo
;
146 $status = $repo->newGood();
147 $destFile = MediaWikiServices
::getInstance()->getRepoGroup()->getLocalRepo()
148 ->newFile( $this->target
);
151 $destFile->lock(); // quickly fail if destination is not available
153 $triplets = $this->getMoveTriplets();
154 $checkStatus = $this->removeNonexistentFiles( $triplets );
155 if ( !$checkStatus->isGood() ) {
157 $this->file
->unlock();
158 $status->merge( $checkStatus ); // couldn't talk to file backend
161 $triplets = $checkStatus->value
;
163 // Verify the file versions metadata in the DB.
164 $statusDb = $this->verifyDBUpdates();
165 if ( !$statusDb->isGood() ) {
167 $this->file
->unlock();
168 $statusDb->setOK( false );
173 if ( !$repo->hasSha1Storage() ) {
174 // Copy the files into their new location.
175 // If a prior process fataled copying or cleaning up files we tolerate any
176 // of the existing files if they are identical to the ones being stored.
177 $statusMove = $repo->storeBatch( $triplets, FileRepo
::OVERWRITE_SAME
);
178 wfDebugLog( 'imagemove', "Moved files for {$this->file->getName()}: " .
179 "{$statusMove->successCount} successes, {$statusMove->failCount} failures" );
180 if ( !$statusMove->isGood() ) {
181 // Delete any files copied over (while the destination is still locked)
182 $this->cleanupTarget( $triplets );
184 $this->file
->unlock();
185 wfDebugLog( 'imagemove', "Error in moving files: "
186 . $statusMove->getWikiText( false, false, 'en' ) );
187 $statusMove->setOK( false );
191 $status->merge( $statusMove );
194 // Rename the file versions metadata in the DB.
195 $this->doDBUpdates();
197 wfDebugLog( 'imagemove', "Renamed {$this->file->getName()} in database: " .
198 "{$statusDb->successCount} successes, {$statusDb->failCount} failures" );
201 $this->file
->unlock();
203 // Everything went ok, remove the source files
204 $this->cleanupSource( $triplets );
206 $status->merge( $statusDb );
212 * Verify the database updates and return a new Status indicating how
213 * many rows would be updated.
217 protected function verifyDBUpdates() {
218 $repo = $this->file
->repo
;
219 $status = $repo->newGood();
222 $hasCurrent = $dbw->lockForUpdate(
224 [ 'img_name' => $this->oldName
],
227 $oldRowCount = $dbw->lockForUpdate(
229 [ 'oi_name' => $this->oldName
],
234 $status->successCount++
;
236 $status->failCount++
;
238 $status->successCount +
= $oldRowCount;
239 // T36934: oldCount is based on files that actually exist.
240 // There may be more DB rows than such files, in which case $affected
241 // can be greater than $total. We use max() to avoid negatives here.
242 $status->failCount +
= max( 0, $this->oldCount
- $oldRowCount );
243 if ( $status->failCount
) {
244 $status->error( 'imageinvalidfilename' );
251 * Do the database updates and return a new Status indicating how
252 * many rows where updated.
254 protected function doDBUpdates() {
257 // Update current image
260 [ 'img_name' => $this->newName
],
261 [ 'img_name' => $this->oldName
],
269 'oi_name' => $this->newName
,
270 'oi_archive_name = ' . $dbw->strreplace( 'oi_archive_name',
271 $dbw->addQuotes( $this->oldName
), $dbw->addQuotes( $this->newName
) ),
273 [ 'oi_name' => $this->oldName
],
279 * Generate triplets for FileRepo::storeBatch().
282 protected function getMoveTriplets() {
283 $moves = array_merge( [ $this->cur
], $this->olds
);
284 $triplets = []; // The format is: (srcUrl, destZone, destUrl)
286 foreach ( $moves as $move ) {
287 // $move: (oldRelativePath, newRelativePath)
288 $srcUrl = $this->file
->repo
->getVirtualUrl() . '/public/' . rawurlencode( $move[0] );
289 $triplets[] = [ $srcUrl, 'public', $move[1] ];
292 "Generated move triplet for {$this->file->getName()}: {$srcUrl} :: public :: {$move[1]}"
300 * Removes non-existent files from move batch.
301 * @param array $triplets
304 protected function removeNonexistentFiles( $triplets ) {
307 foreach ( $triplets as $file ) {
308 $files[$file[0]] = $file[0];
311 $result = $this->file
->repo
->fileExistsBatch( $files );
312 if ( in_array( null, $result, true ) ) {
313 return Status
::newFatal( 'backend-fail-internal',
314 $this->file
->repo
->getBackend()->getName() );
317 $filteredTriplets = [];
318 foreach ( $triplets as $file ) {
319 if ( $result[$file[0]] ) {
320 $filteredTriplets[] = $file;
322 wfDebugLog( 'imagemove', "File {$file[0]} does not exist" );
326 return Status
::newGood( $filteredTriplets );
330 * Cleanup a partially moved array of triplets by deleting the target
331 * files. Called if something went wrong half way.
332 * @param array[] $triplets
334 protected function cleanupTarget( $triplets ) {
335 // Create dest pairs from the triplets
337 foreach ( $triplets as $triplet ) {
338 // $triplet: (old source virtual URL, dst zone, dest rel)
339 $pairs[] = [ $triplet[1], $triplet[2] ];
342 $this->file
->repo
->cleanupBatch( $pairs );
346 * Cleanup a fully moved array of triplets by deleting the source files.
347 * Called at the end of the move process if everything else went ok.
348 * @param array[] $triplets
350 protected function cleanupSource( $triplets ) {
351 // Create source file names from the triplets
353 foreach ( $triplets as $triplet ) {
354 $files[] = $triplet[0];
357 $this->file
->repo
->cleanupBatch( $files );