Merge "36019: Revert b/25095 breaks Special:Categories"
[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 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
50
51 if ( isset( $params['title'] ) ) {
52 $titleObj = Title::newFromText( $params['title'] );
53 if ( !$titleObj ) {
54 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
55 }
56 $pageObj = WikiPage::factory( $titleObj );
57 $pageObj->loadPageData( 'fromdbmaster' );
58 if ( !$pageObj->exists() ) {
59 $this->dieUsageMsg( 'notanarticle' );
60 }
61 } elseif ( isset( $params['pageid'] ) ) {
62 $pageObj = WikiPage::newFromID( $params['pageid'] );
63 if ( !$pageObj ) {
64 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
65 }
66 $titleObj = $pageObj->getTitle();
67 }
68
69 $reason = ( isset( $params['reason'] ) ? $params['reason'] : null );
70 $user = $this->getUser();
71
72 if ( $titleObj->getNamespace() == NS_FILE ) {
73 $retval = self::deleteFile( $pageObj, $user, $params['token'], $params['oldimage'], $reason, false );
74 } else {
75 $retval = self::delete( $pageObj, $user, $params['token'], $reason );
76 }
77
78 if ( count( $retval ) ) {
79 $this->dieUsageMsg( reset( $retval ) ); // We don't care about multiple errors, just report one of them
80 }
81
82 // Deprecated parameters
83 if ( $params['watch'] ) {
84 $watch = 'watch';
85 } elseif ( $params['unwatch'] ) {
86 $watch = 'unwatch';
87 } else {
88 $watch = $params['watchlist'];
89 }
90 $this->setWatch( $watch, $titleObj, 'watchdeletion' );
91
92 $r = array( 'title' => $titleObj->getPrefixedText(), 'reason' => $reason );
93 $this->getResult()->addValue( null, $this->getModuleName(), $r );
94 }
95
96 /**
97 * @param $title Title
98 * @param $user User doing the action
99 * @param $token String
100 * @return array
101 */
102 private static function getPermissionsError( $title, $user, $token ) {
103 // Check permissions
104 return $title->getUserPermissionsErrors( 'delete', $user );
105 }
106
107 /**
108 * We have our own delete() function, since Article.php's implementation is split in two phases
109 *
110 * @param $page WikiPage object to work on
111 * @param $user User doing the action
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( Page $page, User $user, $token, &$reason = null ) {
117 $title = $page->getTitle();
118 $errors = self::getPermissionsError( $title, $user, $token );
119 if ( count( $errors ) ) {
120 return $errors;
121 }
122
123 // Auto-generate a summary, if necessary
124 if ( is_null( $reason ) ) {
125 // Need to pass a throwaway variable because generateReason expects
126 // a reference
127 $hasHistory = false;
128 $reason = $page->getAutoDeleteReason( $hasHistory );
129 if ( $reason === false ) {
130 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
131 }
132 }
133
134 $error = '';
135 // Luckily, Article.php provides a reusable delete function that does the hard work for us
136 if ( $page->doDeleteArticle( $reason, false, 0, true, $error ) ) {
137 return array();
138 } else {
139 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
140 }
141 }
142
143 /**
144 * @param $page WikiPage object to work on
145 * @param $user User doing the action
146 * @param $token
147 * @param $oldimage
148 * @param $reason
149 * @param $suppress bool
150 * @return array|Title
151 */
152 public static function deleteFile( Page $page, User $user, $token, $oldimage, &$reason = null, $suppress = false ) {
153 $title = $page->getTitle();
154 $errors = self::getPermissionsError( $title, $user, $token );
155 if ( count( $errors ) ) {
156 return $errors;
157 }
158
159 $file = $page->getFile();
160 if ( !$file->exists() || !$file->isLocal() || $file->getRedirected() ) {
161 return self::delete( $page, $user, $token, $reason );
162 }
163
164 if ( $oldimage ) {
165 if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
166 return array( array( 'invalidoldimage' ) );
167 }
168 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
169 if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
170 return array( array( 'nodeleteablefile' ) );
171 }
172 } else {
173 $oldfile = false;
174 }
175
176 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
177 $reason = '';
178 }
179 $status = FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress );
180 if ( !$status->isGood() ) {
181 return array( array( 'cannotdelete', $title->getPrefixedText() ) );
182 }
183
184 return array();
185 }
186
187 public function mustBePosted() {
188 return true;
189 }
190
191 public function isWriteMode() {
192 return true;
193 }
194
195 public function getAllowedParams() {
196 return array(
197 'title' => null,
198 'pageid' => array(
199 ApiBase::PARAM_TYPE => 'integer'
200 ),
201 'token' => null,
202 'reason' => null,
203 'watch' => array(
204 ApiBase::PARAM_DFLT => false,
205 ApiBase::PARAM_DEPRECATED => true,
206 ),
207 'watchlist' => array(
208 ApiBase::PARAM_DFLT => 'preferences',
209 ApiBase::PARAM_TYPE => array(
210 'watch',
211 'unwatch',
212 'preferences',
213 'nochange'
214 ),
215 ),
216 'unwatch' => array(
217 ApiBase::PARAM_DFLT => false,
218 ApiBase::PARAM_DEPRECATED => true,
219 ),
220 'oldimage' => null,
221 );
222 }
223
224 public function getParamDescription() {
225 $p = $this->getModulePrefix();
226 return array(
227 'title' => "Title of the page you want to delete. Cannot be used together with {$p}pageid",
228 'pageid' => "Page ID of the page you want to delete. Cannot be used together with {$p}title",
229 'token' => 'A delete token previously retrieved through prop=info',
230 'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used',
231 'watch' => 'Add the page to your watchlist',
232 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
233 'unwatch' => 'Remove the page from your watchlist',
234 'oldimage' => 'The name of the old image to delete as provided by iiprop=archivename'
235 );
236 }
237
238 public function getDescription() {
239 return 'Delete a page';
240 }
241
242 public function getPossibleErrors() {
243 return array_merge( parent::getPossibleErrors(),
244 $this->getRequireOnlyOneParameterErrorMessages( array( 'title', 'pageid' ) ),
245 array(
246 array( 'invalidtitle', 'title' ),
247 array( 'nosuchpageid', 'pageid' ),
248 array( 'notanarticle' ),
249 array( 'hookaborted', 'error' ),
250 array( 'delete-toobig', 'limit' ),
251 array( 'cannotdelete', 'title' ),
252 array( 'invalidoldimage' ),
253 array( 'nodeleteablefile' ),
254 )
255 );
256 }
257
258 public function needsToken() {
259 return true;
260 }
261
262 public function getTokenSalt() {
263 return '';
264 }
265
266 public function getExamples() {
267 return array(
268 'api.php?action=delete&title=Main%20Page&token=123ABC' => 'Delete the Main Page',
269 'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move' => 'Delete the Main Page with the reason "Preparing for move"',
270 );
271 }
272
273 public function getHelpUrls() {
274 return 'https://www.mediawiki.org/wiki/API:Delete';
275 }
276
277 public function getVersion() {
278 return __CLASS__ . ': $Id$';
279 }
280 }