mediawiki-extensions-Discus.../includes/Hooks/ParserHooks.php
Bartosz Dziewoński 695a966a41 Remove unused non-parser-cache mode
Change-Id: Ief9f4153898b09a1ce15ccfdc8656dfad4642269
2021-10-07 17:59:10 +02:00

88 lines
2.7 KiB
PHP

<?php
/**
* DiscussionTools parser hooks
*
* @file
* @ingroup Extensions
* @license MIT
*/
namespace MediaWiki\Extension\DiscussionTools\Hooks;
use ConfigFactory;
use MediaWiki\Extension\DiscussionTools\CommentFormatter;
use MediaWiki\Hook\ParserAfterParseHook;
use MediaWiki\Hook\ParserAfterTidyHook;
use Parser;
use StripState;
class ParserHooks implements
ParserAfterParseHook,
ParserAfterTidyHook
{
/** @var ConfigFactory */
private $configFactory;
/**
* @param ConfigFactory $configFactory
*/
public function __construct(
ConfigFactory $configFactory
) {
$this->configFactory = $configFactory;
}
/**
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterParse
*
* @param Parser $parser
* @param string &$text
* @param StripState $stripState
*/
public function onParserAfterParse( $parser, &$text, $stripState ): void {
$title = $parser->getTitle();
// This condition must be unreliant on current enablement config or user preference.
// In other words, include parser output of talk pages with DT disabled.
//
// This is similar to HookUtils::isAvailableForTitle, but instead of querying the
// database for the latest metadata of a page that exists, we check metadata of
// the given ParserOutput object only (this runs before the edit is saved).
if ( $title->isTalkPage() || $parser->getOutput()->getNewSection() ) {
$dtConfig = $this->configFactory->makeConfig( 'discussiontools' );
$talkExpiry = $dtConfig->get( 'DiscussionToolsTalkPageParserCacheExpiry' );
// Override parser cache expiry of talk pages (T280605).
// Note, this can only shorten it. MediaWiki ignores values higher than the default.
if ( $talkExpiry > 0 ) {
$parser->getOutput()->updateCacheExpiry( $talkExpiry );
}
}
}
/**
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterTidy
*
* @param Parser $parser
* @param string &$text
*/
public function onParserAfterTidy( $parser, &$text ) {
if ( $parser->getOptions()->getInterfaceMessage() || $parser->getOptions()->getIsPreview() ) {
return;
}
// Always apply the DOM transform if DiscussionTools are available for this page,
// to allow linking to individual comments from Echo 'mention' and 'edit-user-talk'
// notifications (T253082, T281590), and to reduce parser cache fragmentation (T279864).
// The extra buttons are hidden in CSS (ext.discussionTools.init.styles module) when
// the user doesn't have DiscussionTools features enabled.
if ( HookUtils::isAvailableForTitle( $parser->getTitle() ) ) {
// This modifies $text
CommentFormatter::addDiscussionTools( $text, $parser->getOutput() );
$parser->getOutput()->addModuleStyles( [
'ext.discussionTools.init.styles',
] );
}
}
}