Merge "(bug 31313) Clarify watchlist preference messages"
[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 public function __construct( $main, $action ) {
36 parent::__construct( $main, $action );
37 }
38
39 /**
40 * Extracts the title, token, and reason from the request parameters and invokes
41 * the local delete() function with these as arguments. It does not make use of
42 * the delete function specified by Article.php. If the deletion succeeds, the
43 * details of the article deleted and the reason for deletion are added to the
44 * result object.
45 */
46 public function execute() {
47 $params = $this->extractRequestParams();
48
49 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
50 if ( !$pageObj->exists() ) {
51 $this->dieUsageMsg( 'notanarticle' );
52 }
53
54 $titleObj = $pageObj->getTitle();
55 $reason = ( isset( $params['reason'] ) ? $params['reason'] : null );
56 $user = $this->getUser();
57
58 if ( $titleObj->getNamespace() == NS_FILE ) {
59 $status = self::deleteFile( $pageObj, $user, $params['token'], $params['oldimage'], $reason, false );
60 } else {
61 $status = self::delete( $pageObj, $user, $params['token'], $reason );
62 }
63
64 if ( !$status->isGood() ) {
65 $errors = $this->getErrorsArray();
66 $this->dieUsageMsg( $errors[0] ); // We don't care about multiple errors, just report one of them
67 }
68
69 // Deprecated parameters
70 if ( $params['watch'] ) {
71 $watch = 'watch';
72 } elseif ( $params['unwatch'] ) {
73 $watch = 'unwatch';
74 } else {
75 $watch = $params['watchlist'];
76 }
77 $this->setWatch( $watch, $titleObj, 'watchdeletion' );
78
79 $r = array(
80 'title' => $titleObj->getPrefixedText(),
81 'reason' => $reason,
82 'logid' => $status->value
83 );
84 $this->getResult()->addValue( null, $this->getModuleName(), $r );
85 }
86
87 /**
88 * @param $title Title
89 * @param $user User doing the action
90 * @param $token String
91 * @return array
92 */
93 private static function getPermissionsError( $title, $user, $token ) {
94 // Check permissions
95 return $title->getUserPermissionsErrors( 'delete', $user );
96 }
97
98 /**
99 * We have our own delete() function, since Article.php's implementation is split in two phases
100 *
101 * @param $page WikiPage object to work on
102 * @param $user User doing the action
103 * @param $token String: delete token (same as edit token)
104 * @param $reason String: reason for the deletion. Autogenerated if NULL
105 * @return Status
106 */
107 public static function delete( Page $page, User $user, $token, &$reason = null ) {
108 $title = $page->getTitle();
109 $errors = self::getPermissionsError( $title, $user, $token );
110 if ( count( $errors ) ) {
111 return $errors;
112 }
113
114 // Auto-generate a summary, if necessary
115 if ( is_null( $reason ) ) {
116 // Need to pass a throwaway variable because generateReason expects
117 // a reference
118 $hasHistory = false;
119 $reason = $page->getAutoDeleteReason( $hasHistory );
120 if ( $reason === false ) {
121 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
122 }
123 }
124
125 $error = '';
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 );
128 }
129
130 /**
131 * @param $page WikiPage object to work on
132 * @param $user User doing the action
133 * @param $token
134 * @param $oldimage
135 * @param $reason
136 * @param $suppress bool
137 * @return Status
138 */
139 public static function deleteFile( Page $page, User $user, $token, $oldimage, &$reason = null, $suppress = false ) {
140 $title = $page->getTitle();
141 $errors = self::getPermissionsError( $title, $user, $token );
142 if ( count( $errors ) ) {
143 return $errors;
144 }
145
146 $file = $page->getFile();
147 if ( !$file->exists() || !$file->isLocal() || $file->getRedirected() ) {
148 return self::delete( $page, $user, $token, $reason );
149 }
150
151 if ( $oldimage ) {
152 if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
153 return array( array( 'invalidoldimage' ) );
154 }
155 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
156 if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
157 return array( array( 'nodeleteablefile' ) );
158 }
159 } else {
160 $oldfile = false;
161 }
162
163 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
164 $reason = '';
165 }
166 return FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress );
167 }
168
169 public function mustBePosted() {
170 return true;
171 }
172
173 public function isWriteMode() {
174 return true;
175 }
176
177 public function getAllowedParams() {
178 return array(
179 'title' => null,
180 'pageid' => array(
181 ApiBase::PARAM_TYPE => 'integer'
182 ),
183 'token' => null,
184 'reason' => null,
185 'watch' => array(
186 ApiBase::PARAM_DFLT => false,
187 ApiBase::PARAM_DEPRECATED => true,
188 ),
189 'watchlist' => array(
190 ApiBase::PARAM_DFLT => 'preferences',
191 ApiBase::PARAM_TYPE => array(
192 'watch',
193 'unwatch',
194 'preferences',
195 'nochange'
196 ),
197 ),
198 'unwatch' => array(
199 ApiBase::PARAM_DFLT => false,
200 ApiBase::PARAM_DEPRECATED => true,
201 ),
202 'oldimage' => null,
203 );
204 }
205
206 public function getParamDescription() {
207 $p = $this->getModulePrefix();
208 return array(
209 'title' => "Title of the page you want to delete. Cannot be used together with {$p}pageid",
210 'pageid' => "Page ID of the page you want to delete. Cannot be used together with {$p}title",
211 'token' => 'A delete token previously retrieved through prop=info',
212 'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used',
213 'watch' => 'Add the page to your watchlist',
214 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
215 'unwatch' => 'Remove the page from your watchlist',
216 'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename'
217 );
218 }
219
220 public function getResultProperties() {
221 return array(
222 '' => array(
223 'title' => 'string',
224 'reason' => 'string'
225 )
226 );
227 }
228
229 public function getDescription() {
230 return 'Delete a page';
231 }
232
233 public function getPossibleErrors() {
234 return array_merge( parent::getPossibleErrors(),
235 $this->getTitleOrPageIdErrorMessage(),
236 array(
237 array( 'notanarticle' ),
238 array( 'hookaborted', 'error' ),
239 array( 'delete-toobig', 'limit' ),
240 array( 'cannotdelete', 'title' ),
241 array( 'invalidoldimage' ),
242 array( 'nodeleteablefile' ),
243 )
244 );
245 }
246
247 public function needsToken() {
248 return true;
249 }
250
251 public function getTokenSalt() {
252 return '';
253 }
254
255 public function getExamples() {
256 return array(
257 'api.php?action=delete&title=Main%20Page&token=123ABC' => 'Delete the Main Page',
258 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move' => 'Delete the Main Page with the reason "Preparing for move"',
259 );
260 }
261
262 public function getHelpUrls() {
263 return 'https://www.mediawiki.org/wiki/API:Delete';
264 }
265
266 public function getVersion() {
267 return __CLASS__ . ': $Id$';
268 }
269 }