Merge "REST: Implement 405 responses"
[lhc/web/wiklou.git] / includes / api / ApiMove.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 use MediaWiki\MediaWikiServices;
24
25 /**
26 * API Module to move pages
27 * @ingroup API
28 */
29 class ApiMove extends ApiBase {
30
31 public function execute() {
32 $this->useTransactionalTimeLimit();
33
34 $user = $this->getUser();
35 $params = $this->extractRequestParams();
36
37 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
38
39 if ( isset( $params['from'] ) ) {
40 $fromTitle = Title::newFromText( $params['from'] );
41 if ( !$fromTitle || $fromTitle->isExternal() ) {
42 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] );
43 }
44 } elseif ( isset( $params['fromid'] ) ) {
45 $fromTitle = Title::newFromID( $params['fromid'] );
46 if ( !$fromTitle ) {
47 $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
48 }
49 }
50
51 if ( !$fromTitle->exists() ) {
52 $this->dieWithError( 'apierror-missingtitle' );
53 }
54 $fromTalk = $fromTitle->getTalkPage();
55
56 $toTitle = Title::newFromText( $params['to'] );
57 if ( !$toTitle || $toTitle->isExternal() ) {
58 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
59 }
60 $toTalk = $toTitle->getTalkPageIfDefined();
61
62 if ( $toTitle->getNamespace() == NS_FILE
63 && !RepoGroup::singleton()->getLocalRepo()->findFile( $toTitle )
64 && MediaWikiServices::getInstance()->getRepoGroup()->findFile( $toTitle )
65 ) {
66 if ( !$params['ignorewarnings'] && $user->isAllowed( 'reupload-shared' ) ) {
67 $this->dieWithError( 'apierror-fileexists-sharedrepo-perm' );
68 } elseif ( !$user->isAllowed( 'reupload-shared' ) ) {
69 $this->dieWithError( 'apierror-cantoverwrite-sharedfile' );
70 }
71 }
72
73 // Rate limit
74 if ( $user->pingLimiter( 'move' ) ) {
75 $this->dieWithError( 'apierror-ratelimited' );
76 }
77
78 // Check if the user is allowed to add the specified changetags
79 if ( $params['tags'] ) {
80 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
81 if ( !$ableToTag->isOK() ) {
82 $this->dieStatus( $ableToTag );
83 }
84 }
85
86 // Move the page
87 $toTitleExists = $toTitle->exists();
88 $status = $this->movePage( $fromTitle, $toTitle, $params['reason'], !$params['noredirect'],
89 $params['tags'] ?: [] );
90 if ( !$status->isOK() ) {
91 $user->spreadAnyEditBlock();
92 $this->dieStatus( $status );
93 }
94
95 $r = [
96 'from' => $fromTitle->getPrefixedText(),
97 'to' => $toTitle->getPrefixedText(),
98 'reason' => $params['reason']
99 ];
100
101 // NOTE: we assume that if the old title exists, it's because it was re-created as
102 // a redirect to the new title. This is not safe, but what we did before was
103 // even worse: we just determined whether a redirect should have been created,
104 // and reported that it was created if it should have, without any checks.
105 $r['redirectcreated'] = $fromTitle->exists();
106
107 $r['moveoverredirect'] = $toTitleExists;
108
109 // Move the talk page
110 if ( $params['movetalk'] && $toTalk && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
111 $toTalkExists = $toTalk->exists();
112 $status = $this->movePage(
113 $fromTalk,
114 $toTalk,
115 $params['reason'],
116 !$params['noredirect'],
117 $params['tags'] ?: []
118 );
119 if ( $status->isOK() ) {
120 $r['talkfrom'] = $fromTalk->getPrefixedText();
121 $r['talkto'] = $toTalk->getPrefixedText();
122 $r['talkmoveoverredirect'] = $toTalkExists;
123 } else {
124 // We're not going to dieWithError() on failure, since we already changed something
125 $r['talkmove-errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
126 }
127 }
128
129 $result = $this->getResult();
130
131 // Move subpages
132 if ( $params['movesubpages'] ) {
133 $r['subpages'] = $this->moveSubpages(
134 $fromTitle,
135 $toTitle,
136 $params['reason'],
137 $params['noredirect'],
138 $params['tags'] ?: []
139 );
140 ApiResult::setIndexedTagName( $r['subpages'], 'subpage' );
141
142 if ( $params['movetalk'] ) {
143 $r['subpages-talk'] = $this->moveSubpages(
144 $fromTalk,
145 $toTalk,
146 $params['reason'],
147 $params['noredirect'],
148 $params['tags'] ?: []
149 );
150 ApiResult::setIndexedTagName( $r['subpages-talk'], 'subpage' );
151 }
152 }
153
154 $watch = 'preferences';
155 if ( isset( $params['watchlist'] ) ) {
156 $watch = $params['watchlist'];
157 }
158
159 // Watch pages
160 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
161 $this->setWatch( $watch, $toTitle, 'watchmoves' );
162
163 $result->addValue( null, $this->getModuleName(), $r );
164 }
165
166 /**
167 * @param Title $from
168 * @param Title $to
169 * @param string $reason
170 * @param bool $createRedirect
171 * @param array $changeTags Applied to the entry in the move log and redirect page revision
172 * @return Status
173 */
174 protected function movePage( Title $from, Title $to, $reason, $createRedirect, $changeTags ) {
175 $mp = new MovePage( $from, $to );
176 $valid = $mp->isValidMove();
177 if ( !$valid->isOK() ) {
178 return $valid;
179 }
180
181 $user = $this->getUser();
182 $permStatus = $mp->checkPermissions( $user, $reason );
183 if ( !$permStatus->isOK() ) {
184 return $permStatus;
185 }
186
187 // Check suppressredirect permission
188 if ( !$user->isAllowed( 'suppressredirect' ) ) {
189 $createRedirect = true;
190 }
191
192 return $mp->move( $user, $reason, $createRedirect, $changeTags );
193 }
194
195 /**
196 * @param Title $fromTitle
197 * @param Title $toTitle
198 * @param string $reason
199 * @param bool $noredirect
200 * @param array $changeTags Applied to the entry in the move log and redirect page revisions
201 * @return array
202 */
203 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect, $changeTags = [] ) {
204 $retval = [];
205
206 $mp = new MovePage( $fromTitle, $toTitle );
207 $result =
208 $mp->moveSubpagesIfAllowed( $this->getUser(), $reason, !$noredirect, $changeTags );
209 if ( !$result->isOk() ) {
210 // This means the whole thing failed
211 return [ 'errors' => $this->getErrorFormatter()->arrayFromStatus( $result ) ];
212 }
213
214 // At least some pages could be moved
215 // Report each of them separately
216 foreach ( $result->getValue() as $oldTitle => $status ) {
217 $r = [ 'from' => $oldTitle ];
218 if ( $status->isOK() ) {
219 $r['to'] = $status->getValue();
220 } else {
221 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
222 }
223 $retval[] = $r;
224 }
225
226 return $retval;
227 }
228
229 public function mustBePosted() {
230 return true;
231 }
232
233 public function isWriteMode() {
234 return true;
235 }
236
237 public function getAllowedParams() {
238 return [
239 'from' => null,
240 'fromid' => [
241 ApiBase::PARAM_TYPE => 'integer'
242 ],
243 'to' => [
244 ApiBase::PARAM_TYPE => 'string',
245 ApiBase::PARAM_REQUIRED => true
246 ],
247 'reason' => '',
248 'movetalk' => false,
249 'movesubpages' => false,
250 'noredirect' => false,
251 'watchlist' => [
252 ApiBase::PARAM_DFLT => 'preferences',
253 ApiBase::PARAM_TYPE => [
254 'watch',
255 'unwatch',
256 'preferences',
257 'nochange'
258 ],
259 ],
260 'ignorewarnings' => false,
261 'tags' => [
262 ApiBase::PARAM_TYPE => 'tags',
263 ApiBase::PARAM_ISMULTI => true,
264 ],
265 ];
266 }
267
268 public function needsToken() {
269 return 'csrf';
270 }
271
272 protected function getExamplesMessages() {
273 return [
274 'action=move&from=Badtitle&to=Goodtitle&token=123ABC&' .
275 'reason=Misspelled%20title&movetalk=&noredirect='
276 => 'apihelp-move-example-move',
277 ];
278 }
279
280 public function getHelpUrls() {
281 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Move';
282 }
283 }