Avoid 'message' in log context in AuthManager
[lhc/web/wiklou.git] / includes / api / ApiDelete.php
1 <?php
2 /**
3 *
4 *
5 * Created on Jun 30, 2007
6 *
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * API module that facilitates deleting pages. The API equivalent of action=delete.
29 * Requires API write mode to be enabled.
30 *
31 * @ingroup API
32 */
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.
40 */
41 public function execute() {
42 $this->useTransactionalTimeLimit();
43
44 $params = $this->extractRequestParams();
45
46 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
47 if ( !$pageObj->exists() ) {
48 $this->dieUsageMsg( 'notanarticle' );
49 }
50
51 $titleObj = $pageObj->getTitle();
52 $reason = $params['reason'];
53 $user = $this->getUser();
54
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] );
59 }
60
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 );
67 }
68 }
69
70 if ( $titleObj->getNamespace() == NS_FILE ) {
71 $status = self::deleteFile(
72 $pageObj,
73 $user,
74 $params['oldimage'],
75 $reason,
76 false
77 );
78 } else {
79 $status = self::delete( $pageObj, $user, $reason );
80 }
81
82 if ( is_array( $status ) ) {
83 $this->dieUsageMsg( $status[0] );
84 }
85 if ( !$status->isGood() ) {
86 $this->dieStatus( $status );
87 }
88
89 // Deprecated parameters
90 if ( $params['watch'] ) {
91 $watch = 'watch';
92 } elseif ( $params['unwatch'] ) {
93 $watch = 'unwatch';
94 } else {
95 $watch = $params['watchlist'];
96 }
97 $this->setWatch( $watch, $titleObj, 'watchdeletion' );
98
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 );
102 }
103
104 $r = [
105 'title' => $titleObj->getPrefixedText(),
106 'reason' => $reason,
107 'logid' => $status->value
108 ];
109 $this->getResult()->addValue( null, $this->getModuleName(), $r );
110 }
111
112 /**
113 * We have our own delete() function, since Article.php's implementation is split in two phases
114 *
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
119 */
120 protected static function delete( Page $page, User $user, &$reason = null ) {
121 $title = $page->getTitle();
122
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() ] ];
131 }
132 }
133
134 $error = '';
135
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 );
138 }
139
140 /**
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
147 */
148 protected static function deleteFile( Page $page, User $user, $oldimage,
149 &$reason = null, $suppress = false
150 ) {
151 $title = $page->getTitle();
152
153 $file = $page->getFile();
154 if ( !$file->exists() || !$file->isLocal() || $file->getRedirected() ) {
155 return self::delete( $page, $user, $reason );
156 }
157
158 if ( $oldimage ) {
159 if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
160 return [ [ 'invalidoldimage' ] ];
161 }
162 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
163 if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
164 return [ [ 'nodeleteablefile' ] ];
165 }
166 }
167
168 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
169 $reason = '';
170 }
171
172 return FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress, $user );
173 }
174
175 public function mustBePosted() {
176 return true;
177 }
178
179 public function isWriteMode() {
180 return true;
181 }
182
183 public function getAllowedParams() {
184 return [
185 'title' => null,
186 'pageid' => [
187 ApiBase::PARAM_TYPE => 'integer'
188 ],
189 'reason' => null,
190 'tags' => [
191 ApiBase::PARAM_TYPE => 'tags',
192 ApiBase::PARAM_ISMULTI => true,
193 ],
194 'watch' => [
195 ApiBase::PARAM_DFLT => false,
196 ApiBase::PARAM_DEPRECATED => true,
197 ],
198 'watchlist' => [
199 ApiBase::PARAM_DFLT => 'preferences',
200 ApiBase::PARAM_TYPE => [
201 'watch',
202 'unwatch',
203 'preferences',
204 'nochange'
205 ],
206 ],
207 'unwatch' => [
208 ApiBase::PARAM_DFLT => false,
209 ApiBase::PARAM_DEPRECATED => true,
210 ],
211 'oldimage' => null,
212 ];
213 }
214
215 public function needsToken() {
216 return 'csrf';
217 }
218
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',
225 ];
226 }
227
228 public function getHelpUrls() {
229 return 'https://www.mediawiki.org/wiki/API:Delete';
230 }
231 }