93baa4e33a015ede7b1e125c7c8c3f76729e919e
[lhc/web/wiklou.git] / includes / api / ApiDelete.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
4 *
5 * Created on Jun 30, 2007
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 * 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 eqivalent 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 global $wgUser;
127 if ( $article->isBigDeletion() && !$wgUser->isAllowed( 'bigdelete' ) ) {
128 global $wgDeleteRevisionsLimit;
129 return array( array( 'delete-toobig', $wgDeleteRevisionsLimit ) );
130 }
131 $title = $article->getTitle();
132 $errors = self::getPermissionsError( $title, $token );
133 if ( count( $errors ) ) {
134 return $errors;
135 }
136
137 // Auto-generate a summary, if necessary
138 if ( is_null( $reason ) ) {
139 // Need to pass a throwaway variable because generateReason expects
140 // a reference
141 $hasHistory = false;
142 $reason = $article->generateReason( $hasHistory );
143 if ( $reason === false ) {
144 return array( array( 'cannotdelete' ) );
145 }
146 }
147
148 $error = '';
149 if ( !wfRunHooks( 'ArticleDelete', array( &$article, &$wgUser, &$reason, $error ) ) ) {
150 return array( array( 'hookaborted', $error ) );
151 }
152
153 // Luckily, Article.php provides a reusable delete function that does the hard work for us
154 if ( $article->doDeleteArticle( $reason ) ) {
155 wfRunHooks( 'ArticleDeleteComplete', array( &$article, &$wgUser, $reason, $article->getId() ) );
156 return array();
157 }
158 return array( array( 'cannotdelete', $article->mTitle->getPrefixedText() ) );
159 }
160
161 public static function deleteFile( $token, &$title, $oldimage, &$reason = null, $suppress = false ) {
162 $errors = self::getPermissionsError( $title, $token );
163 if ( count( $errors ) ) {
164 return $errors;
165 }
166
167 if ( $oldimage && !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
168 return array( array( 'invalidoldimage' ) );
169 }
170
171 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
172 $oldfile = false;
173
174 if ( $oldimage ) {
175 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
176 }
177
178 if ( !FileDeleteForm::haveDeletableFile( $file, $oldfile, $oldimage ) ) {
179 return self::delete( new Article( $title ), $token, $reason );
180 }
181 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
182 $reason = '';
183 }
184 $status = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress );
185
186 if ( !$status->isGood() ) {
187 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
188 }
189
190 return array();
191 }
192
193 public function mustBePosted() {
194 return true;
195 }
196
197 public function isWriteMode() {
198 return true;
199 }
200
201 public function getAllowedParams() {
202 return array(
203 'title' => null,
204 'pageid' => array(
205 ApiBase::PARAM_TYPE => 'integer'
206 ),
207 'token' => null,
208 'reason' => null,
209 'watch' => array(
210 ApiBase::PARAM_DFLT => false,
211 ApiBase::PARAM_DEPRECATED => true,
212 ),
213 'watchlist' => array(
214 ApiBase::PARAM_DFLT => 'preferences',
215 ApiBase::PARAM_TYPE => array(
216 'watch',
217 'unwatch',
218 'preferences',
219 'nochange'
220 ),
221 ),
222 'unwatch' => false,
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(), array(
247 array( 'invalidtitle', 'title' ),
248 array( 'nosuchpageid', 'pageid' ),
249 array( 'notanarticle' ),
250 array( 'hookaborted', 'error' ),
251 ) );
252 }
253
254 public function needsToken() {
255 return true;
256 }
257
258 public function getTokenSalt() {
259 return '';
260 }
261
262 protected function getExamples() {
263 return array(
264 'api.php?action=delete&title=Main%20Page&token=123ABC',
265 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
266 );
267 }
268
269 public function getVersion() {
270 return __CLASS__ . ': $Id$';
271 }
272 }