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
00050 if ( isset( $params['from'] ) )
00051 {
00052 $fromTitle = Title::newFromText( $params['from'] );
00053 if ( !$fromTitle )
00054 $this->dieUsageMsg( array( 'invalidtitle', $params['from'] ) );
00055 }
00056 else if ( isset( $params['fromid'] ) )
00057 {
00058 $fromTitle = Title::newFromID( $params['fromid'] );
00059 if ( !$fromTitle )
00060 $this->dieUsageMsg( array( 'nosuchpageid', $params['fromid'] ) );
00061 }
00062
00063 if ( !$fromTitle->exists() )
00064 $this->dieUsageMsg( array( 'notanarticle' ) );
00065 $fromTalk = $fromTitle->getTalkPage();
00066
00067 $toTitle = Title::newFromText( $params['to'] );
00068 if ( !$toTitle )
00069 $this->dieUsageMsg( array( 'invalidtitle', $params['to'] ) );
00070 $toTalk = $toTitle->getTalkPage();
00071
00072 if ( $toTitle->getNamespace() == NS_FILE
00073 && !RepoGroup::singleton()->getLocalRepo()->findFile( $toTitle )
00074 && wfFindFile( $toTitle ) )
00075 {
00076 if ( !$params['ignorewarnings'] && $wgUser->isAllowed( 'reupload-shared' ) ) {
00077 $this->dieUsageMsg( array( 'sharedfile-exists' ) );
00078 } elseif ( !$wgUser->isAllowed( 'reupload-shared' ) ) {
00079 $this->dieUsageMsg( array( 'cantoverwrite-sharedfile' ) );
00080 }
00081 }
00082
00083
00084 $hookErr = null;
00085 $retval = $fromTitle->moveTo( $toTitle, true, $params['reason'], !$params['noredirect'] );
00086 if ( $retval !== true )
00087 $this->dieUsageMsg( reset( $retval ) );
00088
00089 $r = array( 'from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason'] );
00090 if ( !$params['noredirect'] || !$wgUser->isAllowed( 'suppressredirect' ) )
00091 $r['redirectcreated'] = '';
00092
00093
00094 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() )
00095 {
00096 $retval = $fromTalk->moveTo( $toTalk, true, $params['reason'], !$params['noredirect'] );
00097 if ( $retval === true )
00098 {
00099 $r['talkfrom'] = $fromTalk->getPrefixedText();
00100 $r['talkto'] = $toTalk->getPrefixedText();
00101 }
00102
00103 else
00104 {
00105 $parsed = $this->parseMsg( reset( $retval ) );
00106 $r['talkmove-error-code'] = $parsed['code'];
00107 $r['talkmove-error-info'] = $parsed['info'];
00108 }
00109 }
00110
00111
00112 if ( $params['movesubpages'] )
00113 {
00114 $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle,
00115 $params['reason'], $params['noredirect'] );
00116 $this->getResult()->setIndexedTagName( $r['subpages'], 'subpage' );
00117 if ( $params['movetalk'] )
00118 {
00119 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
00120 $params['reason'], $params['noredirect'] );
00121 $this->getResult()->setIndexedTagName( $r['subpages-talk'], 'subpage' );
00122 }
00123 }
00124
00125
00126 if ( $params['watch'] || $wgUser->getOption( 'watchmoves' ) )
00127 {
00128 $wgUser->addWatch( $fromTitle );
00129 $wgUser->addWatch( $toTitle );
00130 }
00131 else if ( $params['unwatch'] )
00132 {
00133 $wgUser->removeWatch( $fromTitle );
00134 $wgUser->removeWatch( $toTitle );
00135 }
00136 $this->getResult()->addValue( null, $this->getModuleName(), $r );
00137 }
00138
00139 public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect )
00140 {
00141 $retval = array();
00142 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
00143 if ( isset( $success[0] ) )
00144 return array( 'error' => $this->parseMsg( $success ) );
00145 else
00146 {
00147
00148
00149 foreach ( $success as $oldTitle => $newTitle )
00150 {
00151 $r = array( 'from' => $oldTitle );
00152 if ( is_array( $newTitle ) )
00153 $r['error'] = $this->parseMsg( reset( $newTitle ) );
00154 else
00155
00156 $r['to'] = $newTitle;
00157 $retval[] = $r;
00158 }
00159 }
00160 return $retval;
00161 }
00162
00163 public function mustBePosted() {
00164 return true;
00165 }
00166
00167 public function isWriteMode() {
00168 return true;
00169 }
00170
00171 public function getAllowedParams() {
00172 return array (
00173 'from' => null,
00174 'fromid' => array(
00175 ApiBase::PARAM_TYPE => 'integer'
00176 ),
00177 'to' => null,
00178 'token' => null,
00179 'reason' => null,
00180 'movetalk' => false,
00181 'movesubpages' => false,
00182 'noredirect' => false,
00183 'watch' => false,
00184 'unwatch' => false,
00185 'ignorewarnings' => false
00186 );
00187 }
00188
00189 public function getParamDescription() {
00190 return array (
00191 'from' => 'Title of the page you want to move. Cannot be used together with fromid.',
00192 'fromid' => 'Page ID of the page you want to move. Cannot be used together with from.',
00193 'to' => 'Title you want to rename the page to.',
00194 'token' => 'A move token previously retrieved through prop=info',
00195 'reason' => 'Reason for the move (optional).',
00196 'movetalk' => 'Move the talk page, if it exists.',
00197 'movesubpages' => 'Move subpages, if applicable',
00198 'noredirect' => 'Don\'t create a redirect',
00199 'watch' => 'Add the page and the redirect to your watchlist',
00200 'unwatch' => 'Remove the page and the redirect from your watchlist',
00201 'ignorewarnings' => 'Ignore any warnings'
00202 );
00203 }
00204
00205 public function getDescription() {
00206 return array(
00207 'Move a page.'
00208 );
00209 }
00210
00211 public function getPossibleErrors() {
00212 return array_merge( parent::getPossibleErrors(), array(
00213 array( 'missingparam', 'to' ),
00214 array( 'invalidtitle', 'from' ),
00215 array( 'nosuchpageid', 'fromid' ),
00216 array( 'notanarticle' ),
00217 array( 'invalidtitle', 'to' ),
00218 array( 'sharedfile-exists' ),
00219 ) );
00220 }
00221
00222 public function needsToken() {
00223 return true;
00224 }
00225
00226 public function getTokenSalt() {
00227 return '';
00228 }
00229
00230 protected function getExamples() {
00231 return array (
00232 'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk&noredirect'
00233 );
00234 }
00235
00236 public function getVersion() {
00237 return __CLASS__ . ': $Id: ApiMove.php 74217 2010-10-03 15:53:07Z reedy $';
00238 }
00239 }