Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / api / ApiMove.php
blobab7199f6a50b860d18cf58ae0ee8b6d94b279cd8
1 <?php
2 /**
5 * Created on Oct 31, 2007
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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.
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.
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
24 * @file
27 /**
28 * API Module to move pages
29 * @ingroup API
31 class ApiMove extends ApiBase {
33 public function execute() {
34 $this->useTransactionalTimeLimit();
36 $user = $this->getUser();
37 $params = $this->extractRequestParams();
39 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
41 if ( isset( $params['from'] ) ) {
42 $fromTitle = Title::newFromText( $params['from'] );
43 if ( !$fromTitle || $fromTitle->isExternal() ) {
44 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] );
46 } elseif ( isset( $params['fromid'] ) ) {
47 $fromTitle = Title::newFromID( $params['fromid'] );
48 if ( !$fromTitle ) {
49 $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
53 if ( !$fromTitle->exists() ) {
54 $this->dieWithError( 'apierror-missingtitle' );
56 $fromTalk = $fromTitle->getTalkPage();
58 $toTitle = Title::newFromText( $params['to'] );
59 if ( !$toTitle || $toTitle->isExternal() ) {
60 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
62 $toTalk = $toTitle->canTalk() ? $toTitle->getTalkPage() : null;
64 if ( $toTitle->getNamespace() == NS_FILE
65 && !RepoGroup::singleton()->getLocalRepo()->findFile( $toTitle )
66 && wfFindFile( $toTitle )
67 ) {
68 if ( !$params['ignorewarnings'] && $user->isAllowed( 'reupload-shared' ) ) {
69 $this->dieWithError( 'apierror-fileexists-sharedrepo-perm' );
70 } elseif ( !$user->isAllowed( 'reupload-shared' ) ) {
71 $this->dieWithError( 'apierror-cantoverwrite-sharedfile' );
75 // Rate limit
76 if ( $user->pingLimiter( 'move' ) ) {
77 $this->dieWithError( 'apierror-ratelimited' );
80 // Check if the user is allowed to add the specified changetags
81 if ( $params['tags'] ) {
82 $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
83 if ( !$ableToTag->isOK() ) {
84 $this->dieStatus( $ableToTag );
88 // Move the page
89 $toTitleExists = $toTitle->exists();
90 $status = $this->movePage( $fromTitle, $toTitle, $params['reason'], !$params['noredirect'],
91 $params['tags'] ?: [] );
92 if ( !$status->isOK() ) {
93 $this->dieStatus( $status );
96 $r = [
97 'from' => $fromTitle->getPrefixedText(),
98 'to' => $toTitle->getPrefixedText(),
99 'reason' => $params['reason']
102 // NOTE: we assume that if the old title exists, it's because it was re-created as
103 // a redirect to the new title. This is not safe, but what we did before was
104 // even worse: we just determined whether a redirect should have been created,
105 // and reported that it was created if it should have, without any checks.
106 // Also note that isRedirect() is unreliable because of bug 37209.
107 $r['redirectcreated'] = $fromTitle->exists();
109 $r['moveoverredirect'] = $toTitleExists;
111 // Move the talk page
112 if ( $params['movetalk'] && $toTalk && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
113 $toTalkExists = $toTalk->exists();
114 $status = $this->movePage(
115 $fromTalk,
116 $toTalk,
117 $params['reason'],
118 !$params['noredirect'],
119 $params['tags'] ?: []
121 if ( $status->isOK() ) {
122 $r['talkfrom'] = $fromTalk->getPrefixedText();
123 $r['talkto'] = $toTalk->getPrefixedText();
124 $r['talkmoveoverredirect'] = $toTalkExists;
125 } else {
126 // We're not going to dieWithError() on failure, since we already changed something
127 $r['talkmove-errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
131 $result = $this->getResult();
133 // Move subpages
134 if ( $params['movesubpages'] ) {
135 $r['subpages'] = $this->moveSubpages(
136 $fromTitle,
137 $toTitle,
138 $params['reason'],
139 $params['noredirect'],
140 $params['tags'] ?: []
142 ApiResult::setIndexedTagName( $r['subpages'], 'subpage' );
144 if ( $params['movetalk'] ) {
145 $r['subpages-talk'] = $this->moveSubpages(
146 $fromTalk,
147 $toTalk,
148 $params['reason'],
149 $params['noredirect'],
150 $params['tags'] ?: []
152 ApiResult::setIndexedTagName( $r['subpages-talk'], 'subpage' );
156 $watch = 'preferences';
157 if ( isset( $params['watchlist'] ) ) {
158 $watch = $params['watchlist'];
159 } elseif ( $params['watch'] ) {
160 $watch = 'watch';
161 } elseif ( $params['unwatch'] ) {
162 $watch = 'unwatch';
165 // Watch pages
166 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
167 $this->setWatch( $watch, $toTitle, 'watchmoves' );
169 $result->addValue( null, $this->getModuleName(), $r );
173 * @param Title $from
174 * @param Title $to
175 * @param string $reason
176 * @param bool $createRedirect
177 * @param array $changeTags Applied to the entry in the move log and redirect page revision
178 * @return Status
180 protected function movePage( Title $from, Title $to, $reason, $createRedirect, $changeTags ) {
181 $mp = new MovePage( $from, $to );
182 $valid = $mp->isValidMove();
183 if ( !$valid->isOK() ) {
184 return $valid;
187 $user = $this->getUser();
188 $permStatus = $mp->checkPermissions( $user, $reason );
189 if ( !$permStatus->isOK() ) {
190 return $permStatus;
193 // Check suppressredirect permission
194 if ( !$user->isAllowed( 'suppressredirect' ) ) {
195 $createRedirect = true;
198 return $mp->move( $user, $reason, $createRedirect, $changeTags );
202 * @param Title $fromTitle
203 * @param Title $toTitle
204 * @param string $reason
205 * @param bool $noredirect
206 * @param array $changeTags Applied to the entry in the move log and redirect page revisions
207 * @return array
209 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect, $changeTags = [] ) {
210 $retval = [];
212 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect, $changeTags );
213 if ( isset( $success[0] ) ) {
214 $status = $this->errorArrayToStatus( $success );
215 return [ 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ) ];
218 // At least some pages could be moved
219 // Report each of them separately
220 foreach ( $success as $oldTitle => $newTitle ) {
221 $r = [ 'from' => $oldTitle ];
222 if ( is_array( $newTitle ) ) {
223 $status = $this->errorArrayToStatus( $newTitle );
224 $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
225 } else {
226 // Success
227 $r['to'] = $newTitle;
229 $retval[] = $r;
232 return $retval;
235 public function mustBePosted() {
236 return true;
239 public function isWriteMode() {
240 return true;
243 public function getAllowedParams() {
244 return [
245 'from' => null,
246 'fromid' => [
247 ApiBase::PARAM_TYPE => 'integer'
249 'to' => [
250 ApiBase::PARAM_TYPE => 'string',
251 ApiBase::PARAM_REQUIRED => true
253 'reason' => '',
254 'movetalk' => false,
255 'movesubpages' => false,
256 'noredirect' => false,
257 'watch' => [
258 ApiBase::PARAM_DFLT => false,
259 ApiBase::PARAM_DEPRECATED => true,
261 'unwatch' => [
262 ApiBase::PARAM_DFLT => false,
263 ApiBase::PARAM_DEPRECATED => true,
265 'watchlist' => [
266 ApiBase::PARAM_DFLT => 'preferences',
267 ApiBase::PARAM_TYPE => [
268 'watch',
269 'unwatch',
270 'preferences',
271 'nochange'
274 'ignorewarnings' => false,
275 'tags' => [
276 ApiBase::PARAM_TYPE => 'tags',
277 ApiBase::PARAM_ISMULTI => true,
282 public function needsToken() {
283 return 'csrf';
286 protected function getExamplesMessages() {
287 return [
288 'action=move&from=Badtitle&to=Goodtitle&token=123ABC&' .
289 'reason=Misspelled%20title&movetalk=&noredirect='
290 => 'apihelp-move-example-move',
294 public function getHelpUrls() {
295 return 'https://www.mediawiki.org/wiki/API:Move';