Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[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 // @phan-suppress-next-line PhanUndeclaredMethod
46 !( $titleObj->getNamespace() == NS_FILE && self::canDeleteFile( $pageObj->getFile() ) )
47 ) {
48 $this->dieWithError( 'apierror-missingtitle' );
49 }
50
51 $reason = $params['reason'];
52 $user = $this->getUser();
53
54 // Check that the user is allowed to carry out the deletion
55 $this->checkTitleUserPermissions( $titleObj, 'delete' );
56
57 // If change tagging was requested, check that the user is allowed to tag,
58 // and the tags are valid
59 if ( $params['tags'] ) {
60 $tagStatus = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
61 if ( !$tagStatus->isOK() ) {
62 $this->dieStatus( $tagStatus );
63 }
64 }
65
66 if ( $titleObj->getNamespace() == NS_FILE ) {
67 $status = self::deleteFile(
68 $pageObj,
69 $user,
70 $params['oldimage'],
71 $reason,
72 false,
73 $params['tags']
74 );
75 } else {
76 $status = self::delete( $pageObj, $user, $reason, $params['tags'] );
77 }
78
79 if ( !$status->isOK() ) {
80 $this->dieStatus( $status );
81 }
82 $this->addMessagesFromStatus( $status, [ 'warning' ], [ 'delete-scheduled' ] );
83
84 // Deprecated parameters
85 if ( $params['watch'] ) {
86 $watch = 'watch';
87 } elseif ( $params['unwatch'] ) {
88 $watch = 'unwatch';
89 } else {
90 $watch = $params['watchlist'];
91 }
92 $this->setWatch( $watch, $titleObj, 'watchdeletion' );
93
94 $r = [
95 'title' => $titleObj->getPrefixedText(),
96 'reason' => $reason,
97 ];
98 if ( $status->hasMessage( 'delete-scheduled' ) ) {
99 $r['scheduled'] = true;
100 }
101 if ( $status->value !== null ) {
102 // Scheduled deletions don't currently have a log entry available at this point
103 $r['logid'] = $status->value;
104 }
105 $this->getResult()->addValue( null, $this->getModuleName(), $r );
106 }
107
108 /**
109 * We have our own delete() function, since Article.php's implementation is split in two phases
110 *
111 * @param Page|WikiPage $page Page or WikiPage object to work on
112 * @param User $user User doing the action
113 * @param string|null &$reason Reason for the deletion. Autogenerated if null
114 * @param array $tags Tags to tag the deletion with
115 * @return Status
116 */
117 protected static function delete( Page $page, User $user, &$reason = null, $tags = [] ) {
118 $title = $page->getTitle();
119
120 // Auto-generate a summary, if necessary
121 if ( is_null( $reason ) ) {
122 // Need to pass a throwaway variable because generateReason expects
123 // a reference
124 $hasHistory = false;
125 $reason = $page->getAutoDeleteReason( $hasHistory );
126 if ( $reason === false ) {
127 // Should be reachable only if the page has no revisions
128 return Status::newFatal( 'cannotdelete', $title->getPrefixedText() ); // @codeCoverageIgnore
129 }
130 }
131
132 $error = '';
133
134 // Luckily, Article.php provides a reusable delete function that does the hard work for us
135 return $page->doDeleteArticleReal( $reason, false, 0, true, $error, $user, $tags );
136 }
137
138 /**
139 * @param File $file
140 * @return bool
141 */
142 protected static function canDeleteFile( File $file ) {
143 return $file->exists() && $file->isLocal() && !$file->getRedirected();
144 }
145
146 /**
147 * @param Page $page Object to work on
148 * @param User $user User doing the action
149 * @param string $oldimage Archive name
150 * @param string|null &$reason Reason for the deletion. Autogenerated if null.
151 * @param bool $suppress Whether to mark all deleted versions as restricted
152 * @param array $tags Tags to tag the deletion with
153 * @return Status
154 */
155 protected static function deleteFile( Page $page, User $user, $oldimage,
156 &$reason = null, $suppress = false, $tags = []
157 ) {
158 $title = $page->getTitle();
159
160 // @phan-suppress-next-line PhanUndeclaredMethod There's no right typehint for it
161 $file = $page->getFile();
162 if ( !self::canDeleteFile( $file ) ) {
163 return self::delete( $page, $user, $reason, $tags );
164 }
165
166 if ( $oldimage ) {
167 if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
168 return Status::newFatal( 'invalidoldimage' );
169 }
170 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
171 if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
172 return Status::newFatal( 'nodeleteablefile' );
173 }
174 }
175
176 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
177 $reason = '';
178 }
179
180 return FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress, $user, $tags );
181 }
182
183 public function mustBePosted() {
184 return true;
185 }
186
187 public function isWriteMode() {
188 return true;
189 }
190
191 public function getAllowedParams() {
192 return [
193 'title' => null,
194 'pageid' => [
195 ApiBase::PARAM_TYPE => 'integer'
196 ],
197 'reason' => null,
198 'tags' => [
199 ApiBase::PARAM_TYPE => 'tags',
200 ApiBase::PARAM_ISMULTI => true,
201 ],
202 'watch' => [
203 ApiBase::PARAM_DFLT => false,
204 ApiBase::PARAM_DEPRECATED => true,
205 ],
206 'watchlist' => [
207 ApiBase::PARAM_DFLT => 'preferences',
208 ApiBase::PARAM_TYPE => [
209 'watch',
210 'unwatch',
211 'preferences',
212 'nochange'
213 ],
214 ],
215 'unwatch' => [
216 ApiBase::PARAM_DFLT => false,
217 ApiBase::PARAM_DEPRECATED => true,
218 ],
219 'oldimage' => null,
220 ];
221 }
222
223 public function needsToken() {
224 return 'csrf';
225 }
226
227 protected function getExamplesMessages() {
228 return [
229 'action=delete&title=Main%20Page&token=123ABC'
230 => 'apihelp-delete-example-simple',
231 'action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
232 => 'apihelp-delete-example-reason',
233 ];
234 }
235
236 public function getHelpUrls() {
237 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Delete';
238 }
239 }