2021-01-29 17:09:52 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* DiscussionTools parser hooks
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Hooks;
|
|
|
|
|
2021-09-07 20:51:35 +00:00
|
|
|
use ConfigFactory;
|
2021-01-29 18:31:27 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentFormatter;
|
2021-01-29 17:09:52 +00:00
|
|
|
use MediaWiki\Hook\ParserAfterTidyHook;
|
|
|
|
use Parser;
|
|
|
|
|
2022-03-15 16:19:57 +00:00
|
|
|
class ParserHooks implements ParserAfterTidyHook {
|
2021-09-07 20:51:35 +00:00
|
|
|
/** @var ConfigFactory */
|
|
|
|
private $configFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param ConfigFactory $configFactory
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
ConfigFactory $configFactory
|
|
|
|
) {
|
|
|
|
$this->configFactory = $configFactory;
|
|
|
|
}
|
|
|
|
|
2021-05-05 02:02:20 +00:00
|
|
|
/**
|
2022-03-15 16:19:57 +00:00
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterTidy
|
2021-05-05 02:02:20 +00:00
|
|
|
*
|
|
|
|
* @param Parser $parser
|
|
|
|
* @param string &$text
|
|
|
|
*/
|
2022-03-15 16:19:57 +00:00
|
|
|
public function onParserAfterTidy( $parser, &$text ) {
|
|
|
|
if ( $parser->getOptions()->getInterfaceMessage() || $parser->getOptions()->getIsPreview() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-05 02:02:20 +00:00
|
|
|
$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() ) {
|
2021-09-07 20:51:35 +00:00
|
|
|
$dtConfig = $this->configFactory->makeConfig( 'discussiontools' );
|
2021-05-05 02:02:20 +00:00
|
|
|
$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 );
|
|
|
|
}
|
|
|
|
}
|
2021-09-21 07:22:49 +00:00
|
|
|
|
2021-08-18 18:54:33 +00:00
|
|
|
// 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.
|
2022-03-15 16:19:57 +00:00
|
|
|
if ( HookUtils::isAvailableForTitle( $title ) ) {
|
2021-09-27 19:38:09 +00:00
|
|
|
// This modifies $text
|
2022-01-11 15:50:44 +00:00
|
|
|
CommentFormatter::addDiscussionTools( $text, $parser->getOutput(), $parser->getTitle() );
|
2021-09-27 19:38:09 +00:00
|
|
|
|
2021-08-20 16:48:33 +00:00
|
|
|
$parser->getOutput()->addModuleStyles( [
|
|
|
|
'ext.discussionTools.init.styles',
|
|
|
|
] );
|
2021-01-29 17:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|