Merge "Avoid use of rollback() in WikiPage::doEditContent()"
[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 /**
28 * API Module to move pages
29 * @ingroup API
30 */
31 class ApiMove extends ApiBase {
32
33 public function execute() {
34 $this->useTransactionalTimeLimit();
35
36 $user = $this->getUser();
37 $params = $this->extractRequestParams();
38
39 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
40
41 if ( isset( $params['from'] ) ) {
42 $fromTitle = Title::newFromText( $params['from'] );
43 if ( !$fromTitle || $fromTitle->isExternal() ) {
44 $this->dieUsageMsg( array( 'invalidtitle', $params['from'] ) );
45 }
46 } elseif ( isset( $params['fromid'] ) ) {
47 $fromTitle = Title::newFromID( $params['fromid'] );
48 if ( !$fromTitle ) {
49 $this->dieUsageMsg( array( 'nosuchpageid', $params['fromid'] ) );
50 }
51 }
52
53 if ( !$fromTitle->exists() ) {
54 $this->dieUsageMsg( 'notanarticle' );
55 }
56 $fromTalk = $fromTitle->getTalkPage();
57
58 $toTitle = Title::newFromText( $params['to'] );
59 if ( !$toTitle || $toTitle->isExternal() ) {
60 $this->dieUsageMsg( array( 'invalidtitle', $params['to'] ) );
61 }
62 $toTalk = $toTitle->getTalkPage();
63
64 if ( $toTitle->getNamespace() == NS_FILE
65 && !RepoGroup::singleton()->getLocalRepo()->findFile( $toTitle )
66 && wfFindFile( $toTitle )
67 ) {
68 if ( !$params['ignorewarnings'] && $user->isAllowed( 'reupload-shared' ) ) {
69 $this->dieUsageMsg( 'sharedfile-exists' );
70 } elseif ( !$user->isAllowed( 'reupload-shared' ) ) {
71 $this->dieUsageMsg( 'cantoverwrite-sharedfile' );
72 }
73 }
74
75 // Move the page
76 $toTitleExists = $toTitle->exists();
77 $status = $this->movePage( $fromTitle, $toTitle, $params['reason'], !$params['noredirect'] );
78 if ( !$status->isOK() ) {
79 $this->dieStatus( $status );
80 }
81
82 $r = array(
83 'from' => $fromTitle->getPrefixedText(),
84 'to' => $toTitle->getPrefixedText(),
85 'reason' => $params['reason']
86 );
87
88 // NOTE: we assume that if the old title exists, it's because it was re-created as
89 // a redirect to the new title. This is not safe, but what we did before was
90 // even worse: we just determined whether a redirect should have been created,
91 // and reported that it was created if it should have, without any checks.
92 // Also note that isRedirect() is unreliable because of bug 37209.
93 $r['redirectcreated'] = $fromTitle->exists();
94
95 $r['moveoverredirect'] = $toTitleExists;
96
97 // Move the talk page
98 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
99 $toTalkExists = $toTalk->exists();
100 $status = $this->movePage( $fromTalk, $toTalk, $params['reason'], !$params['noredirect'] );
101 if ( $status->isOK() ) {
102 $r['talkfrom'] = $fromTalk->getPrefixedText();
103 $r['talkto'] = $toTalk->getPrefixedText();
104 $r['talkmoveoverredirect'] = $toTalkExists;
105 } else {
106 // We're not gonna dieUsage() on failure, since we already changed something
107 $error = $this->getErrorFromStatus( $status );
108 $r['talkmove-error-code'] = $error[0];
109 $r['talkmove-error-info'] = $error[1];
110 }
111 }
112
113 $result = $this->getResult();
114
115 // Move subpages
116 if ( $params['movesubpages'] ) {
117 $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle,
118 $params['reason'], $params['noredirect'] );
119 ApiResult::setIndexedTagName( $r['subpages'], 'subpage' );
120
121 if ( $params['movetalk'] ) {
122 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
123 $params['reason'], $params['noredirect'] );
124 ApiResult::setIndexedTagName( $r['subpages-talk'], 'subpage' );
125 }
126 }
127
128 $watch = 'preferences';
129 if ( isset( $params['watchlist'] ) ) {
130 $watch = $params['watchlist'];
131 } elseif ( $params['watch'] ) {
132 $watch = 'watch';
133 } elseif ( $params['unwatch'] ) {
134 $watch = 'unwatch';
135 }
136
137 // Watch pages
138 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
139 $this->setWatch( $watch, $toTitle, 'watchmoves' );
140
141 $result->addValue( null, $this->getModuleName(), $r );
142 }
143
144 /**
145 * @param Title $from
146 * @param Title $to
147 * @param string $reason
148 * @param bool $createRedirect
149 * @return Status
150 */
151 protected function movePage( Title $from, Title $to, $reason, $createRedirect ) {
152 $mp = new MovePage( $from, $to );
153 $valid = $mp->isValidMove();
154 if ( !$valid->isOK() ) {
155 return $valid;
156 }
157
158 $permStatus = $mp->checkPermissions( $this->getUser(), $reason );
159 if ( !$permStatus->isOK() ) {
160 return $permStatus;
161 }
162
163 // Check suppressredirect permission
164 if ( !$this->getUser()->isAllowed( 'suppressredirect' ) ) {
165 $createRedirect = true;
166 }
167
168 return $mp->move( $this->getUser(), $reason, $createRedirect );
169 }
170
171 /**
172 * @param Title $fromTitle
173 * @param Title $toTitle
174 * @param string $reason
175 * @param bool $noredirect
176 * @return array
177 */
178 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect ) {
179 $retval = array();
180 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
181 if ( isset( $success[0] ) ) {
182 return array( 'error' => $this->parseMsg( $success ) );
183 }
184
185 // At least some pages could be moved
186 // Report each of them separately
187 foreach ( $success as $oldTitle => $newTitle ) {
188 $r = array( 'from' => $oldTitle );
189 if ( is_array( $newTitle ) ) {
190 $r['error'] = $this->parseMsg( reset( $newTitle ) );
191 } else {
192 // Success
193 $r['to'] = $newTitle;
194 }
195 $retval[] = $r;
196 }
197
198 return $retval;
199 }
200
201 public function mustBePosted() {
202 return true;
203 }
204
205 public function isWriteMode() {
206 return true;
207 }
208
209 public function getAllowedParams() {
210 return array(
211 'from' => null,
212 'fromid' => array(
213 ApiBase::PARAM_TYPE => 'integer'
214 ),
215 'to' => array(
216 ApiBase::PARAM_TYPE => 'string',
217 ApiBase::PARAM_REQUIRED => true
218 ),
219 'reason' => '',
220 'movetalk' => false,
221 'movesubpages' => false,
222 'noredirect' => false,
223 'watch' => array(
224 ApiBase::PARAM_DFLT => false,
225 ApiBase::PARAM_DEPRECATED => true,
226 ),
227 'unwatch' => array(
228 ApiBase::PARAM_DFLT => false,
229 ApiBase::PARAM_DEPRECATED => true,
230 ),
231 'watchlist' => array(
232 ApiBase::PARAM_DFLT => 'preferences',
233 ApiBase::PARAM_TYPE => array(
234 'watch',
235 'unwatch',
236 'preferences',
237 'nochange'
238 ),
239 ),
240 'ignorewarnings' => false
241 );
242 }
243
244 public function needsToken() {
245 return 'csrf';
246 }
247
248 protected function getExamplesMessages() {
249 return array(
250 'action=move&from=Badtitle&to=Goodtitle&token=123ABC&' .
251 'reason=Misspelled%20title&movetalk=&noredirect='
252 => 'apihelp-move-example-move',
253 );
254 }
255
256 public function getHelpUrls() {
257 return 'https://www.mediawiki.org/wiki/API:Move';
258 }
259 }