Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[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 $user->spreadAnyEditBlock();
90 $this->dieStatus( $status );
91 }
92
93 $r = [
94 'from' => $fromTitle->getPrefixedText(),
95 'to' => $toTitle->getPrefixedText(),
96 'reason' => $params['reason']
97 ];
98
99 // NOTE: we assume that if the old title exists, it's because it was re-created as
100 // a redirect to the new title. This is not safe, but what we did before was
101 // even worse: we just determined whether a redirect should have been created,
102 // and reported that it was created if it should have, without any checks.
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 }
156
157 // Watch pages
158 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
159 $this->setWatch( $watch, $toTitle, 'watchmoves' );
160
161 $result->addValue( null, $this->getModuleName(), $r );
162 }
163
164 /**
165 * @param Title $from
166 * @param Title $to
167 * @param string $reason
168 * @param bool $createRedirect
169 * @param array $changeTags Applied to the entry in the move log and redirect page revision
170 * @return Status
171 */
172 protected function movePage( Title $from, Title $to, $reason, $createRedirect, $changeTags ) {
173 $mp = new MovePage( $from, $to );
174 $valid = $mp->isValidMove();
175 if ( !$valid->isOK() ) {
176 return $valid;
177 }
178
179 $user = $this->getUser();
180 $permStatus = $mp->checkPermissions( $user, $reason );
181 if ( !$permStatus->isOK() ) {
182 return $permStatus;
183 }
184
185 // Check suppressredirect permission
186 if ( !$user->isAllowed( 'suppressredirect' ) ) {
187 $createRedirect = true;
188 }
189
190 return $mp->move( $user, $reason, $createRedirect, $changeTags );
191 }
192
193 /**
194 * @param Title $fromTitle
195 * @param Title $toTitle
196 * @param string $reason
197 * @param bool $noredirect
198 * @param array $changeTags Applied to the entry in the move log and redirect page revisions
199 * @return array
200 */
201 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect, $changeTags = [] ) {
202 $retval = [];
203
204 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect, $changeTags );
205 if ( isset( $success[0] ) ) {
206 $status = $this->errorArrayToStatus( $success );
207 return [ 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ) ];
208 }
209
210 // At least some pages could be moved
211 // Report each of them separately
212 foreach ( $success as $oldTitle => $newTitle ) {
213 $r = [ 'from' => $oldTitle ];
214 if ( is_array( $newTitle ) ) {
215 $status = $this->errorArrayToStatus( $newTitle );
216 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
217 } else {
218 // Success
219 $r['to'] = $newTitle;
220 }
221 $retval[] = $r;
222 }
223
224 return $retval;
225 }
226
227 public function mustBePosted() {
228 return true;
229 }
230
231 public function isWriteMode() {
232 return true;
233 }
234
235 public function getAllowedParams() {
236 return [
237 'from' => null,
238 'fromid' => [
239 ApiBase::PARAM_TYPE => 'integer'
240 ],
241 'to' => [
242 ApiBase::PARAM_TYPE => 'string',
243 ApiBase::PARAM_REQUIRED => true
244 ],
245 'reason' => '',
246 'movetalk' => false,
247 'movesubpages' => false,
248 'noredirect' => false,
249 'watchlist' => [
250 ApiBase::PARAM_DFLT => 'preferences',
251 ApiBase::PARAM_TYPE => [
252 'watch',
253 'unwatch',
254 'preferences',
255 'nochange'
256 ],
257 ],
258 'ignorewarnings' => false,
259 'tags' => [
260 ApiBase::PARAM_TYPE => 'tags',
261 ApiBase::PARAM_ISMULTI => true,
262 ],
263 ];
264 }
265
266 public function needsToken() {
267 return 'csrf';
268 }
269
270 protected function getExamplesMessages() {
271 return [
272 'action=move&from=Badtitle&to=Goodtitle&token=123ABC&' .
273 'reason=Misspelled%20title&movetalk=&noredirect='
274 => 'apihelp-move-example-move',
275 ];
276 }
277
278 public function getHelpUrls() {
279 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Move';
280 }
281 }