Stop mutually exclusive values in ApiDelete and ApiMove
[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['watch'] ) && params( $show['unwatch'] ) ) {
57 $this->dieUsageMsg( array( 'show' ) );
58 }
59
60 if ( isset( $params['title'] ) ) {
61 $titleObj = Title::newFromText( $params['title'] );
62 if ( !$titleObj ) {
63 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
64 }
65 } elseif ( isset( $params['pageid'] ) ) {
66 $titleObj = Title::newFromID( $params['pageid'] );
67 if ( !$titleObj ) {
68 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
69 }
70 }
71 if ( !$titleObj->exists() ) {
72 $this->dieUsageMsg( array( 'notanarticle' ) );
73 }
74
75 $reason = ( isset( $params['reason'] ) ? $params['reason'] : null );
76 if ( $titleObj->getNamespace() == NS_FILE ) {
77 $retval = self::deleteFile( $params['token'], $titleObj, $params['oldimage'], $reason, false );
78 if ( count( $retval ) ) {
79 $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
80 }
81 } else {
82 $articleObj = new Article( $titleObj );
83 $retval = self::delete( $articleObj, $params['token'], $reason );
84
85 if ( count( $retval ) ) {
86 $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
87 }
88
89 if ( $params['watch'] || $wgUser->getOption( 'watchdeletion' ) ) {
90 $articleObj->doWatch();
91 } elseif ( $params['unwatch'] ) {
92 $articleObj->doUnwatch();
93 }
94 }
95
96 $r = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $reason );
97 $this->getResult()->addValue( null, $this->getModuleName(), $r );
98 }
99
100 private static function getPermissionsError( &$title, $token ) {
101 global $wgUser;
102
103 // Check permissions
104 $errors = $title->getUserPermissionsErrors( 'delete', $wgUser );
105 if ( count( $errors ) > 0 ) {
106 return $errors;
107 }
108
109 return array();
110 }
111
112 /**
113 * We have our own delete() function, since Article.php's implementation is split in two phases
114 *
115 * @param $article Article object to work on
116 * @param $token String: delete token (same as edit token)
117 * @param $reason String: reason for the deletion. Autogenerated if NULL
118 * @return Title::getUserPermissionsErrors()-like array
119 */
120 public static function delete( &$article, $token, &$reason = null ) {
121 global $wgUser;
122 if ( $article->isBigDeletion() && !$wgUser->isAllowed( 'bigdelete' ) ) {
123 global $wgDeleteRevisionsLimit;
124 return array( array( 'delete-toobig', $wgDeleteRevisionsLimit ) );
125 }
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 // Need to pass a throwaway variable because generateReason expects
135 // a reference
136 $hasHistory = false;
137 $reason = $article->generateReason( $hasHistory );
138 if ( $reason === false ) {
139 return array( array( 'cannotdelete' ) );
140 }
141 }
142
143 $error = '';
144 if ( !wfRunHooks( 'ArticleDelete', array( &$article, &$wgUser, &$reason, $error ) ) ) {
145 $this->dieUsageMsg( array( 'hookaborted', $error ) );
146 }
147
148 // Luckily, Article.php provides a reusable delete function that does the hard work for us
149 if ( $article->doDeleteArticle( $reason ) ) {
150 wfRunHooks( 'ArticleDeleteComplete', array( &$article, &$wgUser, $reason, $article->getId() ) );
151 return array();
152 }
153 return array( array( 'cannotdelete', $article->mTitle->getPrefixedText() ) );
154 }
155
156 public static function deleteFile( $token, &$title, $oldimage, &$reason = null, $suppress = false ) {
157 $errors = self::getPermissionsError( $title, $token );
158 if ( count( $errors ) ) {
159 return $errors;
160 }
161
162 if ( $oldimage && !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
163 return array( array( 'invalidoldimage' ) );
164 }
165
166 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
167 $oldfile = false;
168
169 if ( $oldimage ) {
170 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
171 }
172
173 if ( !FileDeleteForm::haveDeletableFile( $file, $oldfile, $oldimage ) ) {
174 return self::delete( new Article( $title ), $token, $reason );
175 }
176 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
177 $reason = '';
178 }
179 $status = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress );
180
181 if ( !$status->isGood() ) {
182 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
183 }
184
185 return array();
186 }
187
188 public function mustBePosted() {
189 return true;
190 }
191
192 public function isWriteMode() {
193 return true;
194 }
195
196 public function getAllowedParams() {
197 return array(
198 'title' => null,
199 'pageid' => array(
200 ApiBase::PARAM_TYPE => 'integer'
201 ),
202 'token' => null,
203 'reason' => null,
204 'watch' => false,
205 'unwatch' => false,
206 'oldimage' => null
207 );
208 }
209
210 public function getParamDescription() {
211 return array(
212 'title' => 'Title of the page you want to delete. Cannot be used together with pageid',
213 'pageid' => 'Page ID of the page you want to delete. Cannot be used together with title',
214 'token' => 'A delete token previously retrieved through prop=info',
215 'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used.',
216 'watch' => 'Add the page to your watchlist',
217 'unwatch' => 'Remove the page from your watchlist',
218 'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename'
219 );
220 }
221
222 public function getDescription() {
223 return array(
224 'Delete a page.'
225 );
226 }
227
228 public function getPossibleErrors() {
229 return array_merge( parent::getPossibleErrors(), array(
230 array( 'invalidtitle', 'title' ),
231 array( 'nosuchpageid', 'pageid' ),
232 array( 'notanarticle' ),
233 array( 'hookaborted', 'error' ),
234 ) );
235 }
236
237 public function getTokenSalt() {
238 return '';
239 }
240
241 protected function getExamples() {
242 return array(
243 'api.php?action=delete&title=Main%20Page&token=123ABC',
244 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
245 );
246 }
247
248 public function getVersion() {
249 return __CLASS__ . ': $Id$';
250 }
251 }