3 * Created on Oct 3, 2014
5 * Copyright © 2014 Brad Jorsch "bjorsch@wikimedia.org"
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 all deleted revisions.
33 class ApiQueryAllDeletedRevisions
extends ApiQueryRevisionsBase
{
35 public function __construct( ApiQuery
$query, $moduleName ) {
36 parent
::__construct( $query, $moduleName, 'adr' );
40 * @param ApiPageSet $resultPageSet
43 protected function run( ApiPageSet
$resultPageSet = null ) {
44 $user = $this->getUser();
45 // Before doing anything at all, let's check permissions
46 if ( !$user->isAllowed( 'deletedhistory' ) ) {
48 'You don\'t have permission to view deleted revision information',
54 $params = $this->extractRequestParams( false );
56 $result = $this->getResult();
57 $pageSet = $this->getPageSet();
59 // This module operates in two modes:
60 // 'user': List deleted revs by a certain user
61 // 'all': List all deleted revs in NS
63 if ( !is_null( $params['user'] ) ) {
67 if ( $mode == 'user' ) {
68 foreach ( array( 'from', 'to', 'prefix', 'excludeuser' ) as $param ) {
69 if ( !is_null( $params[$param] ) ) {
70 $p = $this->getModulePrefix();
71 $this->dieUsage( "The '{$p}{$param}' parameter cannot be used with '{$p}user'",
76 foreach ( array( 'start', 'end' ) as $param ) {
77 if ( !is_null( $params[$param] ) ) {
78 $p = $this->getModulePrefix();
79 $this->dieUsage( "The '{$p}{$param}' parameter may only be used with '{$p}user'",
85 // If we're generating titles only, we can use DISTINCT for a better
86 // query. But we can't do that in 'user' mode (wrong index), and we can
87 // only do it when sorting ASC (because MySQL apparently can't use an
88 // index backwards for grouping even though it can for ORDER BY, WTF?)
89 $dir = $params['dir'];
90 $optimizeGenerateTitles = false;
91 if ( $mode === 'all' && $params['generatetitles'] && $resultPageSet !== null ) {
92 if ( $dir === 'newer' ) {
93 $optimizeGenerateTitles = true;
95 $p = $this->getModulePrefix();
96 $this->setWarning( "For better performance when generating titles, set {$p}dir=newer" );
100 $this->addTables( 'archive' );
101 if ( $resultPageSet === null ) {
102 $this->parseParameters( $params );
103 $this->addFields( Revision
::selectArchiveFields() );
104 $this->addFields( array( 'ar_title', 'ar_namespace' ) );
106 $this->limit
= $this->getParameter( 'limit' ) ?
: 10;
107 $this->addFields( array( 'ar_title', 'ar_namespace' ) );
108 if ( $optimizeGenerateTitles ) {
109 $this->addOption( 'DISTINCT' );
111 $this->addFields( array( 'ar_timestamp', 'ar_rev_id', 'ar_id' ) );
115 if ( $this->fld_tags
) {
116 $this->addTables( 'tag_summary' );
118 array( 'tag_summary' => array( 'LEFT JOIN', array( 'ar_rev_id=ts_rev_id' ) ) )
120 $this->addFields( 'ts_tags' );
123 if ( !is_null( $params['tag'] ) ) {
124 $this->addTables( 'change_tag' );
126 array( 'change_tag' => array( 'INNER JOIN', array( 'ar_rev_id=ct_rev_id' ) ) )
128 $this->addWhereFld( 'ct_tag', $params['tag'] );
131 if ( $this->fetchContent
) {
132 // Modern MediaWiki has the content for deleted revs in the 'text'
133 // table using fields old_text and old_flags. But revisions deleted
134 // pre-1.5 store the content in the 'archive' table directly using
135 // fields ar_text and ar_flags, and no corresponding 'text' row. So
136 // we have to LEFT JOIN and fetch all four fields.
137 $this->addTables( 'text' );
139 array( 'text' => array( 'LEFT JOIN', array( 'ar_text_id=old_id' ) ) )
141 $this->addFields( array( 'ar_text', 'ar_flags', 'old_text', 'old_flags' ) );
143 // This also means stricter restrictions
144 if ( !$user->isAllowedAny( 'undelete', 'deletedtext' ) ) {
146 'You don\'t have permission to view deleted revision content',
154 if ( $mode == 'all' ) {
155 if ( $params['namespace'] !== null ) {
156 $namespaces = $params['namespace'];
157 $this->addWhereFld( 'ar_namespace', $namespaces );
159 $namespaces = MWNamespace
::getValidNamespaces();
162 // For from/to/prefix, we have to consider the potential
163 // transformations of the title in all specified namespaces.
164 // Generally there will be only one transformation, but wikis with
165 // some namespaces case-sensitive could have two.
166 if ( $params['from'] !== null ||
$params['to'] !== null ) {
167 $isDirNewer = ( $dir === 'newer' );
168 $after = ( $isDirNewer ?
'>=' : '<=' );
169 $before = ( $isDirNewer ?
'<=' : '>=' );
171 foreach ( $namespaces as $ns ) {
173 if ( $params['from'] !== null ) {
174 $w[] = 'ar_title' . $after .
175 $db->addQuotes( $this->titlePartToKey( $params['from'], $ns ) );
177 if ( $params['to'] !== null ) {
178 $w[] = 'ar_title' . $before .
179 $db->addQuotes( $this->titlePartToKey( $params['to'], $ns ) );
181 $w = $db->makeList( $w, LIST_AND
);
184 if ( count( $where ) == 1 ) {
185 $where = key( $where );
186 $this->addWhere( $where );
189 foreach ( $where as $w => $ns ) {
190 $where2[] = $db->makeList( array( $w, 'ar_namespace' => $ns ), LIST_AND
);
192 $this->addWhere( $db->makeList( $where2, LIST_OR
) );
196 if ( isset( $params['prefix'] ) ) {
198 foreach ( $namespaces as $ns ) {
199 $w = 'ar_title' . $db->buildLike(
200 $this->titlePartToKey( $params['prefix'], $ns ),
204 if ( count( $where ) == 1 ) {
205 $where = key( $where );
206 $this->addWhere( $where );
209 foreach ( $where as $w => $ns ) {
210 $where2[] = $db->makeList( array( $w, 'ar_namespace' => $ns ), LIST_AND
);
212 $this->addWhere( $db->makeList( $where2, LIST_OR
) );
216 if ( $this->getConfig()->get( 'MiserMode' ) ) {
217 $miser_ns = $params['namespace'];
219 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
221 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
224 if ( !is_null( $params['user'] ) ) {
225 $this->addWhereFld( 'ar_user_text', $params['user'] );
226 } elseif ( !is_null( $params['excludeuser'] ) ) {
227 $this->addWhere( 'ar_user_text != ' .
228 $db->addQuotes( $params['excludeuser'] ) );
231 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
232 // Paranoia: avoid brute force searches (bug 17342)
233 // (shouldn't be able to get here without 'deletedhistory', but
234 // check it again just in case)
235 if ( !$user->isAllowed( 'deletedhistory' ) ) {
236 $bitmask = Revision
::DELETED_USER
;
237 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
238 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
243 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
247 if ( !is_null( $params['continue'] ) ) {
248 $cont = explode( '|', $params['continue'] );
249 $op = ( $dir == 'newer' ?
'>' : '<' );
250 if ( $optimizeGenerateTitles ) {
251 $this->dieContinueUsageIf( count( $cont ) != 2 );
252 $ns = intval( $cont[0] );
253 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
254 $title = $db->addQuotes( $cont[1] );
255 $this->addWhere( "ar_namespace $op $ns OR " .
256 "(ar_namespace = $ns AND ar_title $op= $title)" );
257 } elseif ( $mode == 'all' ) {
258 $this->dieContinueUsageIf( count( $cont ) != 4 );
259 $ns = intval( $cont[0] );
260 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
261 $title = $db->addQuotes( $cont[1] );
262 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
263 $ar_id = (int)$cont[3];
264 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
265 $this->addWhere( "ar_namespace $op $ns OR " .
266 "(ar_namespace = $ns AND " .
267 "(ar_title $op $title OR " .
268 "(ar_title = $title AND " .
269 "(ar_timestamp $op $ts OR " .
270 "(ar_timestamp = $ts AND " .
271 "ar_id $op= $ar_id)))))" );
273 $this->dieContinueUsageIf( count( $cont ) != 2 );
274 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
275 $ar_id = (int)$cont[1];
276 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
277 $this->addWhere( "ar_timestamp $op $ts OR " .
278 "(ar_timestamp = $ts AND " .
279 "ar_id $op= $ar_id)" );
283 $this->addOption( 'LIMIT', $this->limit +
1 );
285 $sort = ( $dir == 'newer' ?
'' : ' DESC' );
287 if ( $optimizeGenerateTitles ) {
288 // Targeting index name_title_timestamp
289 if ( $params['namespace'] === null ||
count( array_unique( $params['namespace'] ) ) > 1 ) {
290 $orderby[] = "ar_namespace $sort";
292 $orderby[] = "ar_title $sort";
293 } elseif ( $mode == 'all' ) {
294 // Targeting index name_title_timestamp
295 if ( $params['namespace'] === null ||
count( array_unique( $params['namespace'] ) ) > 1 ) {
296 $orderby[] = "ar_namespace $sort";
298 $orderby[] = "ar_title $sort";
299 $orderby[] = "ar_timestamp $sort";
300 $orderby[] = "ar_id $sort";
302 // Targeting index usertext_timestamp
303 // 'user' is always constant.
304 $orderby[] = "ar_timestamp $sort";
305 $orderby[] = "ar_id $sort";
307 $this->addOption( 'ORDER BY', $orderby );
309 $res = $this->select( __METHOD__
);
310 $pageMap = array(); // Maps ns&title to array index
313 $generated = array();
314 foreach ( $res as $row ) {
315 if ( ++
$count > $this->limit
) {
317 if ( $optimizeGenerateTitles ) {
318 $this->setContinueEnumParameter( 'continue', "$row->ar_namespace|$row->ar_title" );
319 } elseif ( $mode == 'all' ) {
320 $this->setContinueEnumParameter( 'continue',
321 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
324 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
329 // Miser mode namespace check
330 if ( $miser_ns !== null && !in_array( $row->ar_namespace
, $miser_ns ) ) {
334 if ( $resultPageSet !== null ) {
335 if ( $params['generatetitles'] ) {
336 $key = "{$row->ar_namespace}:{$row->ar_title}";
337 if ( !isset( $generated[$key] ) ) {
338 $generated[$key] = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
341 $generated[] = $row->ar_rev_id
;
344 $revision = Revision
::newFromArchiveRow( $row );
345 $rev = $this->extractRevisionInfo( $revision, $row );
347 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
348 $index = $nextIndex++
;
349 $pageMap[$row->ar_namespace
][$row->ar_title
] = $index;
350 $title = $revision->getTitle();
352 'pageid' => $title->getArticleID(),
353 'revisions' => array( $rev ),
355 ApiResult
::setIndexedTagName( $a['revisions'], 'rev' );
356 ApiQueryBase
::addTitleInfo( $a, $title );
357 $fit = $result->addValue( array( 'query', $this->getModuleName() ), $index, $a );
359 $index = $pageMap[$row->ar_namespace
][$row->ar_title
];
360 $fit = $result->addValue(
361 array( 'query', $this->getModuleName(), $index, 'revisions' ),
365 if ( $mode == 'all' ) {
366 $this->setContinueEnumParameter( 'continue',
367 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
370 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
377 if ( $resultPageSet !== null ) {
378 if ( $params['generatetitles'] ) {
379 $resultPageSet->populateFromTitles( $generated );
381 $resultPageSet->populateFromRevisionIDs( $generated );
384 $result->addIndexedTagName( array( 'query', $this->getModuleName() ), 'page' );
388 public function getAllowedParams() {
389 $ret = parent
::getAllowedParams() +
array(
391 ApiBase
::PARAM_TYPE
=> 'user'
393 'namespace' => array(
394 ApiBase
::PARAM_ISMULTI
=> true,
395 ApiBase
::PARAM_TYPE
=> 'namespace',
396 ApiBase
::PARAM_DFLT
=> null,
399 ApiBase
::PARAM_TYPE
=> 'timestamp',
400 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'useronly' ) ),
403 ApiBase
::PARAM_TYPE
=> 'timestamp',
404 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'useronly' ) ),
407 ApiBase
::PARAM_TYPE
=> array(
411 ApiBase
::PARAM_DFLT
=> 'older',
412 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
415 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'nonuseronly' ) ),
418 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'nonuseronly' ) ),
421 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'nonuseronly' ) ),
423 'excludeuser' => array(
424 ApiBase
::PARAM_TYPE
=> 'user',
425 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'nonuseronly' ) ),
429 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
431 'generatetitles' => array(
432 ApiBase
::PARAM_DFLT
=> false
436 if ( $this->getConfig()->get( 'MiserMode' ) ) {
437 $ret['user'][ApiBase
::PARAM_HELP_MSG_APPEND
] = array(
438 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
440 $ret['namespace'][ApiBase
::PARAM_HELP_MSG_APPEND
] = array(
441 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
448 protected function getExamplesMessages() {
450 'action=query&list=alldeletedrevisions&adruser=Example&adrlimit=50'
451 => 'apihelp-query+alldeletedrevisions-example-user',
452 'action=query&list=alldeletedrevisions&adrdir=newer&adrlimit=50'
453 => 'apihelp-query+alldeletedrevisions-example-ns-main',
457 public function getHelpUrls() {
458 return 'https://www.mediawiki.org/wiki/API:Alldeletedrevisions';