3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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
24 * API module that facilitates deleting pages. The API equivalent of action=delete.
25 * Requires API write mode to be enabled.
29 class ApiDelete
extends ApiBase
{
31 * Extracts the title and reason from the request parameters and invokes
32 * the local delete() function with these as arguments. It does not make use of
33 * the delete function specified by Article.php. If the deletion succeeds, the
34 * details of the article deleted and the reason for deletion are added to the
37 public function execute() {
38 $this->useTransactionalTimeLimit();
40 $params = $this->extractRequestParams();
42 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
43 $titleObj = $pageObj->getTitle();
44 if ( !$pageObj->exists() &&
45 !( $titleObj->getNamespace() == NS_FILE
&& self
::canDeleteFile( $pageObj->getFile() ) )
47 $this->dieWithError( 'apierror-missingtitle' );
50 $reason = $params['reason'];
51 $user = $this->getUser();
53 // Check that the user is allowed to carry out the deletion
54 $this->checkTitleUserPermissions( $titleObj, 'delete' );
56 // If change tagging was requested, check that the user is allowed to tag,
57 // and the tags are valid
58 if ( $params['tags'] ) {
59 $tagStatus = ChangeTags
::canAddTagsAccompanyingChange( $params['tags'], $user );
60 if ( !$tagStatus->isOK() ) {
61 $this->dieStatus( $tagStatus );
65 if ( $titleObj->getNamespace() == NS_FILE
) {
66 $status = self
::deleteFile(
75 $status = self
::delete( $pageObj, $user, $reason, $params['tags'] );
78 if ( !$status->isGood() ) {
79 $this->dieStatus( $status );
82 // Deprecated parameters
83 if ( $params['watch'] ) {
85 } elseif ( $params['unwatch'] ) {
88 $watch = $params['watchlist'];
90 $this->setWatch( $watch, $titleObj, 'watchdeletion' );
93 'title' => $titleObj->getPrefixedText(),
95 'logid' => $status->value
97 $this->getResult()->addValue( null, $this->getModuleName(), $r );
101 * We have our own delete() function, since Article.php's implementation is split in two phases
103 * @param Page|WikiPage $page Page or WikiPage object to work on
104 * @param User $user User doing the action
105 * @param string|null &$reason Reason for the deletion. Autogenerated if null
106 * @param array $tags Tags to tag the deletion with
109 protected static function delete( Page
$page, User
$user, &$reason = null, $tags = [] ) {
110 $title = $page->getTitle();
112 // Auto-generate a summary, if necessary
113 if ( is_null( $reason ) ) {
114 // Need to pass a throwaway variable because generateReason expects
117 $reason = $page->getAutoDeleteReason( $hasHistory );
118 if ( $reason === false ) {
119 // Should be reachable only if the page has no revisions
120 return Status
::newFatal( 'cannotdelete', $title->getPrefixedText() ); // @codeCoverageIgnore
126 // Luckily, Article.php provides a reusable delete function that does the hard work for us
127 return $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user, $tags );
134 protected static function canDeleteFile( File
$file ) {
135 return $file->exists() && $file->isLocal() && !$file->getRedirected();
139 * @param Page $page Object to work on
140 * @param User $user User doing the action
141 * @param string $oldimage Archive name
142 * @param string|null &$reason Reason for the deletion. Autogenerated if null.
143 * @param bool $suppress Whether to mark all deleted versions as restricted
144 * @param array $tags Tags to tag the deletion with
147 protected static function deleteFile( Page
$page, User
$user, $oldimage,
148 &$reason = null, $suppress = false, $tags = []
150 $title = $page->getTitle();
152 $file = $page->getFile();
153 if ( !self
::canDeleteFile( $file ) ) {
154 return self
::delete( $page, $user, $reason, $tags );
158 if ( !FileDeleteForm
::isValidOldSpec( $oldimage ) ) {
159 return Status
::newFatal( 'invalidoldimage' );
161 $oldfile = RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
162 if ( !$oldfile->exists() ||
!$oldfile->isLocal() ||
$oldfile->getRedirected() ) {
163 return Status
::newFatal( 'nodeleteablefile' );
167 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
171 return FileDeleteForm
::doDelete( $title, $file, $oldimage, $reason, $suppress, $user, $tags );
174 public function mustBePosted() {
178 public function isWriteMode() {
182 public function getAllowedParams() {
186 ApiBase
::PARAM_TYPE
=> 'integer'
190 ApiBase
::PARAM_TYPE
=> 'tags',
191 ApiBase
::PARAM_ISMULTI
=> true,
194 ApiBase
::PARAM_DFLT
=> false,
195 ApiBase
::PARAM_DEPRECATED
=> true,
198 ApiBase
::PARAM_DFLT
=> 'preferences',
199 ApiBase
::PARAM_TYPE
=> [
207 ApiBase
::PARAM_DFLT
=> false,
208 ApiBase
::PARAM_DEPRECATED
=> true,
214 public function needsToken() {
218 protected function getExamplesMessages() {
220 'action=delete&title=Main%20Page&token=123ABC'
221 => 'apihelp-delete-example-simple',
222 'action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
223 => 'apihelp-delete-example-reason',
227 public function getHelpUrls() {
228 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Delete';