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();
58 $titles = $pageSet->getTitles();
60 // This module operates in two modes:
61 // 'user': List deleted revs by a certain user
62 // 'all': List all deleted revs in NS
64 if ( !is_null( $params['user'] ) ) {
68 if ( $mode == 'user' ) {
69 foreach ( array( 'from', 'to', 'prefix', 'excludeuser' ) as $param ) {
70 if ( !is_null( $params[$param] ) ) {
71 $p = $this->getModulePrefix();
72 $this->dieUsage( "The '{$p}{$param}' parameter cannot be used with '{$p}user'",
77 foreach ( array( 'start', 'end' ) as $param ) {
78 if ( !is_null( $params[$param] ) ) {
79 $p = $this->getModulePrefix();
80 $this->dieUsage( "The '{$p}{$param}' parameter may only be used with '{$p}user'",
86 $this->addTables( 'archive' );
87 if ( $resultPageSet === null ) {
88 $this->parseParameters( $params );
89 $this->addFields( Revision
::selectArchiveFields() );
90 $this->addFields( array( 'ar_title', 'ar_namespace' ) );
92 $this->limit
= $this->getParameter( 'limit' ) ?
: 10;
93 $this->addFields( array( 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_rev_id', 'ar_id' ) );
96 if ( $this->fld_tags
) {
97 $this->addTables( 'tag_summary' );
99 array( 'tag_summary' => array( 'LEFT JOIN', array( 'ar_rev_id=ts_rev_id' ) ) )
101 $this->addFields( 'ts_tags' );
104 if ( !is_null( $params['tag'] ) ) {
105 $this->addTables( 'change_tag' );
107 array( 'change_tag' => array( 'INNER JOIN', array( 'ar_rev_id=ct_rev_id' ) ) )
109 $this->addWhereFld( 'ct_tag', $params['tag'] );
112 if ( $this->fld_content ||
!is_null( $this->diffto
) ||
!is_null( $this->difftotext
) ) {
113 // Modern MediaWiki has the content for deleted revs in the 'text'
114 // table using fields old_text and old_flags. But revisions deleted
115 // pre-1.5 store the content in the 'archive' table directly using
116 // fields ar_text and ar_flags, and no corresponding 'text' row. So
117 // we have to LEFT JOIN and fetch all four fields.
118 $this->addTables( 'text' );
120 array( 'text' => array( 'LEFT JOIN', array( 'ar_text_id=old_id' ) ) )
122 $this->addFields( array( 'ar_text', 'ar_flags', 'old_text', 'old_flags' ) );
124 // This also means stricter restrictions
125 if ( !$user->isAllowedAny( 'undelete', 'deletedtext' ) ) {
127 'You don\'t have permission to view deleted revision content',
133 $dir = $params['dir'];
136 if ( $mode == 'all' ) {
137 if ( $params['namespace'] !== null ) {
138 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
141 $from = $params['from'] === null
143 : $this->titlePartToKey( $params['from'], $params['namespace'] );
144 $to = $params['to'] === null
146 : $this->titlePartToKey( $params['to'], $params['namespace'] );
147 $this->addWhereRange( 'ar_title', $dir, $from, $to );
149 if ( isset( $params['prefix'] ) ) {
150 $this->addWhere( 'ar_title' . $db->buildLike(
151 $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
152 $db->anyString() ) );
155 if ( $this->getConfig()->get( 'MiserMode' ) ) {
156 $miser_ns = $params['namespace'];
158 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
160 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
163 if ( !is_null( $params['user'] ) ) {
164 $this->addWhereFld( 'ar_user_text', $params['user'] );
165 } elseif ( !is_null( $params['excludeuser'] ) ) {
166 $this->addWhere( 'ar_user_text != ' .
167 $db->addQuotes( $params['excludeuser'] ) );
170 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
171 // Paranoia: avoid brute force searches (bug 17342)
172 // (shouldn't be able to get here without 'deletedhistory', but
173 // check it again just in case)
174 if ( !$user->isAllowed( 'deletedhistory' ) ) {
175 $bitmask = Revision
::DELETED_USER
;
176 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
177 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
182 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
186 if ( !is_null( $params['continue'] ) ) {
187 $cont = explode( '|', $params['continue'] );
188 $op = ( $dir == 'newer' ?
'>' : '<' );
189 if ( $mode == 'all' ) {
190 $this->dieContinueUsageIf( count( $cont ) != 4 );
191 $ns = intval( $cont[0] );
192 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
193 $title = $db->addQuotes( $cont[1] );
194 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
195 $ar_id = (int)$cont[3];
196 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
197 $this->addWhere( "ar_namespace $op $ns OR " .
198 "(ar_namespace = $ns AND " .
199 "(ar_title $op $title OR " .
200 "(ar_title = $title AND " .
201 "(ar_timestamp $op $ts OR " .
202 "(ar_timestamp = $ts AND " .
203 "ar_id $op= $ar_id)))))" );
205 $this->dieContinueUsageIf( count( $cont ) != 2 );
206 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
207 $ar_id = (int)$cont[1];
208 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
209 $this->addWhere( "ar_timestamp $op $ts OR " .
210 "(ar_timestamp = $ts AND " .
211 "ar_id $op= $ar_id)" );
215 $this->addOption( 'LIMIT', $this->limit +
1 );
217 $sort = ( $dir == 'newer' ?
'' : ' DESC' );
219 if ( $mode == 'all' ) {
220 // Targeting index name_title_timestamp
221 if ( $params['namespace'] === null ||
count( array_unique( $params['namespace'] ) ) > 1 ) {
222 $orderby[] = "ar_namespace $sort";
224 $orderby[] = "ar_title $sort";
225 $orderby[] = "ar_timestamp $sort";
226 $orderby[] = "ar_id $sort";
228 // Targeting index usertext_timestamp
229 // 'user' is always constant.
230 $orderby[] = "ar_timestamp $sort";
231 $orderby[] = "ar_id $sort";
233 $this->addOption( 'ORDER BY', $orderby );
235 $res = $this->select( __METHOD__
);
236 $pageMap = array(); // Maps ns&title to array index
239 $generated = array();
240 foreach ( $res as $row ) {
241 if ( ++
$count > $this->limit
) {
243 if ( $mode == 'all' ) {
244 $this->setContinueEnumParameter( 'continue',
245 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
248 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
253 // Miser mode namespace check
254 if ( $miser_ns !== null && !in_array( $row->ar_namespace
, $miser_ns ) ) {
258 if ( $resultPageSet !== null ) {
259 if ( $params['generatetitles'] ) {
260 $key = "{$row->ar_namespace}:{$row->ar_title}";
261 if ( !isset( $generated[$key] ) ) {
262 $generated[$key] = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
265 $generated[] = $row->ar_rev_id
;
268 $revision = Revision
::newFromArchiveRow( $row );
269 $rev = $this->extractRevisionInfo( $revision, $row );
271 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
272 $index = $nextIndex++
;
273 $pageMap[$row->ar_namespace
][$row->ar_title
] = $index;
274 $title = $revision->getTitle();
276 'pageid' => $title->getArticleID(),
277 'revisions' => array( $rev ),
279 $result->setIndexedTagName( $a['revisions'], 'rev' );
280 ApiQueryBase
::addTitleInfo( $a, $title );
281 $fit = $result->addValue( array( 'query', $this->getModuleName() ), $index, $a );
283 $index = $pageMap[$row->ar_namespace
][$row->ar_title
];
284 $fit = $result->addValue(
285 array( 'query', $this->getModuleName(), $index, 'revisions' ),
289 if ( $mode == 'all' ) {
290 $this->setContinueEnumParameter( 'continue',
291 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
294 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
301 if ( $resultPageSet !== null ) {
302 if ( $params['generatetitles'] ) {
303 $resultPageSet->populateFromTitles( $generated );
305 $resultPageSet->populateFromRevisionIDs( $generated );
308 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
312 public function getAllowedParams() {
313 $ret = parent
::getAllowedParams() +
array(
315 ApiBase
::PARAM_TYPE
=> 'user'
317 'namespace' => array(
318 ApiBase
::PARAM_ISMULTI
=> true,
319 ApiBase
::PARAM_TYPE
=> 'namespace',
320 ApiBase
::PARAM_DFLT
=> null,
323 ApiBase
::PARAM_TYPE
=> 'timestamp',
324 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'useronly' ) ),
327 ApiBase
::PARAM_TYPE
=> 'timestamp',
328 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'useronly' ) ),
331 ApiBase
::PARAM_TYPE
=> array(
335 ApiBase
::PARAM_DFLT
=> 'older',
336 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
339 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'nonuseronly' ) ),
342 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'nonuseronly' ) ),
345 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'nonuseronly' ) ),
347 'excludeuser' => array(
348 ApiBase
::PARAM_TYPE
=> 'user',
349 ApiBase
::PARAM_HELP_MSG_INFO
=> array( array( 'nonuseronly' ) ),
353 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
355 'generatetitles' => array(
356 ApiBase
::PARAM_DFLT
=> false
360 if ( $this->getConfig()->get( 'MiserMode' ) ) {
361 $ret['user'][ApiBase
::PARAM_HELP_MSG_APPEND
] = array(
362 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
364 $ret['namespace'][ApiBase
::PARAM_HELP_MSG_APPEND
] = array(
365 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
372 protected function getExamplesMessages() {
374 'action=query&list=alldeletedrevisions&adruser=Example&adrlimit=50'
375 => 'apihelp-query+alldeletedrevisions-example-user',
376 'action=query&list=alldeletedrevisions&adrdir=newer&adrlimit=50'
377 => 'apihelp-query+alldeletedrevisions-example-ns-main',
381 public function getHelpUrls() {
382 return 'https://www.mediawiki.org/wiki/API:Alldeletedrevisions';