Followup r64197
[lhc/web/wiklou.git] / includes / api / ApiMove.php
1 <?php
2
3 /**
4 * Created on Oct 31, 2007
5 * API for MediaWiki 1.8+
6 *
7 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 */
24
25 if ( !defined( 'MEDIAWIKI' ) ) {
26 // Eclipse helper - will be ignored in production
27 require_once( "ApiBase.php" );
28 }
29
30 /**
31 * @ingroup API
32 */
33 class ApiMove extends ApiBase {
34
35 public function __construct( $main, $action ) {
36 parent::__construct( $main, $action );
37 }
38
39 public function execute() {
40 global $wgUser;
41 $params = $this->extractRequestParams();
42 if ( is_null( $params['reason'] ) ) {
43 $params['reason'] = '';
44 }
45
46 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
47 if ( !isset( $params['to'] ) ) {
48 $this->dieUsageMsg( array( 'missingparam', 'to' ) );
49 }
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( array( '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( array( 'sharedfile-exists' ) );
80 } elseif ( !$wgUser->isAllowed( 'reupload-shared' ) ) {
81 $this->dieUsageMsg( array( 'cantoverwrite-sharedfile' ) );
82 }
83 }
84
85 // Move the page
86 $hookErr = null;
87 $retval = $fromTitle->moveTo( $toTitle, true, $params['reason'], !$params['noredirect'] );
88 if ( $retval !== true ) {
89 $this->dieUsageMsg( reset( $retval ) );
90 }
91
92 $r = array( 'from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason'] );
93 if ( !$params['noredirect'] || !$wgUser->isAllowed( 'suppressredirect' ) )
94 {
95 $r['redirectcreated'] = '';
96 }
97
98 // Move the talk page
99 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() )
100 {
101 $retval = $fromTalk->moveTo( $toTalk, true, $params['reason'], !$params['noredirect'] );
102 if ( $retval === true ) {
103 $r['talkfrom'] = $fromTalk->getPrefixedText();
104 $r['talkto'] = $toTalk->getPrefixedText();
105 } else {
106 // We're not gonna dieUsage() on failure, since we already changed something
107 $parsed = $this->parseMsg( reset( $retval ) );
108 $r['talkmove-error-code'] = $parsed['code'];
109 $r['talkmove-error-info'] = $parsed['info'];
110 }
111 }
112
113 // Move subpages
114 if ( $params['movesubpages'] ) {
115 $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle,
116 $params['reason'], $params['noredirect'] );
117 $this->getResult()->setIndexedTagName( $r['subpages'], 'subpage' );
118 if ( $params['movetalk'] ) {
119 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
120 $params['reason'], $params['noredirect'] );
121 $this->getResult()->setIndexedTagName( $r['subpages-talk'], 'subpage' );
122 }
123 }
124
125 // Watch pages
126 $watch = $this->getWatchlistValue( $params['watchlist'], $titleObj ) || $wgUser->getOption( 'watchmoves' );
127
128 // Deprecated parameters
129 if ( $params['watch'] ) {
130 $watch = true;
131 } elseif ( $params['unwatch'] ) {
132 $watch = false;
133 }
134
135 if ( $watch !== null ) {
136 if ( $watch ) {
137 $wgUser->addWatch( $fromTitle );
138 $wgUser->addWatch( $toTitle );
139 } else {
140 $wgUser->removeWatch( $fromTitle );
141 $wgUser->removeWatch( $toTitle );
142 }
143 }
144 $this->getResult()->addValue( null, $this->getModuleName(), $r );
145 }
146
147 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect ) {
148 $retval = array();
149 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
150 if ( isset( $success[0] ) ) {
151 return array( 'error' => $this->parseMsg( $success ) );
152 } else {
153 // At least some pages could be moved
154 // Report each of them separately
155 foreach ( $success as $oldTitle => $newTitle ) {
156 $r = array( 'from' => $oldTitle );
157 if ( is_array( $newTitle ) ) {
158 $r['error'] = $this->parseMsg( reset( $newTitle ) );
159 } else {
160 // Success
161 $r['to'] = $newTitle;
162 }
163 $retval[] = $r;
164 }
165 }
166 return $retval;
167 }
168
169 public function mustBePosted() {
170 return true;
171 }
172
173 public function isWriteMode() {
174 return true;
175 }
176
177 public function getAllowedParams() {
178 return array(
179 'from' => null,
180 'fromid' => array(
181 ApiBase::PARAM_TYPE => 'integer'
182 ),
183 'to' => null,
184 'token' => null,
185 'reason' => null,
186 'movetalk' => false,
187 'movesubpages' => false,
188 'noredirect' => false,
189 'watch' => array(
190 ApiBase::PARAM_DFLT => false,
191 ApiBase::PARAM_DEPRECATED => true,
192 ),
193 'unwatch' => array(
194 ApiBase::PARAM_DFLT => false,
195 ApiBase::PARAM_DEPRECATED => true,
196 ),
197 'watchlist' => array(
198 ApiBase::PARAM_DFLT => 'preferences',
199 ApiBase::PARAM_TYPE => array(
200 'watch',
201 'unwatch',
202 'preferences',
203 'nochange'
204 ),
205 ),
206 'ignorewarnings' => false
207 );
208 }
209
210 public function getParamDescription() {
211 return array(
212 'from' => 'Title of the page you want to move. Cannot be used together with fromid.',
213 'fromid' => 'Page ID of the page you want to move. Cannot be used together with from.',
214 'to' => 'Title you want to rename the page to.',
215 'token' => 'A move token previously retrieved through prop=info',
216 'reason' => 'Reason for the move (optional).',
217 'movetalk' => 'Move the talk page, if it exists.',
218 'movesubpages' => 'Move subpages, if applicable',
219 'noredirect' => 'Don\'t create a redirect',
220 'watch' => 'Add the page and the redirect to your watchlist',
221 'unwatch' => 'Remove the page and the redirect from your watchlist',
222 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
223 'ignorewarnings' => 'Ignore any warnings'
224 );
225 }
226
227 public function getDescription() {
228 return array(
229 'Move a page.'
230 );
231 }
232
233 public function getPossibleErrors() {
234 return array_merge( parent::getPossibleErrors(), array(
235 array( 'missingparam', 'to' ),
236 array( 'invalidtitle', 'from' ),
237 array( 'nosuchpageid', 'fromid' ),
238 array( 'notanarticle' ),
239 array( 'invalidtitle', 'to' ),
240 array( 'sharedfile-exists' ),
241 ) );
242 }
243
244 public function getTokenSalt() {
245 return '';
246 }
247
248 protected function getExamples() {
249 return array(
250 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk&noredirect'
251 );
252 }
253
254 public function getVersion() {
255 return __CLASS__ . ': $Id$';
256 }
257 }