]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blobdiff - includes/api/ApiMove.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / includes / api / ApiMove.php
index c234f084d97ed095a487506152ec20b2243a1dda..e7b280803e70475e933a22b8ea0dec76a809d5ed 100644 (file)
@@ -1,10 +1,10 @@
 <?php
-
-/*
+/**
+ *
+ *
  * Created on Oct 31, 2007
- * API for MediaWiki 1.8+
  *
- * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
+ * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * You should have received a copy of the GNU General Public License along
  * with this program; if not, write to the Free Software Foundation, Inc.,
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
  */
 
-if ( !defined( 'MEDIAWIKI' ) ) {
-       // Eclipse helper - will be ignored in production
-       require_once ( "ApiBase.php" );
-}
-
-
 /**
+ * API Module to move pages
  * @ingroup API
  */
 class ApiMove extends ApiBase {
 
-       public function __construct( $main, $action ) {
-               parent :: __construct( $main, $action );
-       }
-
        public function execute() {
-               global $wgUser;
+               $this->useTransactionalTimeLimit();
+
+               $user = $this->getUser();
                $params = $this->extractRequestParams();
-               if ( is_null( $params['reason'] ) )
-                       $params['reason'] = '';
 
                $this->requireOnlyOneParameter( $params, 'from', 'fromid' );
-               if ( !isset( $params['to'] ) )
-                       $this->dieUsageMsg( array( 'missingparam', 'to' ) );
 
-               if ( isset( $params['from'] ) )
-               {
+               if ( isset( $params['from'] ) ) {
                        $fromTitle = Title::newFromText( $params['from'] );
-                       if ( !$fromTitle )
-                               $this->dieUsageMsg( array( 'invalidtitle', $params['from'] ) );
-               }
-               else if ( isset( $params['fromid'] ) )
-               {
+                       if ( !$fromTitle || $fromTitle->isExternal() ) {
+                               $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['from'] ) ] );
+                       }
+               } elseif ( isset( $params['fromid'] ) ) {
                        $fromTitle = Title::newFromID( $params['fromid'] );
-                       if ( !$fromTitle )
-                               $this->dieUsageMsg( array( 'nosuchpageid', $params['fromid'] ) );
+                       if ( !$fromTitle ) {
+                               $this->dieWithError( [ 'apierror-nosuchpageid', $params['fromid'] ] );
+                       }
                }
 
-               if ( !$fromTitle->exists() )
-                       $this->dieUsageMsg( array( 'notanarticle' ) );
+               if ( !$fromTitle->exists() ) {
+                       $this->dieWithError( 'apierror-missingtitle' );
+               }
                $fromTalk = $fromTitle->getTalkPage();
 
                $toTitle = Title::newFromText( $params['to'] );
