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