User::isSafeToLoad() should return false if MW_NO_SESSION
[mediawiki.git] / includes / api / ApiDelete.php
blob77911b04b50d9deb7edfeed1cdf0215d5a735f57
1 <?php
2 /**
5 * Created on Jun 30, 2007
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @file
27 /**
28 * API module that facilitates deleting pages. The API equivalent of action=delete.
29 * Requires API write mode to be enabled.
31 * @ingroup API
33 class ApiDelete extends ApiBase {
34 /**
35 * Extracts the title and reason from the request parameters and invokes
36 * the local delete() function with these as arguments. It does not make use of
37 * the delete function specified by Article.php. If the deletion succeeds, the
38 * details of the article deleted and the reason for deletion are added to the
39 * result object.
41 public function execute() {
42 $this->useTransactionalTimeLimit();
44 $params = $this->extractRequestParams();
46 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
47 if ( !$pageObj->exists() ) {
48 $this->dieUsageMsg( 'notanarticle' );
51 $titleObj = $pageObj->getTitle();
52 $reason = $params['reason'];
53 $user = $this->getUser();
55 // Check that the user is allowed to carry out the deletion
56 $errors = $titleObj->getUserPermissionsErrors( 'delete', $user );
57 if ( count( $errors ) ) {
58 $this->dieUsageMsg( $errors[0] );
61 // If change tagging was requested, check that the user is allowed to tag,
62 // and the tags are valid
63 if ( count( $params['tags'] ) ) {
64 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
65 if ( !$tagStatus->isOK() ) {
66 $this->dieStatus( $tagStatus );
70 if ( $titleObj->getNamespace() == NS_FILE ) {
71 $status = self::deleteFile(
72 $pageObj,
73 $user,
74 $params['oldimage'],
75 $reason,
76 false
78 } else {
79 $status = self::delete( $pageObj, $user, $reason );
82 if ( is_array( $status ) ) {
83 $this->dieUsageMsg( $status[0] );
85 if ( !$status->isGood() ) {
86 $this->dieStatus( $status );
89 // Deprecated parameters
90 if ( $params['watch'] ) {
91 $watch = 'watch';
92 } elseif ( $params['unwatch'] ) {
93 $watch = 'unwatch';
94 } else {
95 $watch = $params['watchlist'];
97 $this->setWatch( $watch, $titleObj, 'watchdeletion' );
99 // Apply change tags to the log entry, if requested
100 if ( count( $params['tags'] ) ) {
101 ChangeTags::addTags( $params['tags'], null, null, $status->value, null );
104 $r = [
105 'title' => $titleObj->getPrefixedText(),
106 'reason' => $reason,
107 'logid' => $status->value
109 $this->getResult()->addValue( null, $this->getModuleName(), $r );
113 * We have our own delete() function, since Article.php's implementation is split in two phases
115 * @param Page|WikiPage $page Page or WikiPage object to work on
116 * @param User $user User doing the action
117 * @param string|null $reason Reason for the deletion. Autogenerated if null
118 * @return Status|array
120 protected static function delete( Page $page, User $user, &$reason = null ) {
121 $title = $page->getTitle();
123 // Auto-generate a summary, if necessary
124 if ( is_null( $reason ) ) {
125 // Need to pass a throwaway variable because generateReason expects
126 // a reference
127 $hasHistory = false;
128 $reason = $page->getAutoDeleteReason( $hasHistory );
129 if ( $reason === false ) {
130 return [ [ 'cannotdelete', $title->getPrefixedText() ] ];
134 $error = '';
136 // Luckily, Article.php provides a reusable delete function that does the hard work for us
137 return $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user );
141 * @param Page $page Object to work on
142 * @param User $user User doing the action
143 * @param string $oldimage Archive name
144 * @param string $reason Reason for the deletion. Autogenerated if null.
145 * @param bool $suppress Whether to mark all deleted versions as restricted
146 * @return Status|array
148 protected static function deleteFile( Page $page, User $user, $oldimage,
149 &$reason = null, $suppress = false
151 $title = $page->getTitle();
153 $file = $page->getFile();
154 if ( !$file->exists() || !$file->isLocal() || $file->getRedirected() ) {
155 return self::delete( $page, $user, $reason );
158 if ( $oldimage ) {
159 if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
160 return [ [ 'invalidoldimage' ] ];
162 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
163 if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
164 return [ [ 'nodeleteablefile' ] ];
168 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
169 $reason = '';
172 return FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress, $user );
175 public function mustBePosted() {
176 return true;
179 public function isWriteMode() {
180 return true;
183 public function getAllowedParams() {
184 return [
185 'title' => null,
186 'pageid' => [
187 ApiBase::PARAM_TYPE => 'integer'
189 'reason' => null,
190 'tags' => [
191 ApiBase::PARAM_TYPE => 'tags',
192 ApiBase::PARAM_ISMULTI => true,
194 'watch' => [
195 ApiBase::PARAM_DFLT => false,
196 ApiBase::PARAM_DEPRECATED => true,
198 'watchlist' => [
199 ApiBase::PARAM_DFLT => 'preferences',
200 ApiBase::PARAM_TYPE => [
201 'watch',
202 'unwatch',
203 'preferences',
204 'nochange'
207 'unwatch' => [
208 ApiBase::PARAM_DFLT => false,
209 ApiBase::PARAM_DEPRECATED => true,
211 'oldimage' => null,
215 public function needsToken() {
216 return 'csrf';
219 protected function getExamplesMessages() {
220 return [
221 'action=delete&title=Main%20Page&token=123ABC'
222 => 'apihelp-delete-example-simple',
223 'action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
224 => 'apihelp-delete-example-reason',
228 public function getHelpUrls() {
229 return 'https://www.mediawiki.org/wiki/API:Delete';