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