Followup r64197
[lhc/web/wiklou.git] / includes / api / ApiDelete.php
1 <?php
2
3 /**
4 * Created on Jun 30, 2007
5 * API for MediaWiki 1.8+
6 *
7 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 */
24
25 if ( !defined( 'MEDIAWIKI' ) ) {
26 // Eclipse helper - will be ignored in production
27 require_once( "ApiBase.php" );
28 }
29
30 /**
31 * API module that facilitates deleting pages. The API eqivalent of action=delete.
32 * Requires API write mode to be enabled.
33 *
34 * @ingroup API
35 */
36 class ApiDelete extends ApiBase {
37
38 public function __construct( $main, $action ) {
39 parent::__construct( $main, $action );
40 }
41
42 /**
43 * Extracts the title, token, and reason from the request parameters and invokes
44 * the local delete() function with these as arguments. It does not make use of
45 * the delete function specified by Article.php. If the deletion succeeds, the
46 * details of the article deleted and the reason for deletion are added to the
47 * result object.
48 */
49 public function execute() {
50 global $wgUser;
51
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 $watch = $this->getWatchlistValue( $params['watchlist'], $titleObj ) || $wgUser->getOption( 'watchdeletion' );
86
87 // Deprecated parameters
88 if ( $params['watch'] ) {
89 $watch = true;
90 } elseif ( $params['unwatch'] ) {
91 $watch = false;
92 }
93
94 if ( $watch !== null ) {
95 if ( $watch ) {
96 $articleObj->doWatch();
97 } else {
98 $articleObj->doUnwatch();
99 }
100 }
101 }
102
103 $r = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $reason );
104 $this->getResult()->addValue( null, $this->getModuleName(), $r );
105 }
106
107 private static function getPermissionsError( &$title, $token ) {
108 global $wgUser;
109
110 // Check permissions
111 $errors = $title->getUserPermissionsErrors( 'delete', $wgUser );
112 if ( count( $errors ) > 0 ) {
113 return $errors;
114 }
115
116 return array();
117 }
118
119 /**
120 * We have our own delete() function, since Article.php's implementation is split in two phases
121 *
122 * @param $article Article object to work on
123 * @param $token String: delete token (same as edit token)
124 * @param $reason String: reason for the deletion. Autogenerated if NULL
125 * @return Title::getUserPermissionsErrors()-like array
126 */
127 public static function delete( &$article, $token, &$reason = null ) {
128 global $wgUser;
129 if ( $article->isBigDeletion() && !$wgUser->isAllowed( 'bigdelete' ) ) {
130 global $wgDeleteRevisionsLimit;
131 return array( array( 'delete-toobig', $wgDeleteRevisionsLimit ) );
132 }
133 $title = $article->getTitle();
134 $errors = self::getPermissionsError( $title, $token );
135 if ( count( $errors ) ) {
136 return $errors;
137 }
138
139 // Auto-generate a summary, if necessary
140 if ( is_null( $reason ) ) {
141 // Need to pass a throwaway variable because generateReason expects
142 // a reference
143 $hasHistory = false;
144 $reason = $article->generateReason( $hasHistory );
145 if ( $reason === false ) {
146 return array( array( 'cannotdelete' ) );
147 }
148 }
149
150 $error = '';
151 if ( !wfRunHooks( 'ArticleDelete', array( &$article, &$wgUser, &$reason, $error ) ) ) {
152 $this->dieUsageMsg( array( 'hookaborted', $error ) );
153 }
154
155 // Luckily, Article.php provides a reusable delete function that does the hard work for us
156 if ( $article->doDeleteArticle( $reason ) ) {
157 wfRunHooks( 'ArticleDeleteComplete', array( &$article, &$wgUser, $reason, $article->getId() ) );
158 return array();
159 }
160 return array( array( 'cannotdelete', $article->mTitle->getPrefixedText() ) );
161 }
162
163 public static function deleteFile( $token, &$title, $oldimage, &$reason = null, $suppress = false ) {
164 $errors = self::getPermissionsError( $title, $token );
165 if ( count( $errors ) ) {
166 return $errors;
167 }
168
169 if ( $oldimage && !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
170 return array( array( 'invalidoldimage' ) );
171 }
172
173 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
174 $oldfile = false;
175
176 if ( $oldimage ) {
177 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
178 }
179
180 if ( !FileDeleteForm::haveDeletableFile( $file, $oldfile, $oldimage ) ) {
181 return self::delete( new Article( $title ), $token, $reason );
182 }
183 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
184 $reason = '';
185 }
186 $status = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress );
187
188 if ( !$status->isGood() ) {
189 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
190 }
191
192 return array();
193 }
194
195 public function mustBePosted() {
196 return true;
197 }
198
199 public function isWriteMode() {
200 return true;
201 }
202
203 public function getAllowedParams() {
204 return array(
205 'title' => null,
206 'pageid' => array(
207 ApiBase::PARAM_TYPE => 'integer'
208 ),
209 'token' => null,
210 'reason' => null,
211 'watch' => array(
212 ApiBase::PARAM_DFLT => false,
213 ApiBase::PARAM_DEPRECATED => true,
214 ),
215 'watchlist' => array(
216 ApiBase::PARAM_DFLT => 'preferences',
217 ApiBase::PARAM_TYPE => array(
218 'watch',
219 'unwatch',
220 'preferences',
221 'nochange'
222 ),
223 ),
224 'unwatch' => false,
225 'oldimage' => null
226 );
227 }
228
229 public function getParamDescription() {
230 return array(
231 'title' => 'Title of the page you want to delete. Cannot be used together with pageid',
232 'pageid' => 'Page ID of the page you want to delete. Cannot be used together with title',
233 'token' => 'A delete token previously retrieved through prop=info',
234 'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used.',
235 'watch' => 'Add the page to your watchlist',
236 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
237 'unwatch' => 'Remove the page from your watchlist',
238 'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename'
239 );
240 }
241
242 public function getDescription() {
243 return array(
244 'Delete a page.'
245 );
246 }
247
248 public function getPossibleErrors() {
249 return array_merge( parent::getPossibleErrors(), array(
250 array( 'invalidtitle', 'title' ),
251 array( 'nosuchpageid', 'pageid' ),
252 array( 'notanarticle' ),
253 array( 'hookaborted', 'error' ),
254 ) );
255 }
256
257 public function getTokenSalt() {
258 return '';
259 }
260
261 protected function getExamples() {
262 return array(
263 'api.php?action=delete&title=Main%20Page&token=123ABC',
264 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
265 );
266 }
267
268 public function getVersion() {
269 return __CLASS__ . ': $Id$';
270 }
271 }