MediaWiki master
ContentRenderer.php
Go to the documentation of this file.
1<?php
3
4use Content;
11
19 private $contentHandlerFactory;
20
21 private GlobalIdGenerator $globalIdGenerator;
22
27 public function __construct(
28 IContentHandlerFactory $contentHandlerFactory,
29 GlobalIdGenerator $globalIdGenerator
30 ) {
31 $this->contentHandlerFactory = $contentHandlerFactory;
32 $this->globalIdGenerator = $globalIdGenerator;
33 }
34
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}
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
getParserOutput(Content $content, PageReference $page, $revision=null, ?ParserOptions $parserOptions=null, bool $generateHtml=true)
Returns a ParserOutput object containing information derived from this content.
__construct(IContentHandlerFactory $contentHandlerFactory, GlobalIdGenerator $globalIdGenerator)
Rendered output of a wiki page, as parsed from wikitext.
Page revision base class.
Set options of the Parser.
Class for getting statistically unique IDs without a central coordinator.
Base interface for representing page content.
Definition Content.php:37
getModel()
Returns the ID of the content model used by this Content object.
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.