MediaWiki
master
Toggle main menu visibility
ContentRenderer.php
Go to the documentation of this file.
1
<?php
2
namespace
MediaWiki\Content\Renderer;
3
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;
11
17
class
ContentRenderer
{
18
public
function
__construct
(
19
private
readonly
IContentHandlerFactory
$contentHandlerFactory,
20
private
readonly
GlobalIdGenerator
$globalIdGenerator,
21
) {
22
}
23
38
public
function
getParserOutput
(
39
Content
$content,
40
PageReference
$page,
41
?
RevisionRecord
$revision =
null
,
42
?
ParserOptions
$parserOptions =
null
,
43
$hints = []
44
):
ParserOutput
{
45
$revId = $revision?->getId();
46
$revTimestamp = $revision?->getTimestamp();
47
48
if
( is_bool( $hints ) ) {
49
// For backward compatibility.
50
$hints = [
'generate-html'
=> $hints ];
51
}
52
$cacheTime =
wfTimestampNow
();
53
$contentHandler = $this->contentHandlerFactory->getContentHandler( $content->
getModel
() );
54
$cpoParams =
new
ContentParseParams
(
55
$page,
56
$revision,
57
$parserOptions,
58
$hints[
'generate-html'
] ??
true
,
59
$hints[
'previous-output'
] ??
null
60
);
61
62
$parserOutput = $contentHandler->getParserOutput( $content, $cpoParams );
63
// Set the cache parameters, if not previously set.
64
//
65
// It is expected that this will be where most are set for the first
66
// time, but a ContentHandler can (for example) use a content-based
67
// hash for the render id by setting it inside
68
// ContentHandler::getParserOutput(); any such custom render id
69
// will not be overwritten here. Similarly, a ContentHandler can
70
// continue to use the semi-documented feature of ::setCacheTime(-1)
71
// to indicate "not cacheable", and that will not be overwritten
72
// either.
73
if
( !$parserOutput->hasCacheTime() ) {
74
$parserOutput->setCacheTime( $cacheTime );
75
}
76
if
( $parserOutput->getRenderId() ===
null
) {
77
$parserOutput->setRenderId( $this->globalIdGenerator->newUUIDv1() );
78
}
79
// Revision ID and Revision Timestamp are set here so that we don't
80
// have to load the revision row on view.
81
if
( $parserOutput->getCacheRevisionId() ===
null
&& $revId !==
null
) {
82
$parserOutput->setCacheRevisionId( $revId );
83
}
84
if
( $parserOutput->getRevisionTimestamp() ===
null
&& $revTimestamp !==
null
) {
85
$parserOutput->setRevisionTimestamp( $revTimestamp );
86
}
87
return
$parserOutput;
88
}
89
}
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition
GlobalFunctions.php:1057
MediaWiki\Content\Renderer\ContentParseParams
Definition
ContentParseParams.php:13
MediaWiki\Content\Renderer\ContentRenderer
A service to render content.
Definition
ContentRenderer.php:17
MediaWiki\Content\Renderer\ContentRenderer\getParserOutput
getParserOutput(Content $content, PageReference $page, ?RevisionRecord $revision=null, ?ParserOptions $parserOptions=null, $hints=[])
Returns a ParserOutput object containing information derived from this content.
Definition
ContentRenderer.php:38
MediaWiki\Content\Renderer\ContentRenderer\__construct
__construct(private readonly IContentHandlerFactory $contentHandlerFactory, private readonly GlobalIdGenerator $globalIdGenerator,)
Definition
ContentRenderer.php:18
MediaWiki\Parser\ParserOptions
Set options of the Parser.
Definition
ParserOptions.php:70
MediaWiki\Parser\ParserOutput
ParserOutput is a rendering of a Content object or a message.
Definition
ParserOutput.php:91
MediaWiki\Revision\RevisionRecord
Page revision base class.
Definition
RevisionRecord.php:35
Wikimedia\UUID\GlobalIdGenerator
Class for getting statistically unique IDs without a central coordinator.
Definition
GlobalIdGenerator.php:20
MediaWiki\Content\Content
Content objects represent page content, e.g.
Definition
Content.php:28
MediaWiki\Content\Content\getModel
getModel()
Get the content model ID.
MediaWiki\Content\IContentHandlerFactory
Definition
IContentHandlerFactory.php:8
MediaWiki\Page\PageReference
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
Definition
PageReference.php:38
includes
Content
Renderer
ContentRenderer.php
Generated on
for MediaWiki by
1.18.0