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