Fix regression from r36678: we can't use $this->dieUsageMsg() in a static method...
[lhc/web/wiklou.git] / includes / api / ApiMove.php
index 1ad5d55..a3801bf 100644 (file)
@@ -29,28 +29,28 @@ if (!defined('MEDIAWIKI')) {
 
 
 /**
- * @addtogroup API
+ * @ingroup API
  */
 class ApiMove extends ApiBase {
 
        public function __construct($main, $action) {
                parent :: __construct($main, $action);
        }
-       
+
        public function execute() {
                global $wgUser;
                $this->getMain()->requestWriteMode();
                $params = $this->extractRequestParams();
                if(is_null($params['reason']))
                        $params['reason'] = '';
-       
+
                $titleObj = NULL;
                if(!isset($params['from']))
-                       $this->dieUsageMsg(array('nofrom'));
+                       $this->dieUsageMsg(array('missingparam', 'from'));
                if(!isset($params['to']))
-                       $this->dieUsageMsg(array('noto'));
+                       $this->dieUsageMsg(array('missingparam', 'to'));
                if(!isset($params['token']))
-                       $this->dieUsageMsg(array('notoken'));
+                       $this->dieUsageMsg(array('missingparam', 'token'));
                if(!$wgUser->matchEditToken($params['token']))
                        $this->dieUsageMsg(array('sessionfailure'));
 
@@ -61,22 +61,37 @@ class ApiMove extends ApiBase {
                        $this->dieUsageMsg(array('notanarticle'));
                $fromTalk = $fromTitle->getTalkPage();
 
-               
                $toTitle = Title::newFromText($params['to']);
                if(!$toTitle)
                        $this->dieUsageMsg(array('invalidtitle', $params['to']));
                $toTalk = $toTitle->getTalkPage();
 
-               $dbw = wfGetDB(DB_MASTER);
-               $dbw->begin();          
+               // Run getUserPermissionsErrors() here so we get message arguments too,
+               // rather than just a message key. The latter is troublesome for messages
+               // that use arguments.
+               // FIXME: moveTo() should really return an array, requires some
+               //        refactoring of other code, though (mainly SpecialMovepage.php)
+               $errors = array_merge($fromTitle->getUserPermissionsErrors('move', $wgUser),
+                                       $fromTitle->getUserPermissionsErrors('edit', $wgUser),
+                                       $toTitle->getUserPermissionsErrors('move', $wgUser),
+                                       $toTitle->getUserPermissionsErrors('edit', $wgUser));
+               if(!empty($errors))
+                       // We don't care about multiple errors, just report one of them
+                       $this->dieUsageMsg(current($errors));
+
+               $hookErr = null;
+
                $retval = $fromTitle->moveTo($toTitle, true, $params['reason'], !$params['noredirect']);
                if($retval !== true)
-                       $this->dieUsageMsg(array($retval));
+               {
+                       # FIXME: Title::moveTo() sometimes returns a string
+                       $this->dieUsageMsg(reset($retval));
+               }
 
                $r = array('from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason']);
-               if(!$params['noredirect'])
+               if(!$params['noredirect'] || !$wgUser->isAllowed('suppressredirect'))
                        $r['redirectcreated'] = '';
-       
+
                if($params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage())
                {
                        // We need to move the talk page as well
@@ -92,35 +107,52 @@ class ApiMove extends ApiBase {
                        {
                                $r['talkmove-error-code'] = ApiBase::$messageMap[$retval]['code'];
                                $r['talkmove-error-info'] = ApiBase::$messageMap[$retval]['info'];
-                       }       
+                       }
+               }
+
+               # Watch pages
+               if($params['watch'] || $wgUser->getOption('watchmoves'))
+               {
+                       $wgUser->addWatch($fromTitle);
+                       $wgUser->addWatch($toTitle);
+               }
+               else if($params['unwatch'])
+               {
+                       $wgUser->removeWatch($fromTitle);
+                       $wgUser->removeWatch($toTitle);
                }
-               $dbw->commit(); // Make sure all changes are really written to the DB
                $this->getResult()->addValue(null, $this->getModuleName(), $r);
        }
-       
-       protected function getAllowedParams() {
+
+       public function mustBePosted() { return true; }
+
+       public function getAllowedParams() {
                return array (
                        'from' => null,
                        'to' => null,
                        'token' => null,
                        'reason' => null,
                        'movetalk' => false,
-                       'noredirect' => false
+                       'noredirect' => false,
+                       'watch' => false,
+                       'unwatch' => false
                );
        }
 
-       protected function getParamDescription() {
+       public function getParamDescription() {
                return array (
                        'from' => 'Title of the page you want to move.',
                        '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.',
-                       'noredirect' => 'Don\'t create a redirect'
+                       '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'
                );
        }
 
-       protected function getDescription() {
+       public function getDescription() {
                return array(
                        'Moves a page.'
                );