MediaWiki REL1_32
ApiQueryAllRevisions.php
Go to the documentation of this file.
1<?php
25
33
34 public function __construct( ApiQuery $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'arv' );
36 }
37
42 protected function run( ApiPageSet $resultPageSet = null ) {
43 $db = $this->getDB();
44 $params = $this->extractRequestParams( false );
45 $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
46
47 $result = $this->getResult();
48
49 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
50
51 // Namespace check is likely to be desired, but can't be done
52 // efficiently in SQL.
53 $miser_ns = null;
54 $needPageTable = false;
55 if ( $params['namespace'] !== null ) {
56 $params['namespace'] = array_unique( $params['namespace'] );
57 sort( $params['namespace'] );
58 if ( $params['namespace'] != MWNamespace::getValidNamespaces() ) {
59 $needPageTable = true;
60 if ( $this->getConfig()->get( 'MiserMode' ) ) {
61 $miser_ns = $params['namespace'];
62 } else {
63 $this->addWhere( [ 'page_namespace' => $params['namespace'] ] );
64 }
65 }
66 }
67
68 if ( $resultPageSet === null ) {
69 $this->parseParameters( $params );
70 $revQuery = $revisionStore->getQueryInfo(
71 $this->fetchContent ? [ 'page', 'text' ] : [ 'page' ]
72 );
73 $this->addTables( $revQuery['tables'] );
74 $this->addFields( $revQuery['fields'] );
75 $this->addJoinConds( $revQuery['joins'] );
76
77 // Review this depeneding on the outcome of T113901
78 $this->addOption( 'STRAIGHT_JOIN' );
79 } else {
80 $this->limit = $this->getParameter( 'limit' ) ?: 10;
81 $this->addTables( 'revision' );
82 $this->addFields( [ 'rev_timestamp', 'rev_id' ] );
83 if ( $params['generatetitles'] ) {
84 $this->addFields( [ 'rev_page' ] );
85 }
86
87 if ( $needPageTable ) {
88 $this->addTables( 'page' );
89 $this->addJoinConds(
90 [ 'page' => [ 'INNER JOIN', [ 'rev_page = page_id' ] ] ]
91 );
92 $this->addFieldsIf( [ 'page_namespace' ], (bool)$miser_ns );
93
94 // Review this depeneding on the outcome of T113901
95 $this->addOption( 'STRAIGHT_JOIN' );
96 }
97 }
98
99 $dir = $params['dir'];
100 $this->addTimestampWhereRange( 'rev_timestamp', $dir, $params['start'], $params['end'] );
101
102 if ( $this->fld_tags ) {
103 $this->addTables( 'tag_summary' );
104 $this->addJoinConds(
105 [ 'tag_summary' => [ 'LEFT JOIN', [ 'rev_id=ts_rev_id' ] ] ]
106 );
107 $this->addFields( 'ts_tags' );
108 }
109
110 if ( $params['user'] !== null ) {
111 $actorQuery = ActorMigration::newMigration()
112 ->getWhere( $db, 'rev_user', User::newFromName( $params['user'], false ) );
113 $this->addTables( $actorQuery['tables'] );
114 $this->addJoinConds( $actorQuery['joins'] );
115 $this->addWhere( $actorQuery['conds'] );
116 } elseif ( $params['excludeuser'] !== null ) {
117 $actorQuery = ActorMigration::newMigration()
118 ->getWhere( $db, 'rev_user', User::newFromName( $params['excludeuser'], false ) );
119 $this->addTables( $actorQuery['tables'] );
120 $this->addJoinConds( $actorQuery['joins'] );
121 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
122 }
123
124 if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
125 // Paranoia: avoid brute force searches (T19342)
126 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
127 $bitmask = RevisionRecord::DELETED_USER;
128 } elseif ( !$this->getUser()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
129 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
130 } else {
131 $bitmask = 0;
132 }
133 if ( $bitmask ) {
134 $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
135 }
136 }
137
138 if ( $params['continue'] !== null ) {
139 $op = ( $dir == 'newer' ? '>' : '<' );
140 $cont = explode( '|', $params['continue'] );
141 $this->dieContinueUsageIf( count( $cont ) != 2 );
142 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
143 $rev_id = (int)$cont[1];
144 $this->dieContinueUsageIf( strval( $rev_id ) !== $cont[1] );
145 $this->addWhere( "rev_timestamp $op $ts OR " .
146 "(rev_timestamp = $ts AND " .
147 "rev_id $op= $rev_id)" );
148 }
149
150 $this->addOption( 'LIMIT', $this->limit + 1 );
151
152 $sort = ( $dir == 'newer' ? '' : ' DESC' );
153 $orderby = [];
154 // Targeting index rev_timestamp, user_timestamp, or usertext_timestamp
155 // But 'user' is always constant for the latter two, so it doesn't matter here.
156 $orderby[] = "rev_timestamp $sort";
157 $orderby[] = "rev_id $sort";
158 $this->addOption( 'ORDER BY', $orderby );
159
160 $hookData = [];
161 $res = $this->select( __METHOD__, [], $hookData );
162 $pageMap = []; // Maps rev_page to array index
163 $count = 0;
164 $nextIndex = 0;
165 $generated = [];
166 foreach ( $res as $row ) {
167 if ( $count === 0 && $resultPageSet !== null ) {
168 // Set the non-continue since the list of all revisions is
169 // prone to having entries added at the start frequently.
170 $this->getContinuationManager()->addGeneratorNonContinueParam(
171 $this, 'continue', "$row->rev_timestamp|$row->rev_id"
172 );
173 }
174 if ( ++$count > $this->limit ) {
175 // We've had enough
176 $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" );
177 break;
178 }
179
180 // Miser mode namespace check
181 if ( $miser_ns !== null && !in_array( $row->page_namespace, $miser_ns ) ) {
182 continue;
183 }
184
185 if ( $resultPageSet !== null ) {
186 if ( $params['generatetitles'] ) {
187 $generated[$row->rev_page] = $row->rev_page;
188 } else {
189 $generated[] = $row->rev_id;
190 }
191 } else {
192 $revision = $revisionStore->newRevisionFromRow( $row );
193 $rev = $this->extractRevisionInfo( $revision, $row );
194
195 if ( !isset( $pageMap[$row->rev_page] ) ) {
196 $index = $nextIndex++;
197 $pageMap[$row->rev_page] = $index;
198 $title = Title::newFromLinkTarget( $revision->getPageAsLinkTarget() );
199 $a = [
200 'pageid' => $title->getArticleID(),
201 'revisions' => [ $rev ],
202 ];
203 ApiResult::setIndexedTagName( $a['revisions'], 'rev' );
204 ApiQueryBase::addTitleInfo( $a, $title );
205 $fit = $this->processRow( $row, $a['revisions'][0], $hookData ) &&
206 $result->addValue( [ 'query', $this->getModuleName() ], $index, $a );
207 } else {
208 $index = $pageMap[$row->rev_page];
209 $fit = $this->processRow( $row, $rev, $hookData ) &&
210 $result->addValue( [ 'query', $this->getModuleName(), $index, 'revisions' ], null, $rev );
211 }
212 if ( !$fit ) {
213 $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" );
214 break;
215 }
216 }
217 }
218
219 if ( $resultPageSet !== null ) {
220 if ( $params['generatetitles'] ) {
221 $resultPageSet->populateFromPageIDs( $generated );
222 } else {
223 $resultPageSet->populateFromRevisionIDs( $generated );
224 }
225 } else {
226 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
227 }
228 }
229
230 public function getAllowedParams() {
231 $ret = parent::getAllowedParams() + [
232 'user' => [
233 ApiBase::PARAM_TYPE => 'user',
234 ],
235 'namespace' => [
237 ApiBase::PARAM_TYPE => 'namespace',
238 ApiBase::PARAM_DFLT => null,
239 ],
240 'start' => [
241 ApiBase::PARAM_TYPE => 'timestamp',
242 ],
243 'end' => [
244 ApiBase::PARAM_TYPE => 'timestamp',
245 ],
246 'dir' => [
248 'newer',
249 'older'
250 ],
251 ApiBase::PARAM_DFLT => 'older',
252 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
253 ],
254 'excludeuser' => [
255 ApiBase::PARAM_TYPE => 'user',
256 ],
257 'continue' => [
258 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
259 ],
260 'generatetitles' => [
261 ApiBase::PARAM_DFLT => false,
262 ],
263 ];
264
265 if ( $this->getConfig()->get( 'MiserMode' ) ) {
266 $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
267 'api-help-param-limited-in-miser-mode',
268 ];
269 }
270
271 return $ret;
272 }
273
274 protected function getExamplesMessages() {
275 return [
276 'action=query&list=allrevisions&arvuser=Example&arvlimit=50'
277 => 'apihelp-query+allrevisions-example-user',
278 'action=query&list=allrevisions&arvdir=newer&arvlimit=50'
279 => 'apihelp-query+allrevisions-example-ns-main',
280 ];
281 }
282
283 public function getHelpUrls() {
284 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allrevisions';
285 }
286}
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:2155
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:87
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition ApiBase.php:48
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition ApiBase.php:131
getResult()
Get the result object.
Definition ApiBase.php:659
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:770
requireMaxOneParameter( $params, $required)
Die if more than one of a certain set of parameters is set and not false.
Definition ApiBase.php:939
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition ApiBase.php:124
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:539
getContinuationManager()
Get the continuation manager.
Definition ApiBase.php:699
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition ApiBase.php:51
This class contains a list of pages that the client has requested.
Query module to enumerate all revisions.
getHelpUrls()
Return links to more detailed help pages about the module.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
__construct(ApiQuery $query, $moduleName)
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)
addFieldsIf( $value, $condition)
Same as addFields(), but add the fields only if a condition is met.
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:36
static setIndexedTagName(array &$arr, $tag)
Set the tag name for numeric-keyed values in XML format.
MediaWikiServices is the service locator for the application scope of MediaWiki.
Page revision base class.
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition User.php:592
We use the convention $dbr for read and $dbw for write to help you keep track of whether the database object is a the world will explode Or to be a subsequent write query which succeeded on the master may fail when replicated to the slave due to a unique key collision Replication on the slave will stop and it may take hours to repair the database and get it back online Setting read_only in my cnf on the slave will avoid this but given the dire we prefer to have as many checks as possible We provide a but the wrapper functions like select() and insert() are usually more convenient. They take care of things like table prefixes and escaping for you. If you really need to make your own SQL
$res
Definition database.txt:21
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:2054
null for the local wiki Added should default to null in handler for backwards compatibility add a value to it if you want to add a cookie that have to vary cache options can modify $query
Definition hooks.txt:1656
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition hooks.txt:1818
$sort
$params