merge latest master into Wikidata branch
[lhc/web/wiklou.git] / includes / api / ApiDelete.php
1 <?php
2 /**
3 *
4 *
5 * Created on Jun 30, 2007
6 *
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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 * @file
25 */
26
27 /**
28 * API module that facilitates deleting pages. The API equivalent of action=delete.
29 * Requires API write mode to be enabled.
30 *
31 * @ingroup API
32 */
33 class ApiDelete extends ApiBase {
34
35 public function __construct( $main, $action ) {
36 parent::__construct( $main, $action );
37 }
38
39 /**
40 * Extracts the title, token, and reason from the request parameters and invokes
41 * the local delete() function with these as arguments. It does not make use of
42 * the delete function specified by Article.php. If the deletion succeeds, the
43 * details of the article deleted and the reason for deletion are added to the
44 * result object.
45 */
46 public function execute() {
47 $params = $this->extractRequestParams();
48
49 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
50 if ( !$pageObj->exists() ) {
51 $this->dieUsageMsg( 'notanarticle' );
52 }
53
54 $titleObj = $pageObj->getTitle();
55 $reason = $params['reason'];
56 $user = $this->getUser();
57
58 if ( $titleObj->getNamespace() == NS_FILE ) {
59 $status = self::deleteFile( $pageObj, $user, $params['token'], $params['oldimage'], $reason, false );
60 } else {
61 $status = self::delete( $pageObj, $user, $params['token'], $reason );
62 }
63
64 if ( is_array( $status ) ) {
65 $this->dieUsageMsg( $status[0] );
66 }
67 if ( !$status->isGood() ) {
68 $errors = $status->getErrorsArray();
69 $this->dieUsageMsg( $errors[0] ); // We don't care about multiple errors, just report one of them
70 }
71
72 // Deprecated parameters
73 if ( $params['watch'] ) {
74 $watch = 'watch';
75 } elseif ( $params['unwatch'] ) {
76 $watch = 'unwatch';
77 } else {
78 $watch = $params['watchlist'];
79 }
80 $this->setWatch( $watch, $titleObj, 'watchdeletion' );
81
82 $r = array(
83 'title' => $titleObj->getPrefixedText(),
84 'reason' => $reason,
85 'logid' => $status->value
86 );
87 $this->getResult()->addValue( null, $this->getModuleName(), $r );
88 }
89
90 /**
91 * @param $title Title
92 * @param $user User doing the action
93 * @param $token String
94 * @return array
95 */
96 private static function getPermissionsError( $title, $user, $token ) {
97 // Check permissions
98 return $title->getUserPermissionsErrors( 'delete', $user );
99 }
100
101 /**
102 * We have our own delete() function, since Article.php's implementation is split in two phases
103 *
104 * @param $page Page|WikiPage object to work on
105 * @param $user User doing the action
106 * @param $token String delete token (same as edit token)
107 * @param $reason String|null reason for the deletion. Autogenerated if NULL
108 * @return Status|array
109 */
110 public static function delete( Page $page, User $user, $token, &$reason = null ) {
111 $title = $page->getTitle();
112 $errors = self::getPermissionsError( $title, $user, $token );
113 if ( count( $errors ) ) {
114 return $errors;
115 }
116
117 // Auto-generate a summary, if necessary
118 if ( is_null( $reason ) ) {
119 // Need to pass a throwaway variable because generateReason expects
120 // a reference
121 $hasHistory = false;
122 $reason = $page->getAutoDeleteReason( $hasHistory );
123 if ( $reason === false ) {
124 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
125 }
126 }
127
128 $error = '';
129 // Luckily, Article.php provides a reusable delete function that does the hard work for us
130 return $page->doDeleteArticleReal( $reason, false, 0, true, $error );
131 }
132
133 /**
134 * @param $page WikiPage|Page object to work on
135 * @param $user User doing the action
136 * @param $token
137 * @param $oldimage
138 * @param $reason
139 * @param $suppress bool
140 * @return Status|array
141 */
142 public static function deleteFile( Page $page, User $user, $token, $oldimage, &$reason = null, $suppress = false ) {
143 $title = $page->getTitle();
144 $errors = self::getPermissionsError( $title, $user, $token );
145 if ( count( $errors ) ) {
146 return $errors;
147 }
148
149 $file = $page->getFile();
150 if ( !$file->exists() || !$file->isLocal() || $file->getRedirected() ) {
151 return self::delete( $page, $user, $token, $reason );
152 }
153
154 if ( $oldimage ) {
155 if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
156 return array( array( 'invalidoldimage' ) );
157 }
158 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
159 if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
160 return array( array( 'nodeleteablefile' ) );
161 }
162 }
163
164 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
165 $reason = '';
166 }
167 return FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress, $user );
168 }
169
170 public function mustBePosted() {
171 return true;
172 }
173
174 public function isWriteMode() {
175 return true;
176 }
177
178 public function getAllowedParams() {
179 return array(
180 'title' => null,
181 'pageid' => array(
182 ApiBase::PARAM_TYPE => 'integer'
183 ),
184 'token' => array(
185 ApiBase::PARAM_TYPE => 'string',
186 ApiBase::PARAM_REQUIRED => true
187 ),
188 'reason' => null,
189 'watch' => array(
190 ApiBase::PARAM_DFLT => false,
191 ApiBase::PARAM_DEPRECATED => true,
192 ),
193 'watchlist' => array(
194 ApiBase::PARAM_DFLT => 'preferences',
195 ApiBase::PARAM_TYPE => array(
196 'watch',
197 'unwatch',
198 'preferences',
199 'nochange'
200 ),
201 ),
202 'unwatch' => array(
203 ApiBase::PARAM_DFLT => false,
204 ApiBase::PARAM_DEPRECATED => true,
205 ),
206 'oldimage' => null,
207 );
208 }
209
210 public function getParamDescription() {
211 $p = $this->getModulePrefix();
212 return array(
213 'title' => "Title of the page you want to delete. Cannot be used together with {$p}pageid",
214 'pageid' => "Page ID of the page you want to delete. Cannot be used together with {$p}title",
215 'token' => 'A delete token previously retrieved through prop=info',
216 'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used',
217 'watch' => 'Add the page to your watchlist',
218 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
219 'unwatch' => 'Remove the page from your watchlist',
220 'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename'
221 );
222 }
223
224 public function getResultProperties() {
225 return array(
226 '' => array(
227 'title' => 'string',
228 'reason' => 'string',
229 'logid' => 'integer'
230 )
231 );
232 }
233
234 public function getDescription() {
235 return 'Delete a page';
236 }
237
238 public function getPossibleErrors() {
239 return array_merge( parent::getPossibleErrors(),
240 $this->getTitleOrPageIdErrorMessage(),
241 array(
242 array( 'notanarticle' ),
243 array( 'hookaborted', 'error' ),
244 array( 'delete-toobig', 'limit' ),
245 array( 'cannotdelete', 'title' ),
246 array( 'invalidoldimage' ),
247 array( 'nodeleteablefile' ),
248 )
249 );
250 }
251
252 public function needsToken() {
253 return true;
254 }
255
256 public function getTokenSalt() {
257 return '';
258 }
259
260 public function getExamples() {
261 return array(
262 'api.php?action=delete&title=Main%20Page&token=123ABC' => 'Delete the Main Page',
263 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move' => 'Delete the Main Page with the reason "Preparing for move"',
264 );
265 }
266
267 public function getHelpUrls() {
268 return 'https://www.mediawiki.org/wiki/API:Delete';
269 }
270
271 public function getVersion() {
272 return __CLASS__ . ': $Id$';
273 }
274 }