MediaWiki  REL1_19
ApiMove.php
Go to the documentation of this file.
00001 <?php
00031 class ApiMove extends ApiBase {
00032 
00033         public function __construct( $main, $action ) {
00034                 parent::__construct( $main, $action );
00035         }
00036 
00037         public function execute() {
00038                 $user = $this->getUser();
00039                 $params = $this->extractRequestParams();
00040                 if ( is_null( $params['reason'] ) ) {
00041                         $params['reason'] = '';
00042                 }
00043 
00044                 $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
00045 
00046                 if ( isset( $params['from'] ) ) {
00047                         $fromTitle = Title::newFromText( $params['from'] );
00048                         if ( !$fromTitle ) {
00049                                 $this->dieUsageMsg( array( 'invalidtitle', $params['from'] ) );
00050                         }
00051                 } elseif ( isset( $params['fromid'] ) ) {
00052                         $fromTitle = Title::newFromID( $params['fromid'] );
00053                         if ( !$fromTitle ) {
00054                                 $this->dieUsageMsg( array( 'nosuchpageid', $params['fromid'] ) );
00055                         }
00056                 }
00057 
00058                 if ( !$fromTitle->exists() ) {
00059                         $this->dieUsageMsg( 'notanarticle' );
00060                 }
00061                 $fromTalk = $fromTitle->getTalkPage();
00062 
00063                 $toTitle = Title::newFromText( $params['to'] );
00064                 if ( !$toTitle ) {
00065                         $this->dieUsageMsg( array( 'invalidtitle', $params['to'] ) );
00066                 }
00067                 $toTalk = $toTitle->getTalkPage();
00068 
00069                 if ( $toTitle->getNamespace() == NS_FILE
00070                         && !RepoGroup::singleton()->getLocalRepo()->findFile( $toTitle )
00071                         && wfFindFile( $toTitle ) )
00072                 {
00073                         if ( !$params['ignorewarnings'] && $user->isAllowed( 'reupload-shared' ) ) {
00074                                 $this->dieUsageMsg( 'sharedfile-exists' );
00075                         } elseif ( !$user->isAllowed( 'reupload-shared' ) ) {
00076                                 $this->dieUsageMsg( 'cantoverwrite-sharedfile' );
00077                         }
00078                 }
00079 
00080                 // Move the page
00081                 $retval = $fromTitle->moveTo( $toTitle, true, $params['reason'], !$params['noredirect'] );
00082                 if ( $retval !== true ) {
00083                         $this->dieUsageMsg( reset( $retval ) );
00084                 }
00085 
00086                 $r = array( 'from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason'] );
00087                 if ( !$params['noredirect'] || !$user->isAllowed( 'suppressredirect' ) ) {
00088                         $r['redirectcreated'] = '';
00089                 }
00090 
00091                 // Move the talk page
00092                 if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
00093                         $retval = $fromTalk->moveTo( $toTalk, true, $params['reason'], !$params['noredirect'] );
00094                         if ( $retval === true ) {
00095                                 $r['talkfrom'] = $fromTalk->getPrefixedText();
00096                                 $r['talkto'] = $toTalk->getPrefixedText();
00097                         } else {
00098                                 // We're not gonna dieUsage() on failure, since we already changed something
00099                                 $parsed = $this->parseMsg( reset( $retval ) );
00100                                 $r['talkmove-error-code'] = $parsed['code'];
00101                                 $r['talkmove-error-info'] = $parsed['info'];
00102                         }
00103                 }
00104 
00105                 $result = $this->getResult();
00106 
00107                 // Move subpages
00108                 if ( $params['movesubpages'] ) {
00109                         $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle,
00110                                         $params['reason'], $params['noredirect'] );
00111                         $result->setIndexedTagName( $r['subpages'], 'subpage' );
00112 
00113                         if ( $params['movetalk'] ) {
00114                                 $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
00115                                         $params['reason'], $params['noredirect'] );
00116                                 $result->setIndexedTagName( $r['subpages-talk'], 'subpage' );
00117                         }
00118                 }
00119 
00120                 $watch = "preferences";
00121                 if ( isset( $params['watchlist'] ) ) {
00122                         $watch = $params['watchlist'];
00123                 } elseif ( $params['watch'] ) {
00124                         $watch = 'watch';
00125                 } elseif ( $params['unwatch'] ) {
00126                         $watch = 'unwatch';
00127                 }
00128 
00129                 // Watch pages
00130                 $this->setWatch( $watch, $fromTitle, 'watchmoves' );
00131                 $this->setWatch( $watch, $toTitle, 'watchmoves' );
00132 
00133                 $result->addValue( null, $this->getModuleName(), $r );
00134         }
00135 
00143         public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect ) {
00144                 $retval = array();
00145                 $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
00146                 if ( isset( $success[0] ) ) {
00147                         return array( 'error' => $this->parseMsg( $success ) );
00148                 } else {
00149                         // At least some pages could be moved
00150                         // Report each of them separately
00151                         foreach ( $success as $oldTitle => $newTitle ) {
00152                                 $r = array( 'from' => $oldTitle );
00153                                 if ( is_array( $newTitle ) ) {
00154                                         $r['error'] = $this->parseMsg( reset( $newTitle ) );
00155                                 } else {
00156                                         // Success
00157                                         $r['to'] = $newTitle;
00158                                 }
00159                                 $retval[] = $r;
00160                         }
00161                 }
00162                 return $retval;
00163         }
00164 
00165         public function mustBePosted() {
00166                 return true;
00167         }
00168 
00169         public function isWriteMode() {
00170                 return true;
00171         }
00172 
00173         public function getAllowedParams() {
00174                 return array(
00175                         'from' => null,
00176                         'fromid' => array(
00177                                 ApiBase::PARAM_TYPE => 'integer'
00178                         ),
00179                         'to' => array(
00180                                 ApiBase::PARAM_TYPE => 'string',
00181                                 ApiBase::PARAM_REQUIRED => true
00182                         ),
00183                         'token' => null,
00184                         'reason' => null,
00185                         'movetalk' => false,
00186                         'movesubpages' => false,
00187                         'noredirect' => false,
00188                         'watch' => array(
00189                                 ApiBase::PARAM_DFLT => false,
00190                                 ApiBase::PARAM_DEPRECATED => true,
00191                         ),
00192                         'unwatch' => array(
00193                                 ApiBase::PARAM_DFLT => false,
00194                                 ApiBase::PARAM_DEPRECATED => true,
00195                         ),
00196                         'watchlist' => array(
00197                                 ApiBase::PARAM_DFLT => 'preferences',
00198                                 ApiBase::PARAM_TYPE => array(
00199                                         'watch',
00200                                         'unwatch',
00201                                         'preferences',
00202                                         'nochange'
00203                                 ),
00204                         ),
00205                         'ignorewarnings' => false
00206                 );
00207         }
00208 
00209         public function getParamDescription() {
00210                 $p = $this->getModulePrefix();
00211                 return array(
00212                         'from' => "Title of the page you want to move. Cannot be used together with {$p}fromid",
00213                         'fromid' => "Page ID of the page you want to move. Cannot be used together with {$p}from",
00214                         'to' => 'Title you want to rename the page to',
00215                         'token' => 'A move token previously retrieved through prop=info',
00216                         'reason' => 'Reason for the move (optional)',
00217                         'movetalk' => 'Move the talk page, if it exists',
00218                         'movesubpages' => 'Move subpages, if applicable',
00219                         'noredirect' => 'Don\'t create a redirect',
00220                         'watch' => 'Add the page and the redirect to your watchlist',
00221                         'unwatch' => 'Remove the page and the redirect from your watchlist',
00222                         'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
00223                         'ignorewarnings' => 'Ignore any warnings'
00224                 );
00225         }
00226 
00227         public function getDescription() {
00228                 return 'Move a page';
00229         }
00230 
00231         public function getPossibleErrors() {
00232                 return array_merge( parent::getPossibleErrors(),
00233                         $this->getRequireOnlyOneParameterErrorMessages( array( 'from', 'fromid' ) ),
00234                         array(
00235                                 array( 'invalidtitle', 'from' ),
00236                                 array( 'nosuchpageid', 'fromid' ),
00237                                 array( 'notanarticle' ),
00238                                 array( 'invalidtitle', 'to' ),
00239                                 array( 'sharedfile-exists' ),
00240                         )
00241                 );
00242         }
00243 
00244         public function needsToken() {
00245                 return true;
00246         }
00247 
00248         public function getTokenSalt() {
00249                 return '';
00250         }
00251 
00252         public function getExamples() {
00253                 return array(
00254                         'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk=&noredirect='
00255                 );
00256         }
00257 
00258         public function getHelpUrls() {
00259                 return 'https://www.mediawiki.org/wiki/API:Move';
00260         }
00261 
00262         public function getVersion() {
00263                 return __CLASS__ . ': $Id$';
00264         }
00265 }