ApiQueryDeletedrevs.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <?php
  2. /**
  3. * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. use MediaWiki\MediaWikiServices;
  23. use MediaWiki\Storage\NameTableAccessException;
  24. use MediaWiki\Revision\RevisionRecord;
  25. use MediaWiki\Revision\SlotRecord;
  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 = CommentStore::getStore();
  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 we're in a mode that breaks the same-origin policy, no tokens can
  58. // be obtained
  59. if ( $this->lacksSameOriginSecurity() ) {
  60. $fld_token = false;
  61. }
  62. // If user can't undelete, no tokens
  63. if ( !$this->getPermissionManager()->userHasRight( $user, 'undelete' ) ) {
  64. $fld_token = false;
  65. }
  66. $result = $this->getResult();
  67. $pageSet = $this->getPageSet();
  68. $titles = $pageSet->getTitles();
  69. // This module operates in three modes:
  70. // 'revs': List deleted revs for certain titles (1)
  71. // 'user': List deleted revs by a certain user (2)
  72. // 'all': List all deleted revs in NS (3)
  73. $mode = 'all';
  74. if ( count( $titles ) > 0 ) {
  75. $mode = 'revs';
  76. } elseif ( !is_null( $params['user'] ) ) {
  77. $mode = 'user';
  78. }
  79. if ( $mode == 'revs' || $mode == 'user' ) {
  80. // Ignore namespace and unique due to inability to know whether they were purposely set
  81. foreach ( [ 'from', 'to', 'prefix', /*'namespace', 'unique'*/ ] as $p ) {
  82. if ( !is_null( $params[$p] ) ) {
  83. $this->dieWithError( [ 'apierror-deletedrevs-param-not-1-2', $p ], 'badparams' );
  84. }
  85. }
  86. } else {
  87. foreach ( [ 'start', 'end' ] as $p ) {
  88. if ( !is_null( $params[$p] ) ) {
  89. $this->dieWithError( [ 'apierror-deletedrevs-param-not-3', $p ], 'badparams' );
  90. }
  91. }
  92. }
  93. if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
  94. $this->dieWithError( 'user and excludeuser cannot be used together', 'badparams' );
  95. }
  96. $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
  97. $arQuery = $revisionStore->getArchiveQueryInfo();
  98. $this->addTables( $arQuery['tables'] );
  99. $this->addFields( $arQuery['fields'] );
  100. $this->addJoinConds( $arQuery['joins'] );
  101. $this->addFields( [ 'ar_title', 'ar_namespace' ] );
  102. if ( $fld_tags ) {
  103. $this->addFields( [ 'ts_tags' => ChangeTags::makeTagSummarySubquery( 'archive' ) ] );
  104. }
  105. if ( !is_null( $params['tag'] ) ) {
  106. $this->addTables( 'change_tag' );
  107. $this->addJoinConds(
  108. [ 'change_tag' => [ 'JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
  109. );
  110. $changeTagDefStore = MediaWikiServices::getInstance()->getChangeTagDefStore();
  111. try {
  112. $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
  113. } catch ( NameTableAccessException $exception ) {
  114. // Return nothing.
  115. $this->addWhere( '1=0' );
  116. }
  117. }
  118. // This means stricter restrictions
  119. if ( $fld_content ) {
  120. $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
  121. }
  122. // Check limits
  123. $userMax = $fld_content ? ApiBase::LIMIT_SML1 : ApiBase::LIMIT_BIG1;
  124. $botMax = $fld_content ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_BIG2;
  125. $limit = $params['limit'];
  126. if ( $limit == 'max' ) {
  127. $limit = $this->getMain()->canApiHighLimits() ? $botMax : $userMax;
  128. $this->getResult()->addParsedLimit( $this->getModuleName(), $limit );
  129. }
  130. $this->validateLimit( 'limit', $limit, 1, $userMax, $botMax );
  131. if ( $fld_token ) {
  132. // Undelete tokens are identical for all pages, so we cache one here
  133. $token = $user->getEditToken( '', $this->getMain()->getRequest() );
  134. }
  135. $dir = $params['dir'];
  136. // We need a custom WHERE clause that matches all titles.
  137. if ( $mode == 'revs' ) {
  138. $lb = new LinkBatch( $titles );
  139. $where = $lb->constructSet( 'ar', $db );
  140. $this->addWhere( $where );
  141. } elseif ( $mode == 'all' ) {
  142. $this->addWhereFld( 'ar_namespace', $params['namespace'] );
  143. $from = $params['from'] === null
  144. ? null
  145. : $this->titlePartToKey( $params['from'], $params['namespace'] );
  146. $to = $params['to'] === null
  147. ? null
  148. : $this->titlePartToKey( $params['to'], $params['namespace'] );
  149. $this->addWhereRange( 'ar_title', $dir, $from, $to );
  150. if ( isset( $params['prefix'] ) ) {
  151. $this->addWhere( 'ar_title' . $db->buildLike(
  152. $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
  153. $db->anyString() ) );
  154. }
  155. }
  156. if ( !is_null( $params['user'] ) ) {
  157. // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
  158. $actorQuery = ActorMigration::newMigration()
  159. ->getWhere( $db, 'ar_user', User::newFromName( $params['user'], false ), false );
  160. $this->addTables( $actorQuery['tables'] );
  161. $this->addJoinConds( $actorQuery['joins'] );
  162. $this->addWhere( $actorQuery['conds'] );
  163. } elseif ( !is_null( $params['excludeuser'] ) ) {
  164. // Here there's no chance of using ar_usertext_timestamp.
  165. $actorQuery = ActorMigration::newMigration()
  166. ->getWhere( $db, 'ar_user', User::newFromName( $params['excludeuser'], false ) );
  167. $this->addTables( $actorQuery['tables'] );
  168. $this->addJoinConds( $actorQuery['joins'] );
  169. $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
  170. }
  171. if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
  172. // Paranoia: avoid brute force searches (T19342)
  173. // (shouldn't be able to get here without 'deletedhistory', but
  174. // check it again just in case)
  175. if ( !$this->getPermissionManager()->userHasRight( $user, 'deletedhistory' ) ) {
  176. $bitmask = RevisionRecord::DELETED_USER;
  177. } elseif ( !$this->getPermissionManager()
  178. ->userHasAnyRight( $user, 'suppressrevision', 'viewsuppressed' )
  179. ) {
  180. $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
  181. } else {
  182. $bitmask = 0;
  183. }
  184. if ( $bitmask ) {
  185. $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
  186. }
  187. }
  188. if ( !is_null( $params['continue'] ) ) {
  189. $cont = explode( '|', $params['continue'] );
  190. $op = ( $dir == 'newer' ? '>' : '<' );
  191. if ( $mode == 'all' || $mode == 'revs' ) {
  192. $this->dieContinueUsageIf( count( $cont ) != 4 );
  193. $ns = (int)$cont[0];
  194. $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
  195. $title = $db->addQuotes( $cont[1] );
  196. $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
  197. $ar_id = (int)$cont[3];
  198. $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
  199. $this->addWhere( "ar_namespace $op $ns OR " .
  200. "(ar_namespace = $ns AND " .
  201. "(ar_title $op $title OR " .
  202. "(ar_title = $title AND " .
  203. "(ar_timestamp $op $ts OR " .
  204. "(ar_timestamp = $ts AND " .
  205. "ar_id $op= $ar_id)))))" );
  206. } else {
  207. $this->dieContinueUsageIf( count( $cont ) != 2 );
  208. $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
  209. $ar_id = (int)$cont[1];
  210. $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
  211. $this->addWhere( "ar_timestamp $op $ts OR " .
  212. "(ar_timestamp = $ts AND " .
  213. "ar_id $op= $ar_id)" );
  214. }
  215. }
  216. $this->addOption( 'LIMIT', $limit + 1 );
  217. if ( $mode == 'all' ) {
  218. if ( $params['unique'] ) {
  219. // @todo Does this work on non-MySQL?
  220. $this->addOption( 'GROUP BY', 'ar_title' );
  221. } else {
  222. $sort = ( $dir == 'newer' ? '' : ' DESC' );
  223. $this->addOption( 'ORDER BY', [
  224. 'ar_title' . $sort,
  225. 'ar_timestamp' . $sort,
  226. 'ar_id' . $sort,
  227. ] );
  228. }
  229. } else {
  230. if ( $mode == 'revs' ) {
  231. // Sort by ns and title in the same order as timestamp for efficiency
  232. $this->addWhereRange( 'ar_namespace', $dir, null, null );
  233. $this->addWhereRange( 'ar_title', $dir, null, null );
  234. }
  235. $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
  236. // Include in ORDER BY for uniqueness
  237. $this->addWhereRange( 'ar_id', $dir, null, null );
  238. }
  239. $res = $this->select( __METHOD__ );
  240. $pageMap = []; // Maps ns&title to (fake) pageid
  241. $count = 0;
  242. $newPageID = 0;
  243. foreach ( $res as $row ) {
  244. if ( ++$count > $limit ) {
  245. // We've had enough
  246. if ( $mode == 'all' || $mode == 'revs' ) {
  247. $this->setContinueEnumParameter( 'continue',
  248. "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
  249. );
  250. } else {
  251. $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
  252. }
  253. break;
  254. }
  255. $rev = [];
  256. $anyHidden = false;
  257. $rev['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ar_timestamp );
  258. if ( $fld_revid ) {
  259. $rev['revid'] = (int)$row->ar_rev_id;
  260. }
  261. if ( $fld_parentid && !is_null( $row->ar_parent_id ) ) {
  262. $rev['parentid'] = (int)$row->ar_parent_id;
  263. }
  264. if ( $fld_user || $fld_userid ) {
  265. if ( $row->ar_deleted & RevisionRecord::DELETED_USER ) {
  266. $rev['userhidden'] = true;
  267. $anyHidden = true;
  268. }
  269. if ( Revision::userCanBitfield( $row->ar_deleted, RevisionRecord::DELETED_USER, $user ) ) {
  270. if ( $fld_user ) {
  271. $rev['user'] = $row->ar_user_text;
  272. }
  273. if ( $fld_userid ) {
  274. $rev['userid'] = (int)$row->ar_user;
  275. }
  276. }
  277. }
  278. if ( $fld_comment || $fld_parsedcomment ) {
  279. if ( $row->ar_deleted & RevisionRecord::DELETED_COMMENT ) {
  280. $rev['commenthidden'] = true;
  281. $anyHidden = true;
  282. }
  283. if ( Revision::userCanBitfield( $row->ar_deleted, RevisionRecord::DELETED_COMMENT, $user ) ) {
  284. $comment = $commentStore->getComment( 'ar_comment', $row )->text;
  285. if ( $fld_comment ) {
  286. $rev['comment'] = $comment;
  287. }
  288. if ( $fld_parsedcomment ) {
  289. $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
  290. $rev['parsedcomment'] = Linker::formatComment( $comment, $title );
  291. }
  292. }
  293. }
  294. if ( $fld_minor ) {
  295. $rev['minor'] = $row->ar_minor_edit == 1;
  296. }
  297. if ( $fld_len ) {
  298. $rev['len'] = $row->ar_len;
  299. }
  300. if ( $fld_sha1 ) {
  301. if ( $row->ar_deleted & RevisionRecord::DELETED_TEXT ) {
  302. $rev['sha1hidden'] = true;
  303. $anyHidden = true;
  304. }
  305. if ( Revision::userCanBitfield( $row->ar_deleted, RevisionRecord::DELETED_TEXT, $user ) ) {
  306. if ( $row->ar_sha1 != '' ) {
  307. $rev['sha1'] = Wikimedia\base_convert( $row->ar_sha1, 36, 16, 40 );
  308. } else {
  309. $rev['sha1'] = '';
  310. }
  311. }
  312. }
  313. if ( $fld_content ) {
  314. if ( $row->ar_deleted & RevisionRecord::DELETED_TEXT ) {
  315. $rev['texthidden'] = true;
  316. $anyHidden = true;
  317. }
  318. if ( Revision::userCanBitfield( $row->ar_deleted, RevisionRecord::DELETED_TEXT, $user ) ) {
  319. ApiResult::setContentValue( $rev, 'text',
  320. $revisionStore->newRevisionFromArchiveRow( $row )
  321. ->getContent( SlotRecord::MAIN )->serialize() );
  322. }
  323. }
  324. if ( $fld_tags ) {
  325. if ( $row->ts_tags ) {
  326. $tags = explode( ',', $row->ts_tags );
  327. ApiResult::setIndexedTagName( $tags, 'tag' );
  328. $rev['tags'] = $tags;
  329. } else {
  330. $rev['tags'] = [];
  331. }
  332. }
  333. if ( $anyHidden && ( $row->ar_deleted & RevisionRecord::DELETED_RESTRICTED ) ) {
  334. $rev['suppressed'] = true;
  335. }
  336. if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
  337. $pageID = $newPageID++;
  338. $pageMap[$row->ar_namespace][$row->ar_title] = $pageID;
  339. $a = [ 'revisions' => [ $rev ] ];
  340. ApiResult::setIndexedTagName( $a['revisions'], 'rev' );
  341. $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
  342. ApiQueryBase::addTitleInfo( $a, $title );
  343. if ( $fld_token ) {
  344. $a['token'] = $token;
  345. }
  346. $fit = $result->addValue( [ 'query', $this->getModuleName() ], $pageID, $a );
  347. } else {
  348. $pageID = $pageMap[$row->ar_namespace][$row->ar_title];
  349. $fit = $result->addValue(
  350. [ 'query', $this->getModuleName(), $pageID, 'revisions' ],
  351. null, $rev );
  352. }
  353. if ( !$fit ) {
  354. if ( $mode == 'all' || $mode == 'revs' ) {
  355. $this->setContinueEnumParameter( 'continue',
  356. "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
  357. );
  358. } else {
  359. $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
  360. }
  361. break;
  362. }
  363. }
  364. $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
  365. }
  366. public function isDeprecated() {
  367. return true;
  368. }
  369. public function getAllowedParams() {
  370. return [
  371. 'start' => [
  372. ApiBase::PARAM_TYPE => 'timestamp',
  373. ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 1, 2 ] ],
  374. ],
  375. 'end' => [
  376. ApiBase::PARAM_TYPE => 'timestamp',
  377. ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 1, 2 ] ],
  378. ],
  379. 'dir' => [
  380. ApiBase::PARAM_TYPE => [
  381. 'newer',
  382. 'older'
  383. ],
  384. ApiBase::PARAM_DFLT => 'older',
  385. ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
  386. ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 1, 3 ] ],
  387. ],
  388. 'from' => [
  389. ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
  390. ],
  391. 'to' => [
  392. ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
  393. ],
  394. 'prefix' => [
  395. ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
  396. ],
  397. 'unique' => [
  398. ApiBase::PARAM_DFLT => false,
  399. ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
  400. ],
  401. 'namespace' => [
  402. ApiBase::PARAM_TYPE => 'namespace',
  403. ApiBase::PARAM_DFLT => NS_MAIN,
  404. ApiBase::PARAM_HELP_MSG_INFO => [ [ 'modes', 3 ] ],
  405. ],
  406. 'tag' => null,
  407. 'user' => [
  408. ApiBase::PARAM_TYPE => 'user'
  409. ],
  410. 'excludeuser' => [
  411. ApiBase::PARAM_TYPE => 'user'
  412. ],
  413. 'prop' => [
  414. ApiBase::PARAM_DFLT => 'user|comment',
  415. ApiBase::PARAM_TYPE => [
  416. 'revid',
  417. 'parentid',
  418. 'user',
  419. 'userid',
  420. 'comment',
  421. 'parsedcomment',
  422. 'minor',
  423. 'len',
  424. 'sha1',
  425. 'content',
  426. 'token',
  427. 'tags'
  428. ],
  429. ApiBase::PARAM_ISMULTI => true
  430. ],
  431. 'limit' => [
  432. ApiBase::PARAM_DFLT => 10,
  433. ApiBase::PARAM_TYPE => 'limit',
  434. ApiBase::PARAM_MIN => 1,
  435. ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
  436. ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
  437. ],
  438. 'continue' => [
  439. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  440. ],
  441. ];
  442. }
  443. protected function getExamplesMessages() {
  444. return [
  445. 'action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&' .
  446. 'drprop=user|comment|content'
  447. => 'apihelp-query+deletedrevs-example-mode1',
  448. 'action=query&list=deletedrevs&druser=Bob&drlimit=50'
  449. => 'apihelp-query+deletedrevs-example-mode2',
  450. 'action=query&list=deletedrevs&drdir=newer&drlimit=50'
  451. => 'apihelp-query+deletedrevs-example-mode3-main',
  452. 'action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique='
  453. => 'apihelp-query+deletedrevs-example-mode3-talk',
  454. ];
  455. }
  456. public function getHelpUrls() {
  457. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevs';
  458. }
  459. }