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