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