wiki.techinc.nl/includes/FileDeleteForm.php
Daimona Eaytoy c0c213ba46 DeleteAction-related cleanup, part 3
Move all output-related code from Article to DeleteAction.
Article::doDelete is now deprecated, because there are some callers in
the wild, although I don't think any caller should need it.

Kill some ancient-PHP-style pass-by-refs that are useless and only make
the code more error-prone for both the caller and the callee.

Bug: T288282
Change-Id: Ic1de0ed8ebba15da5ed9f5cd11625017360a7672
2021-10-06 13:43:54 +02:00

150 lines
4.6 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
*/
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() )
);
$page = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title );
'@phan-var WikiFilePage $page';
$dbw = wfGetDB( DB_PRIMARY );
$dbw->startAtomic( __METHOD__ );
// delete the associated article first
$error = '';
$deleteStatus = $page->doDeleteArticleReal(
$reason,
$user,
$suppress,
null,
$error,
null,
$tags
);
// doDeleteArticleReal() 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 ( $deleteStatus->value === null ) {
// No log ID from doDeleteArticleReal(), probably
// because the page/revision didn't exist, so create
// one 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;
} else {
$status->value = $deleteStatus->value; // log id
}
$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;
}
}