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 __construct( $main, $action ) {
34 parent
::__construct( $main, $action );
37 public function execute() {
38 $user = $this->getUser();
39 $params = $this->extractRequestParams();
40 if ( is_null( $params['reason'] ) ) {
41 $params['reason'] = '';
44 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
46 if ( isset( $params['from'] ) ) {
47 $fromTitle = Title
::newFromText( $params['from'] );
49 $this->dieUsageMsg( array( 'invalidtitle', $params['from'] ) );
51 } elseif ( isset( $params['fromid'] ) ) {
52 $fromTitle = Title
::newFromID( $params['fromid'] );
54 $this->dieUsageMsg( array( 'nosuchpageid', $params['fromid'] ) );
58 if ( !$fromTitle->exists() ) {
59 $this->dieUsageMsg( 'notanarticle' );
61 $fromTalk = $fromTitle->getTalkPage();
63 $toTitle = Title
::newFromText( $params['to'] );
65 $this->dieUsageMsg( array( 'invalidtitle', $params['to'] ) );
67 $toTalk = $toTitle->getTalkPage();
69 if ( $toTitle->getNamespace() == NS_FILE
70 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $toTitle )
71 && wfFindFile( $toTitle ) )
73 if ( !$params['ignorewarnings'] && $user->isAllowed( 'reupload-shared' ) ) {
74 $this->dieUsageMsg( 'sharedfile-exists' );
75 } elseif ( !$user->isAllowed( 'reupload-shared' ) ) {
76 $this->dieUsageMsg( 'cantoverwrite-sharedfile' );
81 $retval = $fromTitle->moveTo( $toTitle, true, $params['reason'], !$params['noredirect'] );
82 if ( $retval !== true ) {
83 $this->dieUsageMsg( reset( $retval ) );
86 $r = array( 'from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason'] );
87 if ( !$params['noredirect'] ||
!$user->isAllowed( 'suppressredirect' ) ) {
88 $r['redirectcreated'] = '';
92 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
93 $retval = $fromTalk->moveTo( $toTalk, true, $params['reason'], !$params['noredirect'] );
94 if ( $retval === true ) {
95 $r['talkfrom'] = $fromTalk->getPrefixedText();
96 $r['talkto'] = $toTalk->getPrefixedText();
98 // We're not gonna dieUsage() on failure, since we already changed something
99 $parsed = $this->parseMsg( reset( $retval ) );
100 $r['talkmove-error-code'] = $parsed['code'];
101 $r['talkmove-error-info'] = $parsed['info'];
105 $result = $this->getResult();
108 if ( $params['movesubpages'] ) {
109 $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle,
110 $params['reason'], $params['noredirect'] );
111 $result->setIndexedTagName( $r['subpages'], 'subpage' );
113 if ( $params['movetalk'] ) {
114 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
115 $params['reason'], $params['noredirect'] );
116 $result->setIndexedTagName( $r['subpages-talk'], 'subpage' );
120 $watch = "preferences";
121 if ( isset( $params['watchlist'] ) ) {
122 $watch = $params['watchlist'];
123 } elseif ( $params['watch'] ) {
125 } elseif ( $params['unwatch'] ) {
130 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
131 $this->setWatch( $watch, $toTitle, 'watchmoves' );
133 $result->addValue( null, $this->getModuleName(), $r );
137 * @param Title $fromTitle
138 * @param Title $toTitle
143 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect ) {
145 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
146 if ( isset( $success[0] ) ) {
147 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 ) {
152 $r = array( 'from' => $oldTitle );
153 if ( is_array( $newTitle ) ) {
154 $r['error'] = $this->parseMsg( reset( $newTitle ) );
157 $r['to'] = $newTitle;
165 public function mustBePosted() {
169 public function isWriteMode() {
173 public function getAllowedParams() {
177 ApiBase
::PARAM_TYPE
=> 'integer'
180 ApiBase
::PARAM_TYPE
=> 'string',
181 ApiBase
::PARAM_REQUIRED
=> true
186 'movesubpages' => false,
187 'noredirect' => false,
189 ApiBase
::PARAM_DFLT
=> false,
190 ApiBase
::PARAM_DEPRECATED
=> true,
193 ApiBase
::PARAM_DFLT
=> false,
194 ApiBase
::PARAM_DEPRECATED
=> true,
196 'watchlist' => array(
197 ApiBase
::PARAM_DFLT
=> 'preferences',
198 ApiBase
::PARAM_TYPE
=> array(
205 'ignorewarnings' => false
209 public function getParamDescription() {
210 $p = $this->getModulePrefix();
212 'from' => "Title of the page you want to move. Cannot be used together with {$p}fromid",
213 'fromid' => "Page ID of the page you want to move. Cannot be used together with {$p}from",
214 'to' => 'Title you want to rename the page to',
215 'token' => 'A move token previously retrieved through prop=info',
216 'reason' => 'Reason for the move (optional)',
217 'movetalk' => 'Move the talk page, if it exists',
218 'movesubpages' => 'Move subpages, if applicable',
219 'noredirect' => 'Don\'t create a redirect',
220 'watch' => 'Add the page and the redirect to your watchlist',
221 'unwatch' => 'Remove the page and the redirect from your watchlist',
222 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
223 'ignorewarnings' => 'Ignore any warnings'
227 public function getDescription() {
228 return 'Move a page';
231 public function getPossibleErrors() {
232 return array_merge( parent
::getPossibleErrors(),
233 $this->getRequireOnlyOneParameterErrorMessages( array( 'from', 'fromid' ) ),
235 array( 'invalidtitle', 'from' ),
236 array( 'nosuchpageid', 'fromid' ),
237 array( 'notanarticle' ),
238 array( 'invalidtitle', 'to' ),
239 array( 'sharedfile-exists' ),
244 public function needsToken() {
248 public function getTokenSalt() {
252 public function getExamples() {
254 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk=&noredirect='
258 public function getHelpUrls() {
259 return 'https://www.mediawiki.org/wiki/API:Move';
262 public function getVersion() {
263 return __CLASS__
. ': $Id$';