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