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