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
27 * Query module to enumerate deleted revisions for pages.
31 class ApiQueryDeletedRevisions
extends ApiQueryRevisionsBase
{
33 public function __construct( ApiQuery
$query, $moduleName ) {
34 parent
::__construct( $query, $moduleName, 'drv' );
37 protected function run( ApiPageSet
$resultPageSet = null ) {
38 $user = $this->getUser();
39 // Before doing anything at all, let's check permissions
40 $this->checkUserRightsAny( 'deletedhistory' );
42 $pageSet = $this->getPageSet();
43 $pageMap = $pageSet->getGoodAndMissingTitlesByNamespace();
44 $pageCount = count( $pageSet->getGoodAndMissingTitles() );
45 $revCount = $pageSet->getRevisionCount();
46 if ( $revCount === 0 && $pageCount === 0 ) {
50 if ( $revCount !== 0 && count( $pageSet->getDeletedRevisionIDs() ) === 0 ) {
51 // Nothing to do, revisions were supplied but none are deleted
55 $params = $this->extractRequestParams( false );
59 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
61 if ( $resultPageSet === null ) {
62 $this->parseParameters( $params );
63 $arQuery = Revision
::getArchiveQueryInfo();
64 $this->addTables( $arQuery['tables'] );
65 $this->addFields( $arQuery['fields'] );
66 $this->addJoinConds( $arQuery['joins'] );
67 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
69 $this->limit
= $this->getParameter( 'limit' ) ?
: 10;
70 $this->addTables( 'archive' );
71 $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
74 if ( $this->fld_tags
) {
75 $this->addTables( 'tag_summary' );
77 [ 'tag_summary' => [ 'LEFT JOIN', [ 'ar_rev_id=ts_rev_id' ] ] ]
79 $this->addFields( 'ts_tags' );
82 if ( !is_null( $params['tag'] ) ) {
83 $this->addTables( 'change_tag' );
85 [ 'change_tag' => [ 'INNER JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
87 $this->addWhereFld( 'ct_tag', $params['tag'] );
90 if ( $this->fetchContent
) {
91 $this->addTables( 'text' );
93 [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
95 $this->addFields( [ 'old_text', 'old_flags' ] );
97 // This also means stricter restrictions
98 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
101 $dir = $params['dir'];
103 if ( $revCount !== 0 ) {
105 'ar_rev_id' => array_keys( $pageSet->getDeletedRevisionIDs() )
108 // We need a custom WHERE clause that matches all titles.
109 $lb = new LinkBatch( $pageSet->getGoodAndMissingTitles() );
110 $where = $lb->constructSet( 'ar', $db );
111 $this->addWhere( $where );
114 if ( !is_null( $params['user'] ) ) {
115 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
116 $actorQuery = ActorMigration
::newMigration()
117 ->getWhere( $db, 'ar_user', User
::newFromName( $params['user'], false ), false );
118 $this->addTables( $actorQuery['tables'] );
119 $this->addJoinConds( $actorQuery['joins'] );
120 $this->addWhere( $actorQuery['conds'] );
121 } elseif ( !is_null( $params['excludeuser'] ) ) {
122 // Here there's no chance of using ar_usertext_timestamp.
123 $actorQuery = ActorMigration
::newMigration()
124 ->getWhere( $db, 'ar_user', User
::newFromName( $params['excludeuser'], false ) );
125 $this->addTables( $actorQuery['tables'] );
126 $this->addJoinConds( $actorQuery['joins'] );
127 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
130 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
131 // Paranoia: avoid brute force searches (T19342)
132 // (shouldn't be able to get here without 'deletedhistory', but
133 // check it again just in case)
134 if ( !$user->isAllowed( 'deletedhistory' ) ) {
135 $bitmask = Revision
::DELETED_USER
;
136 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
137 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
142 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
146 if ( !is_null( $params['continue'] ) ) {
147 $cont = explode( '|', $params['continue'] );
148 $op = ( $dir == 'newer' ?
'>' : '<' );
149 if ( $revCount !== 0 ) {
150 $this->dieContinueUsageIf( count( $cont ) != 2 );
151 $rev = intval( $cont[0] );
152 $this->dieContinueUsageIf( strval( $rev ) !== $cont[0] );
153 $ar_id = (int)$cont[1];
154 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
155 $this->addWhere( "ar_rev_id $op $rev OR " .
156 "(ar_rev_id = $rev AND " .
157 "ar_id $op= $ar_id)" );
159 $this->dieContinueUsageIf( count( $cont ) != 4 );
160 $ns = intval( $cont[0] );
161 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
162 $title = $db->addQuotes( $cont[1] );
163 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
164 $ar_id = (int)$cont[3];
165 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
166 $this->addWhere( "ar_namespace $op $ns OR " .
167 "(ar_namespace = $ns AND " .
168 "(ar_title $op $title OR " .
169 "(ar_title = $title AND " .
170 "(ar_timestamp $op $ts OR " .
171 "(ar_timestamp = $ts AND " .
172 "ar_id $op= $ar_id)))))" );
176 $this->addOption( 'LIMIT', $this->limit +
1 );
178 if ( $revCount !== 0 ) {
179 // Sort by ar_rev_id when querying by ar_rev_id
180 $this->addWhereRange( 'ar_rev_id', $dir, null, null );
182 // Sort by ns and title in the same order as timestamp for efficiency
183 // But only when not already unique in the query
184 if ( count( $pageMap ) > 1 ) {
185 $this->addWhereRange( 'ar_namespace', $dir, null, null );
187 $oneTitle = key( reset( $pageMap ) );
188 foreach ( $pageMap as $pages ) {
189 if ( count( $pages ) > 1 ||
key( $pages ) !== $oneTitle ) {
190 $this->addWhereRange( 'ar_title', $dir, null, null );
194 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
196 // Include in ORDER BY for uniqueness
197 $this->addWhereRange( 'ar_id', $dir, null, null );
199 $res = $this->select( __METHOD__
);
202 foreach ( $res as $row ) {
203 if ( ++
$count > $this->limit
) {
205 $this->setContinueEnumParameter( 'continue',
207 ?
"$row->ar_rev_id|$row->ar_id"
208 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
213 if ( $resultPageSet !== null ) {
214 $generated[] = $row->ar_rev_id
;
216 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
218 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
219 $converted = $pageSet->getConvertedTitles();
220 if ( $title && isset( $converted[$title->getPrefixedText()] ) ) {
221 $title = Title
::newFromText( $converted[$title->getPrefixedText()] );
222 if ( $title && isset( $pageMap[$title->getNamespace()][$title->getDBkey()] ) ) {
223 $pageMap[$row->ar_namespace
][$row->ar_title
] =
224 $pageMap[$title->getNamespace()][$title->getDBkey()];
228 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
231 "Found row in archive (ar_id={$row->ar_id}) that didn't get processed by ApiPageSet"
235 $fit = $this->addPageSubItem(
236 $pageMap[$row->ar_namespace
][$row->ar_title
],
237 $this->extractRevisionInfo( Revision
::newFromArchiveRow( $row ), $row ),
241 $this->setContinueEnumParameter( 'continue',
243 ?
"$row->ar_rev_id|$row->ar_id"
244 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
251 if ( $resultPageSet !== null ) {
252 $resultPageSet->populateFromRevisionIDs( $generated );
256 public function getAllowedParams() {
257 return parent
::getAllowedParams() +
[
259 ApiBase
::PARAM_TYPE
=> 'timestamp',
262 ApiBase
::PARAM_TYPE
=> 'timestamp',
265 ApiBase
::PARAM_TYPE
=> [
269 ApiBase
::PARAM_DFLT
=> 'older',
270 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
274 ApiBase
::PARAM_TYPE
=> 'user'
277 ApiBase
::PARAM_TYPE
=> 'user'
280 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
285 protected function getExamplesMessages() {
287 'action=query&prop=deletedrevisions&titles=Main%20Page|Talk:Main%20Page&' .
288 'drvprop=user|comment|content'
289 => 'apihelp-query+deletedrevisions-example-titles',
290 'action=query&prop=deletedrevisions&revids=123456'
291 => 'apihelp-query+deletedrevisions-example-revids',
295 public function getHelpUrls() {
296 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevisions';