MediaWiki  master
ApiQueryAllRevisions.php
Go to the documentation of this file.
1 <?php
29 
37 
39  private $revisionStore;
40 
42  private $actorMigration;
43 
45  private $namespaceInfo;
46 
58  public function __construct(
59  ApiQuery $query,
60  $moduleName,
68  ) {
69  parent::__construct(
70  $query,
71  $moduleName,
72  'arv',
78  );
79  $this->revisionStore = $revisionStore;
80  $this->actorMigration = $actorMigration;
81  $this->namespaceInfo = $namespaceInfo;
82  }
83 
88  protected function run( ApiPageSet $resultPageSet = null ) {
90 
91  $db = $this->getDB();
92  $params = $this->extractRequestParams( false );
93 
94  $result = $this->getResult();
95 
96  $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
97 
98  $tsField = 'rev_timestamp';
99  $idField = 'rev_id';
100  $pageField = 'rev_page';
101  if ( $params['user'] !== null &&
103  ) {
104  // The query is probably best done using the actor_timestamp index on
105  // revision_actor_temp. Use the denormalized fields from that table.
106  $tsField = 'revactor_timestamp';
107  $idField = 'revactor_rev';
108  $pageField = 'revactor_page';
109  }
110 
111  // Namespace check is likely to be desired, but can't be done
112  // efficiently in SQL.
113  $miser_ns = null;
114  $needPageTable = false;
115  if ( $params['namespace'] !== null ) {
116  $params['namespace'] = array_unique( $params['namespace'] );
117  sort( $params['namespace'] );
118  if ( $params['namespace'] != $this->namespaceInfo->getValidNamespaces() ) {
119  $needPageTable = true;
120  if ( $this->getConfig()->get( 'MiserMode' ) ) {
121  $miser_ns = $params['namespace'];
122  } else {
123  $this->addWhere( [ 'page_namespace' => $params['namespace'] ] );
124  }
125  }
126  }
127 
128  if ( $resultPageSet === null ) {
129  $this->parseParameters( $params );
130  $revQuery = $this->revisionStore->getQueryInfo( [ 'page' ] );
131  } else {
132  $this->limit = $this->getParameter( 'limit' ) ?: 10;
133  $revQuery = [
134  'tables' => [ 'revision' ],
135  'fields' => [ 'rev_timestamp', 'rev_id' ],
136  'joins' => [],
137  ];
138 
139  if ( $params['generatetitles'] ) {
140  $revQuery['fields'][] = 'rev_page';
141  }
142 
143  if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
144  $actorQuery = $this->actorMigration->getJoin( 'rev_user' );
145  $revQuery['tables'] += $actorQuery['tables'];
146  $revQuery['joins'] += $actorQuery['joins'];
147  }
148 
149  if ( $needPageTable ) {
150  $revQuery['tables'][] = 'page';
151  $revQuery['joins']['page'] = [ 'JOIN', [ "$pageField = page_id" ] ];
152  if ( (bool)$miser_ns ) {
153  $revQuery['fields'][] = 'page_namespace';
154  }
155  }
156  }
157 
158  // If we're going to be using actor_timestamp, we need to swap the order of `revision`
159  // and `revision_actor_temp` in the query (for the straight join) and adjust some field aliases.
160  if ( $idField !== 'rev_id' && isset( $revQuery['tables']['temp_rev_user'] ) ) {
161  $aliasFields = [ 'rev_id' => $idField, 'rev_timestamp' => $tsField, 'rev_page' => $pageField ];
162  $revQuery['fields'] = array_merge(
163  $aliasFields,
164  array_diff( $revQuery['fields'], array_keys( $aliasFields ) )
165  );
166  unset( $revQuery['tables']['temp_rev_user'] );
167  $revQuery['tables'] = array_merge(
168  [ 'temp_rev_user' => 'revision_actor_temp' ],
169  $revQuery['tables']
170  );
171  $revQuery['joins']['revision'] = $revQuery['joins']['temp_rev_user'];
172  unset( $revQuery['joins']['temp_rev_user'] );
173  }
174 
175  $this->addTables( $revQuery['tables'] );
176  $this->addFields( $revQuery['fields'] );
177  $this->addJoinConds( $revQuery['joins'] );
178 
179  // Seems to be needed to avoid a planner bug (T113901)
180  $this->addOption( 'STRAIGHT_JOIN' );
181 
182  $dir = $params['dir'];
183  $this->addTimestampWhereRange( $tsField, $dir, $params['start'], $params['end'] );
184 
185  if ( $this->fld_tags ) {
186  $this->addFields( [ 'ts_tags' => ChangeTags::makeTagSummarySubquery( 'revision' ) ] );
187  }
188 
189  if ( $params['user'] !== null ) {
190  $actorQuery = $this->actorMigration->getWhere( $db, 'rev_user', $params['user'] );
191  $this->addWhere( $actorQuery['conds'] );
192  } elseif ( $params['excludeuser'] !== null ) {
193  $actorQuery = $this->actorMigration->getWhere( $db, 'rev_user', $params['excludeuser'] );
194  $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
195  }
196 
197  if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
198  // Paranoia: avoid brute force searches (T19342)
199  if ( !$this->getAuthority()->isAllowed( 'deletedhistory' ) ) {
200  $bitmask = RevisionRecord::DELETED_USER;
201  } elseif ( !$this->getAuthority()->isAllowedAny( 'suppressrevision', 'viewsuppressed' )
202  ) {
203  $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
204  } else {
205  $bitmask = 0;
206  }
207  if ( $bitmask ) {
208  $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
209  }
210  }
211 
212  if ( $params['continue'] !== null ) {
213  $op = ( $dir == 'newer' ? '>' : '<' );
214  $cont = explode( '|', $params['continue'] );
215  $this->dieContinueUsageIf( count( $cont ) != 2 );
216  $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
217  $rev_id = (int)$cont[1];
218  $this->dieContinueUsageIf( strval( $rev_id ) !== $cont[1] );
219  $this->addWhere( "$tsField $op $ts OR " .
220  "($tsField = $ts AND " .
221  "$idField $op= $rev_id)" );
222  }
223 
224  $this->addOption( 'LIMIT', $this->limit + 1 );
225 
226  $sort = ( $dir == 'newer' ? '' : ' DESC' );
227  $orderby = [];
228  // Targeting index rev_timestamp, user_timestamp, usertext_timestamp, or actor_timestamp.
229  // But 'user' is always constant for the latter three, so it doesn't matter here.
230  $orderby[] = "rev_timestamp $sort";
231  $orderby[] = "rev_id $sort";
232  $this->addOption( 'ORDER BY', $orderby );
233 
234  $hookData = [];
235  $res = $this->select( __METHOD__, [], $hookData );
236 
237  if ( $resultPageSet === null ) {
238  $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
239  }
240 
241  $pageMap = []; // Maps rev_page to array index
242  $count = 0;
243  $nextIndex = 0;
244  $generated = [];
245  foreach ( $res as $row ) {
246  if ( $count === 0 && $resultPageSet !== null ) {
247  // Set the non-continue since the list of all revisions is
248  // prone to having entries added at the start frequently.
249  $this->getContinuationManager()->addGeneratorNonContinueParam(
250  $this, 'continue', "$row->rev_timestamp|$row->rev_id"
251  );
252  }
253  if ( ++$count > $this->limit ) {
254  // We've had enough
255  $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" );
256  break;
257  }
258 
259  // Miser mode namespace check
260  if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
261  continue;
262  }
263 
264  if ( $resultPageSet !== null ) {
265  if ( $params['generatetitles'] ) {
266  $generated[$row->rev_page] = $row->rev_page;
267  } else {
268  $generated[] = $row->rev_id;
269  }
270  } else {
271  $revision = $this->revisionStore->newRevisionFromRow( $row, 0, Title::newFromRow( $row ) );
272  $rev = $this->extractRevisionInfo( $revision, $row );
273 
274  if ( !isset( $pageMap[$row->rev_page] ) ) {
275  $index = $nextIndex++;
276  $pageMap[$row->rev_page] = $index;
277  $title = Title::newFromLinkTarget( $revision->getPageAsLinkTarget() );
278  $a = [
279  'pageid' => $title->getArticleID(),
280  'revisions' => [ $rev ],
281  ];
282  ApiResult::setIndexedTagName( $a['revisions'], 'rev' );
284  $fit = $this->processRow( $row, $a['revisions'][0], $hookData ) &&
285  $result->addValue( [ 'query', $this->getModuleName() ], $index, $a );
286  } else {
287  $index = $pageMap[$row->rev_page];
288  $fit = $this->processRow( $row, $rev, $hookData ) &&
289  $result->addValue( [ 'query', $this->getModuleName(), $index, 'revisions' ], null, $rev );
290  }
291  if ( !$fit ) {
292  $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" );
293  break;
294  }
295  }
296  }
297 
298  if ( $resultPageSet !== null ) {
299  if ( $params['generatetitles'] ) {
300  $resultPageSet->populateFromPageIDs( $generated );
301  } else {
302  $resultPageSet->populateFromRevisionIDs( $generated );
303  }
304  } else {
305  $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
306  }
307  }
308 
309  public function getAllowedParams() {
310  $ret = parent::getAllowedParams() + [
311  'user' => [
312  ApiBase::PARAM_TYPE => 'user',
313  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'id', 'interwiki' ],
314  UserDef::PARAM_RETURN_OBJECT => true,
315  ],
316  'namespace' => [
317  ApiBase::PARAM_ISMULTI => true,
318  ApiBase::PARAM_TYPE => 'namespace',
319  ApiBase::PARAM_DFLT => null,
320  ],
321  'start' => [
322  ApiBase::PARAM_TYPE => 'timestamp',
323  ],
324  'end' => [
325  ApiBase::PARAM_TYPE => 'timestamp',
326  ],
327  'dir' => [
329  'newer',
330  'older'
331  ],
332  ApiBase::PARAM_DFLT => 'older',
333  ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
334  ],
335  'excludeuser' => [
336  ApiBase::PARAM_TYPE => 'user',
337  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'id', 'interwiki' ],
338  UserDef::PARAM_RETURN_OBJECT => true,
339  ],
340  'continue' => [
341  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
342  ],
343  'generatetitles' => [
344  ApiBase::PARAM_DFLT => false,
345  ],
346  ];
347 
348  if ( $this->getConfig()->get( 'MiserMode' ) ) {
349  $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
350  'api-help-param-limited-in-miser-mode',
351  ];
352  }
353 
354  return $ret;
355  }
356 
357  protected function getExamplesMessages() {
358  return [
359  'action=query&list=allrevisions&arvuser=Example&arvlimit=50'
360  => 'apihelp-query+allrevisions-example-user',
361  'action=query&list=allrevisions&arvdir=newer&arvlimit=50'
362  => 'apihelp-query+allrevisions-example-ns-any',
363  ];
364  }
365 
366  public function getHelpUrls() {
367  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allrevisions';
368  }
369 }
ApiQueryRevisionsBase\parseParameters
parseParameters( $params)
Parse the parameters into the various instance fields.
Definition: ApiQueryRevisionsBase.php:131
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:72
ChangeTags\makeTagSummarySubquery
static makeTagSummarySubquery( $tables)
Make the tag summary subquery based on the given tables and return it.
Definition: ChangeTags.php:983
ApiQueryBase\processRow
processRow( $row, array &$data, array &$hookData)
Call the ApiQueryBaseProcessRow hook.
Definition: ApiQueryBase.php:449
ApiQueryBase\addFields
addFields( $value)
Add a set of fields to select to the internal array.
Definition: ApiQueryBase.php:212
ApiQueryAllRevisions\run
run(ApiPageSet $resultPageSet=null)
Definition: ApiQueryAllRevisions.php:88
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
MediaWiki\Revision\RevisionStore
Service for looking up page revisions.
Definition: RevisionStore.php:88
ApiQueryAllRevisions\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryAllRevisions.php:357
ApiQueryAllRevisions\$revisionStore
RevisionStore $revisionStore
Definition: ApiQueryAllRevisions.php:39
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
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:571
ApiQueryAllRevisions\$actorMigration
ActorMigration $actorMigration
Definition: ApiQueryAllRevisions.php:42
ApiQueryAllRevisions\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryAllRevisions.php:366
ApiQueryBase\addOption
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
Definition: ApiQueryBase.php:378
ActorMigration
This is not intended to be a long-term part of MediaWiki; it will be deprecated and removed once acto...
Definition: ActorMigration.php:15
$res
$res
Definition: testCompression.php:57
$revQuery
$revQuery
Definition: testCompression.php:56
ApiBase\PARAM_HELP_MSG_APPEND
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition: ApiBase.php:112
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
ApiQueryAllRevisions\$namespaceInfo
NamespaceInfo $namespaceInfo
Definition: ApiQueryAllRevisions.php:45
ApiQueryBase\executeGenderCacheFromResultWrapper
executeGenderCacheFromResultWrapper(IResultWrapper $res, $fname=__METHOD__, $fieldPrefix='page')
Preprocess the result set to fill the GenderCache with the necessary information before using self::a...
Definition: ApiQueryBase.php:626
Title\newFromRow
static newFromRow( $row)
Make a Title object from a DB row.
Definition: Title.php:580
ApiQueryAllRevisions\getAllowedParams
getAllowedParams()
Definition: ApiQueryAllRevisions.php:309
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
ApiQueryAllRevisions
Query module to enumerate all revisions.
Definition: ApiQueryAllRevisions.php:36
ApiBase\getContinuationManager
getContinuationManager()
Definition: ApiBase.php:605
ApiQueryAllRevisions\__construct
__construct(ApiQuery $query, $moduleName, RevisionStore $revisionStore, IContentHandlerFactory $contentHandlerFactory, ParserFactory $parserFactory, SlotRoleRegistry $slotRoleRegistry, ActorMigration $actorMigration, NamespaceInfo $namespaceInfo, ContentTransformer $contentTransformer)
Definition: ApiQueryAllRevisions.php:58
ApiResult\setIndexedTagName
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:603
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
ApiQueryRevisionsBase\extractRevisionInfo
extractRevisionInfo(RevisionRecord $revision, $row)
Extract information from the RevisionRecord.
Definition: ApiQueryRevisionsBase.php:286
ApiQueryBase\addJoinConds
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
Definition: ApiQueryBase.php:201
ApiQueryRevisionsBase\$slotRoleRegistry
SlotRoleRegistry $slotRoleRegistry
Definition: ApiQueryRevisionsBase.php:74
Title\newFromLinkTarget
static newFromLinkTarget(LinkTarget $linkTarget, $forceClone='')
Returns a Title given a LinkTarget.
Definition: Title.php:295
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
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:440
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:71
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
NamespaceInfo
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Definition: NamespaceInfo.php:35
SCHEMA_COMPAT_READ_TEMP
const SCHEMA_COMPAT_READ_TEMP
Definition: Defines.php:265
ApiQueryBase\addTitleInfo
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Definition: ApiQueryBase.php:466
$wgActorTableSchemaMigrationStage
int $wgActorTableSchemaMigrationStage
Actor table schema migration stage, for migration from the temporary table revision_actor_temp to the...
Definition: DefaultSettings.php:2405