Merge "Revert "Toolbar: Only show on WikiText pages""
[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 $user = $this->getUser();
35 $params = $this->extractRequestParams();
36
37 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
38
39 if ( isset( $params['from'] ) ) {
40 $fromTitle = Title::newFromText( $params['from'] );
41 if ( !$fromTitle || $fromTitle->isExternal() ) {
42 $this->dieUsageMsg( array( 'invalidtitle', $params['from'] ) );
43 }
44 } elseif ( isset( $params['fromid'] ) ) {
45 $fromTitle = Title::newFromID( $params['fromid'] );
46 if ( !$fromTitle ) {
47 $this->dieUsageMsg( array( 'nosuchpageid', $params['fromid'] ) );
48 }
49 }
50
51 if ( !$fromTitle->exists() ) {
52 $this->dieUsageMsg( 'notanarticle' );
53 }
54 $fromTalk = $fromTitle->getTalkPage();
55
56 $toTitle = Title::newFromText( $params['to'] );
57 if ( !$toTitle || $toTitle->isExternal() ) {
58 $this->dieUsageMsg( array( 'invalidtitle', $params['to'] ) );
59 }
60 $toTalk = $toTitle->getTalkPage();
61
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' );
70 }
71 }
72
73 // Move the page
74 $toTitleExists = $toTitle->exists();
75 $retval = $fromTitle->moveTo( $toTitle, true, $params['reason'], !$params['noredirect'] );
76 if ( $retval !== true ) {
77 $this->dieUsageMsg( reset( $retval ) );
78 }
79
80 $r = array(
81 'from' => $fromTitle->getPrefixedText(),
82 'to' => $toTitle->getPrefixedText(),
83 'reason' => $params['reason']
84 );
85
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'] = '';
93 }
94
95 if ( $toTitleExists ) {
96 $r['moveoverredirect'] = '';
97 }
98
99 // Move the talk page
100 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
101 $toTalkExists = $toTalk->exists();
102 $retval = $fromTalk->moveTo( $toTalk, true, $params['reason'], !$params['noredirect'] );
103 if ( $retval === true ) {
104 $r['talkfrom'] = $fromTalk->getPrefixedText();
105 $r['talkto'] = $toTalk->getPrefixedText();
106 if ( $toTalkExists ) {
107 $r['talkmoveoverredirect'] = '';
108 }
109 } else {
110 // We're not gonna dieUsage() on failure, since we already changed something
111 $parsed = $this->parseMsg( reset( $retval ) );
112 $r['talkmove-error-code'] = $parsed['code'];
113 $r['talkmove-error-info'] = $parsed['info'];
114 }
115 }
116
117 $result = $this->getResult();
118
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' );
124
125 if ( $params['movetalk'] ) {
126 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
127 $params['reason'], $params['noredirect'] );
128 $result->setIndexedTagName( $r['subpages-talk'], 'subpage' );
129 }
130 }
131
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' );
141 }
142
143 // Watch pages
144 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
145 $this->setWatch( $watch, $toTitle, 'watchmoves' );
146
147 $result->addValue( null, $this->getModuleName(), $r );
148 }
149
150 /**
151 * @param Title $fromTitle
152 * @param Title $toTitle
153 * @param string $reason
154 * @param bool $noredirect
155 * @return array
156 */
157 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect ) {
158 $retval = array();
159 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
160 if ( isset( $success[0] ) ) {
161 return array( 'error' => $this->parseMsg( $success ) );
162 }
163
164 // At least some pages could be moved
165 // Report each of them separately
166 foreach ( $success as $oldTitle => $newTitle ) {
167 $r = array( 'from' => $oldTitle );
168 if ( is_array( $newTitle ) ) {
169 $r['error'] = $this->parseMsg( reset( $newTitle ) );
170 } else {
171 // Success
172 $r['to'] = $newTitle;
173 }
174 $retval[] = $r;
175 }
176
177 return $retval;
178 }
179
180 public function mustBePosted() {
181 return true;
182 }
183
184 public function isWriteMode() {
185 return true;
186 }
187
188 public function getAllowedParams() {
189 return array(
190 'from' => null,
191 'fromid' => array(
192 ApiBase::PARAM_TYPE => 'integer'
193 ),
194 'to' => array(
195 ApiBase::PARAM_TYPE => 'string',
196 ApiBase::PARAM_REQUIRED => true
197 ),
198 'reason' => '',
199 'movetalk' => false,
200 'movesubpages' => false,
201 'noredirect' => false,
202 'watch' => array(
203 ApiBase::PARAM_DFLT => false,
204 ApiBase::PARAM_DEPRECATED => true,
205 ),
206 'unwatch' => array(
207 ApiBase::PARAM_DFLT => false,
208 ApiBase::PARAM_DEPRECATED => true,
209 ),
210 'watchlist' => array(
211 ApiBase::PARAM_DFLT => 'preferences',
212 ApiBase::PARAM_TYPE => array(
213 'watch',
214 'unwatch',
215 'preferences',
216 'nochange'
217 ),
218 ),
219 'ignorewarnings' => false
220 );
221 }
222
223 public function getParamDescription() {
224 $p = $this->getModulePrefix();
225
226 return array(
227 'from' => "Title of the page you want to move. Cannot be used together with {$p}fromid",
228 'fromid' => "Page ID of the page you want to move. Cannot be used together with {$p}from",
229 'to' => 'Title you want to rename the page to',
230 'reason' => 'Reason for the move',
231 'movetalk' => 'Move the talk page, if it exists',
232 'movesubpages' => 'Move subpages, if applicable',
233 'noredirect' => 'Don\'t create a redirect',
234 'watch' => 'Add the page and the redirect to your watchlist',
235 'unwatch' => 'Remove the page and the redirect from your watchlist',
236 'watchlist' => 'Unconditionally add or remove the page from your ' .
237 'watchlist, use preferences or do not change watch',
238 'ignorewarnings' => 'Ignore any warnings'
239 );
240 }
241
242 public function getDescription() {
243 return 'Move a page.';
244 }
245
246 public function needsToken() {
247 return 'csrf';
248 }
249
250 public function getExamples() {
251 return array(
252 'api.php?action=move&from=Badtitle&to=Goodtitle&token=123ABC&' .
253 'reason=Misspelled%20title&movetalk=&noredirect='
254 );
255 }
256
257 public function getHelpUrls() {
258 return 'https://www.mediawiki.org/wiki/API:Move';
259 }
260 }