API:
[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->dieUsage('The from parameter must be set', 'nofrom');
50 if(!isset($params['to']))
51 $this->dieUsage('The to parameter must be set', 'noto');
52 if(!isset($params['token']))
53 $this->dieUsage('The token parameter must be set', 'notoken');
54 if(!$wgUser->matchEditToken($params['token']))
55 $this->dieUsage('Invalid token', 'badtoken');
56
57 if($wgUser->isBlocked())
58 $this->dieUsage('You have been blocked from editing', 'blocked');
59 if(wfReadOnly())
60 $this->dieUsage('The wiki is in read-only mode', 'readonly');
61 if($params['noredirect'] && !$wgUser->isAllowed('suppressredirect'))
62 $this->dieUsage("You don't have permission to suppress redirect creation", 'nosuppress');
63
64 $fromTitle = Title::newFromText($params['from']);
65 if(!$fromTitle)
66 $this->dieUsage("Bad title ``{$params['from']}''", 'invalidtitle');
67 if(!$fromTitle->exists())
68 $this->dieUsage("``{$params['from']}'' doesn't exist", 'missingtitle');
69 $fromTalk = $fromTitle->getTalkPage();
70
71
72 $toTitle = Title::newFromText($params['to']);
73 if(!$toTitle)
74 $this->dieUsage("Bad title ``{$params['to']}''", 'invalidtitle');
75 $toTalk = $toTitle->getTalkPage();
76
77 $dbw = wfGetDB(DB_MASTER);
78 $dbw->begin();
79 $retval = $fromTitle->moveTo($toTitle, true, $params['reason'], !$params['noredirect']);
80 if($retval !== true)
81 switch($retval)
82 {
83 // case 'badtitletext': Can't happen
84 // case 'badarticleerror': Can't happen
85 case 'selfmove':
86 $this->dieUsage("Can't move ``{$params['from']}'' to itself", 'selfmove');
87 case 'immobile_namespace':
88 if($fromTitle->isMovable())
89 $this->dieUsage("Pages in the ``{$fromTitle->getNsText()}'' namespace can't be moved", 'immobilenamespace-from');
90 $this->dieUsage("Pages in the ``{$toTitle->getNsText()}'' namespace can't be moved", 'immobilenamespace-to');
91 case 'articleexists':
92 $this->dieUsage("``{$toTitle->getPrefixedText()}'' already exists and is not a redirect to ``{$fromTitle->getPrefixedText()}''", 'targetexists');
93 case 'protectedpage':
94 $this->dieUsage("You don't have permission to move ``{$fromTitle->getPrefixedText()}'' to ``{$toTitle->getPrefixedText()}''", 'permissiondenied');
95 default:
96 throw new MWException( "Title::moveTo: Unknown return value ``{$retval}''" );
97 }
98 $r = array('from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason']);
99 if(!$params['noredirect'])
100 $r['redirectcreated'] = '';
101
102 if($params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage())
103 {
104 // We need to move the talk page as well
105 $toTalk = $toTitle->getTalkPage();
106 $retval = $fromTalk->moveTo($toTalk, true, $params['reason'], !$params['noredirect']);
107 if($retval === true)
108 {
109 $r['talkfrom'] = $fromTalk->getPrefixedText();
110 $r['talkto'] = $toTalk->getPrefixedText();
111 }
112 // We're not gonna dieUsage() on failure, since we already changed something
113 else
114 switch($retval)
115 {
116 case 'immobile_namespace':
117 if($fromTalk->isMovable())
118 {
119 $r['talkmove-error-code'] = 'immobilenamespace-from';
120 $r['talkmove-error-info'] = "Pages in the ``{$fromTalk->getNsText()}'' namespace can't be moved";
121 }
122 else
123 {
124 $r['talkmove-error-code'] = 'immobilenamespace-to';
125 $r['talkmove-error-info'] = "Pages in the ``{$toTalk->getNsText()}'' namespace can't be moved";
126 }
127 break;
128 case 'articleexists':
129 $r['talkmove-error-code'] = 'targetexists';
130 $r['talkmove-error-info'] = "``{$toTalk->getPrefixedText()}'' already exists and is not a redirect to ``{$fromTalk->getPrefixedText()}''";
131 break;
132 case 'protectedpage':
133 $r['talkmove-error-code'] = 'permissiondenied';
134 $r['talkmove-error-info'] = "You don't have permission to move ``{$fromTalk->getPrefixedText()}'' to ``{$toTalk->getPrefixedText()}''";
135 default:
136 $r['talkmove-error-code'] = 'unknownerror';
137 $r['talkmove-error-info'] = "Unknown error ``$retval''";
138 }
139 }
140 $dbw->commit(); // Make sure all changes are really written to the DB
141 $this->getResult()->addValue(null, $this->getModuleName(), $r);
142 }
143
144 public function isEditMode() {
145 return true;
146 }
147
148 protected function getAllowedParams() {
149 return array (
150 'from' => null,
151 'to' => null,
152 'token' => null,
153 'reason' => null,
154 'movetalk' => false,
155 'noredirect' => false
156 );
157 }
158
159 protected function getParamDescription() {
160 return array (
161 'from' => 'Title of the page you want to move.',
162 'to' => 'Title you want to rename the page to.',
163 'token' => 'A move token previously retrieved through prop=info',
164 'reason' => 'Reason for the move (optional).',
165 'movetalk' => 'Move the talk page, if it exists.',
166 'noredirect' => 'Don\'t create a redirect'
167 );
168 }
169
170 protected function getDescription() {
171 return array(
172 'Moves a page.'
173 );
174 }
175
176 protected function getExamples() {
177 return array (
178 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk&noredirect'
179 );
180 }
181
182 public function getVersion() {
183 return __CLASS__ . ': $Id$';
184 }
185 }
186