Provide some info on which case value was not handled in ApiLogin
[lhc/web/wiklou.git] / includes / api / ApiMove.php
index cdda5b1..a3801bf 100644 (file)
-<?php\r
-\r
-/*\r
- * Created on Oct 31, 2007\r
- * API for MediaWiki 1.8+\r
- *\r
- * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl\r
- *\r
- * This program is free software; you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; either version 2 of the License, or\r
- * (at your option) any later version.\r
- *\r
- * This program is distributed in the hope that it will be useful,\r
- * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
- * GNU General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License along\r
- * with this program; if not, write to the Free Software Foundation, Inc.,\r
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\r
- * http://www.gnu.org/copyleft/gpl.html\r
- */\r
-\r
-if (!defined('MEDIAWIKI')) {\r
-       // Eclipse helper - will be ignored in production\r
-       require_once ("ApiBase.php");\r
-}\r
-\r
-\r
-/**\r
- * @addtogroup API\r
- */\r
-class ApiMove extends ApiBase {\r
-\r
-       public function __construct($main, $action) {\r
-                       parent :: __construct($main, $action);\r
-       }\r
-       \r
-       public function execute() {\r
-               global $wgUser;\r
-               $this->getMain()->requestWriteMode();\r
-               $params = $this->extractRequestParams();\r
-               if(is_null($params['reason']))\r
-                               $params['reason'] = '';\r
-               \r
-               $titleObj = NULL;\r
-               if(!isset($params['from']))\r
-                               $this->dieUsage('The from parameter must be set', 'nofrom');\r
-               if(!isset($params['to']))\r
-                               $this->dieUsage('The to parameter must be set', 'noto');\r
-               if(!isset($params['token']))\r
-                               $this->dieUsage('The token parameter must be set', 'notoken');\r
-               if(!$wgUser->matchEditToken($params['token']))\r
-                               $this->dieUsage('Invalid token', 'badtoken');\r
-\r
-               if($wgUser->isBlocked())\r
-                               $this->dieUsage('You have been blocked from editing', 'blocked');\r
-               if(wfReadOnly())\r
-                               $this->dieUsage('The wiki is in read-only mode', 'readonly');\r
-               if($params['noredirect'] && !$wgUser->isAllowed('suppressredirect'))\r
-                               $this->dieUsage("You don't have permission to suppress redirect creation", 'nosuppress');\r
-\r
-               $fromTitle = Title::newFromText($params['from']);\r
-               if(!$fromTitle)\r
-                               $this->dieUsage("Bad title ``{$params['from']}''", 'invalidtitle');\r
-               if(!$fromTitle->exists())\r
-                               $this->dieUsage("``{$params['from']}'' doesn't exist", 'missingtitle');\r
-               $fromTalk = $fromTitle->getTalkPage();\r
-\r
-                       \r
-               $toTitle = Title::newFromText($params['to']);\r
-               if(!$toTitle)\r
-                               $this->dieUsage("Bad title ``{$params['to']}''", 'invalidtitle');\r
-               $toTalk = $toTitle->getTalkPage();\r
-\r
-               $dbw = wfGetDB(DB_MASTER);\r
-               $dbw->begin();                          \r
-               $retval = $fromTitle->moveTo($toTitle, true, $params['reason'], !$params['noredirect']);\r
-               if($retval !== true)\r
-                               switch($retval)\r
-                               {\r
-                                               // case 'badtitletext': Can't happen\r
-                                               // case 'badarticleerror': Can't happen\r
-                                               case 'selfmove':\r
-                                                               $this->dieUsage("Can't move ``{$params['from']}'' to itself", 'selfmove');\r
-                                               case 'immobile_namespace':\r
-                                                               if($fromTitle->isMovable())\r
-                                                                               $this->dieUsage("Pages in the ``{$fromTitle->getNsText()}'' namespace can't be moved", 'immobilenamespace-from');\r
-                                                               $this->dieUsage("Pages in the ``{$toTitle->getNsText()}'' namespace can't be moved", 'immobilenamespace-to');\r
-                                               case 'articleexists':\r
-                                                               $this->dieUsage("``{$toTitle->getPrefixedText()}'' already exists and is not a redirect to ``{$fromTitle->getPrefixedText()}''", 'targetexists');\r
-                                               case 'protectedpage':\r
-                                                               $this->dieUsage("You don't have permission to move ``{$fromTitle->getPrefixedText()}'' to ``{$toTitle->getPrefixedText()}''", 'permissiondenied');\r
-                                               default:\r
-                                                               throw new MWException( "Title::moveTo: Unknown return value ``{$retval}''" );\r
-                               }\r
-               $r = array('from' => $fromTitle->getPrefixedText(), 'to' => $toTitle->getPrefixedText(), 'reason' => $params['reason']);\r
-               if(!$params['noredirect'])\r
-                               $r['redirectcreated'] = '';\r
-               \r
-               if($params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage())\r
-               {\r
-                               // We need to move the talk page as well\r
-                               $toTalk = $toTitle->getTalkPage();\r
-                               $retval = $fromTalk->moveTo($toTalk, true, $params['reason'], !$params['noredirect']);\r
-                               if($retval === true)\r
-                               {\r
-                                               $r['talkfrom'] = $fromTalk->getPrefixedText();\r
-                                               $r['talkto'] = $toTalk->getPrefixedText();\r
-                               }\r
-                               // We're not gonna dieUsage() on failure, since we already changed something\r
-                               else\r
-                                               switch($retval)\r
-                                               {\r
-                                                               case 'immobile_namespace':\r
-                                                                               if($fromTalk->isMovable())\r
-                                                                               {\r
-                                                                                               $r['talkmove-error-code'] = 'immobilenamespace-from';\r
-                                                                                               $r['talkmove-error-info'] = "Pages in the ``{$fromTalk->getNsText()}'' namespace can't be moved";\r
-                                                                               }\r
-                                                                               else\r
-                                                                               {\r
-                                                                                               $r['talkmove-error-code'] = 'immobilenamespace-to';\r
-                                                                                               $r['talkmove-error-info'] = "Pages in the ``{$toTalk->getNsText()}'' namespace can't be moved";\r
-                                                                               }\r
-                                                                               break;\r
-                                                               case 'articleexists':\r
-                                                                               $r['talkmove-error-code'] = 'targetexists';\r
-                                                                               $r['talkmove-error-info'] = "``{$toTalk->getPrefixedText()}'' already exists and is not a redirect to ``{$fromTalk->getPrefixedText()}''";\r
-                                                                               break;\r
-                                                               case 'protectedpage':\r
-                                                                               $r['talkmove-error-code'] = 'permissiondenied';\r
-                                                                               $r['talkmove-error-info'] = "You don't have permission to move ``{$fromTalk->getPrefixedText()}'' to ``{$toTalk->getPrefixedText()}''";\r
-                                                               default:\r
-                                                                               $r['talkmove-error-code'] = 'unknownerror';\r
-                                                                               $r['talkmove-error-info'] = "Unknown error ``$retval''";\r
-                                               }                               \r
-               }\r
-               $dbw->commit(); // Make sure all changes are really written to the DB\r
-               $this->getResult()->addValue(null, $this->getModuleName(), $r);\r
-       }\r
-\r
-       protected function getAllowedParams() {\r
-               return array (\r
-                       'from' => null,\r
-                       'to' => null,\r
-                       'token' => null,\r
-                       'reason' => null,\r
-                       'movetalk' => false,\r
-                       'noredirect' => false\r
-               );\r
-       }\r
-\r
-       protected function getParamDescription() {\r
-               return array (\r
-                       'from' => 'Title of the page you want to move.',\r
-                       'to' => 'Title you want to rename the page to.',\r
-                       'token' => 'A move token previously retrieved through prop=info',\r
-                       'reason' => 'Reason for the move (optional).',\r
-                       'movetalk' => 'Move the talk page, if it exists.',\r
-                       'noredirect' => 'Don\'t create a redirect'\r
-               );\r
-       }\r
-\r
-       protected function getDescription() {\r
-               return array(\r
-                       'Moves a page.'\r
-               );\r
-       }\r
-\r
-       protected function getExamples() {\r
-               return array (\r
-                       'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk&noredirect'\r
-               );\r
-       }\r
-\r
-       public function getVersion() {\r
-               return __CLASS__ . ': $Id$';\r
-       }\r
-}\r
-\r
+<?php
+
+/*
+ * Created on Oct 31, 2007
+ * API for MediaWiki 1.8+
+ *
+ * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
+ *
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * 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.
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+
+if (!defined('MEDIAWIKI')) {
+       // Eclipse helper - will be ignored in production
+       require_once ("ApiBase.php");
+}
+
+
+/**
+ * @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('missingparam', 'from'));
+               if(!isset($params['to']))
+                       $this->dieUsageMsg(array('missingparam', 'to'));
+               if(!isset($params['token']))
+                       $this->dieUsageMsg(array('missingparam', 'token'));
+               if(!$wgUser->matchEditToken($params['token']))
+                       $this->dieUsageMsg(array('sessionfailure'));
+
+               $fromTitle = Title::newFromText($params['from']);
+               if(!$fromTitle)
+                       $this->dieUsageMsg(array('invalidtitle', $params['from']));
+               if(!$fromTitle->exists())
+                       $this->dieUsageMsg(array('notanarticle'));
+               $fromTalk = $fromTitle->getTalkPage();
+
+               $toTitle = Title::newFromText($params['to']);
+               if(!$toTitle)
+                       $this->dieUsageMsg(array('invalidtitle', $params['to']));
+               $toTalk = $toTitle->getTalkPage();
+
+               // 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)
+               {
+                       # 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'] || !$wgUser->isAllowed('suppressredirect'))
+                       $r['redirectcreated'] = '';
+
+               if($params['movetalk'] && $fromTalk->exists() && !$fromTitle->isTalkPage())
+               {
+                       // We need to move the talk page as well
+                       $toTalk = $toTitle->getTalkPage();
+                       $retval = $fromTalk->moveTo($toTalk, true, $params['reason'], !$params['noredirect']);
+                       if($retval === true)
+                       {
+                               $r['talkfrom'] = $fromTalk->getPrefixedText();
+                               $r['talkto'] = $toTalk->getPrefixedText();
+                       }
+                       // We're not gonna dieUsage() on failure, since we already changed something
+                       else
+                       {
+                               $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);
+               }
+               $this->getResult()->addValue(null, $this->getModuleName(), $r);
+       }
+
+       public function mustBePosted() { return true; }
+
+       public function getAllowedParams() {
+               return array (
+                       'from' => null,
+                       'to' => null,
+                       'token' => null,
+                       'reason' => null,
+                       'movetalk' => false,
+                       'noredirect' => false,
+                       'watch' => false,
+                       'unwatch' => false
+               );
+       }
+
+       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',
+                       'watch' => 'Add the page and the redirect to your watchlist',
+                       'unwatch' => 'Remove the page and the redirect from your watchlist'
+               );
+       }
+
+       public function getDescription() {
+               return array(
+                       'Moves a page.'
+               );
+       }
+
+       protected function getExamples() {
+               return array (
+                       'api.php?action=move&from=Exampel&to=Example&token=123ABC&reason=Misspelled%20title&movetalk&noredirect'
+               );
+       }
+
+       public function getVersion() {
+               return __CLASS__ . ': $Id$';
+       }
+}