Follow-up r93258, r93266, r93266: Move the defines to Defines.php
[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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
30 }
31
32 /**
33 * API Module to move pages
34 * @ingroup API
35 */
36 class ApiMove extends ApiBase {
37
38 public function __construct( $main, $action ) {
39 parent::__construct( $main, $action );
40 }
41
42 public function execute() {
43 global $wgUser;
44 $params = $this->extractRequestParams();
45 if ( is_null( $params['reason'] ) ) {
46 $params['reason'] = '';
47 }
48
49 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
50
51 if ( isset( $params['from'] ) ) {
52 $fromTitle = Title::newFromText( $params['from'] );
53 if ( !$fromTitle ) {
54 $this->dieUsageMsg( array( 'invalidtitle', $params['from'] ) );
55 }
56 } elseif ( isset( $params['fromid'] ) ) {
57 $fromTitle = Title::newFromID( $params['fromid'] );
58 if ( !$fromTitle ) {
59 $this->dieUsageMsg( array( 'nosuchpageid', $params['fromid'] ) );
60 }
61 }
62
63 if ( !$fromTitle->exists() ) {
64 $this->dieUsageMsg( 'notanarticle' );
65 }
66 $fromTalk = $fromTitle->getTalkPage();
67
68 $toTitle = Title::newFromText( $params['to'] );
69 if ( !$toTitle ) {
70 $this->dieUsageMsg( array( 'invalidtitle', $params['to'] ) );
71 }
72 $toTalk = $toTitle->getTalkPage();
73
74 if ( $toTitle->getNamespace() == NS_FILE
75 && !RepoGroup::singleton()->getLocalRepo()->findFile( $toTitle )
76 && wfFindFile( $toTitle ) )
77 {
78 if ( !$params['ignorewarnings'] && $wgUser->isAllowed( 'reupload-shared' ) ) {
79 $this->dieUsageMsg( 'sharedfile-exists' );
80 } elseif ( !$wgUser->isAllowed( 'reupload-shared' ) ) {
81 $this->dieUsageMsg( 'cantoverwrite-sharedfile' );
82 }
83 }
84
85 // Move the page
86 $retval = $fromTitle->moveTo( $toTitle, true, $params['reason'], !$params['noredirect'] );
87 if ( $retval !== true ) {
88 $this->dieUsageMsg( reset( $retval ) );
89 }
90
91 $r = array( 'from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason'] );
92 if ( !$params['noredirect'] || !$wgUser->isAllowed( 'suppressredirect' ) ) {
93 $r['redirectcreated'] = '';
94 }
95
96 // Move the talk page
97 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
98 $retval = $fromTalk->moveTo( $toTalk, true, $params['reason'], !$params['noredirect'] );
99 if ( $retval === true ) {
100 $r['talkfrom'] = $fromTalk->getPrefixedText();
101 $r['talkto'] = $toTalk->getPrefixedText();
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'];
107 }
108 }
109
110 $result = $this->getResult();
111
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' );
117
118 if ( $params['movetalk'] ) {
119 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
120 $params['reason'], $params['noredirect'] );
121 $result->setIndexedTagName( $r['subpages-talk'], 'subpage' );
122 }
123 }
124
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';
132 }
133
134 // Watch pages
135 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
136 $this->setWatch( $watch, $toTitle, 'watchmoves' );
137
138 $result->addValue( null, $this->getModuleName(), $r );
139 }
140
141 /**
142 * @param Title $fromTitle
143 * @param Title $toTitle
144 * @param $reason
145 * @param $noredirect
146 * @return array
147 */
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;
163 }
164 $retval[] = $r;
165 }
166 }
167 return $retval;
168 }
169
170 public function mustBePosted() {
171 return true;
172 }
173
174 public function isWriteMode() {
175 return true;
176 }
177
178 public function getAllowedParams() {
179 return array(
180 'from' => null,
181 'fromid' => array(
182 ApiBase::PARAM_TYPE => 'integer'
183 ),
184 'to' => array(
185 ApiBase::PARAM_TYPE => 'string',
186 ApiBase::PARAM_REQUIRED => true
187 ),
188 'token' => null,
189 'reason' => null,
190 'movetalk' => false,
191 'movesubpages' => false,
192 'noredirect' => false,
193 'watch' => array(
194 ApiBase::PARAM_DFLT => false,
195 ApiBase::PARAM_DEPRECATED => true,
196 ),
197 'unwatch' => array(
198 ApiBase::PARAM_DFLT => false,
199 ApiBase::PARAM_DEPRECATED => true,
200 ),
201 'watchlist' => array(
202 ApiBase::PARAM_DFLT => 'preferences',
203 ApiBase::PARAM_TYPE => array(
204 'watch',
205 'unwatch',
206 'preferences',
207 'nochange'
208 ),
209 ),
210 'ignorewarnings' => false
211 );
212 }
213
214 public function getParamDescription() {
215 $p = $this->getModulePrefix();
216 return array(
217 'from' => "Title of the page you want to move. Cannot be used together with {$p}fromid",
218 'fromid' => "Page ID of the page you want to move. Cannot be used together with {$p}from",
219 'to' => 'Title you want to rename the page to',
220 'token' => 'A move token previously retrieved through prop=info',
221 'reason' => 'Reason for the move (optional)',
222 'movetalk' => 'Move the talk page, if it exists',
223 'movesubpages' => 'Move subpages, if applicable',
224 'noredirect' => 'Don\'t create a redirect',
225 'watch' => 'Add the page and the redirect to your watchlist',
226 'unwatch' => 'Remove the page and the redirect from your watchlist',
227 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
228 'ignorewarnings' => 'Ignore any warnings'
229 );
230 }
231
232 public function getDescription() {
233 return 'Move a page';
234 }
235
236 public function getPossibleErrors() {
237 return array_merge( parent::getPossibleErrors(),
238 $this->getRequireOnlyOneParameterErrorMessages( array( 'from', 'fromid' ) ),
239 array(
240 array( 'invalidtitle', 'from' ),
241 array( 'nosuchpageid', 'fromid' ),
242 array( 'notanarticle' ),
243 array( 'invalidtitle', 'to' ),
244 array( 'sharedfile-exists' ),
245 )
246 );
247 }
248
249 public function needsToken() {
250 return true;
251 }
252
253 public function getTokenSalt() {
254 return '';
255 }
256
257 protected function getExamples() {
258 return array(
259 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk=&noredirect='
260 );
261 }
262
263 public function getHelpUrls() {
264 return 'http://www.mediawiki.org/wiki/API:Move';
265 }
266
267 public function getVersion() {
268 return __CLASS__ . ': $Id$';
269 }
270 }