-               if ( !$toTitle )
-                       $this->dieUsageMsg( array( 'invalidtitle', $params['to'] ) );
-               $toTalk = $toTitle->getTalkPage();
+               if ( !$toTitle || $toTitle->isExternal() ) {
+                       $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $params['to'] ) ] );
+               }
+               $toTalk = $toTitle->getTalkPageIfDefined();
 
                if ( $toTitle->getNamespace() == NS_FILE
                        && !RepoGroup::singleton()->getLocalRepo()->findFile( $toTitle )
-                       && wfFindFile( $toTitle ) )
-               {
-                       if ( !$params['ignorewarnings'] && $wgUser->isAllowed( 'reupload-shared' ) ) {
-                               $this->dieUsageMsg( array( 'sharedfile-exists' ) );
-                       } elseif ( !$wgUser->isAllowed( 'reupload-shared' ) ) {
-                               $this->dieUsageMsg( array( 'cantoverwrite-sharedfile' ) );
+                       && wfFindFile( $toTitle )
+               ) {
+                       if ( !$params['ignorewarnings'] && $user->isAllowed( 'reupload-shared' ) ) {
+                               $this->dieWithError( 'apierror-fileexists-sharedrepo-perm' );
+                       } elseif ( !$user->isAllowed( 'reupload-shared' ) ) {
+                               $this->dieWithError( 'apierror-cantoverwrite-sharedfile' );
+                       }
+               }
+
+               // Rate limit
+               if ( $user->pingLimiter( 'move' ) ) {
+                       $this->dieWithError( 'apierror-ratelimited' );
+               }
+
+               // Check if the user is allowed to add the specified changetags
+               if ( $params['tags'] ) {
+                       $ableToTag = ChangeTags::canAddTagsAccompanyingChange( $params['tags'], $user );
+                       if ( !$ableToTag->isOK() ) {
+                               $this->dieStatus( $ableToTag );
                        }
                }
 
                // Move the page
-               $hookErr = null;
-               $retval = $fromTitle->moveTo( $toTitle, true, $params['reason'], !$params['noredirect'] );
-               if ( $retval !== true )
-                       $this->dieUsageMsg( reset( $retval ) );
+               $toTitleExists = $toTitle->exists();
+               $status = $this->movePage( $fromTitle, $toTitle, $params['reason'], !$params['noredirect'],
+                       $params['tags'] ?: [] );
+               if ( !$status->isOK() ) {
+                       $this->dieStatus( $status );
+               }
+
+               $r = [
+                       'from' => $fromTitle->getPrefixedText(),
+                       'to' => $toTitle->getPrefixedText(),
+                       'reason' => $params['reason']
+               ];
+
+               // NOTE: we assume that if the old title exists, it's because it was re-created as
+               // a redirect to the new title. This is not safe, but what we did before was
+               // even worse: we just determined whether a redirect should have been created,
+               // and reported that it was created if it should have, without any checks.
+               // Also note that isRedirect() is unreliable because of T39209.
+               $r['redirectcreated'] = $fromTitle->exists();
 
-               $r = array( 'from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason'] );
-               if ( !$params['noredirect'] || !$wgUser->isAllowed( 'suppressredirect' ) )
-                       $r['redirectcreated'] = '';
+               $r['moveoverredirect'] = $toTitleExists;
 
                // Move the talk page
