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