ApiQueryDeletedrevs.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /*
  3. * Created on Jul 2, 2007
  4. *
  5. * API for MediaWiki 1.8+
  6. *
  7. * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. */
  24. if (!defined('MEDIAWIKI')) {
  25. // Eclipse helper - will be ignored in production
  26. require_once ('ApiQueryBase.php');
  27. }
  28. /**
  29. * Query module to enumerate all available pages.
  30. *
  31. * @ingroup API
  32. */
  33. class ApiQueryDeletedrevs extends ApiQueryBase {
  34. public function __construct($query, $moduleName) {
  35. parent :: __construct($query, $moduleName, 'dr');
  36. }
  37. public function execute() {
  38. global $wgUser;
  39. // Before doing anything at all, let's check permissions
  40. if(!$wgUser->isAllowed('deletedhistory'))
  41. $this->dieUsage('You don\'t have permission to view deleted revision information', 'permissiondenied');
  42. $db = $this->getDB();
  43. $params = $this->extractRequestParams(false);
  44. $prop = array_flip($params['prop']);
  45. $fld_revid = isset($prop['revid']);
  46. $fld_user = isset($prop['user']);
  47. $fld_comment = isset($prop['comment']);
  48. $fld_minor = isset($prop['minor']);
  49. $fld_len = isset($prop['len']);
  50. $fld_content = isset($prop['content']);
  51. $fld_token = isset($prop['token']);
  52. $result = $this->getResult();
  53. $pageSet = $this->getPageSet();
  54. $titles = $pageSet->getTitles();
  55. $data = array();
  56. // This module operates in three modes:
  57. // 'revs': List deleted revs for certain titles
  58. // 'user': List deleted revs by a certain user
  59. // 'all': List all deleted revs
  60. $mode = 'all';
  61. if(count($titles) > 0)
  62. $mode = 'revs';
  63. else if(!is_null($params['user']))
  64. $mode = 'user';
  65. if(!is_null($params['user']) && !is_null($params['excludeuser']))
  66. $this->dieUsage('user and excludeuser cannot be used together', 'badparams');
  67. $this->addTables('archive');
  68. $this->addWhere('ar_deleted = 0');
  69. $this->addFields(array('ar_title', 'ar_namespace', 'ar_timestamp'));
  70. if($fld_revid)
  71. $this->addFields('ar_rev_id');
  72. if($fld_user)
  73. $this->addFields('ar_user_text');
  74. if($fld_comment)
  75. $this->addFields('ar_comment');
  76. if($fld_minor)
  77. $this->addFields('ar_minor_edit');
  78. if($fld_len)
  79. $this->addFields('ar_len');
  80. if($fld_content)
  81. {
  82. $this->addTables('text');
  83. $this->addFields(array('ar_text', 'ar_text_id', 'old_text', 'old_flags'));
  84. $this->addWhere('ar_text_id = old_id');
  85. // This also means stricter restrictions
  86. if(!$wgUser->isAllowed('undelete'))
  87. $this->dieUsage('You don\'t have permission to view deleted revision content', 'permissiondenied');
  88. }
  89. // Check limits
  90. $userMax = $fld_content ? ApiBase :: LIMIT_SML1 : ApiBase :: LIMIT_BIG1;
  91. $botMax = $fld_content ? ApiBase :: LIMIT_SML2 : ApiBase :: LIMIT_BIG2;
  92. $limit = $params['limit'];
  93. if( $limit == 'max' ) {
  94. $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
  95. $this->getResult()->addValue( 'limits', $this->getModuleName(), $limit );
  96. }
  97. $this->validateLimit('limit', $limit, 1, $userMax, $botMax);
  98. if($fld_token)
  99. // Undelete tokens are identical for all pages, so we cache one here
  100. $token = $wgUser->editToken();
  101. // We need a custom WHERE clause that matches all titles.
  102. if($mode == 'revs')
  103. {
  104. $lb = new LinkBatch($titles);
  105. $where = $lb->constructSet('ar', $db);
  106. $this->addWhere($where);
  107. }
  108. elseif($mode == 'all')
  109. {
  110. $this->addWhereFld('ar_namespace', $params['namespace']);
  111. if(!is_null($params['from']))
  112. {
  113. $from = $this->getDB()->strencode($this->titleToKey($params['from']));
  114. $this->addWhere("ar_title >= '$from'");
  115. }
  116. }
  117. if(!is_null($params['user'])) {
  118. $this->addWhereFld('ar_user_text', $params['user']);
  119. } elseif(!is_null($params['excludeuser'])) {
  120. $this->addWhere('ar_user_text != ' .
  121. $this->getDB()->addQuotes($params['excludeuser']));
  122. }
  123. if(!is_null($params['continue']) && ($mode == 'all' || $mode == 'revs'))
  124. {
  125. $cont = explode('|', $params['continue']);
  126. if(count($cont) != 3)
  127. $this->dieUsage("Invalid continue param. You should pass the original value returned by the previous query", "badcontinue");
  128. $ns = intval($cont[0]);
  129. $title = $this->getDB()->strencode($this->titleToKey($cont[1]));
  130. $ts = $this->getDB()->strencode($cont[2]);
  131. $op = ($params['dir'] == 'newer' ? '>' : '<');
  132. $this->addWhere("ar_namespace $op $ns OR " .
  133. "(ar_namespace = $ns AND " .
  134. "(ar_title $op '$title' OR " .
  135. "(ar_title = '$title' AND " .
  136. "ar_timestamp = '$ts')))");
  137. }
  138. $this->addOption('LIMIT', $limit + 1);
  139. $this->addOption('USE INDEX', array('archive' => ($mode == 'user' ? 'usertext_timestamp' : 'name_title_timestamp')));
  140. if($mode == 'all')
  141. {
  142. if($params['unique'])
  143. {
  144. $this->addOption('GROUP BY', 'ar_title');
  145. $this->addOption('ORDER BY', 'ar_title');
  146. }
  147. else
  148. $this->addOption('ORDER BY', 'ar_title, ar_timestamp');
  149. }
  150. else
  151. {
  152. if($mode == 'revs')
  153. {
  154. // Sort by ns and title in the same order as timestamp for efficiency
  155. $this->addWhereRange('ar_namespace', $params['dir'], null, null);
  156. $this->addWhereRange('ar_title', $params['dir'], null, null);
  157. }
  158. $this->addWhereRange('ar_timestamp', $params['dir'], $params['start'], $params['end']);
  159. }
  160. $res = $this->select(__METHOD__);
  161. $pageMap = array(); // Maps ns&title to (fake) pageid
  162. $count = 0;
  163. $newPageID = 0;
  164. while($row = $db->fetchObject($res))
  165. {
  166. if(++$count > $limit)
  167. {
  168. // We've had enough
  169. if($mode == 'all' || $mode == 'revs')
  170. $this->setContinueEnumParameter('continue', intval($row->ar_namespace) . '|' .
  171. $this->keyToTitle($row->ar_title) . '|' . $row->ar_timestamp);
  172. else
  173. $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->ar_timestamp));
  174. break;
  175. }
  176. $rev = array();
  177. $rev['timestamp'] = wfTimestamp(TS_ISO_8601, $row->ar_timestamp);
  178. if($fld_revid)
  179. $rev['revid'] = intval($row->ar_rev_id);
  180. if($fld_user)
  181. $rev['user'] = $row->ar_user_text;
  182. if($fld_comment)
  183. $rev['comment'] = $row->ar_comment;
  184. if($fld_minor)
  185. if($row->ar_minor_edit == 1)
  186. $rev['minor'] = '';
  187. if($fld_len)
  188. $rev['len'] = $row->ar_len;
  189. if($fld_content)
  190. ApiResult::setContent($rev, Revision::getRevisionText($row));
  191. if(!isset($pageMap[$row->ar_namespace][$row->ar_title]))
  192. {
  193. $pageID = $newPageID++;
  194. $pageMap[$row->ar_namespace][$row->ar_title] = $pageID;
  195. $t = Title::makeTitle($row->ar_namespace, $row->ar_title);
  196. $a['revisions'] = array($rev);
  197. $result->setIndexedTagName($a['revisions'], 'rev');
  198. ApiQueryBase::addTitleInfo($a, $t);
  199. if($fld_token)
  200. $a['token'] = $token;
  201. $fit = $result->addValue(array('query', $this->getModuleName()), $pageID, $a);
  202. }
  203. else
  204. {
  205. $pageID = $pageMap[$row->ar_namespace][$row->ar_title];
  206. $fit = $result->addValue(
  207. array('query', $this->getModuleName(), $pageID, 'revisions'),
  208. null, $rev);
  209. }
  210. if(!$fit)
  211. {
  212. if($mode == 'all' || $mode == 'revs')
  213. $this->setContinueEnumParameter('continue', intval($row->ar_namespace) . '|' .
  214. $this->keyToTitle($row->ar_title) . '|' . $row->ar_timestamp);
  215. else
  216. $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->ar_timestamp));
  217. break;
  218. }
  219. }
  220. $db->freeResult($res);
  221. $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'page');
  222. }
  223. public function getAllowedParams() {
  224. return array (
  225. 'start' => array(
  226. ApiBase :: PARAM_TYPE => 'timestamp'
  227. ),
  228. 'end' => array(
  229. ApiBase :: PARAM_TYPE => 'timestamp',
  230. ),
  231. 'dir' => array(
  232. ApiBase :: PARAM_TYPE => array(
  233. 'newer',
  234. 'older'
  235. ),
  236. ApiBase :: PARAM_DFLT => 'older'
  237. ),
  238. 'from' => null,
  239. 'continue' => null,
  240. 'unique' => false,
  241. 'user' => array(
  242. ApiBase :: PARAM_TYPE => 'user'
  243. ),
  244. 'excludeuser' => array(
  245. ApiBase :: PARAM_TYPE => 'user'
  246. ),
  247. 'namespace' => array(
  248. ApiBase :: PARAM_TYPE => 'namespace',
  249. ApiBase :: PARAM_DFLT => 0,
  250. ),
  251. 'limit' => array(
  252. ApiBase :: PARAM_DFLT => 10,
  253. ApiBase :: PARAM_TYPE => 'limit',
  254. ApiBase :: PARAM_MIN => 1,
  255. ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
  256. ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
  257. ),
  258. 'prop' => array(
  259. ApiBase :: PARAM_DFLT => 'user|comment',
  260. ApiBase :: PARAM_TYPE => array(
  261. 'revid',
  262. 'user',
  263. 'comment',
  264. 'minor',
  265. 'len',
  266. 'content',
  267. 'token'
  268. ),
  269. ApiBase :: PARAM_ISMULTI => true
  270. ),
  271. );
  272. }
  273. public function getParamDescription() {
  274. return array (
  275. 'start' => 'The timestamp to start enumerating from. (1,2)',
  276. 'end' => 'The timestamp to stop enumerating at. (1,2)',
  277. 'dir' => 'The direction in which to enumerate. (1,2)',
  278. 'limit' => 'The maximum amount of revisions to list',
  279. 'prop' => 'Which properties to get',
  280. 'namespace' => 'Only list pages in this namespace (3)',
  281. 'user' => 'Only list revisions by this user',
  282. 'excludeuser' => 'Don\'t list revisions by this user',
  283. 'from' => 'Start listing at this title (3)',
  284. 'continue' => 'When more results are available, use this to continue (3)',
  285. 'unique' => 'List only one revision for each page (3)',
  286. );
  287. }
  288. public function getDescription() {
  289. return array( 'List deleted revisions.',
  290. 'This module operates in three modes:',
  291. '1) List deleted revisions for the given title(s), sorted by timestamp',
  292. '2) List deleted contributions for the given user, sorted by timestamp (no titles specified)',
  293. '3) List all deleted revisions in the given namespace, sorted by title and timestamp (no titles specified, druser not set)',
  294. 'Certain parameters only apply to some modes and are ignored in others.',
  295. 'For instance, a parameter marked (1) only applies to mode 1 and is ignored in modes 2 and 3.',
  296. );
  297. }
  298. protected function getExamples() {
  299. return array (
  300. 'List the last deleted revisions of Main Page and Talk:Main Page, with content (mode 1):',
  301. ' api.php?action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&drprop=user|comment|content',
  302. 'List the last 50 deleted contributions by Bob (mode 2):',
  303. ' api.php?action=query&list=deletedrevs&druser=Bob&drlimit=50',
  304. 'List the first 50 deleted revisions in the main namespace (mode 3):',
  305. ' api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50',
  306. 'List the first 50 deleted pages in the Talk namespace (mode 3):',
  307. ' api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique',
  308. );
  309. }
  310. public function getVersion() {
  311. return __CLASS__ . ': $Id: ApiQueryDeletedrevs.php 50220 2009-05-05 14:07:59Z tstarling $';
  312. }
  313. }