MediaWiki REL1_40
ApiQueryAllRevisions.php
Go to the documentation of this file.
1<?php
35
43
45 private $revisionStore;
46
48 private $actorMigration;
49
51 private $namespaceInfo;
52
66 public function __construct(
67 ApiQuery $query,
68 $moduleName,
69 RevisionStore $revisionStore,
70 IContentHandlerFactory $contentHandlerFactory,
71 ParserFactory $parserFactory,
72 SlotRoleRegistry $slotRoleRegistry,
73 ActorMigration $actorMigration,
74 NamespaceInfo $namespaceInfo,
75 ContentRenderer $contentRenderer,
76 ContentTransformer $contentTransformer,
77 CommentFormatter $commentFormatter
78 ) {
79 parent::__construct(
80 $query,
81 $moduleName,
82 'arv',
83 $revisionStore,
84 $contentHandlerFactory,
85 $parserFactory,
86 $slotRoleRegistry,
87 $contentRenderer,
88 $contentTransformer,
89 $commentFormatter
90 );
91 $this->revisionStore = $revisionStore;
92 $this->actorMigration = $actorMigration;
93 $this->namespaceInfo = $namespaceInfo;
94 }
95
100 protected function run( ApiPageSet $resultPageSet = null ) {
101 $db = $this->getDB();
102 $params = $this->extractRequestParams( false );
103
104 $result = $this->getResult();
105
106 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
107
108 $tsField = 'rev_timestamp';
109 $idField = 'rev_id';
110 $pageField = 'rev_page';
111
112 // Namespace check is likely to be desired, but can't be done
113 // efficiently in SQL.
114 $miser_ns = null;
115 $needPageTable = false;
116 if ( $params['namespace'] !== null ) {
117 $params['namespace'] = array_unique( $params['namespace'] );
118 sort( $params['namespace'] );
119 if ( $params['namespace'] != $this->namespaceInfo->getValidNamespaces() ) {
120 $needPageTable = true;
121 if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
122 $miser_ns = $params['namespace'];
123 } else {
124 $this->addWhere( [ 'page_namespace' => $params['namespace'] ] );
125 }
126 }
127 }
128
129 if ( $resultPageSet === null ) {
130 $this->parseParameters( $params );
131 $revQuery = $this->revisionStore->getQueryInfo( [ 'page' ] );
132 } else {
133 $this->limit = $this->getParameter( 'limit' ) ?: 10;
134 $revQuery = [
135 'tables' => [ 'revision' ],
136 'fields' => [ 'rev_timestamp', 'rev_id' ],
137 'joins' => [],
138 ];
139
140 if ( $params['generatetitles'] ) {
141 $revQuery['fields'][] = 'rev_page';
142 }
143
144 if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
145 $actorQuery = $this->actorMigration->getJoin( 'rev_user' );
146 $revQuery['tables'] += $actorQuery['tables'];
147 $revQuery['joins'] += $actorQuery['joins'];
148 }
149
150 if ( $needPageTable ) {
151 $revQuery['tables'][] = 'page';
152 $revQuery['joins']['page'] = [ 'JOIN', [ "$pageField = page_id" ] ];
153 if ( (bool)$miser_ns ) {
154 $revQuery['fields'][] = 'page_namespace';
155 }
156 }
157 }
158
159 $this->addTables( $revQuery['tables'] );
160 $this->addFields( $revQuery['fields'] );
161 $this->addJoinConds( $revQuery['joins'] );
162
163 // Seems to be needed to avoid a planner bug (T113901)
164 $this->addOption( 'STRAIGHT_JOIN' );
165
166 $dir = $params['dir'];
167 $this->addTimestampWhereRange( $tsField, $dir, $params['start'], $params['end'] );
168
169 if ( $this->fld_tags ) {
170 $this->addFields( [ 'ts_tags' => ChangeTags::makeTagSummarySubquery( 'revision' ) ] );
171 }
172
173 if ( $params['user'] !== null ) {
174 $actorQuery = $this->actorMigration->getWhere( $db, 'rev_user', $params['user'] );
175 $this->addWhere( $actorQuery['conds'] );
176 } elseif ( $params['excludeuser'] !== null ) {
177 $actorQuery = $this->actorMigration->getWhere( $db, 'rev_user', $params['excludeuser'] );
178 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
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 ) {
187 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
188 } else {
189 $bitmask = 0;
190 }
191 if ( $bitmask ) {
192 $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
193 }
194 }
195
196 if ( $params['continue'] !== null ) {
197 $op = ( $dir == 'newer' ? '>=' : '<=' );
198 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'timestamp', 'int' ] );
199 $this->addWhere( $db->buildComparison( $op, [
200 $tsField => $db->timestamp( $cont[0] ),
201 $idField => $cont[1],
202 ] ) );
203 }
204
205 $this->addOption( 'LIMIT', $this->limit + 1 );
206
207 $sort = ( $dir == 'newer' ? '' : ' DESC' );
208 $orderby = [];
209 // Targeting index rev_timestamp, user_timestamp, usertext_timestamp, or actor_timestamp.
210 // But 'user' is always constant for the latter three, so it doesn't matter here.
211 $orderby[] = "rev_timestamp $sort";
212 $orderby[] = "rev_id $sort";
213 $this->addOption( 'ORDER BY', $orderby );
214
215 $hookData = [];
216 $res = $this->select( __METHOD__, [], $hookData );
217
218 if ( $resultPageSet === null ) {
219 $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
220 }
221
222 $pageMap = []; // Maps rev_page to array index
223 $count = 0;
224 $nextIndex = 0;
225 $generated = [];
226 foreach ( $res as $row ) {
227 if ( $count === 0 && $resultPageSet !== null ) {
228 // Set the non-continue since the list of all revisions is
229 // prone to having entries added at the start frequently.
230 $this->getContinuationManager()->addGeneratorNonContinueParam(
231 $this, 'continue', "$row->rev_timestamp|$row->rev_id"
232 );
233 }
234 if ( ++$count > $this->limit ) {
235 // We've had enough
236 $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" );
237 break;
238 }
239
240 // Miser mode namespace check
241 if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
242 continue;
243 }
244
245 if ( $resultPageSet !== null ) {
246 if ( $params['generatetitles'] ) {
247 $generated[$row->rev_page] = $row->rev_page;
248 } else {
249 $generated[] = $row->rev_id;
250 }
251 } else {
252 $revision = $this->revisionStore->newRevisionFromRow( $row, 0, Title::newFromRow( $row ) );
253 $rev = $this->extractRevisionInfo( $revision, $row );
254
255 if ( !isset( $pageMap[$row->rev_page] ) ) {
256 $index = $nextIndex++;
257 $pageMap[$row->rev_page] = $index;
258 $title = Title::newFromLinkTarget( $revision->getPageAsLinkTarget() );
259 $a = [
260 'pageid' => $title->getArticleID(),
261 'revisions' => [ $rev ],
262 ];
263 ApiResult::setIndexedTagName( $a['revisions'], 'rev' );
265 $fit = $this->processRow( $row, $a['revisions'][0], $hookData ) &&
266 $result->addValue( [ 'query', $this->getModuleName() ], $index, $a );
267 } else {
268 $index = $pageMap[$row->rev_page];
269 $fit = $this->processRow( $row, $rev, $hookData ) &&
270 $result->addValue( [ 'query', $this->getModuleName(), $index, 'revisions' ], null, $rev );
271 }
272 if ( !$fit ) {
273 $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" );
274 break;
275 }
276 }
277 }
278
279 if ( $resultPageSet !== null ) {
280 if ( $params['generatetitles'] ) {
281 $resultPageSet->populateFromPageIDs( $generated );
282 } else {
283 $resultPageSet->populateFromRevisionIDs( $generated );
284 }
285 } else {
286 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
287 }
288 }
289
290 public function getAllowedParams() {
291 $ret = parent::getAllowedParams() + [
292 'user' => [
293 ParamValidator::PARAM_TYPE => 'user',
294 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'id', 'interwiki' ],
295 UserDef::PARAM_RETURN_OBJECT => true,
296 ],
297 'namespace' => [
298 ParamValidator::PARAM_ISMULTI => true,
299 ParamValidator::PARAM_TYPE => 'namespace',
300 ParamValidator::PARAM_DEFAULT => null,
301 ],
302 'start' => [
303 ParamValidator::PARAM_TYPE => 'timestamp',
304 ],
305 'end' => [
306 ParamValidator::PARAM_TYPE => 'timestamp',
307 ],
308 'dir' => [
309 ParamValidator::PARAM_TYPE => [
310 'newer',
311 'older'
312 ],
313 ParamValidator::PARAM_DEFAULT => 'older',
314 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
316 'newer' => 'api-help-paramvalue-direction-newer',
317 'older' => 'api-help-paramvalue-direction-older',
318 ],
319 ],
320 'excludeuser' => [
321 ParamValidator::PARAM_TYPE => 'user',
322 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'id', 'interwiki' ],
323 UserDef::PARAM_RETURN_OBJECT => true,
324 ],
325 'continue' => [
326 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
327 ],
328 'generatetitles' => [
329 ParamValidator::PARAM_DEFAULT => false,
330 ],
331 ];
332
333 if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
334 $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
335 'api-help-param-limited-in-miser-mode',
336 ];
337 }
338
339 return $ret;
340 }
341
342 protected function getExamplesMessages() {
343 return [
344 'action=query&list=allrevisions&arvuser=Example&arvlimit=50'
345 => 'apihelp-query+allrevisions-example-user',
346 'action=query&list=allrevisions&arvdir=newer&arvlimit=50'
347 => 'apihelp-query+allrevisions-example-ns-any',
348 ];
349 }
350
351 public function getHelpUrls() {
352 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allrevisions';
353 }
354}
getAuthority()
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition ApiBase.php:894
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition ApiBase.php:173
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition ApiBase.php:1649
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition ApiBase.php:204
requireMaxOneParameter( $params,... $required)
Die if more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:946
getResult()
Get the result object.
Definition ApiBase.php:637
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:773
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:166
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:506
getContinuationManager()
Definition ApiBase.php:671
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, ContentRenderer $contentRenderer, ContentTransformer $contentTransformer, CommentFormatter $commentFormatter)
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.
extractRevisionInfo(RevisionRecord $revision, $row)
Extract information from the RevisionRecord.
This is the main query class.
Definition ApiQuery.php:42
static makeTagSummarySubquery( $tables)
Make the tag summary subquery based on the given tables and return it.
This is the main service interface for converting single-line comments from various DB comment fields...
A class containing constants representing the names of configuration variables.
Type definition for user types.
Definition UserDef.php:27
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.
Represents a title within MediaWiki.
Definition Title.php:82
This is not intended to be a long-term part of MediaWiki; it will be deprecated and removed once acto...
This is a utility class for dealing with namespaces that encodes all the "magic" behaviors of them ba...
Service for formatting and validating API parameters.