aboutsummaryrefslogtreecommitdiffstats
path: root/includes/content
diff options
context:
space:
mode:
authorC. Scott Ananian <cscott@cscott.net>2023-09-14 12:11:20 -0400
committerC. Scott Ananian <cscott@cscott.net>2024-02-07 21:22:06 -0500
commit0de13d766200708d6152551a0daf0e56b6d77adc (patch)
treef94bc30181de9bfda4259e8f77a417867887d217 /includes/content
parent2e3ee2af3fc071ed7d321e65abbd60b485d9d4a3 (diff)
downloadmediawikicore-0de13d766200708d6152551a0daf0e56b6d77adc.tar.gz
mediawikicore-0de13d766200708d6152551a0daf0e56b6d77adc.zip
Add ParserOutput::{get,set}RenderId() and set render id in ContentRenderer
Set the render ID for each parse stored into cache so that we are able to identify a specific parse when there are dependencies (for example in an edit based on that parse). This is recorded as a property added to the ParserOutput, not the parent CacheTime interface. Even though the render ID is /related/ to the CacheTime interface, CacheTime is also used directly as a parser cache key, and the UUID should not be part of the lookup key. In general we are trying to move the location where these cache properties are set as early as possible, so we check at each location to ensure we don't overwrite a previously-set value. Eventually we can convert most of these checks into assertions that the cache properties have already been set (T350538). The primary location for setting cache properties is the ContentRenderer. Moved setting the revision timestamp into ContentRenderer as well, as it was set along the same code paths. An extra parameter was added to ContentRenderer::getParserOutput() to support this. Added merge code to ParserOutput::mergeInternalMetaDataFrom() which should ensure that cache time, revision, timestamp, and render id are all set properly when multiple slots are combined together in MCR. In order to ensure the render ID is set on all codepaths we needed to plumb the GlobalIdGenerator service into ContentRenderer, ParserCache, ParserCacheFactory, and RevisionOutputCache. Eventually (T350538) it should only be necessary in the ContentRenderer. Bug: T350538 Bug: T349868 Followup-To: Ic9b7cc0fcf365e772b7d080d76a065e3fd585f80 Change-Id: I72c5e6f86b7f081ab5ce7a56f5365d2f75067a78
Diffstat (limited to 'includes/content')
-rw-r--r--includes/content/Renderer/ContentRenderer.php53
1 files changed, 49 insertions, 4 deletions
diff --git a/includes/content/Renderer/ContentRenderer.php b/includes/content/Renderer/ContentRenderer.php
index 5b8fbaccecf6..10ea597df47b 100644
--- a/includes/content/Renderer/ContentRenderer.php
+++ b/includes/content/Renderer/ContentRenderer.php
@@ -5,7 +5,9 @@ use Content;
use MediaWiki\Content\IContentHandlerFactory;
use MediaWiki\Page\PageReference;
use MediaWiki\Parser\ParserOutput;
+use MediaWiki\Revision\RevisionRecord;
use ParserOptions;
+use Wikimedia\UUID\GlobalIdGenerator;
/**
* A service to render content.
@@ -16,11 +18,18 @@ class ContentRenderer {
/** @var IContentHandlerFactory */
private $contentHandlerFactory;
+ private GlobalIdGenerator $globalIdGenerator;
+
/**
* @param IContentHandlerFactory $contentHandlerFactory
+ * @param GlobalIdGenerator $globalIdGenerator
*/
- public function __construct( IContentHandlerFactory $contentHandlerFactory ) {
+ public function __construct(
+ IContentHandlerFactory $contentHandlerFactory,
+ GlobalIdGenerator $globalIdGenerator
+ ) {
$this->contentHandlerFactory = $contentHandlerFactory;
+ $this->globalIdGenerator = $globalIdGenerator;
}
/**
@@ -28,22 +37,58 @@ class ContentRenderer {
*
* @param Content $content
* @param PageReference $page
- * @param int|null $revId
+ * @param RevisionRecord|int|null $revision
* @param ParserOptions|null $parserOptions
* @param bool $generateHtml
*
* @return ParserOutput
+ * @note Passing an integer as $rev was deprecated in MW 1.42
*/
public function getParserOutput(
Content $content,
PageReference $page,
- ?int $revId = null,
+ $revision = null,
?ParserOptions $parserOptions = null,
bool $generateHtml = true
): ParserOutput {
+ $revId = null;
+ $revTimestamp = null;
+ if ( is_int( $revision ) ) {
+ wfDeprecated( __METHOD__ . ' with integer revision id', '1.42' );
+ $revId = $revision;
+ } elseif ( $revision !== null ) {
+ $revId = $revision->getId();
+ $revTimestamp = $revision->getTimestamp();
+ }
+ $cacheTime = wfTimestampNow();
$contentHandler = $this->contentHandlerFactory->getContentHandler( $content->getModel() );
$cpoParams = new ContentParseParams( $page, $revId, $parserOptions, $generateHtml );
- return $contentHandler->getParserOutput( $content, $cpoParams );
+ $parserOutput = $contentHandler->getParserOutput( $content, $cpoParams );
+ // Set the cache parameters, if not previously set.
+ //
+ // It is expected that this will be where most are set for the first
+ // time, but a ContentHandler can (for example) use a content-based
+ // hash for the render id by setting it inside
+ // ContentHandler::getParserOutput(); any such custom render id
+ // will not be overwritten here. Similarly, a ContentHandler can
+ // continue to use the semi-documented feature of ::setCacheTime(-1)
+ // to indicate "not cacheable", and that will not be overwritten
+ // either.
+ if ( !$parserOutput->hasCacheTime() ) {
+ $parserOutput->setCacheTime( $cacheTime );
+ }
+ if ( $parserOutput->getRenderId() === null ) {
+ $parserOutput->setRenderId( $this->globalIdGenerator->newUUIDv1() );
+ }
+ // Revision ID and Revision Timestamp are set here so that we don't
+ // have to load the revision row on view.
+ if ( $parserOutput->getCacheRevisionId() === null && $revId !== null ) {
+ $parserOutput->setCacheRevisionId( $revId );
+ }
+ if ( $parserOutput->getTimestamp() === null && $revTimestamp !== null ) {
+ $parserOutput->setTimestamp( $revTimestamp );
+ }
+ return $parserOutput;
}
}