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