00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 if (!defined('MEDIAWIKI')) {
00026
00027 require_once ("ApiBase.php");
00028 }
00029
00030
00034 class ApiMove extends ApiBase {
00035
00036 public function __construct($main, $action) {
00037 parent :: __construct($main, $action);
00038 }
00039
00040 public function execute() {
00041 global $wgUser;
00042 $params = $this->extractRequestParams();
00043 if(is_null($params['reason']))
00044 $params['reason'] = '';
00045
00046 $this->requireOnlyOneParameter($params, 'from', 'fromid');
00047 if(!isset($params['to']))
00048 $this->dieUsageMsg(array('missingparam', 'to'));
00049 if(!isset($params['token']))
00050 $this->dieUsageMsg(array('missingparam', 'token'));
00051 if(!$wgUser->matchEditToken($params['token']))
00052 $this->dieUsageMsg(array('sessionfailure'));
00053
00054 if(isset($params['from']))
00055 {
00056 $fromTitle = Title::newFromText($params['from']);
00057 if(!$fromTitle)
00058 $this->dieUsageMsg(array('invalidtitle', $params['from']));
00059 }
00060 else if(isset($params['fromid']))
00061 {
00062 $fromTitle = Title::newFromID($params['fromid']);
00063 if(!$fromTitle)
00064 $this->dieUsageMsg(array('nosuchpageid', $params['fromid']));
00065 }
00066 if(!$fromTitle->exists())
00067 $this->dieUsageMsg(array('notanarticle'));
00068 $fromTalk = $fromTitle->getTalkPage();
00069
00070 $toTitle = Title::newFromText($params['to']);
00071 if(!$toTitle)
00072 $this->dieUsageMsg(array('invalidtitle', $params['to']));
00073 $toTalk = $toTitle->getTalkPage();
00074
00075 # Move the page
00076 $hookErr = null;
00077 $retval = $fromTitle->moveTo($toTitle, true, $params['reason'], !$params['noredirect']);
00078 if($retval !== true)
00079 $this->dieUsageMsg(reset($retval));
00080
00081 $r = array('from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason']);
00082 if(!$params['noredirect'] || !$wgUser->isAllowed('suppressredirect'))
00083 $r['redirectcreated'] = '';
00084
00085 # Move the talk page
00086 if($params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage())
00087 {
00088 $retval = $fromTalk->moveTo($toTalk, true, $params['reason'], !$params['noredirect']);
00089 if($retval === true)
00090 {
00091 $r['talkfrom'] = $fromTalk->getPrefixedText();
00092 $r['talkto'] = $toTalk->getPrefixedText();
00093 }
00094
00095 else
00096 {
00097 $parsed = $this->parseMsg(reset($retval));
00098 $r['talkmove-error-code'] = $parsed['code'];
00099 $r['talkmove-error-info'] = $parsed['info'];
00100 }
00101 }
00102
00103 # Move subpages
00104 if($params['movesubpages'])
00105 {
00106 $r['subpages'] = $this->moveSubpages($fromTitle, $toTitle,
00107 $params['reason'], $params['noredirect']);
00108 $this->getResult()->setIndexedTagName($r['subpages'], 'subpage');
00109 if($params['movetalk'])
00110 {
00111 $r['subpages-talk'] = $this->moveSubpages($fromTalk, $toTalk,
00112 $params['reason'], $params['noredirect']);
00113 $this->getResult()->setIndexedTagName($r['subpages-talk'], 'subpage');
00114 }
00115 }
00116
00117 # Watch pages
00118 if($params['watch'] || $wgUser->getOption('watchmoves'))
00119 {
00120 $wgUser->addWatch($fromTitle);
00121 $wgUser->addWatch($toTitle);
00122 }
00123 else if($params['unwatch'])
00124 {
00125 $wgUser->removeWatch($fromTitle);
00126 $wgUser->removeWatch($toTitle);
00127 }
00128 $this->getResult()->addValue(null, $this->getModuleName(), $r);
00129 }
00130
00131 public function moveSubpages($fromTitle, $toTitle, $reason, $noredirect)
00132 {
00133 $retval = array();
00134 $success = $fromTitle->moveSubpages($toTitle, true, $reason, !$noredirect);
00135 if(isset($success[0]))
00136 return array('error' => $this->parseMsg($success));
00137 else
00138 {
00139
00140
00141 foreach($success as $oldTitle => $newTitle)
00142 {
00143 $r = array('from' => $oldTitle);
00144 if(is_array($newTitle))
00145 $r['error'] = $this->parseMsg(reset($newTitle));
00146 else
00147
00148 $r['to'] = $newTitle;
00149 $retval[] = $r;
00150 }
00151 }
00152 return $retval;
00153 }
00154
00155 public function mustBePosted() { return true; }
00156
00157 public function isWriteMode() {
00158 return true;
00159 }
00160
00161 public function getAllowedParams() {
00162 return array (
00163 'from' => null,
00164 'fromid' => array(
00165 ApiBase::PARAM_TYPE => 'integer'
00166 ),
00167 'to' => null,
00168 'token' => null,
00169 'reason' => null,
00170 'movetalk' => false,
00171 'movesubpages' => false,
00172 'noredirect' => false,
00173 'watch' => false,
00174 'unwatch' => false
00175 );
00176 }
00177
00178 public function getParamDescription() {
00179 return array (
00180 'from' => 'Title of the page you want to move. Cannot be used together with fromid.',
00181 'fromid' => 'Page ID of the page you want to move. Cannot be used together with from.',
00182 'to' => 'Title you want to rename the page to.',
00183 'token' => 'A move token previously retrieved through prop=info',
00184 'reason' => 'Reason for the move (optional).',
00185 'movetalk' => 'Move the talk page, if it exists.',
00186 'movesubpages' => 'Move subpages, if applicable',
00187 'noredirect' => 'Don\'t create a redirect',
00188 'watch' => 'Add the page and the redirect to your watchlist',
00189 'unwatch' => 'Remove the page and the redirect from your watchlist'
00190 );
00191 }
00192
00193 public function getDescription() {
00194 return array(
00195 'Move a page.'
00196 );
00197 }
00198
00199 protected function getExamples() {
00200 return array (
00201 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk&noredirect'
00202 );
00203 }
00204
00205 public function getVersion() {
00206 return __CLASS__ . ': $Id: ApiMove.php 48091 2009-03-06 13:49:44Z catrope $';
00207 }
00208 }