Reverted r42528. Links with href="#" make firefox scroll to the top of the page,...
[mediawiki.git] / includes / api / ApiMove.php
blobcba5f91617eab637ead16904e3a00379640e1e66
1 <?php
3 /*
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");
31 /**
32 * @ingroup API
34 class ApiMove extends ApiBase {
36 public function __construct($main, $action) {
37 parent :: __construct($main, $action);
40 public function execute() {
41 global $wgUser;
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']);
58 if(!$fromTitle)
59 $this->dieUsageMsg(array('invalidtitle', $params['from']));
61 else if(isset($params['fromid']))
63 $fromTitle = Title::newFromID($params['fromid']);
64 if(!$fromTitle)
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']);
72 if(!$toTitle)
73 $this->dieUsageMsg(array('invalidtitle', $params['to']));
74 $toTalk = $toTitle->getTalkPage();
76 $hookErr = null;
77 $retval = $fromTitle->moveTo($toTitle, true, $params['reason'], !$params['noredirect']);
78 if($retval !== true)
79 $this->dieUsageMsg(reset($retval));
81 $r = array('from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason']);
82 if(!$params['noredirect'] || !$wgUser->isAllowed('suppressredirect'))
83 $r['redirectcreated'] = '';
85 if($params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage())
87 // We need to move the talk page as well
88 $toTalk = $toTitle->getTalkPage();
89 $retval = $fromTalk->moveTo($toTalk, true, $params['reason'], !$params['noredirect']);
90 if($retval === true)
92 $r['talkfrom'] = $fromTalk->getPrefixedText();
93 $r['talkto'] = $toTalk->getPrefixedText();
95 // We're not gonna dieUsage() on failure, since we already changed something
96 else
98 $r['talkmove-error-code'] = ApiBase::$messageMap[reset($retval)]['code'];
99 $r['talkmove-error-info'] = ApiBase::$messageMap[reset($retval)]['info'];
103 # Watch pages
104 if($params['watch'] || $wgUser->getOption('watchmoves'))
106 $wgUser->addWatch($fromTitle);
107 $wgUser->addWatch($toTitle);
109 else if($params['unwatch'])
111 $wgUser->removeWatch($fromTitle);
112 $wgUser->removeWatch($toTitle);
114 $this->getResult()->addValue(null, $this->getModuleName(), $r);
117 public function mustBePosted() { return true; }
119 public function getAllowedParams() {
120 return array (
121 'from' => null,
122 'fromid' => array(
123 ApiBase::PARAM_TYPE => 'integer'
125 'to' => null,
126 'token' => null,
127 'reason' => null,
128 'movetalk' => false,
129 'noredirect' => false,
130 'watch' => false,
131 'unwatch' => false
135 public function getParamDescription() {
136 return array (
137 'from' => 'Title of the page you want to move. Cannot be used together with fromid.',
138 'fromid' => 'Page ID of the page you want to move. Cannot be used together with from.',
139 'to' => 'Title you want to rename the page to.',
140 'token' => 'A move token previously retrieved through prop=info',
141 'reason' => 'Reason for the move (optional).',
142 'movetalk' => 'Move the talk page, if it exists.',
143 'noredirect' => 'Don\'t create a redirect',
144 'watch' => 'Add the page and the redirect to your watchlist',
145 'unwatch' => 'Remove the page and the redirect from your watchlist'
149 public function getDescription() {
150 return array(
151 'Move a page.'
155 protected function getExamples() {
156 return array (
157 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk&noredirect'
161 public function getVersion() {
162 return __CLASS__ . ': $Id$';