From 7aaaf51dfdc5749792a8d11f0301277132ef12f7 Mon Sep 17 00:00:00 2001 From: Subramanya Sastry Date: Fri, 17 Nov 2023 15:20:46 -0600 Subject: [PATCH] ParserOutputPostCacheTransform: Don't reprocess content * getText() could be called multiple times on a ParserCache object which would fire the ParserOutputPostCacheTransform handler multiple times. But, I could not track down how this could happen right now. * As a separate issue, while conceptually there are no restrictions against calling getText() multiple times, there is a semantics and performance issue if that did actually happen. getText() does a bunch of transformations and makes no effort to avoid duplicate work. It will accumulate more transformations over time via the OutputTranform pipeline and it is preferable for getText() and/or the OutputTransform pipeline to guarantee semantics where the pipeline won't be run multiple times on the same content. That will free both hook handlers (like this) and the transforms themselves to avoid checks as in this patch. This patch should be reverted once such a change is made to core. Bug: T351461 Change-Id: If5dfa0954e3fd2b7dbea1ed29b475be07f0f3986 --- includes/Hooks/ParserHooks.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/includes/Hooks/ParserHooks.php b/includes/Hooks/ParserHooks.php index 0ac0d804c..541e04f4b 100644 --- a/includes/Hooks/ParserHooks.php +++ b/includes/Hooks/ParserHooks.php @@ -86,6 +86,12 @@ class ParserHooks implements * @inheritDoc */ public function onParserOutputPostCacheTransform( $parserOutput, &$text, &$options ): void { + if ( $parserOutput->getExtensionData( 'DiscussionTools-tocInfo' ) !== null ) { + // Since getText() could be called multiple times, return if we have + // already processed this ParserOutput object + return; + } + // NOTE: This is a temporary proxy for 'isPreview' flag in ParserOptions. // It is not clear whether 'editsectionEditLinks' is disabled only for previews. $isPreview = empty( $options['enableSectionEditLinks'] );