Import: Handle uploads with sha1 starting with 0 properly
[mediawiki.git] / includes / api / ApiMove.php
blobc38457eba777161914b35610ef1e574cb66cbeb7
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->dieUsageMsg( array( 'invalidtitle', $params['from'] ) );
46 } elseif ( isset( $params['fromid'] ) ) {
47 $fromTitle = Title::newFromID( $params['fromid'] );
48 if ( !$fromTitle ) {
49 $this->dieUsageMsg( array( 'nosuchpageid', $params['fromid'] ) );
53 if ( !$fromTitle->exists() ) {
54 $this->dieUsageMsg( 'notanarticle' );
56 $fromTalk = $fromTitle->getTalkPage();
58 $toTitle = Title::newFromText( $params['to'] );
59 if ( !$toTitle || $toTitle->isExternal() ) {
60 $this->dieUsageMsg( array( 'invalidtitle', $params['to'] ) );
62 $toTalk = $toTitle->getTalkPage();
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->dieUsageMsg( 'sharedfile-exists' );
70 } elseif ( !$user->isAllowed( 'reupload-shared' ) ) {
71 $this->dieUsageMsg( 'cantoverwrite-sharedfile' );
75 // Move the page
76 $toTitleExists = $toTitle->exists();
77 $status = $this->movePage( $fromTitle, $toTitle, $params['reason'], !$params['noredirect'] );
78 if ( !$status->isOK() ) {
79 $this->dieStatus( $status );
82 $r = array(
83 'from' => $fromTitle->getPrefixedText(),
84 'to' => $toTitle->getPrefixedText(),
85 'reason' => $params['reason']
88 // NOTE: we assume that if the old title exists, it's because it was re-created as
89 // a redirect to the new title. This is not safe, but what we did before was
90 // even worse: we just determined whether a redirect should have been created,
91 // and reported that it was created if it should have, without any checks.
92 // Also note that isRedirect() is unreliable because of bug 37209.
93 $r['redirectcreated'] = $fromTitle->exists();
95 $r['moveoverredirect'] = $toTitleExists;
97 // Move the talk page
98 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
99 $toTalkExists = $toTalk->exists();
100 $status = $this->movePage( $fromTalk, $toTalk, $params['reason'], !$params['noredirect'] );
101 if ( $status->isOK() ) {
102 $r['talkfrom'] = $fromTalk->getPrefixedText();
103 $r['talkto'] = $toTalk->getPrefixedText();
104 $r['talkmoveoverredirect'] = $toTalkExists;
105 } else {
106 // We're not gonna dieUsage() on failure, since we already changed something
107 $error = $this->getErrorFromStatus( $status );
108 $r['talkmove-error-code'] = $error[0];
109 $r['talkmove-error-info'] = $error[1];
113 $result = $this->getResult();
115 // Move subpages
116 if ( $params['movesubpages'] ) {
117 $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle,
118 $params['reason'], $params['noredirect'] );
119 ApiResult::setIndexedTagName( $r['subpages'], 'subpage' );
121 if ( $params['movetalk'] ) {
122 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
123 $params['reason'], $params['noredirect'] );
124 ApiResult::setIndexedTagName( $r['subpages-talk'], 'subpage' );
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';
137 // Watch pages
138 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
139 $this->setWatch( $watch, $toTitle, 'watchmoves' );
141 $result->addValue( null, $this->getModuleName(), $r );
145 * @param Title $from
146 * @param Title $to
147 * @param string $reason
148 * @param bool $createRedirect
149 * @return Status
151 protected function movePage( Title $from, Title $to, $reason, $createRedirect ) {
152 $mp = new MovePage( $from, $to );
153 $valid = $mp->isValidMove();
154 if ( !$valid->isOK() ) {
155 return $valid;
158 $permStatus = $mp->checkPermissions( $this->getUser(), $reason );
159 if ( !$permStatus->isOK() ) {
160 return $permStatus;
163 // Check suppressredirect permission
164 if ( !$this->getUser()->isAllowed( 'suppressredirect' ) ) {
165 $createRedirect = true;
168 return $mp->move( $this->getUser(), $reason, $createRedirect );
172 * @param Title $fromTitle
173 * @param Title $toTitle
174 * @param string $reason
175 * @param bool $noredirect
176 * @return array
178 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect ) {
179 $retval = array();
180 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
181 if ( isset( $success[0] ) ) {
182 return array( 'error' => $this->parseMsg( $success ) );
185 // At least some pages could be moved
186 // Report each of them separately
187 foreach ( $success as $oldTitle => $newTitle ) {
188 $r = array( 'from' => $oldTitle );
189 if ( is_array( $newTitle ) ) {
190 $r['error'] = $this->parseMsg( reset( $newTitle ) );
191 } else {
192 // Success
193 $r['to'] = $newTitle;
195 $retval[] = $r;
198 return $retval;
201 public function mustBePosted() {
202 return true;
205 public function isWriteMode() {
206 return true;
209 public function getAllowedParams() {
210 return array(
211 'from' => null,
212 'fromid' => array(
213 ApiBase::PARAM_TYPE => 'integer'
215 'to' => array(
216 ApiBase::PARAM_TYPE => 'string',
217 ApiBase::PARAM_REQUIRED => true
219 'reason' => '',
220 'movetalk' => false,
221 'movesubpages' => false,
222 'noredirect' => false,
223 'watch' => array(
224 ApiBase::PARAM_DFLT => false,
225 ApiBase::PARAM_DEPRECATED => true,
227 'unwatch' => array(
228 ApiBase::PARAM_DFLT => false,
229 ApiBase::PARAM_DEPRECATED => true,
231 'watchlist' => array(
232 ApiBase::PARAM_DFLT => 'preferences',
233 ApiBase::PARAM_TYPE => array(
234 'watch',
235 'unwatch',
236 'preferences',
237 'nochange'
240 'ignorewarnings' => false
244 public function needsToken() {
245 return 'csrf';
248 protected function getExamplesMessages() {
249 return array(
250 'action=move&from=Badtitle&to=Goodtitle&token=123ABC&' .
251 'reason=Misspelled%20title&movetalk=&noredirect='
252 => 'apihelp-move-example-move',
256 public function getHelpUrls() {
257 return 'https://www.mediawiki.org/wiki/API:Move';