Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
82.61% covered (warning)
82.61%
19 / 23
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ContentRenderer
82.61% covered (warning)
82.61%
19 / 23
50.00% covered (danger)
50.00%
1 / 2
10.53
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getParserOutput
80.95% covered (warning)
80.95%
17 / 21
0.00% covered (danger)
0.00%
0 / 1
9.56
1<?php
2namespace MediaWiki\Content\Renderer;
3
4use Content;
5use MediaWiki\Content\IContentHandlerFactory;
6use MediaWiki\Page\PageReference;
7use MediaWiki\Parser\ParserOutput;
8use MediaWiki\Revision\RevisionRecord;
9use ParserOptions;
10use Wikimedia\UUID\GlobalIdGenerator;
11
12/**
13 * A service to render content.
14 *
15 * @since 1.38
16 */
17class 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 $generateHtml
43     *
44     * @return ParserOutput
45     * @note Passing an integer as $rev was deprecated in MW 1.42
46     */
47    public function getParserOutput(
48        Content $content,
49        PageReference $page,
50        $revision = null,
51        ?ParserOptions $parserOptions = null,
52        bool $generateHtml = true
53    ): ParserOutput {
54        $revId = null;
55        $revTimestamp = null;
56        if ( is_int( $revision ) ) {
57            wfDeprecated( __METHOD__ . ' with integer revision id', '1.42' );
58            $revId = $revision;
59        } elseif ( $revision !== null ) {
60            $revId = $revision->getId();
61            $revTimestamp = $revision->getTimestamp();
62        }
63        $cacheTime = wfTimestampNow();
64        $contentHandler = $this->contentHandlerFactory->getContentHandler( $content->getModel() );
65        $cpoParams = new ContentParseParams( $page, $revId, $parserOptions, $generateHtml );
66
67        $parserOutput = $contentHandler->getParserOutput( $content, $cpoParams );
68        // Set the cache parameters, if not previously set.
69        //
70        // It is expected that this will be where most are set for the first
71        // time, but a ContentHandler can (for example) use a content-based
72        // hash for the render id by setting it inside
73        // ContentHandler::getParserOutput(); any such custom render id
74        // will not be overwritten here.  Similarly, a ContentHandler can
75        // continue to use the semi-documented feature of ::setCacheTime(-1)
76        // to indicate "not cacheable", and that will not be overwritten
77        // either.
78        if ( !$parserOutput->hasCacheTime() ) {
79            $parserOutput->setCacheTime( $cacheTime );
80        }
81        if ( $parserOutput->getRenderId() === null ) {
82            $parserOutput->setRenderId( $this->globalIdGenerator->newUUIDv1() );
83        }
84        // Revision ID and Revision Timestamp are set here so that we don't
85        // have to load the revision row on view.
86        if ( $parserOutput->getCacheRevisionId() === null && $revId !== null ) {
87            $parserOutput->setCacheRevisionId( $revId );
88        }
89        if ( $parserOutput->getRevisionTimestamp() === null && $revTimestamp !== null ) {
90            $parserOutput->setRevisionTimestamp( $revTimestamp );
91        }
92        return $parserOutput;
93    }
94}