Fix hook situation for Skin::doEditSectionLink
[mediawiki.git] / includes / api / ApiMove.php
blob7fb6303321b73015add0f7676b509c895449fa81
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 $user = $this->getUser();
35 $params = $this->extractRequestParams();
37 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
39 if ( isset( $params['from'] ) ) {
40 $fromTitle = Title::newFromText( $params['from'] );
41 if ( !$fromTitle || $fromTitle->isExternal() ) {
42 $this->dieUsageMsg( array( 'invalidtitle', $params['from'] ) );
44 } elseif ( isset( $params['fromid'] ) ) {
45 $fromTitle = Title::newFromID( $params['fromid'] );
46 if ( !$fromTitle ) {
47 $this->dieUsageMsg( array( 'nosuchpageid', $params['fromid'] ) );
51 if ( !$fromTitle->exists() ) {
52 $this->dieUsageMsg( 'notanarticle' );
54 $fromTalk = $fromTitle->getTalkPage();
56 $toTitle = Title::newFromText( $params['to'] );
57 if ( !$toTitle || $toTitle->isExternal() ) {
58 $this->dieUsageMsg( array( 'invalidtitle', $params['to'] ) );
60 $toTalk = $toTitle->getTalkPage();
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' );
73 // Move the page
74 $toTitleExists = $toTitle->exists();
75 $status = $this->movePage( $fromTitle, $toTitle, $params['reason'], !$params['noredirect'] );
76 if ( !$status->isOK() ) {
77 $this->dieStatus( $status );
80 $r = array(
81 'from' => $fromTitle->getPrefixedText(),
82 'to' => $toTitle->getPrefixedText(),
83 'reason' => $params['reason']
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'] = '';
95 if ( $toTitleExists ) {
96 $r['moveoverredirect'] = '';
99 // Move the talk page
100 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
101 $toTalkExists = $toTalk->exists();
102 $status = $this->movePage( $fromTalk, $toTalk, $params['reason'], !$params['noredirect'] );
103 if ( $status->isOK() ) {
104 $r['talkfrom'] = $fromTalk->getPrefixedText();
105 $r['talkto'] = $toTalk->getPrefixedText();
106 if ( $toTalkExists ) {
107 $r['talkmoveoverredirect'] = '';
109 } else {
110 // We're not gonna dieUsage() on failure, since we already changed something
111 $error = $this->getErrorFromStatus( $status );
112 $r['talkmove-error-code'] = $error[0];
113 $r['talkmove-error-info'] = $error[1];
117 $result = $this->getResult();
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' );
125 if ( $params['movetalk'] ) {
126 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
127 $params['reason'], $params['noredirect'] );
128 $result->setIndexedTagName( $r['subpages-talk'], 'subpage' );
132 $watch = 'preferences';
133 if ( isset( $params['watchlist'] ) ) {
134 $watch = $params['watchlist'];
135 } elseif ( $params['watch'] ) {
136 $watch = 'watch';
137 $this->logFeatureUsage( 'action=move&watch' );
138 } elseif ( $params['unwatch'] ) {
139 $watch = 'unwatch';
140 $this->logFeatureUsage( 'action=move&unwatch' );
143 // Watch pages
144 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
145 $this->setWatch( $watch, $toTitle, 'watchmoves' );
147 $result->addValue( null, $this->getModuleName(), $r );
151 * @param Title $from
152 * @param Title $to
153 * @param string $reason
154 * @param bool $createRedirect
155 * @return Status
157 protected function movePage( Title $from, Title $to, $reason, $createRedirect ) {
158 $mp = new MovePage( $from, $to );
159 $valid = $mp->isValidMove();
160 if ( !$valid->isOK() ) {
161 return $valid;
164 $permStatus = $mp->checkPermissions( $this->getUser(), $reason );
165 if ( !$permStatus->isOK() ) {
166 return $permStatus;
169 // Check suppressredirect permission
170 if ( !$this->getUser()->isAllowed( 'suppressredirect' ) ) {
171 $createRedirect = true;
174 return $mp->move( $this->getUser(), $reason, $createRedirect );
178 * @param Title $fromTitle
179 * @param Title $toTitle
180 * @param string $reason
181 * @param bool $noredirect
182 * @return array
184 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect ) {
185 $retval = array();
186 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
187 if ( isset( $success[0] ) ) {
188 return array( 'error' => $this->parseMsg( $success ) );
191 // At least some pages could be moved
192 // Report each of them separately
193 foreach ( $success as $oldTitle => $newTitle ) {
194 $r = array( 'from' => $oldTitle );
195 if ( is_array( $newTitle ) ) {
196 $r['error'] = $this->parseMsg( reset( $newTitle ) );
197 } else {
198 // Success
199 $r['to'] = $newTitle;
201 $retval[] = $r;
204 return $retval;
207 public function mustBePosted() {
208 return true;
211 public function isWriteMode() {
212 return true;
215 public function getAllowedParams() {
216 return array(
217 'from' => null,
218 'fromid' => array(
219 ApiBase::PARAM_TYPE => 'integer'
221 'to' => array(
222 ApiBase::PARAM_TYPE => 'string',
223 ApiBase::PARAM_REQUIRED => true
225 'reason' => '',
226 'movetalk' => false,
227 'movesubpages' => false,
228 'noredirect' => false,
229 'watch' => array(
230 ApiBase::PARAM_DFLT => false,
231 ApiBase::PARAM_DEPRECATED => true,
233 'unwatch' => array(
234 ApiBase::PARAM_DFLT => false,
235 ApiBase::PARAM_DEPRECATED => true,
237 'watchlist' => array(
238 ApiBase::PARAM_DFLT => 'preferences',
239 ApiBase::PARAM_TYPE => array(
240 'watch',
241 'unwatch',
242 'preferences',
243 'nochange'
246 'ignorewarnings' => false
250 public function needsToken() {
251 return 'csrf';
254 protected function getExamplesMessages() {
255 return array(
256 'action=move&from=Badtitle&to=Goodtitle&token=123ABC&' .
257 'reason=Misspelled%20title&movetalk=&noredirect='
258 => 'apihelp-move-example-move',
262 public function getHelpUrls() {
263 return 'https://www.mediawiki.org/wiki/API:Move';