4 * Created on Oct 31, 2007
5 * API for MediaWiki 1.8+
7 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * http://www.gnu.org/copyleft/gpl.html
25 if ( !defined( 'MEDIAWIKI' ) ) {
26 // Eclipse helper - will be ignored in production
27 require_once ( "ApiBase.php" );
34 class ApiMove extends ApiBase {
36 public function __construct( $main, $action ) {
37 parent :: __construct( $main, $action );
40 public function execute() {
42 $params = $this->extractRequestParams();
43 if ( is_null( $params['reason'] ) )
44 $params['reason'] = '';
46 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
47 if ( !isset( $params['to'] ) )
48 $this->dieUsageMsg( array( 'missingparam', 'to' ) );
50 if ( isset( $params['from'] ) )
52 $fromTitle = Title::newFromText( $params['from'] );
54 $this->dieUsageMsg( array( 'invalidtitle', $params['from'] ) );
56 else if ( isset( $params['fromid'] ) )
58 $fromTitle = Title::newFromID( $params['fromid'] );
60 $this->dieUsageMsg( array( 'nosuchpageid', $params['fromid'] ) );
63 if ( !$fromTitle->exists() )
64 $this->dieUsageMsg( array( 'notanarticle' ) );
65 $fromTalk = $fromTitle->getTalkPage();
67 $toTitle = Title::newFromText( $params['to'] );
69 $this->dieUsageMsg( array( 'invalidtitle', $params['to'] ) );
70 $toTalk = $toTitle->getTalkPage();
72 if ( $toTitle->getNamespace() == NS_FILE
73 && !RepoGroup::singleton()->getLocalRepo()->findFile( $toTitle )
74 && wfFindFile( $toTitle ) )
76 if ( !$params['ignorewarnings'] && $wgUser->isAllowed( 'reupload-shared' ) ) {
77 $this->dieUsageMsg( array( 'sharedfile-exists' ) );
78 } elseif ( !$wgUser->isAllowed( 'reupload-shared' ) ) {
79 $this->dieUsageMsg( array( 'cantoverwrite-sharedfile' ) );
85 $retval = $fromTitle->moveTo( $toTitle, true, $params['reason'], !$params['noredirect'] );
86 if ( $retval !== true )
87 $this->dieUsageMsg( reset( $retval ) );
89 $r = array( 'from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason'] );
90 if ( !$params['noredirect'] || !$wgUser->isAllowed( 'suppressredirect' ) )
91 $r['redirectcreated'] = '';
94 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() )
96 $retval = $fromTalk->moveTo( $toTalk, true, $params['reason'], !$params['noredirect'] );
97 if ( $retval === true )
99 $r['talkfrom'] = $fromTalk->getPrefixedText();
100 $r['talkto'] = $toTalk->getPrefixedText();
102 // We're not gonna dieUsage() on failure, since we already changed something
105 $parsed = $this->parseMsg( reset( $retval ) );
106 $r['talkmove-error-code'] = $parsed['code'];
107 $r['talkmove-error-info'] = $parsed['info'];
112 if ( $params['movesubpages'] )
114 $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle,
115 $params['reason'], $params['noredirect'] );
116 $this->getResult()->setIndexedTagName( $r['subpages'], 'subpage' );
117 if ( $params['movetalk'] )
119 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
120 $params['reason'], $params['noredirect'] );
121 $this->getResult()->setIndexedTagName( $r['subpages-talk'], 'subpage' );
126 if ( $params['watch'] || $wgUser->getOption( 'watchmoves' ) )
128 $wgUser->addWatch( $fromTitle );
129 $wgUser->addWatch( $toTitle );
131 else if ( $params['unwatch'] )
133 $wgUser->removeWatch( $fromTitle );
134 $wgUser->removeWatch( $toTitle );
136 $this->getResult()->addValue( null, $this->getModuleName(), $r );
139 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect )
142 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
143 if ( isset( $success[0] ) )
144 return array( 'error' => $this->parseMsg( $success ) );
147 // At least some pages could be moved
148 // Report each of them separately
149 foreach ( $success as $oldTitle => $newTitle )
151 $r = array( 'from' => $oldTitle );
152 if ( is_array( $newTitle ) )
153 $r['error'] = $this->parseMsg( reset( $newTitle ) );
156 $r['to'] = $newTitle;
163 public function mustBePosted() {
167 public function isWriteMode() {
171 public function getAllowedParams() {
175 ApiBase::PARAM_TYPE => 'integer'
181 'movesubpages' => false,
182 'noredirect' => false,
185 'ignorewarnings' => false
189 public function getParamDescription() {
191 'from' => 'Title of the page you want to move. Cannot be used together with fromid.',
192 'fromid' => 'Page ID of the page you want to move. Cannot be used together with from.',
193 'to' => 'Title you want to rename the page to.',
194 'token' => 'A move token previously retrieved through prop=info',
195 'reason' => 'Reason for the move (optional).',
196 'movetalk' => 'Move the talk page, if it exists.',
197 'movesubpages' => 'Move subpages, if applicable',
198 'noredirect' => 'Don\'t create a redirect',
199 'watch' => 'Add the page and the redirect to your watchlist',
200 'unwatch' => 'Remove the page and the redirect from your watchlist',
201 'ignorewarnings' => 'Ignore any warnings'
205 public function getDescription() {
211 public function getPossibleErrors() {
212 return array_merge( parent::getPossibleErrors(), array(
213 array( 'missingparam', 'to' ),
214 array( 'invalidtitle', 'from' ),
215 array( 'nosuchpageid', 'fromid' ),
216 array( 'notanarticle' ),
217 array( 'invalidtitle', 'to' ),
218 array( 'sharedfile-exists' ),
222 public function needsToken() {
226 public function getTokenSalt() {
230 protected function getExamples() {
232 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk&noredirect'
236 public function getVersion() {
237 return __CLASS__ . ': $Id: ApiMove.php 74217 2010-10-03 15:53:07Z reedy $';