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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
33 * API Module to move pages
36 class ApiMove
extends ApiBase
{
38 public function __construct( $main, $action ) {
39 parent
::__construct( $main, $action );
42 public function execute() {
44 $params = $this->extractRequestParams();
45 if ( is_null( $params['reason'] ) ) {
46 $params['reason'] = '';
49 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
51 if ( isset( $params['from'] ) ) {
52 $fromTitle = Title
::newFromText( $params['from'] );
54 $this->dieUsageMsg( array( 'invalidtitle', $params['from'] ) );
56 } elseif ( isset( $params['fromid'] ) ) {
57 $fromTitle = Title
::newFromID( $params['fromid'] );
59 $this->dieUsageMsg( array( 'nosuchpageid', $params['fromid'] ) );
63 if ( !$fromTitle->exists() ) {
64 $this->dieUsageMsg( 'notanarticle' );
66 $fromTalk = $fromTitle->getTalkPage();
68 $toTitle = Title
::newFromText( $params['to'] );
70 $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( 'sharedfile-exists' );
80 } elseif ( !$wgUser->isAllowed( 'reupload-shared' ) ) {
81 $this->dieUsageMsg( 'cantoverwrite-sharedfile' );
86 $retval = $fromTitle->moveTo( $toTitle, true, $params['reason'], !$params['noredirect'] );
87 if ( $retval !== true ) {
88 $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'] = '';
97 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
98 $retval = $fromTalk->moveTo( $toTalk, true, $params['reason'], !$params['noredirect'] );
99 if ( $retval === true ) {
100 $r['talkfrom'] = $fromTalk->getPrefixedText();
101 $r['talkto'] = $toTalk->getPrefixedText();
103 // We're not gonna dieUsage() on failure, since we already changed something
104 $parsed = $this->parseMsg( reset( $retval ) );
105 $r['talkmove-error-code'] = $parsed['code'];
106 $r['talkmove-error-info'] = $parsed['info'];
110 $result = $this->getResult();
113 if ( $params['movesubpages'] ) {
114 $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle,
115 $params['reason'], $params['noredirect'] );
116 $result->setIndexedTagName( $r['subpages'], 'subpage' );
118 if ( $params['movetalk'] ) {
119 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
120 $params['reason'], $params['noredirect'] );
121 $result->setIndexedTagName( $r['subpages-talk'], 'subpage' );
125 $watch = "preferences";
126 if ( isset( $params['watchlist'] ) ) {
127 $watch = $params['watchlist'];
128 } elseif ( $params['watch'] ) {
130 } elseif ( $params['unwatch'] ) {
135 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
136 $this->setWatch( $watch, $toTitle, 'watchmoves' );
138 $result->addValue( null, $this->getModuleName(), $r );
142 * @param Title $fromTitle
143 * @param Title $toTitle
148 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect ) {
150 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
151 if ( isset( $success[0] ) ) {
152 return array( 'error' => $this->parseMsg( $success ) );
154 // At least some pages could be moved
155 // Report each of them separately
156 foreach ( $success as $oldTitle => $newTitle ) {
157 $r = array( 'from' => $oldTitle );
158 if ( is_array( $newTitle ) ) {
159 $r['error'] = $this->parseMsg( reset( $newTitle ) );
162 $r['to'] = $newTitle;
170 public function mustBePosted() {
174 public function isWriteMode() {
178 public function getAllowedParams() {
182 ApiBase
::PARAM_TYPE
=> 'integer'
185 ApiBase
::PARAM_TYPE
=> 'string',
186 ApiBase
::PARAM_REQUIRED
=> true
191 'movesubpages' => false,
192 'noredirect' => false,
194 ApiBase
::PARAM_DFLT
=> false,
195 ApiBase
::PARAM_DEPRECATED
=> true,
198 ApiBase
::PARAM_DFLT
=> false,
199 ApiBase
::PARAM_DEPRECATED
=> true,
201 'watchlist' => array(
202 ApiBase
::PARAM_DFLT
=> 'preferences',
203 ApiBase
::PARAM_TYPE
=> array(
210 'ignorewarnings' => false
214 public function getParamDescription() {
215 $p = $this->getModulePrefix();
217 'from' => "Title of the page you want to move. Cannot be used together with {$p}fromid",
218 'fromid' => "Page ID of the page you want to move. Cannot be used together with {$p}from",
219 'to' => 'Title you want to rename the page to',
220 'token' => 'A move token previously retrieved through prop=info',
221 'reason' => 'Reason for the move (optional)',
222 'movetalk' => 'Move the talk page, if it exists',
223 'movesubpages' => 'Move subpages, if applicable',
224 'noredirect' => 'Don\'t create a redirect',
225 'watch' => 'Add the page and the redirect to your watchlist',
226 'unwatch' => 'Remove the page and the redirect from your watchlist',
227 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
228 'ignorewarnings' => 'Ignore any warnings'
232 public function getDescription() {
233 return 'Move a page';
236 public function getPossibleErrors() {
237 return array_merge( parent
::getPossibleErrors(),
238 $this->getRequireOnlyOneParameterErrorMessages( array( 'from', 'fromid' ) ),
240 array( 'invalidtitle', 'from' ),
241 array( 'nosuchpageid', 'fromid' ),
242 array( 'notanarticle' ),
243 array( 'invalidtitle', 'to' ),
244 array( 'sharedfile-exists' ),
249 public function needsToken() {
253 public function getTokenSalt() {
257 protected function getExamples() {
259 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk=&noredirect='
263 public function getHelpUrls() {
264 return 'http://www.mediawiki.org/wiki/API:Move';
267 public function getVersion() {
268 return __CLASS__
. ': $Id$';