Merge "Make CdnCacheUpdate::expand() private and update the sole caller"
[lhc/web/wiklou.git] / includes / filerepo / file / LocalFileMoveBatch.php
1 <?php
2 /**
3 * Local file in the wiki's own database.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup FileAbstraction
22 */
23
24 use MediaWiki\MediaWikiServices;
25 use Wikimedia\Rdbms\IDatabase;
26
27 /**
28 * Helper class for file movement
29 * @ingroup FileAbstraction
30 */
31 class LocalFileMoveBatch {
32 /** @var LocalFile */
33 protected $file;
34
35 /** @var Title */
36 protected $target;
37
38 protected $cur;
39
40 protected $olds;
41
42 protected $oldCount;
43
44 protected $archive;
45
46 /** @var IDatabase */
47 protected $db;
48
49 /**
50 * @param File $file
51 * @param Title $target
52 */
53 function __construct( File $file, Title $target ) {
54 $this->file = $file;
55 $this->target = $target;
56 $this->oldHash = $this->file->repo->getHashPath( $this->file->getName() );
57 $this->newHash = $this->file->repo->getHashPath( $this->target->getDBkey() );
58 $this->oldName = $this->file->getName();
59 $this->newName = $this->file->repo->getNameFromTitle( $this->target );
60 $this->oldRel = $this->oldHash . $this->oldName;
61 $this->newRel = $this->newHash . $this->newName;
62 $this->db = $file->getRepo()->getMasterDB();
63 }
64
65 /**
66 * Add the current image to the batch
67 */
68 public function addCurrent() {
69 $this->cur = [ $this->oldRel, $this->newRel ];
70 }
71
72 /**
73 * Add the old versions of the image to the batch
74 * @return string[] List of archive names from old versions
75 */
76 public function addOlds() {
77 $archiveBase = 'archive';
78 $this->olds = [];
79 $this->oldCount = 0;
80 $archiveNames = [];
81
82 $result = $this->db->select( 'oldimage',
83 [ 'oi_archive_name', 'oi_deleted' ],
84 [ 'oi_name' => $this->oldName ],
85 __METHOD__,
86 [ 'LOCK IN SHARE MODE' ] // ignore snapshot
87 );
88
89 foreach ( $result as $row ) {
90 $archiveNames[] = $row->oi_archive_name;
91 $oldName = $row->oi_archive_name;
92 $bits = explode( '!', $oldName, 2 );
93
94 if ( count( $bits ) != 2 ) {
95 wfDebug( "Old file name missing !: '$oldName' \n" );
96 continue;
97 }
98
99 list( $timestamp, $filename ) = $bits;
100
101 if ( $this->oldName != $filename ) {
102 wfDebug( "Old file name doesn't match: '$oldName' \n" );
103 continue;
104 }
105
106 $this->oldCount++;
107
108 // Do we want to add those to oldCount?
109 if ( $row->oi_deleted & File::DELETED_FILE ) {
110 continue;
111 }
112
113 $this->olds[] = [
114 "{$archiveBase}/{$this->oldHash}{$oldName}",
115 "{$archiveBase}/{$this->newHash}{$timestamp}!{$this->newName}"
116 ];
117 }
118
119 return $archiveNames;
120 }
121
122 /**
123 * Perform the move.
124 * @return Status
125 */
126 public function execute() {
127 $repo = $this->file->repo;
128 $status = $repo->newGood();
129 $destFile = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
130 ->newFile( $this->target );
131
132 $this->file->lock();
133 $destFile->lock(); // quickly fail if destination is not available
134
135 $triplets = $this->getMoveTriplets();
136 $checkStatus = $this->removeNonexistentFiles( $triplets );
137 if ( !$checkStatus->isGood() ) {
138 $destFile->unlock();
139 $this->file->unlock();
140 $status->merge( $checkStatus ); // couldn't talk to file backend
141 return $status;
142 }
143 $triplets = $checkStatus->value;
144
145 // Verify the file versions metadata in the DB.
146 $statusDb = $this->verifyDBUpdates();
147 if ( !$statusDb->isGood() ) {
148 $destFile->unlock();
149 $this->file->unlock();
150 $statusDb->setOK( false );
151
152 return $statusDb;
153 }
154
155 if ( !$repo->hasSha1Storage() ) {
156 // Copy the files into their new location.
157 // If a prior process fataled copying or cleaning up files we tolerate any
158 // of the existing files if they are identical to the ones being stored.
159 $statusMove = $repo->storeBatch( $triplets, FileRepo::OVERWRITE_SAME );
160 wfDebugLog( 'imagemove', "Moved files for {$this->file->getName()}: " .
161 "{$statusMove->successCount} successes, {$statusMove->failCount} failures" );
162 if ( !$statusMove->isGood() ) {
163 // Delete any files copied over (while the destination is still locked)
164 $this->cleanupTarget( $triplets );
165 $destFile->unlock();
166 $this->file->unlock();
167 wfDebugLog( 'imagemove', "Error in moving files: "
168 . $statusMove->getWikiText( false, false, 'en' ) );
169 $statusMove->setOK( false );
170
171 return $statusMove;
172 }
173 $status->merge( $statusMove );
174 }
175
176 // Rename the file versions metadata in the DB.
177 $this->doDBUpdates();
178
179 wfDebugLog( 'imagemove', "Renamed {$this->file->getName()} in database: " .
180 "{$statusDb->successCount} successes, {$statusDb->failCount} failures" );
181
182 $destFile->unlock();
183 $this->file->unlock();
184
185 // Everything went ok, remove the source files
186 $this->cleanupSource( $triplets );
187
188 $status->merge( $statusDb );
189
190 return $status;
191 }
192
193 /**
194 * Verify the database updates and return a new Status indicating how
195 * many rows would be updated.
196 *
197 * @return Status
198 */
199 protected function verifyDBUpdates() {
200 $repo = $this->file->repo;
201 $status = $repo->newGood();
202 $dbw = $this->db;
203
204 $hasCurrent = $dbw->lockForUpdate(
205 'image',
206 [ 'img_name' => $this->oldName ],
207 __METHOD__
208 );
209 $oldRowCount = $dbw->lockForUpdate(
210 'oldimage',
211 [ 'oi_name' => $this->oldName ],
212 __METHOD__
213 );
214
215 if ( $hasCurrent ) {
216 $status->successCount++;
217 } else {
218 $status->failCount++;
219 }
220 $status->successCount += $oldRowCount;
221 // T36934: oldCount is based on files that actually exist.
222 // There may be more DB rows than such files, in which case $affected
223 // can be greater than $total. We use max() to avoid negatives here.
224 $status->failCount += max( 0, $this->oldCount - $oldRowCount );
225 if ( $status->failCount ) {
226 $status->error( 'imageinvalidfilename' );
227 }
228
229 return $status;
230 }
231
232 /**
233 * Do the database updates and return a new Status indicating how
234 * many rows where updated.
235 */
236 protected function doDBUpdates() {
237 $dbw = $this->db;
238
239 // Update current image
240 $dbw->update(
241 'image',
242 [ 'img_name' => $this->newName ],
243 [ 'img_name' => $this->oldName ],
244 __METHOD__
245 );
246
247 // Update old images
248 $dbw->update(
249 'oldimage',
250 [
251 'oi_name' => $this->newName,
252 'oi_archive_name = ' . $dbw->strreplace( 'oi_archive_name',
253 $dbw->addQuotes( $this->oldName ), $dbw->addQuotes( $this->newName ) ),
254 ],
255 [ 'oi_name' => $this->oldName ],
256 __METHOD__
257 );
258 }
259
260 /**
261 * Generate triplets for FileRepo::storeBatch().
262 * @return array[]
263 */
264 protected function getMoveTriplets() {
265 $moves = array_merge( [ $this->cur ], $this->olds );
266 $triplets = []; // The format is: (srcUrl, destZone, destUrl)
267
268 foreach ( $moves as $move ) {
269 // $move: (oldRelativePath, newRelativePath)
270 $srcUrl = $this->file->repo->getVirtualUrl() . '/public/' . rawurlencode( $move[0] );
271 $triplets[] = [ $srcUrl, 'public', $move[1] ];
272 wfDebugLog(
273 'imagemove',
274 "Generated move triplet for {$this->file->getName()}: {$srcUrl} :: public :: {$move[1]}"
275 );
276 }
277
278 return $triplets;
279 }
280
281 /**
282 * Removes non-existent files from move batch.
283 * @param array $triplets
284 * @return Status
285 */
286 protected function removeNonexistentFiles( $triplets ) {
287 $files = [];
288
289 foreach ( $triplets as $file ) {
290 $files[$file[0]] = $file[0];
291 }
292
293 $result = $this->file->repo->fileExistsBatch( $files );
294 if ( in_array( null, $result, true ) ) {
295 return Status::newFatal( 'backend-fail-internal',
296 $this->file->repo->getBackend()->getName() );
297 }
298
299 $filteredTriplets = [];
300 foreach ( $triplets as $file ) {
301 if ( $result[$file[0]] ) {
302 $filteredTriplets[] = $file;
303 } else {
304 wfDebugLog( 'imagemove', "File {$file[0]} does not exist" );
305 }
306 }
307
308 return Status::newGood( $filteredTriplets );
309 }
310
311 /**
312 * Cleanup a partially moved array of triplets by deleting the target
313 * files. Called if something went wrong half way.
314 * @param array[] $triplets
315 */
316 protected function cleanupTarget( $triplets ) {
317 // Create dest pairs from the triplets
318 $pairs = [];
319 foreach ( $triplets as $triplet ) {
320 // $triplet: (old source virtual URL, dst zone, dest rel)
321 $pairs[] = [ $triplet[1], $triplet[2] ];
322 }
323
324 $this->file->repo->cleanupBatch( $pairs );
325 }
326
327 /**
328 * Cleanup a fully moved array of triplets by deleting the source files.
329 * Called at the end of the move process if everything else went ok.
330 * @param array[] $triplets
331 */
332 protected function cleanupSource( $triplets ) {
333 // Create source file names from the triplets
334 $files = [];
335 foreach ( $triplets as $triplet ) {
336 $files[] = $triplet[0];
337 }
338
339 $this->file->repo->cleanupBatch( $files );
340 }
341 }