Merge "Revert "selenium: add new message banner test to user spec""
[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->isGood() ) {
79 $this->dieStatus( $status );
80 }
81
82 // Deprecated parameters
83 if ( $params['watch'] ) {
84 $watch = 'watch';
85 } elseif ( $params['unwatch'] ) {
86 $watch = 'unwatch';
87 } else {
88 $watch = $params['watchlist'];
89 }
90 $this->setWatch( $watch, $titleObj, 'watchdeletion' );
91
92 $r = [
93 'title' => $titleObj->getPrefixedText(),
94 'reason' => $reason,
95 'logid' => $status->value
96 ];
97 $this->getResult()->addValue( null, $this->getModuleName(), $r );
98 }
99
100 /**
101 * We have our own delete() function, since Article.php's implementation is split in two phases
102 *
103 * @param Page|WikiPage $page Page or WikiPage object to work on
104 * @param User $user User doing the action
105 * @param string|null &$reason Reason for the deletion. Autogenerated if null
106 * @param array $tags Tags to tag the deletion with
107 * @return Status
108 */
109 protected static function delete( Page $page, User $user, &$reason = null, $tags = [] ) {
110 $title = $page->getTitle();
111
112 // Auto-generate a summary, if necessary
113 if ( is_null( $reason ) ) {
114 // Need to pass a throwaway variable because generateReason expects
115 // a reference
116 $hasHistory = false;
117 $reason = $page->getAutoDeleteReason( $hasHistory );
118 if ( $reason === false ) {
119 return Status::newFatal( 'cannotdelete', $title->getPrefixedText() );
120 }
121 }
122
123 $error = '';
124
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, $user, $tags );
127 }
128
129 /**
130 * @param File $file
131 * @return bool
132 */
133 protected static function canDeleteFile( File $file ) {
134 return $file->exists() && $file->isLocal() && !$file->getRedirected();
135 }
136
137 /**
138 * @param Page $page Object to work on
139 * @param User $user User doing the action
140 * @param string $oldimage Archive name
141 * @param string &$reason Reason for the deletion. Autogenerated if null.
142 * @param bool $suppress Whether to mark all deleted versions as restricted
143 * @param array $tags Tags to tag the deletion with
144 * @return Status
145 */
146 protected static function deleteFile( Page $page, User $user, $oldimage,
147 &$reason = null, $suppress = false, $tags = []
148 ) {
149 $title = $page->getTitle();
150
151 $file = $page->getFile();
152 if ( !self::canDeleteFile( $file ) ) {
153 return self::delete( $page, $user, $reason, $tags );
154 }
155
156 if ( $oldimage ) {
157 if ( !FileDeleteForm::isValidOldSpec( $oldimage ) ) {
158 return Status::newFatal( 'invalidoldimage' );
159 }
160 $oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $oldimage );
161 if ( !$oldfile->exists() || !$oldfile->isLocal() || $oldfile->getRedirected() ) {
162 return Status::newFatal( 'nodeleteablefile' );
163 }
164 }
165
166 if ( is_null( $reason ) ) { // Log and RC don't like null reasons
167 $reason = '';
168 }
169
170 return FileDeleteForm::doDelete( $title, $file, $oldimage, $reason, $suppress, $user, $tags );
171 }
172
173 public function mustBePosted() {
174 return true;
175 }
176
177 public function isWriteMode() {
178 return true;
179 }
180
181 public function getAllowedParams() {
182 return [
183 'title' => null,
184 'pageid' => [
185 ApiBase::PARAM_TYPE => 'integer'
186 ],
187 'reason' => null,
188 'tags' => [
189 ApiBase::PARAM_TYPE => 'tags',
190 ApiBase::PARAM_ISMULTI => true,
191 ],
192 'watch' => [
193 ApiBase::PARAM_DFLT => false,
194 ApiBase::PARAM_DEPRECATED => true,
195 ],
196 'watchlist' => [
197 ApiBase::PARAM_DFLT => 'preferences',
198 ApiBase::PARAM_TYPE => [
199 'watch',
200 'unwatch',
201 'preferences',
202 'nochange'
203 ],
204 ],
205 'unwatch' => [
206 ApiBase::PARAM_DFLT => false,
207 ApiBase::PARAM_DEPRECATED => true,
208 ],
209 'oldimage' => null,
210 ];
211 }
212
213 public function needsToken() {
214 return 'csrf';
215 }
216
217 protected function getExamplesMessages() {
218 return [
219 'action=delete&title=Main%20Page&token=123ABC'
220 => 'apihelp-delete-example-simple',
221 'action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'
222 => 'apihelp-delete-example-reason',
223 ];
224 }
225
226 public function getHelpUrls() {
227 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Delete';
228 }
229 }