Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiQueryDeletedRevisions.php
blobb79bd0803e28d785b71ebd04d939b84a86948ae1
1 <?php
2 /**
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
23 * @file
26 namespace MediaWiki\Api;
28 use MediaWiki\Cache\LinkBatchFactory;
29 use MediaWiki\ChangeTags\ChangeTagsStore;
30 use MediaWiki\CommentFormatter\CommentFormatter;
31 use MediaWiki\Content\IContentHandlerFactory;
32 use MediaWiki\Content\Renderer\ContentRenderer;
33 use MediaWiki\Content\Transform\ContentTransformer;
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;
46 /**
47 * Query module to enumerate deleted revisions for pages.
49 * @ingroup API
51 class ApiQueryDeletedRevisions extends ApiQueryRevisionsBase {
53 private RevisionStore $revisionStore;
54 private NameTableStore $changeTagDefStore;
55 private ChangeTagsStore $changeTagsStore;
56 private LinkBatchFactory $linkBatchFactory;
58 public function __construct(
59 ApiQuery $query,
60 string $moduleName,
61 RevisionStore $revisionStore,
62 IContentHandlerFactory $contentHandlerFactory,
63 ParserFactory $parserFactory,
64 SlotRoleRegistry $slotRoleRegistry,
65 NameTableStore $changeTagDefStore,
66 ChangeTagsStore $changeTagsStore,
67 LinkBatchFactory $linkBatchFactory,
68 ContentRenderer $contentRenderer,
69 ContentTransformer $contentTransformer,
70 CommentFormatter $commentFormatter,
71 TempUserCreator $tempUserCreator,
72 UserFactory $userFactory
73 ) {
74 parent::__construct(
75 $query,
76 $moduleName,
77 'drv',
78 $revisionStore,
79 $contentHandlerFactory,
80 $parserFactory,
81 $slotRoleRegistry,
82 $contentRenderer,
83 $contentTransformer,
84 $commentFormatter,
85 $tempUserCreator,
86 $userFactory
88 $this->revisionStore = $revisionStore;
89 $this->changeTagDefStore = $changeTagDefStore;
90 $this->changeTagsStore = $changeTagsStore;
91 $this->linkBatchFactory = $linkBatchFactory;
94 protected function run( ?ApiPageSet $resultPageSet = null ) {
95 $pageSet = $this->getPageSet();
96 $pageMap = $pageSet->getGoodAndMissingTitlesByNamespace();
97 $pageCount = count( $pageSet->getGoodAndMissingPages() );
98 $revCount = $pageSet->getRevisionCount();
99 if ( $revCount === 0 && $pageCount === 0 ) {
100 // Nothing to do
101 return;
103 if ( $revCount !== 0 && count( $pageSet->getDeletedRevisionIDs() ) === 0 ) {
104 // Nothing to do, revisions were supplied but none are deleted
105 return;
108 $params = $this->extractRequestParams( false );
110 $db = $this->getDB();
112 $this->requireMaxOneParameter( $params, 'user', 'excludeuser' );
114 if ( $resultPageSet === null ) {
115 $this->parseParameters( $params );
116 $arQuery = $this->revisionStore->getArchiveQueryInfo();
117 $this->addTables( $arQuery['tables'] );
118 $this->addFields( $arQuery['fields'] );
119 $this->addJoinConds( $arQuery['joins'] );
120 $this->addFields( [ 'ar_title', 'ar_namespace' ] );
121 } else {
122 $this->limit = $this->getParameter( 'limit' ) ?: 10;
123 $this->addTables( 'archive' );
124 $this->addFields( [ 'ar_title', 'ar_namespace', 'ar_timestamp', 'ar_rev_id', 'ar_id' ] );
127 if ( $this->fld_tags ) {
128 $this->addFields( [
129 'ts_tags' => $this->changeTagsStore->makeTagSummarySubquery( 'archive' )
130 ] );
133 if ( $params['tag'] !== null ) {
134 $this->addTables( 'change_tag' );
135 $this->addJoinConds(
136 [ 'change_tag' => [ 'JOIN', [ 'ar_rev_id=ct_rev_id' ] ] ]
138 try {
139 $this->addWhereFld( 'ct_tag_id', $this->changeTagDefStore->getId( $params['tag'] ) );
140 } catch ( NameTableAccessException $exception ) {
141 // Return nothing.
142 $this->addWhere( '1=0' );
146 // This means stricter restrictions
147 if ( ( $this->fld_comment || $this->fld_parsedcomment ) &&
148 !$this->getAuthority()->isAllowed( 'deletedhistory' )
150 $this->dieWithError( 'apierror-cantview-deleted-comment', 'permissiondenied' );
152 if ( $this->fetchContent && !$this->getAuthority()->isAllowedAny( 'deletedtext', 'undelete' ) ) {
153 $this->dieWithError( 'apierror-cantview-deleted-revision-content', 'permissiondenied' );
156 $dir = $params['dir'];
158 if ( $revCount !== 0 ) {
159 $this->addWhere( [
160 'ar_rev_id' => array_keys( $pageSet->getDeletedRevisionIDs() )
161 ] );
162 } else {
163 // We need a custom WHERE clause that matches all titles.
164 $lb = $this->linkBatchFactory->newLinkBatch( $pageSet->getGoodAndMissingPages() );
165 $where = $lb->constructSet( 'ar', $db );
166 $this->addWhere( $where );
169 if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
170 // In the non-generator case, the actor join will already be present.
171 if ( $resultPageSet !== null ) {
172 $this->addTables( 'actor' );
173 $this->addJoinConds( [ 'actor' => [ 'JOIN', 'actor_id=ar_actor' ] ] );
175 if ( $params['user'] !== null ) {
176 $this->addWhereFld( 'actor_name', $params['user'] );
177 } elseif ( $params['excludeuser'] !== null ) {
178 $this->addWhere( $db->expr( 'actor_name', '!=', $params['excludeuser'] ) );
182 if ( $params['user'] !== null || $params['excludeuser'] !== null ) {
183 // Paranoia: avoid brute force searches (T19342)
184 if ( !$this->getAuthority()->isAllowed( 'deletedhistory' ) ) {
185 $bitmask = RevisionRecord::DELETED_USER;
186 } elseif ( !$this->getAuthority()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
187 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
188 } else {
189 $bitmask = 0;
191 if ( $bitmask ) {
192 $this->addWhere( $db->bitAnd( 'ar_deleted', $bitmask ) . " != $bitmask" );
196 if ( $params['continue'] !== null ) {
197 $op = ( $dir == 'newer' ? '>=' : '<=' );
198 if ( $revCount !== 0 ) {
199 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'int' ] );
200 $this->addWhere( $db->buildComparison( $op, [
201 'ar_rev_id' => $cont[0],
202 'ar_id' => $cont[1],
203 ] ) );
204 } else {
205 $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'int', 'string', 'timestamp', 'int' ] );
206 $this->addWhere( $db->buildComparison( $op, [
207 'ar_namespace' => $cont[0],
208 'ar_title' => $cont[1],
209 'ar_timestamp' => $db->timestamp( $cont[2] ),
210 'ar_id' => $cont[3],
211 ] ) );
215 $this->addOption( 'LIMIT', $this->limit + 1 );
217 if ( $revCount !== 0 ) {
218 // Sort by ar_rev_id when querying by ar_rev_id
219 $this->addWhereRange( 'ar_rev_id', $dir, null, null );
220 } else {
221 // Sort by ns and title in the same order as timestamp for efficiency
222 // But only when not already unique in the query
223 if ( count( $pageMap ) > 1 ) {
224 $this->addWhereRange( 'ar_namespace', $dir, null, null );
226 $oneTitle = key( reset( $pageMap ) );
227 foreach ( $pageMap as $pages ) {
228 if ( count( $pages ) > 1 || key( $pages ) !== $oneTitle ) {
229 $this->addWhereRange( 'ar_title', $dir, null, null );
230 break;
233 $this->addTimestampWhereRange( 'ar_timestamp', $dir, $params['start'], $params['end'] );
235 // Include in ORDER BY for uniqueness
236 $this->addWhereRange( 'ar_id', $dir, null, null );
238 $res = $this->select( __METHOD__ );
239 $count = 0;
240 $generated = [];
241 foreach ( $res as $row ) {
242 if ( ++$count > $this->limit ) {
243 // We've had enough
244 $this->setContinueEnumParameter( 'continue',
245 $revCount
246 ? "$row->ar_rev_id|$row->ar_id"
247 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
249 break;
252 if ( $resultPageSet !== null ) {
253 $generated[] = $row->ar_rev_id;
254 } else {
255 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
256 // Was it converted?
257 $title = Title::makeTitle( $row->ar_namespace, $row->ar_title );
258 $converted = $pageSet->getConvertedTitles();
259 if ( $title && isset( $converted[$title->getPrefixedText()] ) ) {
260 $title = Title::newFromText( $converted[$title->getPrefixedText()] );
261 if ( $title && isset( $pageMap[$title->getNamespace()][$title->getDBkey()] ) ) {
262 $pageMap[$row->ar_namespace][$row->ar_title] =
263 $pageMap[$title->getNamespace()][$title->getDBkey()];
267 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
268 ApiBase::dieDebug(
269 __METHOD__,
270 "Found row in archive (ar_id={$row->ar_id}) that didn't get processed by ApiPageSet"
274 $fit = $this->addPageSubItem(
275 $pageMap[$row->ar_namespace][$row->ar_title],
276 $this->extractRevisionInfo( $this->revisionStore->newRevisionFromArchiveRow( $row ), $row ),
277 'rev'
279 if ( !$fit ) {
280 $this->setContinueEnumParameter( 'continue',
281 $revCount
282 ? "$row->ar_rev_id|$row->ar_id"
283 : "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
285 break;
290 if ( $resultPageSet !== null ) {
291 $resultPageSet->populateFromRevisionIDs( $generated );
295 public function getAllowedParams() {
296 return parent::getAllowedParams() + [
297 'start' => [
298 ParamValidator::PARAM_TYPE => 'timestamp',
300 'end' => [
301 ParamValidator::PARAM_TYPE => 'timestamp',
303 'dir' => [
304 ParamValidator::PARAM_TYPE => [
305 'newer',
306 'older'
308 ParamValidator::PARAM_DEFAULT => 'older',
309 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
310 ApiBase::PARAM_HELP_MSG_PER_VALUE => [
311 'newer' => 'api-help-paramvalue-direction-newer',
312 'older' => 'api-help-paramvalue-direction-older',
315 'tag' => null,
316 'user' => [
317 ParamValidator::PARAM_TYPE => 'user',
318 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'temp', 'id', 'interwiki' ],
320 'excludeuser' => [
321 ParamValidator::PARAM_TYPE => 'user',
322 UserDef::PARAM_ALLOWED_USER_TYPES => [ 'name', 'ip', 'temp', 'id', 'interwiki' ],
324 'continue' => [
325 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
330 protected function getExamplesMessages() {
331 $title = Title::newMainPage();
332 $talkTitle = $title->getTalkPageIfDefined();
333 $examples = [
334 'action=query&prop=deletedrevisions&revids=123456'
335 => 'apihelp-query+deletedrevisions-example-revids',
338 if ( $talkTitle ) {
339 $title = rawurlencode( $title->getPrefixedText() );
340 $talkTitle = rawurlencode( $talkTitle->getPrefixedText() );
341 $examples["action=query&prop=deletedrevisions&titles={$title}|{$talkTitle}&" .
342 'drvslots=*&drvprop=user|comment|content'] = 'apihelp-query+deletedrevisions-example-titles';
345 return $examples;
348 public function getHelpUrls() {
349 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevisions';
353 /** @deprecated class alias since 1.43 */
354 class_alias( ApiQueryDeletedRevisions::class, 'ApiQueryDeletedRevisions' );