API: Unifying "the ... parameter must be set" messages into one ('missingparam')...
[lhc/web/wiklou.git] / includes / api / ApiMove.php
1 <?php
2
3 /*
4 * Created on Oct 31, 2007
5 * API for MediaWiki 1.8+
6 *
7 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
8 *
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.
13 *
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.
18 *
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
23 */
24
25 if (!defined('MEDIAWIKI')) {
26 // Eclipse helper - will be ignored in production
27 require_once ("ApiBase.php");
28 }
29
30
31 /**
32 * @addtogroup API
33 */
34 class ApiMove extends ApiBase {
35
36 public function __construct($main, $action) {
37 parent :: __construct($main, $action);
38 }
39
40 public function execute() {
41 global $wgUser;
42 $this->getMain()->requestWriteMode();
43 $params = $this->extractRequestParams();
44 if(is_null($params['reason']))
45 $params['reason'] = '';
46
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'));
56
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();
63
64
65 $toTitle = Title::newFromText($params['to']);
66 if(!$toTitle)
67 $this->dieUsageMsg(array('invalidtitle', $params['to']));
68 $toTalk = $toTitle->getTalkPage();
69
70 $dbw = wfGetDB(DB_MASTER);
71 $dbw->begin();
72 $retval = $fromTitle->moveTo($toTitle, true, $params['reason'], !$params['noredirect']);
73 if($retval !== true)
74 $this->dieUsageMsg(array($retval));
75
76 $r = array('from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason']);
77 if(!$params['noredirect'])
78 $r['redirectcreated'] = '';
79
80 if($params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage())
81 {
82 // We need to move the talk page as well
83 $toTalk = $toTitle->getTalkPage();
84 $retval = $fromTalk->moveTo($toTalk, true, $params['reason'], !$params['noredirect']);
85 if($retval === true)
86 {
87 $r['talkfrom'] = $fromTalk->getPrefixedText();
88 $r['talkto'] = $toTalk->getPrefixedText();
89 }
90 // We're not gonna dieUsage() on failure, since we already changed something
91 else
92 {
93 $r['talkmove-error-code'] = ApiBase::$messageMap[$retval]['code'];
94 $r['talkmove-error-info'] = ApiBase::$messageMap[$retval]['info'];
95 }
96 }
97 $dbw->commit(); // Make sure all changes are really written to the DB
98 $this->getResult()->addValue(null, $this->getModuleName(), $r);
99 }
100
101 protected function getAllowedParams() {
102 return array (
103 'from' => null,
104 'to' => null,
105 'token' => null,
106 'reason' => null,
107 'movetalk' => false,
108 'noredirect' => false
109 );
110 }
111
112 protected function getParamDescription() {
113 return array (
114 'from' => 'Title of the page you want to move.',
115 'to' => 'Title you want to rename the page to.',
116 'token' => 'A move token previously retrieved through prop=info',
117 'reason' => 'Reason for the move (optional).',
118 'movetalk' => 'Move the talk page, if it exists.',
119 'noredirect' => 'Don\'t create a redirect'
120 );
121 }
122
123 protected function getDescription() {
124 return array(
125 'Moves a page.'
126 );
127 }
128
129 protected function getExamples() {
130 return array (
131 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk&noredirect'
132 );
133 }
134
135 public function getVersion() {
136 return __CLASS__ . ': $Id$';
137 }
138 }