MediaWiki master
ContentRenderer.php
Go to the documentation of this file.
1<?php
3
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
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}
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.
ParserOutput is a rendering of a Content object or a message.
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: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.