PageRenderTime 28ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/phase3/includes/api/ApiQueryDeletedrevs.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 408 lines | 331 code | 35 blank | 42 comment | 67 complexity | b8c9fa4dc58d6dff2e3cdd8f35cd1d58 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. *
  5. * Created on Jul 2, 2007
  6. *
  7. * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@gmail.com
  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. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  22. * http://www.gnu.org/copyleft/gpl.html
  23. *
  24. * @file
  25. */
  26. if ( !defined( 'MEDIAWIKI' ) ) {
  27. // Eclipse helper - will be ignored in production
  28. require_once( 'ApiQueryBase.php' );
  29. }
  30. /**
  31. * Query module to enumerate all deleted revisions.
  32. *
  33. * @ingroup API
  34. */
  35. class ApiQueryDeletedrevs extends ApiQueryBase {
  36. public function __construct( $query, $moduleName ) {
  37. parent::__construct( $query, $moduleName, 'dr' );
  38. }
  39. public function execute() {
  40. global $wgUser;
  41. // Before doing anything at all, let's check permissions
  42. if ( !$wgUser->isAllowed( 'deletedhistory' ) || $wgUser->isBlocked() ) {
  43. $this->dieUsage( 'You don\'t have permission to view deleted revision information', 'permissiondenied' );
  44. }
  45. $db = $this->getDB();
  46. $params = $this->extractRequestParams( false );
  47. $prop = array_flip( $params['prop'] );
  48. $fld_parentid = isset( $prop['parentid'] );
  49. $fld_revid = isset( $prop['revid'] );
  50. $fld_user = isset( $prop['user'] );
  51. $fld_userid = isset( $prop['userid'] );
  52. $fld_comment = isset( $prop['comment'] );
  53. $fld_parsedcomment = isset ( $prop['parsedcomment'] );
  54. $fld_minor = isset( $prop['minor'] );
  55. $fld_len = isset( $prop['len'] );
  56. $fld_content = isset( $prop['content'] );
  57. $fld_token = isset( $prop['token'] );
  58. $result = $this->getResult();
  59. $pageSet = $this->getPageSet();
  60. $titles = $pageSet->getTitles();
  61. // This module operates in three modes:
  62. // 'revs': List deleted revs for certain titles (1)
  63. // 'user': List deleted revs by a certain user (2)
  64. // 'all': List all deleted revs in NS (3)
  65. $mode = 'all';
  66. if ( count( $titles ) > 0 ) {
  67. $mode = 'revs';
  68. } elseif ( !is_null( $params['user'] ) ) {
  69. $mode = 'user';
  70. }
  71. if ( $mode == 'revs' || $mode == 'user' ) {
  72. // Ignore namespace and unique due to inability to know whether they were purposely set
  73. foreach( array( 'from', 'to', 'prefix', /*'namespace',*/ 'continue', /*'unique'*/ ) as $p ) {
  74. if ( !is_null( $params[$p] ) ) {
  75. $this->dieUsage( "The '{$p}' parameter cannot be used in modes 1 or 2", 'badparams');
  76. }
  77. }
  78. } else {
  79. foreach( array( 'start', 'end' ) as $p ) {
  80. if ( !is_null( $params[$p] ) ) {
  81. $this->dieUsage( "The {$p} parameter cannot be used in mode 3", 'badparams');
  82. }
  83. }
  84. }
  85. if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
  86. $this->dieUsage( 'user and excludeuser cannot be used together', 'badparams' );
  87. }
  88. $this->addTables( 'archive' );
  89. $this->addWhere( 'ar_deleted = 0' );
  90. $this->addFields( array( 'ar_title', 'ar_namespace', 'ar_timestamp' ) );
  91. $this->addFieldsIf( 'ar_parent_id', $fld_parentid );
  92. $this->addFieldsIf( 'ar_rev_id', $fld_revid );
  93. $this->addFieldsIf( 'ar_user_text', $fld_user );
  94. $this->addFieldsIf( 'ar_user', $fld_userid );
  95. $this->addFieldsIf( 'ar_comment', $fld_comment || $fld_parsedcomment );
  96. $this->addFieldsIf( 'ar_minor_edit', $fld_minor );
  97. $this->addFieldsIf( 'ar_len', $fld_len );
  98. if ( $fld_content ) {
  99. $this->addTables( 'text' );
  100. $this->addFields( array( 'ar_text', 'ar_text_id', 'old_text', 'old_flags' ) );
  101. $this->addWhere( 'ar_text_id = old_id' );
  102. // This also means stricter restrictions
  103. if ( !$wgUser->isAllowed( 'undelete' ) ) {
  104. $this->dieUsage( 'You don\'t have permission to view deleted revision content', 'permissiondenied' );
  105. }
  106. }
  107. // Check limits
  108. $userMax = $fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1;
  109. $botMax = $fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2;
  110. $limit = $params['limit'];
  111. if ( $limit == 'max' ) {
  112. $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
  113. $this->getResult()->setParsedLimit( $this->getModuleName(), $limit );
  114. }
  115. $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
  116. if ( $fld_token ) {
  117. // Undelete tokens are identical for all pages, so we cache one here
  118. $token = $wgUser->editToken( '', $this->getMain()->getRequest() );
  119. }
  120. $dir = $params['dir'];
  121. // We need a custom WHERE clause that matches all titles.
  122. if ( $mode == 'revs' ) {
  123. $lb = new LinkBatch( $titles );
  124. $where = $lb->constructSet( 'ar', $db );
  125. $this->addWhere( $where );
  126. } elseif ( $mode == 'all' ) {
  127. $this->addWhereFld( 'ar_namespace', $params['namespace'] );
  128. $from = is_null( $params['from'] ) ? null : $this->titleToKey( $params['from'] );
  129. $to = is_null( $params['to'] ) ? null : $this->titleToKey( $params['to'] );
  130. $this->addWhereRange( 'ar_title', $dir, $from, $to );
  131. if ( isset( $params['prefix'] ) ) {
  132. $this->addWhere( 'ar_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
  133. }
  134. }
  135. if ( !is_null( $params['user'] ) ) {
  136. $this->addWhereFld( 'ar_user_text', $params['user'] );
  137. } elseif ( !is_null( $params['excludeuser'] ) ) {
  138. $this->addWhere( 'ar_user_text != ' .
  139. $this->getDB()->addQuotes( $params['excludeuser'] ) );
  140. }
  141. if ( !is_null( $params['continue'] ) && ( $mode == 'all' || $mode == 'revs' ) ) {
  142. $cont = explode( '|', $params['continue'] );
  143. if ( count( $cont ) != 3 ) {
  144. $this->dieUsage( 'Invalid continue param. You should pass the original value returned by the previous query', 'badcontinue' );
  145. }
  146. $ns = intval( $cont[0] );
  147. $title = $this->getDB()->strencode( $this->titleToKey( $cont[1] ) );
  148. $ts = $this->getDB()->strencode( $cont[2] );
  149. $op = ( $dir == 'newer' ? '>' : '<' );
  150. $this->addWhere( "ar_namespace $op $ns OR " .
  151. "(ar_namespace = $ns AND " .
  152. "(ar_title $op '$title' OR " .
  153. "(ar_title = '$title' AND " .
  154. "ar_timestamp $op= '$ts')))" );
  155. }
  156. $this->addOption( 'LIMIT', $limit + 1 );
  157. $this->addOption( 'USE INDEX', array( 'archive' => ( $mode == 'user' ? 'usertext_timestamp' : 'name_title_timestamp' ) ) );
  158. if ( $mode == 'all' ) {
  159. if ( $params['unique'] ) {
  160. $this->addOption( 'GROUP BY', 'ar_title' );
  161. } else {
  162. $this->addOption( 'ORDER BY', 'ar_title, ar_timestamp' );
  163. }
  164. } else {
  165. if ( $mode == 'revs' ) {
  166. // Sort by ns and title in the same order as timestamp for efficiency
  167. $this->addWhereRange( 'ar_namespace', $dir, null, null );
  168. $this->addWhereRange( 'ar_title', $dir, null, null );
  169. }
  170. $this->addWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
  171. }
  172. $res = $this->select( __METHOD__ );
  173. $pageMap = array(); // Maps ns&title to (fake) pageid
  174. $count = 0;
  175. $newPageID = 0;
  176. foreach ( $res as $row ) {
  177. if ( ++$count > $limit ) {
  178. // We've had enough
  179. if ( $mode == 'all' || $mode == 'revs' ) {
  180. $this->setContinueEnumParameter( 'continue', intval( $row->ar_namespace ) . '|' .
  181. $this->keyToTitle( $row->ar_title ) . '|' . $row->ar_timestamp );
  182. } else {
  183. $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ar_timestamp ) );
  184. }
  185. break;
  186. }
  187. $rev = array();
  188. $rev['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ar_timestamp );
  189. if ( $fld_revid ) {
  190. $rev['revid'] = intval( $row->ar_rev_id );
  191. }
  192. if ( $fld_parentid && !is_null( $row->ar_parent_id ) ) {
  193. $rev['parentid'] = intval( $row->ar_parent_id );
  194. }
  195. if ( $fld_user ) {
  196. $rev['user'] = $row->ar_user_text;
  197. }
  198. if ( $fld_userid ) {
  199. $rev['userid'] = $row->ar_user;
  200. }
  201. if ( $fld_comment ) {
  202. $rev['comment'] = $row->ar_comment;
  203. }
  204. $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
  205. if ( $fld_parsedcomment ) {
  206. $rev['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->ar_comment, $title );
  207. }
  208. if ( $fld_minor && $row->ar_minor_edit == 1 ) {
  209. $rev['minor'] = '';
  210. }
  211. if ( $fld_len ) {
  212. $rev['len'] = $row->ar_len;
  213. }
  214. if ( $fld_content ) {
  215. ApiResult::setContent( $rev, Revision::getRevisionText( $row ) );
  216. }
  217. if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
  218. $pageID = $newPageID++;
  219. $pageMap[$row->ar_namespace][$row->ar_title] = $pageID;
  220. $a['revisions'] = array( $rev );
  221. $result->setIndexedTagName( $a['revisions'], 'rev' );
  222. ApiQueryBase::addTitleInfo( $a, $title );
  223. if ( $fld_token ) {
  224. $a['token'] = $token;
  225. }
  226. $fit = $result->addValue( array( 'query', $this->getModuleName() ), $pageID, $a );
  227. } else {
  228. $pageID = $pageMap[$row->ar_namespace][$row->ar_title];
  229. $fit = $result->addValue(
  230. array( 'query', $this->getModuleName(), $pageID, 'revisions' ),
  231. null, $rev );
  232. }
  233. if ( !$fit ) {
  234. if ( $mode == 'all' || $mode == 'revs' ) {
  235. $this->setContinueEnumParameter( 'continue', intval( $row->ar_namespace ) . '|' .
  236. $this->keyToTitle( $row->ar_title ) . '|' . $row->ar_timestamp );
  237. } else {
  238. $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->ar_timestamp ) );
  239. }
  240. break;
  241. }
  242. }
  243. $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
  244. }
  245. public function getAllowedParams() {
  246. return array(
  247. 'start' => array(
  248. ApiBase::PARAM_TYPE => 'timestamp'
  249. ),
  250. 'end' => array(
  251. ApiBase::PARAM_TYPE => 'timestamp',
  252. ),
  253. 'dir' => array(
  254. ApiBase::PARAM_TYPE => array(
  255. 'newer',
  256. 'older'
  257. ),
  258. ApiBase::PARAM_DFLT => 'older'
  259. ),
  260. 'from' => null,
  261. 'to' => null,
  262. 'prefix' => null,
  263. 'continue' => null,
  264. 'unique' => false,
  265. 'user' => array(
  266. ApiBase::PARAM_TYPE => 'user'
  267. ),
  268. 'excludeuser' => array(
  269. ApiBase::PARAM_TYPE => 'user'
  270. ),
  271. 'namespace' => array(
  272. ApiBase::PARAM_TYPE => 'namespace',
  273. ApiBase::PARAM_DFLT => 0,
  274. ),
  275. 'limit' => array(
  276. ApiBase::PARAM_DFLT => 10,
  277. ApiBase::PARAM_TYPE => 'limit',
  278. ApiBase::PARAM_MIN => 1,
  279. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  280. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  281. ),
  282. 'prop' => array(
  283. ApiBase::PARAM_DFLT => 'user|comment',
  284. ApiBase::PARAM_TYPE => array(
  285. 'revid',
  286. 'parentid',
  287. 'user',
  288. 'userid',
  289. 'comment',
  290. 'parsedcomment',
  291. 'minor',
  292. 'len',
  293. 'content',
  294. 'token'
  295. ),
  296. ApiBase::PARAM_ISMULTI => true
  297. ),
  298. );
  299. }
  300. public function getParamDescription() {
  301. return array(
  302. 'start' => 'The timestamp to start enumerating from (1,2)',
  303. 'end' => 'The timestamp to stop enumerating at (1,2)',
  304. 'dir' => $this->getDirectionDescription( $this->getModulePrefix(), ' (1, 3)' ),
  305. 'from' => 'Start listing at this title (3)',
  306. 'to' => 'Stop listing at this title (3)',
  307. 'prefix' => 'Search for all page titles that begin with this value (3)',
  308. 'limit' => 'The maximum amount of revisions to list',
  309. 'prop' => array(
  310. 'Which properties to get',
  311. ' revid - Adds the revision ID of the deleted revision',
  312. ' parentid - Adds the revision ID of the previous revision to the page',
  313. ' user - Adds the user who made the revision',
  314. ' userid - Adds the user ID whom made the revision',
  315. ' comment - Adds the comment of the revision',
  316. ' parsedcomment - Adds the parsed comment of the revision',
  317. ' minor - Tags if the revision is minor',
  318. ' len - Adds the length of the revision',
  319. ' content - Adds the content of the revision',
  320. ' token - Gives the edit token',
  321. ),
  322. 'namespace' => 'Only list pages in this namespace (3)',
  323. 'user' => 'Only list revisions by this user',
  324. 'excludeuser' => 'Don\'t list revisions by this user',
  325. 'continue' => 'When more results are available, use this to continue (3)',
  326. 'unique' => 'List only one revision for each page (3)',
  327. );
  328. }
  329. public function getDescription() {
  330. $p = $this->getModulePrefix();
  331. return array(
  332. 'List deleted revisions.',
  333. 'Operates in three modes:',
  334. ' 1) List deleted revisions for the given title(s), sorted by timestamp',
  335. ' 2) List deleted contributions for the given user, sorted by timestamp (no titles specified)',
  336. " 3) List all deleted revisions in the given namespace, sorted by title and timestamp (no titles specified, {$p}user not set)",
  337. 'Certain parameters only apply to some modes and are ignored in others.',
  338. 'For instance, a parameter marked (1) only applies to mode 1 and is ignored in modes 2 and 3',
  339. );
  340. }
  341. public function getPossibleErrors() {
  342. return array_merge( parent::getPossibleErrors(), array(
  343. array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revision information' ),
  344. array( 'code' => 'badparams', 'info' => 'user and excludeuser cannot be used together' ),
  345. array( 'code' => 'permissiondenied', 'info' => 'You don\'t have permission to view deleted revision content' ),
  346. array( 'code' => 'badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
  347. array( 'code' => 'badparams', 'info' => "The 'from' parameter cannot be used in modes 1 or 2" ),
  348. array( 'code' => 'badparams', 'info' => "The 'to' parameter cannot be used in modes 1 or 2" ),
  349. array( 'code' => 'badparams', 'info' => "The 'prefix' parameter cannot be used in modes 1 or 2" ),
  350. array( 'code' => 'badparams', 'info' => "The 'continue' parameter cannot be used in modes 1 or 2" ),
  351. array( 'code' => 'badparams', 'info' => "The 'start' parameter cannot be used in mode 3" ),
  352. array( 'code' => 'badparams', 'info' => "The 'end' parameter cannot be used in mode 3" ),
  353. ) );
  354. }
  355. public function getExamples() {
  356. return array(
  357. 'List the last deleted revisions of Main Page and Talk:Main Page, with content (mode 1):',
  358. ' api.php?action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&drprop=user|comment|content',
  359. 'List the last 50 deleted contributions by Bob (mode 2):',
  360. ' api.php?action=query&list=deletedrevs&druser=Bob&drlimit=50',
  361. 'List the first 50 deleted revisions in the main namespace (mode 3):',
  362. ' api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50',
  363. 'List the first 50 deleted pages in the Talk namespace (mode 3):',
  364. ' api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique=',
  365. );
  366. }
  367. public function getHelpUrls() {
  368. return 'http://www.mediawiki.org/wiki/API:Deletedrevs';
  369. }
  370. public function getVersion() {
  371. return __CLASS__ . ': $Id$';
  372. }
  373. }