MediaWiki REL1_37
ApiQueryAllRevisions.php
Go to the documentation of this file.
1<?php
29
37
40
43
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' => [
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}
getAuthority()
int $wgActorTableSchemaMigrationStage
Actor table schema migration stage, for migration from the temporary table revision_actor_temp to the...
const SCHEMA_COMPAT_READ_TEMP
Definition Defines.php:265
This is not intended to be a long-term part of MediaWiki; it will be deprecated and removed once acto...
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition ApiBase.php:884
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition ApiBase.php:1620
const PARAM_TYPE
Definition ApiBase.php:81
const PARAM_DFLT
Definition ApiBase.php:73
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition ApiBase.php:169
requireMaxOneParameter( $params,... $required)
Die if more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:936
getResult()
Get the result object.
Definition ApiBase.php:628
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:764
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:162
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:497
getContinuationManager()
Definition ApiBase.php:662
const PARAM_ISMULTI
Definition ApiBase.php:77
This class contains a list of pages that the client has requested.
Query module to enumerate all revisions.
__construct(ApiQuery $query, $moduleName, RevisionStore $revisionStore, IContentHandlerFactory $contentHandlerFactory, ParserFactory $parserFactory, SlotRoleRegistry $slotRoleRegistry, ActorMigration $actorMigration, NamespaceInfo $namespaceInfo, ContentTransformer $contentTransformer)
getHelpUrls()
Return links to more detailed help pages about the module.
getExamplesMessages()
Returns usage examples for this module.
run(ApiPageSet $resultPageSet=null)
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
processRow( $row, array &$data, array &$hookData)
Call the ApiQueryBaseProcessRow hook.
addFields( $value)
Add a set of fields to select to the internal array.
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)
executeGenderCacheFromResultWrapper(IResultWrapper $res, $fname=__METHOD__, $fieldPrefix='page')
Preprocess the result set to fill the GenderCache with the necessary information before using self::a...
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.
addWhere( $value)
Add a set of WHERE clauses to the internal array.
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
A base class for functions common to producing a list of revisions.
parseParameters( $params)
Parse the parameters into the various instance fields.
IContentHandlerFactory $contentHandlerFactory
ContentTransformer $contentTransformer
SlotRoleRegistry $slotRoleRegistry
extractRevisionInfo(RevisionRecord $revision, $row)
Extract information from the RevisionRecord.
This is the main query class.
Definition ApiQuery.php:37
static makeTagSummarySubquery( $tables)
Make the tag summary subquery based on the given tables and return it.
Type definition for user types.
Definition UserDef.php:25
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.
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...