(bug 18880) Make LogEventsList::showLogExtract() more versatile by allowing it to...
[lhc/web/wiklou.git] / includes / api / ApiQueryDeletedrevs.php
index 67694d7..9da080a 100644 (file)
-<?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
+<?php
+
+/*
+ * Created on Jul 2, 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 ('ApiQueryBase.php');
+}
+
+/**
+ * Query module to enumerate all available pages.
+ *
+ * @ingroup API
+ */
+class ApiQueryDeletedrevs extends ApiQueryBase {
+
+       public function __construct($query, $moduleName) {
+               parent :: __construct($query, $moduleName, 'dr');
+       }
+
+       public function execute() {
+
+               global $wgUser;
+               // Before doing anything at all, let's check permissions
+               if(!$wgUser->isAllowed('deletedhistory'))
+                       $this->dieUsage('You don\'t have permission to view deleted revision information', 'permissiondenied');
+
+               $db = $this->getDB();
+               $params = $this->extractRequestParams(false);
+               $prop = array_flip($params['prop']);
+               $fld_revid = isset($prop['revid']);
+               $fld_user = isset($prop['user']);
+               $fld_comment = isset($prop['comment']);
+               $fld_minor = isset($prop['minor']);
+               $fld_len = isset($prop['len']);
+               $fld_content = isset($prop['content']);
+               $fld_token = isset($prop['token']);
+               
+               $result = $this->getResult();
+               $pageSet = $this->getPageSet();
+               $titles = $pageSet->getTitles();
+               $data = array();
+               
+               // This module operates in three modes:
+               // 'revs': List deleted revs for certain titles
+               // 'user': List deleted revs by a certain user
+               // 'all': List all deleted revs
+               $mode = 'all';
+               if(count($titles) > 0)
+                       $mode = 'revs';
+               else if(!is_null($params['user']))
+                       $mode = 'user';
+               
+               if(!is_null($params['user']) && !is_null($params['excludeuser']))
+                               $this->dieUsage('user and excludeuser cannot be used together', 'badparams');
+
+               $this->addTables('archive');
+               $this->addWhere('ar_deleted = 0');
+               $this->addFields(array('ar_title', 'ar_namespace', 'ar_timestamp'));
+               if($fld_revid)
+                       $this->addFields('ar_rev_id');
+               if($fld_user)
+                       $this->addFields('ar_user_text');
+               if($fld_comment)
+                       $this->addFields('ar_comment');
+               if($fld_minor)
+                       $this->addFields('ar_minor_edit');
+               if($fld_len)
+                       $this->addFields('ar_len');
+               if($fld_content)
+               {
+                       $this->addTables('text');
+                       $this->addFields(array('ar_text', 'ar_text_id', 'old_text', 'old_flags'));
+                       $this->addWhere('ar_text_id = old_id');
+
+                       // This also means stricter restrictions
+                       if(!$wgUser->isAllowed('undelete'))
+                               $this->dieUsage('You don\'t have permission to view deleted revision content', 'permissiondenied');
+               }
+               // Check limits
+               $userMax = $fld_content ? ApiBase :: LIMIT_SML1 : ApiBase :: LIMIT_BIG1;
+               $botMax  = $fld_content ? ApiBase :: LIMIT_SML2 : ApiBase :: LIMIT_BIG2;
+
+               $limit = $params['limit'];
+
+               if( $limit == 'max' ) {
+                       $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
+                       $this->getResult()->addValue( 'limits', $this->getModuleName(), $limit );
+               }
+
+               $this->validateLimit('limit', $limit, 1, $userMax, $botMax);
+
+               if($fld_token)
+                       // Undelete tokens are identical for all pages, so we cache one here
+                       $token = $wgUser->editToken();
+
+               // We need a custom WHERE clause that matches all titles.
+               if($mode == 'revs')
+               {
+                       $lb = new LinkBatch($titles);
+                       $where = $lb->constructSet('ar', $db);
+                       $this->addWhere($where);
+               }
+               elseif($mode == 'all')
+               {
+                       $this->addWhereFld('ar_namespace', $params['namespace']);
+                       if(!is_null($params['from']))
+                       {
+                               $from = $this->getDB()->strencode($this->titleToKey($params['from']));
+                               $this->addWhere("ar_title >= '$from'");
+                       }
+               }
+               
+               if(!is_null($params['user'])) {
+                       $this->addWhereFld('ar_user_text', $params['user']);
+               } elseif(!is_null($params['excludeuser'])) {
+                       $this->addWhere('ar_user_text != ' .
+                               $this->getDB()->addQuotes($params['excludeuser']));
+               }
+               
+               if(!is_null($params['continue']) && ($mode == 'all' || $mode == 'revs'))
+               {
+                       $cont = explode('|', $params['continue']);
+                       if(count($cont) != 3)
+                               $this->dieUsage("Invalid continue param. You should pass the original value returned by the previous query", "badcontinue");
+                       $ns = intval($cont[0]);
+                       $title = $this->getDB()->strencode($this->titleToKey($cont[1]));
+                       $ts = $this->getDB()->strencode($cont[2]);
+                       $op = ($params['dir'] == 'newer' ? '>' : '<');
+                       $this->addWhere("ar_namespace $op $ns OR " .
+                                       "(ar_namespace = $ns AND " .
+                                       "(ar_title $op '$title' OR " .
+                                       "(ar_title = '$title' AND " .
+                                       "ar_timestamp = '$ts')))");
+               }
+
+               $this->addOption('LIMIT', $limit + 1);
+               $this->addOption('USE INDEX', array('archive' => ($mode == 'user' ? 'usertext_timestamp' : 'name_title_timestamp')));
+               if($mode == 'all')
+               {
+                       if($params['unique'])
+                       {
+                               $this->addOption('GROUP BY', 'ar_title');
+                               $this->addOption('ORDER BY', 'ar_title');
+                       }
+                       else
+                               $this->addOption('ORDER BY', 'ar_title, ar_timestamp');
+               }
+               else
+               {
+                       if($mode == 'revs')
+                       {
+                               // Sort by ns and title in the same order as timestamp for efficiency
+                               $this->addWhereRange('ar_namespace', $params['dir'], null, null);
+                               $this->addWhereRange('ar_title', $params['dir'], null, null);
+                       }
+                       $this->addWhereRange('ar_timestamp', $params['dir'], $params['start'], $params['end']);
+               }
+               $res = $this->select(__METHOD__);
+               $pageMap = array(); // Maps ns&title to (fake) pageid
+               $count = 0;
+               $newPageID = 0;
+               while($row = $db->fetchObject($res))
+               {
+                       if(++$count > $limit)
+                       {
+                               // We've had enough
+                               if($mode == 'all' || $mode == 'revs')
+                                       $this->setContinueEnumParameter('continue', intval($row->ar_namespace) . '|' .
+                                               $this->keyToTitle($row->ar_title) . '|' . $row->ar_timestamp);
+                               else
+                                       $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->ar_timestamp));
+                               break;
+                       }
+
+                       $rev = array();
+                       $rev['timestamp'] = wfTimestamp(TS_ISO_8601, $row->ar_timestamp);
+                       if($fld_revid)
+                               $rev['revid'] = intval($row->ar_rev_id);
+                       if($fld_user)
+                               $rev['user'] = $row->ar_user_text;
+                       if($fld_comment)
+                               $rev['comment'] = $row->ar_comment;
+                       if($fld_minor)
+                               if($row->ar_minor_edit == 1)
+                                       $rev['minor'] = '';
+                       if($fld_len)
+                               $rev['len'] = $row->ar_len;
+                       if($fld_content)
+                               ApiResult::setContent($rev, Revision::getRevisionText($row));
+
+                       if(!isset($pageMap[$row->ar_namespace][$row->ar_title]))
+                       {
+                               $pageID = $newPageID++;
+                               $pageMap[$row->ar_namespace][$row->ar_title] = $pageID;
+                               $t = Title::makeTitle($row->ar_namespace, $row->ar_title);
+                               $a['revisions'] = array($rev);
+                               $result->setIndexedTagName($a['revisions'], 'rev');
+                               ApiQueryBase::addTitleInfo($a, $t);
+                               if($fld_token)
+                                       $a['token'] = $token;
+                               $fit = $result->addValue(array('query', $this->getModuleName()), $pageID, $a);
+                       }
+                       else
+                       {
+                               $pageID = $pageMap[$row->ar_namespace][$row->ar_title];
+                               $fit = $result->addValue(
+                                       array('query', $this->getModuleName(), $pageID, 'revisions'),
+                                       null, $rev);
+                       }
+                       if(!$fit)
+                       {
+                               if($mode == 'all' || $mode == 'revs')
+                                       $this->setContinueEnumParameter('continue', intval($row->ar_namespace) . '|' .
+                                               $this->keyToTitle($row->ar_title) . '|' . $row->ar_timestamp);
+                               else
+                                       $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->ar_timestamp));
+                               break;
+                       }
+               }
+               $db->freeResult($res);
+               $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'page');
+       }
+
+       public function getAllowedParams() {
+               return array (
+                       'start' => array(
+                               ApiBase :: PARAM_TYPE => 'timestamp'
+                       ),
+                       'end' => array(
+                               ApiBase :: PARAM_TYPE => 'timestamp',
+                       ),
+                       'dir' => array(
+                               ApiBase :: PARAM_TYPE => array(
+                                       'newer',
+                                       'older'
+                               ),
+                               ApiBase :: PARAM_DFLT => 'older'
+                       ),
+                       'from' => null,
+                       'continue' => null,
+                       'unique' => false,
+                       'user' => array(
+                               ApiBase :: PARAM_TYPE => 'user'
+                       ),
+                       'excludeuser' => array(
+                               ApiBase :: PARAM_TYPE => 'user'
+                       ),
+                       'namespace' => array(
+                               ApiBase :: PARAM_TYPE => 'namespace',
+                               ApiBase :: PARAM_DFLT => 0,
+                       ),
+                       'limit' => array(
+                               ApiBase :: PARAM_DFLT => 10,
+                               ApiBase :: PARAM_TYPE => 'limit',
+                               ApiBase :: PARAM_MIN => 1,
+                               ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
+                               ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
+                       ),
+                       'prop' => array(
+                               ApiBase :: PARAM_DFLT => 'user|comment',
+                               ApiBase :: PARAM_TYPE => array(
+                                       'revid',
+                                       'user',
+                                       'comment',
+                                       'minor',
+                                       'len',
+                                       'content',
+                                       'token'
+                               ),
+                               ApiBase :: PARAM_ISMULTI => true
+                       ),
+               );
+       }
+
+       public function getParamDescription() {
+               return array (
+                       'start' => 'The timestamp to start enumerating from. (1,2)',
+                       'end' => 'The timestamp to stop enumerating at. (1,2)',
+                       'dir' => 'The direction in which to enumerate. (1,2)',
+                       'limit' => 'The maximum amount of revisions to list',
+                       'prop' => 'Which properties to get',
+                       'namespace' => 'Only list pages in this namespace (3)',
+                       'user' => 'Only list revisions by this user',
+                       'excludeuser' => 'Don\'t list revisions by this user',
+                       'from' => 'Start listing at this title (3)',
+                       'continue' => 'When more results are available, use this to continue (3)',
+                       'unique' => 'List only one revision for each page (3)',
+               );
+       }
+
+       public function getDescription() {
+               return array(   'List deleted revisions.',
+                               'This module operates in three modes:',
+                               '1) List deleted revisions for the given title(s), sorted by timestamp',
+                               '2) List deleted contributions for the given user, sorted by timestamp (no titles specified)',
+                               '3) List all deleted revisions in the given namespace, sorted by title and timestamp (no titles specified, druser not set)',
+                               'Certain parameters only apply to some modes and are ignored in others.',
+                               'For instance, a parameter marked (1) only applies to mode 1 and is ignored in modes 2 and 3.',
+               );
+       }
+
+       protected function getExamples() {
+               return array (
+                       'List the last deleted revisions of Main Page and Talk:Main Page, with content (mode 1):',
+                       '  api.php?action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&drprop=user|comment|content',
+                       'List the last 50 deleted contributions by Bob (mode 2):',
+                       '  api.php?action=query&list=deletedrevs&druser=Bob&drlimit=50',
+                       'List the first 50 deleted revisions in the main namespace (mode 3):',
+                       '  api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50',
+                       'List the first 50 deleted pages in the Talk namespace (mode 3):',
+                       '  api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique',
+               );
+       }
+
+       public function getVersion() {
+               return __CLASS__ . ': $Id$';
+       }
+}
\ No newline at end of file