3 * Copyright © 2014 Wikimedia Foundation and contributors
5 * Heavily based on ApiQueryDeletedrevs,
6 * Copyright © 2007 Roan Kattouw <roan.kattouw@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
26 namespace MediaWiki\Api
;
28 use MediaWiki\Cache\LinkBatchFactory
;
29 use MediaWiki\CommentFormatter\CommentFormatter
;
30 use MediaWiki\Content\IContentHandlerFactory
;
31 use MediaWiki\Content\Renderer\ContentRenderer
;
32 use MediaWiki\Content\Transform\ContentTransformer
;
33 use MediaWiki\MediaWikiServices
;
34 use MediaWiki\ParamValidator\TypeDef\UserDef
;
35 use MediaWiki\Parser\ParserFactory
;
36 use MediaWiki\Revision\RevisionRecord
;
37 use MediaWiki\Revision\RevisionStore
;
38 use MediaWiki\Revision\SlotRoleRegistry
;
39 use MediaWiki\Storage\NameTableAccessException
;
40 use MediaWiki\Storage\NameTableStore
;
41 use MediaWiki\Title\Title
;
42 use MediaWiki\User\TempUser\TempUserCreator
;
43 use MediaWiki\User\UserFactory
;
44 use Wikimedia\ParamValidator\ParamValidator
;
47 * Query module to enumerate deleted revisions for pages.
51 class ApiQueryDeletedRevisions
extends ApiQueryRevisionsBase
{
53 private RevisionStore
$revisionStore;
54 private NameTableStore
$changeTagDefStore;
55 private LinkBatchFactory
$linkBatchFactory;
57 public function __construct(
60 RevisionStore
$revisionStore,
61 IContentHandlerFactory
$contentHandlerFactory,
62 ParserFactory
$parserFactory,
63 SlotRoleRegistry
$slotRoleRegistry,
64 NameTableStore
$changeTagDefStore,
65 LinkBatchFactory
$linkBatchFactory,
66 ContentRenderer
$contentRenderer,
67 ContentTransformer
$contentTransformer,
68 CommentFormatter
$commentFormatter,
69 TempUserCreator
$tempUserCreator,
70 UserFactory
$userFactory
77 $contentHandlerFactory,
86 $this->revisionStore
= $revisionStore;
87 $this->changeTagDefStore
= $changeTagDefStore;
88 $this->linkBatchFactory
= $linkBatchFactory;
91 protected function run( ?ApiPageSet
$resultPageSet = null ) {
92 $pageSet = $this->getPageSet();
93 $pageMap = $pageSet->getGoodAndMissingTitlesByNamespace();
94 $pageCount = count( $pageSet->getGoodAndMissingPages() );
95 $revCount = $pageSet->getRevisionCount();
96 if ( $revCount === 0 && $pageCount === 0 ) {
100 if ( $revCount !== 0 && count( $pageSet->getDeletedRevisionIDs() ) === 0 ) {
101 // Nothing to do, revisions were supplied but none are deleted
105 $params = $this->extractRequestParams( false );
107 $db = $this->getDB();
109 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
111 if ( $resultPageSet === null ) {
112 $this->parseParameters( $params );
113 $arQuery = $this->revisionStore
->getArchiveQueryInfo();
114 $this->addTables( $arQuery['tables'] );
115 $this->addFields( $arQuery['fields'] );
116 $this->addJoinConds( $arQuery['joins'] );
117 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
119 $this->limit
= $this->getParameter( 'limit' ) ?
: 10;
120 $this->addTables( 'archive' );
121 $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
124 if ( $this->fld_tags
) {
126 'ts_tags' => MediaWikiServices
::getInstance()->getChangeTagsStore()
127 ->makeTagSummarySubquery( 'archive' )
131 if ( $params['tag'] !== null ) {
132 $this->addTables( 'change_tag' );
134 [ 'change_tag' => [ 'JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
137 $this->addWhereFld( 'ct_tag_id', $this->changeTagDefStore
->getId( $params['tag'] ) );
138 } catch ( NameTableAccessException
$exception ) {
140 $this->addWhere( '1=0' );
144 // This means stricter restrictions
145 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) &&
146 !$this->getAuthority()->isAllowed( 'deletedhistory' )
148 $this->dieWithError( 'apierror-cantview-deleted-comment', 'permissiondenied' );
150 if ( $this->fetchContent
&& !$this->getAuthority()->isAllowedAny( 'deletedtext', 'undelete' ) ) {
151 $this->dieWithError( 'apierror-cantview-deleted-revision-content', 'permissiondenied' );
154 $dir = $params['dir'];
156 if ( $revCount !== 0 ) {
158 'ar_rev_id' => array_keys( $pageSet->getDeletedRevisionIDs() )
161 // We need a custom WHERE clause that matches all titles.
162 $lb = $this->linkBatchFactory
->newLinkBatch( $pageSet->getGoodAndMissingPages() );
163 $where = $lb->constructSet( 'ar', $db );
164 $this->addWhere( $where );
167 if ( $params['user'] !== null ||
$params['excludeuser'] !== null ) {
168 // In the non-generator case, the actor join will already be present.
169 if ( $resultPageSet !== null ) {
170 $this->addTables( 'actor' );
171 $this->addJoinConds( [ 'actor' => [ 'JOIN', 'actor_id=ar_actor' ] ] );
173 if ( $params['user'] !== null ) {
174 $this->addWhereFld( 'actor_name', $params['user'] );
175 } elseif ( $params['excludeuser'] !== null ) {
176 $this->addWhere( $db->expr( 'actor_name', '!=', $params['excludeuser'] ) );
180 if ( $params['user'] !== null ||
$params['excludeuser'] !== null ) {
181 // Paranoia: avoid brute force searches (T19342)
182 if ( !$this->getAuthority()->isAllowed( 'deletedhistory' ) ) {
183 $bitmask = RevisionRecord
::DELETED_USER
;
184 } elseif ( !$this->getAuthority()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
185 $bitmask = RevisionRecord
::DELETED_USER | RevisionRecord
::DELETED_RESTRICTED
;
190 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
194 if ( $params['continue'] !== null ) {
195 $op = ( $dir == 'newer' ?
'>=' : '<=' );
196 if ( $revCount !== 0 ) {
197 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'int' ] );
198 $this->addWhere( $db->buildComparison( $op, [
199 'ar_rev_id' => $cont[0],
203 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string', 'timestamp', 'int' ] );
204 $this->addWhere( $db->buildComparison( $op, [
205 'ar_namespace' => $cont[0],
206 'ar_title' => $cont[1],
207 'ar_timestamp' => $db->timestamp( $cont[2] ),
213 $this->addOption( 'LIMIT', $this->limit +
1 );
215 if ( $revCount !== 0 ) {
216 // Sort by ar_rev_id when querying by ar_rev_id
217 $this->addWhereRange( 'ar_rev_id', $dir, null, null );
219 // Sort by ns and title in the same order as timestamp for efficiency
220 // But only when not already unique in the query
221 if ( count( $pageMap ) > 1 ) {
222 $this->addWhereRange( 'ar_namespace', $dir, null, null );
224 $oneTitle = key( reset( $pageMap ) );
225 foreach ( $pageMap as $pages ) {
226 if ( count( $pages ) > 1 ||
key( $pages ) !== $oneTitle ) {
227 $this->addWhereRange( 'ar_title', $dir, null, null );
231 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
233 // Include in ORDER BY for uniqueness
234 $this->addWhereRange( 'ar_id', $dir, null, null );
236 $res = $this->select( __METHOD__
);
239 foreach ( $res as $row ) {
240 if ( ++
$count > $this->limit
) {
242 $this->setContinueEnumParameter( 'continue',
244 ?
"$row->ar_rev_id|$row->ar_id"
245 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
250 if ( $resultPageSet !== null ) {
251 $generated[] = $row->ar_rev_id
;
253 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
255 $title = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
256 $converted = $pageSet->getConvertedTitles();
257 if ( $title && isset( $converted[$title->getPrefixedText()] ) ) {
258 $title = Title
::newFromText( $converted[$title->getPrefixedText()] );
259 if ( $title && isset( $pageMap[$title->getNamespace()][$title->getDBkey()] ) ) {
260 $pageMap[$row->ar_namespace
][$row->ar_title
] =
261 $pageMap[$title->getNamespace()][$title->getDBkey()];
265 if ( !isset( $pageMap[$row->ar_namespace
][$row->ar_title
] ) ) {
268 "Found row in archive (ar_id={$row->ar_id}) that didn't get processed by ApiPageSet"
272 $fit = $this->addPageSubItem(
273 $pageMap[$row->ar_namespace
][$row->ar_title
],
274 $this->extractRevisionInfo( $this->revisionStore
->newRevisionFromArchiveRow( $row ), $row ),
278 $this->setContinueEnumParameter( 'continue',
280 ?
"$row->ar_rev_id|$row->ar_id"
281 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
288 if ( $resultPageSet !== null ) {
289 $resultPageSet->populateFromRevisionIDs( $generated );
293 public function getAllowedParams() {
294 return parent
::getAllowedParams() +
[
296 ParamValidator
::PARAM_TYPE
=> 'timestamp',
299 ParamValidator
::PARAM_TYPE
=> 'timestamp',
302 ParamValidator
::PARAM_TYPE
=> [
306 ParamValidator
::PARAM_DEFAULT
=> 'older',
307 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
308 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [
309 'newer' => 'api-help-paramvalue-direction-newer',
310 'older' => 'api-help-paramvalue-direction-older',
315 ParamValidator
::PARAM_TYPE
=> 'user',
316 UserDef
::PARAM_ALLOWED_USER_TYPES
=> [ 'name', 'ip', 'temp', 'id', 'interwiki' ],
319 ParamValidator
::PARAM_TYPE
=> 'user',
320 UserDef
::PARAM_ALLOWED_USER_TYPES
=> [ 'name', 'ip', 'temp', 'id', 'interwiki' ],
323 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
328 protected function getExamplesMessages() {
329 $title = Title
::newMainPage();
330 $talkTitle = $title->getTalkPageIfDefined();
332 'action=query&prop=deletedrevisions&revids=123456'
333 => 'apihelp-query+deletedrevisions-example-revids',
337 $title = rawurlencode( $title->getPrefixedText() );
338 $talkTitle = rawurlencode( $talkTitle->getPrefixedText() );
339 $examples["action=query&prop=deletedrevisions&titles={$title}|{$talkTitle}&" .
340 'drvslots=*&drvprop=user|comment|content'] = 'apihelp-query+deletedrevisions-example-titles';
346 public function getHelpUrls() {
347 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevisions';
351 /** @deprecated class alias since 1.43 */
352 class_alias( ApiQueryDeletedRevisions
::class, 'ApiQueryDeletedRevisions' );