merged master (2012-09-11)
[lhc/web/wiklou.git] / includes / api / ApiMove.php
1 <?php
2 /**
3 *
4 *
5 * Created on Oct 31, 2007
6 *
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * API Module to move pages
29 * @ingroup API
30 */
31 class ApiMove extends ApiBase {
32
33 public function __construct( $main, $action ) {
34 parent::__construct( $main, $action );
35 }
36
37 public function execute() {
38 $user = $this->getUser();
39 $params = $this->extractRequestParams();
40
41 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
42
43 if ( isset( $params['from'] ) ) {
44 $fromTitle = Title::newFromText( $params['from'] );
45 if ( !$fromTitle ) {
46 $this->dieUsageMsg( array( 'invalidtitle', $params['from'] ) );
47 }
48 } elseif ( isset( $params['fromid'] ) ) {
49 $fromTitle = Title::newFromID( $params['fromid'] );
50 if ( !$fromTitle ) {
51 $this->dieUsageMsg( array( 'nosuchpageid', $params['fromid'] ) );
52 }
53 }
54
55 if ( !$fromTitle->exists() ) {
56 $this->dieUsageMsg( 'notanarticle' );
57 }
58 $fromTalk = $fromTitle->getTalkPage();
59
60 $toTitle = Title::newFromText( $params['to'] );
61 if ( !$toTitle ) {
62 $this->dieUsageMsg( array( 'invalidtitle', $params['to'] ) );
63 }
64 $toTalk = $toTitle->getTalkPage();
65
66 if ( $toTitle->getNamespace() == NS_FILE
67 && !RepoGroup::singleton()->getLocalRepo()->findFile( $toTitle )
68 && wfFindFile( $toTitle ) )
69 {
70 if ( !$params['ignorewarnings'] && $user->isAllowed( 'reupload-shared' ) ) {
71 $this->dieUsageMsg( 'sharedfile-exists' );
72 } elseif ( !$user->isAllowed( 'reupload-shared' ) ) {
73 $this->dieUsageMsg( 'cantoverwrite-sharedfile' );
74 }
75 }
76
77 // Move the page
78 $toTitleExists = $toTitle->exists();
79 $retval = $fromTitle->moveTo( $toTitle, true, $params['reason'], !$params['noredirect'] );
80 if ( $retval !== true ) {
81 $this->dieUsageMsg( reset( $retval ) );
82 }
83
84 $r = array( 'from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason'] );
85 if ( !$params['noredirect'] || !$user->isAllowed( 'suppressredirect' ) ) {
86 $r['redirectcreated'] = '';
87 }
88 if( $toTitleExists ) {
89 $r['moveoverredirect'] = '';
90 }
91
92 // Move the talk page
93 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
94 $toTalkExists = $toTalk->exists();
95 $retval = $fromTalk->moveTo( $toTalk, true, $params['reason'], !$params['noredirect'] );
96 if ( $retval === true ) {
97 $r['talkfrom'] = $fromTalk->getPrefixedText();
98 $r['talkto'] = $toTalk->getPrefixedText();
99 if( $toTalkExists ) {
100 $r['talkmoveoverredirect'] = '';
101 }
102 } else {
103 // We're not gonna dieUsage() on failure, since we already changed something
104 $parsed = $this->parseMsg( reset( $retval ) );
105 $r['talkmove-error-code'] = $parsed['code'];
106 $r['talkmove-error-info'] = $parsed['info'];
107 }
108 }
109
110 $result = $this->getResult();
111
112 // Move subpages
113 if ( $params['movesubpages'] ) {
114 $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle,
115 $params['reason'], $params['noredirect'] );
116 $result->setIndexedTagName( $r['subpages'], 'subpage' );
117
118 if ( $params['movetalk'] ) {
119 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
120 $params['reason'], $params['noredirect'] );
121 $result->setIndexedTagName( $r['subpages-talk'], 'subpage' );
122 }
123 }
124
125 $watch = "preferences";
126 if ( isset( $params['watchlist'] ) ) {
127 $watch = $params['watchlist'];
128 } elseif ( $params['watch'] ) {
129 $watch = 'watch';
130 } elseif ( $params['unwatch'] ) {
131 $watch = 'unwatch';
132 }
133
134 // Watch pages
135 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
136 $this->setWatch( $watch, $toTitle, 'watchmoves' );
137
138 $result->addValue( null, $this->getModuleName(), $r );
139 }
140
141 /**
142 * @param Title $fromTitle
143 * @param Title $toTitle
144 * @param $reason
145 * @param $noredirect
146 * @return array
147 */
148 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect ) {
149 $retval = array();
150 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
151 if ( isset( $success[0] ) ) {
152 return array( 'error' => $this->parseMsg( $success ) );
153 } else {
154 // At least some pages could be moved
155 // Report each of them separately
156 foreach ( $success as $oldTitle => $newTitle ) {
157 $r = array( 'from' => $oldTitle );
158 if ( is_array( $newTitle ) ) {
159 $r['error'] = $this->parseMsg( reset( $newTitle ) );
160 } else {
161 // Success
162 $r['to'] = $newTitle;
163 }
164 $retval[] = $r;
165 }
166 }
167 return $retval;
168 }
169
170 public function mustBePosted() {
171 return true;
172 }
173
174 public function isWriteMode() {
175 return true;
176 }
177
178 public function getAllowedParams() {
179 return array(
180 'from' => null,
181 'fromid' => array(
182 ApiBase::PARAM_TYPE => 'integer'
183 ),
184 'to' => array(
185 ApiBase::PARAM_TYPE => 'string',
186 ApiBase::PARAM_REQUIRED => true
187 ),
188 'token' => array(
189 ApiBase::PARAM_TYPE => 'string',
190 ApiBase::PARAM_REQUIRED => true
191 ),
192 'reason' => '',
193 'movetalk' => false,
194 'movesubpages' => false,
195 'noredirect' => false,
196 'watch' => array(
197 ApiBase::PARAM_DFLT => false,
198 ApiBase::PARAM_DEPRECATED => true,
199 ),
200 'unwatch' => array(
201 ApiBase::PARAM_DFLT => false,
202 ApiBase::PARAM_DEPRECATED => true,
203 ),
204 'watchlist' => array(
205 ApiBase::PARAM_DFLT => 'preferences',
206 ApiBase::PARAM_TYPE => array(
207 'watch',
208 'unwatch',
209 'preferences',
210 'nochange'
211 ),
212 ),
213 'ignorewarnings' => false
214 );
215 }
216
217 public function getParamDescription() {
218 $p = $this->getModulePrefix();
219 return array(
220 'from' => "Title of the page you want to move. Cannot be used together with {$p}fromid",
221 'fromid' => "Page ID of the page you want to move. Cannot be used together with {$p}from",
222 'to' => 'Title you want to rename the page to',
223 'token' => 'A move token previously retrieved through prop=info',
224 'reason' => 'Reason for the move',
225 'movetalk' => 'Move the talk page, if it exists',
226 'movesubpages' => 'Move subpages, if applicable',
227 'noredirect' => 'Don\'t create a redirect',
228 'watch' => 'Add the page and the redirect to your watchlist',
229 'unwatch' => 'Remove the page and the redirect from your watchlist',
230 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
231 'ignorewarnings' => 'Ignore any warnings'
232 );
233 }
234
235 public function getResultProperties() {
236 return array(
237 '' => array(
238 'from' => 'string',
239 'to' => 'string',
240 'reason' => 'string',
241 'redirectcreated' => 'boolean',
242 'moveoverredirect' => 'boolean',
243 'talkfrom' => array(
244 ApiBase::PROP_TYPE => 'string',
245 ApiBase::PROP_NULLABLE => true
246 ),
247 'talkto' => array(
248 ApiBase::PROP_TYPE => 'string',
249 ApiBase::PROP_NULLABLE => true
250 ),
251 'talkmoveoverredirect' => 'boolean',
252 'talkmove-error-code' => array(
253 ApiBase::PROP_TYPE => 'string',
254 ApiBase::PROP_NULLABLE => true
255 ),
256 'talkmove-error-info' => array(
257 ApiBase::PROP_TYPE => 'string',
258 ApiBase::PROP_NULLABLE => true
259 )
260 )
261 );
262 }
263
264 public function getDescription() {
265 return 'Move a page';
266 }
267
268 public function getPossibleErrors() {
269 return array_merge( parent::getPossibleErrors(),
270 $this->getRequireOnlyOneParameterErrorMessages( array( 'from', 'fromid' ) ),
271 array(
272 array( 'invalidtitle', 'from' ),
273 array( 'nosuchpageid', 'fromid' ),
274 array( 'notanarticle' ),
275 array( 'invalidtitle', 'to' ),
276 array( 'sharedfile-exists' ),
277 )
278 );
279 }
280
281 public function needsToken() {
282 return true;
283 }
284
285 public function getTokenSalt() {
286 return '';
287 }
288
289 public function getExamples() {
290 return array(
291 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk=&noredirect='
292 );
293 }
294
295 public function getHelpUrls() {
296 return 'https://www.mediawiki.org/wiki/API:Move';
297 }
298
299 public function getVersion() {
300 return __CLASS__ . ': $Id$';
301 }
302 }