3 * Copyright © 2015 Wikimedia Foundation and contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\MediaWikiServices
;
24 use MediaWiki\Revision\RevisionRecord
;
27 * Query module to enumerate all revisions.
32 class ApiQueryAllRevisions
extends ApiQueryRevisionsBase
{
34 public function __construct( ApiQuery
$query, $moduleName ) {
35 parent
::__construct( $query, $moduleName, 'arv' );
39 * @param ApiPageSet|null $resultPageSet
42 protected function run( ApiPageSet
$resultPageSet = null ) {
44 $params = $this->extractRequestParams( false );
45 $services = MediaWikiServices
::getInstance();
46 $revisionStore = $services->getRevisionStore();
48 $result = $this->getResult();
50 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
52 $tsField = 'rev_timestamp';
54 $pageField = 'rev_page';
55 if ( $params['user'] !== null ) {
56 // The query is probably best done using the actor_timestamp index on
57 // revision_actor_temp. Use the denormalized fields from that table.
58 $tsField = 'revactor_timestamp';
59 $idField = 'revactor_rev';
60 $pageField = 'revactor_page';
63 // Namespace check is likely to be desired, but can't be done
64 // efficiently in SQL.
66 $needPageTable = false;
67 if ( $params['namespace'] !== null ) {
68 $params['namespace'] = array_unique( $params['namespace'] );
69 sort( $params['namespace'] );
70 if ( $params['namespace'] != $services->getNamespaceInfo()->getValidNamespaces() ) {
71 $needPageTable = true;
72 if ( $this->getConfig()->get( 'MiserMode' ) ) {
73 $miser_ns = $params['namespace'];
75 $this->addWhere( [ 'page_namespace' => $params['namespace'] ] );
80 if ( $resultPageSet === null ) {
81 $this->parseParameters( $params );
82 $revQuery = $revisionStore->getQueryInfo( [ 'page' ] );
84 $this->limit
= $this->getParameter( 'limit' ) ?
: 10;
86 'tables' => [ 'revision' ],
87 'fields' => [ 'rev_timestamp', 'rev_id' ],
91 if ( $params['generatetitles'] ) {
92 $revQuery['fields'][] = 'rev_page';
95 if ( $params['user'] !== null ||
$params['excludeuser'] !== null ) {
96 $actorQuery = ActorMigration
::newMigration()->getJoin( 'rev_user' );
97 $revQuery['tables'] +
= $actorQuery['tables'];
98 $revQuery['joins'] +
= $actorQuery['joins'];
101 if ( $needPageTable ) {
102 $revQuery['tables'][] = 'page';
103 $revQuery['joins']['page'] = [ 'JOIN', [ "$pageField = page_id" ] ];
104 if ( (bool)$miser_ns ) {
105 $revQuery['fields'][] = 'page_namespace';
110 // If we're going to be using actor_timestamp, we need to swap the order of `revision`
111 // and `revision_actor_temp` in the query (for the straight join) and adjust some field aliases.
112 if ( $idField !== 'rev_id' && isset( $revQuery['tables']['temp_rev_user'] ) ) {
113 $aliasFields = [ 'rev_id' => $idField, 'rev_timestamp' => $tsField, 'rev_page' => $pageField ];
114 $revQuery['fields'] = array_merge(
116 array_diff( $revQuery['fields'], array_keys( $aliasFields ) )
118 unset( $revQuery['tables']['temp_rev_user'] );
119 $revQuery['tables'] = array_merge(
120 [ 'temp_rev_user' => 'revision_actor_temp' ],
123 $revQuery['joins']['revision'] = $revQuery['joins']['temp_rev_user'];
124 unset( $revQuery['joins']['temp_rev_user'] );
127 $this->addTables( $revQuery['tables'] );
128 $this->addFields( $revQuery['fields'] );
129 $this->addJoinConds( $revQuery['joins'] );
131 // Seems to be needed to avoid a planner bug (T113901)
132 $this->addOption( 'STRAIGHT_JOIN' );
134 $dir = $params['dir'];
135 $this->addTimestampWhereRange( $tsField, $dir, $params['start'], $params['end'] );
137 if ( $this->fld_tags
) {
138 $this->addFields( [ 'ts_tags' => ChangeTags
::makeTagSummarySubquery( 'revision' ) ] );
141 if ( $params['user'] !== null ) {
142 $actorQuery = ActorMigration
::newMigration()
143 ->getWhere( $db, 'rev_user', User
::newFromName( $params['user'], false ) );
144 $this->addWhere( $actorQuery['conds'] );
145 } elseif ( $params['excludeuser'] !== null ) {
146 $actorQuery = ActorMigration
::newMigration()
147 ->getWhere( $db, 'rev_user', User
::newFromName( $params['excludeuser'], false ) );
148 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
151 if ( $params['user'] !== null ||
$params['excludeuser'] !== null ) {
152 // Paranoia: avoid brute force searches (T19342)
153 if ( !$this->getPermissionManager()->userHasRight( $this->getUser(), 'deletedhistory' ) ) {
154 $bitmask = RevisionRecord
::DELETED_USER
;
155 } elseif ( !$this->getPermissionManager()
156 ->userHasAnyRight( $this->getUser(), 'suppressrevision', 'viewsuppressed' )
158 $bitmask = RevisionRecord
::DELETED_USER | RevisionRecord
::DELETED_RESTRICTED
;
163 $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
167 if ( $params['continue'] !== null ) {
168 $op = ( $dir == 'newer' ?
'>' : '<' );
169 $cont = explode( '|', $params['continue'] );
170 $this->dieContinueUsageIf( count( $cont ) != 2 );
171 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
172 $rev_id = (int)$cont[1];
173 $this->dieContinueUsageIf( strval( $rev_id ) !== $cont[1] );
174 $this->addWhere( "$tsField $op $ts OR " .
175 "($tsField = $ts AND " .
176 "$idField $op= $rev_id)" );
179 $this->addOption( 'LIMIT', $this->limit +
1 );
181 $sort = ( $dir == 'newer' ?
'' : ' DESC' );
183 // Targeting index rev_timestamp, user_timestamp, usertext_timestamp, or actor_timestamp.
184 // But 'user' is always constant for the latter three, so it doesn't matter here.
185 $orderby[] = "rev_timestamp $sort";
186 $orderby[] = "rev_id $sort";
187 $this->addOption( 'ORDER BY', $orderby );
190 $res = $this->select( __METHOD__
, [], $hookData );
191 $pageMap = []; // Maps rev_page to array index
195 foreach ( $res as $row ) {
196 if ( $count === 0 && $resultPageSet !== null ) {
197 // Set the non-continue since the list of all revisions is
198 // prone to having entries added at the start frequently.
199 $this->getContinuationManager()->addGeneratorNonContinueParam(
200 $this, 'continue', "$row->rev_timestamp|$row->rev_id"
203 if ( ++
$count > $this->limit
) {
205 $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" );
209 // Miser mode namespace check
210 if ( $miser_ns !== null && !in_array( $row->page_namespace
, $miser_ns ) ) {
214 if ( $resultPageSet !== null ) {
215 if ( $params['generatetitles'] ) {
216 $generated[$row->rev_page
] = $row->rev_page
;
218 $generated[] = $row->rev_id
;
221 $revision = $revisionStore->newRevisionFromRow( $row );
222 $rev = $this->extractRevisionInfo( $revision, $row );
224 if ( !isset( $pageMap[$row->rev_page
] ) ) {
225 $index = $nextIndex++
;
226 $pageMap[$row->rev_page
] = $index;
227 $title = Title
::newFromLinkTarget( $revision->getPageAsLinkTarget() );
229 'pageid' => $title->getArticleID(),
230 'revisions' => [ $rev ],
232 ApiResult
::setIndexedTagName( $a['revisions'], 'rev' );
233 ApiQueryBase
::addTitleInfo( $a, $title );
234 $fit = $this->processRow( $row, $a['revisions'][0], $hookData ) &&
235 $result->addValue( [ 'query', $this->getModuleName() ], $index, $a );
237 $index = $pageMap[$row->rev_page
];
238 $fit = $this->processRow( $row, $rev, $hookData ) &&
239 $result->addValue( [ 'query', $this->getModuleName(), $index, 'revisions' ], null, $rev );
242 $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" );
248 if ( $resultPageSet !== null ) {
249 if ( $params['generatetitles'] ) {
250 $resultPageSet->populateFromPageIDs( $generated );
252 $resultPageSet->populateFromRevisionIDs( $generated );
255 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
259 public function getAllowedParams() {
260 $ret = parent
::getAllowedParams() +
[
262 ApiBase
::PARAM_TYPE
=> 'user',
265 ApiBase
::PARAM_ISMULTI
=> true,
266 ApiBase
::PARAM_TYPE
=> 'namespace',
267 ApiBase
::PARAM_DFLT
=> null,
270 ApiBase
::PARAM_TYPE
=> 'timestamp',
273 ApiBase
::PARAM_TYPE
=> 'timestamp',
276 ApiBase
::PARAM_TYPE
=> [
280 ApiBase
::PARAM_DFLT
=> 'older',
281 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
284 ApiBase
::PARAM_TYPE
=> 'user',
287 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
289 'generatetitles' => [
290 ApiBase
::PARAM_DFLT
=> false,
294 if ( $this->getConfig()->get( 'MiserMode' ) ) {
295 $ret['namespace'][ApiBase
::PARAM_HELP_MSG_APPEND
] = [
296 'api-help-param-limited-in-miser-mode',
303 protected function getExamplesMessages() {
305 'action=query&list=allrevisions&arvuser=Example&arvlimit=50'
306 => 'apihelp-query+allrevisions-example-user',
307 'action=query&list=allrevisions&arvdir=newer&arvlimit=50'
308 => 'apihelp-query+allrevisions-example-ns-main',
312 public function getHelpUrls() {
313 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allrevisions';