APIEDIT BRANCH MERGE: Adding apiedit modules: action={block,changerights,delete,move...
authorRoan Kattouw <catrope@users.mediawiki.org>
Sun, 2 Dec 2007 14:24:07 +0000 (14:24 +0000)
committerRoan Kattouw <catrope@users.mediawiki.org>
Sun, 2 Dec 2007 14:24:07 +0000 (14:24 +0000)
14 files changed:
RELEASE-NOTES
includes/AutoLoader.php
includes/api/ApiBlock.php [new file with mode: 0644]
includes/api/ApiChangeRights.php [new file with mode: 0644]
includes/api/ApiDelete.php [new file with mode: 0644]
includes/api/ApiMain.php
includes/api/ApiMove.php [new file with mode: 0644]
includes/api/ApiProtect.php [new file with mode: 0644]
includes/api/ApiQuery.php
includes/api/ApiQueryBlocks.php [new file with mode: 0644]
includes/api/ApiQueryDeletedrevs.php [new file with mode: 0644]
includes/api/ApiRollback.php [new file with mode: 0644]
includes/api/ApiUnblock.php [new file with mode: 0644]
includes/api/ApiUndelete.php [new file with mode: 0644]

index 077f74a..d9271ad 100644 (file)
@@ -315,6 +315,7 @@ Full API documentation is available at http://www.mediawiki.org/wiki/API
 * Add rvtoken=rollback to prop=revisions
 * Add meta=allmessages to get messages from site's messages cache.
 * Use bold and italics highlighting only in API help
+* Added action={block,changerights,delete,move,protect,rollback,unblock,undelete} and list={blocks,deletedrevs}\
 
 === Languages updated in 1.12 ===
 
index 86b5212..ec533ff 100644 (file)
@@ -344,6 +344,18 @@ function __autoload($className) {
                'ApiQueryWatchlist' => 'includes/api/ApiQueryWatchlist.php',
                'ApiRender' => 'includes/api/ApiRender.php',
                'ApiResult' => 'includes/api/ApiResult.php',
+
+               # apiedit branch
+               'ApiBlock' => 'includes/api/ApiBlock.php',
+               'ApiChangeRights' => 'includes/api/ApiChangeRights.php',
+               'ApiDelete' => 'includes/api/ApiDelete.php',
+               'ApiMove' => 'includes/api/ApiMove.php',
+               'ApiProtect' => 'includes/api/ApiProtect.php',
+               'ApiQueryBlocks' => 'includes/api/ApiQueryBlocks.php',
+               'ApiQueryDeletedrevs' => 'includes/api/ApiQueryDeletedrevs.php',
+               'ApiRollback' => 'includes/api/ApiRollback.php',
+               'ApiUnblock' => 'includes/api/ApiUnblock.php',
+               'ApiUndelete' => 'includes/api/ApiUndelete.php'
        );
        
        wfProfileIn( __METHOD__ );
diff --git a/includes/api/ApiBlock.php b/includes/api/ApiBlock.php
new file mode 100644 (file)
index 0000000..636d887
--- /dev/null
@@ -0,0 +1,164 @@
+<?php\r
+\r
+/*\r
+ * Created on Sep 4, 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
+ * @addtogroup API\r
+ */\r
+class ApiBlock 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->requestWriteMode();\r
+               $params = $this->extractRequestParams();\r
+\r
+               if($params['gettoken'])\r
+               {\r
+                       $res['blocktoken'] = $wgUser->editToken();\r
+                       $this->getResult()->addValue(null, $this->getModuleName(), $res);\r
+                       return;\r
+               }\r
+\r
+               if(is_null($params['user']))\r
+                       $this->dieUsage('The user parameter must be set', 'nouser');\r
+               if(is_null($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
+               if(!$wgUser->isAllowed('block'))\r
+                       $this->dieUsage('You don\'t have permission to block users', 'permissiondenied');\r
+               if($params['hidename'] && !$wgUser->isAllowed('hideuser'))\r
+                       $this->dieUsage('You don\'t have permission to hide user names from the block log', 'nohide');\r
+               if(wfReadOnly())\r
+                       $this->dieUsage('The wiki is in read-only mode', 'readonly');\r
+\r
+               $form = new IPBlockForm('');\r
+               $form->BlockAddress = $params['user'];\r
+               $form->BlockReason = $params['reason'];\r
+               $form->BlockReasonList = 'other';\r
+               $form->BlockExpiry = ($params['expiry'] == 'never' ? 'infinite' : $params['expiry']);\r
+               $form->BlockOther = '';\r
+               $form->BlockAnonOnly = $params['anononly'];\r
+               $form->BlockCreateAccount = $params['nocreate'];\r
+               $form->BlockEnableAutoBlock = $params['autoblock'];\r
+               $form->BlockEmail = $params['noemail'];\r
+               $form->BlockHideName = $params['hidename'];\r
+\r
+               $dbw = wfGetDb(DB_MASTER);\r
+               $dbw->begin();\r
+               $retval = $form->doBlock($userID, $expiry);\r
+               switch($retval)\r
+               {\r
+                       case IPBlockForm::BLOCK_SUCCESS:\r
+                               break; // We'll deal with that later\r
+                       case IPBlockForm::BLOCK_RANGE_INVALID:\r
+                               $this->dieUsage("Invalid IP range ``{$params['user']}''", 'invalidrange');\r
+                       case IPBlockForm::BLOCK_RANGE_DISABLED:\r
+                               $this->dieUsage('Blocking IP ranges has been disabled', 'rangedisabled');\r
+                       case IPBlockForm::BLOCK_NONEXISTENT_USER:\r
+                               $this->dieUsage("User ``{$params['user']}'' doesn't exist", 'nosuchuser');\r
+                       case IPBlockForm::BLOCK_IP_INVALID:\r
+                               $this->dieUsage("Invaild IP address ``{$params['user']}''", 'invalidip');\r
+                       case IPBlockForm::BLOCK_EXPIRY_INVALID:\r
+                               $this->dieUsage("Invalid expiry time ``{$params['expiry']}''", 'invalidexpiry');\r
+                       case IPBlockForm::BLOCK_ALREADY_BLOCKED:\r
+                               $this->dieUsage("User ``{$params['user']}'' is already blocked", 'alreadyblocked');\r
+                       default:\r
+                               $this->dieDebug(__METHOD__, "IPBlockForm::doBlock() returned an unknown error ($retval)");\r
+               }\r
+               $dbw->commit();\r
+               \r
+               $res['user'] = $params['user'];\r
+               $res['userID'] = $userID;\r
+               $res['expiry'] = ($expiry == Block::infinity() ? 'infinite' : $expiry);\r
+               $res['reason'] = $params['reason'];\r
+               if($params['anononly'])\r
+                       $res['anononly'] = '';\r
+               if($params['nocreate'])\r
+                       $res['nocreate'] = '';\r
+               if($params['autoblock'])\r
+                       $res['autoblock'] = '';\r
+               if($params['noemail'])\r
+                       $res['noemail'] = '';\r
+               if($params['hidename'])\r
+                       $res['hidename'] = '';\r
+\r
+               $this->getResult()->addValue(null, $this->getModuleName(), $res);\r
+       }\r
+\r
+       protected function getAllowedParams() {\r
+               return array (\r
+                       'user' => null,\r
+                       'token' => null,\r
+                       'gettoken' => false,\r
+                       'expiry' => 'never',\r
+                       'reason' => null,\r
+                       'anononly' => false,\r
+                       'nocreate' => false,\r
+                       'autoblock' => false,\r
+                       'noemail' => false,\r
+                       'hidename' => false,\r
+               );\r
+       }\r
+\r
+       protected function getParamDescription() {\r
+               return array (\r
+                       'user' => 'Username, IP address or IP range you want to block',\r
+                       'token' => 'A block token previously obtained through the gettoken parameter',\r
+                       'gettoken' => 'If set, a block token will be returned, and no other action will be taken',\r
+                       'expiry' => 'Relative expiry time, e.g. \'5 months\' or \'2 weeks\'. If set to \'infinite\', \'indefinite\' or \'never\', the block will never expire.',\r
+                       'reason' => 'Reason for block (optional)',\r
+                       'anononly' => 'Block anonymous users only (i.e. disable anonymous edits for this IP)',\r
+                       'nocreate' => 'Prevent account creation',\r
+                       'autoblock' => 'Automatically block the last used IP address, and any subsequent IP addresses they try to login from',\r
+                       'noemail' => 'Prevent user from sending e-mail through the wiki',\r
+                       'hidename' => 'Hide the username from the block log.'\r
+               );\r
+       }\r
+\r
+       protected function getDescription() {\r
+               return array(\r
+                       'Block a user.'\r
+               );\r
+       }\r
+\r
+       protected function getExamples() {\r
+               return array (\r
+                       'api.php?action=block&user=123.5.5.12&expiry=3%20days&reason=First%20strike',\r
+                       'api.php?action=block&user=Vandal&expiry=never&reason=Vandalism&nocreate&autoblock&noemail'\r
+               );\r
+       }\r
+\r
+       public function getVersion() {\r
+               return __CLASS__ . ': $Id$';\r
+       }\r
+}\r
diff --git a/includes/api/ApiChangeRights.php b/includes/api/ApiChangeRights.php
new file mode 100644 (file)
index 0000000..ff819a9
--- /dev/null
@@ -0,0 +1,170 @@
+<?php\r
+\r
+/*\r
+ * Created on Sep 11, 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
+ * @addtogroup API\r
+ */\r
+class ApiChangeRights extends ApiBase {\r
+\r
+       public function __construct($main, $action) {\r
+               parent :: __construct($main, $action);\r
+       }\r
+\r
+       public function execute() {\r
+               global $wgUser, $wgRequest;\r
+               $this->requestWriteMode();\r
+               \r
+               if(wfReadOnly())\r
+                               $this->dieUsage('The wiki is in read-only mode', 'readonly');\r
+               $params = $this->extractRequestParams();\r
+\r
+               $ur = new UserrightsForm($wgRequest);\r
+               $allowed = $ur->changeableGroups();\r
+               $res = array();\r
+\r
+               if(is_null($params['user']))\r
+                       $this->dieUsage('The user parameter must be set', 'nouser');\r
+\r
+               $uName = User::getCanonicalName($params['user']);\r
+               $u = User::newFromName($uName);\r
+               if(!$u)\r
+                       $this->dieUsage("Invalid username ``{$params['user']}''", 'invaliduser');\r
+               if($u->getId() == 0) // Anon or non-existent\r
+                       $this->dieUsage("User ``{$params['user']}'' doesn't exist", 'nosuchuser');\r
+\r
+               $curgroups = $u->getGroups();\r
+\r
+               if($params['listgroups'])\r
+               {\r
+                       $res['user'] = $uName;\r
+                       $res['allowedgroups'] = $allowed;\r
+                       $res['ingroups'] = $curgroups;\r
+                       $this->getResult()->setIndexedTagName($res['ingroups'], 'group');\r
+                       $this->getResult()->setIndexedTagName($res['allowedgroups']['add'], 'group');\r
+                       $this->getResult()->setIndexedTagName($res['allowedgroups']['remove'], 'group');\r
+               }\r
+;\r
+               if($params['gettoken'])\r
+               {\r
+                       $res['changerightstoken'] = $wgUser->editToken($uName);\r
+                       $this->getResult()->addValue(null, $this->getModuleName(), $res);\r
+                       return;\r
+               }\r
+\r
+               if(empty($params['addto']) && empty($params['rmfrom']))\r
+                       $this->dieUsage('At least one of the addto and rmfrom parameters must be set', 'noaddrm');\r
+               if(is_null($params['token']))\r
+                       $this->dieUsage('The token parameter must be set', 'notoken');\r
+               if(!$wgUser->matchEditToken($params['token'], $uName))\r
+                       $this->dieUsage('Invalid token', 'badtoken');\r
+\r
+               if(!$wgUser->isAllowed('userrights'))\r
+                       $this->dieUsage('You don\'t have permission to change users\' rights', 'permissiondenied');\r
+\r
+               // First let's remove redundant groups and check permissions while we're at it\r
+               if(is_null($params['addto']))\r
+                       $params['addto'] = array();\r
+               $addto = array();\r
+               foreach($params['addto'] as $g)\r
+               {\r
+                       if(!in_array($g, $allowed['add']))\r
+                               $this->dieUsage("You don't have permission to add to group ``$g''", 'cantadd');\r
+                       if(!in_array($g, $curgroups))\r
+                               $addto[] = $g;\r
+               }\r
+\r
+               if(is_null($params['rmfrom']))\r
+                       $params['rmfrom'] = array();\r
+               $rmfrom = array();\r
+               foreach($params['rmfrom'] as $g)\r
+               {\r
+                       if(!in_array($g, $allowed['remove']))\r
+                               $this->dieUsage("You don't have permission to remove from group ``$g''", 'cantremove');\r
+                       if(in_array($g, $curgroups))\r
+                               $rmfrom[] = $g;\r
+               }\r
+               $dbw = wfGetDb(DB_MASTER);\r
+               $dbw->begin();\r
+               $ur->doSaveUserGroups($u, $rmfrom, $addto, $params['reason']);\r
+               $dbw->commit();\r
+               $res['user'] = $uName;\r
+               $res['addedto'] = $addto;\r
+               $res['removedfrom'] = $rmfrom;\r
+               $res['reason'] = $params['reason'];\r
+\r
+               $this->getResult()->setIndexedTagName($res['addedto'], 'group');\r
+               $this->getResult()->setIndexedTagName($res['removedfrom'], 'group');\r
+               $this->getResult()->addValue(null, $this->getModuleName(), $res);\r
+       }\r
+\r
+       protected function getAllowedParams() {\r
+               return array (\r
+                       'user' => null,\r
+                       'token' => null,\r
+                       'gettoken' => false,\r
+                       'listgroups' => false,\r
+                       'addto' => array(\r
+                               ApiBase :: PARAM_ISMULTI => true,\r
+                       ),\r
+                       'rmfrom' => array(\r
+                               ApiBase :: PARAM_ISMULTI => true,\r
+                       ),\r
+                       'reason' => ''\r
+               );\r
+       }\r
+\r
+       protected function getParamDescription() {\r
+               return array (\r
+                       'user' => 'The user you want to add to or remove from groups.',\r
+                       'token' => 'A changerights token previously obtained through the gettoken parameter.',\r
+                       'gettoken' => 'Output a token. Note that the user parameter still has to be set.',\r
+                       'listgroups' => 'List the groups the user is in, and the ones you can add them to and remove them from.',\r
+                       'addto' => 'Pipe-separated list of groups to add this user to',\r
+                       'rmfrom' => 'Pipe-separated list of groups to remove this user from',\r
+                       'reason' => 'Reason for change (optional)'\r
+               );\r
+       }\r
+\r
+       protected function getDescription() {\r
+               return array(\r
+                       'Add or remove a user from certain groups.'\r
+               );\r
+       }\r
+\r
+       protected function getExamples() {\r
+               return array (\r
+                       'api.php?action=changerights&user=Bob&gettoken&listgroups',\r
+                       'api.php?action=changerights&user=Bob&token=123ABC&addto=sysop&reason=Promoting%20per%20RFA'\r
+               );\r
+       }\r
+\r
+       public function getVersion() {\r
+               return __CLASS__ . ': $Id$';\r
+       }\r
+}\r
diff --git a/includes/api/ApiDelete.php b/includes/api/ApiDelete.php
new file mode 100644 (file)
index 0000000..b0d064b
--- /dev/null
@@ -0,0 +1,172 @@
+<?php\r
+\r
+/*\r
+ * Created on Jun 30, 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 ApiDelete extends ApiBase {\r
+\r
+       public function __construct($main, $action) {\r
+               parent :: __construct($main, $action);\r
+       }\r
+\r
+       /**\r
+        * We have our own delete() function, since Article.php's implementation is split in two phases\r
+        * @param Article $article - Article object to work on\r
+        * @param string $token - Delete token (same as edit token)\r
+        * @param string $reason - Reason for the deletion. Autogenerated if NULL\r
+        * @return DELETE_SUCCESS on success, DELETE_* on failure\r
+        */\r
+\r
+        const DELETE_SUCCESS = 0;\r
+        const DELETE_PERM = 1;\r
+        const DELETE_BLOCKED = 2;\r
+        const DELETE_READONLY = 3;\r
+        const DELETE_BADTOKEN = 4;\r
+        const DELETE_BADARTICLE = 5;\r
+\r
+       public static function delete(&$article, $token, &$reason = NULL)\r
+       {\r
+               global $wgUser;\r
+\r
+               // Check permissions first\r
+               if(!$article->mTitle->userCan('delete'))\r
+                       return self::DELETE_PERM;\r
+               if($wgUser->isBlocked())\r
+                       return self::DELETE_BLOCKED;\r
+               if(wfReadOnly())\r
+                       return self::DELETE_READONLY;\r
+\r
+               // Check token\r
+               if(!$wgUser->matchEditToken($token))\r
+                       return self::DELETE_BADTOKEN;\r
+\r
+               // Auto-generate a summary, if necessary\r
+               if(is_null($reason))\r
+               {\r
+                       $reason = $article->generateReason($hasHistory);\r
+                       if($reason === false)\r
+                               return self::DELETE_BADARTICLE;\r
+               }\r
+\r
+               // Luckily, Article.php provides a reusable delete function that does the hard work for us\r
+               if($article->doDeleteArticle($reason))\r
+                       return self::DELETE_SUCCESS;\r
+               return self::DELETE_BADARTICLE;\r
+       }\r
+\r
+       public function execute() {\r
+               global $wgUser;\r
+               $this->requestWriteMode();\r
+               $params = $this->extractRequestParams();\r
+               \r
+               $titleObj = NULL;\r
+               if(!isset($params['title']))\r
+                       $this->dieUsage('The title parameter must be set', 'notitle');\r
+               if(!isset($params['token']))\r
+                       $this->dieUsage('The token parameter must be set', 'notoken');\r
+\r
+               // delete() also checks for these, but we wanna save some work\r
+               if(!$wgUser->isAllowed('delete'))\r
+                       $this->dieUsage('You don\'t have permission to delete pages', 'permissiondenied');\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
+\r
+               $titleObj = Title::newFromText($params['title']);\r
+               if(!$titleObj)\r
+                       $this->dieUsage("Bad title ``{$params['title']}''", 'invalidtitle');\r
+               if(!$titleObj->exists())\r
+                       $this->dieUsage("``{$params['title']}'' doesn't exist", 'missingtitle');\r
+\r
+               $articleObj = new Article($titleObj);\r
+               $reason = (isset($params['reason']) ? $params['reason'] : NULL);\r
+               $dbw = wfGetDb(DB_MASTER);\r
+               $dbw->begin();\r
+               $retval = self::delete(&$articleObj, $params['token'], &$reason);\r
+\r
+               switch($retval)\r
+               {\r
+                       case self::DELETE_SUCCESS:\r
+                               break; // We'll deal with that later\r
+                       case self::DELETE_PERM:  // If we get PERM, BLOCKED or READONLY that's weird, but it's possible\r
+                               $this->dieUsage('You don\'t have permission to delete', 'permissiondenied');\r
+                       case self::DELETE_BLOCKED:\r
+                               $this->dieUsage('You have been blocked from editing', 'blocked');\r
+                       case self::DELETE_READONLY:\r
+                               $this->dieUsage('The wiki is in read-only mode', 'readonly');\r
+                       case self::DELETE_BADTOKEN:\r
+                               $this->dieUsage('Invalid token', 'badtoken');\r
+                       case self::DELETE_BADARTICLE:\r
+                               $this->dieUsage("The article ``{$params['title']}'' doesn't exist or has already been deleted", 'missingtitle');\r
+                       default:\r
+                               // delete() has apparently invented a new error, which is extremely weird\r
+                               $this->dieDebug(__METHOD__, "delete() returned an unknown error ($retval)");\r
+               }\r
+               // $retval has to be self::DELETE_SUCCESS if we get here\r
+               $dbw->commit();\r
+               $r = array('title' => $titleObj->getPrefixedText(), 'reason' => $reason);\r
+               $this->getResult()->addValue(null, $this->getModuleName(), $r);\r
+       }\r
+\r
+       protected function getAllowedParams() {\r
+               return array (\r
+                       'title' => null,\r
+                       'token' => null,\r
+                       'reason' => null,\r
+               );\r
+       }\r
+\r
+       protected function getParamDescription() {\r
+               return array (\r
+                       'title' => 'Title of the page you want to delete.',\r
+                       'token' => 'A delete token previously retrieved through prop=info',\r
+                       'reason' => 'Reason for the deletion. If not set, an automatically generated reason will be used.'\r
+               );\r
+       }\r
+\r
+       protected function getDescription() {\r
+               return array(\r
+                       'Deletes a page. You need to be logged in as a sysop to use this function, see also action=login.'\r
+               );\r
+       }\r
+\r
+       protected function getExamples() {\r
+               return array (\r
+                       'api.php?action=delete&title=Main%20Page&token=123ABC',\r
+                       'api.php?action=delete&title=Main%20Page&token=123ABC&reason=Preparing%20for%20move'\r
+               );\r
+       }\r
+\r
+       public function getVersion() {\r
+               return __CLASS__ . ': $Id: ApiDelete.php 22289 2007-05-20 23:31:44Z yurik $';\r
+       }\r
+}\r
index d8bc30e..1243e3f 100644 (file)
@@ -57,6 +57,14 @@ class ApiMain extends ApiBase {
                'expandtemplates' => 'ApiExpandTemplates',
                'render' => 'ApiRender',
                'parse' => 'ApiParse',
+               'rollback' => 'ApiRollback',
+               'delete' => 'ApiDelete',
+               'undelete' => 'ApiUndelete',
+               'protect' => 'ApiProtect',
+               'block' => 'ApiBlock',
+               'unblock' => 'ApiUnblock',
+               'changerights' => 'ApiChangeRights',
+               'move' => 'ApiMove',
                'opensearch' => 'ApiOpenSearch',
                'feedwatchlist' => 'ApiFeedWatchlist',
                'help' => 'ApiHelp',
diff --git a/includes/api/ApiMove.php b/includes/api/ApiMove.php
new file mode 100644 (file)
index 0000000..5875042
--- /dev/null
@@ -0,0 +1,182 @@
+<?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->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
diff --git a/includes/api/ApiProtect.php b/includes/api/ApiProtect.php
new file mode 100644 (file)
index 0000000..1431621
--- /dev/null
@@ -0,0 +1,142 @@
+<?php\r
+\r
+/*\r
+ * Created on Sep 1, 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
+ * @addtogroup API\r
+ */\r
+class ApiProtect 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->requestWriteMode();\r
+               $params = $this->extractRequestParams();\r
+               \r
+               $titleObj = NULL;\r
+               if(!isset($params['title']))\r
+                       $this->dieUsage('The title parameter must be set', 'notitle');\r
+               if(!isset($params['token']))\r
+                       $this->dieUsage('The token parameter must be set', 'notoken');\r
+               if(!isset($params['protections']) || empty($params['protections']))\r
+                       $this->dieUsage('The protections parameter must be set', 'noprotections');\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(!$wgUser->matchEditToken($params['token']))\r
+                       $this->dieUsage('Invalid token', 'badtoken');\r
+\r
+               $titleObj = Title::newFromText($params['title']);\r
+               if(!$titleObj)\r
+                       $this->dieUsage("Bad title ``{$params['title']}''", 'invalidtitle');\r
+               if(!$titleObj->exists())\r
+                       $this->dieUsage("``{$params['title']}'' doesn't exist", 'missingtitle');\r
+               if(!$titleObj->userCan('protect'))\r
+                       $this->dieUsage('You don\'t have permission to change protection levels', 'permissiondenied');\r
+               $articleObj = new Article($titleObj);\r
+               \r
+               if(in_array($params['expiry'], array('infinite', 'indefinite', 'never')))\r
+                       $expiry = Block::infinity();\r
+               else\r
+               {\r
+                       $expiry = strtotime($params['expiry']);\r
+                       if($expiry < 0 || $expiry == false)\r
+                               $this->dieUsage('Invalid expiry time', 'invalidexpiry');\r
+                       \r
+                       $expiry = wfTimestamp(TS_MW, $expiry);\r
+                       if($expiry < wfTimestampNow())\r
+                               $this->dieUsage('Expiry time is in the past', 'pastexpiry');\r
+               }\r
+\r
+               $protections = array();\r
+               foreach($params['protections'] as $prot)\r
+               {\r
+                       $p = explode('=', $prot);\r
+                       $protections[$p[0]] = ($p[1] == 'all' ? '' : $p[1]);\r
+               }\r
+\r
+               $dbw = wfGetDb(DB_MASTER);\r
+               $dbw->begin();\r
+               $ok = $articleObj->updateRestrictions($protections, $params['reason'], $params['cascade'], $expiry);\r
+               if(!$ok)\r
+                       // This is very weird. Maybe the article was deleted or the user was blocked/desysopped in the meantime?\r
+                       $this->dieUsage('Unknown error', 'unknownerror');\r
+               $dbw->commit();\r
+               $res = array('title' => $titleObj->getPrefixedText(), 'reason' => $params['reason'], 'expiry' => $expiry);\r
+               if($params['cascade'])\r
+                       $res['cascade'] = '';\r
+               $res['protections'] = $protections;\r
+               $this->getResult()->addValue(null, $this->getModuleName(), $res);\r
+       }\r
+\r
+       protected function getAllowedParams() {\r
+               return array (\r
+                       'title' => null,\r
+                       'token' => null,\r
+                       'protections' => array(\r
+                               ApiBase :: PARAM_ISMULTI => true\r
+                       ),\r
+                       'expiry' => 'infinite',\r
+                       'reason' => '',\r
+                       'cascade' => false\r
+               );\r
+       }\r
+\r
+       protected function getParamDescription() {\r
+               return array (\r
+                       'title' => 'Title of the page you want to restore.',\r
+                       'token' => 'A protect token previously retrieved through prop=info',\r
+                       'protections' => 'Pipe-separated list of protection levels, formatted action=group (e.g. edit=sysop)',\r
+                       'expiry' => 'Expiry timestamp. If set to \'infinite\', \'indefinite\' or \'never\', the protection will never expire.',\r
+                       'reason' => 'Reason for (un)protecting (optional)',\r
+                       'cascade' => 'Enable cascading protection (i.e. protect pages included in this page)'\r
+               );\r
+       }\r
+\r
+       protected function getDescription() {\r
+               return array(\r
+                       'Change the protection level of a page.'\r
+               );\r
+       }\r
+\r
+       protected function getExamples() {\r
+               return array (\r
+                       'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=sysop|move=sysop&cascade&expiry=20070901163000',\r
+                       'api.php?action=protect&title=Main%20Page&token=123ABC&protections=edit=all|move=all&reason=Lifting%20restrictions'\r
+               );\r
+       }\r
+\r
+       public function getVersion() {\r
+               return __CLASS__ . ': $Id$';\r
+       }\r
+}\r
index f1a5488..40485ad 100644 (file)
@@ -62,7 +62,9 @@ class ApiQuery extends ApiBase {
                'alllinks' => 'ApiQueryAllLinks',
                'allusers' => 'ApiQueryAllUsers',
                'backlinks' => 'ApiQueryBacklinks',
+               'blocks' => 'ApiQueryBlocks',
                'categorymembers' => 'ApiQueryCategoryMembers',
+               'deletedrevs' => 'ApiQueryDeletedrevs',
                'embeddedin' => 'ApiQueryBacklinks',
                'imageusage' => 'ApiQueryBacklinks',
                'logevents' => 'ApiQueryLogEvents',
diff --git a/includes/api/ApiQueryBlocks.php b/includes/api/ApiQueryBlocks.php
new file mode 100644 (file)
index 0000000..1ef9b4f
--- /dev/null
@@ -0,0 +1,241 @@
+<?php\r
+\r
+/*\r
+ * Created on Sep 10, 2007\r
+ *\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 ('ApiQueryBase.php');\r
+}\r
+\r
+/**\r
+ * Query module to enumerate all available pages.\r
+ * \r
+ * @addtogroup API\r
+ */\r
+class ApiQueryBlocks extends ApiQueryBase {\r
+\r
+       public function __construct($query, $moduleName) {\r
+               parent :: __construct($query, $moduleName, 'bk');\r
+       }\r
+\r
+       public function execute() {\r
+               $this->run();\r
+       }\r
+\r
+       private function run() {\r
+               global $wgUser;\r
+\r
+               $params = $this->extractRequestParams();\r
+               $prop = array_flip($params['prop']);\r
+               $fld_id = isset($prop['id']);\r
+               $fld_user = isset($prop['user']);\r
+               $fld_by = isset($prop['by']);\r
+               $fld_timestamp = isset($prop['timestamp']);\r
+               $fld_expiry = isset($prop['expiry']);\r
+               $fld_reason = isset($prop['reason']);\r
+               $fld_range = isset($prop['range']);\r
+               $fld_flags = isset($prop['flags']);\r
+\r
+               $result = $this->getResult();\r
+               $pageSet = $this->getPageSet();\r
+               $titles = $pageSet->getTitles();\r
+               $data = array();\r
+\r
+               $this->addTables('ipblocks');\r
+               if($fld_id)\r
+                       $this->addFields('ipb_id');\r
+               if($fld_user)\r
+                       $this->addFields(array('ipb_address', 'ipb_user'));\r
+               if($fld_by)\r
+               {\r
+                       $this->addTables('user');\r
+                       $this->addFields(array('ipb_by', 'user_name'));\r
+                       $this->addWhere('user_id = ipb_by');\r
+               }\r
+               if($fld_timestamp)\r
+                       $this->addFields('ipb_timestamp');\r
+               if($fld_expiry)\r
+                       $this->addFields('ipb_expiry');\r
+               if($fld_reason)\r
+                       $this->addFields('ipb_reason');\r
+               if($fld_range)\r
+                       $this->addFields(array('ipb_range_start', 'ipb_range_end'));\r
+               if($fld_flags)\r
+                       $this->addFields(array('ipb_auto', 'ipb_anon_only', 'ipb_create_account', 'ipb_enable_autoblock', 'ipb_block_email', 'ipb_deleted'));\r
+\r
+               $this->addOption('LIMIT', $params['limit'] + 1);\r
+               $this->addWhereRange('ipb_timestamp', $params['dir'], $params['start'], $params['end']);\r
+               if(isset($params['ids']))\r
+                       $this->addWhere(array('ipb_id' => $params['ids']));\r
+               if(isset($params['users']))\r
+                       $this->addWhere(array('ipb_address' => $params['users']));\r
+               if(!$wgUser->isAllowed('oversight'))\r
+                       $this->addWhere(array('ipb_deleted' => 0));\r
+\r
+               // Purge expired entries on one in every 10 queries\r
+               if(!mt_rand(0, 10))\r
+                       Block::purgeExpired();\r
+\r
+               $res = $this->select(__METHOD__);\r
+               $db = wfGetDB();\r
+\r
+               $count = 0;\r
+               while($row = $db->fetchObject($res))\r
+               {\r
+                       if($count++ == $params['limit'])\r
+                       {\r
+                               // We've had enough\r
+                               $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->ipb_timestamp));\r
+                               break;\r
+                       }\r
+                       $block = array();\r
+                       if($fld_id)\r
+                               $block['id'] = $row->ipb_id;\r
+                       if($fld_user)\r
+                       {\r
+                               $block['user'] = $row->ipb_address;\r
+                               $block['userid'] = $row->ipb_user;\r
+                       }\r
+                       if($fld_by)\r
+                       {\r
+                               $block['by'] = $row->user_name;\r
+                               $block['byuserid'] = $row->ipb_by;\r
+                       }\r
+                       if($fld_timestamp)\r
+                               $block['timestamp'] = wfTimestamp(TS_ISO_8601, $row->ipb_timestamp);\r
+                       if($fld_expiry)\r
+                               $block['expiry'] = Block::decodeExpiry($row->ipb_expiry, TS_ISO_8601);\r
+                       if($fld_reason)\r
+                               $block['reason'] = $row->ipb_reason;\r
+                       if($fld_range)\r
+                       {\r
+                               $block['rangestart'] = $this->convertHexIP($row->ipb_range_start);\r
+                               $block['rangeend'] = $this->convertHexIP($row->ipb_range_end);\r
+                       }\r
+                       if($fld_flags)\r
+                       {\r
+                               // For clarity, these flags use the same names as their action=block counterparts\r
+                               if($row->ipb_auto)\r
+                                       $block['automatic'] = '';\r
+                               if($row->ipb_anon_only)\r
+                                       $block['anononly'] = '';\r
+                               if($row->ipb_create_account)\r
+                                       $block['nocreate'] = '';\r
+                               if($row->ipb_enable_autoblock)\r
+                                       $block['autoblock'] = '';\r
+                               if($row->ipb_block_email)\r
+                                       $block['noemail'] = '';\r
+                               if($row->ipb_deleted)\r
+                                       $block['hidden'] = '';\r
+                       }\r
+                       $data[] = $block;\r
+               }\r
+               $result->setIndexedTagName($data, 'block');\r
+               $result->addValue('query', $this->getModuleName(), $data);\r
+       }\r
+\r
+       protected function convertHexIP($ip)\r
+       {\r
+               // Converts a hexadecimal IP to nnn.nnn.nnn.nnn format\r
+               $dec = wfBaseConvert($ip, 16, 10);\r
+               $parts[0] = (int)($dec / (256*256*256));\r
+               $dec %= 256*256*256;\r
+               $parts[1] = (int)($dec / (256*256));\r
+               $dec %= 256*256;\r
+               $parts[2] = (int)($dec / 256);\r
+               $parts[3] = $dec % 256;\r
+               return implode('.', $parts);\r
+       }\r
+\r
+       protected function getAllowedParams() {\r
+               return array (\r
+                       'start' => array(\r
+                               ApiBase :: PARAM_TYPE => 'timestamp'\r
+                       ),\r
+                       'end' => array(\r
+                               ApiBase :: PARAM_TYPE => 'timestamp',\r
+                       ),\r
+                       'dir' => array(\r
+                               ApiBase :: PARAM_TYPE => array(\r
+                                       'newer',\r
+                                       'older'\r
+                               ),\r
+                               ApiBase :: PARAM_DFLT => 'older'\r
+                       ),\r
+                       'ids' => array(\r
+                               ApiBase :: PARAM_TYPE => 'integer',\r
+                               ApiBase :: PARAM_ISMULTI => true\r
+                       ),\r
+                       'users' => array(\r
+                               ApiBase :: PARAM_ISMULTI => true\r
+                       ),\r
+                       'limit' => array(\r
+                               ApiBase :: PARAM_DFLT => 10,\r
+                               ApiBase :: PARAM_TYPE => 'limit',\r
+                               ApiBase :: PARAM_MIN => 1,\r
+                               ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,\r
+                               ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2\r
+                       ),\r
+                       'prop' => array(\r
+                               ApiBase :: PARAM_DFLT => 'id|user|by|timestamp|expiry|reason|flags',\r
+                               ApiBase :: PARAM_TYPE => array(\r
+                                               'id',\r
+                                               'user',\r
+                                               'by',\r
+                                               'timestamp',\r
+                                               'expiry',\r
+                                               'reason',\r
+                                               'range',\r
+                                               'flags'\r
+                                       ),\r
+                               ApiBase :: PARAM_ISMULTI => true\r
+                       )\r
+               );\r
+       }\r
+\r
+       protected function getParamDescription() {\r
+               return array (\r
+                       'start' => 'The timestamp to start enumerating from',\r
+                       'end' => 'The timestamp to stop enumerating at',\r
+                       'dir' => 'The direction in which to enumerate',\r
+                       'ids' => 'Pipe-separated list of block IDs to list (optional)',\r
+                       'users' => 'Pipe-separated list of users to search for (optional)',\r
+                       'limit' => 'The maximum amount of blocks to list',\r
+                       'prop' => 'Which properties to get',\r
+               );\r
+       }\r
+\r
+       protected function getDescription() {\r
+               return 'List all blocked users and IP addresses.';\r
+       }\r
+\r
+       protected function getExamples() {\r
+               return array (\r
+               );\r
+       }\r
+\r
+       public function getVersion() {\r
+               return __CLASS__ . ': $Id$';\r
+       }\r
+}\r
diff --git a/includes/api/ApiQueryDeletedrevs.php b/includes/api/ApiQueryDeletedrevs.php
new file mode 100644 (file)
index 0000000..67694d7
--- /dev/null
@@ -0,0 +1,232 @@
+<?php\r
+\r
+/*\r
+ * Created on Jul 2, 2007\r
+ *\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 ('ApiQueryBase.php');\r
+}\r
+\r
+/**\r
+ * Query module to enumerate all available pages.\r
+ * \r
+ * @addtogroup API\r
+ */\r
+class ApiQueryDeletedrevs extends ApiQueryBase {\r
+\r
+       public function __construct($query, $moduleName) {\r
+               parent :: __construct($query, $moduleName, 'dr');\r
+       }\r
+\r
+       public function execute() {\r
+               $this->run();\r
+       }\r
+\r
+       private function run() {\r
+\r
+               global $wgUser;\r
+               // Before doing anything at all, let's check permissions\r
+               if(!$wgUser->isAllowed('deletedhistory'))\r
+                       $this->dieUsage('You don\'t have permission to view deleted revisions', 'permissiondenied');\r
+\r
+               $db = $this->getDB();\r
+               $params = $this->extractRequestParams();\r
+               $prop = array_flip($params['prop']);\r
+               $fld_revid = isset($prop['revid']);\r
+               $fld_user = isset($prop['user']);\r
+               $fld_comment = isset($prop['comment']);\r
+               $fld_minor = isset($prop['minor']);\r
+               $fld_len = isset($prop['len']);\r
+               $fld_content = isset($prop['content']);\r
+               $fld_token = isset($prop['token']);\r
+\r
+               $result = $this->getResult();\r
+               $pageSet = $this->getPageSet();\r
+               $titles = $pageSet->getTitles();\r
+               $data = array();\r
+\r
+               $this->addTables('archive');\r
+               $this->addFields(array('ar_title', 'ar_namespace', 'ar_timestamp'));\r
+               if($fld_revid)\r
+                       $this->addFields('ar_rev_id');\r
+               if($fld_user)\r
+                       $this->addFields('ar_user_text');\r
+               if($fld_comment)\r
+                       $this->addFields('ar_comment');\r
+               if($fld_minor)\r
+                       $this->addFields('ar_minor_edit');\r
+               if($fld_len)\r
+                       $this->addFields('ar_len');\r
+               if($fld_content)\r
+               {\r
+                       $this->addTables('text');\r
+                       $this->addFields(array('ar_text', 'ar_text_id', 'old_text', 'old_flags'));\r
+                       $this->addWhere('ar_text_id = old_id');\r
+\r
+                       // This also means stricter limits\r
+                       $userMax = 50;\r
+                       $botMax = 200;\r
+                       $this->validateLimit('limit', $params['limit'], 1, $userMax, $botMax);\r
+               }\r
+               if($fld_token)\r
+                       // Undelete tokens are identical for all pages, so we cache one here\r
+                       $token = $wgUser->editToken();\r
+\r
+               // We need a custom WHERE clause that matches all titles.\r
+               if(count($titles) > 0)\r
+               {\r
+                       $lb = new LinkBatch($titles);\r
+                       $where = $lb->constructSet('ar', $db);\r
+                       $this->addWhere($where);\r
+               }\r
+\r
+               $this->addOption('LIMIT', $params['limit'] + 1);\r
+               $this->addWhereRange('ar_timestamp', $params['dir'], $params['start'], $params['end']);\r
+               if(isset($params['namespace']))\r
+                       $this->addWhereFld('ar_namespace', $params['namespace']);\r
+               $res = $this->select(__METHOD__);\r
+               $pages = array();\r
+               $count = 0;\r
+               // First populate the $pages array\r
+               while($row = $db->fetchObject($res))\r
+               {\r
+                       if($count++ == $params['limit'])\r
+                       {\r
+                               // We've had enough\r
+                               $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->ar_timestamp));\r
+                               break;\r
+                       }\r
+\r
+                       $rev = array();\r
+                       $rev['timestamp'] = wfTimestamp(TS_ISO_8601, $row->ar_timestamp);\r
+                       if($fld_revid)\r
+                               $rev['revid'] = $row->ar_rev_id;\r
+                       if($fld_user)\r
+                               $rev['user'] = $row->ar_user_text;\r
+                       if($fld_comment)\r
+                               $rev['comment'] = $row->ar_comment;\r
+                       if($fld_minor)\r
+                               if($row->ar_minor_edit == 1)\r
+                                       $rev['minor'] = '';\r
+                       if($fld_len)\r
+                               $rev['len'] = $row->ar_len;\r
+                       if($fld_content)\r
+                               ApiResult::setContent($rev, Revision::getRevisionText($row));\r
+\r
+                       $t = Title::makeTitle($row->ar_namespace, $row->ar_title);\r
+                       if(!isset($pages[$t->getPrefixedText()]))\r
+                       {\r
+                               $pages[$t->getPrefixedText()] = array(\r
+                                       'title' => $t->getPrefixedText(),\r
+                                       'ns' => intval($row->ar_namespace),\r
+                                       'revisions' => array($rev)\r
+                               );\r
+                               if($fld_token)\r
+                                       $pages[$t->getPrefixedText()]['token'] = $token;\r
+                       }\r
+                       else\r
+                               $pages[$t->getPrefixedText()]['revisions'][] = $rev;\r
+               }\r
+               $db->freeResult($res);\r
+\r
+               // We don't want entire pagenames as keys, so let's make this array indexed\r
+               foreach($pages as $page)\r
+               {\r
+                       $result->setIndexedTagName($page['revisions'], 'rev');\r
+                       $data[] = $page;\r
+               }\r
+               $result->setIndexedTagName($data, 'page');\r
+               $result->addValue('query', $this->getModuleName(), $data);\r
+       }\r
+\r
+       protected function getAllowedParams() {\r
+               return array (\r
+                       'start' => array(\r
+                               ApiBase :: PARAM_TYPE => 'timestamp'\r
+                       ),\r
+                       'end' => array(\r
+                               ApiBase :: PARAM_TYPE => 'timestamp',\r
+                       ),\r
+                       'dir' => array(\r
+                               ApiBase :: PARAM_TYPE => array(\r
+                                       'newer',\r
+                                       'older'\r
+                               ),\r
+                               ApiBase :: PARAM_DFLT => 'older'\r
+                       ),\r
+                       'namespace' => array(\r
+                               ApiBase :: PARAM_ISMULTI => true,\r
+                               ApiBase :: PARAM_TYPE => 'namespace'\r
+                       ),\r
+                       'limit' => array(\r
+                               ApiBase :: PARAM_DFLT => 10,\r
+                               ApiBase :: PARAM_TYPE => 'limit',\r
+                               ApiBase :: PARAM_MIN => 1,\r
+                               ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,\r
+                               ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2\r
+                       ),\r
+                       'prop' => array(\r
+                               ApiBase :: PARAM_DFLT => 'user|comment',\r
+                               ApiBase :: PARAM_TYPE => array(\r
+                                               'revid',\r
+                                               'user',\r
+                                               'comment',\r
+                                               'minor',\r
+                                               'len',\r
+                                               'content',\r
+                                               'token'\r
+                                       ),\r
+                               ApiBase :: PARAM_ISMULTI => true\r
+                       )\r
+               );\r
+       }\r
+\r
+       protected function getParamDescription() {\r
+               return array (\r
+                       'start' => 'The timestamp to start enumerating from',\r
+                       'end' => 'The timestamp to stop enumerating at',\r
+                       'dir' => 'The direction in which to enumerate',\r
+                       'namespace' => 'The namespaces to search in',\r
+                       'limit' => 'The maximum amount of revisions to list',\r
+                       'prop' => 'Which properties to get'\r
+               );\r
+       }\r
+\r
+       protected function getDescription() {\r
+               return 'List deleted revisions.';\r
+       }\r
+\r
+       protected function getExamples() {\r
+               return array (\r
+                       'List the first 50 deleted revisions in the Category and Category talk namespaces',\r
+                       '  api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=14|15',\r
+                       'List the last deleted revisions of Main Page and Talk:Main Page, with content:',\r
+                       '  api.php?action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&drprop=user|comment|content'\r
+               );\r
+       }\r
+\r
+       public function getVersion() {\r
+               return __CLASS__ . ': $Id: ApiQueryDeletedrevs.php 23531 2007-06-30 01:19:14Z simetrical $';\r
+       }\r
+}\r
diff --git a/includes/api/ApiRollback.php b/includes/api/ApiRollback.php
new file mode 100644 (file)
index 0000000..899d076
--- /dev/null
@@ -0,0 +1,156 @@
+<?php\r
+\r
+/*\r
+ * Created on Jun 20, 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
+ * @addtogroup API\r
+ */\r
+class ApiRollback 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->requestWriteMode();\r
+               $params = $this->extractRequestParams();\r
+               \r
+               $titleObj = NULL;\r
+               if(!isset($params['title']))\r
+                       $this->dieUsage('The title parameter must be set', 'notitle');\r
+               if(!isset($params['user']))\r
+                       $this->dieUsage('The user parameter must be set', 'nouser');\r
+               if(!isset($params['token']))\r
+                       $this->dieUsage('The token parameter must be set', 'notoken');\r
+\r
+               // doRollback() also checks for these, but we wanna save some work\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
+\r
+               $titleObj = Title::newFromText($params['title']);\r
+               if(!$titleObj)\r
+                       $this->dieUsage("Bad title ``{$params['title']}''", 'invalidtitle');\r
+               if(!$titleObj->userCan('rollback'))\r
+                       $this->dieUsage('You don\'t have permission to rollback', 'permissiondenied');\r
+\r
+               $username = User::getCanonicalName($params['user']);\r
+               if(!$username)\r
+                       $this->dieUsage("Invalid username ``{$params['user']}''", 'invaliduser');\r
+\r
+               $articleObj = new Article($titleObj);\r
+               $summary = (isset($params['summary']) ? $params['summary'] : "");\r
+               $details = NULL;\r
+               $dbw = wfGetDb(DB_MASTER);\r
+               $dbw->begin();\r
+               $retval = $articleObj->doRollback($username, $summary, $params['token'], $params['markbot'], &$details);\r
+\r
+               switch($retval)\r
+               {\r
+                       case Article::SUCCESS:\r
+                               break; // We'll deal with that later\r
+                       case Article::PERM_DENIED:\r
+                               $this->dieUsage("You don't have permission to rollback", 'permissiondenied');\r
+                       case Article::BLOCKED: // If we get BLOCKED or PERM_DENIED that's very weird, but it's possible\r
+                               $this->dieUsage('You have been blocked from editing', 'blocked');\r
+                       case Article::READONLY:\r
+                               $this->dieUsage('The wiki is in read-only mode', 'readonly');\r
+                       case Article::BAD_TOKEN:\r
+                               $this->dieUsage('Invalid token', 'badtoken');\r
+                       case Article::BAD_TITLE:\r
+                               $this->dieUsage("``{$params['title']}'' doesn't exist", 'missingtitle');\r
+                       case Article::ALREADYROLLED:\r
+                               $current = $details['current'];\r
+                               $currentID = $current->getId();\r
+                               $this->dieUsage("The edit(s) you tried to rollback is/are already rolled back." .\r
+                                               "The current revision ID is ``$currentID''", 'alreadyrolled');\r
+                       case Article::ONLY_AUTHOR:\r
+                               $this->dieUsage("User ``$username'' is the only author of the page", 'onlyauthor');\r
+                       case Article::RATE_LIMITED:\r
+                               $this->dieUsage("You can't rollback too many articles in too short a time. Please wait a little while and try again", 'ratelimited');\r
+                       default:\r
+                               // rollback() has apparently invented a new error, which is extremely weird\r
+                               $this->dieDebug(__METHOD__, "rollback() returned an unknown error ($retval)");\r
+               }\r
+               // $retval has to be Article::SUCCESS if we get here\r
+               $dbw->commit();\r
+               $current = $target = $summary = NULL;\r
+               extract($details);\r
+\r
+               $info = array(\r
+                       'title' => $titleObj->getPrefixedText(),\r
+                       'pageid' => $current->getPage(),\r
+                       'summary' => $summary,\r
+                       'revid' => $titleObj->getLatestRevID(),\r
+                       'old_revid' => $current->getID(),\r
+                       'last_revid' => $target->getID()\r
+               );\r
+\r
+               $this->getResult()->addValue(null, $this->getModuleName(), $info);\r
+       }\r
+\r
+       protected function getAllowedParams() {\r
+               return array (\r
+                       'title' => null,\r
+                       'user' => null,\r
+                       'token' => null,\r
+                       'summary' => null,\r
+                       'markbot' => false\r
+               );\r
+       }\r
+\r
+       protected function getParamDescription() {\r
+               return array (\r
+                       'title' => 'Title of the page you want to rollback.',\r
+                       'user' => 'Name of the user whose edits are to be rolled back. If set incorrectly, you\'ll get a badtoken error.',\r
+                       'token' => 'A rollback token previously retrieved through prop=info',\r
+                       'summary' => 'Custom edit summary. If not set, default summary will be used.',\r
+                       'markbot' => 'Mark the reverted edits and the revert as bot edits'\r
+               );\r
+       }\r
+\r
+       protected function getDescription() {\r
+               return array(\r
+                               'Undoes the last edit to the page. If the last user who edited the page made multiple edits in a row,',\r
+                               'they will all be rolled back. You need to be logged in as a sysop to use this function, see also action=login.'\r
+                       );\r
+       }\r
+\r
+       protected function getExamples() {\r
+               return array (\r
+                       'api.php?action=rollback&title=Main%20Page&user=Catrope&token=123ABC',\r
+                       'api.php?action=rollback&title=Main%20Page&user=217.121.114.116&token=123ABC&summary=Reverting%20vandalism&markbot=1'\r
+               );\r
+       }\r
+\r
+       public function getVersion() {\r
+               return __CLASS__ . ': $Id: ApiRollback.php 22289 2007-05-20 23:31:44Z yurik $';\r
+       }\r
+}\r
diff --git a/includes/api/ApiUnblock.php b/includes/api/ApiUnblock.php
new file mode 100644 (file)
index 0000000..31bae7b
--- /dev/null
@@ -0,0 +1,130 @@
+<?php\r
+\r
+/*\r
+ * Created on Sep 7, 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
+ * @addtogroup API\r
+ */\r
+class ApiUnblock 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->requestWriteMode();\r
+               $params = $this->extractRequestParams();\r
+\r
+               if($params['gettoken'])\r
+               {\r
+                       $res['unblocktoken'] = $wgUser->editToken();\r
+                       $this->getResult()->addValue(null, $this->getModuleName(), $res);\r
+                       return;\r
+               }\r
+\r
+               if(is_null($params['id']) && is_null($params['user']))\r
+                       $this->dieUsage('Either the id or the user parameter must be set', 'notarget');\r
+               if(!is_null($params['id']) && !is_null($params['user']))\r
+                       $this->dieUsage('The id and user parameters can\'t be used together', 'idanduser');\r
+               if(is_null($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
+               if(!$wgUser->isAllowed('block'))\r
+                       $this->dieUsage('You don\'t have permission to unblock users', 'permissiondenied');\r
+               if(wfReadOnly())\r
+                       $this->dieUsage('The wiki is in read-only mode', 'readonly');\r
+\r
+               $id = $params['id'];\r
+               $user = $params['user'];\r
+               $reason = $params['reason'];\r
+               $dbw = wfGetDb(DB_MASTER);\r
+               $dbw->begin();\r
+               $retval = IPUnblockForm::doUnblock(&$id, &$user, &$reason, &$range);\r
+\r
+               switch($retval)\r
+               {\r
+                       case IPUnblockForm::UNBLOCK_SUCCESS:\r
+                               break; // We'll deal with that later\r
+                       case IPUnblockForm::UNBLOCK_NO_SUCH_ID:\r
+                               $this->dieUsage("There is no block with ID ``$id''", 'nosuchid');\r
+                       case IPUnblockForm::UNBLOCK_USER_NOT_BLOCKED:\r
+                               $this->dieUsage("User ``$user'' is not blocked", 'notblocked');\r
+                       case IPUnblockForm::UNBLOCK_BLOCKED_AS_RANGE:\r
+                               $this->dieUsage("IP address ``$user'' was blocked as part of range ``$range''. You can't unblock the IP invidually, but you can unblock the range as a whole.", 'blockedasrange');\r
+                       case IPUnblockForm::UNBLOCK_UNKNOWNERR:\r
+                               $this->dieUsage("Unknown error", 'unknownerr');\r
+                       default:\r
+                               $this->dieDebug(__METHOD__, "IPBlockForm::doBlock() returned an unknown error ($retval)");\r
+               }\r
+               $dbw->commit();\r
+               \r
+               $res['id'] = $id;\r
+               $res['user'] = $user;\r
+               $res['reason'] = $reason;\r
+               $this->getResult()->addValue(null, $this->getModuleName(), $res);\r
+       }\r
+\r
+       protected function getAllowedParams() {\r
+               return array (\r
+                       'id' => null,\r
+                       'user' => null,\r
+                       'token' => null,\r
+                       'gettoken' => false,\r
+                       'reason' => null,\r
+               );\r
+       }\r
+\r
+       protected function getParamDescription() {\r
+               return array (\r
+                       'id' => 'ID of the block you want to unblock (obtained through list=blocks). Cannot be user together with user',\r
+                       'user' => 'Username, IP address or IP range you want to unblock. Cannot be used together with id',\r
+                       'token' => 'An unblock token previously obtained through the gettoken parameter',\r
+                       'gettoken' => 'If set, an unblock token will be returned, and no other action will be taken',\r
+                       'reason' => 'Reason for unblock (optional)',\r
+               );\r
+       }\r
+\r
+       protected function getDescription() {\r
+               return array(\r
+                       'Unblock a user.'\r
+               );\r
+       }\r
+\r
+       protected function getExamples() {\r
+               return array (\r
+                       'api.php?action=unblock&id=105',\r
+                       'api.php?action=unblock&user=Bob&reason=Sorry%20Bob'\r
+               );\r
+       }\r
+\r
+       public function getVersion() {\r
+               return __CLASS__ . ': $Id$';\r
+       }\r
+}\r
diff --git a/includes/api/ApiUndelete.php b/includes/api/ApiUndelete.php
new file mode 100644 (file)
index 0000000..886bdca
--- /dev/null
@@ -0,0 +1,129 @@
+<?php\r
+\r
+/*\r
+ * Created on Jul 3, 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
+ * @addtogroup API\r
+ */\r
+class ApiUndelete 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->requestWriteMode();\r
+               $params = $this->extractRequestParams();\r
+               \r
+               $titleObj = NULL;\r
+               if(!isset($params['title']))\r
+                       $this->dieUsage('The title parameter must be set', 'notitle');\r
+               if(!isset($params['token']))\r
+                       $this->dieUsage('The token parameter must be set', 'notoken');\r
+\r
+               if(!$wgUser->isAllowed('delete'))\r
+                       $this->dieUsage('You don\'t have permission to restore deleted revisions', 'permissiondenied');\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(!$wgUser->matchEditToken($params['token']))\r
+                       $this->dieUsage('Invalid token', 'badtoken');\r
+\r
+               $titleObj = Title::newFromText($params['title']);\r
+               if(!$titleObj)\r
+                       $this->dieUsage("Bad title ``{$params['title']}''", 'invalidtitle');\r
+\r
+               // Convert timestamps\r
+               if(!is_array($params['timestamps']))\r
+                       $params['timestamps'] = array($params['timestamps']);\r
+               foreach($params['timestamps'] as $i => $ts)\r
+                       $params['timestamps'][$i] = wfTimestamp(TS_MW, $ts);\r
+\r
+               $pa = new PageArchive($titleObj);\r
+               $dbw = wfGetDb(DB_MASTER);\r
+               $dbw->begin();\r
+               $retval = $pa->undelete((isset($params['timestamps']) ? $params['timestamps'] : array()), $params['reason']);\r
+               if(!is_array($retval))\r
+                       switch($retval)\r
+                       {\r
+                               case PageArchive::UNDELETE_NOTHINGRESTORED:\r
+                                       $this->dieUsage('No revisions could be restored', 'norevs');\r
+                               case PageArchive::UNDELETE_NOTAVAIL:\r
+                                       $this->dieUsage('Not all requested revisions could be found', 'revsnotfound');\r
+                               case PageArchive::UNDELETE_UNKNOWNERR:\r
+                                       $this->dieUsage('Undeletion failed with unknown error', 'unknownerror');\r
+                       }\r
+               $dbw->commit();\r
+               \r
+               $info['title'] = $titleObj->getPrefixedText();\r
+               $info['revisions'] = $retval[0];\r
+               $info['fileversions'] = $retval[1];\r
+               $info['reason'] = $retval[2];\r
+               $this->getResult()->addValue(null, $this->getModuleName(), $info);\r
+       }\r
+\r
+       protected function getAllowedParams() {\r
+               return array (\r
+                       'title' => null,\r
+                       'token' => null,\r
+                       'reason' => "",\r
+                       'timestamps' => array(\r
+                               ApiBase :: PARAM_ISMULTI => true\r
+                       )\r
+               );\r
+       }\r
+\r
+       protected function getParamDescription() {\r
+               return array (\r
+                       'title' => 'Title of the page you want to restore.',\r
+                       'token' => 'An undelete token previously retrieved through list=deletedrevs',\r
+                       'reason' => 'Reason for restoring (optional)',\r
+                       'timestamps' => 'Timestamps of the revisions to restore. If not set, all revisions will be restored.'\r
+               );\r
+       }\r
+\r
+       protected function getDescription() {\r
+               return array(\r
+                       'Restore certain revisions of a deleted page. A list of deleted revisions (including timestamps) can be',\r
+                       'retrieved through list=deletedrevs'\r
+               );\r
+       }\r
+\r
+       protected function getExamples() {\r
+               return array (\r
+                       'api.php?action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page',\r
+                       'api.php?action=undelete&title=Main%20Page&token=123ABC&timestamps=20070703220045|20070702194856'\r
+               );\r
+       }\r
+\r
+       public function getVersion() {\r
+               return __CLASS__ . ': $Id: ApiUndelete.php 22289 2007-05-20 23:31:44Z yurik $';\r
+       }\r
+}\r