Remove arrays from getDescription where we are only using 1 line
[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 * API Module to move pages
32 * @ingroup API
33 */
34 class ApiMove extends ApiBase {
35
36 public function __construct( $main, $action ) {
37 parent::__construct( $main, $action );
38 }
39
40 public function execute() {
41 global $wgUser;
42 $params = $this->extractRequestParams();
43 if ( is_null( $params['reason'] ) ) {
44 $params['reason'] = '';
45 }
46
47 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
48 if ( !isset( $params['to'] ) ) {
49 $this->dieUsageMsg( array( 'missingparam', 'to' ) );
50 }
51
52 if ( isset( $params['from'] ) ) {
53 $fromTitle = Title::newFromText( $params['from'] );
54 if ( !$fromTitle ) {
55 $this->dieUsageMsg( array( 'invalidtitle', $params['from'] ) );
56 }
57 } elseif ( isset( $params['fromid'] ) ) {
58 $fromTitle = Title::newFromID( $params['fromid'] );
59 if ( !$fromTitle ) {
60 $this->dieUsageMsg( array( 'nosuchpageid', $params['fromid'] ) );
61 }
62 }
63
64 if ( !$fromTitle->exists() ) {
65 $this->dieUsageMsg( array( 'notanarticle' ) );
66 }
67 $fromTalk = $fromTitle->getTalkPage();
68
69 $toTitle = Title::newFromText( $params['to'] );
70 if ( !$toTitle ) {
71 $this->dieUsageMsg( array( 'invalidtitle', $params['to'] ) );
72 }
73 $toTalk = $toTitle->getTalkPage();
74
75 if ( $toTitle->getNamespace() == NS_FILE
76 && !RepoGroup::singleton()->getLocalRepo()->findFile( $toTitle )
77 && wfFindFile( $toTitle ) )
78 {
79 if ( !$params['ignorewarnings'] && $wgUser->isAllowed( 'reupload-shared' ) ) {
80 $this->dieUsageMsg( array( 'sharedfile-exists' ) );
81 } elseif ( !$wgUser->isAllowed( 'reupload-shared' ) ) {
82 $this->dieUsageMsg( array( 'cantoverwrite-sharedfile' ) );
83 }
84 }
85
86 // Move the page
87 $hookErr = null;
88 $retval = $fromTitle->moveTo( $toTitle, true, $params['reason'], !$params['noredirect'] );
89 if ( $retval !== true ) {
90 $this->dieUsageMsg( reset( $retval ) );
91 }
92
93 $r = array( 'from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason'] );
94 if ( !$params['noredirect'] || !$wgUser->isAllowed( 'suppressredirect' ) )
95 {
96 $r['redirectcreated'] = '';
97 }
98
99 // Move the talk page
100 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() )
101 {
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 } else {
107 // We're not gonna dieUsage() on failure, since we already changed something
108 $parsed = $this->parseMsg( reset( $retval ) );
109 $r['talkmove-error-code'] = $parsed['code'];
110 $r['talkmove-error-info'] = $parsed['info'];
111 }
112 }
113
114 // Move subpages
115 if ( $params['movesubpages'] ) {
116 $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle,
117 $params['reason'], $params['noredirect'] );
118 $this->getResult()->setIndexedTagName( $r['subpages'], 'subpage' );
119 if ( $params['movetalk'] ) {
120 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
121 $params['reason'], $params['noredirect'] );
122 $this->getResult()->setIndexedTagName( $r['subpages-talk'], 'subpage' );
123 }
124 }
125
126 $watch = "preferences";
127 if ( isset( $params['watchlist'] ) ) {
128 $watch = $params['watchlist'];
129 } elseif ( $params['watch'] ) {
130 $watch = 'watch';
131 } elseif ( $params['unwatch'] ) {
132 $watch = 'unwatch';
133 }
134
135 // Watch pages
136 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
137 $this->setWatch( $watch, $toTitle, 'watchmoves' );
138
139 $this->getResult()->addValue( null, $this->getModuleName(), $r );
140 }
141
142 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect ) {
143 $retval = array();
144 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
145 if ( isset( $success[0] ) ) {
146 return array( 'error' => $this->parseMsg( $success ) );
147 } else {
148 // At least some pages could be moved
149 // Report each of them separately
150 foreach ( $success as $oldTitle => $newTitle ) {
151 $r = array( 'from' => $oldTitle );
152 if ( is_array( $newTitle ) ) {
153 $r['error'] = $this->parseMsg( reset( $newTitle ) );
154 } else {
155 // Success
156 $r['to'] = $newTitle;
157 }
158 $retval[] = $r;
159 }
160 }
161 return $retval;
162 }
163
164 public function mustBePosted() {
165 return true;
166 }
167
168 public function isWriteMode() {
169 return true;
170 }
171
172 public function getAllowedParams() {
173 return array(
174 'from' => null,
175 'fromid' => array(
176 ApiBase::PARAM_TYPE => 'integer'
177 ),
178 'to' => null,
179 'token' => null,
180 'reason' => null,
181 'movetalk' => false,
182 'movesubpages' => false,
183 'noredirect' => false,
184 'watch' => array(
185 ApiBase::PARAM_DFLT => false,
186 ApiBase::PARAM_DEPRECATED => true,
187 ),
188 'unwatch' => array(
189 ApiBase::PARAM_DFLT => false,
190 ApiBase::PARAM_DEPRECATED => true,
191 ),
192 'watchlist' => array(
193 ApiBase::PARAM_DFLT => 'preferences',
194 ApiBase::PARAM_TYPE => array(
195 'watch',
196 'unwatch',
197 'preferences',
198 'nochange'
199 ),
200 ),
201 'ignorewarnings' => false
202 );
203 }
204
205 public function getParamDescription() {
206 $p = $this->getModulePrefix();
207 return array(
208 'from' => "Title of the page you want to move. Cannot be used together with {$p}fromid",
209 'fromid' => "Page ID of the page you want to move. Cannot be used together with {$p}from",
210 'to' => 'Title you want to rename the page to',
211 'token' => 'A move token previously retrieved through prop=info',
212 'reason' => 'Reason for the move (optional)',
213 'movetalk' => 'Move the talk page, if it exists',
214 'movesubpages' => 'Move subpages, if applicable',
215 'noredirect' => 'Don\'t create a redirect',
216 'watch' => 'Add the page and the redirect to your watchlist',
217 'unwatch' => 'Remove the page and the redirect from your watchlist',
218 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
219 'ignorewarnings' => 'Ignore any warnings'
220 );
221 }
222
223 public function getDescription() {
224 return 'Move a page';
225 }
226
227 public function getPossibleErrors() {
228 return array_merge( parent::getPossibleErrors(), array(
229 array( 'missingparam', 'to' ),
230 array( 'invalidtitle', 'from' ),
231 array( 'nosuchpageid', 'fromid' ),
232 array( 'notanarticle' ),
233 array( 'invalidtitle', 'to' ),
234 array( 'sharedfile-exists' ),
235 ) );
236 }
237
238 public function getTokenSalt() {
239 return '';
240 }
241
242 protected function getExamples() {
243 return array(
244 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk&noredirect'
245 );
246 }
247
248 public function getVersion() {
249 return __CLASS__ . ': $Id$';
250 }
251 }