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 $user = $this->getUser();
35 $params = $this->extractRequestParams();
37 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
39 if ( isset( $params['from'] ) ) {
40 $fromTitle = Title
::newFromText( $params['from'] );
41 if ( !$fromTitle ||
$fromTitle->isExternal() ) {
42 $this->dieUsageMsg( array( 'invalidtitle', $params['from'] ) );
44 } elseif ( isset( $params['fromid'] ) ) {
45 $fromTitle = Title
::newFromID( $params['fromid'] );
47 $this->dieUsageMsg( array( 'nosuchpageid', $params['fromid'] ) );
51 if ( !$fromTitle->exists() ) {
52 $this->dieUsageMsg( 'notanarticle' );
54 $fromTalk = $fromTitle->getTalkPage();
56 $toTitle = Title
::newFromText( $params['to'] );
57 if ( !$toTitle ||
$toTitle->isExternal() ) {
58 $this->dieUsageMsg( array( 'invalidtitle', $params['to'] ) );
60 $toTalk = $toTitle->getTalkPage();
62 if ( $toTitle->getNamespace() == NS_FILE
63 && !RepoGroup
::singleton()->getLocalRepo()->findFile( $toTitle )
64 && wfFindFile( $toTitle ) )
66 if ( !$params['ignorewarnings'] && $user->isAllowed( 'reupload-shared' ) ) {
67 $this->dieUsageMsg( 'sharedfile-exists' );
68 } elseif ( !$user->isAllowed( 'reupload-shared' ) ) {
69 $this->dieUsageMsg( 'cantoverwrite-sharedfile' );
74 $toTitleExists = $toTitle->exists();
75 $retval = $fromTitle->moveTo( $toTitle, true, $params['reason'], !$params['noredirect'] );
76 if ( $retval !== true ) {
77 $this->dieUsageMsg( reset( $retval ) );
80 $r = array( 'from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason'] );
82 if ( $fromTitle->exists() ) {
83 //NOTE: we assume that if the old title exists, it's because it was re-created as
84 // a redirect to the new title. This is not safe, but what we did before was
85 // even worse: we just determined whether a redirect should have been created,
86 // and reported that it was created if it should have, without any checks.
87 // Also note that isRedirect() is unreliable because of bug 37209.
88 $r['redirectcreated'] = '';
91 if ( $toTitleExists ) {
92 $r['moveoverredirect'] = '';
96 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
97 $toTalkExists = $toTalk->exists();
98 $retval = $fromTalk->moveTo( $toTalk, true, $params['reason'], !$params['noredirect'] );
99 if ( $retval === true ) {
100 $r['talkfrom'] = $fromTalk->getPrefixedText();
101 $r['talkto'] = $toTalk->getPrefixedText();
102 if ( $toTalkExists ) {
103 $r['talkmoveoverredirect'] = '';
106 // 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'];
113 $result = $this->getResult();
116 if ( $params['movesubpages'] ) {
117 $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle,
118 $params['reason'], $params['noredirect'] );
119 $result->setIndexedTagName( $r['subpages'], 'subpage' );
121 if ( $params['movetalk'] ) {
122 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
123 $params['reason'], $params['noredirect'] );
124 $result->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 );
145 * @param Title $fromTitle
146 * @param Title $toTitle
151 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect ) {
153 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
154 if ( isset( $success[0] ) ) {
155 return array( 'error' => $this->parseMsg( $success ) );
157 // At least some pages could be moved
158 // Report each of them separately
159 foreach ( $success as $oldTitle => $newTitle ) {
160 $r = array( 'from' => $oldTitle );
161 if ( is_array( $newTitle ) ) {
162 $r['error'] = $this->parseMsg( reset( $newTitle ) );
165 $r['to'] = $newTitle;
173 public function mustBePosted() {
177 public function isWriteMode() {
181 public function getAllowedParams() {
185 ApiBase
::PARAM_TYPE
=> 'integer'
188 ApiBase
::PARAM_TYPE
=> 'string',
189 ApiBase
::PARAM_REQUIRED
=> true
192 ApiBase
::PARAM_TYPE
=> 'string',
193 ApiBase
::PARAM_REQUIRED
=> true
197 'movesubpages' => false,
198 'noredirect' => false,
200 ApiBase
::PARAM_DFLT
=> false,
201 ApiBase
::PARAM_DEPRECATED
=> true,
204 ApiBase
::PARAM_DFLT
=> false,
205 ApiBase
::PARAM_DEPRECATED
=> true,
207 'watchlist' => array(
208 ApiBase
::PARAM_DFLT
=> 'preferences',
209 ApiBase
::PARAM_TYPE
=> array(
216 'ignorewarnings' => false
220 public function getParamDescription() {
221 $p = $this->getModulePrefix();
223 'from' => "Title of the page you want to move. Cannot be used together with {$p}fromid",
224 'fromid' => "Page ID of the page you want to move. Cannot be used together with {$p}from",
225 'to' => 'Title you want to rename the page to',
226 'token' => 'A move token previously retrieved through prop=info',
227 'reason' => 'Reason for the move',
228 'movetalk' => 'Move the talk page, if it exists',
229 'movesubpages' => 'Move subpages, if applicable',
230 'noredirect' => 'Don\'t create a redirect',
231 'watch' => 'Add the page and the redirect to your watchlist',
232 'unwatch' => 'Remove the page and the redirect from your watchlist',
233 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
234 'ignorewarnings' => 'Ignore any warnings'
238 public function getResultProperties() {
243 'reason' => 'string',
244 'redirectcreated' => 'boolean',
245 'moveoverredirect' => 'boolean',
247 ApiBase
::PROP_TYPE
=> 'string',
248 ApiBase
::PROP_NULLABLE
=> true
251 ApiBase
::PROP_TYPE
=> 'string',
252 ApiBase
::PROP_NULLABLE
=> true
254 'talkmoveoverredirect' => 'boolean',
255 'talkmove-error-code' => array(
256 ApiBase
::PROP_TYPE
=> 'string',
257 ApiBase
::PROP_NULLABLE
=> true
259 'talkmove-error-info' => array(
260 ApiBase
::PROP_TYPE
=> 'string',
261 ApiBase
::PROP_NULLABLE
=> true
267 public function getDescription() {
268 return 'Move a page';
271 public function getPossibleErrors() {
272 return array_merge( parent
::getPossibleErrors(),
273 $this->getRequireOnlyOneParameterErrorMessages( array( 'from', 'fromid' ) ),
275 array( 'invalidtitle', 'from' ),
276 array( 'nosuchpageid', 'fromid' ),
277 array( 'notanarticle' ),
278 array( 'invalidtitle', 'to' ),
279 array( 'sharedfile-exists' ),
284 public function needsToken() {
288 public function getTokenSalt() {
292 public function getExamples() {
294 'api.php?action=move&from=Badtitle&to=Goodtitle&token=123ABC&reason=Misspelled%20title&movetalk=&noredirect='
298 public function getHelpUrls() {
299 return 'https://www.mediawiki.org/wiki/API:Move';