MediaWiki master
ContentRenderer.php
Go to the documentation of this file.
1<?php
3
11
18 public function __construct(
19 private readonly IContentHandlerFactory $contentHandlerFactory,
20 private readonly GlobalIdGenerator $globalIdGenerator,
21 ) {
22 }
23
38 public function getParserOutput(
39 Content $content,
40 PageReference $page,
41 ?RevisionRecord $revision = null,
42 ?ParserOptions $parserOptions = null,
43 $hints = []
44 ): ParserOutput {
45 $revId = $revision?->getId();
46 $revTimestamp = $revision?->getTimestamp();
47
48 if ( is_bool( $hints ) ) {
49 // For backward compatibility.
50 $hints = [ 'generate-html' => $hints ];
51 }
52 $cacheTime = wfTimestampNow();
53 $contentHandler = $this->contentHandlerFactory->getContentHandler( $content->getModel() );
54 $cpoParams = new ContentParseParams(
55 $page,
56 $revId,
57 $parserOptions,
58 $hints['generate-html'] ?? true,
59 $hints['previous-output'] ?? null
60 );
61
62 $parserOutput = $contentHandler->getParserOutput( $content, $cpoParams );
63 // Set the cache parameters, if not previously set.
64 //
65 // It is expected that this will be where most are set for the first
66 // time, but a ContentHandler can (for example) use a content-based
67 // hash for the render id by setting it inside
68 // ContentHandler::getParserOutput(); any such custom render id
69 // will not be overwritten here. Similarly, a ContentHandler can
70 // continue to use the semi-documented feature of ::setCacheTime(-1)
71 // to indicate "not cacheable", and that will not be overwritten
72 // either.
73 if ( !$parserOutput->hasCacheTime() ) {
74 $parserOutput->setCacheTime( $cacheTime );
75 }
76 if ( $parserOutput->getRenderId() === null ) {
77 $parserOutput->setRenderId( $this->globalIdGenerator->newUUIDv1() );
78 }
79 // Revision ID and Revision Timestamp are set here so that we don't
80 // have to load the revision row on view.
81 if ( $parserOutput->getCacheRevisionId() === null && $revId !== null ) {
82 $parserOutput->setCacheRevisionId( $revId );
83 }
84 if ( $parserOutput->getRevisionTimestamp() === null && $revTimestamp !== null ) {
85 $parserOutput->setRevisionTimestamp( $revTimestamp );
86 }
87 return $parserOutput;
88 }
89}
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(private readonly IContentHandlerFactory $contentHandlerFactory, private readonly 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.