28use Wikimedia\Timestamp\TimestampFormat as TS;
55 parent::__construct( $query, $moduleName,
'dr' );
56 $this->commentStore = $commentStore;
57 $this->commentFormatter = $commentFormatter;
58 $this->revisionStore = $revisionStore;
59 $this->changeTagDefStore = $changeTagDefStore;
60 $this->changeTagsStore = $changeTagsStore;
61 $this->linkBatchFactory = $linkBatchFactory;
68 $this->
addDeprecation(
'apiwarn-deprecation-deletedrevs',
'action=query&list=deletedrevs' );
73 $prop = array_fill_keys( $params[
'prop'],
true );
74 $fld_parentid = isset( $prop[
'parentid'] );
75 $fld_revid = isset( $prop[
'revid'] );
76 $fld_user = isset( $prop[
'user'] );
77 $fld_userid = isset( $prop[
'userid'] );
78 $fld_comment = isset( $prop[
'comment'] );
79 $fld_parsedcomment = isset( $prop[
'parsedcomment'] );
80 $fld_minor = isset( $prop[
'minor'] );
81 $fld_len = isset( $prop[
'len'] );
82 $fld_sha1 = isset( $prop[
'sha1'] );
83 $fld_content = isset( $prop[
'content'] );
84 $fld_token = isset( $prop[
'token'] );
85 $fld_tags = isset( $prop[
'tags'] );
98 $titles = $pageSet->getPages();
105 if ( count( $titles ) > 0 ) {
107 } elseif ( $params[
'user'] !==
null ) {
111 if ( $mode ==
'revs' || $mode ==
'user' ) {
113 foreach ( [
'from',
'to',
'prefix', ] as $p ) {
114 if ( $params[$p] !==
null ) {
115 $this->
dieWithError( [
'apierror-deletedrevs-param-not-1-2', $p ],
'badparams' );
119 foreach ( [
'start',
'end' ] as $p ) {
120 if ( $params[$p] !==
null ) {
121 $this->
dieWithError( [
'apierror-deletedrevs-param-not-3', $p ],
'badparams' );
126 if ( $params[
'user'] !==
null && $params[
'excludeuser'] !==
null ) {
127 $this->
dieWithError(
'user and excludeuser cannot be used together',
'badparams' );
130 $arQuery = $this->revisionStore->getArchiveQueryInfo();
134 $this->
addFields( [
'ar_title',
'ar_namespace' ] );
138 'ts_tags' => $this->changeTagsStore->makeTagSummarySubquery(
'archive' )
143 $pairExpr = $db->buildGroupConcat(
144 $db->buildConcat( [
'sr.role_name', $db->addQuotes(
':' ),
'c.content_sha1' ] ),
147 $sha1Subquery = $db->newSelectQueryBuilder()
151 'ar_slot_pairs' => $pairExpr,
154 ->join(
'slots',
's', [
'ar_rev_id = s.slot_revision_id' ] )
155 ->join(
'content',
'c', [
's.slot_content_id = c.content_id' ] )
156 ->join(
'slot_roles',
'sr', [
's.slot_role_id = sr.role_id' ] )
157 ->groupBy( [
'ar_rev_id',
'ar_deleted' ] )
158 ->caller( __METHOD__ )
163 'ar_deleted' =>
'arsha1.ar_deleted',
164 'ar_slot_pairs' =>
'arsha1.ar_slot_pairs'
166 $this->
addJoinConds( [
'arsha1' => [
'LEFT JOIN', [
'ar_rev_id = arsha1.ar_rev_id' ] ] ] );
169 if ( $params[
'tag'] !==
null ) {
172 [
'change_tag' => [
'JOIN', [
'ar_rev_id=ct_rev_id' ] ] ]
175 $this->
addWhereFld(
'ct_tag_id', $this->changeTagDefStore->getId( $params[
'tag'] ) );
183 if ( $fld_content ) {
190 $limit = $params[
'limit'];
192 if ( $limit ==
'max' ) {
193 $limit = $this->
getMain()->canApiHighLimits() ? $botMax : $userMax;
197 $limit = $this->
getMain()->getParamValidator()->validateValue(
198 $this,
'limit', $limit, [
199 ParamValidator::PARAM_TYPE =>
'limit',
200 IntegerDef::PARAM_MIN => 1,
201 IntegerDef::PARAM_MAX => $userMax,
202 IntegerDef::PARAM_MAX2 => $botMax,
203 IntegerDef::PARAM_IGNORE_RANGE =>
true,
212 $dir = $params[
'dir'];
215 if ( $mode ==
'revs' ) {
216 $lb = $this->linkBatchFactory->newLinkBatch( $titles );
217 $where = $lb->constructSet(
'ar', $db );
219 } elseif ( $mode ==
'all' ) {
220 $this->
addWhereFld(
'ar_namespace', $params[
'namespace'] );
222 $from = $params[
'from'] ===
null
225 $to = $params[
'to'] ===
null
230 if ( isset( $params[
'prefix'] ) ) {
244 if ( $params[
'user'] !==
null ) {
246 $this->
addWhereFld(
'actor_name', $params[
'user'] );
247 } elseif ( $params[
'excludeuser'] !==
null ) {
248 $this->
addWhere( $db->expr(
'actor_name',
'!=', $params[
'excludeuser'] ) );
251 if ( $params[
'user'] !==
null || $params[
'excludeuser'] !==
null ) {
255 if ( !$this->
getAuthority()->isAllowed(
'deletedhistory' ) ) {
256 $bitmask = RevisionRecord::DELETED_USER;
257 } elseif ( !$this->
getAuthority()->isAllowedAny(
'suppressrevision',
'viewsuppressed' ) ) {
258 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
263 $this->
addWhere( $db->bitAnd(
'ar_deleted', $bitmask ) .
" != $bitmask" );
267 if ( $params[
'continue'] !==
null ) {
268 $op = ( $dir ==
'newer' ?
'>=' :
'<=' );
269 if ( $mode ==
'all' || $mode ==
'revs' ) {
271 $this->
addWhere( $db->buildComparison( $op, [
272 'ar_namespace' => $cont[0],
273 'ar_title' => $cont[1],
274 'ar_timestamp' => $db->timestamp( $cont[2] ),
279 $this->
addWhere( $db->buildComparison( $op, [
280 'ar_timestamp' => $db->timestamp( $cont[0] ),
287 if ( $mode ==
'all' ) {
288 if ( $params[
'unique'] ) {
290 $this->
addOption(
'GROUP BY',
'ar_title' );
292 $sort = ( $dir ==
'newer' ?
'' :
' DESC' );
295 'ar_timestamp' . $sort,
300 if ( $mode ==
'revs' ) {
309 $res = $this->
select( __METHOD__ );
311 $formattedComments = [];
312 if ( $fld_parsedcomment ) {
313 $formattedComments = $this->commentFormatter->formatItems(
314 $this->commentFormatter->rows( $res )
315 ->indexField(
'ar_id' )
316 ->commentKey(
'ar_comment' )
317 ->namespaceField(
'ar_namespace' )
318 ->titleField(
'ar_title' )
325 foreach ( $res as $row ) {
326 if ( ++$count > $limit ) {
328 if ( $mode ==
'all' || $mode ==
'revs' ) {
330 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
341 $rev[
'timestamp'] =
wfTimestamp( TS::ISO_8601, $row->ar_timestamp );
343 $rev[
'revid'] = (int)$row->ar_rev_id;
345 if ( $fld_parentid && $row->ar_parent_id !==
null ) {
346 $rev[
'parentid'] = (int)$row->ar_parent_id;
348 if ( $fld_user || $fld_userid ) {
349 if ( $row->ar_deleted & RevisionRecord::DELETED_USER ) {
350 $rev[
'userhidden'] =
true;
353 if ( RevisionRecord::userCanBitfield(
355 RevisionRecord::DELETED_USER,
359 $rev[
'user'] = $row->ar_user_text;
362 $rev[
'userid'] = (int)$row->ar_user;
367 if ( $fld_comment || $fld_parsedcomment ) {
368 if ( $row->ar_deleted & RevisionRecord::DELETED_COMMENT ) {
369 $rev[
'commenthidden'] =
true;
372 if ( RevisionRecord::userCanBitfield(
374 RevisionRecord::DELETED_COMMENT,
377 $comment = $this->commentStore->getComment(
'ar_comment', $row )->text;
378 if ( $fld_comment ) {
379 $rev[
'comment'] = $comment;
381 if ( $fld_parsedcomment ) {
382 $rev[
'parsedcomment'] = $formattedComments[$row->ar_id];
388 $rev[
'minor'] = $row->ar_minor_edit == 1;
391 $rev[
'len'] = $row->ar_len;
394 if ( $row->ar_deleted & RevisionRecord::DELETED_TEXT ) {
395 $rev[
'sha1hidden'] =
true;
398 if ( RevisionRecord::userCanBitfield(
400 RevisionRecord::DELETED_TEXT,
403 if ( $row->ar_slot_pairs !==
null ) {
404 $combinedBase36 =
'';
405 if ( $row->ar_slot_pairs !==
'' ) {
406 $items = explode(
',', $row->ar_slot_pairs );
408 foreach ( $items as $item ) {
409 $parts = explode(
':', $item );
410 $slotHashes[$parts[0]] = $parts[1];
412 ksort( $slotHashes );
415 foreach ( $slotHashes as $slotHash ) {
416 $accu = $accu ===
null
418 : SlotRecord::base36Sha1( $accu . $slotHash );
420 $combinedBase36 = $accu ?? SlotRecord::base36Sha1(
'' );
423 $rev[
'sha1'] = $combinedBase36 !==
''
424 ? \Wikimedia\base_convert( $combinedBase36, 36, 16, 40 )
429 if ( $fld_content ) {
430 if ( $row->ar_deleted & RevisionRecord::DELETED_TEXT ) {
431 $rev[
'texthidden'] =
true;
434 if ( RevisionRecord::userCanBitfield(
436 RevisionRecord::DELETED_TEXT,
440 $this->revisionStore->newRevisionFromArchiveRow( $row )
441 ->getContent( SlotRecord::MAIN )->serialize() );
446 if ( $row->ts_tags ) {
447 $tags = explode(
',', $row->ts_tags );
449 $rev[
'tags'] = $tags;
455 if ( $anyHidden && ( $row->ar_deleted & RevisionRecord::DELETED_RESTRICTED ) ) {
456 $rev[
'suppressed'] =
true;
459 if ( !isset( $pageMap[$row->ar_namespace][$row->ar_title] ) ) {
460 $pageID = $newPageID++;
461 $pageMap[$row->ar_namespace][$row->ar_title] = $pageID;
462 $a = [
'revisions' => [ $rev ] ];
468 $a[
'token'] = $token;
470 $fit = $result->addValue( [
'query', $this->
getModuleName() ], $pageID, $a );
472 $pageID = $pageMap[$row->ar_namespace][$row->ar_title];
473 $fit = $result->addValue(
478 if ( $mode ==
'all' || $mode ==
'revs' ) {
480 "$row->ar_namespace|$row->ar_title|$row->ar_timestamp|$row->ar_id"
488 $result->addIndexedTagName( [
'query', $this->
getModuleName() ],
'page' );
501 ParamValidator::PARAM_TYPE =>
'timestamp',
505 ParamValidator::PARAM_TYPE =>
'timestamp',
509 ParamValidator::PARAM_TYPE => [
513 ParamValidator::PARAM_DEFAULT =>
'older',
516 'newer' =>
'api-help-paramvalue-direction-newer',
517 'older' =>
'api-help-paramvalue-direction-older',
531 ParamValidator::PARAM_DEFAULT =>
false,
535 ParamValidator::PARAM_TYPE =>
'namespace',
536 ParamValidator::PARAM_DEFAULT =>
NS_MAIN,
541 ParamValidator::PARAM_TYPE =>
'user',
542 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'ip',
'temp',
'id',
'interwiki' ],
545 ParamValidator::PARAM_TYPE =>
'user',
546 UserDef::PARAM_ALLOWED_USER_TYPES => [
'name',
'ip',
'temp',
'id',
'interwiki' ],
549 ParamValidator::PARAM_DEFAULT =>
'user|comment',
550 ParamValidator::PARAM_TYPE => [
564 ParamValidator::PARAM_ISMULTI =>
true,
566 'content' => [
'apihelp-query+deletedrevs-paramvalue-prop-content', $smallLimit ],
568 EnumDef::PARAM_DEPRECATED_VALUES => [
573 ParamValidator::PARAM_DEFAULT => 10,
574 ParamValidator::PARAM_TYPE =>
'limit',
575 IntegerDef::PARAM_MIN => 1,
588 $title = Title::newMainPage();
589 $talkTitle = $title->getTalkPageIfDefined();
593 $title = rawurlencode( $title->getPrefixedText() );
594 $talkTitle = rawurlencode( $talkTitle->getPrefixedText() );
596 "action=query&list=deletedrevs&titles={$title}|{$talkTitle}&" .
597 'drprop=user|comment|content'
598 =>
'apihelp-query+deletedrevs-example-mode1',
602 return array_merge( $examples, [
603 'action=query&list=deletedrevs&druser=Bob&drlimit=50'
604 =>
'apihelp-query+deletedrevs-example-mode2',
605 'action=query&list=deletedrevs&drdir=newer&drlimit=50'
606 =>
'apihelp-query+deletedrevs-example-mode3-main',
607 'action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique='
608 =>
'apihelp-query+deletedrevs-example-mode3-talk',
614 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Deletedrevs';
619class_alias( ApiQueryDeletedrevs::class,
'ApiQueryDeletedrevs' );
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
Factory for LinkBatch objects to batch query page metadata.