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