Merge "Added assertArrayEquals method to MediaWikiTestCase to avoid duplicating asort...
[lhc/web/wiklou.git] / includes / api / ApiMove.php
index 62609fd..c89f59b 100644 (file)
@@ -1,10 +1,10 @@
 <?php
-
 /**
+ *
+ *
  * Created on Oct 31, 2007
- * API for MediaWiki 1.8+
  *
- * Copyright © 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 {
@@ -37,16 +35,13 @@ class ApiMove extends ApiBase {
        }
 
        public function execute() {
-               global $wgUser;
+               $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'] ) ) {
                        $fromTitle = Title::newFromText( $params['from'] );
@@ -61,7 +56,7 @@ class ApiMove extends ApiBase {
                }
 
                if ( !$fromTitle->exists() ) {
-                       $this->dieUsageMsg( array( 'notanarticle' ) );
+                       $this->dieUsageMsg( 'notanarticle' );
                }
                $fromTalk = $fromTitle->getTalkPage();
 
@@ -75,29 +70,26 @@ class ApiMove extends ApiBase {
                        && !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' ) );
+                       if ( !$params['ignorewarnings'] && $user->isAllowed( 'reupload-shared' ) ) {
+                               $this->dieUsageMsg( 'sharedfile-exists' );
+                       } elseif ( !$user->isAllowed( 'reupload-shared' ) ) {
+                               $this->dieUsageMsg( 'cantoverwrite-sharedfile' );
                        }
                }
 
                // Move the page
-               $hookErr = null;
                $retval = $fromTitle->moveTo( $toTitle, true, $params['reason'], !$params['noredirect'] );
                if ( $retval !== true ) {
                        $this->dieUsageMsg( reset( $retval ) );
                }
 
                $r = array( 'from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason'] );
-               if ( !$params['noredirect'] || !$wgUser->isAllowed( 'suppressredirect' ) )
-               {
+               if ( !$params['noredirect'] || !$user->isAllowed( 'suppressredirect' ) ) {
                        $r['redirectcreated'] = '';
                }
 
                // Move the talk page
-               if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() )
-               {
+               if ( $params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage() ) {
                        $retval = $fromTalk->moveTo( $toTalk, true, $params['reason'], !$params['noredirect'] );
                        if ( $retval === true ) {
                                $r['talkfrom'] = $fromTalk->getPrefixedText();
@@ -110,32 +102,44 @@ class ApiMove extends ApiBase {
                        }
                }
 
+               $result = $this->getResult();
+
                // Move subpages
                if ( $params['movesubpages'] ) {
                        $r['subpages'] = $this->moveSubpages( $fromTitle, $toTitle,
                                        $params['reason'], $params['noredirect'] );
-                       $this->getResult()->setIndexedTagName( $r['subpages'], 'subpage' );
+                       $result->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' );
+                               $result->setIndexedTagName( $r['subpages-talk'], 'subpage' );
                        }
                }
-               
-               // Watch pages
-               $watch = $this->getWatchlistValue( $params['watchlist'], $titleObj ) || $wgUser->getOption( 'watchmoves' );
-
-               // Deprecated parameters
-               if ( $params['watch'] || $watch ) {
-                       $wgUser->addWatch( $fromTitle );
-                       $wgUser->addWatch( $toTitle );
-               } elseif ( $params['unwatch'] || !$watch ) {
-                       $wgUser->removeWatch( $fromTitle );
-                       $wgUser->removeWatch( $toTitle );
+
+               $watch = "preferences";
+               if ( isset( $params['watchlist'] ) ) {
+                       $watch = $params['watchlist'];
+               } elseif ( $params['watch'] ) {
+                       $watch = 'watch';
+               } elseif ( $params['unwatch'] ) {
+                       $watch = 'unwatch';
                }
-               $this->getResult()->addValue( null, $this->getModuleName(), $r );
+
+               // Watch pages
+               $this->setWatch( $watch, $fromTitle, 'watchmoves' );
+               $this->setWatch( $watch, $toTitle, 'watchmoves' );
+
+               $result->addValue( null, $this->getModuleName(), $r );
        }
 
+       /**
+        * @param Title $fromTitle
+        * @param Title $toTitle
+        * @param  $reason
+        * @param  $noredirect
+        * @return array
+        */
        public function moveSubpages( $fromTitle, $toTitle, $reason, $noredirect ) {
                $retval = array();
                $success = $fromTitle->moveSubpages( $toTitle, true, $reason, !$noredirect );
@@ -172,7 +176,10 @@ class ApiMove extends ApiBase {
                        'fromid' => array(
                                ApiBase::PARAM_TYPE => 'integer'
                        ),
-                       'to' => null,
+                       'to' => array(
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => true
+                       ),
                        'token' => null,
                        'reason' => null,
                        'movetalk' => false,
@@ -200,13 +207,14 @@ class ApiMove extends ApiBase {
        }
 
        public function getParamDescription() {
+               $p = $this->getModulePrefix();
                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.',
+                       'from' => "Title of the page you want to move. Cannot be used together with {$p}fromid",
+                       'fromid' => "Page ID of the page you want to move. Cannot be used together with {$p}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.',
+                       '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',
@@ -216,33 +224,68 @@ class ApiMove extends ApiBase {
                );
        }
 
-       public function getDescription() {
+       public function getResultProperties() {
                return array(
-                       'Move a page.'
+                       '' => array(
+                               'from' => 'string',
+                               'to' => 'string',
+                               'reason' => 'string',
+                               'redirectcreated' => 'boolean',
+                               'talkfrom' => array(
+                                       ApiBase::PROP_TYPE => 'string',
+                                       ApiBase::PROP_NULLABLE => true
+                               ),
+                               'talkto' => array(
+                                       ApiBase::PROP_TYPE => 'string',
+                                       ApiBase::PROP_NULLABLE => true
+                               ),
+                               'talkmove-error-code' => array(
+                                       ApiBase::PROP_TYPE => 'string',
+                                       ApiBase::PROP_NULLABLE => true
+                               ),
+                               'talkmove-error-info' => array(
+                                       ApiBase::PROP_TYPE => 'string',
+                                       ApiBase::PROP_NULLABLE => true
+                               )
+                       )
                );
        }
 
+       public function getDescription() {
+               return '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' ),
-               ) );
+               return array_merge( parent::getPossibleErrors(),
+                       $this->getRequireOnlyOneParameterErrorMessages( array( 'from', 'fromid' ) ),
+                       array(
+                               array( 'invalidtitle', 'from' ),
+                               array( 'nosuchpageid', 'fromid' ),
+                               array( 'notanarticle' ),
+                               array( 'invalidtitle', 'to' ),
+                               array( 'sharedfile-exists' ),
+                       )
+               );
+       }
+
+       public function needsToken() {
+               return true;
        }
 
        public function getTokenSalt() {
                return '';
        }
 
-       protected function getExamples() {
+       public function getExamples() {
                return array(
-                       'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk&noredirect'
+                       'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk=&noredirect='
                );
        }
 
+       public function getHelpUrls() {
+               return 'https://www.mediawiki.org/wiki/API:Move';
+       }
+
        public function getVersion() {
                return __CLASS__ . ': $Id$';
        }