Merge "maintenance: Script to rename titles for Unicode uppercasing changes"
[lhc/web/wiklou.git] / includes / filerepo / file / LocalFileDeleteBatch.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
26 /**
27 * Helper class for file deletion
28 * @ingroup FileAbstraction
29 */
30 class LocalFileDeleteBatch {
31 /** @var LocalFile */
32 private $file;
33
34 /** @var string */
35 private $reason;
36
37 /** @var array */
38 private $srcRels = [];
39
40 /** @var array */
41 private $archiveUrls = [];
42
43 /** @var array Items to be processed in the deletion batch */
44 private $deletionBatch;
45
46 /** @var bool Whether to suppress all suppressable fields when deleting */
47 private $suppress;
48
49 /** @var Status */
50 private $status;
51
52 /** @var User */
53 private $user;
54
55 /**
56 * @param File $file
57 * @param string $reason
58 * @param bool $suppress
59 * @param User|null $user
60 */
61 function __construct( File $file, $reason = '', $suppress = false, $user = null ) {
62 $this->file = $file;
63 $this->reason = $reason;
64 $this->suppress = $suppress;
65 global $wgUser;
66 $this->user = $user ?: $wgUser;
67 $this->status = $file->repo->newGood();
68 }
69
70 public function addCurrent() {
71 $this->srcRels['.'] = $this->file->getRel();
72 }
73
74 /**
75 * @param string $oldName
76 */
77 public function addOld( $oldName ) {
78 $this->srcRels[$oldName] = $this->file->getArchiveRel( $oldName );
79 $this->archiveUrls[] = $this->file->getArchiveUrl( $oldName );
80 }
81
82 /**
83 * Add the old versions of the image to the batch
84 * @return string[] List of archive names from old versions
85 */
86 public function addOlds() {
87 $archiveNames = [];
88
89 $dbw = $this->file->repo->getMasterDB();
90 $result = $dbw->select( 'oldimage',
91 [ 'oi_archive_name' ],
92 [ 'oi_name' => $this->file->getName() ],
93 __METHOD__
94 );
95
96 foreach ( $result as $row ) {
97 $this->addOld( $row->oi_archive_name );
98 $archiveNames[] = $row->oi_archive_name;
99 }
100
101 return $archiveNames;
102 }
103
104 /**
105 * @return array
106 */
107 protected function getOldRels() {
108 if ( !isset( $this->srcRels['.'] ) ) {
109 $oldRels =& $this->srcRels;
110 $deleteCurrent = false;
111 } else {
112 $oldRels = $this->srcRels;
113 unset( $oldRels['.'] );
114 $deleteCurrent = true;
115 }
116
117 return [ $oldRels, $deleteCurrent ];
118 }
119
120 /**
121 * @return array
122 */
123 protected function getHashes() {
124 $hashes = [];
125 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
126
127 if ( $deleteCurrent ) {
128 $hashes['.'] = $this->file->getSha1();
129 }
130
131 if ( count( $oldRels ) ) {
132 $dbw = $this->file->repo->getMasterDB();
133 $res = $dbw->select(
134 'oldimage',
135 [ 'oi_archive_name', 'oi_sha1' ],
136 [ 'oi_archive_name' => array_keys( $oldRels ),
137 'oi_name' => $this->file->getName() ], // performance
138 __METHOD__
139 );
140
141 foreach ( $res as $row ) {
142 if ( rtrim( $row->oi_sha1, "\0" ) === '' ) {
143 // Get the hash from the file
144 $oldUrl = $this->file->getArchiveVirtualUrl( $row->oi_archive_name );
145 $props = $this->file->repo->getFileProps( $oldUrl );
146
147 if ( $props['fileExists'] ) {
148 // Upgrade the oldimage row
149 $dbw->update( 'oldimage',
150 [ 'oi_sha1' => $props['sha1'] ],
151 [ 'oi_name' => $this->file->getName(), 'oi_archive_name' => $row->oi_archive_name ],
152 __METHOD__ );
153 $hashes[$row->oi_archive_name] = $props['sha1'];
154 } else {
155 $hashes[$row->oi_archive_name] = false;
156 }
157 } else {
158 $hashes[$row->oi_archive_name] = $row->oi_sha1;
159 }
160 }
161 }
162
163 $missing = array_diff_key( $this->srcRels, $hashes );
164
165 foreach ( $missing as $name => $rel ) {
166 $this->status->error( 'filedelete-old-unregistered', $name );
167 }
168
169 foreach ( $hashes as $name => $hash ) {
170 if ( !$hash ) {
171 $this->status->error( 'filedelete-missing', $this->srcRels[$name] );
172 unset( $hashes[$name] );
173 }
174 }
175
176 return $hashes;
177 }
178
179 protected function doDBInserts() {
180 global $wgActorTableSchemaMigrationStage;
181
182 $now = time();
183 $dbw = $this->file->repo->getMasterDB();
184
185 $commentStore = MediaWikiServices::getInstance()->getCommentStore();
186 $actorMigration = ActorMigration::newMigration();
187
188 $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $now ) );
189 $encUserId = $dbw->addQuotes( $this->user->getId() );
190 $encGroup = $dbw->addQuotes( 'deleted' );
191 $ext = $this->file->getExtension();
192 $dotExt = $ext === '' ? '' : ".$ext";
193 $encExt = $dbw->addQuotes( $dotExt );
194 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
195
196 // Bitfields to further suppress the content
197 if ( $this->suppress ) {
198 $bitfield = Revision::SUPPRESSED_ALL;
199 } else {
200 $bitfield = 'oi_deleted';
201 }
202
203 if ( $deleteCurrent ) {
204 $tables = [ 'image' ];
205 $fields = [
206 'fa_storage_group' => $encGroup,
207 'fa_storage_key' => $dbw->conditional(
208 [ 'img_sha1' => '' ],
209 $dbw->addQuotes( '' ),
210 $dbw->buildConcat( [ "img_sha1", $encExt ] )
211 ),
212 'fa_deleted_user' => $encUserId,
213 'fa_deleted_timestamp' => $encTimestamp,
214 'fa_deleted' => $this->suppress ? $bitfield : 0,
215 'fa_name' => 'img_name',
216 'fa_archive_name' => 'NULL',
217 'fa_size' => 'img_size',
218 'fa_width' => 'img_width',
219 'fa_height' => 'img_height',
220 'fa_metadata' => 'img_metadata',
221 'fa_bits' => 'img_bits',
222 'fa_media_type' => 'img_media_type',
223 'fa_major_mime' => 'img_major_mime',
224 'fa_minor_mime' => 'img_minor_mime',
225 'fa_description_id' => 'img_description_id',
226 'fa_timestamp' => 'img_timestamp',
227 'fa_sha1' => 'img_sha1'
228 ];
229 $joins = [];
230
231 $fields += array_map(
232 [ $dbw, 'addQuotes' ],
233 $commentStore->insert( $dbw, 'fa_deleted_reason', $this->reason )
234 );
235
236 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD ) {
237 $fields['fa_user'] = 'img_user';
238 $fields['fa_user_text'] = 'img_user_text';
239 }
240 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW ) {
241 $fields['fa_actor'] = 'img_actor';
242 }
243
244 if (
245 ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_BOTH ) === SCHEMA_COMPAT_WRITE_BOTH
246 ) {
247 // Upgrade any rows that are still old-style. Otherwise an upgrade
248 // might be missed if a deletion happens while the migration script
249 // is running.
250 $res = $dbw->select(
251 [ 'image' ],
252 [ 'img_name', 'img_user', 'img_user_text' ],
253 [ 'img_name' => $this->file->getName(), 'img_actor' => 0 ],
254 __METHOD__
255 );
256 foreach ( $res as $row ) {
257 $actorId = User::newFromAnyId( $row->img_user, $row->img_user_text, null )->getActorId( $dbw );
258 $dbw->update(
259 'image',
260 [ 'img_actor' => $actorId ],
261 [ 'img_name' => $row->img_name, 'img_actor' => 0 ],
262 __METHOD__
263 );
264 }
265 }
266
267 $dbw->insertSelect( 'filearchive', $tables, $fields,
268 [ 'img_name' => $this->file->getName() ], __METHOD__, [], [], $joins );
269 }
270
271 if ( count( $oldRels ) ) {
272 $fileQuery = OldLocalFile::getQueryInfo();
273 $res = $dbw->select(
274 $fileQuery['tables'],
275 $fileQuery['fields'],
276 [
277 'oi_name' => $this->file->getName(),
278 'oi_archive_name' => array_keys( $oldRels )
279 ],
280 __METHOD__,
281 [ 'FOR UPDATE' ],
282 $fileQuery['joins']
283 );
284 $rowsInsert = [];
285 if ( $res->numRows() ) {
286 $reason = $commentStore->createComment( $dbw, $this->reason );
287 foreach ( $res as $row ) {
288 $comment = $commentStore->getComment( 'oi_description', $row );
289 $user = User::newFromAnyId( $row->oi_user, $row->oi_user_text, $row->oi_actor );
290 $rowsInsert[] = [
291 // Deletion-specific fields
292 'fa_storage_group' => 'deleted',
293 'fa_storage_key' => ( $row->oi_sha1 === '' )
294 ? ''
295 : "{$row->oi_sha1}{$dotExt}",
296 'fa_deleted_user' => $this->user->getId(),
297 'fa_deleted_timestamp' => $dbw->timestamp( $now ),
298 // Counterpart fields
299 'fa_deleted' => $this->suppress ? $bitfield : $row->oi_deleted,
300 'fa_name' => $row->oi_name,
301 'fa_archive_name' => $row->oi_archive_name,
302 'fa_size' => $row->oi_size,
303 'fa_width' => $row->oi_width,
304 'fa_height' => $row->oi_height,
305 'fa_metadata' => $row->oi_metadata,
306 'fa_bits' => $row->oi_bits,
307 'fa_media_type' => $row->oi_media_type,
308 'fa_major_mime' => $row->oi_major_mime,
309 'fa_minor_mime' => $row->oi_minor_mime,
310 'fa_timestamp' => $row->oi_timestamp,
311 'fa_sha1' => $row->oi_sha1
312 ] + $commentStore->insert( $dbw, 'fa_deleted_reason', $reason )
313 + $commentStore->insert( $dbw, 'fa_description', $comment )
314 + $actorMigration->getInsertValues( $dbw, 'fa_user', $user );
315 }
316 }
317
318 $dbw->insert( 'filearchive', $rowsInsert, __METHOD__ );
319 }
320 }
321
322 function doDBDeletes() {
323 $dbw = $this->file->repo->getMasterDB();
324 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
325
326 if ( count( $oldRels ) ) {
327 $dbw->delete( 'oldimage',
328 [
329 'oi_name' => $this->file->getName(),
330 'oi_archive_name' => array_keys( $oldRels )
331 ], __METHOD__ );
332 }
333
334 if ( $deleteCurrent ) {
335 $dbw->delete( 'image', [ 'img_name' => $this->file->getName() ], __METHOD__ );
336 }
337 }
338
339 /**
340 * Run the transaction
341 * @return Status
342 */
343 public function execute() {
344 $repo = $this->file->getRepo();
345 $this->file->lock();
346
347 // Prepare deletion batch
348 $hashes = $this->getHashes();
349 $this->deletionBatch = [];
350 $ext = $this->file->getExtension();
351 $dotExt = $ext === '' ? '' : ".$ext";
352
353 foreach ( $this->srcRels as $name => $srcRel ) {
354 // Skip files that have no hash (e.g. missing DB record, or sha1 field and file source)
355 if ( isset( $hashes[$name] ) ) {
356 $hash = $hashes[$name];
357 $key = $hash . $dotExt;
358 $dstRel = $repo->getDeletedHashPath( $key ) . $key;
359 $this->deletionBatch[$name] = [ $srcRel, $dstRel ];
360 }
361 }
362
363 if ( !$repo->hasSha1Storage() ) {
364 // Removes non-existent file from the batch, so we don't get errors.
365 // This also handles files in the 'deleted' zone deleted via revision deletion.
366 $checkStatus = $this->removeNonexistentFiles( $this->deletionBatch );
367 if ( !$checkStatus->isGood() ) {
368 $this->status->merge( $checkStatus );
369 return $this->status;
370 }
371 $this->deletionBatch = $checkStatus->value;
372
373 // Execute the file deletion batch
374 $status = $this->file->repo->deleteBatch( $this->deletionBatch );
375 if ( !$status->isGood() ) {
376 $this->status->merge( $status );
377 }
378 }
379
380 if ( !$this->status->isOK() ) {
381 // Critical file deletion error; abort
382 $this->file->unlock();
383
384 return $this->status;
385 }
386
387 // Copy the image/oldimage rows to filearchive
388 $this->doDBInserts();
389 // Delete image/oldimage rows
390 $this->doDBDeletes();
391
392 // Commit and return
393 $this->file->unlock();
394
395 return $this->status;
396 }
397
398 /**
399 * Removes non-existent files from a deletion batch.
400 * @param array $batch
401 * @return Status
402 */
403 protected function removeNonexistentFiles( $batch ) {
404 $files = $newBatch = [];
405
406 foreach ( $batch as $batchItem ) {
407 list( $src, ) = $batchItem;
408 $files[$src] = $this->file->repo->getVirtualUrl( 'public' ) . '/' . rawurlencode( $src );
409 }
410
411 $result = $this->file->repo->fileExistsBatch( $files );
412 if ( in_array( null, $result, true ) ) {
413 return Status::newFatal( 'backend-fail-internal',
414 $this->file->repo->getBackend()->getName() );
415 }
416
417 foreach ( $batch as $batchItem ) {
418 if ( $result[$batchItem[0]] ) {
419 $newBatch[] = $batchItem;
420 }
421 }
422
423 return Status::newGood( $newBatch );
424 }
425 }