Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
83.87% |
26 / 31 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
ContentRenderer | |
83.87% |
26 / 31 |
|
50.00% |
1 / 2 |
11.51 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getParserOutput | |
82.76% |
24 / 29 |
|
0.00% |
0 / 1 |
10.51 |
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 | |
12 | /** |
13 | * A service to render content. |
14 | * |
15 | * @since 1.38 |
16 | */ |
17 | class ContentRenderer { |
18 | /** @var IContentHandlerFactory */ |
19 | private $contentHandlerFactory; |
20 | |
21 | private GlobalIdGenerator $globalIdGenerator; |
22 | |
23 | /** |
24 | * @param IContentHandlerFactory $contentHandlerFactory |
25 | * @param GlobalIdGenerator $globalIdGenerator |
26 | */ |
27 | public function __construct( |
28 | IContentHandlerFactory $contentHandlerFactory, |
29 | GlobalIdGenerator $globalIdGenerator |
30 | ) { |
31 | $this->contentHandlerFactory = $contentHandlerFactory; |
32 | $this->globalIdGenerator = $globalIdGenerator; |
33 | } |
34 | |
35 | /** |
36 | * Returns a ParserOutput object containing information derived from this content. |
37 | * |
38 | * @param Content $content |
39 | * @param PageReference $page |
40 | * @param RevisionRecord|null $revision |
41 | * @param ParserOptions|null $parserOptions |
42 | * @param bool|array{generate-html?:bool,previous-output?:?ParserOutput} $hints |
43 | * For back-compatibility, passing a bool is equivalent to setting |
44 | * the 'generate-html' hint. |
45 | * |
46 | * @return ParserOutput |
47 | * @note Passing an integer as $rev was deprecated in MW 1.42 |
48 | */ |
49 | public function getParserOutput( |
50 | Content $content, |
51 | PageReference $page, |
52 | $revision = null, |
53 | ?ParserOptions $parserOptions = null, |
54 | $hints = [] |
55 | ): ParserOutput { |
56 | $revId = null; |
57 | $revTimestamp = null; |
58 | if ( is_int( $revision ) ) { |
59 | wfDeprecated( __METHOD__ . ' with integer revision id', '1.42' ); |
60 | $revId = $revision; |
61 | } elseif ( $revision !== null ) { |
62 | $revId = $revision->getId(); |
63 | $revTimestamp = $revision->getTimestamp(); |
64 | } |
65 | if ( is_bool( $hints ) ) { |
66 | // For backward compatibility. |
67 | $hints = [ 'generate-html' => $hints ]; |
68 | } |
69 | $cacheTime = wfTimestampNow(); |
70 | $contentHandler = $this->contentHandlerFactory->getContentHandler( $content->getModel() ); |
71 | $cpoParams = new ContentParseParams( |
72 | $page, |
73 | $revId, |
74 | $parserOptions, |
75 | $hints['generate-html'] ?? true, |
76 | $hints['previous-output'] ?? null |
77 | ); |
78 | |
79 | $parserOutput = $contentHandler->getParserOutput( $content, $cpoParams ); |
80 | // Set the cache parameters, if not previously set. |
81 | // |
82 | // It is expected that this will be where most are set for the first |
83 | // time, but a ContentHandler can (for example) use a content-based |
84 | // hash for the render id by setting it inside |
85 | // ContentHandler::getParserOutput(); any such custom render id |
86 | // will not be overwritten here. Similarly, a ContentHandler can |
87 | // continue to use the semi-documented feature of ::setCacheTime(-1) |
88 | // to indicate "not cacheable", and that will not be overwritten |
89 | // either. |
90 | if ( !$parserOutput->hasCacheTime() ) { |
91 | $parserOutput->setCacheTime( $cacheTime ); |
92 | } |
93 | if ( $parserOutput->getRenderId() === null ) { |
94 | $parserOutput->setRenderId( $this->globalIdGenerator->newUUIDv1() ); |
95 | } |
96 | // Revision ID and Revision Timestamp are set here so that we don't |
97 | // have to load the revision row on view. |
98 | if ( $parserOutput->getCacheRevisionId() === null && $revId !== null ) { |
99 | $parserOutput->setCacheRevisionId( $revId ); |
100 | } |
101 | if ( $parserOutput->getRevisionTimestamp() === null && $revTimestamp !== null ) { |
102 | $parserOutput->setRevisionTimestamp( $revTimestamp ); |
103 | } |
104 | return $parserOutput; |
105 | } |
106 | } |