* API: Added apfilterlanglinks parameter to list=allpages, replacing query.php?what...
[mediawiki.git] / includes / api / ApiMove.php
blob0c219d34e4d943f827211f7d0df9f95ff7aec381
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 * @addtogroup 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 $titleObj = NULL;
48 if(!isset($params['from']))
49 $this->dieUsageMsg(array('missingparam', 'from'));
50 if(!isset($params['to']))
51 $this->dieUsageMsg(array('missingparam', 'to'));
52 if(!isset($params['token']))
53 $this->dieUsageMsg(array('missingparam', 'token'));
54 if(!$wgUser->matchEditToken($params['token']))
55 $this->dieUsageMsg(array('sessionfailure'));
57 $fromTitle = Title::newFromText($params['from']);
58 if(!$fromTitle)
59 $this->dieUsageMsg(array('invalidtitle', $params['from']));
60 if(!$fromTitle->exists())
61 $this->dieUsageMsg(array('notanarticle'));
62 $fromTalk = $fromTitle->getTalkPage();
64 $toTitle = Title::newFromText($params['to']);
65 if(!$toTitle)
66 $this->dieUsageMsg(array('invalidtitle', $params['to']));
67 $toTalk = $toTitle->getTalkPage();
69 // Run getUserPermissionsErrors() here so we get message arguments too,
70 // rather than just a message key. The latter is troublesome for messages
71 // that use arguments.
72 // FIXME: moveTo() should really return an array, requires some
73 // refactoring of other code, though (mainly SpecialMovepage.php)
74 $errors = array_merge($fromTitle->getUserPermissionsErrors('move', $wgUser),
75 $fromTitle->getUserPermissionsErrors('edit', $wgUser),
76 $toTitle->getUserPermissionsErrors('move', $wgUser),
77 $toTitle->getUserPermissionsErrors('edit', $wgUser));
78 if(!empty($errors))
79 // We don't care about multiple errors, just report one of them
80 $this->dieUsageMsg(current($errors));
82 $dbw = wfGetDB(DB_MASTER);
83 $dbw->begin();
84 $retval = $fromTitle->moveTo($toTitle, true, $params['reason'], !$params['noredirect']);
85 if($retval !== true)
86 $this->dieUsageMsg(array($retval));
88 $r = array('from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason']);
89 if(!$params['noredirect'])
90 $r['redirectcreated'] = '';
92 if($params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage())
94 // We need to move the talk page as well
95 $toTalk = $toTitle->getTalkPage();
96 $retval = $fromTalk->moveTo($toTalk, true, $params['reason'], !$params['noredirect']);
97 if($retval === true)
99 $r['talkfrom'] = $fromTalk->getPrefixedText();
100 $r['talkto'] = $toTalk->getPrefixedText();
102 // We're not gonna dieUsage() on failure, since we already changed something
103 else
105 $r['talkmove-error-code'] = ApiBase::$messageMap[$retval]['code'];
106 $r['talkmove-error-info'] = ApiBase::$messageMap[$retval]['info'];
109 $dbw->commit(); // Make sure all changes are really written to the DB
110 $this->getResult()->addValue(null, $this->getModuleName(), $r);
113 public function mustBePosted() { return true; }
115 protected function getAllowedParams() {
116 return array (
117 'from' => null,
118 'to' => null,
119 'token' => null,
120 'reason' => null,
121 'movetalk' => false,
122 'noredirect' => false
126 protected function getParamDescription() {
127 return array (
128 'from' => 'Title of the page you want to move.',
129 'to' => 'Title you want to rename the page to.',
130 'token' => 'A move token previously retrieved through prop=info',
131 'reason' => 'Reason for the move (optional).',
132 'movetalk' => 'Move the talk page, if it exists.',
133 'noredirect' => 'Don\'t create a redirect'
137 protected function getDescription() {
138 return array(
139 'Moves a page.'
143 protected function getExamples() {
144 return array (
145 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk&noredirect'
149 public function getVersion() {
150 return __CLASS__ . ': $Id$';