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