2 namespace MediaWiki\Content\Renderer
;
4 use MediaWiki\Content\Content
;
5 use MediaWiki\Content\IContentHandlerFactory
;
6 use MediaWiki\Page\PageReference
;
7 use MediaWiki\Parser\ParserOptions
;
8 use MediaWiki\Parser\ParserOutput
;
9 use MediaWiki\Revision\RevisionRecord
;
10 use Wikimedia\UUID\GlobalIdGenerator
;
13 * A service to render content.
17 class ContentRenderer
{
18 /** @var IContentHandlerFactory */
19 private $contentHandlerFactory;
21 private GlobalIdGenerator
$globalIdGenerator;
23 public function __construct(
24 IContentHandlerFactory
$contentHandlerFactory,
25 GlobalIdGenerator
$globalIdGenerator
27 $this->contentHandlerFactory
= $contentHandlerFactory;
28 $this->globalIdGenerator
= $globalIdGenerator;
32 * Returns a ParserOutput object containing information derived from this content.
34 * @param Content $content
35 * @param PageReference $page
36 * @param RevisionRecord|null $revision
37 * @param ParserOptions|null $parserOptions
38 * @param bool|array{generate-html?:bool,previous-output?:?ParserOutput} $hints
39 * For back-compatibility, passing a bool is equivalent to setting
40 * the 'generate-html' hint.
42 * @return ParserOutput
43 * @note Passing an integer as $rev was deprecated in MW 1.42
45 public function getParserOutput(
49 ?ParserOptions
$parserOptions = null,
54 if ( is_int( $revision ) ) {
55 wfDeprecated( __METHOD__
. ' with integer revision id', '1.42' );
57 } elseif ( $revision !== null ) {
58 $revId = $revision->getId();
59 $revTimestamp = $revision->getTimestamp();
61 if ( is_bool( $hints ) ) {
62 // For backward compatibility.
63 $hints = [ 'generate-html' => $hints ];
65 $cacheTime = wfTimestampNow();
66 $contentHandler = $this->contentHandlerFactory
->getContentHandler( $content->getModel() );
67 $cpoParams = new ContentParseParams(
71 $hints['generate-html'] ??
true,
72 $hints['previous-output'] ??
null
75 $parserOutput = $contentHandler->getParserOutput( $content, $cpoParams );
76 // Set the cache parameters, if not previously set.
78 // It is expected that this will be where most are set for the first
79 // time, but a ContentHandler can (for example) use a content-based
80 // hash for the render id by setting it inside
81 // ContentHandler::getParserOutput(); any such custom render id
82 // will not be overwritten here. Similarly, a ContentHandler can
83 // continue to use the semi-documented feature of ::setCacheTime(-1)
84 // to indicate "not cacheable", and that will not be overwritten
86 if ( !$parserOutput->hasCacheTime() ) {
87 $parserOutput->setCacheTime( $cacheTime );
89 if ( $parserOutput->getRenderId() === null ) {
90 $parserOutput->setRenderId( $this->globalIdGenerator
->newUUIDv1() );
92 // Revision ID and Revision Timestamp are set here so that we don't
93 // have to load the revision row on view.
94 if ( $parserOutput->getCacheRevisionId() === null && $revId !== null ) {
95 $parserOutput->setCacheRevisionId( $revId );
97 if ( $parserOutput->getRevisionTimestamp() === null && $revTimestamp !== null ) {
98 $parserOutput->setRevisionTimestamp( $revTimestamp );
100 return $parserOutput;