Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / includes / api / ApiQueryAllDeletedRevisions.php
blob4e95f5b8ca52ae66dc5990334192c851b074eea2
1 <?php
2 /**
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
25 * @file
28 /**
29 * Query module to enumerate all deleted revisions.
31 * @ingroup API
33 class ApiQueryAllDeletedRevisions extends ApiQueryRevisionsBase {
35 public function __construct( ApiQuery $query, $moduleName ) {
36 parent::__construct( $query, $moduleName, 'adr' );
39 /**
40 * @param ApiPageSet $resultPageSet
41 * @return void
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' ) ) {
47 $this->dieUsage(
48 'You don\'t have permission to view deleted revision information',
49 'permissiondenied'
53 $db = $this->getDB();
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
63 $mode = 'all';
64 if ( !is_null( $params['user'] ) ) {
65 $mode = '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'",
73 'badparams' );
76 } else {
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'",
81 'badparams' );
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' ) );
91 } else {
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' );
98 $this->addJoinConds(
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' );
106 $this->addJoinConds(
107 array( 'change_tag' => array( 'INNER JOIN', array( 'ar_rev_id=ct_rev_id' ) ) )
109 $this->addWhereFld( 'ct_tag', $params['tag'] );
112 if ( $this->fetchContent ) {
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' );
119 $this->addJoinConds(
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' ) ) {
126 $this->dieUsage(
127 'You don\'t have permission to view deleted revision content',
128 'permissiondenied'
133 $dir = $params['dir'];
134 $miser_ns = null;
136 if ( $mode == 'all' ) {
137 if ( $params['namespace'] !== null ) {
138 $namespaces = $params['namespace'];
139 $this->addWhereFld( 'ar_namespace', $namespaces );
140 } else {
141 $namespaces = MWNamespace::getValidNamespaces();
144 // For from/to/prefix, we have to consider the potential
145 // transformations of the title in all specified namespaces.
146 // Generally there will be only one transformation, but wikis with
147 // some namespaces case-sensitive could have two.
148 if ( $params['from'] !== null || $params['to'] !== null ) {
149 $isDirNewer = ( $dir === 'newer' );
150 $after = ( $isDirNewer ? '>=' : '<=' );
151 $before = ( $isDirNewer ? '<=' : '>=' );
152 $where = array();
153 foreach ( $namespaces as $ns ) {
154 $w = array();
155 if ( $params['from'] !== null ) {
156 $w[] = 'ar_title' . $after .
157 $db->addQuotes( $this->titlePartToKey( $params['from'], $ns ) );
159 if ( $params['to'] !== null ) {
160 $w[] = 'ar_title' . $before .
161 $db->addQuotes( $this->titlePartToKey( $params['to'], $ns ) );
163 $w = $db->makeList( $w, LIST_AND );
164 $where[$w][] = $ns;
166 if ( count( $where ) == 1 ) {
167 $where = key( $where );
168 $this->addWhere( $where );
169 } else {
170 $where2 = array();
171 foreach ( $where as $w => $ns ) {
172 $where2[] = $db->makeList( array( $w, 'ar_namespace' => $ns ), LIST_AND );
174 $this->addWhere( $db->makeList( $where2, LIST_OR ) );
178 if ( isset( $params['prefix'] ) ) {
179 $where = array();
180 foreach ( $namespaces as $ns ) {
181 $w = 'ar_title' . $db->buildLike(
182 $this->titlePartToKey( $params['prefix'], $ns ),
183 $db->anyString() );
184 $where[$w][] = $ns;
186 if ( count( $where ) == 1 ) {
187 $where = key( $where );
188 $this->addWhere( $where );
189 } else {
190 $where2 = array();
191 foreach ( $where as $w => $ns ) {
192 $where2[] = $db->makeList( array( $w, 'ar_namespace' => $ns ), LIST_AND );
194 $this->addWhere( $db->makeList( $where2, LIST_OR ) );
197 } else {
198 if ( $this->getConfig()->get( 'MiserMode' ) ) {
199 $miser_ns = $params['namespace'];
200 } else {
201 $this->addWhereFld( 'ar_namespace', $params['namespace'] );
203 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
206 if ( !is_null( $params['user'] ) ) {
207 $this->addWhereFld( 'ar_user_text', $params['user'] );
208 } elseif ( !is_null( $params['excludeuser'] ) ) {
209 $this->addWhere( 'ar_user_text != ' .
210 $db->addQuotes( $params['excludeuser'] ) );
213 if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
214 // Paranoia: avoid brute force searches (bug 17342)
215 // (shouldn't be able to get here without 'deletedhistory', but
216 // check it again just in case)
217 if ( !$user->isAllowed( 'deletedhistory' ) ) {
218 $bitmask = Revision::DELETED_USER;
219 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
220 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
221 } else {
222 $bitmask = 0;
224 if ( $bitmask ) {
225 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
229 if ( !is_null( $params['continue'] ) ) {
230 $cont = explode( '|', $params['continue'] );
231 $op = ( $dir == 'newer' ? '>' : '<' );
232 if ( $mode == 'all' ) {
233 $this->dieContinueUsageIf( count( $cont ) != 4 );
234 $ns = intval( $cont[0] );
235 $this->dieContinueUsageIf( strval( $ns ) !== $cont[0] );
236 $title = $db->addQuotes( $cont[1] );
237 $ts = $db->addQuotes( $db->timestamp( $cont[2] ) );
238 $ar_id = (int)$cont[3];
239 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[3] );
240 $this->addWhere( "ar_namespace $op $ns OR " .
241 "(ar_namespace = $ns AND " .
242 "(ar_title $op $title OR " .
243 "(ar_title = $title AND " .
244 "(ar_timestamp $op $ts OR " .
245 "(ar_timestamp = $ts AND " .
246 "ar_id $op= $ar_id)))))" );
247 } else {
248 $this->dieContinueUsageIf( count( $cont ) != 2 );
249 $ts = $db->addQuotes( $db->timestamp( $cont[0] ) );
250 $ar_id = (int)$cont[1];
251 $this->dieContinueUsageIf( strval( $ar_id ) !== $cont[1] );
252 $this->addWhere( "ar_timestamp $op $ts OR " .
253 "(ar_timestamp = $ts AND " .
254 "ar_id $op= $ar_id)" );
258 $this->addOption( 'LIMIT', $this->limit + 1 );
260 $sort = ( $dir == 'newer' ? '' : ' DESC' );
261 $orderby = array();
262 if ( $mode == 'all' ) {
263 // Targeting index name_title_timestamp
264 if ( $params['namespace'] === null || count( array_unique( $params['namespace'] ) ) > 1 ) {
265 $orderby[] = "ar_namespace $sort";
267 $orderby[] = "ar_title $sort";
268 $orderby[] = "ar_timestamp $sort";
269 $orderby[] = "ar_id $sort";
270 } else {
271 // Targeting index usertext_timestamp
272 // 'user' is always constant.
273 $orderby[] = "ar_timestamp $sort";
274 $orderby[] = "ar_id $sort";
276 $this->addOption( 'ORDER BY', $orderby );
278 $res = $this->select( __METHOD__ );
279 $pageMap = array(); // Maps ns&title to array index
280 $count = 0;
281 $nextIndex = 0;
282 $generated = array();
283 foreach ( $res as $row ) {
284 if ( ++$count > $this->limit ) {
285 // We've had enough
286 if ( $mode == 'all' ) {
287 $this->setContinueEnumParameter( 'continue',
288 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
290 } else {
291 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
293 break;
296 // Miser mode namespace check
297 if ( $miser_ns !== null && !in_array( $row->ar_namespace, $miser_ns ) ) {
298 continue;
301 if ( $resultPageSet !== null ) {
302 if ( $params['generatetitles'] ) {
303 $key = "{$row->ar_namespace}:{$row->ar_title}";
304 if ( !isset( $generated[$key] ) ) {
305 $generated[$key] = Title::makeTitle( $row->ar_namespace, $row->ar_title );
307 } else {
308 $generated[] = $row->ar_rev_id;
310 } else {
311 $revision = Revision::newFromArchiveRow( $row );
312 $rev = $this->extractRevisionInfo( $revision, $row );
314 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
315 $index = $nextIndex++;
316 $pageMap[$row->ar_namespace][$row->ar_title] = $index;
317 $title = $revision->getTitle();
318 $a = array(
319 'pageid' => $title->getArticleID(),
320 'revisions' => array( $rev ),
322 $result->setIndexedTagName( $a['revisions'], 'rev' );
323 ApiQueryBase::addTitleInfo( $a, $title );
324 $fit = $result->addValue( array( 'query', $this->getModuleName() ), $index, $a );
325 } else {
326 $index = $pageMap[$row->ar_namespace][$row->ar_title];
327 $fit = $result->addValue(
328 array( 'query', $this->getModuleName(), $index, 'revisions' ),
329 null, $rev );
331 if ( !$fit ) {
332 if ( $mode == 'all' ) {
333 $this->setContinueEnumParameter( 'continue',
334 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
336 } else {
337 $this->setContinueEnumParameter( 'continue', "$row->ar_timestamp|$row->ar_id" );
339 break;
344 if ( $resultPageSet !== null ) {
345 if ( $params['generatetitles'] ) {
346 $resultPageSet->populateFromTitles( $generated );
347 } else {
348 $resultPageSet->populateFromRevisionIDs( $generated );
350 } else {
351 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
355 public function getAllowedParams() {
356 $ret = parent::getAllowedParams() + array(
357 'user' => array(
358 ApiBase::PARAM_TYPE => 'user'
360 'namespace' => array(
361 ApiBase::PARAM_ISMULTI => true,
362 ApiBase::PARAM_TYPE => 'namespace',
363 ApiBase::PARAM_DFLT => null,
365 'start' => array(
366 ApiBase::PARAM_TYPE => 'timestamp',
367 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'useronly' ) ),
369 'end' => array(
370 ApiBase::PARAM_TYPE => 'timestamp',
371 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'useronly' ) ),
373 'dir' => array(
374 ApiBase::PARAM_TYPE => array(
375 'newer',
376 'older'
378 ApiBase::PARAM_DFLT => 'older',
379 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
381 'from' => array(
382 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'nonuseronly' ) ),
384 'to' => array(
385 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'nonuseronly' ) ),
387 'prefix' => array(
388 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'nonuseronly' ) ),
390 'excludeuser' => array(
391 ApiBase::PARAM_TYPE => 'user',
392 ApiBase::PARAM_HELP_MSG_INFO => array( array( 'nonuseronly' ) ),
394 'tag' => null,
395 'continue' => array(
396 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
398 'generatetitles' => array(
399 ApiBase::PARAM_DFLT => false
403 if ( $this->getConfig()->get( 'MiserMode' ) ) {
404 $ret['user'][ApiBase::PARAM_HELP_MSG_APPEND] = array(
405 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
407 $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = array(
408 'apihelp-query+alldeletedrevisions-param-miser-user-namespace',
412 return $ret;
415 protected function getExamplesMessages() {
416 return array(
417 'action=query&list=alldeletedrevisions&adruser=Example&adrlimit=50'
418 => 'apihelp-query+alldeletedrevisions-example-user',
419 'action=query&list=alldeletedrevisions&adrdir=newer&adrlimit=50'
420 => 'apihelp-query+alldeletedrevisions-example-ns-main',
424 public function getHelpUrls() {
425 return 'https://www.mediawiki.org/wiki/API:Alldeletedrevisions';