3 * Created on Oct 3, 2014
5 * Copyright © 2014 Wikimedia Foundation and contributors
7 * Heavily based on ApiQueryDeletedrevs,
8 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
29 * Query module to enumerate deleted revisions for pages.
33 class ApiQueryDeletedRevisions
extends ApiQueryRevisionsBase
{
35 public function __construct( ApiQuery
$query, $moduleName ) {
36 parent
::__construct( $query, $moduleName, 'drv' );
39 protected function run( ApiPageSet
$resultPageSet = null ) {
40 $user = $this->getUser();
41 // Before doing anything at all, let's check permissions
42 $this->checkUserRightsAny( 'deletedhistory' );
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 );
61 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
63 if ( $resultPageSet === null ) {
64 $this->parseParameters( $params );
65 $arQuery = Revision
::getArchiveQueryInfo();
66 $this->addTables( $arQuery['tables'] );
67 $this->addFields( $arQuery['fields'] );
68 $this->addJoinConds( $arQuery['joins'] );
69 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
71 $this->limit
= $this->getParameter( 'limit' ) ?
: 10;
72 $this->addTables( 'archive' );
73 $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
76 if ( $this->fld_tags
) {
77 $this->addTables( 'tag_summary' );
79 [ 'tag_summary' => [ 'LEFT JOIN', [ 'ar_rev_id=ts_rev_id' ] ] ]
81 $this->addFields( 'ts_tags' );
84 if ( !is_null( $params['tag'] ) ) {
85 $this->addTables( 'change_tag' );
87 [ 'change_tag' => [ 'INNER JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
89 $this->addWhereFld( 'ct_tag', $params['tag'] );
92 if ( $this->fetchContent
) {
93 // Modern MediaWiki has the content for deleted revs in the 'text'
94 // table using fields old_text and old_flags. But revisions deleted
95 // pre-1.5 store the content in the 'archive' table directly using
96 // fields ar_text and ar_flags, and no corresponding 'text' row. So
97 // we have to LEFT JOIN and fetch all four fields.
98 $this->addTables( 'text' );
100 [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
102 $this->addFields( [ 'ar_text', 'ar_flags', 'old_text', 'old_flags' ] );
104 // This also means stricter restrictions
105 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
108 $dir = $params['dir'];
110 if ( $revCount !== 0 ) {
112 'ar_rev_id' => array_keys( $pageSet->getDeletedRevisionIDs() )
115 // We need a custom WHERE clause that matches all titles.
116 $lb = new LinkBatch( $pageSet->getGoodAndMissingTitles() );
117 $where = $lb->constructSet( 'ar', $db );
118 $this->addWhere( $where );
121 if ( !is_null( $params['user'] ) ) {
122 $this->addWhereFld( 'ar_user_text', $params['user'] );
123 } elseif ( !is_null( $params['excludeuser'] ) ) {
124 $this->addWhere( 'ar_user_text != ' .
125 $db->addQuotes( $params['excludeuser'] ) );
128 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
129 // Paranoia: avoid brute force searches (T19342)
130 // (shouldn't be able to get here without 'deletedhistory', but
131 // check it again just in case)
132 if ( !$user->isAllowed( 'deletedhistory' ) ) {
133 $bitmask = Revision
::DELETED_USER
;
134 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
135 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
140 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
144 if ( !is_null( $params['continue'] ) ) {
145 $cont = explode( '|', $params['continue'] );
146 $op = ( $dir == 'newer' ?
'>' : '<' );
147 if ( $revCount !== 0 ) {
148 $this->dieContinueUsageIf( count( $cont ) != 2 );
149 $rev = intval( $cont[0] );
150 $this->dieContinueUsageIf( strval( $rev ) !== $cont[0] );
151 $ar_id = (int)$cont[1];
152 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
153 $this->addWhere( "ar_rev_id $op $rev OR " .
154 "(ar_rev_id = $rev AND " .
155 "ar_id $op= $ar_id)" );
157 $this->dieContinueUsageIf( count( $cont ) != 4 );
158 $ns = intval( $cont[0] );
159 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
160 $title = $db->addQuotes( $cont[1] );
161 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
162 $ar_id = (int)$cont[3];
163 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
164 $this->addWhere( "ar_namespace $op $ns OR " .
165 "(ar_namespace = $ns AND " .
166 "(ar_title $op $title OR " .
167 "(ar_title = $title AND " .
168 "(ar_timestamp $op $ts OR " .
169 "(ar_timestamp = $ts AND " .
170 "ar_id $op= $ar_id)))))" );
174 $this->addOption( 'LIMIT', $this->limit +
1 );
176 if ( $revCount !== 0 ) {
177 // Sort by ar_rev_id when querying by ar_rev_id
178 $this->addWhereRange( 'ar_rev_id', $dir, null, null );
180 // Sort by ns and title in the same order as timestamp for efficiency
181 // But only when not already unique in the query
182 if ( count( $pageMap ) > 1 ) {
183 $this->addWhereRange( 'ar_namespace', $dir, null, null );
185 $oneTitle = key( reset( $pageMap ) );
186 foreach ( $pageMap as $pages ) {
187 if ( count( $pages ) > 1 ||
key( $pages ) !== $oneTitle ) {
188 $this->addWhereRange( 'ar_title', $dir, null, null );
192 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
194 // Include in ORDER BY for uniqueness
195 $this->addWhereRange( 'ar_id', $dir, null, null );
197 $res = $this->select( __METHOD__
);
200 foreach ( $res as $row ) {
201 if ( ++
$count > $this->limit
) {
203 $this->setContinueEnumParameter( 'continue',
205 ?
"$row->ar_rev_id|$row->ar_id"
206 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
211 if ( $resultPageSet !== null ) {
212 $generated[] = $row->ar_rev_id
;
214 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
216 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
217 $converted = $pageSet->getConvertedTitles();
218 if ( $title && isset( $converted[$title->getPrefixedText()] ) ) {
219 $title = Title
::newFromText( $converted[$title->getPrefixedText()] );
220 if ( $title && isset( $pageMap[$title->getNamespace()][$title->getDBkey()] ) ) {
221 $pageMap[$row->ar_namespace
][$row->ar_title
] =
222 $pageMap[$title->getNamespace()][$title->getDBkey()];
226 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
229 "Found row in archive (ar_id={$row->ar_id}) that didn't get processed by ApiPageSet"
233 $fit = $this->addPageSubItem(
234 $pageMap[$row->ar_namespace
][$row->ar_title
],
235 $this->extractRevisionInfo( Revision
::newFromArchiveRow( $row ), $row ),
239 $this->setContinueEnumParameter( 'continue',
241 ?
"$row->ar_rev_id|$row->ar_id"
242 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
249 if ( $resultPageSet !== null ) {
250 $resultPageSet->populateFromRevisionIDs( $generated );
254 public function getAllowedParams() {
255 return parent
::getAllowedParams() +
[
257 ApiBase
::PARAM_TYPE
=> 'timestamp',
260 ApiBase
::PARAM_TYPE
=> 'timestamp',
263 ApiBase
::PARAM_TYPE
=> [
267 ApiBase
::PARAM_DFLT
=> 'older',
268 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
272 ApiBase
::PARAM_TYPE
=> 'user'
275 ApiBase
::PARAM_TYPE
=> 'user'
278 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
283 protected function getExamplesMessages() {
285 'action=query&prop=deletedrevisions&titles=Main%20Page|Talk:Main%20Page&' .
286 'drvprop=user|comment|content'
287 => 'apihelp-query+deletedrevisions-example-titles',
288 'action=query&prop=deletedrevisions&revids=123456'
289 => 'apihelp-query+deletedrevisions-example-revids',
293 public function getHelpUrls() {
294 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevisions';