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