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' ) );
49 if ( !$wgUser->matchEditToken( $params['token'] ) )
50 $this->dieUsageMsg( array( 'sessionfailure' ) );
52 if ( isset( $params['from'] ) )
54 $fromTitle = Title
::newFromText( $params['from'] );
56 $this->dieUsageMsg( array( 'invalidtitle', $params['from'] ) );
58 else if ( isset( $params['fromid'] ) )
60 $fromTitle = Title
::newFromID( $params['fromid'] );
62 $this->dieUsageMsg( array( 'nosuchpageid', $params['fromid'] ) );
65 if ( !$fromTitle->exists() )
66 $this->dieUsageMsg( array( 'notanarticle' ) );
67 $fromTalk = $fromTitle->getTalkPage();
69 $toTitle = Title
::newFromText( $params['to'] );
71 $this->dieUsageMsg( array( 'invalidtitle', $params['to'] ) );
72 $toTalk = $toTitle->getTalkPage();
74 if ( $toTitle->getNamespace() == NS_FILE
75 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $toTitle )
76 && wfFindFile( $toTitle ) )
78 if ( !$params['ignorewarnings'] && $wgUser->isAllowed( 'reupload-shared' ) ) {
79 $this->dieUsageMsg( array( 'sharedfile-exists' ) );
80 } elseif ( !$wgUser->isAllowed( 'reupload-shared' ) ) {
81 $this->dieUsageMsg( array( 'cantoverwrite-sharedfile' ) );
87 $retval = $fromTitle->moveTo( $toTitle, true, $params['reason'], !$params['noredirect'] );
88 if ( $retval !== true )
89 $this->dieUsageMsg( reset( $retval ) );
91 $r = array( 'from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason'] );
92 if ( !$params['noredirect'] ||
!$wgUser->isAllowed( 'suppressredirect' ) )
93 $r['redirectcreated'] = '';
96 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() )
98 $retval = $fromTalk->moveTo( $toTalk, true, $params['reason'], !$params['noredirect'] );
99 if ( $retval === true )
101 $r['talkfrom'] = $fromTalk->getPrefixedText();
102 $r['talkto'] = $toTalk->getPrefixedText();
104 // We're not gonna dieUsage() on failure, since we already changed something
107 $parsed = $this->parseMsg( reset( $retval ) );
108 $r['talkmove-error-code'] = $parsed['code'];
109 $r['talkmove-error-info'] = $parsed['info'];
114 if ( $params['movesubpages'] )
116 $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle,
117 $params['reason'], $params['noredirect'] );
118 $this->getResult()->setIndexedTagName( $r['subpages'], 'subpage' );
119 if ( $params['movetalk'] )
121 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
122 $params['reason'], $params['noredirect'] );
123 $this->getResult()->setIndexedTagName( $r['subpages-talk'], 'subpage' );
128 if ( $params['watch'] ||
$wgUser->getOption( 'watchmoves' ) )
130 $wgUser->addWatch( $fromTitle );
131 $wgUser->addWatch( $toTitle );
133 else if ( $params['unwatch'] )
135 $wgUser->removeWatch( $fromTitle );
136 $wgUser->removeWatch( $toTitle );
138 $this->getResult()->addValue( null, $this->getModuleName(), $r );
141 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect )
144 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
145 if ( isset( $success[0] ) )
146 return array( 'error' => $this->parseMsg( $success ) );
149 // At least some pages could be moved
150 // Report each of them separately
151 foreach ( $success as $oldTitle => $newTitle )
153 $r = array( 'from' => $oldTitle );
154 if ( is_array( $newTitle ) )
155 $r['error'] = $this->parseMsg( reset( $newTitle ) );
158 $r['to'] = $newTitle;
165 public function mustBePosted() {
169 public function isWriteMode() {
173 public function getAllowedParams() {
177 ApiBase
::PARAM_TYPE
=> 'integer'
183 'movesubpages' => false,
184 'noredirect' => false,
187 'ignorewarnings' => false
191 public function getParamDescription() {
193 'from' => 'Title of the page you want to move. Cannot be used together with fromid.',
194 'fromid' => 'Page ID of the page you want to move. Cannot be used together with from.',
195 'to' => 'Title you want to rename the page to.',
196 'token' => 'A move token previously retrieved through prop=info',
197 'reason' => 'Reason for the move (optional).',
198 'movetalk' => 'Move the talk page, if it exists.',
199 'movesubpages' => 'Move subpages, if applicable',
200 'noredirect' => 'Don\'t create a redirect',
201 'watch' => 'Add the page and the redirect to your watchlist',
202 'unwatch' => 'Remove the page and the redirect from your watchlist',
203 'ignorewarnings' => 'Ignore any warnings'
207 public function getDescription() {
213 public function getPossibleErrors() {
214 return array_merge( parent
::getPossibleErrors(), array(
215 array( 'missingparam', 'to' ),
216 array( 'sessionfailure' ),
217 array( 'invalidtitle', 'from' ),
218 array( 'nosuchpageid', 'fromid' ),
219 array( 'notanarticle' ),
220 array( 'invalidtitle', 'to' ),
221 array( 'sharedfile-exists' ),
225 public function requiresToken() {
229 protected function getExamples() {
231 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk&noredirect'
235 public function getVersion() {
236 return __CLASS__
. ': $Id$';