Add user rights 'viewmywatchlist', 'editmywatchlist'
[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 return $retval;
171 }
172
173 public function mustBePosted() {
174 return true;
175 }
176
177 public function isWriteMode() {
178 return true;
179 }
180
181 public function getAllowedParams() {
182 return array(
183 'from' => null,
184 'fromid' => array(
185 ApiBase::PARAM_TYPE => 'integer'
186 ),
187 'to' => array(
188 ApiBase::PARAM_TYPE => 'string',
189 ApiBase::PARAM_REQUIRED => true
190 ),
191 'token' => array(
192 ApiBase::PARAM_TYPE => 'string',
193 ApiBase::PARAM_REQUIRED => true
194 ),
195 'reason' => '',
196 'movetalk' => false,
197 'movesubpages' => false,
198 'noredirect' => false,
199 'watch' => array(
200 ApiBase::PARAM_DFLT => false,
201 ApiBase::PARAM_DEPRECATED => true,
202 ),
203 'unwatch' => array(
204 ApiBase::PARAM_DFLT => false,
205 ApiBase::PARAM_DEPRECATED => true,
206 ),
207 'watchlist' => array(
208 ApiBase::PARAM_DFLT => 'preferences',
209 ApiBase::PARAM_TYPE => array(
210 'watch',
211 'unwatch',
212 'preferences',
213 'nochange'
214 ),
215 ),
216 'ignorewarnings' => false
217 );
218 }
219
220 public function getParamDescription() {
221 $p = $this->getModulePrefix();
222 return array(
223 'from' => "Title of the page you want to move. Cannot be used together with {$p}fromid",
224 'fromid' => "Page ID of the page you want to move. Cannot be used together with {$p}from",
225 'to' => 'Title you want to rename the page to',
226 'token' => 'A move token previously retrieved through prop=info',
227 'reason' => 'Reason for the move',
228 'movetalk' => 'Move the talk page, if it exists',
229 'movesubpages' => 'Move subpages, if applicable',
230 'noredirect' => 'Don\'t create a redirect',
231 'watch' => 'Add the page and the redirect to your watchlist',
232 'unwatch' => 'Remove the page and the redirect from your watchlist',
233 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
234 'ignorewarnings' => 'Ignore any warnings'
235 );
236 }
237
238 public function getResultProperties() {
239 return array(
240 '' => array(
241 'from' => 'string',
242 'to' => 'string',
243 'reason' => 'string',
244 'redirectcreated' => 'boolean',
245 'moveoverredirect' => 'boolean',
246 'talkfrom' => array(
247 ApiBase::PROP_TYPE => 'string',
248 ApiBase::PROP_NULLABLE => true
249 ),
250 'talkto' => array(
251 ApiBase::PROP_TYPE => 'string',
252 ApiBase::PROP_NULLABLE => true
253 ),
254 'talkmoveoverredirect' => 'boolean',
255 'talkmove-error-code' => array(
256 ApiBase::PROP_TYPE => 'string',
257 ApiBase::PROP_NULLABLE => true
258 ),
259 'talkmove-error-info' => array(
260 ApiBase::PROP_TYPE => 'string',
261 ApiBase::PROP_NULLABLE => true
262 )
263 )
264 );
265 }
266
267 public function getDescription() {
268 return 'Move a page';
269 }
270
271 public function getPossibleErrors() {
272 return array_merge( parent::getPossibleErrors(),
273 $this->getRequireOnlyOneParameterErrorMessages( array( 'from', 'fromid' ) ),
274 array(
275 array( 'invalidtitle', 'from' ),
276 array( 'nosuchpageid', 'fromid' ),
277 array( 'notanarticle' ),
278 array( 'invalidtitle', 'to' ),
279 array( 'sharedfile-exists' ),
280 )
281 );
282 }
283
284 public function needsToken() {
285 return true;
286 }
287
288 public function getTokenSalt() {
289 return '';
290 }
291
292 public function getExamples() {
293 return array(
294 'api.php?action=move&from=Badtitle&to=Goodtitle&token=123ABC&reason=Misspelled%20title&movetalk=&noredirect='
295 );
296 }
297
298 public function getHelpUrls() {
299 return 'https://www.mediawiki.org/wiki/API:Move';
300 }
301 }