Refactor redundant attrib dropping into new method
[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 $params = $this->extractRequestParams();
43 if(is_null($params['reason']))
44 $params['reason'] = '';
45
46 $this->requireOnlyOneParameter($params, 'from', 'fromid');
47 if(!isset($params['to']))
48 $this->dieUsageMsg(array('missingparam', 'to'));
49 if(!isset($params['token']))
50 $this->dieUsageMsg(array('missingparam', 'token'));
51 if(!$wgUser->matchEditToken($params['token']))
52 $this->dieUsageMsg(array('sessionfailure'));
53
54 if(isset($params['from']))
55 {
56 $fromTitle = Title::newFromText($params['from']);
57 if(!$fromTitle)
58 $this->dieUsageMsg(array('invalidtitle', $params['from']));
59 }
60 else if(isset($params['fromid']))
61 {
62 $fromTitle = Title::newFromID($params['fromid']);
63 if(!$fromTitle)
64 $this->dieUsageMsg(array('nosuchpageid', $params['fromid']));
65 }
66 if(!$fromTitle->exists())
67 $this->dieUsageMsg(array('notanarticle'));
68 $fromTalk = $fromTitle->getTalkPage();
69
70 $toTitle = Title::newFromText($params['to']);
71 if(!$toTitle)
72 $this->dieUsageMsg(array('invalidtitle', $params['to']));
73 $toTalk = $toTitle->getTalkPage();
74
75 # Move the page
76 $hookErr = null;
77 $retval = $fromTitle->moveTo($toTitle, true, $params['reason'], !$params['noredirect']);
78 if($retval !== true)
79 $this->dieUsageMsg(reset($retval));
80
81 $r = array('from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason']);
82 if(!$params['noredirect'] || !$wgUser->isAllowed('suppressredirect'))
83 $r['redirectcreated'] = '';
84
85 # Move the talk page
86 if($params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage())
87 {
88 $retval = $fromTalk->moveTo($toTalk, true, $params['reason'], !$params['noredirect']);
89 if($retval === true)
90 {
91 $r['talkfrom'] = $fromTalk->getPrefixedText();
92 $r['talkto'] = $toTalk->getPrefixedText();
93 }
94 // We're not gonna dieUsage() on failure, since we already changed something
95 else
96 {
97 $parsed = $this->parseMsg(reset($retval));
98 $r['talkmove-error-code'] = $parsed['code'];
99 $r['talkmove-error-info'] = $parsed['info'];
100 }
101 }
102
103 # Move subpages
104 if($params['movesubpages'])
105 {
106 $r['subpages'] = $this->moveSubpages($fromTitle, $toTitle,
107 $params['reason'], $params['noredirect']);
108 $this->getResult()->setIndexedTagName($r['subpages'], 'subpage');
109 if($params['movetalk'])
110 {
111 $r['subpages-talk'] = $this->moveSubpages($fromTalk, $toTalk,
112 $params['reason'], $params['noredirect']);
113 $this->getResult()->setIndexedTagName($r['subpages-talk'], 'subpage');
114 }
115 }
116
117 # Watch pages
118 if($params['watch'] || $wgUser->getOption('watchmoves'))
119 {
120 $wgUser->addWatch($fromTitle);
121 $wgUser->addWatch($toTitle);
122 }
123 else if($params['unwatch'])
124 {
125 $wgUser->removeWatch($fromTitle);
126 $wgUser->removeWatch($toTitle);
127 }
128 $this->getResult()->addValue(null, $this->getModuleName(), $r);
129 }
130
131 public function moveSubpages($fromTitle, $toTitle, $reason, $noredirect)
132 {
133 $retval = array();
134 $success = $fromTitle->moveSubpages($toTitle, true, $reason, !$noredirect);
135 if(isset($success[0]))
136 return array('error' => $this->parseMsg($success));
137 else
138 {
139 // At least some pages could be moved
140 // Report each of them separately
141 foreach($success as $oldTitle => $newTitle)
142 {
143 $r = array('from' => $oldTitle);
144 if(is_array($newTitle))
145 $r['error'] = $this->parseMsg(reset($newTitle));
146 else
147 // Success
148 $r['to'] = $newTitle;
149 $retval[] = $r;
150 }
151 }
152 return $retval;
153 }
154
155 public function mustBePosted() { return true; }
156
157 public function isWriteMode() {
158 return true;
159 }
160
161 public function getAllowedParams() {
162 return array (
163 'from' => null,
164 'fromid' => array(
165 ApiBase::PARAM_TYPE => 'integer'
166 ),
167 'to' => null,
168 'token' => null,
169 'reason' => null,
170 'movetalk' => false,
171 'movesubpages' => false,
172 'noredirect' => false,
173 'watch' => false,
174 'unwatch' => false
175 );
176 }
177
178 public function getParamDescription() {
179 return array (
180 'from' => 'Title of the page you want to move. Cannot be used together with fromid.',
181 'fromid' => 'Page ID of the page you want to move. Cannot be used together with from.',
182 'to' => 'Title you want to rename the page to.',
183 'token' => 'A move token previously retrieved through prop=info',
184 'reason' => 'Reason for the move (optional).',
185 'movetalk' => 'Move the talk page, if it exists.',
186 'movesubpages' => 'Move subpages, if applicable',
187 'noredirect' => 'Don\'t create a redirect',
188 'watch' => 'Add the page and the redirect to your watchlist',
189 'unwatch' => 'Remove the page and the redirect from your watchlist'
190 );
191 }
192
193 public function getDescription() {
194 return array(
195 'Move a page.'
196 );
197 }
198
199 protected function getExamples() {
200 return array (
201 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk&noredirect'
202 );
203 }
204
205 public function getVersion() {
206 return __CLASS__ . ': $Id$';
207 }
208 }