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\Revision\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() {
182 $dbw = $this->file
->repo
->getMasterDB();
184 $commentStore = MediaWikiServices
::getInstance()->getCommentStore();
185 $actorMigration = ActorMigration
::newMigration();
187 $encTimestamp = $dbw->addQuotes( $dbw->timestamp( $now ) );
188 $encUserId = $dbw->addQuotes( $this->user
->getId() );
189 $encGroup = $dbw->addQuotes( 'deleted' );
190 $ext = $this->file
->getExtension();
191 $dotExt = $ext === '' ?
'' : ".$ext";
192 $encExt = $dbw->addQuotes( $dotExt );
193 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
195 // Bitfields to further suppress the content
196 if ( $this->suppress
) {
197 $bitfield = RevisionRecord
::SUPPRESSED_ALL
;
199 $bitfield = 'oi_deleted';
202 if ( $deleteCurrent ) {
203 $tables = [ 'image' ];
205 'fa_storage_group' => $encGroup,
206 'fa_storage_key' => $dbw->conditional(
207 [ 'img_sha1' => '' ],
208 $dbw->addQuotes( '' ),
209 $dbw->buildConcat( [ "img_sha1", $encExt ] )
211 'fa_deleted_user' => $encUserId,
212 'fa_deleted_timestamp' => $encTimestamp,
213 'fa_deleted' => $this->suppress ?
$bitfield : 0,
214 'fa_name' => 'img_name',
215 'fa_archive_name' => 'NULL',
216 'fa_size' => 'img_size',
217 'fa_width' => 'img_width',
218 'fa_height' => 'img_height',
219 'fa_metadata' => 'img_metadata',
220 'fa_bits' => 'img_bits',
221 'fa_media_type' => 'img_media_type',
222 'fa_major_mime' => 'img_major_mime',
223 'fa_minor_mime' => 'img_minor_mime',
224 'fa_description_id' => 'img_description_id',
225 'fa_timestamp' => 'img_timestamp',
226 'fa_sha1' => 'img_sha1',
227 'fa_actor' => 'img_actor',
231 $fields +
= array_map(
232 [ $dbw, 'addQuotes' ],
233 $commentStore->insert( $dbw, 'fa_deleted_reason', $this->reason
)
236 $dbw->insertSelect( 'filearchive', $tables, $fields,
237 [ 'img_name' => $this->file
->getName() ], __METHOD__
, [], [], $joins );
240 if ( count( $oldRels ) ) {
241 $fileQuery = OldLocalFile
::getQueryInfo();
243 $fileQuery['tables'],
244 $fileQuery['fields'],
246 'oi_name' => $this->file
->getName(),
247 'oi_archive_name' => array_keys( $oldRels )
254 if ( $res->numRows() ) {
255 $reason = $commentStore->createComment( $dbw, $this->reason
);
256 foreach ( $res as $row ) {
257 $comment = $commentStore->getComment( 'oi_description', $row );
258 $user = User
::newFromAnyId( $row->oi_user
, $row->oi_user_text
, $row->oi_actor
);
260 // Deletion-specific fields
261 'fa_storage_group' => 'deleted',
262 'fa_storage_key' => ( $row->oi_sha1
=== '' )
264 : "{$row->oi_sha1}{$dotExt}",
265 'fa_deleted_user' => $this->user
->getId(),
266 'fa_deleted_timestamp' => $dbw->timestamp( $now ),
267 // Counterpart fields
268 'fa_deleted' => $this->suppress ?
$bitfield : $row->oi_deleted
,
269 'fa_name' => $row->oi_name
,
270 'fa_archive_name' => $row->oi_archive_name
,
271 'fa_size' => $row->oi_size
,
272 'fa_width' => $row->oi_width
,
273 'fa_height' => $row->oi_height
,
274 'fa_metadata' => $row->oi_metadata
,
275 'fa_bits' => $row->oi_bits
,
276 'fa_media_type' => $row->oi_media_type
,
277 'fa_major_mime' => $row->oi_major_mime
,
278 'fa_minor_mime' => $row->oi_minor_mime
,
279 'fa_timestamp' => $row->oi_timestamp
,
280 'fa_sha1' => $row->oi_sha1
281 ] +
$commentStore->insert( $dbw, 'fa_deleted_reason', $reason )
282 +
$commentStore->insert( $dbw, 'fa_description', $comment )
283 +
$actorMigration->getInsertValues( $dbw, 'fa_user', $user );
287 $dbw->insert( 'filearchive', $rowsInsert, __METHOD__
);
291 function doDBDeletes() {
292 $dbw = $this->file
->repo
->getMasterDB();
293 list( $oldRels, $deleteCurrent ) = $this->getOldRels();
295 if ( count( $oldRels ) ) {
296 $dbw->delete( 'oldimage',
298 'oi_name' => $this->file
->getName(),
299 'oi_archive_name' => array_keys( $oldRels )
303 if ( $deleteCurrent ) {
304 $dbw->delete( 'image', [ 'img_name' => $this->file
->getName() ], __METHOD__
);
309 * Run the transaction
312 public function execute() {
313 $repo = $this->file
->getRepo();
316 // Prepare deletion batch
317 $hashes = $this->getHashes();
318 $this->deletionBatch
= [];
319 $ext = $this->file
->getExtension();
320 $dotExt = $ext === '' ?
'' : ".$ext";
322 foreach ( $this->srcRels
as $name => $srcRel ) {
323 // Skip files that have no hash (e.g. missing DB record, or sha1 field and file source)
324 if ( isset( $hashes[$name] ) ) {
325 $hash = $hashes[$name];
326 $key = $hash . $dotExt;
327 $dstRel = $repo->getDeletedHashPath( $key ) . $key;
328 $this->deletionBatch
[$name] = [ $srcRel, $dstRel ];
332 if ( !$repo->hasSha1Storage() ) {
333 // Removes non-existent file from the batch, so we don't get errors.
334 // This also handles files in the 'deleted' zone deleted via revision deletion.
335 $checkStatus = $this->removeNonexistentFiles( $this->deletionBatch
);
336 if ( !$checkStatus->isGood() ) {
337 $this->status
->merge( $checkStatus );
338 return $this->status
;
340 $this->deletionBatch
= $checkStatus->value
;
342 // Execute the file deletion batch
343 $status = $this->file
->repo
->deleteBatch( $this->deletionBatch
);
344 if ( !$status->isGood() ) {
345 $this->status
->merge( $status );
349 if ( !$this->status
->isOK() ) {
350 // Critical file deletion error; abort
351 $this->file
->unlock();
353 return $this->status
;
356 // Copy the image/oldimage rows to filearchive
357 $this->doDBInserts();
358 // Delete image/oldimage rows
359 $this->doDBDeletes();
362 $this->file
->unlock();
364 return $this->status
;
368 * Removes non-existent files from a deletion batch.
369 * @param array $batch
372 protected function removeNonexistentFiles( $batch ) {
373 $files = $newBatch = [];
375 foreach ( $batch as $batchItem ) {
376 list( $src, ) = $batchItem;
377 $files[$src] = $this->file
->repo
->getVirtualUrl( 'public' ) . '/' . rawurlencode( $src );
380 $result = $this->file
->repo
->fileExistsBatch( $files );
381 if ( in_array( null, $result, true ) ) {
382 return Status
::newFatal( 'backend-fail-internal',
383 $this->file
->repo
->getBackend()->getName() );
386 foreach ( $batch as $batchItem ) {
387 if ( $result[$batchItem[0]] ) {
388 $newBatch[] = $batchItem;
392 return Status
::newGood( $newBatch );