Merge "Add .pipeline/ with dev image variant"
[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 $repoGroup = MediaWikiServices::getInstance()->getRepoGroup();
63 if ( $toTitle->getNamespace() == NS_FILE
64 && !$repoGroup->getLocalRepo()->findFile( $toTitle )
65 && $repoGroup->findFile( $toTitle )
66 ) {
67 if ( !$params['ignorewarnings'] &&
68 $this->getPermissionManager()->userHasRight( $user, 'reupload-shared' ) ) {
69 $this->dieWithError( 'apierror-fileexists-sharedrepo-perm' );
70 } elseif ( !$this->getPermissionManager()->userHasRight( $user, 'reupload-shared' ) ) {
71 $this->dieWithError( 'apierror-cantoverwrite-sharedfile' );
72 }
73 }
74
75 // Rate limit
76 if ( $user->pingLimiter( 'move' ) ) {
77 $this->dieWithError( 'apierror-ratelimited' );
78 }
79
80 // Check if the user is allowed to add the specified changetags
81 if ( $params['tags'] ) {
82 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
83 if ( !$ableToTag->isOK() ) {
84 $this->dieStatus( $ableToTag );
85 }
86 }
87
88 // Move the page
89 $toTitleExists = $toTitle->exists();
90 $status = $this->movePage( $fromTitle, $toTitle, $params['reason'], !$params['noredirect'],
91 $params['tags'] ?: [] );
92 if ( !$status->isOK() ) {
93 $user->spreadAnyEditBlock();
94 $this->dieStatus( $status );
95 }
96
97 $r = [
98 'from' => $fromTitle->getPrefixedText(),
99 'to' => $toTitle->getPrefixedText(),
100 'reason' => $params['reason']
101 ];
102
103 // NOTE: we assume that if the old title exists, it's because it was re-created as
104 // a redirect to the new title. This is not safe, but what we did before was
105 // even worse: we just determined whether a redirect should have been created,
106 // and reported that it was created if it should have, without any checks.
107 $r['redirectcreated'] = $fromTitle->exists();
108
109 $r['moveoverredirect'] = $toTitleExists;
110
111 // Move the talk page
112 if ( $params['movetalk'] && $toTalk && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
113 $toTalkExists = $toTalk->exists();
114 $status = $this->movePage(
115 $fromTalk,
116 $toTalk,
117 $params['reason'],
118 !$params['noredirect'],
119 $params['tags'] ?: []
120 );
121 if ( $status->isOK() ) {
122 $r['talkfrom'] = $fromTalk->getPrefixedText();
123 $r['talkto'] = $toTalk->getPrefixedText();
124 $r['talkmoveoverredirect'] = $toTalkExists;
125 } else {
126 // We're not going to dieWithError() on failure, since we already changed something
127 $r['talkmove-errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
128 }
129 }
130
131 $result = $this->getResult();
132
133 // Move subpages
134 if ( $params['movesubpages'] ) {
135 $r['subpages'] = $this->moveSubpages(
136 $fromTitle,
137 $toTitle,
138 $params['reason'],
139 $params['noredirect'],
140 $params['tags'] ?: []
141 );
142 ApiResult::setIndexedTagName( $r['subpages'], 'subpage' );
143
144 if ( $params['movetalk'] ) {
145 $r['subpages-talk'] = $this->moveSubpages(
146 $fromTalk,
147 $toTalk,
148 $params['reason'],
149 $params['noredirect'],
150 $params['tags'] ?: []
151 );
152 ApiResult::setIndexedTagName( $r['subpages-talk'], 'subpage' );
153 }
154 }
155
156 $watch = 'preferences';
157 if ( isset( $params['watchlist'] ) ) {
158 $watch = $params['watchlist'];
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 = MediaWikiServices::getInstance()->getMovePageFactory()->newMovePage( $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 ( !$this->getPermissionManager()->userHasRight( $user, '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 $mp = new MovePage( $fromTitle, $toTitle );
209 $result =
210 $mp->moveSubpagesIfAllowed( $this->getUser(), $reason, !$noredirect, $changeTags );
211 if ( !$result->isOK() ) {
212 // This means the whole thing failed
213 return [ 'errors' => $this->getErrorFormatter()->arrayFromStatus( $result ) ];
214 }
215
216 // At least some pages could be moved
217 // Report each of them separately
218 foreach ( $result->getValue() as $oldTitle => $status ) {
219 $r = [ 'from' => $oldTitle ];
220 if ( $status->isOK() ) {
221 $r['to'] = $status->getValue();
222 } else {
223 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
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 'watchlist' => [
254 ApiBase::PARAM_DFLT => 'preferences',
255 ApiBase::PARAM_TYPE => [
256 'watch',
257 'unwatch',
258 'preferences',
259 'nochange'
260 ],
261 ],
262 'ignorewarnings' => false,
263 'tags' => [
264 ApiBase::PARAM_TYPE => 'tags',
265 ApiBase::PARAM_ISMULTI => true,
266 ],
267 ];
268 }
269
270 public function needsToken() {
271 return 'csrf';
272 }
273
274 protected function getExamplesMessages() {
275 return [
276 'action=move&from=Badtitle&to=Goodtitle&token=123ABC&' .
277 'reason=Misspelled%20title&movetalk=&noredirect='
278 => 'apihelp-move-example-move',
279 ];
280 }
281
282 public function getHelpUrls() {
283 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Move';
284 }
285 }