67 private const IS_DELETED = 1;
68 private const CANNOT_VIEW = 2;
70 private const LIMIT_PARSE = 1;
122 private $numUncachedDiffs = 0;
142 string $paramPrefix =
'',
154 parent::__construct( $queryModule, $moduleName, $paramPrefix );
158 $this->revisionStore = $revisionStore ?? $services->getRevisionStore();
159 $this->contentHandlerFactory = $contentHandlerFactory ?? $services->getContentHandlerFactory();
160 $this->parserFactory = $parserFactory ?? $services->getParserFactory();
161 $this->slotRoleRegistry = $slotRoleRegistry ?? $services->getSlotRoleRegistry();
162 $this->contentRenderer = $contentRenderer ?? $services->getContentRenderer();
163 $this->contentTransformer = $contentTransformer ?? $services->getContentTransformer();
164 $this->commentFormatter = $commentFormatter ?? $services->getCommentFormatter();
165 $this->tempUserCreator = $tempUserCreator ?? $services->getTempUserCreator();
166 $this->userFactory = $userFactory ?? $services->getUserFactory();
167 $this->userNameUtils = $userNameUtils ?? $services->getUserNameUtils();
175 $this->
run( $resultPageSet );
190 $prop = array_fill_keys( $params[
'prop'],
true );
192 $this->fld_ids = isset( $prop[
'ids'] );
193 $this->fld_flags = isset( $prop[
'flags'] );
194 $this->fld_timestamp = isset( $prop[
'timestamp'] );
195 $this->fld_comment = isset( $prop[
'comment'] );
196 $this->fld_parsedcomment = isset( $prop[
'parsedcomment'] );
197 $this->fld_size = isset( $prop[
'size'] );
198 $this->fld_slotsize = isset( $prop[
'slotsize'] );
199 $this->fld_sha1 = isset( $prop[
'sha1'] );
200 $this->fld_slotsha1 = isset( $prop[
'slotsha1'] );
201 $this->fld_content = isset( $prop[
'content'] );
202 $this->fld_contentmodel = isset( $prop[
'contentmodel'] );
203 $this->fld_userid = isset( $prop[
'userid'] );
204 $this->fld_user = isset( $prop[
'user'] );
205 $this->fld_tags = isset( $prop[
'tags'] );
206 $this->fld_roles = isset( $prop[
'roles'] );
207 $this->fld_parsetree = isset( $prop[
'parsetree'] );
209 $this->slotRoles = $params[
'slots'];
211 if ( $this->slotRoles !==
null ) {
212 if ( $this->fld_parsetree ) {
214 'apierror-invalidparammix-cannotusewith',
217 ],
'invalidparammix' );
220 'expandtemplates',
'generatexml',
'parse',
'diffto',
'difftotext',
'difftotextpst',
223 if ( $params[$p] !==
null && $params[$p] !==
false ) {
225 'apierror-invalidparammix-cannotusewith',
228 ],
'invalidparammix' );
231 $this->slotContentFormats = [];
232 foreach ( $this->slotRoles as $slotRole ) {
233 if ( isset( $params[
'contentformat-' . $slotRole] ) ) {
234 $this->slotContentFormats[$slotRole] = $params[
'contentformat-' . $slotRole];
239 if ( !empty( $params[
'contentformat'] ) ) {
240 $this->contentFormat = $params[
'contentformat'];
243 $this->limit = $params[
'limit'];
245 if ( $params[
'difftotext'] !==
null ) {
246 $this->difftotext = $params[
'difftotext'];
247 $this->difftotextpst = $params[
'difftotextpst'];
248 } elseif ( $params[
'diffto'] !==
null ) {
249 if ( $params[
'diffto'] ==
'cur' ) {
250 $params[
'diffto'] = 0;
252 if ( ( !ctype_digit( $params[
'diffto'] ) || $params[
'diffto'] < 0 )
253 && $params[
'diffto'] !=
'prev' && $params[
'diffto'] !=
'next'
256 $this->
dieWithError( [
'apierror-baddiffto', $p ],
'diffto' );
261 if ( is_numeric( $params[
'diffto'] ) && $params[
'diffto'] != 0 ) {
262 $difftoRev = $this->revisionStore->getRevisionById( $params[
'diffto'] );
264 $this->
dieWithError( [
'apierror-nosuchrevid', $params[
'diffto'] ] );
267 $revDel = $this->checkRevDel( $difftoRev, RevisionRecord::DELETED_TEXT );
268 if ( $revDel & self::CANNOT_VIEW ) {
269 $this->
addWarning( [
'apiwarn-difftohidden', $difftoRev->getId() ] );
270 $params[
'diffto'] =
null;
273 $this->diffto = $params[
'diffto'];
276 $this->fetchContent = $this->fld_content || $this->diffto !==
null
280 if ( $this->fetchContent ) {
282 $this->expandTemplates = $params[
'expandtemplates'];
283 $this->generateXML = $params[
'generatexml'];
284 $this->parseContent = $params[
'parse'];
285 if ( $this->parseContent ) {
287 $this->limit ??= self::LIMIT_PARSE;
289 $this->section = $params[
'section'] ??
false;
292 $userMax = $this->parseContent ? self::LIMIT_PARSE :
294 $botMax = $this->parseContent ? self::LIMIT_PARSE :
296 if ( $this->limit ==
'max' ) {
297 $this->limit = $this->
getMain()->canApiHighLimits() ? $botMax : $userMax;
298 if ( $this->setParsedLimit ) {
303 $this->limit = $this->
getMain()->getParamValidator()->validateValue(
304 $this,
'limit', $this->limit ?? 10, [
305 ParamValidator::PARAM_TYPE =>
'limit',
306 IntegerDef::PARAM_MIN => 1,
307 IntegerDef::PARAM_MAX => $userMax,
308 IntegerDef::PARAM_MAX2 => $botMax,
309 IntegerDef::PARAM_IGNORE_RANGE =>
true,
313 $this->needSlots = $this->fetchContent || $this->fld_contentmodel ||
315 if ( $this->needSlots && $this->slotRoles ===
null ) {
319 $parentParam = $parent->encodeParamName( $parent->getModuleManager()->getModuleGroup( $name ) );
321 [
'apiwarn-deprecation-missingparam', $encParam ],
322 "action=query&{$parentParam}={$name}&!{$encParam}"
334 private function checkRevDel(
RevisionRecord $revision, $field ) {
335 $ret = $revision->
isDeleted( $field ) ? self::IS_DELETED : 0;
338 $ret |= ( $canSee ? 0 : self::CANNOT_VIEW );
355 if ( $this->fld_ids ) {
356 $vals[
'revid'] = (int)$revision->
getId();
362 if ( $this->fld_flags ) {
363 $vals[
'minor'] = $revision->
isMinor();
366 if ( $this->fld_user || $this->fld_userid ) {
367 $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_USER );
368 if ( $revDel & self::IS_DELETED ) {
369 $vals[
'userhidden'] =
true;
372 if ( !( $revDel & self::CANNOT_VIEW ) ) {
373 $u = $revision->
getUser( RevisionRecord::RAW );
374 if ( $this->fld_user ) {
375 $vals[
'user'] = $u->getName();
377 if ( $this->userNameUtils->isTemp( $u->getName() ) ) {
378 $vals[
'temp'] =
true;
380 if ( !$u->isRegistered() ) {
381 $vals[
'anon'] =
true;
384 if ( $this->fld_userid ) {
385 $vals[
'userid'] = $u->getId();
390 if ( $this->fld_timestamp ) {
394 if ( $this->fld_size ) {
396 $vals[
'size'] = (int)$revision->
getSize();
404 if ( $this->fld_sha1 ) {
405 $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_TEXT );
406 if ( $revDel & self::IS_DELETED ) {
407 $vals[
'sha1hidden'] =
true;
410 if ( !( $revDel & self::CANNOT_VIEW ) ) {
412 $vals[
'sha1'] = \Wikimedia\base_convert( $revision->
getSha1(), 36, 16, 40 );
422 if ( $this->fld_roles ) {
426 if ( $this->needSlots ) {
427 $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_TEXT );
428 if ( ( $this->fld_slotsha1 || $this->fetchContent ) && ( $revDel & self::IS_DELETED ) ) {
431 $vals = array_merge( $vals, $this->extractAllSlotInfo( $revision, $revDel ) );
436 $vals[
'slotsmissing'] =
true;
438 LoggerFactory::getInstance(
'api-warning' )->error(
439 'Failed to access revision slots',
440 [
'revision' => $revision->
getId(),
'exception' => $ex, ]
444 if ( $this->fld_comment || $this->fld_parsedcomment ) {
445 $revDel = $this->checkRevDel( $revision, RevisionRecord::DELETED_COMMENT );
446 if ( $revDel & self::IS_DELETED ) {
447 $vals[
'commenthidden'] =
true;
450 if ( !( $revDel & self::CANNOT_VIEW ) ) {
451 $comment = $revision->
getComment( RevisionRecord::RAW );
452 $comment = $comment->text ??
'';
454 if ( $this->fld_comment ) {
455 $vals[
'comment'] = $comment;
458 if ( $this->fld_parsedcomment ) {
459 $vals[
'parsedcomment'] = $this->commentFormatter->format(
466 if ( $this->fld_tags ) {
467 if ( $row->ts_tags ) {
468 $tags = explode(
',', $row->ts_tags );
470 $vals[
'tags'] = $tags;
476 if ( $anyHidden && $revision->
isDeleted( RevisionRecord::DELETED_RESTRICTED ) ) {
477 $vals[
'suppressed'] =
true;
492 private function extractAllSlotInfo(
RevisionRecord $revision, $revDel ): array {
495 if ( $this->slotRoles ===
null ) {
497 $slot = $revision->
getSlot( SlotRecord::MAIN, RevisionRecord::RAW );
498 }
catch ( RevisionAccessException $e ) {
501 $vals[
'textmissing'] =
true;
507 $vals += $this->extractSlotInfo( $slot, $revDel, $content );
508 if ( !empty( $vals[
'nosuchsection'] ) ) {
511 'apierror-nosuchsection-what',
513 $this->
msg(
'revid', $revision->
getId() )
519 $vals += $this->extractDeprecatedContent( $content, $revision );
523 $roles = array_intersect( $this->slotRoles, $revision->
getSlotRoles() );
527 foreach ( $roles as $role ) {
529 $slot = $revision->
getSlot( $role, RevisionRecord::RAW );
530 }
catch ( RevisionAccessException $e ) {
533 $vals[
'slots'][$role][
'missing'] =
true;
537 $vals[
'slots'][$role] = $this->extractSlotInfo( $slot, $revDel, $content );
542 $model = $content->getModel();
543 $format = $this->slotContentFormats[$role] ?? $content->getDefaultFormat();
544 if ( !$content->isSupportedFormat( $format ) ) {
546 'apierror-badformat',
549 $this->
msg(
'revid', $revision->
getId() )
551 $vals[
'slots'][$role][
'badcontentformat'] =
true;
553 $vals[
'slots'][$role][
'contentmodel'] = $model;
554 $vals[
'slots'][$role][
'contentformat'] = $format;
556 $vals[
'slots'][$role],
558 $content->serialize( $format )
578 private function extractSlotInfo( SlotRecord $slot, $revDel, &$content =
null ) {
582 if ( $this->fld_slotsize ) {
583 $vals[
'size'] = (int)$slot->getSize();
586 if ( $this->fld_slotsha1 ) {
587 if ( $revDel & self::IS_DELETED ) {
588 $vals[
'sha1hidden'] =
true;
590 if ( !( $revDel & self::CANNOT_VIEW ) ) {
591 if ( $slot->getSha1() !=
'' ) {
592 $vals[
'sha1'] = \Wikimedia\base_convert( $slot->getSha1(), 36, 16, 40 );
599 if ( $this->fld_contentmodel ) {
600 $vals[
'contentmodel'] = $slot->getModel();
604 if ( $this->fetchContent ) {
605 if ( $revDel & self::IS_DELETED ) {
606 $vals[
'texthidden'] =
true;
608 if ( !( $revDel & self::CANNOT_VIEW ) ) {
610 $content = $slot->getContent();
611 }
catch ( RevisionAccessException $e ) {
613 $vals[
'textmissing'] =
true;
618 if ( $content && $this->section !==
false ) {
619 $content = $content->getSection( $this->section );
621 $vals[
'nosuchsection'] =
true;
636 private function extractDeprecatedContent( Content $content, RevisionRecord $revision ) {
638 $title = Title::newFromPageIdentity( $revision->getPage() );
640 if ( $this->fld_parsetree || ( $this->fld_content && $this->generateXML ) ) {
643 '@phan-var WikitextContent $content';
644 $t = $content->getText(); # note: don
't set $text
646 $parser = $this->parserFactory->create();
647 $parser->startExternalParse(
649 ParserOptions::newFromContext( $this->getContext() ),
650 Parser::OT_PREPROCESS
652 $dom = $parser->preprocessToDom( $t );
653 // @phan-suppress-next-line PhanUndeclaredMethodInCallable
654 if ( is_callable( [ $dom, 'saveXML
' ] ) ) {
655 // @phan-suppress-next-line PhanUndeclaredMethod
656 $xml = $dom->saveXML();
658 // @phan-suppress-next-line PhanUndeclaredMethod
659 $xml = $dom->__toString();
661 $vals['parsetree
'] = $xml;
663 $vals['badcontentformatforparsetree
'] = true;
666 'apierror-parsetree-notwikitext-title
',
667 wfEscapeWikiText( $title->getPrefixedText() ),
670 'parsetree-notwikitext
'
675 if ( $this->fld_content ) {
678 if ( $this->expandTemplates && !$this->parseContent ) {
679 if ( $content->getModel() === CONTENT_MODEL_WIKITEXT ) {
681 '@phan-var WikitextContent $content
';
682 $text = $content->getText();
684 $text = $this->parserFactory->create()->preprocess(
687 ParserOptions::newFromContext( $this->getContext() )
691 'apierror-templateexpansion-notwikitext
',
692 wfEscapeWikiText( $title->getPrefixedText() ),
695 $vals['badcontentformat
'] = true;
699 if ( $this->parseContent ) {
700 $popts = ParserOptions::newFromContext( $this->getContext() );
701 $po = $this->contentRenderer->getParserOutput(
707 // TODO T371004 move runOutputPipeline out of $parserOutput
708 $text = $po->runOutputPipeline( $popts, [] )->getContentHolderText();
711 if ( $text === null ) {
712 $format = $this->contentFormat ?: $content->getDefaultFormat();
713 $model = $content->getModel();
715 if ( !$content->isSupportedFormat( $format ) ) {
716 $name = wfEscapeWikiText( $title->getPrefixedText() );
717 $this->addWarning( [ 'apierror-badformat
', $this->contentFormat, $model, $name ] );
718 $vals['badcontentformat
'] = true;
721 $text = $content->serialize( $format );
722 // always include format and model.
723 // Format is needed to deserialize, model is needed to interpret.
724 $vals['contentformat
'] = $format;
725 $vals['contentmodel
'] = $model;
729 if ( $text !== false ) {
730 ApiResult::setContentValue( $vals, 'content
', $text );
734 if ( $content && ( $this->diffto !== null || $this->difftotext !== null ) ) {
735 if ( $this->numUncachedDiffs < $this->getConfig()->get( MainConfigNames::APIMaxUncachedDiffs ) ) {
737 $context = new DerivativeContext( $this->getContext() );
738 $context->setTitle( $title );
739 $handler = $content->getContentHandler();
741 if ( $this->difftotext !== null ) {
742 $model = $title->getContentModel();
744 if ( $this->contentFormat
745 && !$this->contentHandlerFactory->getContentHandler( $model )
746 ->isSupportedFormat( $this->contentFormat )
748 $name = wfEscapeWikiText( $title->getPrefixedText() );
749 $this->addWarning( [ 'apierror-badformat
', $this->contentFormat, $model, $name ] );
750 $vals['diff
']['badcontentformat
'] = true;
753 $difftocontent = $this->contentHandlerFactory->getContentHandler( $model )
754 ->unserializeContent( $this->difftotext, $this->contentFormat );
756 if ( $this->difftotextpst ) {
757 $popts = ParserOptions::newFromContext( $this->getContext() );
758 $difftocontent = $this->contentTransformer->preSaveTransform(
761 $this->getUserForPreview(),
766 $engine = $handler->createDifferenceEngine( $context );
767 $engine->setContent( $content, $difftocontent );
770 $engine = $handler->createDifferenceEngine( $context, $revision->getId(), $this->diffto );
771 $vals['diff
']['from
'] = $engine->getOldid();
772 $vals['diff
']['to
'] = $engine->getNewid();
775 $difftext = $engine->getDiffBody();
776 ApiResult::setContentValue( $vals['diff
'], 'body
', $difftext );
777 if ( !$engine->wasCacheHit() ) {
778 $this->numUncachedDiffs++;
780 foreach ( $engine->getRevisionLoadErrors() as $msg ) {
781 $this->addWarning( $msg );
785 $vals['diff
']['notcached
'] = true;
792 private function getUserForPreview(): UserIdentity {
793 $user = $this->getUser();
794 if ( $this->tempUserCreator->shouldAutoCreate( $user, 'edit
' ) ) {
795 return $this->userFactory->newUnsavedTempUser(
796 $this->tempUserCreator->getStashedName( $this->getRequest()->getSession() )
808 public function getCacheMode( $params ) {
809 if ( $this->userCanSeeRevDel() ) {
820 public function getAllowedParams() {
821 $slotRoles = $this->slotRoleRegistry->getKnownRoles();
822 sort( $slotRoles, SORT_STRING );
823 $smallLimit = $this->getMain()->canApiHighLimits() ? ApiBase::LIMIT_SML2 : ApiBase::LIMIT_SML1;
827 ParamValidator::PARAM_ISMULTI => true,
828 ParamValidator::PARAM_DEFAULT => 'ids|timestamp|flags|comment|user
',
829 ParamValidator::PARAM_TYPE => [
847 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-prop
',
848 ApiBase::PARAM_HELP_MSG_PER_VALUE => [
849 'ids
' => 'apihelp-query+revisions+base-paramvalue-prop-ids
',
850 'flags
' => 'apihelp-query+revisions+base-paramvalue-prop-flags
',
851 'timestamp
' => 'apihelp-query+revisions+base-paramvalue-prop-timestamp
',
852 'user
' => 'apihelp-query+revisions+base-paramvalue-prop-user
',
853 'userid
' => 'apihelp-query+revisions+base-paramvalue-prop-userid
',
854 'size
' => 'apihelp-query+revisions+base-paramvalue-prop-size
',
855 'slotsize
' => 'apihelp-query+revisions+base-paramvalue-prop-slotsize
',
856 'sha1
' => 'apihelp-query+revisions+base-paramvalue-prop-sha1
',
857 'slotsha1
' => 'apihelp-query+revisions+base-paramvalue-prop-slotsha1
',
858 'contentmodel
' => 'apihelp-query+revisions+base-paramvalue-prop-contentmodel
',
859 'comment
' => 'apihelp-query+revisions+base-paramvalue-prop-comment
',
860 'parsedcomment
' => 'apihelp-query+revisions+base-paramvalue-prop-parsedcomment
',
861 'content
' => [ 'apihelp-query+revisions+base-paramvalue-prop-content
', $smallLimit ],
862 'tags
' => 'apihelp-query+revisions+base-paramvalue-prop-tags
',
863 'roles
' => 'apihelp-query+revisions+base-paramvalue-prop-roles
',
864 'parsetree
' => [ 'apihelp-query+revisions+base-paramvalue-prop-parsetree
',
865 CONTENT_MODEL_WIKITEXT, $smallLimit ],
867 EnumDef::PARAM_DEPRECATED_VALUES => [
872 ParamValidator::PARAM_TYPE => $slotRoles,
873 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-slots
',
874 ParamValidator::PARAM_ISMULTI => true,
875 ParamValidator::PARAM_ALL => true,
877 'contentformat-{slot}
' => [
878 ApiBase::PARAM_TEMPLATE_VARS => [ 'slot
' => 'slots
' ],
879 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-contentformat-slot
',
880 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
883 ParamValidator::PARAM_TYPE => 'limit
',
884 IntegerDef::PARAM_MIN => 1,
885 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
886 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
887 ApiBase::PARAM_HELP_MSG => [ 'apihelp-query+revisions+base-param-limit
',
888 $smallLimit, self::LIMIT_PARSE ],
890 'expandtemplates
' => [
891 ParamValidator::PARAM_DEFAULT => false,
892 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-expandtemplates
',
893 ParamValidator::PARAM_DEPRECATED => true,
896 ParamValidator::PARAM_DEFAULT => false,
897 ParamValidator::PARAM_DEPRECATED => true,
898 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-generatexml
',
901 ParamValidator::PARAM_DEFAULT => false,
902 ApiBase::PARAM_HELP_MSG => [ 'apihelp-query+revisions+base-param-parse
', self::LIMIT_PARSE ],
903 ParamValidator::PARAM_DEPRECATED => true,
906 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-section
',
909 ApiBase::PARAM_HELP_MSG => [ 'apihelp-query+revisions+base-param-diffto
', $smallLimit ],
910 ParamValidator::PARAM_DEPRECATED => true,
913 ApiBase::PARAM_HELP_MSG => [ 'apihelp-query+revisions+base-param-difftotext
', $smallLimit ],
914 ParamValidator::PARAM_DEPRECATED => true,
917 ParamValidator::PARAM_DEFAULT => false,
918 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-difftotextpst
',
919 ParamValidator::PARAM_DEPRECATED => true,
922 ParamValidator::PARAM_TYPE => $this->contentHandlerFactory->getAllContentFormats(),
923 ApiBase::PARAM_HELP_MSG => 'apihelp-query+revisions+base-param-contentformat
',
924 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.
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.
Content objects represent page content, e.g.