removed x-codeBlob functions and modified blob handling acordingly
[mediawiki.git] / includes / api / ApiMove.php
blobeaa2e1673d1e8170983218cfb141e903c40cb070
1 <?php
3 /*
4 * Created on Oct 31, 2007
5 * API for MediaWiki 1.8+
7 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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.
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.
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
25 if (!defined('MEDIAWIKI')) {
26 // Eclipse helper - will be ignored in production
27 require_once ("ApiBase.php");
31 /**
32 * @ingroup API
34 class ApiMove extends ApiBase {
36 public function __construct($main, $action) {
37 parent :: __construct($main, $action);
40 public function execute() {
41 global $wgUser;
42 $params = $this->extractRequestParams();
43 if(is_null($params['reason']))
44 $params['reason'] = '';
46 $this->requireOnlyOneParameter($params, 'from', 'fromid');
47 if(!isset($params['to']))
48 $this->dieUsageMsg(array('missingparam', 'to'));
49 if(!isset($params['token']))
50 $this->dieUsageMsg(array('missingparam', 'token'));
51 if(!$wgUser->matchEditToken($params['token']))
52 $this->dieUsageMsg(array('sessionfailure'));
54 if(isset($params['from']))
56 $fromTitle = Title::newFromText($params['from']);
57 if(!$fromTitle)
58 $this->dieUsageMsg(array('invalidtitle', $params['from']));
60 else if(isset($params['fromid']))
62 $fromTitle = Title::newFromID($params['fromid']);
63 if(!$fromTitle)
64 $this->dieUsageMsg(array('nosuchpageid', $params['fromid']));
66 if(!$fromTitle->exists())
67 $this->dieUsageMsg(array('notanarticle'));
68 $fromTalk = $fromTitle->getTalkPage();
70 $toTitle = Title::newFromText($params['to']);
71 if(!$toTitle)
72 $this->dieUsageMsg(array('invalidtitle', $params['to']));
73 $toTalk = $toTitle->getTalkPage();
75 if ( $toTitle->getNamespace() == NS_FILE
76 && !RepoGroup::singleton()->getLocalRepo()->findFile( $toTitle )
77 && wfFindFile( $toTitle ) )
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'));
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));
92 $r = array('from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason']);
93 if(!$params['noredirect'] || !$wgUser->isAllowed('suppressredirect'))
94 $r['redirectcreated'] = '';
96 # Move the talk page
97 if($params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage())
99 $retval = $fromTalk->moveTo($toTalk, true, $params['reason'], !$params['noredirect']);
100 if($retval === true)
102 $r['talkfrom'] = $fromTalk->getPrefixedText();
103 $r['talkto'] = $toTalk->getPrefixedText();
105 // We're not gonna dieUsage() on failure, since we already changed something
106 else
108 $parsed = $this->parseMsg(reset($retval));
109 $r['talkmove-error-code'] = $parsed['code'];
110 $r['talkmove-error-info'] = $parsed['info'];
114 # Move subpages
115 if($params['movesubpages'])
117 $r['subpages'] = $this->moveSubpages($fromTitle, $toTitle,
118 $params['reason'], $params['noredirect']);
119 $this->getResult()->setIndexedTagName($r['subpages'], 'subpage');
120 if($params['movetalk'])
122 $r['subpages-talk'] = $this->moveSubpages($fromTalk, $toTalk,
123 $params['reason'], $params['noredirect']);
124 $this->getResult()->setIndexedTagName($r['subpages-talk'], 'subpage');
128 # Watch pages
129 if($params['watch'] || $wgUser->getOption('watchmoves'))
131 $wgUser->addWatch($fromTitle);
132 $wgUser->addWatch($toTitle);
134 else if($params['unwatch'])
136 $wgUser->removeWatch($fromTitle);
137 $wgUser->removeWatch($toTitle);
139 $this->getResult()->addValue(null, $this->getModuleName(), $r);
142 public function moveSubpages($fromTitle, $toTitle, $reason, $noredirect)
144 $retval = array();
145 $success = $fromTitle->moveSubpages($toTitle, true, $reason, !$noredirect);
146 if(isset($success[0]))
147 return array('error' => $this->parseMsg($success));
148 else
150 // At least some pages could be moved
151 // Report each of them separately
152 foreach($success as $oldTitle => $newTitle)
154 $r = array('from' => $oldTitle);
155 if(is_array($newTitle))
156 $r['error'] = $this->parseMsg(reset($newTitle));
157 else
158 // Success
159 $r['to'] = $newTitle;
160 $retval[] = $r;
163 return $retval;
166 public function mustBePosted() { return true; }
168 public function isWriteMode() {
169 return true;
172 public function getAllowedParams() {
173 return array (
174 'from' => null,
175 'fromid' => array(
176 ApiBase::PARAM_TYPE => 'integer'
178 'to' => null,
179 'token' => null,
180 'reason' => null,
181 'movetalk' => false,
182 'movesubpages' => false,
183 'noredirect' => false,
184 'watch' => false,
185 'unwatch' => false,
186 'ignorewarnings' => false
190 public function getParamDescription() {
191 return array (
192 'from' => 'Title of the page you want to move. Cannot be used together with fromid.',
193 'fromid' => 'Page ID of the page you want to move. Cannot be used together with from.',
194 'to' => 'Title you want to rename the page to.',
195 'token' => 'A move token previously retrieved through prop=info',
196 'reason' => 'Reason for the move (optional).',
197 'movetalk' => 'Move the talk page, if it exists.',
198 'movesubpages' => 'Move subpages, if applicable',
199 'noredirect' => 'Don\'t create a redirect',
200 'watch' => 'Add the page and the redirect to your watchlist',
201 'unwatch' => 'Remove the page and the redirect from your watchlist',
202 'ignorewarnings' => 'Ignore any warnings'
206 public function getDescription() {
207 return array(
208 'Move a page.'
212 protected function getExamples() {
213 return array (
214 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk&noredirect'
218 public function getVersion() {
219 return __CLASS__ . ': $Id$';