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