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
;
27 * Helper class for file deletion
28 * @ingroup FileAbstraction
30 class LocalFileDeleteBatch
{
38 private $srcRels = [];
41 private $archiveUrls = [];
43 /** @var array Items to be processed in the deletion batch */
44 private $deletionBatch;
46 /** @var bool Whether to suppress all suppressable fields when deleting */
57 * @param string $reason
58 * @param bool $suppress
59 * @param User|null $user
61 function __construct( File
$file, $reason = '', $suppress = false, $user = null ) {
63 $this->reason
= $reason;
64 $this->suppress
= $suppress;
69 $this->user
= $wgUser;
71 $this->status
= $file->repo
->newGood();
74 public function addCurrent() {
75 $this->srcRels
['.'] = $this->file
->getRel();
79 * @param string $oldName
81 public function addOld( $oldName ) {
82 $this->srcRels
[$oldName] = $this->file
->getArchiveRel( $oldName );
83 $this->archiveUrls
[] = $this->file
->getArchiveUrl( $oldName );
87 * Add the old versions of the image to the batch
88 * @return string[] List of archive names from old versions
90 public function addOlds() {
93 $dbw = $this->file
->repo
->getMasterDB();
94 $result = $dbw->select( 'oldimage',
95 [ 'oi_archive_name' ],
96 [ 'oi_name' => $this->file
->getName() ],
100 foreach ( $result as $row ) {
101 $this->addOld( $row->oi_archive_name
);
102 $archiveNames[] = $row->oi_archive_name
;
105 return $archiveNames;
111 protected function getOldRels() {
112 if ( !isset( $this->srcRels
['.'] ) ) {
113 $oldRels =& $this->srcRels
;
114 $deleteCurrent = false;
116 $oldRels = $this->srcRels
;
117 unset( $oldRels['.'] );
118 $deleteCurrent = true;
121 return [ $oldRels, $deleteCurrent ];
127 protected function getHashes() {
129 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
131 if ( $deleteCurrent ) {
132 $hashes['.'] = $this->file
->getSha1();
135 if ( count( $oldRels ) ) {
136 $dbw = $this->file
->repo
->getMasterDB();
139 [ 'oi_archive_name', 'oi_sha1' ],
140 [ 'oi_archive_name' => array_keys( $oldRels ),
141 'oi_name' => $this->file
->getName() ], // performance
145 foreach ( $res as $row ) {
146 if ( rtrim( $row->oi_sha1
, "\0" ) === '' ) {
147 // Get the hash from the file
148 $oldUrl = $this->file
->getArchiveVirtualUrl( $row->oi_archive_name
);
149 $props = $this->file
->repo
->getFileProps( $oldUrl );
151 if ( $props['fileExists'] ) {
152 // Upgrade the oldimage row
153 $dbw->update( 'oldimage',
154 [ 'oi_sha1' => $props['sha1'] ],
155 [ 'oi_name' => $this->file
->getName(), 'oi_archive_name' => $row->oi_archive_name
],
157 $hashes[$row->oi_archive_name
] = $props['sha1'];
159 $hashes[$row->oi_archive_name
] = false;
162 $hashes[$row->oi_archive_name
] = $row->oi_sha1
;
167 $missing = array_diff_key( $this->srcRels
, $hashes );
169 foreach ( $missing as $name => $rel ) {
170 $this->status
->error( 'filedelete-old-unregistered', $name );
173 foreach ( $hashes as $name => $hash ) {
175 $this->status
->error( 'filedelete-missing', $this->srcRels
[$name] );
176 unset( $hashes[$name] );
183 protected function doDBInserts() {
184 global $wgActorTableSchemaMigrationStage;
187 $dbw = $this->file
->repo
->getMasterDB();
189 $commentStore = MediaWikiServices
::getInstance()->getCommentStore();
190 $actorMigration = ActorMigration
::newMigration();
192 $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $now ) );
193 $encUserId = $dbw->addQuotes( $this->user
->getId() );
194 $encGroup = $dbw->addQuotes( 'deleted' );
195 $ext = $this->file
->getExtension();
196 $dotExt = $ext === '' ?
'' : ".$ext";
197 $encExt = $dbw->addQuotes( $dotExt );
198 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
200 // Bitfields to further suppress the content
201 if ( $this->suppress
) {
202 $bitfield = Revision
::SUPPRESSED_ALL
;
204 $bitfield = 'oi_deleted';
207 if ( $deleteCurrent ) {
208 $tables = [ 'image' ];
210 'fa_storage_group' => $encGroup,
211 'fa_storage_key' => $dbw->conditional(
212 [ 'img_sha1' => '' ],
213 $dbw->addQuotes( '' ),
214 $dbw->buildConcat( [ "img_sha1", $encExt ] )
216 'fa_deleted_user' => $encUserId,
217 'fa_deleted_timestamp' => $encTimestamp,
218 'fa_deleted' => $this->suppress ?
$bitfield : 0,
219 'fa_name' => 'img_name',
220 'fa_archive_name' => 'NULL',
221 'fa_size' => 'img_size',
222 'fa_width' => 'img_width',
223 'fa_height' => 'img_height',
224 'fa_metadata' => 'img_metadata',
225 'fa_bits' => 'img_bits',
226 'fa_media_type' => 'img_media_type',
227 'fa_major_mime' => 'img_major_mime',
228 'fa_minor_mime' => 'img_minor_mime',
229 'fa_description_id' => 'img_description_id',
230 'fa_timestamp' => 'img_timestamp',
231 'fa_sha1' => 'img_sha1'
235 $fields +
= array_map(
236 [ $dbw, 'addQuotes' ],
237 $commentStore->insert( $dbw, 'fa_deleted_reason', $this->reason
)
240 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD
) {
241 $fields['fa_user'] = 'img_user';
242 $fields['fa_user_text'] = 'img_user_text';
244 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) {
245 $fields['fa_actor'] = 'img_actor';
249 ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_BOTH
) === SCHEMA_COMPAT_WRITE_BOTH
251 // Upgrade any rows that are still old-style. Otherwise an upgrade
252 // might be missed if a deletion happens while the migration script
256 [ 'img_name', 'img_user', 'img_user_text' ],
257 [ 'img_name' => $this->file
->getName(), 'img_actor' => 0 ],
260 foreach ( $res as $row ) {
261 $actorId = User
::newFromAnyId( $row->img_user
, $row->img_user_text
, null )->getActorId( $dbw );
264 [ 'img_actor' => $actorId ],
265 [ 'img_name' => $row->img_name
, 'img_actor' => 0 ],
271 $dbw->insertSelect( 'filearchive', $tables, $fields,
272 [ 'img_name' => $this->file
->getName() ], __METHOD__
, [], [], $joins );
275 if ( count( $oldRels ) ) {
276 $fileQuery = OldLocalFile
::getQueryInfo();
278 $fileQuery['tables'],
279 $fileQuery['fields'],
281 'oi_name' => $this->file
->getName(),
282 'oi_archive_name' => array_keys( $oldRels )
289 if ( $res->numRows() ) {
290 $reason = $commentStore->createComment( $dbw, $this->reason
);
291 foreach ( $res as $row ) {
292 $comment = $commentStore->getComment( 'oi_description', $row );
293 $user = User
::newFromAnyId( $row->oi_user
, $row->oi_user_text
, $row->oi_actor
);
295 // Deletion-specific fields
296 'fa_storage_group' => 'deleted',
297 'fa_storage_key' => ( $row->oi_sha1
=== '' )
299 : "{$row->oi_sha1}{$dotExt}",
300 'fa_deleted_user' => $this->user
->getId(),
301 'fa_deleted_timestamp' => $dbw->timestamp( $now ),
302 // Counterpart fields
303 'fa_deleted' => $this->suppress ?
$bitfield : $row->oi_deleted
,
304 'fa_name' => $row->oi_name
,
305 'fa_archive_name' => $row->oi_archive_name
,
306 'fa_size' => $row->oi_size
,
307 'fa_width' => $row->oi_width
,
308 'fa_height' => $row->oi_height
,
309 'fa_metadata' => $row->oi_metadata
,
310 'fa_bits' => $row->oi_bits
,
311 'fa_media_type' => $row->oi_media_type
,
312 'fa_major_mime' => $row->oi_major_mime
,
313 'fa_minor_mime' => $row->oi_minor_mime
,
314 'fa_timestamp' => $row->oi_timestamp
,
315 'fa_sha1' => $row->oi_sha1
316 ] +
$commentStore->insert( $dbw, 'fa_deleted_reason', $reason )
317 +
$commentStore->insert( $dbw, 'fa_description', $comment )
318 +
$actorMigration->getInsertValues( $dbw, 'fa_user', $user );
322 $dbw->insert( 'filearchive', $rowsInsert, __METHOD__
);
326 function doDBDeletes() {
327 $dbw = $this->file
->repo
->getMasterDB();
328 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
330 if ( count( $oldRels ) ) {
331 $dbw->delete( 'oldimage',
333 'oi_name' => $this->file
->getName(),
334 'oi_archive_name' => array_keys( $oldRels )
338 if ( $deleteCurrent ) {
339 $dbw->delete( 'image', [ 'img_name' => $this->file
->getName() ], __METHOD__
);
344 * Run the transaction
347 public function execute() {
348 $repo = $this->file
->getRepo();
351 // Prepare deletion batch
352 $hashes = $this->getHashes();
353 $this->deletionBatch
= [];
354 $ext = $this->file
->getExtension();
355 $dotExt = $ext === '' ?
'' : ".$ext";
357 foreach ( $this->srcRels
as $name => $srcRel ) {
358 // Skip files that have no hash (e.g. missing DB record, or sha1 field and file source)
359 if ( isset( $hashes[$name] ) ) {
360 $hash = $hashes[$name];
361 $key = $hash . $dotExt;
362 $dstRel = $repo->getDeletedHashPath( $key ) . $key;
363 $this->deletionBatch
[$name] = [ $srcRel, $dstRel ];
367 if ( !$repo->hasSha1Storage() ) {
368 // Removes non-existent file from the batch, so we don't get errors.
369 // This also handles files in the 'deleted' zone deleted via revision deletion.
370 $checkStatus = $this->removeNonexistentFiles( $this->deletionBatch
);
371 if ( !$checkStatus->isGood() ) {
372 $this->status
->merge( $checkStatus );
373 return $this->status
;
375 $this->deletionBatch
= $checkStatus->value
;
377 // Execute the file deletion batch
378 $status = $this->file
->repo
->deleteBatch( $this->deletionBatch
);
379 if ( !$status->isGood() ) {
380 $this->status
->merge( $status );
384 if ( !$this->status
->isOK() ) {
385 // Critical file deletion error; abort
386 $this->file
->unlock();
388 return $this->status
;
391 // Copy the image/oldimage rows to filearchive
392 $this->doDBInserts();
393 // Delete image/oldimage rows
394 $this->doDBDeletes();
397 $this->file
->unlock();
399 return $this->status
;
403 * Removes non-existent files from a deletion batch.
404 * @param array $batch
407 protected function removeNonexistentFiles( $batch ) {
408 $files = $newBatch = [];
410 foreach ( $batch as $batchItem ) {
411 list( $src, ) = $batchItem;
412 $files[$src] = $this->file
->repo
->getVirtualUrl( 'public' ) . '/' . rawurlencode( $src );
415 $result = $this->file
->repo
->fileExistsBatch( $files );
416 if ( in_array( null, $result, true ) ) {
417 return Status
::newFatal( 'backend-fail-internal',
418 $this->file
->repo
->getBackend()->getName() );
421 foreach ( $batch as $batchItem ) {
422 if ( $result[$batchItem[0]] ) {
423 $newBatch[] = $batchItem;
427 return Status
::newGood( $newBatch );