MediaWiki master
ContentRenderer.php
Go to the documentation of this file.
1<?php
3
11
18 private IContentHandlerFactory $contentHandlerFactory;
19 private GlobalIdGenerator $globalIdGenerator;
20
21 public function __construct(
22 IContentHandlerFactory $contentHandlerFactory,
23 GlobalIdGenerator $globalIdGenerator
24 ) {
25 $this->contentHandlerFactory = $contentHandlerFactory;
26 $this->globalIdGenerator = $globalIdGenerator;
27 }
28
43 public function getParserOutput(
44 Content $content,
45 PageReference $page,
46 ?RevisionRecord $revision = null,
47 ?ParserOptions $parserOptions = null,
48 $hints = []
49 ): ParserOutput {
50 $revId = $revision?->getId();
51 $revTimestamp = $revision?->getTimestamp();
52
53 if ( is_bool( $hints ) ) {
54 // For backward compatibility.
55 $hints = [ 'generate-html' => $hints ];
56 }
57 $cacheTime = wfTimestampNow();
58 $contentHandler = $this->contentHandlerFactory->getContentHandler( $content->getModel() );
59 $cpoParams = new ContentParseParams(
60 $page,
61 $revId,
62 $parserOptions,
63 $hints['generate-html'] ?? true,
64 $hints['previous-output'] ?? null
65 );
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.
getParserOutput(Content $content, PageReference $page, ?RevisionRecord $revision=null, ?ParserOptions $parserOptions=null, $hints=[])
Returns a ParserOutput object containing information derived from this content.
__construct(IContentHandlerFactory $contentHandlerFactory, GlobalIdGenerator $globalIdGenerator)
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.
Content objects represent page content, e.g.
Definition Content.php:28
getModel()
Get the content model ID.
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.