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