3 * Copyright © 2014 Wikimedia Foundation and contributors
5 * Heavily based on ApiQueryDeletedrevs,
6 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
26 use MediaWiki\MediaWikiServices
;
27 use MediaWiki\Storage\NameTableAccessException
;
28 use MediaWiki\Storage\RevisionRecord
;
31 * Query module to enumerate deleted revisions for pages.
35 class ApiQueryDeletedRevisions
extends ApiQueryRevisionsBase
{
37 public function __construct( ApiQuery
$query, $moduleName ) {
38 parent
::__construct( $query, $moduleName, 'drv' );
41 protected function run( ApiPageSet
$resultPageSet = null ) {
42 global $wgChangeTagsSchemaMigrationStage;
44 $user = $this->getUser();
45 // Before doing anything at all, let's check permissions
46 $this->checkUserRightsAny( 'deletedhistory' );
48 $pageSet = $this->getPageSet();
49 $pageMap = $pageSet->getGoodAndMissingTitlesByNamespace();
50 $pageCount = count( $pageSet->getGoodAndMissingTitles() );
51 $revCount = $pageSet->getRevisionCount();
52 if ( $revCount === 0 && $pageCount === 0 ) {
56 if ( $revCount !== 0 && count( $pageSet->getDeletedRevisionIDs() ) === 0 ) {
57 // Nothing to do, revisions were supplied but none are deleted
61 $params = $this->extractRequestParams( false );
64 $revisionStore = MediaWikiServices
::getInstance()->getRevisionStore();
66 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
68 if ( $resultPageSet === null ) {
69 $this->parseParameters( $params );
70 $arQuery = $revisionStore->getArchiveQueryInfo();
71 $this->addTables( $arQuery['tables'] );
72 $this->addFields( $arQuery['fields'] );
73 $this->addJoinConds( $arQuery['joins'] );
74 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
76 $this->limit
= $this->getParameter( 'limit' ) ?
: 10;
77 $this->addTables( 'archive' );
78 $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
81 if ( $this->fld_tags
) {
82 $this->addTables( 'tag_summary' );
84 [ 'tag_summary' => [ 'LEFT JOIN', [ 'ar_rev_id=ts_rev_id' ] ] ]
86 $this->addFields( 'ts_tags' );
89 if ( !is_null( $params['tag'] ) ) {
90 $this->addTables( 'change_tag' );
92 [ 'change_tag' => [ 'INNER JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
94 if ( $wgChangeTagsSchemaMigrationStage > MIGRATION_WRITE_BOTH
) {
95 $changeTagDefStore = MediaWikiServices
::getInstance()->getChangeTagDefStore();
97 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
98 } catch ( NameTableAccessException
$exception ) {
100 $this->addWhere( '1=0' );
103 $this->addWhereFld( 'ct_tag', $params['tag'] );
107 if ( $this->fetchContent
) {
108 $this->addTables( 'text' );
110 [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
112 $this->addFields( [ 'old_text', 'old_flags' ] );
114 // This also means stricter restrictions
115 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
118 $dir = $params['dir'];
120 if ( $revCount !== 0 ) {
122 'ar_rev_id' => array_keys( $pageSet->getDeletedRevisionIDs() )
125 // We need a custom WHERE clause that matches all titles.
126 $lb = new LinkBatch( $pageSet->getGoodAndMissingTitles() );
127 $where = $lb->constructSet( 'ar', $db );
128 $this->addWhere( $where );
131 if ( !is_null( $params['user'] ) ) {
132 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
133 $actorQuery = ActorMigration
::newMigration()
134 ->getWhere( $db, 'ar_user', User
::newFromName( $params['user'], false ), false );
135 $this->addTables( $actorQuery['tables'] );
136 $this->addJoinConds( $actorQuery['joins'] );
137 $this->addWhere( $actorQuery['conds'] );
138 } elseif ( !is_null( $params['excludeuser'] ) ) {
139 // Here there's no chance of using ar_usertext_timestamp.
140 $actorQuery = ActorMigration
::newMigration()
141 ->getWhere( $db, 'ar_user', User
::newFromName( $params['excludeuser'], false ) );
142 $this->addTables( $actorQuery['tables'] );
143 $this->addJoinConds( $actorQuery['joins'] );
144 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
147 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
148 // Paranoia: avoid brute force searches (T19342)
149 // (shouldn't be able to get here without 'deletedhistory', but
150 // check it again just in case)
151 if ( !$user->isAllowed( 'deletedhistory' ) ) {
152 $bitmask = RevisionRecord
::DELETED_USER
;
153 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
154 $bitmask = RevisionRecord
::DELETED_USER | RevisionRecord
::DELETED_RESTRICTED
;
159 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
163 if ( !is_null( $params['continue'] ) ) {
164 $cont = explode( '|', $params['continue'] );
165 $op = ( $dir == 'newer' ?
'>' : '<' );
166 if ( $revCount !== 0 ) {
167 $this->dieContinueUsageIf( count( $cont ) != 2 );
168 $rev = intval( $cont[0] );
169 $this->dieContinueUsageIf( strval( $rev ) !== $cont[0] );
170 $ar_id = (int)$cont[1];
171 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
172 $this->addWhere( "ar_rev_id $op $rev OR " .
173 "(ar_rev_id = $rev AND " .
174 "ar_id $op= $ar_id)" );
176 $this->dieContinueUsageIf( count( $cont ) != 4 );
177 $ns = intval( $cont[0] );
178 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
179 $title = $db->addQuotes( $cont[1] );
180 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
181 $ar_id = (int)$cont[3];
182 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
183 $this->addWhere( "ar_namespace $op $ns OR " .
184 "(ar_namespace = $ns AND " .
185 "(ar_title $op $title OR " .
186 "(ar_title = $title AND " .
187 "(ar_timestamp $op $ts OR " .
188 "(ar_timestamp = $ts AND " .
189 "ar_id $op= $ar_id)))))" );
193 $this->addOption( 'LIMIT', $this->limit +
1 );
195 if ( $revCount !== 0 ) {
196 // Sort by ar_rev_id when querying by ar_rev_id
197 $this->addWhereRange( 'ar_rev_id', $dir, null, null );
199 // Sort by ns and title in the same order as timestamp for efficiency
200 // But only when not already unique in the query
201 if ( count( $pageMap ) > 1 ) {
202 $this->addWhereRange( 'ar_namespace', $dir, null, null );
204 $oneTitle = key( reset( $pageMap ) );
205 foreach ( $pageMap as $pages ) {
206 if ( count( $pages ) > 1 ||
key( $pages ) !== $oneTitle ) {
207 $this->addWhereRange( 'ar_title', $dir, null, null );
211 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
213 // Include in ORDER BY for uniqueness
214 $this->addWhereRange( 'ar_id', $dir, null, null );
216 $res = $this->select( __METHOD__
);
219 foreach ( $res as $row ) {
220 if ( ++
$count > $this->limit
) {
222 $this->setContinueEnumParameter( 'continue',
224 ?
"$row->ar_rev_id|$row->ar_id"
225 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
230 if ( $resultPageSet !== null ) {
231 $generated[] = $row->ar_rev_id
;
233 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
235 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
236 $converted = $pageSet->getConvertedTitles();
237 if ( $title && isset( $converted[$title->getPrefixedText()] ) ) {
238 $title = Title
::newFromText( $converted[$title->getPrefixedText()] );
239 if ( $title && isset( $pageMap[$title->getNamespace()][$title->getDBkey()] ) ) {
240 $pageMap[$row->ar_namespace
][$row->ar_title
] =
241 $pageMap[$title->getNamespace()][$title->getDBkey()];
245 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
248 "Found row in archive (ar_id={$row->ar_id}) that didn't get processed by ApiPageSet"
252 $fit = $this->addPageSubItem(
253 $pageMap[$row->ar_namespace
][$row->ar_title
],
254 $this->extractRevisionInfo( $revisionStore->newRevisionFromArchiveRow( $row ), $row ),
258 $this->setContinueEnumParameter( 'continue',
260 ?
"$row->ar_rev_id|$row->ar_id"
261 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
268 if ( $resultPageSet !== null ) {
269 $resultPageSet->populateFromRevisionIDs( $generated );
273 public function getAllowedParams() {
274 return parent
::getAllowedParams() +
[
276 ApiBase
::PARAM_TYPE
=> 'timestamp',
279 ApiBase
::PARAM_TYPE
=> 'timestamp',
282 ApiBase
::PARAM_TYPE
=> [
286 ApiBase
::PARAM_DFLT
=> 'older',
287 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
291 ApiBase
::PARAM_TYPE
=> 'user'
294 ApiBase
::PARAM_TYPE
=> 'user'
297 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
302 protected function getExamplesMessages() {
304 'action=query&prop=deletedrevisions&titles=Main%20Page|Talk:Main%20Page&' .
305 'drvslots=*&drvprop=user|comment|content'
306 => 'apihelp-query+deletedrevisions-example-titles',
307 'action=query&prop=deletedrevisions&revids=123456'
308 => 'apihelp-query+deletedrevisions-example-revids',
312 public function getHelpUrls() {
313 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevisions';