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