Merge "Don't check namespace in SpecialWantedtemplates"
[lhc/web/wiklou.git] / includes / api / ApiMove.php
1 <?php
2 /**
3 *
4 *
5 * Created on Oct 31, 2007
6 *
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * API Module to move pages
29 * @ingroup API
30 */
31 class ApiMove extends ApiBase {
32
33 public function execute() {
34 $this->useTransactionalTimeLimit();
35
36 $user = $this->getUser();
37 $params = $this->extractRequestParams();
38
39 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
40
41 if ( isset( $params['from'] ) ) {
42 $fromTitle = Title::newFromText( $params['from'] );
43 if ( !$fromTitle || $fromTitle->isExternal() ) {
44 $this->dieUsageMsg( array( 'invalidtitle', $params['from'] ) );
45 }
46 } elseif ( isset( $params['fromid'] ) ) {
47 $fromTitle = Title::newFromID( $params['fromid'] );
48 if ( !$fromTitle ) {
49 $this->dieUsageMsg( array( 'nosuchpageid', $params['fromid'] ) );
50 }
51 }
52
53 if ( !$fromTitle->exists() ) {
54 $this->dieUsageMsg( 'notanarticle' );
55 }
56 $fromTalk = $fromTitle->getTalkPage();
57
58 $toTitle = Title::newFromText( $params['to'] );
59 if ( !$toTitle || $toTitle->isExternal() ) {
60 $this->dieUsageMsg( array( 'invalidtitle', $params['to'] ) );
61 }
62 $toTalk = $toTitle->getTalkPage();
63
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' );
72 }
73 }
74
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 );
80 }
81
82 $r = array(
83 'from' => $fromTitle->getPrefixedText(),
84 'to' => $toTitle->getPrefixedText(),
85 'reason' => $params['reason']
86 );
87
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();
94
95 $r['moveoverredirect'] = $toTitleExists;
96
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];
110 }
111 }
112
113 $result = $this->getResult();
114
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' );
120
121 if ( $params['movetalk'] ) {
122 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
123 $params['reason'], $params['noredirect'] );
124 ApiResult::setIndexedTagName( $r['subpages-talk'], 'subpage' );
125 }
126 }
127
128 $watch = 'preferences';
129 if ( isset( $params['watchlist'] ) ) {
130 $watch = $params['watchlist'];
131 } elseif ( $params['watch'] ) {
132 $watch = 'watch';
133 $this->logFeatureUsage( 'action=move&watch' );
134 } elseif ( $params['unwatch'] ) {
135 $watch = 'unwatch';
136 $this->logFeatureUsage( 'action=move&unwatch' );
137 }
138
139 // Watch pages
140 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
141 $this->setWatch( $watch, $toTitle, 'watchmoves' );
142
143 $result->addValue( null, $this->getModuleName(), $r );
144 }
145
146 /**
147 * @param Title $from
148 * @param Title $to
149 * @param string $reason
150 * @param bool $createRedirect
151 * @return Status
152 */
153 protected function movePage( Title $from, Title $to, $reason, $createRedirect ) {
154 $mp = new MovePage( $from, $to );
155 $valid = $mp->isValidMove();
156 if ( !$valid->isOK() ) {
157 return $valid;
158 }
159
160 $permStatus = $mp->checkPermissions( $this->getUser(), $reason );
161 if ( !$permStatus->isOK() ) {
162 return $permStatus;
163 }
164
165 // Check suppressredirect permission
166 if ( !$this->getUser()->isAllowed( 'suppressredirect' ) ) {
167 $createRedirect = true;
168 }
169
170 return $mp->move( $this->getUser(), $reason, $createRedirect );
171 }
172
173 /**
174 * @param Title $fromTitle
175 * @param Title $toTitle
176 * @param string $reason
177 * @param bool $noredirect
178 * @return array
179 */
180 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect ) {
181 $retval = array();
182 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
183 if ( isset( $success[0] ) ) {
184 return array( 'error' => $this->parseMsg( $success ) );
185 }
186
187 // At least some pages could be moved
188 // Report each of them separately
189 foreach ( $success as $oldTitle => $newTitle ) {
190 $r = array( 'from' => $oldTitle );
191 if ( is_array( $newTitle ) ) {
192 $r['error'] = $this->parseMsg( reset( $newTitle ) );
193 } else {
194 // Success
195 $r['to'] = $newTitle;
196 }
197 $retval[] = $r;
198 }
199
200 return $retval;
201 }
202
203 public function mustBePosted() {
204 return true;
205 }
206
207 public function isWriteMode() {
208 return true;
209 }
210
211 public function getAllowedParams() {
212 return array(
213 'from' => null,
214 'fromid' => array(
215 ApiBase::PARAM_TYPE => 'integer'
216 ),
217 'to' => array(
218 ApiBase::PARAM_TYPE => 'string',
219 ApiBase::PARAM_REQUIRED => true
220 ),
221 'reason' => '',
222 'movetalk' => false,
223 'movesubpages' => false,
224 'noredirect' => false,
225 'watch' => array(
226 ApiBase::PARAM_DFLT => false,
227 ApiBase::PARAM_DEPRECATED => true,
228 ),
229 'unwatch' => array(
230 ApiBase::PARAM_DFLT => false,
231 ApiBase::PARAM_DEPRECATED => true,
232 ),
233 'watchlist' => array(
234 ApiBase::PARAM_DFLT => 'preferences',
235 ApiBase::PARAM_TYPE => array(
236 'watch',
237 'unwatch',
238 'preferences',
239 'nochange'
240 ),
241 ),
242 'ignorewarnings' => false
243 );
244 }
245
246 public function needsToken() {
247 return 'csrf';
248 }
249
250 protected function getExamplesMessages() {
251 return array(
252 'action=move&from=Badtitle&to=Goodtitle&token=123ABC&' .
253 'reason=Misspelled%20title&movetalk=&noredirect='
254 => 'apihelp-move-example-move',
255 );
256 }
257
258 public function getHelpUrls() {
259 return 'https://www.mediawiki.org/wiki/API:Move';
260 }
261 }