Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
ParserHooks
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 5
462
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 transformHtml
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
42
 onParserOutputPostCacheTransform
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
72
 onParserAfterTidy
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
30
 onGetDoubleUnderscoreIDs
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * DiscussionTools parser hooks
4 *
5 * @file
6 * @ingroup Extensions
7 * @license MIT
8 */
9
10namespace MediaWiki\Extension\DiscussionTools\Hooks;
11
12use MediaWiki\Config\Config;
13use MediaWiki\Config\ConfigFactory;
14use MediaWiki\Extension\DiscussionTools\BatchModifyElements;
15use MediaWiki\Extension\DiscussionTools\CommentFormatter;
16use MediaWiki\Hook\GetDoubleUnderscoreIDsHook;
17use MediaWiki\MainConfigNames;
18use MediaWiki\MediaWikiServices;
19use MediaWiki\Parser\Hook\ParserAfterTidyHook;
20use MediaWiki\Parser\Hook\ParserOutputPostCacheTransformHook;
21use MediaWiki\Parser\Parser;
22use MediaWiki\Parser\ParserOptions;
23use MediaWiki\Parser\ParserOutput;
24use MediaWiki\Parser\ParserOutputFlags;
25use MediaWiki\Title\Title;
26
27class ParserHooks implements
28    ParserOutputPostCacheTransformHook,
29    GetDoubleUnderscoreIDsHook,
30    ParserAfterTidyHook
31{
32
33    private readonly Config $config;
34
35    public function __construct(
36        ConfigFactory $configFactory
37    ) {
38        $this->config = $configFactory->makeConfig( 'discussiontools' );
39    }
40
41    private function transformHtml(
42        ParserOutput $pout, string &$html, Title $title, bool $isPreview
43    ): void {
44        // This condition must not be reliant on current enablement config or user preference.
45        // In other words, include parser output of talk pages with DT disabled.
46        //
47        // This is similar to HookUtils::isAvailableForTitle, but instead of querying the
48        // database for the latest metadata of a page that exists, we check metadata of
49        // the given ParserOutput object only (this runs before the edit is saved).
50        if ( $title->isTalkPage() || $pout->getNewSection() ) {
51            $talkExpiry = $this->config->get( 'DiscussionToolsTalkPageParserCacheExpiry' );
52            // Override parser cache expiry of talk pages (T280605).
53            // Note, this can only shorten it. MediaWiki ignores values higher than the default.
54            // NOTE: this currently has no effect for Parsoid read
55            // views, since parsoid executes this method as a
56            // post-cache transform.  *However* future work may allow
57            // caching of intermediate results of the "post cache"
58            // transformation pipeline, in which case this code will
59            // again be effective. (More: T350626)
60            if ( $talkExpiry > 0 ) {
61                $pout->updateCacheExpiry( $talkExpiry );
62            }
63        }
64
65        // Always apply the DOM transform if DiscussionTools are available for this page,
66        // to allow linking to individual comments from Echo 'mention' and 'edit-user-talk'
67        // notifications (T253082, T281590), and to reduce parser cache fragmentation (T279864).
68        // The extra buttons are hidden in CSS (ext.discussionTools.init.styles module) when
69        // the user doesn't have DiscussionTools features enabled.
70        if ( HookUtils::isAvailableForTitle( $title ) ) {
71            // This modifies $html
72            CommentFormatter::addDiscussionTools( $html, $pout, $title );
73
74            if ( $isPreview ) {
75                $batchModifyElements = new BatchModifyElements();
76                CommentFormatter::removeInteractiveTools( $batchModifyElements );
77                $html = $batchModifyElements->apply( $html );
78                // Suppress the empty state
79                $pout->setExtensionData( 'DiscussionTools-isEmptyTalkPage', null );
80                $pout->setExtensionData( 'DiscussionTools-isPreview', true );
81            }
82
83            $pout->addModuleStyles( [ 'ext.discussionTools.init.styles' ] );
84        }
85    }
86
87    /**
88     * For now, this hook only runs on Parsoid HTML. Eventually, this is likely
89     * to be run for legacy HTML but that requires ParserCache storage to be allocated
90     * for DiscussionTools HTML which will be perused separately.
91     *
92     * @inheritDoc
93     */
94    public function onParserOutputPostCacheTransform( $parserOutput, &$text, &$options ): void {
95        $popts = $options[ 'parserOptions' ] ?? null;
96        if ( $popts instanceof ParserOptions && $popts->isMessage() ) {
97            return;
98        }
99
100        // as per Id73a1b5751cfc055e84188bcb19583c72b84032f, this is always set when transforming HTML
101        // in DiscussionTools, so it's a reasonable way to not execute it twice for legacy content coming
102        // from the ParserCache
103        if ( ( $parserOutput->getExtensionData( 'DiscussionTools-isEmptyTalkPage' ) !== null ||
104                // well, there is an exception to that rule: if we're in preview mode, AND we're previewing in legacy
105                // mode, we reset DiscussionTools-isEmptyTalkPage to null - so in that case we also set isPreview, so
106                // that we can catch this case here. By definition this can't come from the cache; so there's no risk
107                // that the newly introduced flag isn't set if it is needed.
108                // TODO this MUST disappear once ParserAfterTidy is removed - this is only a temporary fix that won't
109                // be necessary once that happens.
110                $parserOutput->getExtensionData( 'DiscussionTools-isPreview' ) ) &&
111             // T419830: sometimes parsoid recursive processes a small
112             // component of the page?  But we should always run this pass
113             // if we're using Parsoid.
114             !( $popts instanceof ParserOptions && $popts->getUseParsoid() )
115        ) {
116            return;
117        }
118
119        $linkTarget = $parserOutput->getTitle();
120        if ( !$linkTarget ) {
121            return;
122        }
123
124        $isPreview = $parserOutput->getOutputFlag( ParserOutputFlags::IS_PREVIEW );
125        $title = Title::newFromLinkTarget( $linkTarget );
126        $this->transformHtml( $parserOutput, $text, $title, $isPreview );
127    }
128
129    /**
130     * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterTidy
131     *
132     * @param Parser $parser
133     * @param string &$text
134     */
135    public function onParserAfterTidy( $parser, &$text ) {
136        $pOpts = $parser->getOptions();
137        if ( $pOpts->isMessage() ) {
138            return;
139        }
140
141        $output = $parser->getOutput();
142        // if we have a post-processing cache for legacy parses, we use the post-processing pipeline instead
143        // (and cache it there)
144        // we also don't want to try to do the post-processing if we're getting a page from the cache that
145        // doesn't yet hold its title.
146        if ( $output->getTitle() !== null &&
147            MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::UsePostprocCacheLegacy ) ) {
148            return;
149        }
150        // Don't invoke this hook from the ::parseExtensionTagAsTopLevelDoc()
151        // method in Parsoid, either.
152        if ( $pOpts->getUseParsoid() ) {
153            return;
154        }
155
156        $this->transformHtml(
157            $output, $text, $parser->getTitle(), $pOpts->getIsPreview()
158        );
159    }
160
161    /**
162     * @see https://www.mediawiki.org/wiki/Manual:Hooks/GetDoubleUnderscoreIDs
163     *
164     * @param string[] &$doubleUnderscoreIDs
165     * @return bool|void
166     */
167    public function onGetDoubleUnderscoreIDs( &$doubleUnderscoreIDs ) {
168        $doubleUnderscoreIDs[] = 'archivedtalk';
169        $doubleUnderscoreIDs[] = 'notalk';
170    }
171}