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