Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiMove.php
blobbfe428fe1d2f312934fbc6933a634606a77829f9
1 <?php
2 /**
3 * Copyright © 2007 Roan Kattouw <roan.kattouw@gmail.com>
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.
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.
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
20 * @file
23 namespace MediaWiki\Api;
25 use LogicException;
26 use MediaWiki\MainConfigNames;
27 use MediaWiki\Page\MovePageFactory;
28 use MediaWiki\Status\Status;
29 use MediaWiki\Title\Title;
30 use MediaWiki\User\Options\UserOptionsLookup;
31 use MediaWiki\Watchlist\WatchlistManager;
32 use RepoGroup;
33 use Wikimedia\ParamValidator\ParamValidator;
35 /**
36 * API Module to move pages
37 * @ingroup API
39 class ApiMove extends ApiBase {
41 use ApiWatchlistTrait;
43 private MovePageFactory $movePageFactory;
44 private RepoGroup $repoGroup;
46 public function __construct(
47 ApiMain $mainModule,
48 string $moduleName,
49 MovePageFactory $movePageFactory,
50 RepoGroup $repoGroup,
51 WatchlistManager $watchlistManager,
52 UserOptionsLookup $userOptionsLookup
53 ) {
54 parent::__construct( $mainModule, $moduleName );
56 $this->movePageFactory = $movePageFactory;
57 $this->repoGroup = $repoGroup;
59 // Variables needed in ApiWatchlistTrait trait
60 $this->watchlistExpiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry );
61 $this->watchlistMaxDuration =
62 $this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration );
63 $this->watchlistManager = $watchlistManager;
64 $this->userOptionsLookup = $userOptionsLookup;
67 public function execute() {
68 $this->useTransactionalTimeLimit();
70 $user = $this->getUser();
71 $params = $this->extractRequestParams();
73 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
75 if ( isset( $params['from'] ) ) {
76 $fromTitle = Title::newFromText( $params['from'] );
77 if ( !$fromTitle || $fromTitle->isExternal() ) {
78 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] );
80 } elseif ( isset( $params['fromid'] ) ) {
81 $fromTitle = Title::newFromID( $params['fromid'] );
82 if ( !$fromTitle ) {
83 $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
85 } else {
86 throw new LogicException( 'Unreachable due to requireOnlyOneParameter' );
89 if ( !$fromTitle->exists() ) {
90 $this->dieWithError( 'apierror-missingtitle' );
92 $fromTalk = $fromTitle->getTalkPage();
94 $toTitle = Title::newFromText( $params['to'] );
95 if ( !$toTitle || $toTitle->isExternal() ) {
96 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
98 $toTalk = $toTitle->getTalkPageIfDefined();
100 if ( $toTitle->getNamespace() === NS_FILE
101 && !$this->repoGroup->getLocalRepo()->findFile( $toTitle )
102 && $this->repoGroup->findFile( $toTitle )
104 if ( !$params['ignorewarnings'] &&
105 $this->getAuthority()->isAllowed( 'reupload-shared' ) ) {
106 $this->dieWithError( 'apierror-fileexists-sharedrepo-perm' );
107 } elseif ( !$this->getAuthority()->isAllowed( 'reupload-shared' ) ) {
108 $this->dieWithError( 'apierror-cantoverwrite-sharedfile' );
112 // Move the page
113 $toTitleExists = $toTitle->exists();
114 $mp = $this->movePageFactory->newMovePage( $fromTitle, $toTitle );
115 $status = $mp->moveIfAllowed(
116 $this->getAuthority(),
117 $params['reason'],
118 !$params['noredirect'],
119 $params['tags'] ?: []
121 if ( !$status->isOK() ) {
122 $this->dieStatus( $status );
125 $r = [
126 'from' => $fromTitle->getPrefixedText(),
127 'to' => $toTitle->getPrefixedText(),
128 'reason' => $params['reason']
131 // NOTE: we assume that if the old title exists, it's because it was re-created as
132 // a redirect to the new title. This is not safe, but what we did before was
133 // even worse: we just determined whether a redirect should have been created,
134 // and reported that it was created if it should have, without any checks.
135 $r['redirectcreated'] = $fromTitle->exists();
137 $r['moveoverredirect'] = $toTitleExists;
139 // Move the talk page
140 if ( $params['movetalk'] && $toTalk && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
141 $toTalkExists = $toTalk->exists();
142 $mp = $this->movePageFactory->newMovePage( $fromTalk, $toTalk );
143 $status = $mp->moveIfAllowed(
144 $this->getAuthority(),
145 $params['reason'],
146 !$params['noredirect'],
147 $params['tags'] ?: []
149 if ( $status->isOK() ) {
150 $r['talkfrom'] = $fromTalk->getPrefixedText();
151 $r['talkto'] = $toTalk->getPrefixedText();
152 $r['talkmoveoverredirect'] = $toTalkExists;
153 } else {
154 // We're not going to dieWithError() on failure, since we already changed something
155 $r['talkmove-errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
159 $result = $this->getResult();
161 // Move subpages
162 if ( $params['movesubpages'] ) {
163 $r['subpages'] = $this->moveSubpages(
164 $fromTitle,
165 $toTitle,
166 $params['reason'],
167 $params['noredirect'],
168 $params['tags'] ?: []
170 ApiResult::setIndexedTagName( $r['subpages'], 'subpage' );
172 if ( $params['movetalk'] && $toTalk ) {
173 $r['subpages-talk'] = $this->moveSubpages(
174 $fromTalk,
175 $toTalk,
176 $params['reason'],
177 $params['noredirect'],
178 $params['tags'] ?: []
180 ApiResult::setIndexedTagName( $r['subpages-talk'], 'subpage' );
184 $watch = $params['watchlist'] ?? 'preferences';
185 $watchlistExpiry = $this->getExpiryFromParams( $params );
187 // Watch pages
188 $this->setWatch( $watch, $fromTitle, $user, 'watchmoves', $watchlistExpiry );
189 $this->setWatch( $watch, $toTitle, $user, 'watchmoves', $watchlistExpiry );
191 $result->addValue( null, $this->getModuleName(), $r );
195 * @param Title $fromTitle
196 * @param Title $toTitle
197 * @param string $reason
198 * @param bool $noredirect
199 * @param string[] $changeTags Applied to the entry in the move log and redirect page revisions
200 * @return array
202 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect, $changeTags = [] ) {
203 $retval = [];
205 $mp = $this->movePageFactory->newMovePage( $fromTitle, $toTitle );
206 $result =
207 $mp->moveSubpagesIfAllowed( $this->getAuthority(), $reason, !$noredirect, $changeTags );
208 if ( !$result->isOK() ) {
209 // This means the whole thing failed
210 return [ 'errors' => $this->getErrorFormatter()->arrayFromStatus( $result ) ];
213 // At least some pages could be moved
214 // Report each of them separately
215 foreach ( $result->getValue() as $oldTitle => $status ) {
216 /** @var Status $status */
217 $r = [ 'from' => $oldTitle ];
218 if ( $status->isOK() ) {
219 $r['to'] = $status->getValue();
220 } else {
221 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
223 $retval[] = $r;
226 return $retval;
229 public function mustBePosted() {
230 return true;
233 public function isWriteMode() {
234 return true;
237 public function getAllowedParams() {
238 $params = [
239 'from' => null,
240 'fromid' => [
241 ParamValidator::PARAM_TYPE => 'integer'
243 'to' => [
244 ParamValidator::PARAM_TYPE => 'string',
245 ParamValidator::PARAM_REQUIRED => true
247 'reason' => '',
248 'movetalk' => false,
249 'movesubpages' => false,
250 'noredirect' => false,
253 // Params appear in the docs in the order they are defined,
254 // which is why this is here and not at the bottom.
255 $params += $this->getWatchlistParams();
257 return $params + [
258 'ignorewarnings' => false,
259 'tags' => [
260 ParamValidator::PARAM_TYPE => 'tags',
261 ParamValidator::PARAM_ISMULTI => true,
266 public function needsToken() {
267 return 'csrf';
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',
278 public function getHelpUrls() {
279 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Move';
283 /** @deprecated class alias since 1.43 */
284 class_alias( ApiMove::class, 'ApiMove' );