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 // Modern MediaWiki has the content for deleted revs in the 'text'
92 // table using fields old_text and old_flags. But revisions deleted
93 // pre-1.5 store the content in the 'archive' table directly using
94 // fields ar_text and ar_flags, and no corresponding 'text' row. So
95 // we have to LEFT JOIN and fetch all four fields.
96 $this->addTables( 'text' );
98 [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
100 $this->addFields( [ 'ar_text', 'ar_flags', 'old_text', 'old_flags' ] );
102 // This also means stricter restrictions
103 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
106 $dir = $params['dir'];
108 if ( $revCount !== 0 ) {
110 'ar_rev_id' => array_keys( $pageSet->getDeletedRevisionIDs() )
113 // We need a custom WHERE clause that matches all titles.
114 $lb = new LinkBatch( $pageSet->getGoodAndMissingTitles() );
115 $where = $lb->constructSet( 'ar', $db );
116 $this->addWhere( $where );
119 if ( !is_null( $params['user'] ) ) {
120 $this->addWhereFld( 'ar_user_text', $params['user'] );
121 } elseif ( !is_null( $params['excludeuser'] ) ) {
122 $this->addWhere( 'ar_user_text != ' .
123 $db->addQuotes( $params['excludeuser'] ) );
126 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
127 // Paranoia: avoid brute force searches (T19342)
128 // (shouldn't be able to get here without 'deletedhistory', but
129 // check it again just in case)
130 if ( !$user->isAllowed( 'deletedhistory' ) ) {
131 $bitmask = Revision
::DELETED_USER
;
132 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
133 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
138 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
142 if ( !is_null( $params['continue'] ) ) {
143 $cont = explode( '|', $params['continue'] );
144 $op = ( $dir == 'newer' ?
'>' : '<' );
145 if ( $revCount !== 0 ) {
146 $this->dieContinueUsageIf( count( $cont ) != 2 );
147 $rev = intval( $cont[0] );
148 $this->dieContinueUsageIf( strval( $rev ) !== $cont[0] );
149 $ar_id = (int)$cont[1];
150 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
151 $this->addWhere( "ar_rev_id $op $rev OR " .
152 "(ar_rev_id = $rev AND " .
153 "ar_id $op= $ar_id)" );
155 $this->dieContinueUsageIf( count( $cont ) != 4 );
156 $ns = intval( $cont[0] );
157 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
158 $title = $db->addQuotes( $cont[1] );
159 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
160 $ar_id = (int)$cont[3];
161 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
162 $this->addWhere( "ar_namespace $op $ns OR " .
163 "(ar_namespace = $ns AND " .
164 "(ar_title $op $title OR " .
165 "(ar_title = $title AND " .
166 "(ar_timestamp $op $ts OR " .
167 "(ar_timestamp = $ts AND " .
168 "ar_id $op= $ar_id)))))" );
172 $this->addOption( 'LIMIT', $this->limit +
1 );
174 if ( $revCount !== 0 ) {
175 // Sort by ar_rev_id when querying by ar_rev_id
176 $this->addWhereRange( 'ar_rev_id', $dir, null, null );
178 // Sort by ns and title in the same order as timestamp for efficiency
179 // But only when not already unique in the query
180 if ( count( $pageMap ) > 1 ) {
181 $this->addWhereRange( 'ar_namespace', $dir, null, null );
183 $oneTitle = key( reset( $pageMap ) );
184 foreach ( $pageMap as $pages ) {
185 if ( count( $pages ) > 1 ||
key( $pages ) !== $oneTitle ) {
186 $this->addWhereRange( 'ar_title', $dir, null, null );
190 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
192 // Include in ORDER BY for uniqueness
193 $this->addWhereRange( 'ar_id', $dir, null, null );
195 $res = $this->select( __METHOD__
);
198 foreach ( $res as $row ) {
199 if ( ++
$count > $this->limit
) {
201 $this->setContinueEnumParameter( 'continue',
203 ?
"$row->ar_rev_id|$row->ar_id"
204 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
209 if ( $resultPageSet !== null ) {
210 $generated[] = $row->ar_rev_id
;
212 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
214 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
215 $converted = $pageSet->getConvertedTitles();
216 if ( $title && isset( $converted[$title->getPrefixedText()] ) ) {
217 $title = Title
::newFromText( $converted[$title->getPrefixedText()] );
218 if ( $title && isset( $pageMap[$title->getNamespace()][$title->getDBkey()] ) ) {
219 $pageMap[$row->ar_namespace
][$row->ar_title
] =
220 $pageMap[$title->getNamespace()][$title->getDBkey()];
224 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
227 "Found row in archive (ar_id={$row->ar_id}) that didn't get processed by ApiPageSet"
231 $fit = $this->addPageSubItem(
232 $pageMap[$row->ar_namespace
][$row->ar_title
],
233 $this->extractRevisionInfo( Revision
::newFromArchiveRow( $row ), $row ),
237 $this->setContinueEnumParameter( 'continue',
239 ?
"$row->ar_rev_id|$row->ar_id"
240 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
247 if ( $resultPageSet !== null ) {
248 $resultPageSet->populateFromRevisionIDs( $generated );
252 public function getAllowedParams() {
253 return parent
::getAllowedParams() +
[
255 ApiBase
::PARAM_TYPE
=> 'timestamp',
258 ApiBase
::PARAM_TYPE
=> 'timestamp',
261 ApiBase
::PARAM_TYPE
=> [
265 ApiBase
::PARAM_DFLT
=> 'older',
266 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
270 ApiBase
::PARAM_TYPE
=> 'user'
273 ApiBase
::PARAM_TYPE
=> 'user'
276 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
281 protected function getExamplesMessages() {
283 'action=query&prop=deletedrevisions&titles=Main%20Page|Talk:Main%20Page&' .
284 'drvprop=user|comment|content'
285 => 'apihelp-query+deletedrevisions-example-titles',
286 'action=query&prop=deletedrevisions&revids=123456'
287 => 'apihelp-query+deletedrevisions-example-revids',
291 public function getHelpUrls() {
292 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevisions';