3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@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
23 use MediaWiki\MediaWikiServices
;
26 * API Module to move pages
29 class ApiMove
extends ApiBase
{
31 public function execute() {
32 $this->useTransactionalTimeLimit();
34 $user = $this->getUser();
35 $params = $this->extractRequestParams();
37 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
39 if ( isset( $params['from'] ) ) {
40 $fromTitle = Title
::newFromText( $params['from'] );
41 if ( !$fromTitle ||
$fromTitle->isExternal() ) {
42 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] );
44 } elseif ( isset( $params['fromid'] ) ) {
45 $fromTitle = Title
::newFromID( $params['fromid'] );
47 $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
51 if ( !$fromTitle->exists() ) {
52 $this->dieWithError( 'apierror-missingtitle' );
54 $fromTalk = $fromTitle->getTalkPage();
56 $toTitle = Title
::newFromText( $params['to'] );
57 if ( !$toTitle ||
$toTitle->isExternal() ) {
58 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
60 $toTalk = $toTitle->getTalkPageIfDefined();
62 if ( $toTitle->getNamespace() == NS_FILE
63 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $toTitle )
64 && MediaWikiServices
::getInstance()->getRepoGroup()->findFile( $toTitle )
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' );
75 if ( $user->pingLimiter( 'move' ) ) {
76 $this->dieWithError( 'apierror-ratelimited' );
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 );
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 );
97 'from' => $fromTitle->getPrefixedText(),
98 'to' => $toTitle->getPrefixedText(),
99 'reason' => $params['reason']
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();
108 $r['moveoverredirect'] = $toTitleExists;
110 // Move the talk page
111 if ( $params['movetalk'] && $toTalk && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
112 $toTalkExists = $toTalk->exists();
113 $status = $this->movePage(
117 !$params['noredirect'],
118 $params['tags'] ?
: []
120 if ( $status->isOK() ) {
121 $r['talkfrom'] = $fromTalk->getPrefixedText();
122 $r['talkto'] = $toTalk->getPrefixedText();
123 $r['talkmoveoverredirect'] = $toTalkExists;
125 // We're not going to dieWithError() on failure, since we already changed something
126 $r['talkmove-errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
130 $result = $this->getResult();
133 if ( $params['movesubpages'] ) {
134 $r['subpages'] = $this->moveSubpages(
138 $params['noredirect'],
139 $params['tags'] ?
: []
141 ApiResult
::setIndexedTagName( $r['subpages'], 'subpage' );
143 if ( $params['movetalk'] ) {
144 $r['subpages-talk'] = $this->moveSubpages(
148 $params['noredirect'],
149 $params['tags'] ?
: []
151 ApiResult
::setIndexedTagName( $r['subpages-talk'], 'subpage' );
155 $watch = 'preferences';
156 if ( isset( $params['watchlist'] ) ) {
157 $watch = $params['watchlist'];
161 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
162 $this->setWatch( $watch, $toTitle, 'watchmoves' );
164 $result->addValue( null, $this->getModuleName(), $r );
170 * @param string $reason
171 * @param bool $createRedirect
172 * @param array $changeTags Applied to the entry in the move log and redirect page revision
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() ) {
182 $user = $this->getUser();
183 $permStatus = $mp->checkPermissions( $user, $reason );
184 if ( !$permStatus->isOK() ) {
188 // Check suppressredirect permission
189 if ( !$this->getPermissionManager()->userHasRight( $user, 'suppressredirect' ) ) {
190 $createRedirect = true;
193 return $mp->move( $user, $reason, $createRedirect, $changeTags );
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
204 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect, $changeTags = [] ) {
207 $mp = new MovePage( $fromTitle, $toTitle );
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 ) ];
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();
222 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
230 public function mustBePosted() {
234 public function isWriteMode() {
238 public function getAllowedParams() {
242 ApiBase
::PARAM_TYPE
=> 'integer'
245 ApiBase
::PARAM_TYPE
=> 'string',
246 ApiBase
::PARAM_REQUIRED
=> true
250 'movesubpages' => false,
251 'noredirect' => false,
253 ApiBase
::PARAM_DFLT
=> 'preferences',
254 ApiBase
::PARAM_TYPE
=> [
261 'ignorewarnings' => false,
263 ApiBase
::PARAM_TYPE
=> 'tags',
264 ApiBase
::PARAM_ISMULTI
=> true,
269 public function needsToken() {
273 protected function getExamplesMessages() {
275 'action=move&from=Badtitle&to=Goodtitle&token=123ABC&' .
276 'reason=Misspelled%20title&movetalk=&noredirect='
277 => 'apihelp-move-example-move',
281 public function getHelpUrls() {
282 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Move';