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 $this->getMain()->requestWriteMode();
43 $params = $this->extractRequestParams();
44 if(is_null($params['reason']))
45 $params['reason'] = '';
47 $this->requireOnlyOneParameter($params, 'from', 'fromid');
48 if(!isset($params['to']))
49 $this->dieUsageMsg(array('missingparam', 'to'));
50 if(!isset($params['token']))
51 $this->dieUsageMsg(array('missingparam', 'token'));
52 if(!$wgUser->matchEditToken($params['token']))
53 $this->dieUsageMsg(array('sessionfailure'));
55 if(isset($params['from']))
57 $fromTitle = Title
::newFromText($params['from']);
59 $this->dieUsageMsg(array('invalidtitle', $params['from']));
61 else if(isset($params['fromid']))
63 $fromTitle = Title
::newFromID($params['fromid']);
65 $this->dieUsageMsg(array('nosuchpageid', $params['fromid']));
67 if(!$fromTitle->exists())
68 $this->dieUsageMsg(array('notanarticle'));
69 $fromTalk = $fromTitle->getTalkPage();
71 $toTitle = Title
::newFromText($params['to']);
73 $this->dieUsageMsg(array('invalidtitle', $params['to']));
74 $toTalk = $toTitle->getTalkPage();
78 $retval = $fromTitle->moveTo($toTitle, true, $params['reason'], !$params['noredirect']);
80 $this->dieUsageMsg(reset($retval));
82 $r = array('from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason']);
83 if(!$params['noredirect'] ||
!$wgUser->isAllowed('suppressredirect'))
84 $r['redirectcreated'] = '';
87 if($params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage())
89 $retval = $fromTalk->moveTo($toTalk, true, $params['reason'], !$params['noredirect']);
92 $r['talkfrom'] = $fromTalk->getPrefixedText();
93 $r['talkto'] = $toTalk->getPrefixedText();
95 // We're not gonna dieUsage() on failure, since we already changed something
98 $parsed = $this->parseMsg(reset($retval));
99 $r['talkmove-error-code'] = $parsed['code'];
100 $r['talkmove-error-info'] = $parsed['info'];
105 if($params['movesubpages'])
107 $r['subpages'] = $this->moveSubpages($fromTitle, $toTitle,
108 $params['reason'], $params['noredirect']);
109 $this->getResult()->setIndexedTagName($r['subpages'], 'subpage');
110 if($params['movetalk'])
112 $r['subpages-talk'] = $this->moveSubpages($fromTalk, $toTalk,
113 $params['reason'], $params['noredirect']);
114 $this->getResult()->setIndexedTagName($r['subpages-talk'], 'subpage');
119 if($params['watch'] ||
$wgUser->getOption('watchmoves'))
121 $wgUser->addWatch($fromTitle);
122 $wgUser->addWatch($toTitle);
124 else if($params['unwatch'])
126 $wgUser->removeWatch($fromTitle);
127 $wgUser->removeWatch($toTitle);
129 $this->getResult()->addValue(null, $this->getModuleName(), $r);
132 public function moveSubpages($fromTitle, $toTitle, $reason, $noredirect)
135 $success = $fromTitle->moveSubpages($toTitle, true, $reason, !$noredirect);
136 if(isset($success[0]))
137 return array('error' => $this->parseMsg($success));
140 // At least some pages could be moved
141 // Report each of them separately
142 foreach($success as $oldTitle => $newTitle)
144 $r = array('from' => $oldTitle);
145 if(is_array($newTitle))
146 $r['error'] = $this->parseMsg(reset($newTitle));
149 $r['to'] = $newTitle;
156 public function mustBePosted() { return true; }
158 public function getAllowedParams() {
162 ApiBase
::PARAM_TYPE
=> 'integer'
168 'movesubpages' => false,
169 'noredirect' => false,
175 public function getParamDescription() {
177 'from' => 'Title of the page you want to move. Cannot be used together with fromid.',
178 'fromid' => 'Page ID of the page you want to move. Cannot be used together with from.',
179 'to' => 'Title you want to rename the page to.',
180 'token' => 'A move token previously retrieved through prop=info',
181 'reason' => 'Reason for the move (optional).',
182 'movetalk' => 'Move the talk page, if it exists.',
183 'movesubpages' => 'Move subpages, if applicable',
184 'noredirect' => 'Don\'t create a redirect',
185 'watch' => 'Add the page and the redirect to your watchlist',
186 'unwatch' => 'Remove the page and the redirect from your watchlist'
190 public function getDescription() {
196 protected function getExamples() {
198 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk&noredirect'
202 public function getVersion() {
203 return __CLASS__
. ': $Id$';