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