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\Revision\RevisionRecord
;
28 use MediaWiki\Storage\NameTableAccessException
;
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 $user = $this->getUser();
44 $pageSet = $this->getPageSet();
45 $pageMap = $pageSet->getGoodAndMissingTitlesByNamespace();
46 $pageCount = count( $pageSet->getGoodAndMissingTitles() );
47 $revCount = $pageSet->getRevisionCount();
48 if ( $revCount === 0 && $pageCount === 0 ) {
52 if ( $revCount !== 0 && count( $pageSet->getDeletedRevisionIDs() ) === 0 ) {
53 // Nothing to do, revisions were supplied but none are deleted
57 $params = $this->extractRequestParams( false );
60 $revisionStore = MediaWikiServices
::getInstance()->getRevisionStore();
62 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
64 if ( $resultPageSet === null ) {
65 $this->parseParameters( $params );
66 $arQuery = $revisionStore->getArchiveQueryInfo();
67 $this->addTables( $arQuery['tables'] );
68 $this->addFields( $arQuery['fields'] );
69 $this->addJoinConds( $arQuery['joins'] );
70 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
72 $this->limit
= $this->getParameter( 'limit' ) ?
: 10;
73 $this->addTables( 'archive' );
74 $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
77 if ( $this->fld_tags
) {
78 $this->addFields( [ 'ts_tags' => ChangeTags
::makeTagSummarySubquery( 'archive' ) ] );
81 if ( !is_null( $params['tag'] ) ) {
82 $this->addTables( 'change_tag' );
84 [ 'change_tag' => [ 'JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
86 $changeTagDefStore = MediaWikiServices
::getInstance()->getChangeTagDefStore();
88 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $params['tag'] ) );
89 } catch ( NameTableAccessException
$exception ) {
91 $this->addWhere( '1=0' );
95 // This means stricter restrictions
96 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) &&
97 !$this->getPermissionManager()->userHasRight( $user, 'deletedhistory' )
99 $this->dieWithError( 'apierror-cantview-deleted-comment', 'permissiondenied' );
101 if ( $this->fetchContent
&&
102 !$this->getPermissionManager()->userHasAnyRight( $user, 'deletedtext', 'undelete' )
104 $this->dieWithError( 'apierror-cantview-deleted-revision-content', 'permissiondenied' );
107 $dir = $params['dir'];
109 if ( $revCount !== 0 ) {
111 'ar_rev_id' => array_keys( $pageSet->getDeletedRevisionIDs() )
114 // We need a custom WHERE clause that matches all titles.
115 $lb = new LinkBatch( $pageSet->getGoodAndMissingTitles() );
116 $where = $lb->constructSet( 'ar', $db );
117 $this->addWhere( $where );
120 if ( !is_null( $params['user'] ) ) {
121 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
122 $actorQuery = ActorMigration
::newMigration()
123 ->getWhere( $db, 'ar_user', User
::newFromName( $params['user'], false ), false );
124 $this->addTables( $actorQuery['tables'] );
125 $this->addJoinConds( $actorQuery['joins'] );
126 $this->addWhere( $actorQuery['conds'] );
127 } elseif ( !is_null( $params['excludeuser'] ) ) {
128 // Here there's no chance of using ar_usertext_timestamp.
129 $actorQuery = ActorMigration
::newMigration()
130 ->getWhere( $db, 'ar_user', User
::newFromName( $params['excludeuser'], false ) );
131 $this->addTables( $actorQuery['tables'] );
132 $this->addJoinConds( $actorQuery['joins'] );
133 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
136 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
137 // Paranoia: avoid brute force searches (T19342)
138 if ( !$this->getPermissionManager()->userHasRight( $user, 'deletedhistory' ) ) {
139 $bitmask = RevisionRecord
::DELETED_USER
;
140 } elseif ( !$this->getPermissionManager()
141 ->userHasAnyRight( $this->getUser(), 'suppressrevision', 'viewsuppressed' )
143 $bitmask = RevisionRecord
::DELETED_USER | RevisionRecord
::DELETED_RESTRICTED
;
148 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
152 if ( !is_null( $params['continue'] ) ) {
153 $cont = explode( '|', $params['continue'] );
154 $op = ( $dir == 'newer' ?
'>' : '<' );
155 if ( $revCount !== 0 ) {
156 $this->dieContinueUsageIf( count( $cont ) != 2 );
157 $rev = (int)$cont[0];
158 $this->dieContinueUsageIf( strval( $rev ) !== $cont[0] );
159 $ar_id = (int)$cont[1];
160 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
161 $this->addWhere( "ar_rev_id $op $rev OR " .
162 "(ar_rev_id = $rev AND " .
163 "ar_id $op= $ar_id)" );
165 $this->dieContinueUsageIf( count( $cont ) != 4 );
167 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
168 $title = $db->addQuotes( $cont[1] );
169 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
170 $ar_id = (int)$cont[3];
171 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
172 $this->addWhere( "ar_namespace $op $ns OR " .
173 "(ar_namespace = $ns AND " .
174 "(ar_title $op $title OR " .
175 "(ar_title = $title AND " .
176 "(ar_timestamp $op $ts OR " .
177 "(ar_timestamp = $ts AND " .
178 "ar_id $op= $ar_id)))))" );
182 $this->addOption( 'LIMIT', $this->limit +
1 );
184 if ( $revCount !== 0 ) {
185 // Sort by ar_rev_id when querying by ar_rev_id
186 $this->addWhereRange( 'ar_rev_id', $dir, null, null );
188 // Sort by ns and title in the same order as timestamp for efficiency
189 // But only when not already unique in the query
190 if ( count( $pageMap ) > 1 ) {
191 $this->addWhereRange( 'ar_namespace', $dir, null, null );
193 $oneTitle = key( reset( $pageMap ) );
194 foreach ( $pageMap as $pages ) {
195 if ( count( $pages ) > 1 ||
key( $pages ) !== $oneTitle ) {
196 $this->addWhereRange( 'ar_title', $dir, null, null );
200 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
202 // Include in ORDER BY for uniqueness
203 $this->addWhereRange( 'ar_id', $dir, null, null );
205 $res = $this->select( __METHOD__
);
208 foreach ( $res as $row ) {
209 if ( ++
$count > $this->limit
) {
211 $this->setContinueEnumParameter( 'continue',
213 ?
"$row->ar_rev_id|$row->ar_id"
214 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
219 if ( $resultPageSet !== null ) {
220 $generated[] = $row->ar_rev_id
;
222 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
224 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
225 $converted = $pageSet->getConvertedTitles();
226 if ( $title && isset( $converted[$title->getPrefixedText()] ) ) {
227 $title = Title
::newFromText( $converted[$title->getPrefixedText()] );
228 if ( $title && isset( $pageMap[$title->getNamespace()][$title->getDBkey()] ) ) {
229 $pageMap[$row->ar_namespace
][$row->ar_title
] =
230 $pageMap[$title->getNamespace()][$title->getDBkey()];
234 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
237 "Found row in archive (ar_id={$row->ar_id}) that didn't get processed by ApiPageSet"
241 $fit = $this->addPageSubItem(
242 $pageMap[$row->ar_namespace
][$row->ar_title
],
243 $this->extractRevisionInfo( $revisionStore->newRevisionFromArchiveRow( $row ), $row ),
247 $this->setContinueEnumParameter( 'continue',
249 ?
"$row->ar_rev_id|$row->ar_id"
250 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
257 if ( $resultPageSet !== null ) {
258 $resultPageSet->populateFromRevisionIDs( $generated );
262 public function getAllowedParams() {
263 return parent
::getAllowedParams() +
[
265 ApiBase
::PARAM_TYPE
=> 'timestamp',
268 ApiBase
::PARAM_TYPE
=> 'timestamp',
271 ApiBase
::PARAM_TYPE
=> [
275 ApiBase
::PARAM_DFLT
=> 'older',
276 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
280 ApiBase
::PARAM_TYPE
=> 'user'
283 ApiBase
::PARAM_TYPE
=> 'user'
286 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
291 protected function getExamplesMessages() {
293 'action=query&prop=deletedrevisions&titles=Main%20Page|Talk:Main%20Page&' .
294 'drvslots=*&drvprop=user|comment|content'
295 => 'apihelp-query+deletedrevisions-example-titles',
296 'action=query&prop=deletedrevisions&revids=123456'
297 => 'apihelp-query+deletedrevisions-example-revids',
301 public function getHelpUrls() {
302 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevisions';