New infrastructure for actions, as discussed on wikitech-l. Fairly huge commit.
[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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
30 }
31
32 /**
33 * API module that facilitates deleting pages. The API equivalent of action=delete.
34 * Requires API write mode to be enabled.
35 *
36 * @ingroup API
37 */
38 class ApiDelete extends ApiBase {
39
40 public function __construct( $main, $action ) {
41 parent::__construct( $main, $action );
42 }
43
44 /**
45 * Extracts the title, token, and reason from the request parameters and invokes
46 * the local delete() function with these as arguments. It does not make use of
47 * the delete function specified by Article.php. If the deletion succeeds, the
48 * details of the article deleted and the reason for deletion are added to the
49 * result object.
50 */
51 public function execute() {
52 $params = $this->extractRequestParams();
53
54 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
55
56 if ( isset( $params['title'] ) ) {
57 $titleObj = Title::newFromText( $params['title'] );
58 if ( !$titleObj ) {
59 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
60 }
61 } elseif ( isset( $params['pageid'] ) ) {
62 $titleObj = Title::newFromID( $params['pageid'] );
63 if ( !$titleObj ) {
64 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
65 }
66 }
67 if ( !$titleObj->exists() ) {
68 $this->dieUsageMsg( array( 'notanarticle' ) );
69 }
70
71 $reason = ( isset( $params['reason'] ) ? $params['reason'] : null );
72 if ( $titleObj->getNamespace() == NS_FILE ) {
73 $retval = self::deleteFile( $params['token'], $titleObj, $params['oldimage'], $reason, false );
74 if ( count( $retval ) ) {
75 $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
76 }
77 } else {
78 $articleObj = new Article( $titleObj );
79 $retval = self::delete( $articleObj, $params['token'], $reason );
80
81 if ( count( $retval ) ) {
82 $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
83 }
84
85 // Deprecated parameters
86 if ( $params['watch'] ) {
87 $watch = 'watch';
88 } elseif ( $params['unwatch'] ) {
89 $watch = 'unwatch';
90 } else {
91 $watch = $params['watchlist'];
92 }
93 $this->setWatch( $watch, $titleObj, 'watchdeletion' );
94 }
95
96 $r = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $reason );
97 $this->getResult()->addValue( null, $this->getModuleName(), $r );
98 }
99
100 /**
101 *
102 * @param &$title Title
103 * @param $token String
104 */
105 private static function getPermissionsError( &$title, $token ) {
106 global $wgUser;
107
108 // Check permissions
109 $errors = $title->getUserPermissionsErrors( 'delete', $wgUser );
110 if ( count( $errors ) > 0 ) {
111 return $errors;
112 }
113
114 return array();
115 }
116
117 /**
118 * We have our own delete() function, since Article.php's implementation is split in two phases
119 *
120 * @param $article Article object to work on
121 * @param $token String: delete token (same as edit token)
122 * @param $reason String: reason for the deletion. Autogenerated if NULL
123 * @return Title::getUserPermissionsErrors()-like array
124 */
125 public static function delete( &$article, $token, &$reason = null ) {
126 $title = $article->getTitle();
127 $errors = self::getPermissionsError( $title, $token );
128 if ( count( $errors ) ) {
129 return $errors;
130 }
131
132 // Auto-generate a summary, if necessary
133 if ( is_null( $reason ) ) {
134 $reason = DeleteAction::getAutoReason( $article );
135 if ( $reason === false ) {
136 return array( array( 'cannotdelete' ) );
137 }
138 }
139
140 $action = Action::factory( 'delete', $article );
141 $data = array(
142 'Reason' => $reason,
143 'Suppress' => false, // The thought of people doing this through the API is scary...
144 );
145
146 try {
147 $action->execute( $data, false );
148 }
149 catch ( ErrorPageError $e ){
150 if( $e->msg == 'delete-toobig' ){
151 global $wgDeleteRevisionsLimit;
152 return array( array( 'delete-toobig', $wgDeleteRevisionsLimit ) );
153 } else {
154 array( array( 'cannotdelete', $article->mTitle->getPrefixedText() ) );
155 }
156 }
157 }
158
159 /**
160 * @static
161 * @param $token
162 * @param $title Title
163 * @param $oldimage
164 * @param $reason
165 * @param $suppress bool
166 * @return \type|array|Title
167 */
168 public static function deleteFile( $token, &$title, $oldimage, &$reason = null, $suppress = false ) {
169 $errors = self::getPermissionsError( $title, $token );
170 if ( count( $errors ) ) {
171 return $errors;
172 }
173
174 if ( $oldimage && !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
175 return array( array( 'invalidoldimage' ) );
176 }
177
178 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
179 $oldfile = false;
180
181 if ( $oldimage ) {
182 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
183 }
184
185 if ( !FileDeleteForm::haveDeletableFile( $file, $oldfile, $oldimage ) ) {
186 return self::delete( new Article( $title ), $token, $reason );
187 }
188 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
189 $reason = '';
190 }
191 $status = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress );
192
193 if ( !$status->isGood() ) {
194 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
195 }
196
197 return array();
198 }
199
200 public function mustBePosted() {
201 return true;
202 }
203
204 public function isWriteMode() {
205 return true;
206 }
207
208 public function getAllowedParams() {
209 return array(
210 'title' => null,
211 'pageid' => array(
212 ApiBase::PARAM_TYPE => 'integer'
213 ),
214 'token' => null,
215 'reason' => null,
216 'watch' => array(
217 ApiBase::PARAM_DFLT => false,
218 ApiBase::PARAM_DEPRECATED => true,
219 ),
220 'watchlist' => array(
221 ApiBase::PARAM_DFLT => 'preferences',
222 ApiBase::PARAM_TYPE => array(
223 'watch',
224 'unwatch',
225 'preferences',
226 'nochange'
227 ),
228 ),
229 'unwatch' => array(
230 ApiBase::PARAM_DFLT => false,
231 ApiBase::PARAM_DEPRECATED => true,
232 ),
233 'oldimage' => null,
234 );
235 }
236
237 public function getParamDescription() {
238 $p = $this->getModulePrefix();
239 return array(
240 'title' => "Title of the page you want to delete. Cannot be used together with {$p}pageid",
241 'pageid' => "Page ID of the page you want to delete. Cannot be used together with {$p}title",
242 'token' => 'A delete token previously retrieved through prop=info',
243 'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used',
244 'watch' => 'Add the page to your watchlist',
245 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
246 'unwatch' => 'Remove the page from your watchlist',
247 'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename'
248 );
249 }
250
251 public function getDescription() {
252 return 'Delete a page';
253 }
254
255 public function getPossibleErrors() {
256 return array_merge( parent::getPossibleErrors(),
257 $this->getRequireOnlyOneParameterErrorMessages( array( 'title', 'pageid' ) ),
258 array(
259 array( 'invalidtitle', 'title' ),
260 array( 'nosuchpageid', 'pageid' ),
261 array( 'notanarticle' ),
262 array( 'hookaborted', 'error' ),
263 )
264 );
265 }
266
267 public function needsToken() {
268 return true;
269 }
270
271 public function getTokenSalt() {
272 return '';
273 }
274
275 protected function getExamples() {
276 return array(
277 'api.php?action=delete&title=Main%20Page&token=123ABC',
278 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
279 );
280 }
281
282 public function getVersion() {
283 return __CLASS__ . ': $Id$';
284 }
285 }