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 $revisionStore = MediaWikiServices
::getInstance()->getRevisionStore();
47 $result = $this->getResult();
49 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
51 // Namespace check is likely to be desired, but can't be done
52 // efficiently in SQL.
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'];
63 $this->addWhere( [ 'page_namespace' => $params['namespace'] ] );
68 if ( $resultPageSet === null ) {
69 $this->parseParameters( $params );
70 $revQuery = $revisionStore->getQueryInfo(
71 $this->fetchContent ?
[ 'page', 'text' ] : [ 'page' ]
73 $this->addTables( $revQuery['tables'] );
74 $this->addFields( $revQuery['fields'] );
75 $this->addJoinConds( $revQuery['joins'] );
77 // Review this depeneding on the outcome of T113901
78 $this->addOption( 'STRAIGHT_JOIN' );
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' ] );
87 if ( $needPageTable ) {
88 $this->addTables( 'page' );
90 [ 'page' => [ 'INNER JOIN', [ 'rev_page = page_id' ] ] ]
92 $this->addFieldsIf( [ 'page_namespace' ], (bool)$miser_ns );
94 // Review this depeneding on the outcome of T113901
95 $this->addOption( 'STRAIGHT_JOIN' );
99 $dir = $params['dir'];
100 $this->addTimestampWhereRange( 'rev_timestamp', $dir, $params['start'], $params['end'] );
102 if ( $this->fld_tags
) {
103 $this->addTables( 'tag_summary' );
105 [ 'tag_summary' => [ 'LEFT JOIN', [ 'rev_id=ts_rev_id' ] ] ]
107 $this->addFields( 'ts_tags' );
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'] . ')' );
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
;
134 $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
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)" );
150 $this->addOption( 'LIMIT', $this->limit +
1 );
152 $sort = ( $dir == 'newer' ?
'' : ' DESC' );
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 );
161 $res = $this->select( __METHOD__
, [], $hookData );
162 $pageMap = []; // Maps rev_page to array index
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"
174 if ( ++
$count > $this->limit
) {
176 $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" );
180 // Miser mode namespace check
181 if ( $miser_ns !== null && !in_array( $row->page_namespace
, $miser_ns ) ) {
185 if ( $resultPageSet !== null ) {
186 if ( $params['generatetitles'] ) {
187 $generated[$row->rev_page
] = $row->rev_page
;
189 $generated[] = $row->rev_id
;
192 $revision = $revisionStore->newRevisionFromRow( $row );
193 $rev = $this->extractRevisionInfo( $revision, $row );
195 if ( !isset( $pageMap[$row->rev_page
] ) ) {
196 $index = $nextIndex++
;
197 $pageMap[$row->rev_page
] = $index;
198 $title = Title
::newFromLinkTarget( $revision->getPageAsLinkTarget() );
200 'pageid' => $title->getArticleID(),
201 'revisions' => [ $rev ],
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 );
208 $index = $pageMap[$row->rev_page
];
209 $fit = $this->processRow( $row, $rev, $hookData ) &&
210 $result->addValue( [ 'query', $this->getModuleName(), $index, 'revisions' ], null, $rev );
213 $this->setContinueEnumParameter( 'continue', "$row->rev_timestamp|$row->rev_id" );
219 if ( $resultPageSet !== null ) {
220 if ( $params['generatetitles'] ) {
221 $resultPageSet->populateFromPageIDs( $generated );
223 $resultPageSet->populateFromRevisionIDs( $generated );
226 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
230 public function getAllowedParams() {
231 $ret = parent
::getAllowedParams() +
[
233 ApiBase
::PARAM_TYPE
=> 'user',
236 ApiBase
::PARAM_ISMULTI
=> true,
237 ApiBase
::PARAM_TYPE
=> 'namespace',
238 ApiBase
::PARAM_DFLT
=> null,
241 ApiBase
::PARAM_TYPE
=> 'timestamp',
244 ApiBase
::PARAM_TYPE
=> 'timestamp',
247 ApiBase
::PARAM_TYPE
=> [
251 ApiBase
::PARAM_DFLT
=> 'older',
252 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
255 ApiBase
::PARAM_TYPE
=> 'user',
258 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
260 'generatetitles' => [
261 ApiBase
::PARAM_DFLT
=> false,
265 if ( $this->getConfig()->get( 'MiserMode' ) ) {
266 $ret['namespace'][ApiBase
::PARAM_HELP_MSG_APPEND
] = [
267 'api-help-param-limited-in-miser-mode',
274 protected function getExamplesMessages() {
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',
283 public function getHelpUrls() {
284 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allrevisions';