Start of "Bug 21991 - Move common query parameter (uc, rc) validation, token requiri...
[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
55 if ( isset( $params['title'] ) )
56 {
57 $titleObj = Title::newFromText( $params['title'] );
58 if ( !$titleObj )
59 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
60 }
61 else if ( isset( $params['pageid'] ) )
62 {
63 $titleObj = Title::newFromID( $params['pageid'] );
64 if ( !$titleObj )
65 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
66 }
67 if ( !$titleObj->exists() )
68 $this->dieUsageMsg( array( 'notanarticle' ) );
69
70 $reason = ( isset( $params['reason'] ) ? $params['reason'] : null );
71 if ( $titleObj->getNamespace() == NS_FILE ) {
72 $retval = self::deleteFile( $params['token'], $titleObj, $params['oldimage'], $reason, false );
73 if ( count( $retval ) )
74 $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
75 } else {
76 $articleObj = new Article( $titleObj );
77 $retval = self::delete( $articleObj, $params['token'], $reason );
78
79 if ( count( $retval ) )
80 $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
81
82 if ( $params['watch'] || $wgUser->getOption( 'watchdeletion' ) )
83 $articleObj->doWatch();
84 else if ( $params['unwatch'] )
85 $articleObj->doUnwatch();
86 }
87
88 $r = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $reason );
89 $this->getResult()->addValue( null, $this->getModuleName(), $r );
90 }
91
92 private static function getPermissionsError( &$title, $token ) {
93 global $wgUser;
94
95 // Check permissions
96 $errors = $title->getUserPermissionsErrors( 'delete', $wgUser );
97 if ( count( $errors ) > 0 ) return $errors;
98
99 // Check token
100 if ( !$wgUser->matchEditToken( $token ) )
101 return array( array( 'sessionfailure' ) );
102 return array();
103 }
104
105 /**
106 * We have our own delete() function, since Article.php's implementation is split in two phases
107 *
108 * @param Article $article - Article object to work on
109 * @param string $token - Delete token (same as edit token)
110 * @param string $reason - Reason for the deletion. Autogenerated if NULL
111 * @return Title::getUserPermissionsErrors()-like array
112 */
113 public static function delete( &$article, $token, &$reason = null )
114 {
115 global $wgUser;
116 if ( $article->isBigDeletion() && !$wgUser->isAllowed( 'bigdelete' ) ) {
117 global $wgDeleteRevisionsLimit;
118 return array( array( 'delete-toobig', $wgDeleteRevisionsLimit ) );
119 }
120 $title = $article->getTitle();
121 $errors = self::getPermissionsError( $title, $token );
122 if ( count( $errors ) ) return $errors;
123
124 // Auto-generate a summary, if necessary
125 if ( is_null( $reason ) )
126 {
127 // Need to pass a throwaway variable because generateReason expects
128 // a reference
129 $hasHistory = false;
130 $reason = $article->generateReason( $hasHistory );
131 if ( $reason === false )
132 return array( array( 'cannotdelete' ) );
133 }
134
135 $error = '';
136 if ( !wfRunHooks( 'ArticleDelete', array( &$article, &$wgUser, &$reason, $error ) ) )
137 $this->dieUsageMsg( array( 'hookaborted', $error ) );
138
139 // Luckily, Article.php provides a reusable delete function that does the hard work for us
140 if ( $article->doDeleteArticle( $reason ) ) {
141 wfRunHooks( 'ArticleDeleteComplete', array( &$article, &$wgUser, $reason, $article->getId() ) );
142 return array();
143 }
144 return array( array( 'cannotdelete', $article->mTitle->getPrefixedText() ) );
145 }
146
147 public static function deleteFile( $token, &$title, $oldimage, &$reason = null, $suppress = false )
148 {
149 $errors = self::getPermissionsError( $title, $token );
150 if ( count( $errors ) ) return $errors;
151
152 if ( $oldimage && !FileDeleteForm::isValidOldSpec( $oldimage ) )
153 return array( array( 'invalidoldimage' ) );
154
155 $file = wfFindFile( $title, array( 'ignoreRedirect' => true ) );
156 $oldfile = false;
157
158 if ( $oldimage )
159 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
160
161 if ( !FileDeleteForm::haveDeletableFile( $file, $oldfile, $oldimage ) )
162 return self::delete( new Article( $title ), $token, $reason );
163 if ( is_null( $reason ) ) // Log and RC don't like null reasons
164 $reason = '';
165 $status = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress );
166
167 if ( !$status->isGood() )
168 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
169
170 return array();
171 }
172
173 public function mustBePosted() {
174 return true;
175 }
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( 'invalidtitle', 'title' ),
216 array( 'nosuchpageid', 'pageid' ),
217 array( 'notanarticle' ),
218 array( 'hookaborted', 'error' ),
219 ) );
220 }
221
222 public function requiresToken() {
223 return true;
224 }
225
226 protected function getExamples() {
227 return array (
228 'api.php?action=delete&title=Main%20Page&token=123ABC',
229 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
230 );
231 }
232
233 public function getVersion() {
234 return __CLASS__ . ': $Id$';
235 }
236 }