| 1 |
<?php |
| 2 |
namespace MediaWiki\Content\Renderer; |
| 3 |
|
| 4 |
use MediaWiki\Content\Content; |
| 5 |
use MediaWiki\Content\IContentHandlerFactory; |
| 6 |
use MediaWiki\Page\PageReference; |
| 7 |
use MediaWiki\Parser\ParserOptions; |
| 8 |
use MediaWiki\Parser\ParserOutput; |
| 9 |
use MediaWiki\Revision\RevisionRecord; |
| 10 |
use Wikimedia\UUID\GlobalIdGenerator; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class ContentRenderer { |
| 18 |
|
| 19 |
private $contentHandlerFactory; |
| 20 |
|
| 21 |
private GlobalIdGenerator $globalIdGenerator; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public function __construct( |
| 28 |
IContentHandlerFactory $contentHandlerFactory, |
| 29 |
GlobalIdGenerator $globalIdGenerator |
| 30 |
) { |
| 31 |
$this->contentHandlerFactory = $contentHandlerFactory; |
| 32 |
$this->globalIdGenerator = $globalIdGenerator; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 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 |
|
| 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 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
if ( !$parserOutput->hasCacheTime() ) { |
| 91 |
$parserOutput->setCacheTime( $cacheTime ); |
| 92 |
} |
| 93 |
if ( $parserOutput->getRenderId() === null ) { |
| 94 |
$parserOutput->setRenderId( $this->globalIdGenerator->newUUIDv1() ); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 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 |
} |