66 private const IS_DELETED = 1;
67 private const CANNOT_VIEW = 2;
69 private const LIMIT_PARSE = 1;
121 private $numUncachedDiffs = 0;
141 string $paramPrefix =
'',
153 parent::__construct( $queryModule, $moduleName, $paramPrefix );
157 $this->revisionStore = $revisionStore ?? $services->getRevisionStore();
158 $this->contentHandlerFactory = $contentHandlerFactory ?? $services->getContentHandlerFactory();
159 $this->parserFactory = $parserFactory ?? $services->getParserFactory();
160 $this->slotRoleRegistry = $slotRoleRegistry ?? $services->getSlotRoleRegistry();
161 $this->contentRenderer = $contentRenderer ?? $services->getContentRenderer();
162 $this->contentTransformer = $contentTransformer ?? $services->getContentTransformer();
163 $this->commentFormatter = $commentFormatter ?? $services->getCommentFormatter();
164 $this->tempUserCreator = $tempUserCreator ?? $services->getTempUserCreator();
165 $this->userFactory = $userFactory ?? $services->getUserFactory();
166 $this->userNameUtils = $userNameUtils ?? $services->getUserNameUtils();
174 $this->
run( $resultPageSet );
189 $prop = array_fill_keys(
$params[
'prop'],
true );
191 $this->fld_ids = isset( $prop[
'ids'] );
192 $this->fld_flags = isset( $prop[
'flags'] );
193 $this->fld_timestamp = isset( $prop[
'timestamp'] );
194 $this->fld_comment = isset( $prop[
'comment'] );
195 $this->fld_parsedcomment = isset( $prop[
'parsedcomment'] );
196 $this->fld_size = isset( $prop[
'size'] );
197 $this->fld_slotsize = isset( $prop[
'slotsize'] );
198 $this->fld_sha1 = isset( $prop[
'sha1'] );
199 $this->fld_slotsha1 = isset( $prop[
'slotsha1'] );
200 $this->fld_content = isset( $prop[
'content'] );
201 $this->fld_contentmodel = isset( $prop[
'contentmodel'] );
202 $this->fld_userid = isset( $prop[
'userid'] );
203 $this->fld_user = isset( $prop[
'user'] );
204 $this->fld_tags = isset( $prop[
'tags'] );
205 $this->fld_roles = isset( $prop[
'roles'] );
206 $this->fld_parsetree = isset( $prop[
'parsetree'] );
208 $this->slotRoles =
$params[
'slots'];
210 if ( $this->slotRoles !==
null ) {
211 if ( $this->fld_parsetree ) {
213 'apierror-invalidparammix-cannotusewith',
216 ],
'invalidparammix' );
219 'expandtemplates',
'generatexml',
'parse',
'diffto',
'difftotext',
'difftotextpst',
224 'apierror-invalidparammix-cannotusewith',
227 ],
'invalidparammix' );
230 $this->slotContentFormats = [];
231 foreach ( $this->slotRoles as $slotRole ) {
232 if ( isset(
$params[
'contentformat-' . $slotRole] ) ) {
233 $this->slotContentFormats[$slotRole] =
$params[
'contentformat-' . $slotRole];
238 if ( !empty(
$params[
'contentformat'] ) ) {
239 $this->contentFormat =
$params[
'contentformat'];
242 $this->limit =
$params[
'limit'];
244 if (
$params[
'difftotext'] !==
null ) {
245 $this->difftotext =
$params[
'difftotext'];
246 $this->difftotextpst =
$params[
'difftotextpst'];
247 } elseif (
$params[
'diffto'] !==
null ) {
248 if (
$params[
'diffto'] ==
'cur' ) {
251 if ( ( !ctype_digit(
$params[
'diffto'] ) ||
$params[
'diffto'] < 0 )
255 $this->
dieWithError( [
'apierror-baddiffto', $p ],
'diffto' );
260 if ( is_numeric(
$params[
'diffto'] ) &&
$params[
'diffto'] != 0 ) {
261 $difftoRev = $this->revisionStore->getRevisionById(
$params[
'diffto'] );
266 $revDel = $this->checkRevDel( $difftoRev, RevisionRecord::DELETED_TEXT );
267 if ( $revDel & self::CANNOT_VIEW ) {
268 $this->
addWarning( [
'apiwarn-difftohidden', $difftoRev->getId() ] );
272 $this->diffto =
$params[
'diffto'];
275 $this->fetchContent = $this->fld_content || $this->diffto !==
null
279 if ( $this->fetchContent ) {
281 $this->expandTemplates =
$params[
'expandtemplates'];
282 $this->generateXML =
$params[
'generatexml'];
283 $this->parseContent =
$params[
'parse'];
284 if ( $this->parseContent ) {
286 $this->limit ??= self::LIMIT_PARSE;
288 $this->section =
$params[
'section'] ??
false;
291 $userMax = $this->parseContent ? self::LIMIT_PARSE :
293 $botMax = $this->parseContent ? self::LIMIT_PARSE :
295 if ( $this->limit ==
'max' ) {
296 $this->limit = $this->
getMain()->canApiHighLimits() ? $botMax : $userMax;
297 if ( $this->setParsedLimit ) {
302 $this->limit = $this->
getMain()->getParamValidator()->validateValue(
303 $this,
'limit', $this->limit ?? 10, [
304 ParamValidator::PARAM_TYPE =>
'limit',
305 IntegerDef::PARAM_MIN => 1,
306 IntegerDef::PARAM_MAX => $userMax,
307 IntegerDef::PARAM_MAX2 => $botMax,
308 IntegerDef::PARAM_IGNORE_RANGE =>
true,
312 $this->needSlots = $this->fetchContent || $this->fld_contentmodel ||
314 if ( $this->needSlots && $this->slotRoles ===
null ) {
318 $parentParam = $parent->encodeParamName( $parent->getModuleManager()->getModuleGroup( $name ) );
320 [
'apiwarn-deprecation-missingparam', $encParam ],
321 "action=query&{$parentParam}={$name}&!{$encParam}"
333 private function checkRevDel(
RevisionRecord $revision, $field ) {
334 $ret = $revision->
isDeleted( $field ) ? self::IS_DELETED : 0;
337 $ret |= ( $canSee ? 0 : self::CANNOT_VIEW );
354 if ( $this->fld_ids ) {
355 $vals[
'revid'] = (int)$revision->
getId();
361 if ( $this->fld_flags ) {
362 $vals[
'minor'] = $revision->
isMinor();
365 if ( $this->fld_user || $this->fld_userid ) {
366 $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_USER );
367 if ( $revDel & self::IS_DELETED ) {
368 $vals[
'userhidden'] =
true;
371 if ( !( $revDel & self::CANNOT_VIEW ) ) {
372 $u = $revision->
getUser( RevisionRecord::RAW );
373 if ( $this->fld_user ) {
374 $vals[
'user'] = $u->getName();
376 if ( $this->userNameUtils->isTemp( $u->getName() ) ) {
377 $vals[
'temp'] =
true;
379 if ( !$u->isRegistered() ) {
380 $vals[
'anon'] =
true;
383 if ( $this->fld_userid ) {
384 $vals[
'userid'] = $u->getId();
389 if ( $this->fld_timestamp ) {
393 if ( $this->fld_size ) {
395 $vals[
'size'] = (int)$revision->
getSize();
403 if ( $this->fld_sha1 ) {
404 $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_TEXT );
405 if ( $revDel & self::IS_DELETED ) {
406 $vals[
'sha1hidden'] =
true;
409 if ( !( $revDel & self::CANNOT_VIEW ) ) {
411 $vals[
'sha1'] = \Wikimedia\base_convert( $revision->
getSha1(), 36, 16, 40 );
421 if ( $this->fld_roles ) {
425 if ( $this->needSlots ) {
426 $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_TEXT );
427 if ( ( $this->fld_slotsha1 || $this->fetchContent ) && ( $revDel & self::IS_DELETED ) ) {
430 $vals = array_merge( $vals, $this->extractAllSlotInfo( $revision, $revDel ) );
435 $vals[
'slotsmissing'] =
true;
437 LoggerFactory::getInstance(
'api-warning' )->error(
438 'Failed to access revision slots',
439 [
'revision' => $revision->
getId(),
'exception' => $ex, ]
443 if ( $this->fld_comment || $this->fld_parsedcomment ) {
444 $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_COMMENT );
445 if ( $revDel & self::IS_DELETED ) {
446 $vals[
'commenthidden'] =
true;
449 if ( !( $revDel & self::CANNOT_VIEW ) ) {
450 $comment = $revision->
getComment( RevisionRecord::RAW );
451 $comment = $comment->text ??
'';
453 if ( $this->fld_comment ) {
454 $vals[
'comment'] = $comment;
457 if ( $this->fld_parsedcomment ) {
458 $vals[
'parsedcomment'] = $this->commentFormatter->format(
465 if ( $this->fld_tags ) {
466 if ( $row->ts_tags ) {
467 $tags = explode(
',', $row->ts_tags );
469 $vals[
'tags'] = $tags;
475 if ( $anyHidden && $revision->
isDeleted( RevisionRecord::DELETED_RESTRICTED ) ) {
476 $vals[
'suppressed'] =
true;
491 private function extractAllSlotInfo(
RevisionRecord $revision, $revDel ): array {
494 if ( $this->slotRoles ===
null ) {
496 $slot = $revision->
getSlot( SlotRecord::MAIN, RevisionRecord::RAW );
497 }
catch ( RevisionAccessException $e ) {
500 $vals[
'textmissing'] =
true;
506 $vals += $this->extractSlotInfo( $slot, $revDel, $content );
507 if ( !empty( $vals[
'nosuchsection'] ) ) {
510 'apierror-nosuchsection-what',
512 $this->
msg(
'revid', $revision->
getId() )
518 $vals += $this->extractDeprecatedContent( $content, $revision );
522 $roles = array_intersect( $this->slotRoles, $revision->
getSlotRoles() );
526 foreach ( $roles as $role ) {
528 $slot = $revision->
getSlot( $role, RevisionRecord::RAW );
529 }
catch ( RevisionAccessException $e ) {
532 $vals[
'slots'][$role][
'missing'] =
true;
536 $vals[
'slots'][$role] = $this->extractSlotInfo( $slot, $revDel, $content );
541 $model = $content->getModel();
542 $format = $this->slotContentFormats[$role] ?? $content->getDefaultFormat();
543 if ( !$content->isSupportedFormat( $format ) ) {
545 'apierror-badformat',
548 $this->
msg(
'revid', $revision->
getId() )
550 $vals[
'slots'][$role][
'badcontentformat'] =
true;
552 $vals[
'slots'][$role][
'contentmodel'] = $model;
553 $vals[
'slots'][$role][
'contentformat'] = $format;
555 $vals[
'slots'][$role],
557 $content->serialize( $format )
577 private function extractSlotInfo( SlotRecord $slot, $revDel, &$content =
null ) {
581 if ( $this->fld_slotsize ) {
582 $vals[
'size'] = (int)$slot->getSize();
585 if ( $this->fld_slotsha1 ) {
586 if ( $revDel & self::IS_DELETED ) {
587 $vals[
'sha1hidden'] =
true;
589 if ( !( $revDel & self::CANNOT_VIEW ) ) {
590 if ( $slot->getSha1() !=
'' ) {
591 $vals[
'sha1'] = \Wikimedia\base_convert( $slot->getSha1(), 36, 16, 40 );
598 if ( $this->fld_contentmodel ) {
599 $vals[
'contentmodel'] = $slot->getModel();
603 if ( $this->fetchContent ) {
604 if ( $revDel & self::IS_DELETED ) {
605 $vals[
'texthidden'] =
true;
607 if ( !( $revDel & self::CANNOT_VIEW ) ) {
609 $content = $slot->getContent();
610 }
catch ( RevisionAccessException $e ) {
612 $vals[
'textmissing'] =
true;
617 if ( $content && $this->section !==
false ) {
618 $content = $content->getSection( $this->section );
620 $vals[
'nosuchsection'] =
true;
635 private function extractDeprecatedContent( Content $content, RevisionRecord $revision ) {
637 $title = Title::newFromLinkTarget( $revision->getPageAsLinkTarget() );
639 if ( $this->fld_parsetree || ( $this->fld_content && $this->generateXML ) ) {
642 '@phan-var WikitextContent $content';
643 $t = $content->getText(); # note: don
't set $text
645 $parser = $this->parserFactory->create();
646 $parser->startExternalParse(
648 ParserOptions::newFromContext( $this->getContext() ),
649 Parser::OT_PREPROCESS
651 $dom = $parser->preprocessToDom( $t );
652 // @phan-suppress-next-line PhanUndeclaredMethodInCallable
653 if ( is_callable( [ $dom, 'saveXML
' ] ) ) {
654 // @phan-suppress-next-line PhanUndeclaredMethod
655 $xml = $dom->saveXML();
657 // @phan-suppress-next-line PhanUndeclaredMethod
658 $xml = $dom->__toString();
660 $vals['parsetree
'] = $xml;
662 $vals['badcontentformatforparsetree
'] = true;
665 'apierror-parsetree-notwikitext-title
',
666 wfEscapeWikiText( $title->getPrefixedText() ),
669 'parsetree-notwikitext
'
674 if ( $this->fld_content ) {
677 if ( $this->expandTemplates && !$this->parseContent ) {
678 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
680 '@phan-var WikitextContent $content
';
681 $text = $content->getText();
683 $text = $this->parserFactory->create()->preprocess(
686 ParserOptions::newFromContext( $this->getContext() )
690 'apierror-templateexpansion-notwikitext
',
691 wfEscapeWikiText( $title->getPrefixedText() ),
694 $vals['badcontentformat
'] = true;
698 if ( $this->parseContent ) {
699 $popts = ParserOptions::newFromContext( $this->getContext() );
700 $po = $this->contentRenderer->getParserOutput(
706 // TODO T371004 move runOutputPipeline out of $parserOutput
707 $text = $po->runOutputPipeline( $popts, [] )->getContentHolderText();
710 if ( $text === null ) {
711 $format = $this->contentFormat ?: $content->getDefaultFormat();
712 $model = $content->getModel();
714 if ( !$content->isSupportedFormat( $format ) ) {
715 $name = wfEscapeWikiText( $title->getPrefixedText() );
716 $this->addWarning( [ 'apierror-badformat
', $this->contentFormat, $model, $name ] );
717 $vals['badcontentformat
'] = true;
720 $text = $content->serialize( $format );
721 // always include format and model.
722 // Format is needed to deserialize, model is needed to interpret.
723 $vals['contentformat
'] = $format;
724 $vals['contentmodel
'] = $model;
728 if ( $text !== false ) {
729 ApiResult::setContentValue( $vals, 'content
', $text );
733 if ( $content && ( $this->diffto !== null || $this->difftotext !== null ) ) {
734 if ( $this->numUncachedDiffs < $this->getConfig()->get( MainConfigNames::APIMaxUncachedDiffs ) ) {
736 $context = new DerivativeContext( $this->getContext() );
737 $context->setTitle( $title );
738 $handler = $content->getContentHandler();
740 if ( $this->difftotext !== null ) {
741 $model = $title->getContentModel();
743 if ( $this->contentFormat
744 && !$this->contentHandlerFactory->getContentHandler( $model )
745 ->isSupportedFormat( $this->contentFormat )
747 $name = wfEscapeWikiText( $title->getPrefixedText() );
748 $this->addWarning( [ 'apierror-badformat
', $this->contentFormat, $model, $name ] );
749 $vals['diff
']['badcontentformat
'] = true;
752 $difftocontent = $this->contentHandlerFactory->getContentHandler( $model )
753 ->unserializeContent( $this->difftotext, $this->contentFormat );
755 if ( $this->difftotextpst ) {
756 $popts = ParserOptions::newFromContext( $this->getContext() );
757 $difftocontent = $this->contentTransformer->preSaveTransform(
760 $this->getUserForPreview(),
765 $engine = $handler->createDifferenceEngine( $context );
766 $engine->setContent( $content, $difftocontent );
769 $engine = $handler->createDifferenceEngine( $context, $revision->getId(), $this->diffto );
770 $vals['diff
']['from
'] = $engine->getOldid();
771 $vals['diff
']['to
'] = $engine->getNewid();
774 $difftext = $engine->getDiffBody();
775 ApiResult::setContentValue( $vals['diff
'], 'body
', $difftext );
776 if ( !$engine->wasCacheHit() ) {
777 $this->numUncachedDiffs++;
779 foreach ( $engine->getRevisionLoadErrors() as $msg ) {
780 $this->addWarning( $msg );
784 $vals['diff
']['notcached
'] = true;
791 private function getUserForPreview() {
792 $user = $this->getUser();
793 if ( $this->tempUserCreator->shouldAutoCreate( $user, 'edit
' ) ) {
794 return $this->userFactory->newUnsavedTempUser(
795 $this->tempUserCreator->getStashedName( $this->getRequest()->getSession() )
807 public function getCacheMode( $params ) {
808 if ( $this->userCanSeeRevDel() ) {
819 public function getAllowedParams() {
820 $slotRoles = $this->slotRoleRegistry->getKnownRoles();
821 sort( $slotRoles, SORT_STRING );
822 $smallLimit = $this->getMain()->canApiHighLimits() ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_SML1;
826 ParamValidator::PARAM_ISMULTI => true,
827 ParamValidator::PARAM_DEFAULT => 'ids|timestamp|flags|comment|user
',
828 ParamValidator::PARAM_TYPE => [
846 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-prop
',
847 ApiBase::PARAM_HELP_MSG_PER_VALUE => [
848 'ids
' => 'apihelp-query+revisions+base-paramvalue-prop-ids
',
849 'flags
' => 'apihelp-query+revisions+base-paramvalue-prop-flags
',
850 'timestamp
' => 'apihelp-query+revisions+base-paramvalue-prop-timestamp
',
851 'user
' => 'apihelp-query+revisions+base-paramvalue-prop-user
',
852 'userid
' => 'apihelp-query+revisions+base-paramvalue-prop-userid
',
853 'size
' => 'apihelp-query+revisions+base-paramvalue-prop-size
',
854 'slotsize
' => 'apihelp-query+revisions+base-paramvalue-prop-slotsize
',
855 'sha1
' => 'apihelp-query+revisions+base-paramvalue-prop-sha1
',
856 'slotsha1
' => 'apihelp-query+revisions+base-paramvalue-prop-slotsha1
',
857 'contentmodel
' => 'apihelp-query+revisions+base-paramvalue-prop-contentmodel
',
858 'comment
' => 'apihelp-query+revisions+base-paramvalue-prop-comment
',
859 'parsedcomment
' => 'apihelp-query+revisions+base-paramvalue-prop-parsedcomment
',
860 'content
' => [ 'apihelp-query+revisions+base-paramvalue-prop-content
', $smallLimit ],
861 'tags
' => 'apihelp-query+revisions+base-paramvalue-prop-tags
',
862 'roles
' => 'apihelp-query+revisions+base-paramvalue-prop-roles
',
863 'parsetree
' => [ 'apihelp-query+revisions+base-paramvalue-prop-parsetree
',
864 CONTENT_MODEL_WIKITEXT, $smallLimit ],
866 EnumDef::PARAM_DEPRECATED_VALUES => [
871 ParamValidator::PARAM_TYPE => $slotRoles,
872 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-slots
',
873 ParamValidator::PARAM_ISMULTI => true,
874 ParamValidator::PARAM_ALL => true,
876 'contentformat-{slot}
' => [
877 ApiBase::PARAM_TEMPLATE_VARS => [ 'slot
' => 'slots
' ],
878 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-contentformat-slot
',
879 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
882 ParamValidator::PARAM_TYPE => 'limit
',
883 IntegerDef::PARAM_MIN => 1,
884 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
885 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
886 ApiBase::PARAM_HELP_MSG => [ 'apihelp-query+revisions+base-param-limit
',
887 $smallLimit, self::LIMIT_PARSE ],
889 'expandtemplates
' => [
890 ParamValidator::PARAM_DEFAULT => false,
891 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-expandtemplates
',
892 ParamValidator::PARAM_DEPRECATED => true,
895 ParamValidator::PARAM_DEFAULT => false,
896 ParamValidator::PARAM_DEPRECATED => true,
897 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-generatexml
',
900 ParamValidator::PARAM_DEFAULT => false,
901 ApiBase::PARAM_HELP_MSG => [ 'apihelp-query+revisions+base-param-parse
', self::LIMIT_PARSE ],
902 ParamValidator::PARAM_DEPRECATED => true,
905 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-section
',
908 ApiBase::PARAM_HELP_MSG => [ 'apihelp-query+revisions+base-param-diffto
', $smallLimit ],
909 ParamValidator::PARAM_DEPRECATED => true,
912 ApiBase::PARAM_HELP_MSG => [ 'apihelp-query+revisions+base-param-difftotext
', $smallLimit ],
913 ParamValidator::PARAM_DEPRECATED => true,
916 ParamValidator::PARAM_DEFAULT => false,
917 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-difftotextpst
',
918 ParamValidator::PARAM_DEPRECATED => true,
921 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
922 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-contentformat
',
923 ParamValidator::PARAM_DEPRECATED => true,
const CONTENT_MODEL_WIKITEXT
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
array $params
The job parameters.
This class contains a list of pages that the client has requested.
A service to render content.
A service to transform content.
Content object for wiki text pages.
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
An IContextSource implementation which will inherit context from another source but allow individual ...
A class containing constants representing the names of configuration variables.
Base interface for representing page content.