bbf408911f47c6ae08434f52720ae84b8a838f9f
[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
86 if ( $fromTitle->exists() ) {
87 //NOTE: we assume that if the old title exists, it's because it was re-created as
88 // a redirect to the new title. This is not safe, but what we did before was
89 // even worse: we just determined whether a redirect should have been created,
90 // and reported that it was created if it should have, without any checks.
91 // Also note that isRedirect() is unreliable because of bug 37209.
92 $r['redirectcreated'] = '';
93 }
94
95 if( $toTitleExists ) {
96 $r['moveoverredirect'] = '';
97 }
98
99 // Move the talk page
100 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
101 $toTalkExists = $toTalk->exists();
102 $retval = $fromTalk->moveTo( $toTalk, true, $params['reason'], !$params['noredirect'] );
103 if ( $retval === true ) {
104 $r['talkfrom'] = $fromTalk->getPrefixedText();
105 $r['talkto'] = $toTalk->getPrefixedText();
106 if( $toTalkExists ) {
107 $r['talkmoveoverredirect'] = '';
108 }
109 } else {
110 // We're not gonna dieUsage() on failure, since we already changed something
111 $parsed = $this->parseMsg( reset( $retval ) );
112 $r['talkmove-error-code'] = $parsed['code'];
113 $r['talkmove-error-info'] = $parsed['info'];
114 }
115 }
116
117 $result = $this->getResult();
118
119 // Move subpages
120 if ( $params['movesubpages'] ) {
121 $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle,
122 $params['reason'], $params['noredirect'] );
123 $result->setIndexedTagName( $r['subpages'], 'subpage' );
124
125 if ( $params['movetalk'] ) {
126 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
127 $params['reason'], $params['noredirect'] );
128 $result->setIndexedTagName( $r['subpages-talk'], 'subpage' );
129 }
130 }
131
132 $watch = 'preferences';
133 if ( isset( $params['watchlist'] ) ) {
134 $watch = $params['watchlist'];
135 } elseif ( $params['watch'] ) {
136 $watch = 'watch';
137 } elseif ( $params['unwatch'] ) {
138 $watch = 'unwatch';
139 }
140
141 // Watch pages
142 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
143 $this->setWatch( $watch, $toTitle, 'watchmoves' );
144
145 $result->addValue( null, $this->getModuleName(), $r );
146 }
147
148 /**
149 * @param Title $fromTitle
150 * @param Title $toTitle
151 * @param $reason
152 * @param $noredirect
153 * @return array
154 */
155 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect ) {
156 $retval = array();
157 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
158 if ( isset( $success[0] ) ) {
159 return array( 'error' => $this->parseMsg( $success ) );
160 } else {
161 // At least some pages could be moved
162 // Report each of them separately
163 foreach ( $success as $oldTitle => $newTitle ) {
164 $r = array( 'from' => $oldTitle );
165 if ( is_array( $newTitle ) ) {
166 $r['error'] = $this->parseMsg( reset( $newTitle ) );
167 } else {
168 // Success
169 $r['to'] = $newTitle;
170 }
171 $retval[] = $r;
172 }
173 }
174 return $retval;
175 }
176
177 public function mustBePosted() {
178 return true;
179 }
180
181 public function isWriteMode() {
182 return true;
183 }
184
185 public function getAllowedParams() {
186 return array(
187 'from' => null,
188 'fromid' => array(
189 ApiBase::PARAM_TYPE => 'integer'
190 ),
191 'to' => array(
192 ApiBase::PARAM_TYPE => 'string',
193 ApiBase::PARAM_REQUIRED => true
194 ),
195 'token' => array(
196 ApiBase::PARAM_TYPE => 'string',
197 ApiBase::PARAM_REQUIRED => true
198 ),
199 'reason' => '',
200 'movetalk' => false,
201 'movesubpages' => false,
202 'noredirect' => false,
203 'watch' => array(
204 ApiBase::PARAM_DFLT => false,
205 ApiBase::PARAM_DEPRECATED => true,
206 ),
207 'unwatch' => array(
208 ApiBase::PARAM_DFLT => false,
209 ApiBase::PARAM_DEPRECATED => true,
210 ),
211 'watchlist' => array(
212 ApiBase::PARAM_DFLT => 'preferences',
213 ApiBase::PARAM_TYPE => array(
214 'watch',
215 'unwatch',
216 'preferences',
217 'nochange'
218 ),
219 ),
220 'ignorewarnings' => false
221 );
222 }
223
224 public function getParamDescription() {
225 $p = $this->getModulePrefix();
226 return array(
227 'from' => "Title of the page you want to move. Cannot be used together with {$p}fromid",
228 'fromid' => "Page ID of the page you want to move. Cannot be used together with {$p}from",
229 'to' => 'Title you want to rename the page to',
230 'token' => 'A move token previously retrieved through prop=info',
231 'reason' => 'Reason for the move',
232 'movetalk' => 'Move the talk page, if it exists',
233 'movesubpages' => 'Move subpages, if applicable',
234 'noredirect' => 'Don\'t create a redirect',
235 'watch' => 'Add the page and the redirect to your watchlist',
236 'unwatch' => 'Remove the page and the redirect from your watchlist',
237 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
238 'ignorewarnings' => 'Ignore any warnings'
239 );
240 }
241
242 public function getResultProperties() {
243 return array(
244 '' => array(
245 'from' => 'string',
246 'to' => 'string',
247 'reason' => 'string',
248 'redirectcreated' => 'boolean',
249 'moveoverredirect' => 'boolean',
250 'talkfrom' => array(
251 ApiBase::PROP_TYPE => 'string',
252 ApiBase::PROP_NULLABLE => true
253 ),
254 'talkto' => array(
255 ApiBase::PROP_TYPE => 'string',
256 ApiBase::PROP_NULLABLE => true
257 ),
258 'talkmoveoverredirect' => 'boolean',
259 'talkmove-error-code' => array(
260 ApiBase::PROP_TYPE => 'string',
261 ApiBase::PROP_NULLABLE => true
262 ),
263 'talkmove-error-info' => array(
264 ApiBase::PROP_TYPE => 'string',
265 ApiBase::PROP_NULLABLE => true
266 )
267 )
268 );
269 }
270
271 public function getDescription() {
272 return 'Move a page';
273 }
274
275 public function getPossibleErrors() {
276 return array_merge( parent::getPossibleErrors(),
277 $this->getRequireOnlyOneParameterErrorMessages( array( 'from', 'fromid' ) ),
278 array(
279 array( 'invalidtitle', 'from' ),
280 array( 'nosuchpageid', 'fromid' ),
281 array( 'notanarticle' ),
282 array( 'invalidtitle', 'to' ),
283 array( 'sharedfile-exists' ),
284 )
285 );
286 }
287
288 public function needsToken() {
289 return true;
290 }
291
292 public function getTokenSalt() {
293 return '';
294 }
295
296 public function getExamples() {
297 return array(
298 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk=&noredirect='
299 );
300 }
301
302 public function getHelpUrls() {
303 return 'https://www.mediawiki.org/wiki/API:Move';
304 }
305 }