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 MediaWiki\Storage\RevisionRecord
;
28 * Helper class for file deletion
29 * @ingroup FileAbstraction
31 class LocalFileDeleteBatch
{
39 private $srcRels = [];
42 private $archiveUrls = [];
44 /** @var array Items to be processed in the deletion batch */
45 private $deletionBatch;
47 /** @var bool Whether to suppress all suppressable fields when deleting */
58 * @param string $reason
59 * @param bool $suppress
60 * @param User|null $user
62 function __construct( File
$file, $reason = '', $suppress = false, $user = null ) {
64 $this->reason
= $reason;
65 $this->suppress
= $suppress;
67 $this->user
= $user ?
: $wgUser;
68 $this->status
= $file->repo
->newGood();
71 public function addCurrent() {
72 $this->srcRels
['.'] = $this->file
->getRel();
76 * @param string $oldName
78 public function addOld( $oldName ) {
79 $this->srcRels
[$oldName] = $this->file
->getArchiveRel( $oldName );
80 $this->archiveUrls
[] = $this->file
->getArchiveUrl( $oldName );
84 * Add the old versions of the image to the batch
85 * @return string[] List of archive names from old versions
87 public function addOlds() {
90 $dbw = $this->file
->repo
->getMasterDB();
91 $result = $dbw->select( 'oldimage',
92 [ 'oi_archive_name' ],
93 [ 'oi_name' => $this->file
->getName() ],
97 foreach ( $result as $row ) {
98 $this->addOld( $row->oi_archive_name
);
99 $archiveNames[] = $row->oi_archive_name
;
102 return $archiveNames;
108 protected function getOldRels() {
109 if ( !isset( $this->srcRels
['.'] ) ) {
110 $oldRels =& $this->srcRels
;
111 $deleteCurrent = false;
113 $oldRels = $this->srcRels
;
114 unset( $oldRels['.'] );
115 $deleteCurrent = true;
118 return [ $oldRels, $deleteCurrent ];
124 protected function getHashes() {
126 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
128 if ( $deleteCurrent ) {
129 $hashes['.'] = $this->file
->getSha1();
132 if ( count( $oldRels ) ) {
133 $dbw = $this->file
->repo
->getMasterDB();
136 [ 'oi_archive_name', 'oi_sha1' ],
137 [ 'oi_archive_name' => array_keys( $oldRels ),
138 'oi_name' => $this->file
->getName() ], // performance
142 foreach ( $res as $row ) {
143 if ( rtrim( $row->oi_sha1
, "\0" ) === '' ) {
144 // Get the hash from the file
145 $oldUrl = $this->file
->getArchiveVirtualUrl( $row->oi_archive_name
);
146 $props = $this->file
->repo
->getFileProps( $oldUrl );
148 if ( $props['fileExists'] ) {
149 // Upgrade the oldimage row
150 $dbw->update( 'oldimage',
151 [ 'oi_sha1' => $props['sha1'] ],
152 [ 'oi_name' => $this->file
->getName(), 'oi_archive_name' => $row->oi_archive_name
],
154 $hashes[$row->oi_archive_name
] = $props['sha1'];
156 $hashes[$row->oi_archive_name
] = false;
159 $hashes[$row->oi_archive_name
] = $row->oi_sha1
;
164 $missing = array_diff_key( $this->srcRels
, $hashes );
166 foreach ( $missing as $name => $rel ) {
167 $this->status
->error( 'filedelete-old-unregistered', $name );
170 foreach ( $hashes as $name => $hash ) {
172 $this->status
->error( 'filedelete-missing', $this->srcRels
[$name] );
173 unset( $hashes[$name] );
180 protected function doDBInserts() {
181 global $wgActorTableSchemaMigrationStage;
184 $dbw = $this->file
->repo
->getMasterDB();
186 $commentStore = MediaWikiServices
::getInstance()->getCommentStore();
187 $actorMigration = ActorMigration
::newMigration();
189 $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $now ) );
190 $encUserId = $dbw->addQuotes( $this->user
->getId() );
191 $encGroup = $dbw->addQuotes( 'deleted' );
192 $ext = $this->file
->getExtension();
193 $dotExt = $ext === '' ?
'' : ".$ext";
194 $encExt = $dbw->addQuotes( $dotExt );
195 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
197 // Bitfields to further suppress the content
198 if ( $this->suppress
) {
199 $bitfield = RevisionRecord
::SUPPRESSED_ALL
;
201 $bitfield = 'oi_deleted';
204 if ( $deleteCurrent ) {
205 $tables = [ 'image' ];
207 'fa_storage_group' => $encGroup,
208 'fa_storage_key' => $dbw->conditional(
209 [ 'img_sha1' => '' ],
210 $dbw->addQuotes( '' ),
211 $dbw->buildConcat( [ "img_sha1", $encExt ] )
213 'fa_deleted_user' => $encUserId,
214 'fa_deleted_timestamp' => $encTimestamp,
215 'fa_deleted' => $this->suppress ?
$bitfield : 0,
216 'fa_name' => 'img_name',
217 'fa_archive_name' => 'NULL',
218 'fa_size' => 'img_size',
219 'fa_width' => 'img_width',
220 'fa_height' => 'img_height',
221 'fa_metadata' => 'img_metadata',
222 'fa_bits' => 'img_bits',
223 'fa_media_type' => 'img_media_type',
224 'fa_major_mime' => 'img_major_mime',
225 'fa_minor_mime' => 'img_minor_mime',
226 'fa_description_id' => 'img_description_id',
227 'fa_timestamp' => 'img_timestamp',
228 'fa_sha1' => 'img_sha1'
232 $fields +
= array_map(
233 [ $dbw, 'addQuotes' ],
234 $commentStore->insert( $dbw, 'fa_deleted_reason', $this->reason
)
237 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD
) {
238 $fields['fa_user'] = 'img_user';
239 $fields['fa_user_text'] = 'img_user_text';
241 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) {
242 $fields['fa_actor'] = 'img_actor';
246 ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_BOTH
) === SCHEMA_COMPAT_WRITE_BOTH
248 // Upgrade any rows that are still old-style. Otherwise an upgrade
249 // might be missed if a deletion happens while the migration script
253 [ 'img_name', 'img_user', 'img_user_text' ],
254 [ 'img_name' => $this->file
->getName(), 'img_actor' => 0 ],
257 foreach ( $res as $row ) {
258 $actorId = User
::newFromAnyId( $row->img_user
, $row->img_user_text
, null )->getActorId( $dbw );
261 [ 'img_actor' => $actorId ],
262 [ 'img_name' => $row->img_name
, 'img_actor' => 0 ],
268 $dbw->insertSelect( 'filearchive', $tables, $fields,
269 [ 'img_name' => $this->file
->getName() ], __METHOD__
, [], [], $joins );
272 if ( count( $oldRels ) ) {
273 $fileQuery = OldLocalFile
::getQueryInfo();
275 $fileQuery['tables'],
276 $fileQuery['fields'],
278 'oi_name' => $this->file
->getName(),
279 'oi_archive_name' => array_keys( $oldRels )
286 if ( $res->numRows() ) {
287 $reason = $commentStore->createComment( $dbw, $this->reason
);
288 foreach ( $res as $row ) {
289 $comment = $commentStore->getComment( 'oi_description', $row );
290 $user = User
::newFromAnyId( $row->oi_user
, $row->oi_user_text
, $row->oi_actor
);
292 // Deletion-specific fields
293 'fa_storage_group' => 'deleted',
294 'fa_storage_key' => ( $row->oi_sha1
=== '' )
296 : "{$row->oi_sha1}{$dotExt}",
297 'fa_deleted_user' => $this->user
->getId(),
298 'fa_deleted_timestamp' => $dbw->timestamp( $now ),
299 // Counterpart fields
300 'fa_deleted' => $this->suppress ?
$bitfield : $row->oi_deleted
,
301 'fa_name' => $row->oi_name
,
302 'fa_archive_name' => $row->oi_archive_name
,
303 'fa_size' => $row->oi_size
,
304 'fa_width' => $row->oi_width
,
305 'fa_height' => $row->oi_height
,
306 'fa_metadata' => $row->oi_metadata
,
307 'fa_bits' => $row->oi_bits
,
308 'fa_media_type' => $row->oi_media_type
,
309 'fa_major_mime' => $row->oi_major_mime
,
310 'fa_minor_mime' => $row->oi_minor_mime
,
311 'fa_timestamp' => $row->oi_timestamp
,
312 'fa_sha1' => $row->oi_sha1
313 ] +
$commentStore->insert( $dbw, 'fa_deleted_reason', $reason )
314 +
$commentStore->insert( $dbw, 'fa_description', $comment )
315 +
$actorMigration->getInsertValues( $dbw, 'fa_user', $user );
319 $dbw->insert( 'filearchive', $rowsInsert, __METHOD__
);
323 function doDBDeletes() {
324 $dbw = $this->file
->repo
->getMasterDB();
325 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
327 if ( count( $oldRels ) ) {
328 $dbw->delete( 'oldimage',
330 'oi_name' => $this->file
->getName(),
331 'oi_archive_name' => array_keys( $oldRels )
335 if ( $deleteCurrent ) {
336 $dbw->delete( 'image', [ 'img_name' => $this->file
->getName() ], __METHOD__
);
341 * Run the transaction
344 public function execute() {
345 $repo = $this->file
->getRepo();
348 // Prepare deletion batch
349 $hashes = $this->getHashes();
350 $this->deletionBatch
= [];
351 $ext = $this->file
->getExtension();
352 $dotExt = $ext === '' ?
'' : ".$ext";
354 foreach ( $this->srcRels
as $name => $srcRel ) {
355 // Skip files that have no hash (e.g. missing DB record, or sha1 field and file source)
356 if ( isset( $hashes[$name] ) ) {
357 $hash = $hashes[$name];
358 $key = $hash . $dotExt;
359 $dstRel = $repo->getDeletedHashPath( $key ) . $key;
360 $this->deletionBatch
[$name] = [ $srcRel, $dstRel ];
364 if ( !$repo->hasSha1Storage() ) {
365 // Removes non-existent file from the batch, so we don't get errors.
366 // This also handles files in the 'deleted' zone deleted via revision deletion.
367 $checkStatus = $this->removeNonexistentFiles( $this->deletionBatch
);
368 if ( !$checkStatus->isGood() ) {
369 $this->status
->merge( $checkStatus );
370 return $this->status
;
372 $this->deletionBatch
= $checkStatus->value
;
374 // Execute the file deletion batch
375 $status = $this->file
->repo
->deleteBatch( $this->deletionBatch
);
376 if ( !$status->isGood() ) {
377 $this->status
->merge( $status );
381 if ( !$this->status
->isOK() ) {
382 // Critical file deletion error; abort
383 $this->file
->unlock();
385 return $this->status
;
388 // Copy the image/oldimage rows to filearchive
389 $this->doDBInserts();
390 // Delete image/oldimage rows
391 $this->doDBDeletes();
394 $this->file
->unlock();
396 return $this->status
;
400 * Removes non-existent files from a deletion batch.
401 * @param array $batch
404 protected function removeNonexistentFiles( $batch ) {
405 $files = $newBatch = [];
407 foreach ( $batch as $batchItem ) {
408 list( $src, ) = $batchItem;
409 $files[$src] = $this->file
->repo
->getVirtualUrl( 'public' ) . '/' . rawurlencode( $src );
412 $result = $this->file
->repo
->fileExistsBatch( $files );
413 if ( in_array( null, $result, true ) ) {
414 return Status
::newFatal( 'backend-fail-internal',
415 $this->file
->repo
->getBackend()->getName() );
418 foreach ( $batch as $batchItem ) {
419 if ( $result[$batchItem[0]] ) {
420 $newBatch[] = $batchItem;
424 return Status
::newGood( $newBatch );