66bf71bdda6cc6638bcc1486221196971c38117c
[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 (C) 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 /**
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 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
47 if ( !isset( $params['to'] ) )
48 $this->dieUsageMsg( array( 'missingparam', 'to' ) );
49
50 if ( isset( $params['from'] ) )
51 {
52 $fromTitle = Title::newFromText( $params['from'] );
53 if ( !$fromTitle )
54 $this->dieUsageMsg( array( 'invalidtitle', $params['from'] ) );
55 }
56 else if ( isset( $params['fromid'] ) )
57 {
58 $fromTitle = Title::newFromID( $params['fromid'] );
59 if ( !$fromTitle )
60 $this->dieUsageMsg( array( 'nosuchpageid', $params['fromid'] ) );
61 }
62
63 if ( !$fromTitle->exists() )
64 $this->dieUsageMsg( array( 'notanarticle' ) );
65 $fromTalk = $fromTitle->getTalkPage();
66
67 $toTitle = Title::newFromText( $params['to'] );
68 if ( !$toTitle )
69 $this->dieUsageMsg( array( 'invalidtitle', $params['to'] ) );
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 $hookErr = null;
85 $retval = $fromTitle->moveTo( $toTitle, true, $params['reason'], !$params['noredirect'] );
86 if ( $retval !== true )
87 $this->dieUsageMsg( reset( $retval ) );
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 // Move the talk page
94 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() )
95 {
96 $retval = $fromTalk->moveTo( $toTalk, true, $params['reason'], !$params['noredirect'] );
97 if ( $retval === true )
98 {
99 $r['talkfrom'] = $fromTalk->getPrefixedText();
100 $r['talkto'] = $toTalk->getPrefixedText();
101 }
102 // We're not gonna dieUsage() on failure, since we already changed something
103 else
104 {
105 $parsed = $this->parseMsg( reset( $retval ) );
106 $r['talkmove-error-code'] = $parsed['code'];
107 $r['talkmove-error-info'] = $parsed['info'];
108 }
109 }
110
111 // Move subpages
112 if ( $params['movesubpages'] )
113 {
114 $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle,
115 $params['reason'], $params['noredirect'] );
116 $this->getResult()->setIndexedTagName( $r['subpages'], 'subpage' );
117 if ( $params['movetalk'] )
118 {
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 if ( $params['watch'] || $wgUser->getOption( 'watchmoves' ) )
127 {
128 $wgUser->addWatch( $fromTitle );
129 $wgUser->addWatch( $toTitle );
130 }
131 else if ( $params['unwatch'] )
132 {
133 $wgUser->removeWatch( $fromTitle );
134 $wgUser->removeWatch( $toTitle );
135 }
136 $this->getResult()->addValue( null, $this->getModuleName(), $r );
137 }
138
139 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect )
140 {
141 $retval = array();
142 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
143 if ( isset( $success[0] ) )
144 return array( 'error' => $this->parseMsg( $success ) );
145 else
146 {
147 // At least some pages could be moved
148 // Report each of them separately
149 foreach ( $success as $oldTitle => $newTitle )
150 {
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 $retval[] = $r;
158 }
159 }
160 return $retval;
161 }
162
163 public function mustBePosted() {
164 return true;
165 }
166
167 public function isWriteMode() {
168 return true;
169 }
170
171 public function getAllowedParams() {
172 return array (
173 'from' => null,
174 'fromid' => array(
175 ApiBase::PARAM_TYPE => 'integer'
176 ),
177 'to' => null,
178 'token' => null,
179 'reason' => null,
180 'movetalk' => false,
181 'movesubpages' => false,
182 'noredirect' => false,
183 'watch' => false,
184 'unwatch' => false,
185 'ignorewarnings' => false
186 );
187 }
188
189 public function getParamDescription() {
190 return array (
191 'from' => 'Title of the page you want to move. Cannot be used together with fromid.',
192 'fromid' => 'Page ID of the page you want to move. Cannot be used together with from.',
193 'to' => 'Title you want to rename the page to.',
194 'token' => 'A move token previously retrieved through prop=info',
195 'reason' => 'Reason for the move (optional).',
196 'movetalk' => 'Move the talk page, if it exists.',
197 'movesubpages' => 'Move subpages, if applicable',
198 'noredirect' => 'Don\'t create a redirect',
199 'watch' => 'Add the page and the redirect to your watchlist',
200 'unwatch' => 'Remove the page and the redirect from your watchlist',
201 'ignorewarnings' => 'Ignore any warnings'
202 );
203 }
204
205 public function getDescription() {
206 return array(
207 'Move a page.'
208 );
209 }
210
211 public function getPossibleErrors() {
212 return array_merge( parent::getPossibleErrors(), array(
213 array( 'missingparam', 'to' ),
214 array( 'invalidtitle', 'from' ),
215 array( 'nosuchpageid', 'fromid' ),
216 array( 'notanarticle' ),
217 array( 'invalidtitle', 'to' ),
218 array( 'sharedfile-exists' ),
219 ) );
220 }
221
222 public function getTokenSalt() {
223 return null;
224 }
225
226 protected function getExamples() {
227 return array (
228 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk&noredirect'
229 );
230 }
231
232 public function getVersion() {
233 return __CLASS__ . ': $Id$';
234 }
235 }