2021-01-29 17:09:52 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* DiscussionTools parser hooks
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
* @license MIT
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools\Hooks;
|
|
|
|
|
2022-09-02 22:24:20 +00:00
|
|
|
use Config;
|
2021-09-07 20:51:35 +00:00
|
|
|
use ConfigFactory;
|
2021-01-29 18:31:27 +00:00
|
|
|
use MediaWiki\Extension\DiscussionTools\CommentFormatter;
|
2022-03-24 16:45:14 +00:00
|
|
|
use MediaWiki\Hook\GetDoubleUnderscoreIDsHook;
|
2021-01-29 17:09:52 +00:00
|
|
|
use MediaWiki\Hook\ParserAfterTidyHook;
|
|
|
|
use Parser;
|
|
|
|
|
2022-03-24 16:45:14 +00:00
|
|
|
class ParserHooks implements
|
|
|
|
GetDoubleUnderscoreIDsHook,
|
|
|
|
ParserAfterTidyHook
|
|
|
|
{
|
2022-10-21 19:34:18 +00:00
|
|
|
|
|
|
|
private Config $config;
|
2021-09-07 20:51:35 +00:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
ConfigFactory $configFactory
|
|
|
|
) {
|
2022-09-02 22:24:20 +00:00
|
|
|
$this->config = $configFactory->makeConfig( 'discussiontools' );
|
2021-09-07 20:51:35 +00:00
|
|
|
}
|
|
|
|
|
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 ) {
|
2022-06-01 21:43:42 +00:00
|
|
|
if ( $parser->getOptions()->getInterfaceMessage() ) {
|
2022-03-15 16:19:57 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-05 02:02:20 +00:00
|
|
|
$title = $parser->getTitle();
|
2022-04-07 00:00:05 +00:00
|
|
|
$pout = $parser->getOutput();
|
2021-05-05 02:02:20 +00:00
|
|
|
|
|
|
|
// 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).
|
2022-04-07 00:00:05 +00:00
|
|
|
if ( $title->isTalkPage() || $pout->getNewSection() ) {
|
2022-09-02 22:24:20 +00:00
|
|
|
$talkExpiry = $this->config->get( 'DiscussionToolsTalkPageParserCacheExpiry' );
|
2021-05-05 02:02:20 +00:00
|
|
|
// Override parser cache expiry of talk pages (T280605).
|
|
|
|
// Note, this can only shorten it. MediaWiki ignores values higher than the default.
|
|
|
|
if ( $talkExpiry > 0 ) {
|
2022-04-07 00:00:05 +00:00
|
|
|
$pout->updateCacheExpiry( $talkExpiry );
|
2021-05-05 02:02:20 +00:00
|
|
|
}
|
|
|
|
}
|
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-04-07 00:00:05 +00:00
|
|
|
CommentFormatter::addDiscussionTools( $text, $pout, $parser );
|
2021-09-27 19:38:09 +00:00
|
|
|
|
2022-08-02 19:24:35 +00:00
|
|
|
if ( $parser->getOptions()->getIsPreview() ) {
|
|
|
|
$text = CommentFormatter::removeInteractiveTools( $text );
|
2023-06-22 08:24:42 +00:00
|
|
|
// Suppress the empty state
|
|
|
|
$pout->setExtensionData( 'DiscussionTools-isEmptyTalkPage', null );
|
2022-08-02 19:24:35 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 00:00:05 +00:00
|
|
|
$pout->addModuleStyles( [
|
2021-08-20 16:48:33 +00:00
|
|
|
'ext.discussionTools.init.styles',
|
|
|
|
] );
|
2021-01-29 17:09:52 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-24 16:45:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/GetDoubleUnderscoreIDs
|
|
|
|
*
|
|
|
|
* @param string[] &$doubleUnderscoreIDs
|
|
|
|
* @return bool|void
|
|
|
|
*/
|
|
|
|
public function onGetDoubleUnderscoreIDs( &$doubleUnderscoreIDs ) {
|
|
|
|
$doubleUnderscoreIDs[] = 'archivedtalk';
|
2023-03-27 12:50:45 +00:00
|
|
|
$doubleUnderscoreIDs[] = 'notalk';
|
2022-03-24 16:45:14 +00:00
|
|
|
}
|
2021-01-29 17:09:52 +00:00
|
|
|
}
|