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