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 all deleted revisions.
31 class ApiQueryAllDeletedRevisions
extends ApiQueryRevisionsBase
{
33 public function __construct( ApiQuery
$query, $moduleName ) {
34 parent
::__construct( $query, $moduleName, 'adr' );
38 * @param ApiPageSet $resultPageSet
41 protected function run( ApiPageSet
$resultPageSet = null ) {
42 // Before doing anything at all, let's check permissions
43 $this->checkUserRightsAny( 'deletedhistory' );
45 $user = $this->getUser();
47 $params = $this->extractRequestParams( false );
49 $result = $this->getResult();
51 // If the user wants no namespaces, they get no pages.
52 if ( $params['namespace'] === [] ) {
53 if ( $resultPageSet === null ) {
54 $result->addValue( 'query', $this->getModuleName(), [] );
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 ( [ 'from', 'to', 'prefix', 'excludeuser' ] as $param ) {
69 if ( !is_null( $params[$param] ) ) {
70 $p = $this->getModulePrefix();
72 [ 'apierror-invalidparammix-cannotusewith', $p.$param, "{$p}user" ],
78 foreach ( [ 'start', 'end' ] as $param ) {
79 if ( !is_null( $params[$param] ) ) {
80 $p = $this->getModulePrefix();
82 [ 'apierror-invalidparammix-mustusewith', $p.$param, "{$p}user" ],
89 // If we're generating titles only, we can use DISTINCT for a better
90 // query. But we can't do that in 'user' mode (wrong index), and we can
91 // only do it when sorting ASC (because MySQL apparently can't use an
92 // index backwards for grouping even though it can for ORDER BY, WTF?)
93 $dir = $params['dir'];
94 $optimizeGenerateTitles = false;
95 if ( $mode === 'all' && $params['generatetitles'] && $resultPageSet !== null ) {
96 if ( $dir === 'newer' ) {
97 $optimizeGenerateTitles = true;
99 $p = $this->getModulePrefix();
100 $this->addWarning( [ 'apiwarn-alldeletedrevisions-performance', $p ], 'performance' );
104 if ( $resultPageSet === null ) {
105 $this->parseParameters( $params );
106 $arQuery = Revision
::getArchiveQueryInfo();
107 $this->addTables( $arQuery['tables'] );
108 $this->addJoinConds( $arQuery['joins'] );
109 $this->addFields( $arQuery['fields'] );
110 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
112 $this->limit
= $this->getParameter( 'limit' ) ?
: 10;
113 $this->addTables( 'archive' );
114 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
115 if ( $optimizeGenerateTitles ) {
116 $this->addOption( 'DISTINCT' );
118 $this->addFields( [ 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
122 if ( $this->fld_tags
) {
123 $this->addTables( 'tag_summary' );
125 [ 'tag_summary' => [ 'LEFT JOIN', [ 'ar_rev_id=ts_rev_id' ] ] ]
127 $this->addFields( 'ts_tags' );
130 if ( !is_null( $params['tag'] ) ) {
131 $this->addTables( 'change_tag' );
133 [ 'change_tag' => [ 'INNER JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
135 $this->addWhereFld( 'ct_tag', $params['tag'] );
138 if ( $this->fetchContent
) {
139 $this->addTables( 'text' );
141 [ 'text' => [ 'LEFT JOIN', [ 'ar_text_id=old_id' ] ] ]
143 $this->addFields( [ 'old_text', 'old_flags' ] );
145 // This also means stricter restrictions
146 $this->checkUserRightsAny( [ 'deletedtext', 'undelete' ] );
151 if ( $mode == 'all' ) {
152 if ( $params['namespace'] !== null ) {
153 $namespaces = $params['namespace'];
155 $namespaces = MWNamespace
::getValidNamespaces();
157 $this->addWhereFld( 'ar_namespace', $namespaces );
159 // For from/to/prefix, we have to consider the potential
160 // transformations of the title in all specified namespaces.
161 // Generally there will be only one transformation, but wikis with
162 // some namespaces case-sensitive could have two.
163 if ( $params['from'] !== null ||
$params['to'] !== null ) {
164 $isDirNewer = ( $dir === 'newer' );
165 $after = ( $isDirNewer ?
'>=' : '<=' );
166 $before = ( $isDirNewer ?
'<=' : '>=' );
168 foreach ( $namespaces as $ns ) {
170 if ( $params['from'] !== null ) {
171 $w[] = 'ar_title' . $after .
172 $db->addQuotes( $this->titlePartToKey( $params['from'], $ns ) );
174 if ( $params['to'] !== null ) {
175 $w[] = 'ar_title' . $before .
176 $db->addQuotes( $this->titlePartToKey( $params['to'], $ns ) );
178 $w = $db->makeList( $w, LIST_AND
);
181 if ( count( $where ) == 1 ) {
182 $where = key( $where );
183 $this->addWhere( $where );
186 foreach ( $where as $w => $ns ) {
187 $where2[] = $db->makeList( [ $w, 'ar_namespace' => $ns ], LIST_AND
);
189 $this->addWhere( $db->makeList( $where2, LIST_OR
) );
193 if ( isset( $params['prefix'] ) ) {
195 foreach ( $namespaces as $ns ) {
196 $w = 'ar_title' . $db->buildLike(
197 $this->titlePartToKey( $params['prefix'], $ns ),
201 if ( count( $where ) == 1 ) {
202 $where = key( $where );
203 $this->addWhere( $where );
206 foreach ( $where as $w => $ns ) {
207 $where2[] = $db->makeList( [ $w, 'ar_namespace' => $ns ], LIST_AND
);
209 $this->addWhere( $db->makeList( $where2, LIST_OR
) );
213 if ( $this->getConfig()->get( 'MiserMode' ) ) {
214 $miser_ns = $params['namespace'];
216 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
218 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
221 if ( !is_null( $params['user'] ) ) {
222 // Don't query by user ID here, it might be able to use the ar_usertext_timestamp index.
223 $actorQuery = ActorMigration
::newMigration()
224 ->getWhere( $db, 'ar_user', User
::newFromName( $params['user'], false ), false );
225 $this->addTables( $actorQuery['tables'] );
226 $this->addJoinConds( $actorQuery['joins'] );
227 $this->addWhere( $actorQuery['conds'] );
228 } elseif ( !is_null( $params['excludeuser'] ) ) {
229 // Here there's no chance of using ar_usertext_timestamp.
230 $actorQuery = ActorMigration
::newMigration()
231 ->getWhere( $db, 'ar_user', User
::newFromName( $params['excludeuser'], false ) );
232 $this->addTables( $actorQuery['tables'] );
233 $this->addJoinConds( $actorQuery['joins'] );
234 $this->addWhere( 'NOT(' . $actorQuery['conds'] . ')' );
237 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
238 // Paranoia: avoid brute force searches (T19342)
239 // (shouldn't be able to get here without 'deletedhistory', but
240 // check it again just in case)
241 if ( !$user->isAllowed( 'deletedhistory' ) ) {
242 $bitmask = Revision
::DELETED_USER
;
243 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
244 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
249 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
253 if ( !is_null( $params['continue'] ) ) {
254 $cont = explode( '|', $params['continue'] );
255 $op = ( $dir == 'newer' ?
'>' : '<' );
256 if ( $optimizeGenerateTitles ) {
257 $this->dieContinueUsageIf( count( $cont ) != 2 );
258 $ns = intval( $cont[0] );
259 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
260 $title = $db->addQuotes( $cont[1] );
261 $this->addWhere( "ar_namespace $op $ns OR " .
262 "(ar_namespace = $ns AND ar_title $op= $title)" );
263 } elseif ( $mode == 'all' ) {
264 $this->dieContinueUsageIf( count( $cont ) != 4 );
265 $ns = intval( $cont[0] );
266 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
267 $title = $db->addQuotes( $cont[1] );
268 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
269 $ar_id = (int)$cont[3];
270 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
271 $this->addWhere( "ar_namespace $op $ns OR " .
272 "(ar_namespace = $ns AND " .
273 "(ar_title $op $title OR " .
274 "(ar_title = $title AND " .
275 "(ar_timestamp $op $ts OR " .
276 "(ar_timestamp = $ts AND " .
277 "ar_id $op= $ar_id)))))" );
279 $this->dieContinueUsageIf( count( $cont ) != 2 );
280 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
281 $ar_id = (int)$cont[1];
282 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
283 $this->addWhere( "ar_timestamp $op $ts OR " .
284 "(ar_timestamp = $ts AND " .
285 "ar_id $op= $ar_id)" );
289 $this->addOption( 'LIMIT', $this->limit +
1 );
291 $sort = ( $dir == 'newer' ?
'' : ' DESC' );
293 if ( $optimizeGenerateTitles ) {
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 } elseif ( $mode == 'all' ) {
300 // Targeting index name_title_timestamp
301 if ( $params['namespace'] === null ||
count( array_unique( $params['namespace'] ) ) > 1 ) {
302 $orderby[] = "ar_namespace $sort";
304 $orderby[] = "ar_title $sort";
305 $orderby[] = "ar_timestamp $sort";
306 $orderby[] = "ar_id $sort";
308 // Targeting index usertext_timestamp
309 // 'user' is always constant.
310 $orderby[] = "ar_timestamp $sort";
311 $orderby[] = "ar_id $sort";
313 $this->addOption( 'ORDER BY', $orderby );
315 $res = $this->select( __METHOD__
);
316 $pageMap = []; // Maps ns&title to array index
320 foreach ( $res as $row ) {
321 if ( ++
$count > $this->limit
) {
323 if ( $optimizeGenerateTitles ) {
324 $this->setContinueEnumParameter( 'continue', "$row->ar_namespace|$row->ar_title" );
325 } elseif ( $mode == 'all' ) {
326 $this->setContinueEnumParameter( 'continue',
327 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
330 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
335 // Miser mode namespace check
336 if ( $miser_ns !== null && !in_array( $row->ar_namespace
, $miser_ns ) ) {
340 if ( $resultPageSet !== null ) {
341 if ( $params['generatetitles'] ) {
342 $key = "{$row->ar_namespace}:{$row->ar_title}";
343 if ( !isset( $generated[$key] ) ) {
344 $generated[$key] = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
347 $generated[] = $row->ar_rev_id
;
350 $revision = Revision
::newFromArchiveRow( $row );
351 $rev = $this->extractRevisionInfo( $revision, $row );
353 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
354 $index = $nextIndex++
;
355 $pageMap[$row->ar_namespace
][$row->ar_title
] = $index;
356 $title = $revision->getTitle();
358 'pageid' => $title->getArticleID(),
359 'revisions' => [ $rev ],
361 ApiResult
::setIndexedTagName( $a['revisions'], 'rev' );
362 ApiQueryBase
::addTitleInfo( $a, $title );
363 $fit = $result->addValue( [ 'query', $this->getModuleName() ], $index, $a );
365 $index = $pageMap[$row->ar_namespace
][$row->ar_title
];
366 $fit = $result->addValue(
367 [ 'query', $this->getModuleName(), $index, 'revisions' ],
371 if ( $mode == 'all' ) {
372 $this->setContinueEnumParameter( 'continue',
373 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
376 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
383 if ( $resultPageSet !== null ) {
384 if ( $params['generatetitles'] ) {
385 $resultPageSet->populateFromTitles( $generated );
387 $resultPageSet->populateFromRevisionIDs( $generated );
390 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
394 public function getAllowedParams() {
395 $ret = parent
::getAllowedParams() +
[
397 ApiBase
::PARAM_TYPE
=> 'user'
400 ApiBase
::PARAM_ISMULTI
=> true,
401 ApiBase
::PARAM_TYPE
=> 'namespace',
404 ApiBase
::PARAM_TYPE
=> 'timestamp',
405 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'useronly' ] ],
408 ApiBase
::PARAM_TYPE
=> 'timestamp',
409 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'useronly' ] ],
412 ApiBase
::PARAM_TYPE
=> [
416 ApiBase
::PARAM_DFLT
=> 'older',
417 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
420 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'nonuseronly' ] ],
423 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'nonuseronly' ] ],
426 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'nonuseronly' ] ],
429 ApiBase
::PARAM_TYPE
=> 'user',
430 ApiBase
::PARAM_HELP_MSG_INFO
=> [ [ 'nonuseronly' ] ],
434 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
436 'generatetitles' => [
437 ApiBase
::PARAM_DFLT
=> false
441 if ( $this->getConfig()->get( 'MiserMode' ) ) {
442 $ret['user'][ApiBase
::PARAM_HELP_MSG_APPEND
] = [
443 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
445 $ret['namespace'][ApiBase
::PARAM_HELP_MSG_APPEND
] = [
446 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
453 protected function getExamplesMessages() {
455 'action=query&list=alldeletedrevisions&adruser=Example&adrlimit=50'
456 => 'apihelp-query+alldeletedrevisions-example-user',
457 'action=query&list=alldeletedrevisions&adrdir=newer&adrnamespace=0&adrlimit=50'
458 => 'apihelp-query+alldeletedrevisions-example-ns-main',
462 public function getHelpUrls() {
463 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Alldeletedrevisions';