2007-12-06 16:06:22 +00:00
|
|
|
<?php
|
2010-02-19 12:54:09 +00:00
|
|
|
/**
|
2012-07-15 20:13:02 +00:00
|
|
|
* Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
|
2007-12-06 16:06:22 +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.,
|
2010-06-21 13:13:32 +00:00
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2007-12-06 16:06:22 +00:00
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-08-07 19:59:42 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
2007-12-06 16:06:22 +00:00
|
|
|
*/
|
|
|
|
|
|
2020-03-08 21:32:26 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
2007-12-06 16:06:22 +00:00
|
|
|
/**
|
2010-10-20 18:50:33 +00:00
|
|
|
* API module that facilitates deleting pages. The API equivalent of action=delete.
|
2008-01-12 07:08:17 +00:00
|
|
|
* Requires API write mode to be enabled.
|
|
|
|
|
*
|
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 API
|
2007-12-06 16:06:22 +00:00
|
|
|
*/
|
|
|
|
|
class ApiDelete extends ApiBase {
|
|
|
|
|
/**
|
2015-10-08 07:34:27 +00:00
|
|
|
* Extracts the title and reason from the request parameters and invokes
|
2008-04-14 07:45:50 +00:00
|
|
|
* the local delete() function with these as arguments. It does not make use of
|
|
|
|
|
* the delete function specified by Article.php. If the deletion succeeds, the
|
|
|
|
|
* details of the article deleted and the reason for deletion are added to the
|
2008-01-12 07:08:17 +00:00
|
|
|
* result object.
|
2007-12-06 16:06:22 +00:00
|
|
|
*/
|
|
|
|
|
public function execute() {
|
2015-08-08 00:08:33 +00:00
|
|
|
$this->useTransactionalTimeLimit();
|
|
|
|
|
|
2007-12-06 16:06:22 +00:00
|
|
|
$params = $this->extractRequestParams();
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2012-04-28 13:45:37 +00:00
|
|
|
$pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
|
2017-06-12 17:09:26 +00:00
|
|
|
$titleObj = $pageObj->getTitle();
|
|
|
|
|
if ( !$pageObj->exists() &&
|
2019-08-31 16:14:38 +00:00
|
|
|
// @phan-suppress-next-line PhanUndeclaredMethod
|
2017-06-12 17:09:26 +00:00
|
|
|
!( $titleObj->getNamespace() == NS_FILE && self::canDeleteFile( $pageObj->getFile() ) )
|
|
|
|
|
) {
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->dieWithError( 'apierror-missingtitle' );
|
2010-02-19 12:54:09 +00:00
|
|
|
}
|
2007-12-06 16:06:22 +00:00
|
|
|
|
2012-07-07 07:12:04 +00:00
|
|
|
$reason = $params['reason'];
|
2011-11-16 15:57:56 +00:00
|
|
|
$user = $this->getUser();
|
|
|
|
|
|
2015-10-08 07:34:27 +00:00
|
|
|
// Check that the user is allowed to carry out the deletion
|
2016-10-19 16:54:25 +00:00
|
|
|
$this->checkTitleUserPermissions( $titleObj, 'delete' );
|
2015-10-08 07:34:27 +00:00
|
|
|
|
|
|
|
|
// If change tagging was requested, check that the user is allowed to tag,
|
|
|
|
|
// and the tags are valid
|
2017-12-04 18:36:48 +00:00
|
|
|
if ( $params['tags'] ) {
|
2015-10-08 07:34:27 +00:00
|
|
|
$tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
|
|
|
|
|
if ( !$tagStatus->isOK() ) {
|
|
|
|
|
$this->dieStatus( $tagStatus );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-11 15:55:52 +00:00
|
|
|
if ( $titleObj->getNamespace() == NS_FILE ) {
|
2013-11-16 19:09:17 +00:00
|
|
|
$status = self::deleteFile(
|
|
|
|
|
$pageObj,
|
|
|
|
|
$user,
|
|
|
|
|
$params['oldimage'],
|
|
|
|
|
$reason,
|
2016-09-21 17:18:08 +00:00
|
|
|
false,
|
|
|
|
|
$params['tags']
|
2013-11-16 19:09:17 +00:00
|
|
|
);
|
2008-05-19 21:30:03 +00:00
|
|
|
} else {
|
2016-09-21 17:18:08 +00:00
|
|
|
$status = self::delete( $pageObj, $user, $reason, $params['tags'] );
|
2011-11-16 15:57:56 +00:00
|
|
|
}
|
2010-02-19 12:54:09 +00:00
|
|
|
|
2019-03-29 23:20:50 +00:00
|
|
|
if ( !$status->isOK() ) {
|
2013-06-13 17:56:29 +00:00
|
|
|
$this->dieStatus( $status );
|
2011-11-16 15:57:56 +00:00
|
|
|
}
|
2018-12-20 14:59:02 +00:00
|
|
|
$this->addMessagesFromStatus( $status, [ 'warning' ], [ 'delete-scheduled' ] );
|
2010-02-19 01:25:57 +00:00
|
|
|
|
2011-11-16 15:57:56 +00:00
|
|
|
// Deprecated parameters
|
|
|
|
|
if ( $params['watch'] ) {
|
|
|
|
|
$watch = 'watch';
|
|
|
|
|
} elseif ( $params['unwatch'] ) {
|
|
|
|
|
$watch = 'unwatch';
|
|
|
|
|
} else {
|
|
|
|
|
$watch = $params['watchlist'];
|
2008-05-19 21:30:03 +00:00
|
|
|
}
|
2011-11-16 15:57:56 +00:00
|
|
|
$this->setWatch( $watch, $titleObj, 'watchdeletion' );
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$r = [
|
2012-05-14 01:20:05 +00:00
|
|
|
'title' => $titleObj->getPrefixedText(),
|
|
|
|
|
'reason' => $reason,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2018-12-20 14:59:02 +00:00
|
|
|
if ( $status->hasMessage( 'delete-scheduled' ) ) {
|
|
|
|
|
$r['scheduled'] = true;
|
|
|
|
|
}
|
|
|
|
|
if ( $status->value !== null ) {
|
|
|
|
|
// Scheduled deletions don't currently have a log entry available at this point
|
|
|
|
|
$r['logid'] = $status->value;
|
|
|
|
|
}
|
2010-01-11 15:55:52 +00:00
|
|
|
$this->getResult()->addValue( null, $this->getModuleName(), $r );
|
2007-12-06 16:06:22 +00:00
|
|
|
}
|
2008-01-12 07:08:17 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* We have our own delete() function, since Article.php's implementation is split in two phases
|
|
|
|
|
*
|
2020-03-17 16:56:06 +00:00
|
|
|
* @param WikiPage $page WikiPage object to work on
|
2014-04-15 18:12:09 +00:00
|
|
|
* @param User $user User doing the action
|
2017-08-11 00:23:16 +00:00
|
|
|
* @param string|null &$reason Reason for the deletion. Autogenerated if null
|
2016-09-21 17:18:08 +00:00
|
|
|
* @param array $tags Tags to tag the deletion with
|
2016-10-19 16:54:25 +00:00
|
|
|
* @return Status
|
2008-01-12 07:08:17 +00:00
|
|
|
*/
|
2020-03-17 16:56:06 +00:00
|
|
|
private static function delete( WikiPage $page, User $user, &$reason = null, $tags = [] ) {
|
2011-11-16 15:57:56 +00:00
|
|
|
$title = $page->getTitle();
|
2008-01-12 07:08:17 +00:00
|
|
|
|
|
|
|
|
// Auto-generate a summary, if necessary
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $reason === null ) {
|
2011-04-13 23:36:27 +00:00
|
|
|
// Need to pass a throwaway variable because generateReason expects
|
|
|
|
|
// a reference
|
|
|
|
|
$hasHistory = false;
|
2012-10-10 18:13:40 +00:00
|
|
|
$reason = $page->getAutoDeleteReason( $hasHistory );
|
2010-02-19 12:54:09 +00:00
|
|
|
if ( $reason === false ) {
|
2018-03-18 19:58:02 +00:00
|
|
|
// Should be reachable only if the page has no revisions
|
|
|
|
|
return Status::newFatal( 'cannotdelete', $title->getPrefixedText() ); // @codeCoverageIgnore
|
2010-02-19 12:54:09 +00:00
|
|
|
}
|
2008-01-12 07:08:17 +00:00
|
|
|
}
|
2009-01-29 01:04:00 +00:00
|
|
|
|
2011-04-13 23:36:27 +00:00
|
|
|
$error = '';
|
2013-11-14 12:40:22 +00:00
|
|
|
|
2020-03-17 16:56:06 +00:00
|
|
|
// Luckily, WikiPage provides a reusable delete function that does the hard work for us
|
|
|
|
|
return $page->doDeleteArticleReal(
|
|
|
|
|
$reason,
|
|
|
|
|
$user,
|
|
|
|
|
false, // don't suppress
|
|
|
|
|
null, // unused
|
|
|
|
|
$error,
|
|
|
|
|
null, // unused
|
|
|
|
|
$tags
|
|
|
|
|
);
|
2008-01-12 07:08:17 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2017-06-12 17:09:26 +00:00
|
|
|
/**
|
|
|
|
|
* @param File $file
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
protected static function canDeleteFile( File $file ) {
|
|
|
|
|
return $file->exists() && $file->isLocal() && !$file->getRedirected();
|
|
|
|
|
}
|
|
|
|
|
|
2010-10-09 00:01:45 +00:00
|
|
|
/**
|
2020-03-17 16:56:06 +00:00
|
|
|
* @param WikiPage $page Object to work on
|
2013-11-17 19:04:13 +00:00
|
|
|
* @param User $user User doing the action
|
|
|
|
|
* @param string $oldimage Archive name
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param string|null &$reason Reason for the deletion. Autogenerated if null.
|
2013-11-17 19:04:13 +00:00
|
|
|
* @param bool $suppress Whether to mark all deleted versions as restricted
|
2016-09-21 17:18:08 +00:00
|
|
|
* @param array $tags Tags to tag the deletion with
|
2016-10-19 16:54:25 +00:00
|
|
|
* @return Status
|
2010-10-09 00:01:45 +00:00
|
|
|
*/
|
2020-03-17 16:56:06 +00:00
|
|
|
private static function deleteFile( WikiPage $page, User $user, $oldimage,
|
2016-09-21 17:18:08 +00:00
|
|
|
&$reason = null, $suppress = false, $tags = []
|
2013-11-16 19:09:17 +00:00
|
|
|
) {
|
2011-11-16 15:57:56 +00:00
|
|
|
$title = $page->getTitle();
|
2008-05-19 21:30:03 +00:00
|
|
|
|
2019-08-31 16:14:38 +00:00
|
|
|
// @phan-suppress-next-line PhanUndeclaredMethod There's no right typehint for it
|
2011-11-16 15:57:56 +00:00
|
|
|
$file = $page->getFile();
|
2017-06-12 17:09:26 +00:00
|
|
|
if ( !self::canDeleteFile( $file ) ) {
|
2016-09-21 17:18:08 +00:00
|
|
|
return self::delete( $page, $user, $reason, $tags );
|
2010-02-19 12:54:09 +00:00
|
|
|
}
|
2008-05-19 21:30:03 +00:00
|
|
|
|
2010-02-19 12:54:09 +00:00
|
|
|
if ( $oldimage ) {
|
2011-11-16 15:57:56 +00:00
|
|
|
if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
return Status::newFatal( 'invalidoldimage' );
|
2011-11-16 15:57:56 +00:00
|
|
|
}
|
2020-03-08 21:32:26 +00:00
|
|
|
$oldfile = MediaWikiServices::getInstance()->getRepoGroup()
|
|
|
|
|
->getLocalRepo()->newFromArchiveName( $title, $oldimage );
|
2011-11-16 15:57:56 +00:00
|
|
|
if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
|
2016-10-19 16:54:25 +00:00
|
|
|
return Status::newFatal( 'nodeleteablefile' );
|
2011-11-16 15:57:56 +00:00
|
|
|
}
|
2010-02-19 12:54:09 +00:00
|
|
|
}
|
|
|
|
|
|
2020-01-09 23:48:34 +00:00
|
|
|
if ( $reason === null ) { // Log and RC don't like null reasons
|
2008-12-13 21:07:18 +00:00
|
|
|
$reason = '';
|
2010-02-19 12:54:09 +00:00
|
|
|
}
|
2013-11-14 12:40:22 +00:00
|
|
|
|
2016-09-21 17:18:08 +00:00
|
|
|
return FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress, $user, $tags );
|
2008-05-19 21:30:03 +00:00
|
|
|
}
|
2010-02-19 12:54:09 +00:00
|
|
|
|
2010-02-14 21:12:11 +00:00
|
|
|
public function mustBePosted() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2009-03-06 13:49:44 +00:00
|
|
|
public function isWriteMode() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-28 19:05:26 +00:00
|
|
|
public function getAllowedParams() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2007-12-06 16:06:22 +00:00
|
|
|
'title' => null,
|
2016-02-17 09:09:32 +00:00
|
|
|
'pageid' => [
|
2008-10-07 14:57:59 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'integer'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2007-12-06 16:06:22 +00:00
|
|
|
'reason' => null,
|
2016-02-17 09:09:32 +00:00
|
|
|
'tags' => [
|
2016-01-20 09:51:01 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'tags',
|
2015-10-08 07:34:27 +00:00
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'watch' => [
|
2010-03-25 22:15:08 +00:00
|
|
|
ApiBase::PARAM_DFLT => false,
|
|
|
|
|
ApiBase::PARAM_DEPRECATED => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
'watchlist' => [
|
2010-03-25 22:15:08 +00:00
|
|
|
ApiBase::PARAM_DFLT => 'preferences',
|
2016-02-17 09:09:32 +00:00
|
|
|
ApiBase::PARAM_TYPE => [
|
2010-03-25 22:15:08 +00:00
|
|
|
'watch',
|
|
|
|
|
'unwatch',
|
|
|
|
|
'preferences',
|
|
|
|
|
'nochange'
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
'unwatch' => [
|
2010-10-09 13:59:15 +00:00
|
|
|
ApiBase::PARAM_DFLT => false,
|
|
|
|
|
ApiBase::PARAM_DEPRECATED => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2010-10-13 23:11:40 +00:00
|
|
|
'oldimage' => null,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2007-12-06 16:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
2010-10-01 20:12:50 +00:00
|
|
|
public function needsToken() {
|
2014-08-08 16:56:07 +00:00
|
|
|
return 'csrf';
|
2010-02-14 22:20:27 +00:00
|
|
|
}
|
2007-12-06 16:06:22 +00:00
|
|
|
|
2014-10-28 17:17:02 +00:00
|
|
|
protected function getExamplesMessages() {
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
2014-09-18 17:38:23 +00:00
|
|
|
'action=delete&title=Main%20Page&token=123ABC'
|
|
|
|
|
=> 'apihelp-delete-example-simple',
|
|
|
|
|
'action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
|
|
|
|
|
=> 'apihelp-delete-example-reason',
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2007-12-06 16:06:22 +00:00
|
|
|
}
|
|
|
|
|
|
2011-07-17 16:38:24 +00:00
|
|
|
public function getHelpUrls() {
|
2017-04-04 22:52:57 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Delete';
|
2011-07-17 16:38:24 +00:00
|
|
|
}
|
2011-03-23 21:54:59 +00:00
|
|
|
}
|