- Use DeletePage in FileDeleteForm instead of WikiPage::doDeleteArticleReal - Properly handle scheduled deletions in FileDeleteForm: previously, a null status value could indicate a missing page OR a scheduled deletion, but the code always assumed the first, and it would generate a duplicated log entry. The API response would also not contain the "delete-scheduled" message. This has been broken since the introduction of scheduled deletion. - In ApiDelete, for file deletions, check whether the status is OK not good. The two might be equivalent, but this way it's more consistent. - Add some documentation for the Status objects returned by file-related methods. This is still incomplete, as there are many methods using Status and none of them says what the status could be. In particular, this means that for now we keep checking whether the status is OK instead of good, even though it's unclear what could produce a non-fatal error. - In LocalFileDeleteBatch, avoid using a class property for the returned status, as that's hard to follow. Instead, use a local variable and pass it around when needed. Bug: T288758 Change-Id: I22d60c05bdd4a3ea531e63dbb9e49efc36935137
152 lines
4.9 KiB
PHP
152 lines
4.9 KiB
PHP
<?php
|
|
/**
|
|
* File deletion utilities.
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
*
|
|
* @file
|
|
* @author Rob Church <robchur@gmail.com>
|
|
* @ingroup Media
|
|
*/
|
|
|
|
use MediaWiki\MediaWikiServices;
|
|
use MediaWiki\User\UserIdentity;
|
|
|
|
/**
|
|
* File deletion user interface
|
|
*
|
|
* @ingroup Media
|
|
*/
|
|
class FileDeleteForm {
|
|
/**
|
|
* Really delete the file
|
|
*
|
|
* @param Title $title
|
|
* @param LocalFile $file
|
|
* @param string|null $oldimage Archive name
|
|
* @param string $reason Reason of the deletion
|
|
* @param bool $suppress Whether to mark all deleted versions as restricted
|
|
* @param UserIdentity $user
|
|
* @param string[] $tags Tags to apply to the deletion action
|
|
* @throws MWException
|
|
* @return Status The value can be an integer with the log ID of the deletion, or false in case of
|
|
* scheduled deletion.
|
|
*/
|
|
public static function doDelete( Title $title, LocalFile $file, ?string $oldimage, $reason,
|
|
$suppress, UserIdentity $user, $tags = []
|
|
): Status {
|
|
if ( $oldimage ) {
|
|
$page = null;
|
|
$status = $file->deleteOldFile( $oldimage, $reason, $user, $suppress );
|
|
if ( $status->isOK() ) {
|
|
// Need to do a log item
|
|
$logComment = wfMessage( 'deletedrevision', $oldimage )->inContentLanguage()->text();
|
|
if ( trim( $reason ) !== '' ) {
|
|
$logComment .= wfMessage( 'colon-separator' )
|
|
->inContentLanguage()->text() . $reason;
|
|
}
|
|
|
|
$logtype = $suppress ? 'suppress' : 'delete';
|
|
|
|
$logEntry = new ManualLogEntry( $logtype, 'delete' );
|
|
$logEntry->setPerformer( $user );
|
|
$logEntry->setTarget( $title );
|
|
$logEntry->setComment( $logComment );
|
|
$logEntry->addTags( $tags );
|
|
$logid = $logEntry->insert();
|
|
$logEntry->publish( $logid );
|
|
|
|
$status->value = $logid;
|
|
}
|
|
} else {
|
|
$status = Status::newFatal( 'cannotdelete',
|
|
wfEscapeWikiText( $title->getPrefixedText() )
|
|
);
|
|
$services = MediaWikiServices::getInstance();
|
|
$page = $services->getWikiPageFactory()->newFromTitle( $title );
|
|
'@phan-var WikiFilePage $page';
|
|
$deleter = $services->getUserFactory()->newFromUserIdentity( $user );
|
|
$deletePage = $services->getDeletePageFactory()->newDeletePage( $page, $deleter );
|
|
$dbw = wfGetDB( DB_PRIMARY );
|
|
$dbw->startAtomic( __METHOD__ );
|
|
// delete the associated article first
|
|
$deleteStatus = $deletePage
|
|
->setSuppress( $suppress )
|
|
->setTags( $tags ?: [] )
|
|
->deleteUnsafe( $reason );
|
|
|
|
// DeletePage returns a non-fatal error status if the page
|
|
// or revision is missing, so check for isOK() rather than isGood().
|
|
if ( $deleteStatus->isOK() ) {
|
|
$status = $file->deleteFile( $reason, $user, $suppress );
|
|
if ( $status->isOK() ) {
|
|
if ( $deletePage->deletionWasScheduled() ) {
|
|
$status->value = false;
|
|
} else {
|
|
$deletedIDs = $deletePage->getSuccessfulDeletionsIDs();
|
|
if ( $deletedIDs ) {
|
|
$status->value = $deletedIDs[0];
|
|
} else {
|
|
// Means that the page/revision didn't exist, so create a log entry here.
|
|
$logtype = $suppress ? 'suppress' : 'delete';
|
|
$logEntry = new ManualLogEntry( $logtype, 'delete' );
|
|
$logEntry->setPerformer( $user );
|
|
$logEntry->setTarget( $title );
|
|
$logEntry->setComment( $reason );
|
|
$logEntry->addTags( $tags );
|
|
$logid = $logEntry->insert();
|
|
$dbw->onTransactionPreCommitOrIdle(
|
|
static function () use ( $logEntry, $logid ) {
|
|
$logEntry->publish( $logid );
|
|
},
|
|
__METHOD__
|
|
);
|
|
$status->value = $logid;
|
|
}
|
|
}
|
|
$dbw->endAtomic( __METHOD__ );
|
|
} else {
|
|
// Page deleted but file still there? rollback page delete
|
|
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
|
|
$lbFactory->rollbackPrimaryChanges( __METHOD__ );
|
|
}
|
|
} else {
|
|
$dbw->endAtomic( __METHOD__ );
|
|
}
|
|
}
|
|
|
|
if ( $status->isOK() ) {
|
|
$legacyUser = MediaWikiServices::getInstance()
|
|
->getUserFactory()
|
|
->newFromUserIdentity( $user );
|
|
Hooks::runner()->onFileDeleteComplete( $file, $oldimage, $page, $legacyUser, $reason );
|
|
}
|
|
|
|
return $status;
|
|
}
|
|
|
|
/**
|
|
* Is the provided `oldimage` value valid?
|
|
*
|
|
* @param string $oldimage
|
|
* @return bool
|
|
*/
|
|
public static function isValidOldSpec( $oldimage ) {
|
|
return strlen( $oldimage ) >= 16
|
|
&& strpos( $oldimage, '/' ) === false
|
|
&& strpos( $oldimage, '\\' ) === false;
|
|
}
|
|
}
|