Merge "allow combined width/height param in {{filepath:}}"
[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 $retval = $fromTitle->moveTo( $toTitle, true, $params['reason'], !$params['noredirect'] );
79 if ( $retval !== true ) {
80 $this->dieUsageMsg( reset( $retval ) );
81 }
82
83 $r = array( 'from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason'] );
84 if ( !$params['noredirect'] || !$user->isAllowed( 'suppressredirect' ) ) {
85 $r['redirectcreated'] = '';
86 }
87
88 // Move the talk page
89 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
90 $retval = $fromTalk->moveTo( $toTalk, true, $params['reason'], !$params['noredirect'] );
91 if ( $retval === true ) {
92 $r['talkfrom'] = $fromTalk->getPrefixedText();
93 $r['talkto'] = $toTalk->getPrefixedText();
94 } else {
95 // We're not gonna dieUsage() on failure, since we already changed something
96 $parsed = $this->parseMsg( reset( $retval ) );
97 $r['talkmove-error-code'] = $parsed['code'];
98 $r['talkmove-error-info'] = $parsed['info'];
99 }
100 }
101
102 $result = $this->getResult();
103
104 // Move subpages
105 if ( $params['movesubpages'] ) {
106 $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle,
107 $params['reason'], $params['noredirect'] );
108 $result->setIndexedTagName( $r['subpages'], 'subpage' );
109
110 if ( $params['movetalk'] ) {
111 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
112 $params['reason'], $params['noredirect'] );
113 $result->setIndexedTagName( $r['subpages-talk'], 'subpage' );
114 }
115 }
116
117 $watch = "preferences";
118 if ( isset( $params['watchlist'] ) ) {
119 $watch = $params['watchlist'];
120 } elseif ( $params['watch'] ) {
121 $watch = 'watch';
122 } elseif ( $params['unwatch'] ) {
123 $watch = 'unwatch';
124 }
125
126 // Watch pages
127 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
128 $this->setWatch( $watch, $toTitle, 'watchmoves' );
129
130 $result->addValue( null, $this->getModuleName(), $r );
131 }
132
133 /**
134 * @param Title $fromTitle
135 * @param Title $toTitle
136 * @param $reason
137 * @param $noredirect
138 * @return array
139 */
140 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect ) {
141 $retval = array();
142 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
143 if ( isset( $success[0] ) ) {
144 return array( 'error' => $this->parseMsg( $success ) );
145 } else {
146 // At least some pages could be moved
147 // Report each of them separately
148 foreach ( $success as $oldTitle => $newTitle ) {
149 $r = array( 'from' => $oldTitle );
150 if ( is_array( $newTitle ) ) {
151 $r['error'] = $this->parseMsg( reset( $newTitle ) );
152 } else {
153 // Success
154 $r['to'] = $newTitle;
155 }
156 $retval[] = $r;
157 }
158 }
159 return $retval;
160 }
161
162 public function mustBePosted() {
163 return true;
164 }
165
166 public function isWriteMode() {
167 return true;
168 }
169
170 public function getAllowedParams() {
171 return array(
172 'from' => null,
173 'fromid' => array(
174 ApiBase::PARAM_TYPE => 'integer'
175 ),
176 'to' => array(
177 ApiBase::PARAM_TYPE => 'string',
178 ApiBase::PARAM_REQUIRED => true
179 ),
180 'token' => array(
181 ApiBase::PARAM_TYPE => 'string',
182 ApiBase::PARAM_REQUIRED => true
183 ),
184 'reason' => '',
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',
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 }