-               if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() )
-               {
-                       $retval = $fromTalk->moveTo( $toTalk, true, $params['reason'], !$params['noredirect'] );
-                       if ( $retval === true )
-                       {
+               if ( $params['movetalk'] && $toTalk && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
+                       $toTalkExists = $toTalk->exists();
+                       $status = $this->movePage(
+                               $fromTalk,
+                               $toTalk,
+                               $params['reason'],
+                               !$params['noredirect'],
+                               $params['tags'] ?: []
+                       );
+                       if ( $status->isOK() ) {
                                $r['talkfrom'] = $fromTalk->getPrefixedText();
                                $r['talkto'] = $toTalk->getPrefixedText();
-                       }
-                       // We're not gonna dieUsage() on failure, since we already changed something
-                       else
-                       {
-                               $parsed = $this->parseMsg( reset( $retval ) );
-                               $r['talkmove-error-code'] = $parsed['code'];
-                               $r['talkmove-error-info'] = $parsed['info'];
+                               $r['talkmoveoverredirect'] = $toTalkExists;
+                       } else {
+                               // We're not going to dieWithError() on failure, since we already changed something
+                               $r['talkmove-errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
                        }
                }
 
+               $result = $this->getResult();
+
                // Move subpages
-               if ( $params['movesubpages'] )
-               {
-                       $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle,
-                                       $params['reason'], $params['noredirect'] );
-                       $this->getResult()->setIndexedTagName( $r['subpages'], 'subpage' );
-                       if ( $params['movetalk'] )
-                       {
-                               $r['subpages-talk'] = $this->moveSubpages( $fromTalk, $toTalk,
-                                       $params['reason'], $params['noredirect'] );
-                               $this->getResult()->setIndexedTagName( $r['subpages-talk'], 'subpage' );
+               if ( $params['movesubpages'] ) {
+                       $r['subpages'] = $this->moveSubpages(
+                               $fromTitle,
+                               $toTitle,
+                               $params['reason'],
+                               $params['noredirect'],
+                               $params['tags'] ?: []
+                       );
+                       ApiResult::setIndexedTagName( $r['subpages'], 'subpage' );
+
+                       if ( $params['movetalk'] ) {
+                               $r['subpages-talk'] = $this->moveSubpages(
+                                       $fromTalk,
+                                       $toTalk,
+                                       $params['reason'],
+                                       $params['noredirect'],
+                                       $params['tags'] ?: []
+                               );
+                               ApiResult::setIndexedTagName( $r['subpages-talk'], 'subpage' );
                        }
                }
 
+               $watch = 'preferences';
+               if ( isset( $params['watchlist'] ) ) {
+                       $watch = $params['watchlist'];
+               } elseif ( $params['watch'] ) {
+                       $watch = 'watch';
+               } elseif ( $params['unwatch'] ) {
+                       $watch = 'unwatch';
+               }
+
                // Watch pages
-               if ( $params['watch'] || $wgUser->getOption( 'watchmoves' ) )
-               {
-                       $wgUser->addWatch( $fromTitle );
-                       $wgUser->addWatch( $toTitle );
+               $this->setWatch( $watch, $fromTitle, 'watchmoves' );
+               $this->setWatch( $watch, $toTitle, 'watchmoves' );
+
+               $result->addValue( null, $this->getModuleName(), $r );
+       }
+
+       /**
+        * @param Title $from
+        * @param Title $to
+        * @param string $reason
+        * @param bool $createRedirect
+        * @param array $changeTags Applied to the entry in the move log and redirect page revision
+        * @return Status
+        */
+       protected function movePage( Title $from, Title $to, $reason, $createRedirect, $changeTags ) {
+               $mp = new MovePage( $from, $to );
+               $valid = $mp->isValidMove();
+               if ( !$valid->isOK() ) {
+                       return $valid;
+               }
+
+               $user = $this->getUser();
+               $permStatus = $mp->checkPermissions( $user, $reason );
+               if ( !$permStatus->isOK() ) {
+                       return $permStatus;
                }
-               else if ( $params['unwatch'] )
-               {
-                       $wgUser->removeWatch( $fromTitle );
-                       $wgUser->removeWatch( $toTitle );
+
+               // Check suppressredirect permission
+               if ( !$user->isAllowed( 'suppressredirect' ) ) {
+                       $createRedirect = true;
                }
-               $this->getResult()->addValue( null, $this->getModuleName(), $r );
+
+               return $mp->move( $user, $reason, $createRedirect, $changeTags );
        }
 
-       public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect )
-       {
-               $retval = array();
-               $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
-               if ( isset( $success[0] ) )
-                       return array( 'error' => $this->parseMsg( $success ) );
-               else
-               {
-                       // At least some pages could be moved
-                       // Report each of them separately
-                       foreach ( $success as $oldTitle => $newTitle )
-                       {
-                               $r = array( 'from' => $oldTitle );
-                               if ( is_array( $newTitle ) )
-                                       $r['error'] = $this->parseMsg( reset( $newTitle ) );
-                               else
-                                       // Success
-                                       $r['to'] = $newTitle;
-                               $retval[] = $r;
+       /**
+        * @param Title $fromTitle
+        * @param Title $toTitle
+        * @param string $reason
+        * @param bool $noredirect
+        * @param array $changeTags Applied to the entry in the move log and redirect page revisions
+        * @return array
+        */
+       public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect, $changeTags = [] ) {
+               $retval = [];
+
+               $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect, $changeTags );
+               if ( isset( $success[0] ) ) {
+                       $status = $this->errorArrayToStatus( $success );
+                       return [ 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ) ];
+               }
+
+               // At least some pages could be moved
+               // Report each of them separately
+               foreach ( $success as $oldTitle => $newTitle ) {
+                       $r = [ 'from' => $oldTitle ];
+                       if ( is_array( $newTitle ) ) {
+                               $status = $this->errorArrayToStatus( $newTitle );
+                               $r['errors'] = $this->getErrorFormatter()->arrayFromStatus( $status );
+                       } else {
+                               // Success
+                               $r['to'] = $newTitle;
                        }
+                       $retval[] = $r;
                }
+
                return $retval;
        }
 
@@ -169,71 +241,57 @@ class ApiMove extends ApiBase {
        }
 
        public function getAllowedParams() {
-               return array (
+               return [
                        'from' => null,
-                       'fromid' => array(
+                       'fromid' => [
                                ApiBase::PARAM_TYPE => 'integer'
-                       ),
-                       'to' => null,
-                       'token' => null,
-                       'reason' => null,
+                       ],
+                       'to' => [
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => true
+                       ],
+                       'reason' => '',
                        'movetalk' => false,
                        'movesubpages' => false,
                        'noredirect' => false,
-                       'watch' => false,
-                       'unwatch' => false,
-                       'ignorewarnings' => false
-               );
-       }
-
-       public function getParamDescription() {
-               return array (
-                       'from' => 'Title of the page you want to move. Cannot be used together with fromid.',
-                       'fromid' => 'Page ID of the page you want to move. Cannot be used together with from.',
-                       'to' => 'Title you want to rename the page to.',
-                       'token' => 'A move token previously retrieved through prop=info',
-                       'reason' => 'Reason for the move (optional).',
-                       'movetalk' => 'Move the talk page, if it exists.',
-                       'movesubpages' => 'Move subpages, if applicable',
-                       'noredirect' => 'Don\'t create a redirect',
-                       'watch' => 'Add the page and the redirect to your watchlist',
-                       'unwatch' => 'Remove the page and the redirect from your watchlist',
-                       'ignorewarnings' => 'Ignore any warnings'
-               );
-       }
-
-       public function getDescription() {
-               return array(
-                       'Move a page.'
-               );
-       }
-       
-       public function getPossibleErrors() {
-               return array_merge( parent::getPossibleErrors(), array(
-                       array( 'missingparam', 'to' ),
-                       array( 'invalidtitle', 'from' ),
-                       array( 'nosuchpageid', 'fromid' ),
-                       array( 'notanarticle' ),
-                       array( 'invalidtitle', 'to' ),
-                       array( 'sharedfile-exists' ),
-               ) );
+                       'watch' => [
+                               ApiBase::PARAM_DFLT => false,
+                               ApiBase::PARAM_DEPRECATED => true,
+                       ],
+                       'unwatch' => [
+                               ApiBase::PARAM_DFLT => false,
+                               ApiBase::PARAM_DEPRECATED => true,
+                       ],
+                       'watchlist' => [
+                               ApiBase::PARAM_DFLT => 'preferences',
+                               ApiBase::PARAM_TYPE => [
+                                       'watch',
+                                       'unwatch',
+                                       'preferences',
+                                       'nochange'
+                               ],
+                       ],
+                       'ignorewarnings' => false,
+                       'tags' => [
+                               ApiBase::PARAM_TYPE => 'tags',
+                               ApiBase::PARAM_ISMULTI => true,
+                       ],
+               ];
        }
 
        public function needsToken() {
-               return true;
-       }
-
-       public function getTokenSalt() {
-               return '';
+               return 'csrf';
        }
 
-       protected function getExamples() {
-               return array (
-                       'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk&noredirect'
-               );
+       protected function getExamplesMessages() {
+               return [
+                       'action=move&from=Badtitle&to=Goodtitle&token=123ABC&' .
+                               'reason=Misspelled%20title&movetalk=&noredirect='
+                               => 'apihelp-move-example-move',
+               ];
        }
 
-       public function getVersion() {
-               return __CLASS__ . ': $Id: ApiMove.php 74217 2010-10-03 15:53:07Z reedy $';
+       public function getHelpUrls() {
+               return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Move';
        }
 }