MediaWiki master
ContentRenderer.php
Go to the documentation of this file.
1<?php
3
11
19 private $contentHandlerFactory;
20
21 private GlobalIdGenerator $globalIdGenerator;
22
23 public function __construct(
24 IContentHandlerFactory $contentHandlerFactory,
25 GlobalIdGenerator $globalIdGenerator
26 ) {
27 $this->contentHandlerFactory = $contentHandlerFactory;
28 $this->globalIdGenerator = $globalIdGenerator;
29 }
30
45 public function getParserOutput(
46 Content $content,
47 PageReference $page,
48 $revision = null,
49 ?ParserOptions $parserOptions = null,
50 $hints = []
51 ): ParserOutput {
52 $revId = null;
53 $revTimestamp = null;
54 if ( is_int( $revision ) ) {
55 wfDeprecated( __METHOD__ . ' with integer revision id', '1.42' );
56 $revId = $revision;
57 } elseif ( $revision !== null ) {
58 $revId = $revision->getId();
59 $revTimestamp = $revision->getTimestamp();
60 }
61 if ( is_bool( $hints ) ) {
62 // For backward compatibility.
63 $hints = [ 'generate-html' => $hints ];
64 }
65 $cacheTime = wfTimestampNow();
66 $contentHandler = $this->contentHandlerFactory->getContentHandler( $content->getModel() );
67 $cpoParams = new ContentParseParams(
68 $page,
69 $revId,
70 $parserOptions,
71 $hints['generate-html'] ?? true,
72 $hints['previous-output'] ?? null
73 );
74
75 $parserOutput = $contentHandler->getParserOutput( $content, $cpoParams );
76 // Set the cache parameters, if not previously set.
77 //
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
85 // either.
86 if ( !$parserOutput->hasCacheTime() ) {
87 $parserOutput->setCacheTime( $cacheTime );
88 }
89 if ( $parserOutput->getRenderId() === null ) {
90 $parserOutput->setRenderId( $this->globalIdGenerator->newUUIDv1() );
91 }
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 );
96 }
97 if ( $parserOutput->getRevisionTimestamp() === null && $revTimestamp !== null ) {
98 $parserOutput->setRevisionTimestamp( $revTimestamp );
99 }
100 return $parserOutput;
101 }
102}
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.
__construct(IContentHandlerFactory $contentHandlerFactory, GlobalIdGenerator $globalIdGenerator)
getParserOutput(Content $content, PageReference $page, $revision=null, ?ParserOptions $parserOptions=null, $hints=[])
Returns a ParserOutput object containing information derived from this content.
Set options of the Parser.
ParserOutput is a rendering of a Content object or a message.
Page revision base class.
Class for getting statistically unique IDs without a central coordinator.
Base interface for representing page content.
Definition Content.php:39
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.