5 * Created on Oct 31, 2007
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
28 * API Module to move pages
31 class ApiMove
extends ApiBase
{
33 public function execute() {
34 $this->useTransactionalTimeLimit();
36 $user = $this->getUser();
37 $params = $this->extractRequestParams();
39 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
41 if ( isset( $params['from'] ) ) {
42 $fromTitle = Title
::newFromText( $params['from'] );
43 if ( !$fromTitle ||
$fromTitle->isExternal() ) {
44 $this->dieUsageMsg( [ 'invalidtitle', $params['from'] ] );
46 } elseif ( isset( $params['fromid'] ) ) {
47 $fromTitle = Title
::newFromID( $params['fromid'] );
49 $this->dieUsageMsg( [ 'nosuchpageid', $params['fromid'] ] );
53 if ( !$fromTitle->exists() ) {
54 $this->dieUsageMsg( 'notanarticle' );
56 $fromTalk = $fromTitle->getTalkPage();
58 $toTitle = Title
::newFromText( $params['to'] );
59 if ( !$toTitle ||
$toTitle->isExternal() ) {
60 $this->dieUsageMsg( [ 'invalidtitle', $params['to'] ] );
62 $toTalk = $toTitle->getTalkPage();
64 if ( $toTitle->getNamespace() == NS_FILE
65 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $toTitle )
66 && wfFindFile( $toTitle )
68 if ( !$params['ignorewarnings'] && $user->isAllowed( 'reupload-shared' ) ) {
69 $this->dieUsageMsg( 'sharedfile-exists' );
70 } elseif ( !$user->isAllowed( 'reupload-shared' ) ) {
71 $this->dieUsageMsg( 'cantoverwrite-sharedfile' );
76 if ( $user->pingLimiter( 'move' ) ) {
77 $this->dieUsageMsg( 'actionthrottledtext' );
81 $toTitleExists = $toTitle->exists();
82 $status = $this->movePage( $fromTitle, $toTitle, $params['reason'], !$params['noredirect'] );
83 if ( !$status->isOK() ) {
84 $this->dieStatus( $status );
88 'from' => $fromTitle->getPrefixedText(),
89 'to' => $toTitle->getPrefixedText(),
90 'reason' => $params['reason']
93 // NOTE: we assume that if the old title exists, it's because it was re-created as
94 // a redirect to the new title. This is not safe, but what we did before was
95 // even worse: we just determined whether a redirect should have been created,
96 // and reported that it was created if it should have, without any checks.
97 // Also note that isRedirect() is unreliable because of bug 37209.
98 $r['redirectcreated'] = $fromTitle->exists();
100 $r['moveoverredirect'] = $toTitleExists;
102 // Move the talk page
103 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
104 $toTalkExists = $toTalk->exists();
105 $status = $this->movePage( $fromTalk, $toTalk, $params['reason'], !$params['noredirect'] );
106 if ( $status->isOK() ) {
107 $r['talkfrom'] = $fromTalk->getPrefixedText();
108 $r['talkto'] = $toTalk->getPrefixedText();
109 $r['talkmoveoverredirect'] = $toTalkExists;
111 // We're not gonna dieUsage() on failure, since we already changed something
112 $error = $this->getErrorFromStatus( $status );
113 $r['talkmove-error-code'] = $error[0];
114 $r['talkmove-error-info'] = $error[1];
118 $result = $this->getResult();
121 if ( $params['movesubpages'] ) {
122 $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle,
123 $params['reason'], $params['noredirect'] );
124 ApiResult
::setIndexedTagName( $r['subpages'], 'subpage' );
126 if ( $params['movetalk'] ) {
127 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
128 $params['reason'], $params['noredirect'] );
129 ApiResult
::setIndexedTagName( $r['subpages-talk'], 'subpage' );
133 $watch = 'preferences';
134 if ( isset( $params['watchlist'] ) ) {
135 $watch = $params['watchlist'];
136 } elseif ( $params['watch'] ) {
138 } elseif ( $params['unwatch'] ) {
143 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
144 $this->setWatch( $watch, $toTitle, 'watchmoves' );
146 $result->addValue( null, $this->getModuleName(), $r );
152 * @param string $reason
153 * @param bool $createRedirect
156 protected function movePage( Title
$from, Title
$to, $reason, $createRedirect ) {
157 $mp = new MovePage( $from, $to );
158 $valid = $mp->isValidMove();
159 if ( !$valid->isOK() ) {
163 $permStatus = $mp->checkPermissions( $this->getUser(), $reason );
164 if ( !$permStatus->isOK() ) {
168 // Check suppressredirect permission
169 if ( !$this->getUser()->isAllowed( 'suppressredirect' ) ) {
170 $createRedirect = true;
173 return $mp->move( $this->getUser(), $reason, $createRedirect );
177 * @param Title $fromTitle
178 * @param Title $toTitle
179 * @param string $reason
180 * @param bool $noredirect
183 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect ) {
185 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
186 if ( isset( $success[0] ) ) {
187 return [ 'error' => $this->parseMsg( $success ) ];
190 // At least some pages could be moved
191 // Report each of them separately
192 foreach ( $success as $oldTitle => $newTitle ) {
193 $r = [ 'from' => $oldTitle ];
194 if ( is_array( $newTitle ) ) {
195 $r['error'] = $this->parseMsg( reset( $newTitle ) );
198 $r['to'] = $newTitle;
206 public function mustBePosted() {
210 public function isWriteMode() {
214 public function getAllowedParams() {
218 ApiBase
::PARAM_TYPE
=> 'integer'
221 ApiBase
::PARAM_TYPE
=> 'string',
222 ApiBase
::PARAM_REQUIRED
=> true
226 'movesubpages' => false,
227 'noredirect' => false,
229 ApiBase
::PARAM_DFLT
=> false,
230 ApiBase
::PARAM_DEPRECATED
=> true,
233 ApiBase
::PARAM_DFLT
=> false,
234 ApiBase
::PARAM_DEPRECATED
=> true,
237 ApiBase
::PARAM_DFLT
=> 'preferences',
238 ApiBase
::PARAM_TYPE
=> [
245 'ignorewarnings' => false
249 public function needsToken() {
253 protected function getExamplesMessages() {
255 'action=move&from=Badtitle&to=Goodtitle&token=123ABC&' .
256 'reason=Misspelled%20title&movetalk=&noredirect='
257 => 'apihelp-move-example-move',
261 public function getHelpUrls() {
262 return 'https://www.mediawiki.org/wiki/API:Move';