In the spirit of r35407, don't go crazy if Title::moveTo() returns a string (which...
[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 * @ingroup 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 $toTitle = Title::newFromText($params['to']);
65 if(!$toTitle)
66 $this->dieUsageMsg(array('invalidtitle', $params['to']));
67 $toTalk = $toTitle->getTalkPage();
68
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));
81
82 $hookErr = null;
83
84 $retval = $fromTitle->moveTo($toTitle, true, $params['reason'], !$params['noredirect']);
85 if($retval !== true)
86 {
87 # FIXME: Title::moveTo() sometimes returns a string
88 if(is_array($retval))
89 $this->dieUsageMsg(reset($retval));
90 else
91 $this->dieUsageMsg(array('unknownerror', $error));
92 }
93
94 $r = array('from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason']);
95 if(!$params['noredirect'] || !$wgUser->isAllowed('suppressredirect'))
96 $r['redirectcreated'] = '';
97
98 if($params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage())
99 {
100 // We need to move the talk page as well
101 $toTalk = $toTitle->getTalkPage();
102 $retval = $fromTalk->moveTo($toTalk, true, $params['reason'], !$params['noredirect']);
103 if($retval === true)
104 {
105 $r['talkfrom'] = $fromTalk->getPrefixedText();
106 $r['talkto'] = $toTalk->getPrefixedText();
107 }
108 // We're not gonna dieUsage() on failure, since we already changed something
109 else
110 {
111 $r['talkmove-error-code'] = ApiBase::$messageMap[$retval]['code'];
112 $r['talkmove-error-info'] = ApiBase::$messageMap[$retval]['info'];
113 }
114 }
115
116 # Watch pages
117 if($params['watch'] || $wgUser->getOption('watchmoves'))
118 {
119 $wgUser->addWatch($fromTitle);
120 $wgUser->addWatch($toTitle);
121 }
122 else if($params['unwatch'])
123 {
124 $wgUser->removeWatch($fromTitle);
125 $wgUser->removeWatch($toTitle);
126 }
127 $this->getResult()->addValue(null, $this->getModuleName(), $r);
128 }
129
130 public function mustBePosted() { return true; }
131
132 public function getAllowedParams() {
133 return array (
134 'from' => null,
135 'to' => null,
136 'token' => null,
137 'reason' => null,
138 'movetalk' => false,
139 'noredirect' => false,
140 'watch' => false,
141 'unwatch' => false
142 );
143 }
144
145 public function getParamDescription() {
146 return array (
147 'from' => 'Title of the page you want to move.',
148 'to' => 'Title you want to rename the page to.',
149 'token' => 'A move token previously retrieved through prop=info',
150 'reason' => 'Reason for the move (optional).',
151 'movetalk' => 'Move the talk page, if it exists.',
152 'noredirect' => 'Don\'t create a redirect',
153 'watch' => 'Add the page and the redirect to your watchlist',
154 'unwatch' => 'Remove the page and the redirect from your watchlist'
155 );
156 }
157
158 public function getDescription() {
159 return array(
160 'Moves a page.'
161 );
162 }
163
164 protected function getExamples() {
165 return array (
166 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk&noredirect'
167 );
168 }
169
170 public function getVersion() {
171 return __CLASS__ . ': $Id$';
172 }
173 }