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
24 * API Module to move pages
27 class ApiMove
extends ApiBase
{
29 public function execute() {
30 $this->useTransactionalTimeLimit();
32 $user = $this->getUser();
33 $params = $this->extractRequestParams();
35 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
37 if ( isset( $params['from'] ) ) {
38 $fromTitle = Title
::newFromText( $params['from'] );
39 if ( !$fromTitle ||
$fromTitle->isExternal() ) {
40 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] );
42 } elseif ( isset( $params['fromid'] ) ) {
43 $fromTitle = Title
::newFromID( $params['fromid'] );
45 $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
49 if ( !$fromTitle->exists() ) {
50 $this->dieWithError( 'apierror-missingtitle' );
52 $fromTalk = $fromTitle->getTalkPage();
54 $toTitle = Title
::newFromText( $params['to'] );
55 if ( !$toTitle ||
$toTitle->isExternal() ) {
56 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
58 $toTalk = $toTitle->getTalkPageIfDefined();
60 if ( $toTitle->getNamespace() == NS_FILE
61 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $toTitle )
62 && wfFindFile( $toTitle )
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' );
72 if ( $user->pingLimiter( 'move' ) ) {
73 $this->dieWithError( 'apierror-ratelimited' );
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 );
85 $toTitleExists = $toTitle->exists();
86 $status = $this->movePage( $fromTitle, $toTitle, $params['reason'], !$params['noredirect'],
87 $params['tags'] ?
: [] );
88 if ( !$status->isOK() ) {
89 $this->dieStatus( $status );
93 'from' => $fromTitle->getPrefixedText(),
94 'to' => $toTitle->getPrefixedText(),
95 'reason' => $params['reason']
98 // NOTE: we assume that if the old title exists, it's because it was re-created as
99 // a redirect to the new title. This is not safe, but what we did before was
100 // even worse: we just determined whether a redirect should have been created,
101 // and reported that it was created if it should have, without any checks.
102 // Also note that isRedirect() is unreliable because of T39209.
103 $r['redirectcreated'] = $fromTitle->exists();
105 $r['moveoverredirect'] = $toTitleExists;
107 // Move the talk page
108 if ( $params['movetalk'] && $toTalk && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
109 $toTalkExists = $toTalk->exists();
110 $status = $this->movePage(
114 !$params['noredirect'],
115 $params['tags'] ?
: []
117 if ( $status->isOK() ) {
118 $r['talkfrom'] = $fromTalk->getPrefixedText();
119 $r['talkto'] = $toTalk->getPrefixedText();
120 $r['talkmoveoverredirect'] = $toTalkExists;
122 // We're not going to dieWithError() on failure, since we already changed something
123 $r['talkmove-errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
127 $result = $this->getResult();
130 if ( $params['movesubpages'] ) {
131 $r['subpages'] = $this->moveSubpages(
135 $params['noredirect'],
136 $params['tags'] ?
: []
138 ApiResult
::setIndexedTagName( $r['subpages'], 'subpage' );
140 if ( $params['movetalk'] ) {
141 $r['subpages-talk'] = $this->moveSubpages(
145 $params['noredirect'],
146 $params['tags'] ?
: []
148 ApiResult
::setIndexedTagName( $r['subpages-talk'], 'subpage' );
152 $watch = 'preferences';
153 if ( isset( $params['watchlist'] ) ) {
154 $watch = $params['watchlist'];
155 } elseif ( $params['watch'] ) {
157 } elseif ( $params['unwatch'] ) {
162 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
163 $this->setWatch( $watch, $toTitle, 'watchmoves' );
165 $result->addValue( null, $this->getModuleName(), $r );
171 * @param string $reason
172 * @param bool $createRedirect
173 * @param array $changeTags Applied to the entry in the move log and redirect page revision
176 protected function movePage( Title
$from, Title
$to, $reason, $createRedirect, $changeTags ) {
177 $mp = new MovePage( $from, $to );
178 $valid = $mp->isValidMove();
179 if ( !$valid->isOK() ) {
183 $user = $this->getUser();
184 $permStatus = $mp->checkPermissions( $user, $reason );
185 if ( !$permStatus->isOK() ) {
189 // Check suppressredirect permission
190 if ( !$user->isAllowed( 'suppressredirect' ) ) {
191 $createRedirect = true;
194 return $mp->move( $user, $reason, $createRedirect, $changeTags );
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
205 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect, $changeTags = [] ) {
208 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect, $changeTags );
209 if ( isset( $success[0] ) ) {
210 $status = $this->errorArrayToStatus( $success );
211 return [ 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ) ];
214 // At least some pages could be moved
215 // Report each of them separately
216 foreach ( $success as $oldTitle => $newTitle ) {
217 $r = [ 'from' => $oldTitle ];
218 if ( is_array( $newTitle ) ) {
219 $status = $this->errorArrayToStatus( $newTitle );
220 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
223 $r['to'] = $newTitle;
231 public function mustBePosted() {
235 public function isWriteMode() {
239 public function getAllowedParams() {
243 ApiBase
::PARAM_TYPE
=> 'integer'
246 ApiBase
::PARAM_TYPE
=> 'string',
247 ApiBase
::PARAM_REQUIRED
=> true
251 'movesubpages' => false,
252 'noredirect' => false,
254 ApiBase
::PARAM_DFLT
=> false,
255 ApiBase
::PARAM_DEPRECATED
=> true,
258 ApiBase
::PARAM_DFLT
=> false,
259 ApiBase
::PARAM_DEPRECATED
=> true,
262 ApiBase
::PARAM_DFLT
=> 'preferences',
263 ApiBase
::PARAM_TYPE
=> [
270 'ignorewarnings' => false,
272 ApiBase
::PARAM_TYPE
=> 'tags',
273 ApiBase
::PARAM_ISMULTI
=> true,
278 public function needsToken() {
282 protected function getExamplesMessages() {
284 'action=move&from=Badtitle&to=Goodtitle&token=123ABC&' .
285 'reason=Misspelled%20title&movetalk=&noredirect='
286 => 'apihelp-move-example-move',
290 public function getHelpUrls() {
291 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Move';