ApiQueryDeletedRevisions.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. /**
  3. * Copyright © 2014 Wikimedia Foundation and contributors
  4. *
  5. * Heavily based on ApiQueryDeletedrevs,
  6. * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  21. * http://www.gnu.org/copyleft/gpl.html
  22. *
  23. * @file
  24. */
  25. use MediaWiki\MediaWikiServices;
  26. use MediaWiki\Revision\RevisionRecord;
  27. use MediaWiki\Storage\NameTableAccessException;
  28. /**
  29. * Query module to enumerate deleted revisions for pages.
  30. *
  31. * @ingroup API
  32. */
  33. class ApiQueryDeletedRevisions extends ApiQueryRevisionsBase {
  34. public function __construct( ApiQuery $query, $moduleName ) {
  35. parent::__construct( $query, $moduleName, 'drv' );
  36. }
  37. protected function run( ApiPageSet $resultPageSet = null ) {
  38. $user = $this->getUser();
  39. $pageSet = $this->getPageSet();
  40. $pageMap = $pageSet->getGoodAndMissingTitlesByNamespace();
  41. $pageCount = count( $pageSet->getGoodAndMissingTitles() );
  42. $revCount = $pageSet->getRevisionCount();
  43. if ( $revCount === 0 && $pageCount === 0 ) {
  44. // Nothing to do
  45. return;
  46. }
  47. if ( $revCount !== 0 && count( $pageSet->getDeletedRevisionIDs() ) === 0 ) {
  48. // Nothing to do, revisions were supplied but none are deleted
  49. return;
  50. }
  51. $params = $this->extractRequestParams( false );
  52. $db = $this->getDB();
  53. $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
  54. $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
  55. if ( $resultPageSet === null ) {
  56. $this->parseParameters( $params );
  57. $arQuery = $revisionStore->getArchiveQueryInfo();
  58. $this->addTables( $arQuery['tables'] );
  59. $this->addFields( $arQuery['fields'] );
  60. $this->addJoinConds( $arQuery['joins'] );
  61. $this->addFields( [ 'ar_title', 'ar_namespace' ] );
  62. } else {
  63. $this->limit = $this->getParameter( 'limit' ) ?: 10;
  64. $this->addTables( 'archive' );
  65. $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
  66. }
  67. if ( $this->fld_tags ) {
  68. $this->addFields( [ 'ts_tags' => ChangeTags::makeTagSummarySubquery( 'archive' ) ] );
  69. }
  70. if ( !is_null( $params['tag'] ) ) {
  71. $this->addTables( 'change_tag' );
  72. $this->addJoinConds(
  73. [ 'change_tag' => [ 'JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
  74. );
  75. $changeTagDefStore = MediaWikiServices::getInstance()->getChangeTagDefStore();
  76. try {
  77. $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
  78. } catch ( NameTableAccessException $exception ) {
  79. // Return nothing.
  80. $this->addWhere( '1=0' );
  81. }
  82. }
  83. // This means stricter restrictions
  84. if ( ( $this->fld_comment || $this->fld_parsedcomment ) &&
  85. !$this->getPermissionManager()->userHasRight( $user, 'deletedhistory' )
  86. ) {
  87. $this->dieWithError( 'apierror-cantview-deleted-comment', 'permissiondenied' );
  88. }
  89. if ( $this->fetchContent &&
  90. !$this->getPermissionManager()->userHasAnyRight( $user, 'deletedtext', 'undelete' )
  91. ) {
  92. $this->dieWithError( 'apierror-cantview-deleted-revision-content', 'permissiondenied' );
  93. }
  94. $dir = $params['dir'];
  95. if ( $revCount !== 0 ) {
  96. $this->addWhere( [
  97. 'ar_rev_id' => array_keys( $pageSet->getDeletedRevisionIDs() )
  98. ] );
  99. } else {
  100. // We need a custom WHERE clause that matches all titles.
  101. $lb = new LinkBatch( $pageSet->getGoodAndMissingTitles() );
  102. $where = $lb->constructSet( 'ar', $db );
  103. $this->addWhere( $where );
  104. }
  105. if ( !is_null( $params['user'] ) ) {
  106. // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
  107. $actorQuery = ActorMigration::newMigration()
  108. ->getWhere( $db, 'ar_user', User::newFromName( $params['user'], false ), false );
  109. $this->addTables( $actorQuery['tables'] );
  110. $this->addJoinConds( $actorQuery['joins'] );
  111. $this->addWhere( $actorQuery['conds'] );
  112. } elseif ( !is_null( $params['excludeuser'] ) ) {
  113. // Here there's no chance of using ar_usertext_timestamp.
  114. $actorQuery = ActorMigration::newMigration()
  115. ->getWhere( $db, 'ar_user', User::newFromName( $params['excludeuser'], false ) );
  116. $this->addTables( $actorQuery['tables'] );
  117. $this->addJoinConds( $actorQuery['joins'] );
  118. $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
  119. }
  120. if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
  121. // Paranoia: avoid brute force searches (T19342)
  122. if ( !$this->getPermissionManager()->userHasRight( $user, 'deletedhistory' ) ) {
  123. $bitmask = RevisionRecord::DELETED_USER;
  124. } elseif ( !$this->getPermissionManager()
  125. ->userHasAnyRight( $this->getUser(), 'suppressrevision', 'viewsuppressed' )
  126. ) {
  127. $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
  128. } else {
  129. $bitmask = 0;
  130. }
  131. if ( $bitmask ) {
  132. $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
  133. }
  134. }
  135. if ( !is_null( $params['continue'] ) ) {
  136. $cont = explode( '|', $params['continue'] );
  137. $op = ( $dir == 'newer' ? '>' : '<' );
  138. if ( $revCount !== 0 ) {
  139. $this->dieContinueUsageIf( count( $cont ) != 2 );
  140. $rev = (int)$cont[0];
  141. $this->dieContinueUsageIf( strval( $rev ) !== $cont[0] );
  142. $ar_id = (int)$cont[1];
  143. $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
  144. $this->addWhere( "ar_rev_id $op $rev OR " .
  145. "(ar_rev_id = $rev AND " .
  146. "ar_id $op= $ar_id)" );
  147. } else {
  148. $this->dieContinueUsageIf( count( $cont ) != 4 );
  149. $ns = (int)$cont[0];
  150. $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
  151. $title = $db->addQuotes( $cont[1] );
  152. $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
  153. $ar_id = (int)$cont[3];
  154. $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
  155. $this->addWhere( "ar_namespace $op $ns OR " .
  156. "(ar_namespace = $ns AND " .
  157. "(ar_title $op $title OR " .
  158. "(ar_title = $title AND " .
  159. "(ar_timestamp $op $ts OR " .
  160. "(ar_timestamp = $ts AND " .
  161. "ar_id $op= $ar_id)))))" );
  162. }
  163. }
  164. $this->addOption( 'LIMIT', $this->limit + 1 );
  165. if ( $revCount !== 0 ) {
  166. // Sort by ar_rev_id when querying by ar_rev_id
  167. $this->addWhereRange( 'ar_rev_id', $dir, null, null );
  168. } else {
  169. // Sort by ns and title in the same order as timestamp for efficiency
  170. // But only when not already unique in the query
  171. if ( count( $pageMap ) > 1 ) {
  172. $this->addWhereRange( 'ar_namespace', $dir, null, null );
  173. }
  174. $oneTitle = key( reset( $pageMap ) );
  175. foreach ( $pageMap as $pages ) {
  176. if ( count( $pages ) > 1 || key( $pages ) !== $oneTitle ) {
  177. $this->addWhereRange( 'ar_title', $dir, null, null );
  178. break;
  179. }
  180. }
  181. $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
  182. }
  183. // Include in ORDER BY for uniqueness
  184. $this->addWhereRange( 'ar_id', $dir, null, null );
  185. $res = $this->select( __METHOD__ );
  186. $count = 0;
  187. $generated = [];
  188. foreach ( $res as $row ) {
  189. if ( ++$count > $this->limit ) {
  190. // We've had enough
  191. $this->setContinueEnumParameter( 'continue',
  192. $revCount
  193. ? "$row->ar_rev_id|$row->ar_id"
  194. : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
  195. );
  196. break;
  197. }
  198. if ( $resultPageSet !== null ) {
  199. $generated[] = $row->ar_rev_id;
  200. } else {
  201. if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
  202. // Was it converted?
  203. $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
  204. $converted = $pageSet->getConvertedTitles();
  205. if ( $title && isset( $converted[$title->getPrefixedText()] ) ) {
  206. $title = Title::newFromText( $converted[$title->getPrefixedText()] );
  207. if ( $title && isset( $pageMap[$title->getNamespace()][$title->getDBkey()] ) ) {
  208. $pageMap[$row->ar_namespace][$row->ar_title] =
  209. $pageMap[$title->getNamespace()][$title->getDBkey()];
  210. }
  211. }
  212. }
  213. if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
  214. ApiBase::dieDebug(
  215. __METHOD__,
  216. "Found row in archive (ar_id={$row->ar_id}) that didn't get processed by ApiPageSet"
  217. );
  218. }
  219. $fit = $this->addPageSubItem(
  220. $pageMap[$row->ar_namespace][$row->ar_title],
  221. $this->extractRevisionInfo( $revisionStore->newRevisionFromArchiveRow( $row ), $row ),
  222. 'rev'
  223. );
  224. if ( !$fit ) {
  225. $this->setContinueEnumParameter( 'continue',
  226. $revCount
  227. ? "$row->ar_rev_id|$row->ar_id"
  228. : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
  229. );
  230. break;
  231. }
  232. }
  233. }
  234. if ( $resultPageSet !== null ) {
  235. $resultPageSet->populateFromRevisionIDs( $generated );
  236. }
  237. }
  238. public function getAllowedParams() {
  239. return parent::getAllowedParams() + [
  240. 'start' => [
  241. ApiBase::PARAM_TYPE => 'timestamp',
  242. ],
  243. 'end' => [
  244. ApiBase::PARAM_TYPE => 'timestamp',
  245. ],
  246. 'dir' => [
  247. ApiBase::PARAM_TYPE => [
  248. 'newer',
  249. 'older'
  250. ],
  251. ApiBase::PARAM_DFLT => 'older',
  252. ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
  253. ],
  254. 'tag' => null,
  255. 'user' => [
  256. ApiBase::PARAM_TYPE => 'user'
  257. ],
  258. 'excludeuser' => [
  259. ApiBase::PARAM_TYPE => 'user'
  260. ],
  261. 'continue' => [
  262. ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
  263. ],
  264. ];
  265. }
  266. protected function getExamplesMessages() {
  267. return [
  268. 'action=query&prop=deletedrevisions&titles=Main%20Page|Talk:Main%20Page&' .
  269. 'drvslots=*&drvprop=user|comment|content'
  270. => 'apihelp-query+deletedrevisions-example-titles',
  271. 'action=query&prop=deletedrevisions&revids=123456'
  272. => 'apihelp-query+deletedrevisions-example-revids',
  273. ];
  274. }
  275. public function getHelpUrls() {
  276. return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevisions';
  277. }
  278. }