ApiDelete: Handle batched deletions properly
[lhc/web/wiklou.git] / includes / api / ApiDelete.php
1 <?php
2 /**
3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * API module that facilitates deleting pages. The API equivalent of action=delete.
25 * Requires API write mode to be enabled.
26 *
27 * @ingroup API
28 */
29 class ApiDelete extends ApiBase {
30 /**
31 * Extracts the title and reason from the request parameters and invokes
32 * the local delete() function with these as arguments. It does not make use of
33 * the delete function specified by Article.php. If the deletion succeeds, the
34 * details of the article deleted and the reason for deletion are added to the
35 * result object.
36 */
37 public function execute() {
38 $this->useTransactionalTimeLimit();
39
40 $params = $this->extractRequestParams();
41
42 $pageObj = $this->getTitleOrPageId( $params, 'fromdbmaster' );
43 $titleObj = $pageObj->getTitle();
44 if ( !$pageObj->exists() &&
45 !( $titleObj->getNamespace() == NS_FILE && self::canDeleteFile( $pageObj->getFile() ) )
46 ) {
47 $this->dieWithError( 'apierror-missingtitle' );
48 }
49
50 $reason = $params['reason'];
51 $user = $this->getUser();
52
53 // Check that the user is allowed to carry out the deletion
54 $this->checkTitleUserPermissions( $titleObj, 'delete' );
55
56 // If change tagging was requested, check that the user is allowed to tag,
57 // and the tags are valid
58 if ( $params['tags'] ) {
59 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
60 if ( !$tagStatus->isOK() ) {
61 $this->dieStatus( $tagStatus );
62 }
63 }
64
65 if ( $titleObj->getNamespace() == NS_FILE ) {
66 $status = self::deleteFile(
67 $pageObj,
68 $user,
69 $params['oldimage'],
70 $reason,
71 false,
72 $params['tags']
73 );
74 } else {
75 $status = self::delete( $pageObj, $user, $reason, $params['tags'] );
76 }
77
78 if ( !$status->isOk() ) {
79 $this->dieStatus( $status );
80 }
81 $this->addMessagesFromStatus( $status, [ 'warning' ], [ 'delete-scheduled' ] );
82
83 // Deprecated parameters
84 if ( $params['watch'] ) {
85 $watch = 'watch';
86 } elseif ( $params['unwatch'] ) {
87 $watch = 'unwatch';
88 } else {
89 $watch = $params['watchlist'];
90 }
91 $this->setWatch( $watch, $titleObj, 'watchdeletion' );
92
93 $r = [
94 'title' => $titleObj->getPrefixedText(),
95 'reason' => $reason,
96 ];
97 if ( $status->hasMessage( 'delete-scheduled' ) ) {
98 $r['scheduled'] = true;
99 }
100 if ( $status->value !== null ) {
101 // Scheduled deletions don't currently have a log entry available at this point
102 $r['logid'] = $status->value;
103 }
104 $this->getResult()->addValue( null, $this->getModuleName(), $r );
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 $page Page or WikiPage object to work on
111 * @param User $user User doing the action
112 * @param string|null &$reason Reason for the deletion. Autogenerated if null
113 * @param array $tags Tags to tag the deletion with
114 * @return Status
115 */
116 protected static function delete( Page $page, User $user, &$reason = null, $tags = [] ) {
117 $title = $page->getTitle();
118
119 // Auto-generate a summary, if necessary
120 if ( is_null( $reason ) ) {
121 // Need to pass a throwaway variable because generateReason expects
122 // a reference
123 $hasHistory = false;
124 $reason = $page->getAutoDeleteReason( $hasHistory );
125 if ( $reason === false ) {
126 // Should be reachable only if the page has no revisions
127 return Status::newFatal( 'cannotdelete', $title->getPrefixedText() ); // @codeCoverageIgnore
128 }
129 }
130
131 $error = '';
132
133 // Luckily, Article.php provides a reusable delete function that does the hard work for us
134 return $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user, $tags );
135 }
136
137 /**
138 * @param File $file
139 * @return bool
140 */
141 protected static function canDeleteFile( File $file ) {
142 return $file->exists() && $file->isLocal() && !$file->getRedirected();
143 }
144
145 /**
146 * @param Page $page Object to work on
147 * @param User $user User doing the action
148 * @param string $oldimage Archive name
149 * @param string|null &$reason Reason for the deletion. Autogenerated if null.
150 * @param bool $suppress Whether to mark all deleted versions as restricted
151 * @param array $tags Tags to tag the deletion with
152 * @return Status
153 */
154 protected static function deleteFile( Page $page, User $user, $oldimage,
155 &$reason = null, $suppress = false, $tags = []
156 ) {
157 $title = $page->getTitle();
158
159 $file = $page->getFile();
160 if ( !self::canDeleteFile( $file ) ) {
161 return self::delete( $page, $user, $reason, $tags );
162 }
163
164 if ( $oldimage ) {
165 if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
166 return Status::newFatal( 'invalidoldimage' );
167 }
168 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
169 if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
170 return Status::newFatal( 'nodeleteablefile' );
171 }
172 }
173
174 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
175 $reason = '';
176 }
177
178 return FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress, $user, $tags );
179 }
180
181 public function mustBePosted() {
182 return true;
183 }
184
185 public function isWriteMode() {
186 return true;
187 }
188
189 public function getAllowedParams() {
190 return [
191 'title' => null,
192 'pageid' => [
193 ApiBase::PARAM_TYPE => 'integer'
194 ],
195 'reason' => null,
196 'tags' => [
197 ApiBase::PARAM_TYPE => 'tags',
198 ApiBase::PARAM_ISMULTI => true,
199 ],
200 'watch' => [
201 ApiBase::PARAM_DFLT => false,
202 ApiBase::PARAM_DEPRECATED => true,
203 ],
204 'watchlist' => [
205 ApiBase::PARAM_DFLT => 'preferences',
206 ApiBase::PARAM_TYPE => [
207 'watch',
208 'unwatch',
209 'preferences',
210 'nochange'
211 ],
212 ],
213 'unwatch' => [
214 ApiBase::PARAM_DFLT => false,
215 ApiBase::PARAM_DEPRECATED => true,
216 ],
217 'oldimage' => null,
218 ];
219 }
220
221 public function needsToken() {
222 return 'csrf';
223 }
224
225 protected function getExamplesMessages() {
226 return [
227 'action=delete&title=Main%20Page&token=123ABC'
228 => 'apihelp-delete-example-simple',
229 'action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
230 => 'apihelp-delete-example-reason',
231 ];
232 }
233
234 public function getHelpUrls() {
235 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Delete';
236 }
237 }