2007-08-17 20:53:17 +00:00
|
|
|
<?php
|
2012-05-14 17:59:58 +00:00
|
|
|
/**
|
2021-08-11 12:46:34 +00:00
|
|
|
* File deletion utilities.
|
2012-05-14 17:59:58 +00:00
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2021-05-28 22:39:28 +00:00
|
|
|
|
2017-01-18 20:55:20 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2021-06-01 23:27:25 +00:00
|
|
|
use MediaWiki\User\UserIdentity;
|
2007-08-17 20:53:17 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* File deletion user interface
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Media
|
2007-08-17 20:53:17 +00:00
|
|
|
*/
|
|
|
|
|
class FileDeleteForm {
|
2010-01-13 21:25:03 +00:00
|
|
|
/**
|
|
|
|
|
* Really delete the file
|
|
|
|
|
*
|
2017-08-11 00:23:16 +00:00
|
|
|
* @param Title &$title
|
2019-08-31 16:14:38 +00:00
|
|
|
* @param LocalFile &$file
|
2019-12-22 10:29:30 +00:00
|
|
|
* @param ?string &$oldimage Archive name
|
2014-04-08 15:29:17 +00:00
|
|
|
* @param string $reason Reason of the deletion
|
|
|
|
|
* @param bool $suppress Whether to mark all deleted versions as restricted
|
2021-06-01 23:27:25 +00:00
|
|
|
* @param UserIdentity $user
|
2020-11-20 13:12:54 +00:00
|
|
|
* @param string[] $tags Tags to apply to the deletion action
|
2012-10-07 23:35:26 +00:00
|
|
|
* @throws MWException
|
2016-10-19 16:54:25 +00:00
|
|
|
* @return Status
|
2010-01-13 21:25:03 +00:00
|
|
|
*/
|
2014-05-11 15:32:18 +00:00
|
|
|
public static function doDelete( &$title, &$file, &$oldimage, $reason,
|
2021-06-01 23:27:25 +00:00
|
|
|
$suppress, UserIdentity $user, $tags = []
|
2021-07-22 03:11:47 +00:00
|
|
|
): Status {
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( $oldimage ) {
|
2011-11-15 18:08:34 +00:00
|
|
|
$page = null;
|
2020-02-25 22:33:18 +00:00
|
|
|
$status = $file->deleteOldFile( $oldimage, $reason, $user, $suppress );
|
2019-08-31 16:14:38 +00:00
|
|
|
if ( $status->isOK() ) {
|
2008-04-04 12:16:50 +00:00
|
|
|
// Need to do a log item
|
2012-08-19 20:44:29 +00:00
|
|
|
$logComment = wfMessage( 'deletedrevision', $oldimage )->inContentLanguage()->text();
|
2013-04-20 22:49:30 +00:00
|
|
|
if ( trim( $reason ) != '' ) {
|
2012-08-19 20:44:29 +00:00
|
|
|
$logComment .= wfMessage( 'colon-separator' )
|
|
|
|
|
->inContentLanguage()->text() . $reason;
|
2010-08-29 19:11:23 +00:00
|
|
|
}
|
2012-07-01 20:09:11 +00:00
|
|
|
|
|
|
|
|
$logtype = $suppress ? 'suppress' : 'delete';
|
|
|
|
|
|
|
|
|
|
$logEntry = new ManualLogEntry( $logtype, 'delete' );
|
|
|
|
|
$logEntry->setPerformer( $user );
|
|
|
|
|
$logEntry->setTarget( $title );
|
|
|
|
|
$logEntry->setComment( $logComment );
|
2019-07-07 22:07:56 +00:00
|
|
|
$logEntry->addTags( $tags );
|
2012-07-01 20:09:11 +00:00
|
|
|
$logid = $logEntry->insert();
|
|
|
|
|
$logEntry->publish( $logid );
|
2015-10-07 14:19:36 +00:00
|
|
|
|
|
|
|
|
$status->value = $logid;
|
2008-04-04 12:16:50 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2012-02-06 15:21:10 +00:00
|
|
|
$status = Status::newFatal( 'cannotdelete',
|
|
|
|
|
wfEscapeWikiText( $title->getPrefixedText() )
|
|
|
|
|
);
|
2020-11-11 21:43:18 +00:00
|
|
|
$page = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title );
|
|
|
|
|
'@phan-var WikiFilePage $page';
|
2021-04-29 02:37:11 +00:00
|
|
|
$dbw = wfGetDB( DB_PRIMARY );
|
2016-08-19 20:17:33 +00:00
|
|
|
$dbw->startAtomic( __METHOD__ );
|
|
|
|
|
// delete the associated article first
|
|
|
|
|
$error = '';
|
2020-03-19 00:47:20 +00:00
|
|
|
$deleteStatus = $page->doDeleteArticleReal(
|
|
|
|
|
$reason,
|
|
|
|
|
$user,
|
|
|
|
|
$suppress,
|
|
|
|
|
null,
|
|
|
|
|
$error,
|
|
|
|
|
null,
|
|
|
|
|
$tags
|
|
|
|
|
);
|
2016-08-19 20:17:33 +00:00
|
|
|
// doDeleteArticleReal() returns a non-fatal error status if the page
|
|
|
|
|
// or revision is missing, so check for isOK() rather than isGood()
|
|
|
|
|
if ( $deleteStatus->isOK() ) {
|
2020-03-20 20:46:28 +00:00
|
|
|
$status = $file->deleteFile( $reason, $user, $suppress );
|
2016-08-19 20:17:33 +00:00
|
|
|
if ( $status->isOK() ) {
|
2017-06-12 17:19:57 +00:00
|
|
|
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( clone $title );
|
|
|
|
|
$logEntry->setComment( $reason );
|
2019-07-07 22:07:56 +00:00
|
|
|
$logEntry->addTags( $tags );
|
2017-06-12 17:19:57 +00:00
|
|
|
$logid = $logEntry->insert();
|
|
|
|
|
$dbw->onTransactionPreCommitOrIdle(
|
2021-02-10 22:31:02 +00:00
|
|
|
static function () use ( $logEntry, $logid ) {
|
2017-06-12 17:19:57 +00:00
|
|
|
$logEntry->publish( $logid );
|
|
|
|
|
},
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
$status->value = $logid;
|
|
|
|
|
} else {
|
|
|
|
|
$status->value = $deleteStatus->value; // log id
|
|
|
|
|
}
|
2015-10-07 18:48:23 +00:00
|
|
|
$dbw->endAtomic( __METHOD__ );
|
2016-08-19 20:17:33 +00:00
|
|
|
} else {
|
|
|
|
|
// Page deleted but file still there? rollback page delete
|
2017-02-07 13:31:46 +00:00
|
|
|
$lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
|
2021-09-02 19:14:52 +00:00
|
|
|
$lbFactory->rollbackPrimaryChanges( __METHOD__ );
|
2008-04-04 12:16:50 +00:00
|
|
|
}
|
2016-08-19 20:17:33 +00:00
|
|
|
} else {
|
|
|
|
|
$dbw->endAtomic( __METHOD__ );
|
2008-04-04 12:16:50 +00:00
|
|
|
}
|
|
|
|
|
}
|
2011-11-15 18:08:34 +00:00
|
|
|
|
2012-02-06 15:21:10 +00:00
|
|
|
if ( $status->isOK() ) {
|
2021-06-01 23:27:25 +00:00
|
|
|
$legacyUser = MediaWikiServices::getInstance()
|
|
|
|
|
->getUserFactory()
|
|
|
|
|
->newFromUserIdentity( $user );
|
|
|
|
|
Hooks::runner()->onFileDeleteComplete( $file, $oldimage, $page, $legacyUser, $reason );
|
2011-11-15 18:08:34 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2008-04-04 12:16:50 +00:00
|
|
|
return $status;
|
|
|
|
|
}
|
2007-09-13 08:51:13 +00:00
|
|
|
|
2007-08-17 20:53:17 +00:00
|
|
|
/**
|
|
|
|
|
* Is the provided `oldimage` value valid?
|
|
|
|
|
*
|
2014-04-20 19:16:57 +00:00
|
|
|
* @param string $oldimage
|
2007-08-17 20:53:17 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2013-02-03 20:05:24 +00:00
|
|
|
public static function isValidOldSpec( $oldimage ) {
|
2008-04-04 13:59:04 +00:00
|
|
|
return strlen( $oldimage ) >= 16
|
|
|
|
|
&& strpos( $oldimage, '/' ) === false
|
|
|
|
|
&& strpos( $oldimage, '\\' ) === false;
|
2007-08-17 20:53:17 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2007-08-17 20:53:17 +00:00
|
|
|
/**
|
|
|
|
|
* Could we delete the file specified? If an `oldimage`
|
|
|
|
|
* value was provided, does it correspond to an
|
|
|
|
|
* existing, local, old version of this file?
|
|
|
|
|
*
|
2019-09-04 18:46:57 +00:00
|
|
|
* @param LocalFile &$file
|
|
|
|
|
* @param LocalFile &$oldfile
|
2020-05-07 19:55:06 +00:00
|
|
|
* @param string $oldimage
|
2007-08-17 20:53:17 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
2013-02-03 20:05:24 +00:00
|
|
|
public static function haveDeletableFile( &$file, &$oldfile, $oldimage ) {
|
2008-04-04 13:59:04 +00:00
|
|
|
return $oldimage
|
|
|
|
|
? $oldfile && $oldfile->exists() && $oldfile->isLocal()
|
|
|
|
|
: $file && $file->exists() && $file->isLocal();
|
2007-08-17 20:53:17 +00:00
|
|
|
}
|
2008-01-25 10:15:46 +00:00
|
|
|
}
|