Merge "Fix method declaration in UploadFromStash"
[mediawiki.git] / includes / api / ApiMove.php
blob9d73562bbee18ef76c3238f4d0755a3850dc789e
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 __construct( $main, $action ) {
34 parent::__construct( $main, $action );
37 public function execute() {
38 $user = $this->getUser();
39 $params = $this->extractRequestParams();
41 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
43 if ( isset( $params['from'] ) ) {
44 $fromTitle = Title::newFromText( $params['from'] );
45 if ( !$fromTitle ) {
46 $this->dieUsageMsg( array( 'invalidtitle', $params['from'] ) );
48 } elseif ( isset( $params['fromid'] ) ) {
49 $fromTitle = Title::newFromID( $params['fromid'] );
50 if ( !$fromTitle ) {
51 $this->dieUsageMsg( array( 'nosuchpageid', $params['fromid'] ) );
55 if ( !$fromTitle->exists() ) {
56 $this->dieUsageMsg( 'notanarticle' );
58 $fromTalk = $fromTitle->getTalkPage();
60 $toTitle = Title::newFromText( $params['to'] );
61 if ( !$toTitle ) {
62 $this->dieUsageMsg( array( 'invalidtitle', $params['to'] ) );
64 $toTalk = $toTitle->getTalkPage();
66 if ( $toTitle->getNamespace() == NS_FILE
67 && !RepoGroup::singleton()->getLocalRepo()->findFile( $toTitle )
68 && wfFindFile( $toTitle ) )
70 if ( !$params['ignorewarnings'] && $user->isAllowed( 'reupload-shared' ) ) {
71 $this->dieUsageMsg( 'sharedfile-exists' );
72 } elseif ( !$user->isAllowed( 'reupload-shared' ) ) {
73 $this->dieUsageMsg( 'cantoverwrite-sharedfile' );
77 // Move the page
78 $toTitleExists = $toTitle->exists();
79 $retval = $fromTitle->moveTo( $toTitle, true, $params['reason'], !$params['noredirect'] );
80 if ( $retval !== true ) {
81 $this->dieUsageMsg( reset( $retval ) );
84 $r = array( 'from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason'] );
85 if ( !$params['noredirect'] || !$user->isAllowed( 'suppressredirect' ) ) {
86 $r['redirectcreated'] = '';
88 if( $toTitleExists ) {
89 $r['moveoverredirect'] = '';
92 // Move the talk page
93 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
94 $toTalkExists = $toTalk->exists();
95 $retval = $fromTalk->moveTo( $toTalk, true, $params['reason'], !$params['noredirect'] );
96 if ( $retval === true ) {
97 $r['talkfrom'] = $fromTalk->getPrefixedText();
98 $r['talkto'] = $toTalk->getPrefixedText();
99 if( $toTalkExists ) {
100 $r['talkmoveoverredirect'] = '';
102 } else {
103 // We're not gonna dieUsage() on failure, since we already changed something
104 $parsed = $this->parseMsg( reset( $retval ) );
105 $r['talkmove-error-code'] = $parsed['code'];
106 $r['talkmove-error-info'] = $parsed['info'];
110 $result = $this->getResult();
112 // Move subpages
113 if ( $params['movesubpages'] ) {
114 $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle,
115 $params['reason'], $params['noredirect'] );
116 $result->setIndexedTagName( $r['subpages'], 'subpage' );
118 if ( $params['movetalk'] ) {
119 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
120 $params['reason'], $params['noredirect'] );
121 $result->setIndexedTagName( $r['subpages-talk'], 'subpage' );
125 $watch = "preferences";
126 if ( isset( $params['watchlist'] ) ) {
127 $watch = $params['watchlist'];
128 } elseif ( $params['watch'] ) {
129 $watch = 'watch';
130 } elseif ( $params['unwatch'] ) {
131 $watch = 'unwatch';
134 // Watch pages
135 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
136 $this->setWatch( $watch, $toTitle, 'watchmoves' );
138 $result->addValue( null, $this->getModuleName(), $r );
142 * @param Title $fromTitle
143 * @param Title $toTitle
144 * @param $reason
145 * @param $noredirect
146 * @return array
148 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect ) {
149 $retval = array();
150 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
151 if ( isset( $success[0] ) ) {
152 return array( 'error' => $this->parseMsg( $success ) );
153 } else {
154 // At least some pages could be moved
155 // Report each of them separately
156 foreach ( $success as $oldTitle => $newTitle ) {
157 $r = array( 'from' => $oldTitle );
158 if ( is_array( $newTitle ) ) {
159 $r['error'] = $this->parseMsg( reset( $newTitle ) );
160 } else {
161 // Success
162 $r['to'] = $newTitle;
164 $retval[] = $r;
167 return $retval;
170 public function mustBePosted() {
171 return true;
174 public function isWriteMode() {
175 return true;
178 public function getAllowedParams() {
179 return array(
180 'from' => null,
181 'fromid' => array(
182 ApiBase::PARAM_TYPE => 'integer'
184 'to' => array(
185 ApiBase::PARAM_TYPE => 'string',
186 ApiBase::PARAM_REQUIRED => true
188 'token' => array(
189 ApiBase::PARAM_TYPE => 'string',
190 ApiBase::PARAM_REQUIRED => true
192 'reason' => '',
193 'movetalk' => false,
194 'movesubpages' => false,
195 'noredirect' => false,
196 'watch' => array(
197 ApiBase::PARAM_DFLT => false,
198 ApiBase::PARAM_DEPRECATED => true,
200 'unwatch' => array(
201 ApiBase::PARAM_DFLT => false,
202 ApiBase::PARAM_DEPRECATED => true,
204 'watchlist' => array(
205 ApiBase::PARAM_DFLT => 'preferences',
206 ApiBase::PARAM_TYPE => array(
207 'watch',
208 'unwatch',
209 'preferences',
210 'nochange'
213 'ignorewarnings' => false
217 public function getParamDescription() {
218 $p = $this->getModulePrefix();
219 return array(
220 'from' => "Title of the page you want to move. Cannot be used together with {$p}fromid",
221 'fromid' => "Page ID of the page you want to move. Cannot be used together with {$p}from",
222 'to' => 'Title you want to rename the page to',
223 'token' => 'A move token previously retrieved through prop=info',
224 'reason' => 'Reason for the move',
225 'movetalk' => 'Move the talk page, if it exists',
226 'movesubpages' => 'Move subpages, if applicable',
227 'noredirect' => 'Don\'t create a redirect',
228 'watch' => 'Add the page and the redirect to your watchlist',
229 'unwatch' => 'Remove the page and the redirect from your watchlist',
230 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
231 'ignorewarnings' => 'Ignore any warnings'
235 public function getResultProperties() {
236 return array(
237 '' => array(
238 'from' => 'string',
239 'to' => 'string',
240 'reason' => 'string',
241 'redirectcreated' => 'boolean',
242 'moveoverredirect' => 'boolean',
243 'talkfrom' => array(
244 ApiBase::PROP_TYPE => 'string',
245 ApiBase::PROP_NULLABLE => true
247 'talkto' => array(
248 ApiBase::PROP_TYPE => 'string',
249 ApiBase::PROP_NULLABLE => true
251 'talkmoveoverredirect' => 'boolean',
252 'talkmove-error-code' => array(
253 ApiBase::PROP_TYPE => 'string',
254 ApiBase::PROP_NULLABLE => true
256 'talkmove-error-info' => array(
257 ApiBase::PROP_TYPE => 'string',
258 ApiBase::PROP_NULLABLE => true
264 public function getDescription() {
265 return 'Move a page';
268 public function getPossibleErrors() {
269 return array_merge( parent::getPossibleErrors(),
270 $this->getRequireOnlyOneParameterErrorMessages( array( 'from', 'fromid' ) ),
271 array(
272 array( 'invalidtitle', 'from' ),
273 array( 'nosuchpageid', 'fromid' ),
274 array( 'notanarticle' ),
275 array( 'invalidtitle', 'to' ),
276 array( 'sharedfile-exists' ),
281 public function needsToken() {
282 return true;
285 public function getTokenSalt() {
286 return '';
289 public function getExamples() {
290 return array(
291 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk=&noredirect='
295 public function getHelpUrls() {
296 return 'https://www.mediawiki.org/wiki/API:Move';
299 public function getVersion() {
300 return __CLASS__ . ': $Id$';