ApiQueryDeletedrevs.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. /**
  27. * Query module to enumerate all deleted revisions.
  28. *
  29. * @ingroup API
  30. * @deprecated since 1.25
  31. */
  32. class ApiQueryDeletedrevs extends ApiQueryBase {
  33. public function __construct( ApiQuery $query, $moduleName ) {
  34. parent::__construct( $query, $moduleName, 'dr' );
  35. }
  36. public function execute() {
  37. // Before doing anything at all, let's check permissions
  38. $this->checkUserRightsAny( 'deletedhistory' );
  39. $this->addDeprecation( 'apiwarn-deprecation-deletedrevs', 'action=query&list=deletedrevs' );
  40. $user = $this->getUser();
  41. $db = $this->getDB();
  42. $commentStore = new CommentStore( 'ar_comment' );
  43. $params = $this->extractRequestParams( false );
  44. $prop = array_flip( $params['prop'] );
  45. $fld_parentid = isset( $prop['parentid'] );
  46. $fld_revid = isset( $prop['revid'] );
  47. $fld_user = isset( $prop['user'] );
  48. $fld_userid = isset( $prop['userid'] );
  49. $fld_comment = isset( $prop['comment'] );
  50. $fld_parsedcomment = isset( $prop['parsedcomment'] );
  51. $fld_minor = isset( $prop['minor'] );
  52. $fld_len = isset( $prop['len'] );
  53. $fld_sha1 = isset( $prop['sha1'] );
  54. $fld_content = isset( $prop['content'] );
  55. $fld_token = isset( $prop['token'] );
  56. $fld_tags = isset( $prop['tags'] );
  57. if ( isset( $prop['token'] ) ) {
  58. $p = $this->getModulePrefix();
  59. }
  60. // If we're in a mode that breaks the same-origin policy, no tokens can
  61. // be obtained
  62. if ( $this->lacksSameOriginSecurity() ) {
  63. $fld_token = false;
  64. }
  65. // If user can't undelete, no tokens
  66. if ( !$user->isAllowed( 'undelete' ) ) {
  67. $fld_token = false;
  68. }
  69. $result = $this->getResult();
  70. $pageSet = $this->getPageSet();
  71. $titles = $pageSet->getTitles();
  72. // This module operates in three modes:
  73. // 'revs': List deleted revs for certain titles (1)
  74. // 'user': List deleted revs by a certain user (2)
  75. // 'all': List all deleted revs in NS (3)
  76. $mode = 'all';
  77. if ( count( $titles ) > 0 ) {
  78. $mode = 'revs';
  79. } elseif ( !is_null( $params['user'] ) ) {
  80. $mode = 'user';
  81. }
  82. if ( $mode == 'revs' || $mode == 'user' ) {
  83. // Ignore namespace and unique due to inability to know whether they were purposely set
  84. foreach ( [ 'from', 'to', 'prefix', /*'namespace', 'unique'*/ ] as $p ) {
  85. if ( !is_null( $params[$p] ) ) {
  86. $this->dieWithError( [ 'apierror-deletedrevs-param-not-1-2', $p ], 'badparams' );
  87. }
  88. }
  89. } else {
  90. foreach ( [ 'start', 'end' ] as $p ) {
  91. if ( !is_null( $params[$p] ) ) {
  92. $this->dieWithError( [ 'apierror-deletedrevs-param-not-3', $p ], 'badparams' );
  93. }
  94. }
  95. }
  96. if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
  97. $this->dieWithError( 'user and excludeuser cannot be used together', 'badparams' );
  98. }
  99. $this->addTables( 'archive' );
  100. $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_deleted', 'ar_id' ] );
  101. $this->addFieldsIf( 'ar_parent_id', $fld_parentid );
  102. $this->addFieldsIf( 'ar_rev_id', $fld_revid );
  103. $this->addFieldsIf( 'ar_user_text', $fld_user );
  104. $this->addFieldsIf( 'ar_user', $fld_userid );
  105. $this->addFieldsIf( 'ar_minor_edit', $fld_minor );
  106. $this->addFieldsIf( 'ar_len', $fld_len );
  107. $this->addFieldsIf( 'ar_sha1', $fld_sha1 );
  108. if ( $fld_comment || $fld_parsedcomment ) {
  109. $commentQuery = $commentStore->getJoin();
  110. $this->addTables( $commentQuery['tables'] );
  111. $this->addFields( $commentQuery['fields'] );
  112. $this->addJoinConds( $commentQuery['joins'] );
  113. }
  114. if ( $fld_tags ) {
  115. $this->addTables( 'tag_summary' );
  116. $this->addJoinConds(
  117. [ 'tag_summary' => [ 'LEFT JOIN', [ 'ar_rev_id=ts_rev_id' ] ] ]
  118. );
  119. $this->addFields( 'ts_tags' );
  120. }
  121. if ( !is_null( $params['tag'] ) ) {
  122. $this->addTables( 'change_tag' );
  123. $this->addJoinConds(
  124. [ 'change_tag' => [ 'INNER JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
  125. );
  126. $this->addWhereFld( 'ct_tag', $params['tag'] );
  127. }
  128. if ( $fld_content ) {
  129. // Modern MediaWiki has the content for deleted revs in the 'text'
  130. // table using fields old_text and old_flags. But revisions deleted
  131. // pre-1.5 store the content in the 'archive' table directly using
  132. // fields ar_text and ar_flags, and no corresponding 'text' row. So
  133. // we have to LEFT JOIN and fetch all four fields, plus ar_text_id
  134. // to be able to tell the difference.
  135. $this->addTables( 'text' );
  136. $this->addJoinConds(
  137. [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
  138. );
  139. $this->addFields( [ 'ar_text', 'ar_flags', 'ar_text_id', 'old_text', 'old_flags' ] );
  140. // This also means stricter restrictions
  141. $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
  142. }
  143. // Check limits
  144. $userMax = $fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1;
  145. $botMax = $fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2;
  146. $limit = $params['limit'];
  147. if ( $limit == 'max' ) {
  148. $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
  149. $this->getResult()->addParsedLimit( $this->getModuleName(), $limit );
  150. }
  151. $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
  152. if ( $fld_token ) {
  153. // Undelete tokens are identical for all pages, so we cache one here
  154. $token = $user->getEditToken( '', $this->getMain()->getRequest() );
  155. }
  156. $dir = $params['dir'];
  157. // We need a custom WHERE clause that matches all titles.
  158. if ( $mode == 'revs' ) {
  159. $lb = new LinkBatch( $titles );
  160. $where = $lb->constructSet( 'ar', $db );
  161. $this->addWhere( $where );
  162. } elseif ( $mode == 'all' ) {
  163. $this->addWhereFld( 'ar_namespace', $params['namespace'] );
  164. $from = $params['from'] === null
  165. ? null
  166. : $this->titlePartToKey( $params['from'], $params['namespace'] );
  167. $to = $params['to'] === null
  168. ? null
  169. : $this->titlePartToKey( $params['to'], $params['namespace'] );
  170. $this->addWhereRange( 'ar_title', $dir, $from, $to );
  171. if ( isset( $params['prefix'] ) ) {
  172. $this->addWhere( 'ar_title' . $db->buildLike(
  173. $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
  174. $db->anyString() ) );
  175. }
  176. }
  177. if ( !is_null( $params['user'] ) ) {
  178. $this->addWhereFld( 'ar_user_text', $params['user'] );
  179. } elseif ( !is_null( $params['excludeuser'] ) ) {
  180. $this->addWhere( 'ar_user_text != ' .
  181. $db->addQuotes( $params['excludeuser'] ) );
  182. }
  183. if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
  184. // Paranoia: avoid brute force searches (T19342)
  185. // (shouldn't be able to get here without 'deletedhistory', but
  186. // check it again just in case)
  187. if ( !$user->isAllowed( 'deletedhistory' ) ) {
  188. $bitmask = Revision::DELETED_USER;
  189. } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
  190. $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
  191. } else {
  192. $bitmask = 0;
  193. }
  194. if ( $bitmask ) {
  195. $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
  196. }
  197. }
  198. if ( !is_null( $params['continue'] ) ) {
  199. $cont = explode( '|', $params['continue'] );
  200. $op = ( $dir == 'newer' ? '>' : '<' );
  201. if ( $mode == 'all' || $mode == 'revs' ) {
  202. $this->dieContinueUsageIf( count( $cont ) != 4 );
  203. $ns = intval( $cont[0] );
  204. $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
  205. $title = $db->addQuotes( $cont[1] );
  206. $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
  207. $ar_id = (int)$cont[3];
  208. $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
  209. $this->addWhere( "ar_namespace $op $ns OR " .
  210. "(ar_namespace = $ns AND " .
  211. "(ar_title $op $title OR " .
  212. "(ar_title = $title AND " .
  213. "(ar_timestamp $op $ts OR " .
  214. "(ar_timestamp = $ts AND " .
  215. "ar_id $op= $ar_id)))))" );
  216. } else {
  217. $this->dieContinueUsageIf( count( $cont ) != 2 );
  218. $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
  219. $ar_id = (int)$cont[1];
  220. $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
  221. $this->addWhere( "ar_timestamp $op $ts OR " .
  222. "(ar_timestamp = $ts AND " .
  223. "ar_id $op= $ar_id)" );
  224. }
  225. }
  226. $this->addOption( 'LIMIT', $limit + 1 );
  227. $this->addOption(
  228. 'USE INDEX',
  229. [ 'archive' => ( $mode == 'user' ? 'ar_usertext_timestamp' : 'name_title_timestamp' ) ]
  230. );
  231. if ( $mode == 'all' ) {
  232. if ( $params['unique'] ) {
  233. // @todo Does this work on non-MySQL?
  234. $this->addOption( 'GROUP BY', 'ar_title' );
  235. } else {
  236. $sort = ( $dir == 'newer' ? '' : ' DESC' );
  237. $this->addOption( 'ORDER BY', [
  238. 'ar_title' . $sort,
  239. 'ar_timestamp' . $sort,
  240. 'ar_id' . $sort,
  241. ] );
  242. }
  243. } else {
  244. if ( $mode == 'revs' ) {
  245. // Sort by ns and title in the same order as timestamp for efficiency
  246. $this->addWhereRange( 'ar_namespace', $dir, null, null );
  247. $this->addWhereRange( 'ar_title', $dir, null, null );
  248. }
  249. $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
  250. // Include in ORDER BY for uniqueness
  251. $this->addWhereRange( 'ar_id', $dir, null, null );
  252. }
  253. $res = $this->select( __METHOD__ );
  254. $pageMap = []; // Maps ns&title to (fake) pageid
  255. $count = 0;
  256. $newPageID = 0;
  257. foreach ( $res as $row ) {
  258. if ( ++$count > $limit ) {
  259. // We've had enough
  260. if ( $mode == 'all' || $mode == 'revs' ) {
  261. $this->setContinueEnumParameter( 'continue',
  262. "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
  263. );
  264. } else {
  265. $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
  266. }
  267. break;
  268. }
  269. $rev = [];
  270. $anyHidden = false;
  271. $rev['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ar_timestamp );
  272. if ( $fld_revid ) {
  273. $rev['revid'] = intval( $row->ar_rev_id );
  274. }
  275. if ( $fld_parentid && !is_null( $row->ar_parent_id ) ) {
  276. $rev['parentid'] = intval( $row->ar_parent_id );
  277. }
  278. if ( $fld_user || $fld_userid ) {
  279. if ( $row->ar_deleted & Revision::DELETED_USER ) {
  280. $rev['userhidden'] = true;
  281. $anyHidden = true;
  282. }
  283. if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_USER, $user ) ) {
  284. if ( $fld_user ) {
  285. $rev['user'] = $row->ar_user_text;
  286. }
  287. if ( $fld_userid ) {
  288. $rev['userid'] = (int)$row->ar_user;
  289. }
  290. }
  291. }
  292. if ( $fld_comment || $fld_parsedcomment ) {
  293. if ( $row->ar_deleted & Revision::DELETED_COMMENT ) {
  294. $rev['commenthidden'] = true;
  295. $anyHidden = true;
  296. }
  297. if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_COMMENT, $user ) ) {
  298. $comment = $commentStore->getComment( $row )->text;
  299. if ( $fld_comment ) {
  300. $rev['comment'] = $comment;
  301. }
  302. if ( $fld_parsedcomment ) {
  303. $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
  304. $rev['parsedcomment'] = Linker::formatComment( $comment, $title );
  305. }
  306. }
  307. }
  308. if ( $fld_minor ) {
  309. $rev['minor'] = $row->ar_minor_edit == 1;
  310. }
  311. if ( $fld_len ) {
  312. $rev['len'] = $row->ar_len;
  313. }
  314. if ( $fld_sha1 ) {
  315. if ( $row->ar_deleted & Revision::DELETED_TEXT ) {
  316. $rev['sha1hidden'] = true;
  317. $anyHidden = true;
  318. }
  319. if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_TEXT, $user ) ) {
  320. if ( $row->ar_sha1 != '' ) {
  321. $rev['sha1'] = Wikimedia\base_convert( $row->ar_sha1, 36, 16, 40 );
  322. } else {
  323. $rev['sha1'] = '';
  324. }
  325. }
  326. }
  327. if ( $fld_content ) {
  328. if ( $row->ar_deleted & Revision::DELETED_TEXT ) {
  329. $rev['texthidden'] = true;
  330. $anyHidden = true;
  331. }
  332. if ( Revision::userCanBitfield( $row->ar_deleted, Revision::DELETED_TEXT, $user ) ) {
  333. if ( isset( $row->ar_text ) && !$row->ar_text_id ) {
  334. // Pre-1.5 ar_text row (if condition from Revision::newFromArchiveRow)
  335. ApiResult::setContentValue( $rev, 'text', Revision::getRevisionText( $row, 'ar_' ) );
  336. } else {
  337. ApiResult::setContentValue( $rev, 'text', Revision::getRevisionText( $row ) );
  338. }
  339. }
  340. }
  341. if ( $fld_tags ) {
  342. if ( $row->ts_tags ) {
  343. $tags = explode( ',', $row->ts_tags );
  344. ApiResult::setIndexedTagName( $tags, 'tag' );
  345. $rev['tags'] = $tags;
  346. } else {
  347. $rev['tags'] = [];
  348. }
  349. }
  350. if ( $anyHidden && ( $row->ar_deleted & Revision::DELETED_RESTRICTED ) ) {
  351. $rev['suppressed'] = true;
  352. }
  353. if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
  354. $pageID = $newPageID++;
  355. $pageMap[$row->ar_namespace][$row->ar_title] = $pageID;
  356. $a['revisions'] = [ $rev ];
  357. ApiResult::setIndexedTagName( $a['revisions'], 'rev' );
  358. $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
  359. ApiQueryBase::addTitleInfo( $a, $title );
  360. if ( $fld_token ) {
  361. $a['token'] = $token;
  362. }
  363. $fit = $result->addValue( [ 'query', $this->getModuleName() ], $pageID, $a );
  364. } else {
  365. $pageID = $pageMap[$row->ar_namespace][$row->ar_title];
  366. $fit = $result->addValue(
  367. [ 'query', $this->getModuleName(), $pageID, 'revisions' ],
  368. null, $rev );
  369. }
  370. if ( !$fit ) {
  371. if ( $mode == 'all' || $mode == 'revs' ) {
  372. $this->setContinueEnumParameter( 'continue',
  373. "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
  374. );
  375. } else {
  376. $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
  377. }
  378. break;
  379. }
  380. }
  381. $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
  382. }
  383. public function isDeprecated() {
  384. return true;
  385. }
  386. public function getAllowedParams() {
  387. return [
  388. 'start' => [
  389. ApiBase::PARAM_TYPE => 'timestamp',
  390. ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 1, 2 ] ],
  391. ],
  392. 'end' => [
  393. ApiBase::PARAM_TYPE => 'timestamp',
  394. ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 1, 2 ] ],
  395. ],
  396. 'dir' => [
  397. ApiBase::PARAM_TYPE => [
  398. 'newer',
  399. 'older'
  400. ],
  401. ApiBase::PARAM_DFLT => 'older',
  402. ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
  403. ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 1, 3 ] ],
  404. ],
  405. 'from' => [
  406. ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
  407. ],
  408. 'to' => [
  409. ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
  410. ],
  411. 'prefix' => [
  412. ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
  413. ],
  414. 'unique' => [
  415. ApiBase::PARAM_DFLT => false,
  416. ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
  417. ],
  418. 'namespace' => [
  419. ApiBase::PARAM_TYPE => 'namespace',
  420. ApiBase::PARAM_DFLT => NS_MAIN,
  421. ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
  422. ],
  423. 'tag' => null,
  424. 'user' => [
  425. ApiBase::PARAM_TYPE => 'user'
  426. ],
  427. 'excludeuser' => [
  428. ApiBase::PARAM_TYPE => 'user'
  429. ],
  430. 'prop' => [
  431. ApiBase::PARAM_DFLT => 'user|comment',
  432. ApiBase::PARAM_TYPE => [
  433. 'revid',
  434. 'parentid',
  435. 'user',
  436. 'userid',
  437. 'comment',
  438. 'parsedcomment',
  439. 'minor',
  440. 'len',
  441. 'sha1',
  442. 'content',
  443. 'token',
  444. 'tags'
  445. ],
  446. ApiBase::PARAM_ISMULTI => true
  447. ],
  448. 'limit' => [
  449. ApiBase::PARAM_DFLT => 10,
  450. ApiBase::PARAM_TYPE => 'limit',
  451. ApiBase::PARAM_MIN => 1,
  452. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  453. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  454. ],
  455. 'continue' => [
  456. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  457. ],
  458. ];
  459. }
  460. protected function getExamplesMessages() {
  461. return [
  462. 'action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&' .
  463. 'drprop=user|comment|content'
  464. => 'apihelp-query+deletedrevs-example-mode1',
  465. 'action=query&list=deletedrevs&druser=Bob&drlimit=50'
  466. => 'apihelp-query+deletedrevs-example-mode2',
  467. 'action=query&list=deletedrevs&drdir=newer&drlimit=50'
  468. => 'apihelp-query+deletedrevs-example-mode3-main',
  469. 'action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique='
  470. => 'apihelp-query+deletedrevs-example-mode3-talk',
  471. ];
  472. }
  473. public function getHelpUrls() {
  474. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevs';
  475. }
  476. }