$this isn't valid in a static function. Fix for r46507
[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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 = 'nochange';
86 // Deprecated parameters
87 if ( $params['watch'] ) {
88 $watch = 'watch';
89 } elseif ( $params['unwatch'] ) {
90 $watch = 'unwatch';
91 } else {
92 $watch = $params['watchlist'];
93 }
94 $this->setWatch( $watch, $titleObj, 'watchdeletion' );
95 }
96
97 $r = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $reason );
98 $this->getResult()->addValue( null, $this->getModuleName(), $r );
99 }
100
101 private static function getPermissionsError( &$title, $token ) {
102 global $wgUser;
103
104 // Check permissions
105 $errors = $title->getUserPermissionsErrors( 'delete', $wgUser );
106 if ( count( $errors ) > 0 ) {
107 return $errors;
108 }
109
110 return array();
111 }
112
113 /**
114 * We have our own delete() function, since Article.php's implementation is split in two phases
115 *
116 * @param $article Article object to work on
117 * @param $token String: delete token (same as edit token)
118 * @param $reason String: reason for the deletion. Autogenerated if NULL
119 * @return Title::getUserPermissionsErrors()-like array
120 */
121 public static function delete( &$article, $token, &$reason = null ) {
122 global $wgUser;
123 if ( $article->isBigDeletion() && !$wgUser->isAllowed( 'bigdelete' ) ) {
124 global $wgDeleteRevisionsLimit;
125 return array( array( 'delete-toobig', $wgDeleteRevisionsLimit ) );
126 }
127 $title = $article->getTitle();
128 $errors = self::getPermissionsError( $title, $token );
129 if ( count( $errors ) ) {
130 return $errors;
131 }
132
133 // Auto-generate a summary, if necessary
134 if ( is_null( $reason ) ) {
135 // Need to pass a throwaway variable because generateReason expects
136 // a reference
137 $hasHistory = false;
138 $reason = $article->generateReason( $hasHistory );
139 if ( $reason === false ) {
140 return array( array( 'cannotdelete' ) );
141 }
142 }
143
144 $error = '';
145 if ( !wfRunHooks( 'ArticleDelete', array( &$article, &$wgUser, &$reason, $error ) ) ) {
146 return array( array( 'hookaborted', $error ) );
147 }
148
149 // Luckily, Article.php provides a reusable delete function that does the hard work for us
150 if ( $article->doDeleteArticle( $reason ) ) {
151 wfRunHooks( 'ArticleDeleteComplete', array( &$article, &$wgUser, $reason, $article->getId() ) );
152 return array();
153 }
154 return array( array( 'cannotdelete', $article->mTitle->getPrefixedText() ) );
155 }
156
157 public static function deleteFile( $token, &$title, $oldimage, &$reason = null, $suppress = false ) {
158 $errors = self::getPermissionsError( $title, $token );
159 if ( count( $errors ) ) {
160 return $errors;
161 }
162
163 if ( $oldimage && !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
164 return array( array( 'invalidoldimage' ) );
165 }
166
167 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
168 $oldfile = false;
169
170 if ( $oldimage ) {
171 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
172 }
173
174 if ( !FileDeleteForm::haveDeletableFile( $file, $oldfile, $oldimage ) ) {
175 return self::delete( new Article( $title ), $token, $reason );
176 }
177 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
178 $reason = '';
179 }
180 $status = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress );
181
182 if ( !$status->isGood() ) {
183 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
184 }
185
186 return array();
187 }
188
189 public function mustBePosted() {
190 return true;
191 }
192
193 public function isWriteMode() {
194 return true;
195 }
196
197 public function getAllowedParams() {
198 return array(
199 'title' => null,
200 'pageid' => array(
201 ApiBase::PARAM_TYPE => 'integer'
202 ),
203 'token' => null,
204 'reason' => null,
205 'watch' => array(
206 ApiBase::PARAM_DFLT => false,
207 ApiBase::PARAM_DEPRECATED => true,
208 ),
209 'watchlist' => array(
210 ApiBase::PARAM_DFLT => 'preferences',
211 ApiBase::PARAM_TYPE => array(
212 'watch',
213 'unwatch',
214 'preferences',
215 'nochange'
216 ),
217 ),
218 'unwatch' => false,
219 'oldimage' => null
220 );
221 }
222
223 public function getParamDescription() {
224 $p = $this->getModulePrefix();
225 return array(
226 'title' => "Title of the page you want to delete. Cannot be used together with {$p}pageid",
227 'pageid' => "Page ID of the page you want to delete. Cannot be used together with {$p}title",
228 'token' => 'A delete token previously retrieved through prop=info',
229 'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used',
230 'watch' => 'Add the page to your watchlist',
231 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
232 'unwatch' => 'Remove the page from your watchlist',
233 'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename'
234 );
235 }
236
237 public function getDescription() {
238 return 'Delete a page';
239 }
240
241 public function getPossibleErrors() {
242 return array_merge( parent::getPossibleErrors(), array(
243 array( 'invalidtitle', 'title' ),
244 array( 'nosuchpageid', 'pageid' ),
245 array( 'notanarticle' ),
246 array( 'hookaborted', 'error' ),
247 ) );
248 }
249
250 public function getTokenSalt() {
251 return '';
252 }
253
254 protected function getExamples() {
255 return array(
256 'api.php?action=delete&title=Main%20Page&token=123ABC',
257 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
258 );
259 }
260
261 public function getVersion() {
262 return __CLASS__ . ': $Id$';
263 }
264 }