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( array( 'invalidtitle', $params['from'] ) );
46 } elseif ( isset( $params['fromid'] ) ) {
47 $fromTitle = Title
::newFromID( $params['fromid'] );
49 $this->dieUsageMsg( array( '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( array( '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 $toTitleExists = $toTitle->exists();
77 $status = $this->movePage( $fromTitle, $toTitle, $params['reason'], !$params['noredirect'] );
78 if ( !$status->isOK() ) {
79 $this->dieStatus( $status );
83 'from' => $fromTitle->getPrefixedText(),
84 'to' => $toTitle->getPrefixedText(),
85 'reason' => $params['reason']
88 // NOTE: we assume that if the old title exists, it's because it was re-created as
89 // a redirect to the new title. This is not safe, but what we did before was
90 // even worse: we just determined whether a redirect should have been created,
91 // and reported that it was created if it should have, without any checks.
92 // Also note that isRedirect() is unreliable because of bug 37209.
93 $r['redirectcreated'] = $fromTitle->exists();
95 $r['moveoverredirect'] = $toTitleExists;
98 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
99 $toTalkExists = $toTalk->exists();
100 $status = $this->movePage( $fromTalk, $toTalk, $params['reason'], !$params['noredirect'] );
101 if ( $status->isOK() ) {
102 $r['talkfrom'] = $fromTalk->getPrefixedText();
103 $r['talkto'] = $toTalk->getPrefixedText();
104 $r['talkmoveoverredirect'] = $toTalkExists;
106 // We're not gonna dieUsage() on failure, since we already changed something
107 $error = $this->getErrorFromStatus( $status );
108 $r['talkmove-error-code'] = $error[0];
109 $r['talkmove-error-info'] = $error[1];
113 $result = $this->getResult();
116 if ( $params['movesubpages'] ) {
117 $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle,
118 $params['reason'], $params['noredirect'] );
119 ApiResult
::setIndexedTagName( $r['subpages'], 'subpage' );
121 if ( $params['movetalk'] ) {
122 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
123 $params['reason'], $params['noredirect'] );
124 ApiResult
::setIndexedTagName( $r['subpages-talk'], 'subpage' );
128 $watch = 'preferences';
129 if ( isset( $params['watchlist'] ) ) {
130 $watch = $params['watchlist'];
131 } elseif ( $params['watch'] ) {
133 } elseif ( $params['unwatch'] ) {
138 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
139 $this->setWatch( $watch, $toTitle, 'watchmoves' );
141 $result->addValue( null, $this->getModuleName(), $r );
147 * @param string $reason
148 * @param bool $createRedirect
151 protected function movePage( Title
$from, Title
$to, $reason, $createRedirect ) {
152 $mp = new MovePage( $from, $to );
153 $valid = $mp->isValidMove();
154 if ( !$valid->isOK() ) {
158 $permStatus = $mp->checkPermissions( $this->getUser(), $reason );
159 if ( !$permStatus->isOK() ) {
163 // Check suppressredirect permission
164 if ( !$this->getUser()->isAllowed( 'suppressredirect' ) ) {
165 $createRedirect = true;
168 return $mp->move( $this->getUser(), $reason, $createRedirect );
172 * @param Title $fromTitle
173 * @param Title $toTitle
174 * @param string $reason
175 * @param bool $noredirect
178 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect ) {
180 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
181 if ( isset( $success[0] ) ) {
182 return array( 'error' => $this->parseMsg( $success ) );
185 // At least some pages could be moved
186 // Report each of them separately
187 foreach ( $success as $oldTitle => $newTitle ) {
188 $r = array( 'from' => $oldTitle );
189 if ( is_array( $newTitle ) ) {
190 $r['error'] = $this->parseMsg( reset( $newTitle ) );
193 $r['to'] = $newTitle;
201 public function mustBePosted() {
205 public function isWriteMode() {
209 public function getAllowedParams() {
213 ApiBase
::PARAM_TYPE
=> 'integer'
216 ApiBase
::PARAM_TYPE
=> 'string',
217 ApiBase
::PARAM_REQUIRED
=> true
221 'movesubpages' => false,
222 'noredirect' => false,
224 ApiBase
::PARAM_DFLT
=> false,
225 ApiBase
::PARAM_DEPRECATED
=> true,
228 ApiBase
::PARAM_DFLT
=> false,
229 ApiBase
::PARAM_DEPRECATED
=> true,
231 'watchlist' => array(
232 ApiBase
::PARAM_DFLT
=> 'preferences',
233 ApiBase
::PARAM_TYPE
=> array(
240 'ignorewarnings' => false
244 public function needsToken() {
248 protected function getExamplesMessages() {
250 'action=move&from=Badtitle&to=Goodtitle&token=123ABC&' .
251 'reason=Misspelled%20title&movetalk=&noredirect='
252 => 'apihelp-move-example-move',
256 public function getHelpUrls() {
257 return 'https://www.mediawiki.org/wiki/API:Move';