MediaWiki  master
ApiQueryAllRevisions.php
Go to the documentation of this file.
1 <?php
26 
34 
35  public function __construct( ApiQuery $query, $moduleName ) {
36  parent::__construct( $query, $moduleName, 'arv' );
37  }
38 
43  protected function run( ApiPageSet $resultPageSet = null ) {
44  $db = $this->getDB();
45  $params = $this->extractRequestParams( false );
46  $services = MediaWikiServices::getInstance();
47  $revisionStore = $services->getRevisionStore();
48 
49  $result = $this->getResult();
50 
51  $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
52 
53  $tsField = 'rev_timestamp';
54  $idField = 'rev_id';
55  $pageField = 'rev_page';
56  if ( $params['user'] !== null ) {
57  // The query is probably best done using the actor_timestamp index on
58  // revision_actor_temp. Use the denormalized fields from that table.
59  $tsField = 'revactor_timestamp';
60  $idField = 'revactor_rev';
61  $pageField = 'revactor_page';
62  }
63 
64  // Namespace check is likely to be desired, but can't be done
65  // efficiently in SQL.
66  $miser_ns = null;
67  $needPageTable = false;
68  if ( $params['namespace'] !== null ) {
69  $params['namespace'] = array_unique( $params['namespace'] );
70  sort( $params['namespace'] );
71  if ( $params['namespace'] != $services->getNamespaceInfo()->getValidNamespaces() ) {
72  $needPageTable = true;
73  if ( $this->getConfig()->get( 'MiserMode' ) ) {
74  $miser_ns = $params['namespace'];
75  } else {
76  $this->addWhere( [ 'page_namespace' => $params['namespace'] ] );
77  }
78  }
79  }
80 
81  if ( $resultPageSet === null ) {
82  $this->parseParameters( $params );
83  $revQuery = $revisionStore->getQueryInfo( [ 'page' ] );
84  } else {
85  $this->limit = $this->getParameter( 'limit' ) ?: 10;
86  $revQuery = [
87  'tables' => [ 'revision' ],
88  'fields' => [ 'rev_timestamp', 'rev_id' ],
89  'joins' => [],
90  ];
91 
92  if ( $params['generatetitles'] ) {
93  $revQuery['fields'][] = 'rev_page';
94  }
95 
96  if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
97  $actorQuery = ActorMigration::newMigration()->getJoin( 'rev_user' );
98  $revQuery['tables'] += $actorQuery['tables'];
99  $revQuery['joins'] += $actorQuery['joins'];
100  }
101 
102  if ( $needPageTable ) {
103  $revQuery['tables'][] = 'page';
104  $revQuery['joins']['page'] = [ 'JOIN', [ "$pageField = page_id" ] ];
105  if ( (bool)$miser_ns ) {
106  $revQuery['fields'][] = 'page_namespace';
107  }
108  }
109  }
110 
111  // If we're going to be using actor_timestamp, we need to swap the order of `revision`
112  // and `revision_actor_temp` in the query (for the straight join) and adjust some field aliases.
113  if ( $idField !== 'rev_id' && isset( $revQuery['tables']['temp_rev_user'] ) ) {
114  $aliasFields = [ 'rev_id' => $idField, 'rev_timestamp' => $tsField, 'rev_page' => $pageField ];
115  $revQuery['fields'] = array_merge(
116  $aliasFields,
117  array_diff( $revQuery['fields'], array_keys( $aliasFields ) )
118  );
119  unset( $revQuery['tables']['temp_rev_user'] );
120  $revQuery['tables'] = array_merge(
121  [ 'temp_rev_user' => 'revision_actor_temp' ],
122  $revQuery['tables']
123  );
124  $revQuery['joins']['revision'] = $revQuery['joins']['temp_rev_user'];
125  unset( $revQuery['joins']['temp_rev_user'] );
126  }
127 
128  $this->addTables( $revQuery['tables'] );
129  $this->addFields( $revQuery['fields'] );
130  $this->addJoinConds( $revQuery['joins'] );
131 
132  // Seems to be needed to avoid a planner bug (T113901)
133  $this->addOption( 'STRAIGHT_JOIN' );
134 
135  $dir = $params['dir'];
136  $this->addTimestampWhereRange( $tsField, $dir, $params['start'], $params['end'] );
137 
138  if ( $this->fld_tags ) {
139  $this->addFields( [ 'ts_tags' => ChangeTags::makeTagSummarySubquery( 'revision' ) ] );
140  }
141 
142  if ( $params['user'] !== null ) {
143  $actorQuery = ActorMigration::newMigration()
144  ->getWhere( $db, 'rev_user', $params['user'] );
145  $this->addWhere( $actorQuery['conds'] );
146  } elseif ( $params['excludeuser'] !== null ) {
147  $actorQuery = ActorMigration::newMigration()
148  ->getWhere( $db, 'rev_user', $params['excludeuser'] );
149  $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
150  }
151 
152  if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
153  // Paranoia: avoid brute force searches (T19342)
154  if ( !$this->getPermissionManager()->userHasRight( $this->getUser(), 'deletedhistory' ) ) {
155  $bitmask = RevisionRecord::DELETED_USER;
156  } elseif ( !$this->getPermissionManager()
157  ->userHasAnyRight( $this->getUser(), 'suppressrevision', 'viewsuppressed' )
158  ) {
159  $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
160  } else {
161  $bitmask = 0;
162  }
163  if ( $bitmask ) {
164  $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
165  }
166  }
167 
168  if ( $params['continue'] !== null ) {
169  $op = ( $dir == 'newer' ? '>' : '<' );
170  $cont = explode( '|', $params['continue'] );
171  $this->dieContinueUsageIf( count( $cont ) != 2 );
172  $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
173  $rev_id = (int)$cont[1];
174  $this->dieContinueUsageIf( strval( $rev_id ) !== $cont[1] );
175  $this->addWhere( "$tsField $op $ts OR " .
176  "($tsField = $ts AND " .
177  "$idField $op= $rev_id)" );
178  }
179 
180  $this->addOption( 'LIMIT', $this->limit + 1 );
181 
182  $sort = ( $dir == 'newer' ? '' : ' DESC' );
183  $orderby = [];
184  // Targeting index rev_timestamp, user_timestamp, usertext_timestamp, or actor_timestamp.
185  // But 'user' is always constant for the latter three, so it doesn't matter here.
186  $orderby[] = "rev_timestamp $sort";
187  $orderby[] = "rev_id $sort";
188  $this->addOption( 'ORDER BY', $orderby );
189 
190  $hookData = [];
191  $res = $this->select( __METHOD__, [], $hookData );
192 
193  if ( $resultPageSet === null ) {
194  $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
195  }
196 
197  $pageMap = []; // Maps rev_page to array index
198  $count = 0;
199  $nextIndex = 0;
200  $generated = [];
201  foreach ( $res as $row ) {
202  if ( $count === 0 && $resultPageSet !== null ) {
203  // Set the non-continue since the list of all revisions is
204  // prone to having entries added at the start frequently.
205  $this->getContinuationManager()->addGeneratorNonContinueParam(
206  $this, 'continue', "$row->rev_timestamp|$row->rev_id"
207  );
208  }
209  if ( ++$count > $this->limit ) {
210  // We've had enough
211  $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" );
212  break;
213  }
214 
215  // Miser mode namespace check
216  if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
217  continue;
218  }
219 
220  if ( $resultPageSet !== null ) {
221  if ( $params['generatetitles'] ) {
222  $generated[$row->rev_page] = $row->rev_page;
223  } else {
224  $generated[] = $row->rev_id;
225  }
226  } else {
227  $revision = $revisionStore->newRevisionFromRow( $row, 0, Title::newFromRow( $row ) );
228  $rev = $this->extractRevisionInfo( $revision, $row );
229 
230  if ( !isset( $pageMap[$row->rev_page] ) ) {
231  $index = $nextIndex++;
232  $pageMap[$row->rev_page] = $index;
233  $title = Title::newFromLinkTarget( $revision->getPageAsLinkTarget() );
234  $a = [
235  'pageid' => $title->getArticleID(),
236  'revisions' => [ $rev ],
237  ];
238  ApiResult::setIndexedTagName( $a['revisions'], 'rev' );
240  $fit = $this->processRow( $row, $a['revisions'][0], $hookData ) &&
241  $result->addValue( [ 'query', $this->getModuleName() ], $index, $a );
242  } else {
243  $index = $pageMap[$row->rev_page];
244  $fit = $this->processRow( $row, $rev, $hookData ) &&
245  $result->addValue( [ 'query', $this->getModuleName(), $index, 'revisions' ], null, $rev );
246  }
247  if ( !$fit ) {
248  $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" );
249  break;
250  }
251  }
252  }
253 
254  if ( $resultPageSet !== null ) {
255  if ( $params['generatetitles'] ) {
256  $resultPageSet->populateFromPageIDs( $generated );
257  } else {
258  $resultPageSet->populateFromRevisionIDs( $generated );
259  }
260  } else {
261  $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
262  }
263  }
264 
265  public function getAllowedParams() {
266  $ret = parent::getAllowedParams() + [
267  'user' => [
268  ApiBase::PARAM_TYPE => 'user',
269  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'id', 'interwiki' ],
270  UserDef::PARAM_RETURN_OBJECT => true,
271  ],
272  'namespace' => [
273  ApiBase::PARAM_ISMULTI => true,
274  ApiBase::PARAM_TYPE => 'namespace',
275  ApiBase::PARAM_DFLT => null,
276  ],
277  'start' => [
278  ApiBase::PARAM_TYPE => 'timestamp',
279  ],
280  'end' => [
281  ApiBase::PARAM_TYPE => 'timestamp',
282  ],
283  'dir' => [
285  'newer',
286  'older'
287  ],
288  ApiBase::PARAM_DFLT => 'older',
289  ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
290  ],
291  'excludeuser' => [
292  ApiBase::PARAM_TYPE => 'user',
293  UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'id', 'interwiki' ],
294  UserDef::PARAM_RETURN_OBJECT => true,
295  ],
296  'continue' => [
297  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
298  ],
299  'generatetitles' => [
300  ApiBase::PARAM_DFLT => false,
301  ],
302  ];
303 
304  if ( $this->getConfig()->get( 'MiserMode' ) ) {
305  $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
306  'api-help-param-limited-in-miser-mode',
307  ];
308  }
309 
310  return $ret;
311  }
312 
313  protected function getExamplesMessages() {
314  return [
315  'action=query&list=allrevisions&arvuser=Example&arvlimit=50'
316  => 'apihelp-query+allrevisions-example-user',
317  'action=query&list=allrevisions&arvdir=newer&arvlimit=50'
318  => 'apihelp-query+allrevisions-example-ns-any',
319  ];
320  }
321 
322  public function getHelpUrls() {
323  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allrevisions';
324  }
325 }
ApiQueryRevisionsBase\parseParameters
parseParameters( $params)
Parse the parameters into the various instance fields.
Definition: ApiQueryRevisionsBase.php:82
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:67
ChangeTags\makeTagSummarySubquery
static makeTagSummarySubquery( $tables)
Make the tag summary subquery based on the given tables and return it.
Definition: ChangeTags.php:887
ApiQueryBase\processRow
processRow( $row, array &$data, array &$hookData)
Call the ApiQueryBaseProcessRow hook.
Definition: ApiQueryBase.php:452
ApiQueryBase\addFields
addFields( $value)
Add a set of fields to select to the internal array.
Definition: ApiQueryBase.php:215
ApiQueryAllRevisions\run
run(ApiPageSet $resultPageSet=null)
Definition: ApiQueryAllRevisions.php:43
ApiQuery
This is the main query class.
Definition: ApiQuery.php:37
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:46
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
ApiQueryAllRevisions\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryAllRevisions.php:313
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:369
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:107
ApiBase\PARAM_TYPE
const PARAM_TYPE
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:71
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:565
ApiQueryAllRevisions\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryAllRevisions.php:322
ApiQueryBase\addOption
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
Definition: ApiQueryBase.php:381
$res
$res
Definition: testCompression.php:57
ContextSource\getUser
getUser()
Stable to override.
Definition: ContextSource.php:131
$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:114
ActorMigration\newMigration
static newMigration()
Static constructor.
Definition: ActorMigration.php:140
ApiPageSet
This class contains a list of pages that the client has requested.
Definition: ApiPageSet.php:42
Wikimedia\ParamValidator\ParamValidator::TypeDef\UserDef
Type definition for user types.
Definition: UserDef.php:25
ApiQueryRevisionsBase
A base class for functions common to producing a list of revisions.
Definition: ApiQueryRevisionsBase.php:39
ApiQueryGeneratorBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
Definition: ApiQueryGeneratorBase.php:83
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:630
Title\newFromRow
static newFromRow( $row)
Make a Title object from a DB row.
Definition: Title.php:524
ApiQueryAllRevisions\getAllowedParams
getAllowedParams()
Stable to override.
Definition: ApiQueryAllRevisions.php:265
ApiQueryBase\getDB
getDB()
Get the Query database connection (read-only) Stable to override.
Definition: ApiQueryBase.php:119
ApiQueryBase\addTables
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
Definition: ApiQueryBase.php:185
ApiQueryBase\select
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
Definition: ApiQueryBase.php:402
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:717
$title
$title
Definition: testCompression.php:38
ApiQueryAllRevisions
Query module to enumerate all revisions.
Definition: ApiQueryAllRevisions.php:33
ApiBase\getContinuationManager
getContinuationManager()
Get the continuation manager.
Definition: ApiBase.php:607
ApiResult\setIndexedTagName
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
Definition: ApiResult.php:604
ApiBase\requireMaxOneParameter
requireMaxOneParameter( $params,... $required)
Die if more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:889
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:1562
ApiQueryRevisionsBase\extractRevisionInfo
extractRevisionInfo(RevisionRecord $revision, $row)
Extract information from the RevisionRecord.
Definition: ApiQueryRevisionsBase.php:238
ApiBase\getPermissionManager
getPermissionManager()
Obtain a PermissionManager instance that subclasses may use in their authorization checks.
Definition: ApiBase.php:637
ApiQueryBase\addJoinConds
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
Definition: ApiQueryBase.php:204
Title\newFromLinkTarget
static newFromLinkTarget(LinkTarget $linkTarget, $forceClone='')
Returns a Title given a LinkTarget.
Definition: Title.php:281
ApiBase\PARAM_DFLT
const PARAM_DFLT
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:69
ApiBase\getParameter
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition: ApiBase.php:837
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:444
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:70
ApiQueryBase\addWhere
addWhere( $value)
Add a set of WHERE clauses to the internal array.
Definition: ApiQueryBase.php:248
ApiQueryAllRevisions\__construct
__construct(ApiQuery $query, $moduleName)
Definition: ApiQueryAllRevisions.php:35
ApiQueryBase\addTitleInfo
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
Definition: ApiQueryBase.php:470