MediaWiki  master
ApiQueryDeletedRevisions.php
Go to the documentation of this file.
1 <?php
35 
42 
44  private $revisionStore;
45 
48 
51 
63  public function __construct(
64  ApiQuery $query,
65  $moduleName,
73  ) {
74  parent::__construct(
75  $query,
76  $moduleName,
77  'drv',
83  );
84  $this->revisionStore = $revisionStore;
85  $this->changeTagDefStore = $changeTagDefStore;
86  $this->linkBatchFactory = $linkBatchFactory;
87  }
88 
89  protected function run( ApiPageSet $resultPageSet = null ) {
90  $user = $this->getUser();
91 
92  $pageSet = $this->getPageSet();
93  $pageMap = $pageSet->getGoodAndMissingTitlesByNamespace();
94  $pageCount = count( $pageSet->getGoodAndMissingPages() );
95  $revCount = $pageSet->getRevisionCount();
96  if ( $revCount === 0 && $pageCount === 0 ) {
97  // Nothing to do
98  return;
99  }
100  if ( $revCount !== 0 && count( $pageSet->getDeletedRevisionIDs() ) === 0 ) {
101  // Nothing to do, revisions were supplied but none are deleted
102  return;
103  }
104 
105  $params = $this->extractRequestParams( false );
106 
107  $db = $this->getDB();
108 
109  $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
110 
111  if ( $resultPageSet === null ) {
112  $this->parseParameters( $params );
113  $arQuery = $this->revisionStore->getArchiveQueryInfo();
114  $this->addTables( $arQuery['tables'] );
115  $this->addFields( $arQuery['fields'] );
116  $this->addJoinConds( $arQuery['joins'] );
117  $this->addFields( [ 'ar_title', 'ar_namespace' ] );
118  } else {
119  $this->limit = $this->getParameter( 'limit' ) ?: 10;
120  $this->addTables( 'archive' );
121  $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
122  }
123 
124  if ( $this->fld_tags ) {
125  $this->addFields( [ 'ts_tags' => ChangeTags::makeTagSummarySubquery( 'archive' ) ] );
126  }
127 
128  if ( $params['tag'] !== null ) {
129  $this->addTables( 'change_tag' );
130  $this->addJoinConds(
131  [ 'change_tag' => [ 'JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
132  );
133  try {
134  $this->addWhereFld( 'ct_tag_id', $this->changeTagDefStore->getId( $params['tag'] ) );
135  } catch ( NameTableAccessException $exception ) {
136  // Return nothing.
137  $this->addWhere( '1=0' );
138  }
139  }
140 
141  // This means stricter restrictions
142  if ( ( $this->fld_comment || $this->fld_parsedcomment ) &&
143  !$this->getAuthority()->isAllowed( 'deletedhistory' )
144  ) {
145  $this->dieWithError( 'apierror-cantview-deleted-comment', 'permissiondenied' );
146  }
147  if ( $this->fetchContent && !$this->getAuthority()->isAllowedAny( 'deletedtext', 'undelete' ) ) {
148  $this->dieWithError( 'apierror-cantview-deleted-revision-content', 'permissiondenied' );
149  }
150 
151  $dir = $params['dir'];
152 
153  if ( $revCount !== 0 ) {
154  $this->addWhere( [
155  'ar_rev_id' => array_keys( $pageSet->getDeletedRevisionIDs() )
156  ] );
157  } else {
158  // We need a custom WHERE clause that matches all titles.
159  $lb = $this->linkBatchFactory->newLinkBatch( $pageSet->getGoodAndMissingPages() );
160  $where = $lb->constructSet( 'ar', $db );
161  $this->addWhere( $where );
162  }
163 
164  if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
165  // In the non-generator case, the actor join will already be present.
166  if ( $resultPageSet !== null ) {
167  $this->addTables( 'actor' );
168  $this->addJoinConds( [ 'actor' => [ 'JOIN', 'actor_id=ar_actor' ] ] );
169  }
170  if ( $params['user'] !== null ) {
171  $this->addWhereFld( 'actor_name', $params['user'] );
172  } elseif ( $params['excludeuser'] !== null ) {
173  $this->addWhere( 'actor_name<>' . $db->addQuotes( $params['excludeuser'] ) );
174  }
175  }
176 
177  if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
178  // Paranoia: avoid brute force searches (T19342)
179  if ( !$this->getAuthority()->isAllowed( 'deletedhistory' ) ) {
180  $bitmask = RevisionRecord::DELETED_USER;
181  } elseif ( !$this->getAuthority()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
182  $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
183  } else {
184  $bitmask = 0;
185  }
186  if ( $bitmask ) {
187  $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
188  }
189  }
190 
191  if ( $params['continue'] !== null ) {
192  $cont = explode( '|', $params['continue'] );
193  $op = ( $dir == 'newer' ? '>' : '<' );
194  if ( $revCount !== 0 ) {
195  $this->dieContinueUsageIf( count( $cont ) != 2 );
196  $rev = (int)$cont[0];
197  $this->dieContinueUsageIf( strval( $rev ) !== $cont[0] );
198  $ar_id = (int)$cont[1];
199  $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
200  $this->addWhere( "ar_rev_id $op $rev OR " .
201  "(ar_rev_id = $rev AND " .
202  "ar_id $op= $ar_id)" );
203  } else {
204  $this->dieContinueUsageIf( count( $cont ) != 4 );
205  $ns = (int)$cont[0];
206  $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
207  $title = $db->addQuotes( $cont[1] );
208  $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
209  $ar_id = (int)$cont[3];
210  $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
211  $this->addWhere( "ar_namespace $op $ns OR " .
212  "(ar_namespace = $ns AND " .
213  "(ar_title $op $title OR " .
214  "(ar_title = $title AND " .
215  "(ar_timestamp $op $ts OR " .
216  "(ar_timestamp = $ts AND " .
217  "ar_id $op= $ar_id)))))" );
218  }
219  }
220 
221  $this->addOption( 'LIMIT', $this->limit + 1 );
222 
223  if ( $revCount !== 0 ) {
224  // Sort by ar_rev_id when querying by ar_rev_id
225  $this->addWhereRange( 'ar_rev_id', $dir, null, null );
226  } else {
227  // Sort by ns and title in the same order as timestamp for efficiency
228  // But only when not already unique in the query
229  if ( count( $pageMap ) > 1 ) {
230  $this->addWhereRange( 'ar_namespace', $dir, null, null );
231  }
232  $oneTitle = key( reset( $pageMap ) );
233  foreach ( $pageMap as $pages ) {
234  if ( count( $pages ) > 1 || key( $pages ) !== $oneTitle ) {
235  $this->addWhereRange( 'ar_title', $dir, null, null );
236  break;
237  }
238  }
239  $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
240  }
241  // Include in ORDER BY for uniqueness
242  $this->addWhereRange( 'ar_id', $dir, null, null );
243 
244  $res = $this->select( __METHOD__ );
245  $count = 0;
246  $generated = [];
247  foreach ( $res as $row ) {
248  if ( ++$count > $this->limit ) {
249  // We've had enough
250  $this->setContinueEnumParameter( 'continue',
251  $revCount
252  ? "$row->ar_rev_id|$row->ar_id"
253  : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
254  );
255  break;
256  }
257 
258  if ( $resultPageSet !== null ) {
259  $generated[] = $row->ar_rev_id;
260  } else {
261  if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
262  // Was it converted?
263  $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
264  $converted = $pageSet->getConvertedTitles();
265  if ( $title && isset( $converted[$title->getPrefixedText()] ) ) {
266  $title = Title::newFromText( $converted[$title->getPrefixedText()] );
267  if ( $title && isset( $pageMap[$title->getNamespace()][$title->getDBkey()] ) ) {
268  $pageMap[$row->ar_namespace][$row->ar_title] =
269  $pageMap[$title->getNamespace()][$title->getDBkey()];
270  }
271  }
272  }
273  if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
275  __METHOD__,
276  "Found row in archive (ar_id={$row->ar_id}) that didn't get processed by ApiPageSet"
277  );
278  }
279 
280  $fit = $this->addPageSubItem(
281  $pageMap[$row->ar_namespace][$row->ar_title],
282  $this->extractRevisionInfo( $this->revisionStore->newRevisionFromArchiveRow( $row ), $row ),
283  'rev'
284  );
285  if ( !$fit ) {
286  $this->setContinueEnumParameter( 'continue',
287  $revCount
288  ? "$row->ar_rev_id|$row->ar_id"
289  : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
290  );
291  break;
292  }
293  }
294  }
295 
296  if ( $resultPageSet !== null ) {
297  $resultPageSet->populateFromRevisionIDs( $generated );
298  }
299  }
300 
301  public function getAllowedParams() {
302  return parent::getAllowedParams() + [
303  'start' => [
304  ApiBase::PARAM_TYPE => 'timestamp',
305  ],
306  'end' => [
307  ApiBase::PARAM_TYPE => 'timestamp',
308  ],
309  'dir' => [
311  'newer',
312  'older'
313  ],
314  ApiBase::PARAM_DFLT => 'older',
315  ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
316  ],
317  'tag' => null,
318  'user' => [
319  ApiBase::PARAM_TYPE => 'user',
320  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'id', 'interwiki' ],
321  ],
322  'excludeuser' => [
323  ApiBase::PARAM_TYPE => 'user',
324  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'id', 'interwiki' ],
325  ],
326  'continue' => [
327  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
328  ],
329  ];
330  }
331 
332  protected function getExamplesMessages() {
333  return [
334  'action=query&prop=deletedrevisions&titles=Main%20Page|Talk:Main%20Page&' .
335  'drvslots=*&drvprop=user|comment|content'
336  => 'apihelp-query+deletedrevisions-example-titles',
337  'action=query&prop=deletedrevisions&revids=123456'
338  => 'apihelp-query+deletedrevisions-example-revids',
339  ];
340  }
341 
342  public function getHelpUrls() {
343  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevisions';
344  }
345 }
ApiQueryDeletedRevisions\run
run(ApiPageSet $resultPageSet=null)
Definition: ApiQueryDeletedRevisions.php:89
ApiQueryRevisionsBase\parseParameters
parseParameters( $params)
Parse the parameters into the various instance fields.
Definition: ApiQueryRevisionsBase.php:131
ChangeTags\makeTagSummarySubquery
static makeTagSummarySubquery( $tables)
Make the tag summary subquery based on the given tables and return it.
Definition: ChangeTags.php:983
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:383
ApiQueryBase\addFields
addFields( $value)
Add a set of fields to select to the internal array.
Definition: ApiQueryBase.php:212
ApiQuery
This is the main query class.
Definition: ApiQuery.php:37
MediaWiki\Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
ApiQueryRevisionsBase\$contentHandlerFactory
IContentHandlerFactory $contentHandlerFactory
Definition: ApiQueryRevisionsBase.php:68
ApiQueryDeletedRevisions
Query module to enumerate deleted revisions for pages.
Definition: ApiQueryDeletedRevisions.php:41
MediaWiki\Revision\RevisionStore
Service for looking up page revisions.
Definition: RevisionStore.php:88
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1379
ApiQueryBase\addTimestampWhereRange
addTimestampWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, similar to addWhereRange, but converts $start and $end t...
Definition: ApiQueryBase.php:366
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:105
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:72
ApiQueryDeletedRevisions\$linkBatchFactory
LinkBatchFactory $linkBatchFactory
Definition: ApiQueryDeletedRevisions.php:50
ApiQueryDeletedRevisions\$changeTagDefStore
NameTableStore $changeTagDefStore
Definition: ApiQueryDeletedRevisions.php:47
ApiQueryBase\addOption
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
Definition: ApiQueryBase.php:378
$res
$res
Definition: testCompression.php:57
ContextSource\getUser
getUser()
Definition: ContextSource.php:136
ApiPageSet
This class contains a list of pages that the client has requested.
Definition: ApiPageSet.php:45
Wikimedia\ParamValidator\ParamValidator::TypeDef\UserDef
Type definition for user types.
Definition: UserDef.php:26
ApiQueryRevisionsBase
A base class for functions common to producing a list of revisions.
Definition: ApiQueryRevisionsBase.php:42
ApiQueryGeneratorBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
Definition: ApiQueryGeneratorBase.php:83
ApiQueryDeletedRevisions\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryDeletedRevisions.php:332
ApiQueryGeneratorBase\getPageSet
getPageSet()
Get the PageSet object to work on.
Definition: ApiQueryGeneratorBase.php:57
MediaWiki\Cache\LinkBatchFactory
Definition: LinkBatchFactory.php:39
ApiQueryBase\getDB
getDB()
Get the Query database connection (read-only)
Definition: ApiQueryBase.php:117
MediaWiki\Content\Transform\ContentTransformer
A service to transform content.
Definition: ContentTransformer.php:15
ApiQueryBase\addTables
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
Definition: ApiQueryBase.php:182
ApiQueryBase\select
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
Definition: ApiQueryBase.php:399
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:707
$title
$title
Definition: testCompression.php:38
ApiQueryDeletedRevisions\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryDeletedRevisions.php:342
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:651
ApiQueryBase\addWhereRange
addWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, and an ORDER BY clause to sort in the right direction.
Definition: ApiQueryBase.php:337
ParserFactory
Definition: ParserFactory.php:37
ApiBase\requireMaxOneParameter
requireMaxOneParameter( $params,... $required)
Die if more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:879
ApiQueryRevisionsBase\$parserFactory
ParserFactory $parserFactory
Definition: ApiQueryRevisionsBase.php:71
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:1569
MediaWiki\Content\IContentHandlerFactory
Definition: IContentHandlerFactory.php:10
ContextSource\getAuthority
getAuthority()
Definition: ContextSource.php:144
ApiQueryBase\addJoinConds
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
Definition: ApiQueryBase.php:201
ApiQueryBase\addWhereFld
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
Definition: ApiQueryBase.php:282
MediaWiki\Storage\NameTableStore
Definition: NameTableStore.php:36
ApiQueryRevisionsBase\$slotRoleRegistry
SlotRoleRegistry $slotRoleRegistry
Definition: ApiQueryRevisionsBase.php:74
ApiQueryDeletedRevisions\getAllowedParams
getAllowedParams()
Definition: ApiQueryDeletedRevisions.php:301
ApiQueryDeletedRevisions\__construct
__construct(ApiQuery $query, $moduleName, RevisionStore $revisionStore, IContentHandlerFactory $contentHandlerFactory, ParserFactory $parserFactory, SlotRoleRegistry $slotRoleRegistry, NameTableStore $changeTagDefStore, LinkBatchFactory $linkBatchFactory, ContentTransformer $contentTransformer)
Definition: ApiQueryDeletedRevisions.php:63
ApiQueryRevisionsBase\$contentTransformer
ContentTransformer $contentTransformer
Definition: ApiQueryRevisionsBase.php:77
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:70
ApiBase\getParameter
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition: ApiBase.php:827
MediaWiki\Storage\NameTableAccessException
Exception representing a failure to look up a row from a name table.
Definition: NameTableAccessException.php:33
MediaWiki\Revision\SlotRoleRegistry
A registry service for SlotRoleHandlers, used to define which slot roles are available on which page.
Definition: SlotRoleRegistry.php:48
ApiQueryBase\addWhere
addWhere( $value)
Add a set of WHERE clauses to the internal array.
Definition: ApiQueryBase.php:245
ApiQueryBase\addPageSubItem
addPageSubItem( $pageId, $item, $elemname=null)
Same as addPageSubItems(), but one element of $data at a time.
Definition: ApiQueryBase.php:494
ApiBase\dieDebug
static dieDebug( $method, $message)
Internal code errors should be reported with this method.
Definition: ApiBase.php:1582
ApiQueryDeletedRevisions\$revisionStore
RevisionStore $revisionStore
Definition: ApiQueryDeletedRevisions.php:44