Test ApiMove
[lhc/web/wiklou.git] / includes / api / ApiMove.php
1 <?php
2 /**
3 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * API Module to move pages
25 * @ingroup API
26 */
27 class ApiMove extends ApiBase {
28
29 public function execute() {
30 $this->useTransactionalTimeLimit();
31
32 $user = $this->getUser();
33 $params = $this->extractRequestParams();
34
35 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
36
37 if ( isset( $params['from'] ) ) {
38 $fromTitle = Title::newFromText( $params['from'] );
39 if ( !$fromTitle || $fromTitle->isExternal() ) {
40 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] );
41 }
42 } elseif ( isset( $params['fromid'] ) ) {
43 $fromTitle = Title::newFromID( $params['fromid'] );
44 if ( !$fromTitle ) {
45 $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
46 }
47 }
48
49 if ( !$fromTitle->exists() ) {
50 $this->dieWithError( 'apierror-missingtitle' );
51 }
52 $fromTalk = $fromTitle->getTalkPage();
53
54 $toTitle = Title::newFromText( $params['to'] );
55 if ( !$toTitle || $toTitle->isExternal() ) {
56 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
57 }
58 $toTalk = $toTitle->getTalkPageIfDefined();
59
60 if ( $toTitle->getNamespace() == NS_FILE
61 && !RepoGroup::singleton()->getLocalRepo()->findFile( $toTitle )
62 && wfFindFile( $toTitle )
63 ) {
64 if ( !$params['ignorewarnings'] && $user->isAllowed( 'reupload-shared' ) ) {
65 $this->dieWithError( 'apierror-fileexists-sharedrepo-perm' );
66 } elseif ( !$user->isAllowed( 'reupload-shared' ) ) {
67 $this->dieWithError( 'apierror-cantoverwrite-sharedfile' );
68 }
69 }
70
71 // Rate limit
72 if ( $user->pingLimiter( 'move' ) ) {
73 $this->dieWithError( 'apierror-ratelimited' );
74 }
75
76 // Check if the user is allowed to add the specified changetags
77 if ( $params['tags'] ) {
78 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
79 if ( !$ableToTag->isOK() ) {
80 $this->dieStatus( $ableToTag );
81 }
82 }
83
84 // Move the page
85 $toTitleExists = $toTitle->exists();
86 $status = $this->movePage( $fromTitle, $toTitle, $params['reason'], !$params['noredirect'],
87 $params['tags'] ?: [] );
88 if ( !$status->isOK() ) {
89 $this->dieStatus( $status );
90 }
91
92 $r = [
93 'from' => $fromTitle->getPrefixedText(),
94 'to' => $toTitle->getPrefixedText(),
95 'reason' => $params['reason']
96 ];
97
98 // NOTE: we assume that if the old title exists, it's because it was re-created as
99 // a redirect to the new title. This is not safe, but what we did before was
100 // even worse: we just determined whether a redirect should have been created,
101 // and reported that it was created if it should have, without any checks.
102 $r['redirectcreated'] = $fromTitle->exists();
103
104 $r['moveoverredirect'] = $toTitleExists;
105
106 // Move the talk page
107 if ( $params['movetalk'] && $toTalk && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
108 $toTalkExists = $toTalk->exists();
109 $status = $this->movePage(
110 $fromTalk,
111 $toTalk,
112 $params['reason'],
113 !$params['noredirect'],
114 $params['tags'] ?: []
115 );
116 if ( $status->isOK() ) {
117 $r['talkfrom'] = $fromTalk->getPrefixedText();
118 $r['talkto'] = $toTalk->getPrefixedText();
119 $r['talkmoveoverredirect'] = $toTalkExists;
120 } else {
121 // We're not going to dieWithError() on failure, since we already changed something
122 $r['talkmove-errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
123 }
124 }
125
126 $result = $this->getResult();
127
128 // Move subpages
129 if ( $params['movesubpages'] ) {
130 $r['subpages'] = $this->moveSubpages(
131 $fromTitle,
132 $toTitle,
133 $params['reason'],
134 $params['noredirect'],
135 $params['tags'] ?: []
136 );
137 ApiResult::setIndexedTagName( $r['subpages'], 'subpage' );
138
139 if ( $params['movetalk'] ) {
140 $r['subpages-talk'] = $this->moveSubpages(
141 $fromTalk,
142 $toTalk,
143 $params['reason'],
144 $params['noredirect'],
145 $params['tags'] ?: []
146 );
147 ApiResult::setIndexedTagName( $r['subpages-talk'], 'subpage' );
148 }
149 }
150
151 $watch = 'preferences';
152 if ( isset( $params['watchlist'] ) ) {
153 $watch = $params['watchlist'];
154 }
155
156 // Watch pages
157 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
158 $this->setWatch( $watch, $toTitle, 'watchmoves' );
159
160 $result->addValue( null, $this->getModuleName(), $r );
161 }
162
163 /**
164 * @param Title $from
165 * @param Title $to
166 * @param string $reason
167 * @param bool $createRedirect
168 * @param array $changeTags Applied to the entry in the move log and redirect page revision
169 * @return Status
170 */
171 protected function movePage( Title $from, Title $to, $reason, $createRedirect, $changeTags ) {
172 $mp = new MovePage( $from, $to );
173 $valid = $mp->isValidMove();
174 if ( !$valid->isOK() ) {
175 return $valid;
176 }
177
178 $user = $this->getUser();
179 $permStatus = $mp->checkPermissions( $user, $reason );
180 if ( !$permStatus->isOK() ) {
181 return $permStatus;
182 }
183
184 // Check suppressredirect permission
185 if ( !$user->isAllowed( 'suppressredirect' ) ) {
186 $createRedirect = true;
187 }
188
189 return $mp->move( $user, $reason, $createRedirect, $changeTags );
190 }
191
192 /**
193 * @param Title $fromTitle
194 * @param Title $toTitle
195 * @param string $reason
196 * @param bool $noredirect
197 * @param array $changeTags Applied to the entry in the move log and redirect page revisions
198 * @return array
199 */
200 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect, $changeTags = [] ) {
201 $retval = [];
202
203 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect, $changeTags );
204 if ( isset( $success[0] ) ) {
205 $status = $this->errorArrayToStatus( $success );
206 return [ 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ) ];
207 }
208
209 // At least some pages could be moved
210 // Report each of them separately
211 foreach ( $success as $oldTitle => $newTitle ) {
212 $r = [ 'from' => $oldTitle ];
213 if ( is_array( $newTitle ) ) {
214 $status = $this->errorArrayToStatus( $newTitle );
215 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
216 } else {
217 // Success
218 $r['to'] = $newTitle;
219 }
220 $retval[] = $r;
221 }
222
223 return $retval;
224 }
225
226 public function mustBePosted() {
227 return true;
228 }
229
230 public function isWriteMode() {
231 return true;
232 }
233
234 public function getAllowedParams() {
235 return [
236 'from' => null,
237 'fromid' => [
238 ApiBase::PARAM_TYPE => 'integer'
239 ],
240 'to' => [
241 ApiBase::PARAM_TYPE => 'string',
242 ApiBase::PARAM_REQUIRED => true
243 ],
244 'reason' => '',
245 'movetalk' => false,
246 'movesubpages' => false,
247 'noredirect' => false,
248 'watchlist' => [
249 ApiBase::PARAM_DFLT => 'preferences',
250 ApiBase::PARAM_TYPE => [
251 'watch',
252 'unwatch',
253 'preferences',
254 'nochange'
255 ],
256 ],
257 'ignorewarnings' => false,
258 'tags' => [
259 ApiBase::PARAM_TYPE => 'tags',
260 ApiBase::PARAM_ISMULTI => true,
261 ],
262 ];
263 }
264
265 public function needsToken() {
266 return 'csrf';
267 }
268
269 protected function getExamplesMessages() {
270 return [
271 'action=move&from=Badtitle&to=Goodtitle&token=123ABC&' .
272 'reason=Misspelled%20title&movetalk=&noredirect='
273 => 'apihelp-move-example-move',
274 ];
275 }
276
277 public function getHelpUrls() {
278 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Move';
279 }
280 }