| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
namespace MediaWiki\Extension\DiscussionTools\Hooks; |
| 11 |
|
| 12 |
use MediaWiki\Config\Config; |
| 13 |
use MediaWiki\Config\ConfigFactory; |
| 14 |
use MediaWiki\Extension\DiscussionTools\CommentFormatter; |
| 15 |
use MediaWiki\Hook\GetDoubleUnderscoreIDsHook; |
| 16 |
use MediaWiki\Hook\ParserAfterTidyHook; |
| 17 |
use MediaWiki\Hook\ParserOutputPostCacheTransformHook; |
| 18 |
use MediaWiki\Parser\Parser; |
| 19 |
use MediaWiki\Parser\ParserOutput; |
| 20 |
use MediaWiki\Parser\ParserOutputFlags; |
| 21 |
use MediaWiki\Parser\Parsoid\PageBundleParserOutputConverter; |
| 22 |
use MediaWiki\Parser\Parsoid\ParsoidParser; |
| 23 |
use MediaWiki\Title\Title; |
| 24 |
|
| 25 |
class ParserHooks implements |
| 26 |
ParserOutputPostCacheTransformHook, |
| 27 |
GetDoubleUnderscoreIDsHook, |
| 28 |
ParserAfterTidyHook |
| 29 |
{ |
| 30 |
|
| 31 |
private Config $config; |
| 32 |
|
| 33 |
public function __construct( |
| 34 |
ConfigFactory $configFactory |
| 35 |
) { |
| 36 |
$this->config = $configFactory->makeConfig( 'discussiontools' ); |
| 37 |
} |
| 38 |
|
| 39 |
private function transformHtml( |
| 40 |
ParserOutput $pout, string &$html, Title $title, bool $isPreview |
| 41 |
): void { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
if ( $title->isTalkPage() || $pout->getNewSection() ) { |
| 49 |
$talkExpiry = $this->config->get( 'DiscussionToolsTalkPageParserCacheExpiry' ); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
if ( $talkExpiry > 0 ) { |
| 59 |
$pout->updateCacheExpiry( $talkExpiry ); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
if ( HookUtils::isAvailableForTitle( $title ) ) { |
| 69 |
|
| 70 |
CommentFormatter::addDiscussionTools( $html, $pout, $title ); |
| 71 |
|
| 72 |
if ( $isPreview ) { |
| 73 |
$html = CommentFormatter::removeInteractiveTools( $html ); |
| 74 |
|
| 75 |
$pout->setExtensionData( 'DiscussionTools-isEmptyTalkPage', null ); |
| 76 |
} |
| 77 |
|
| 78 |
$pout->addModuleStyles( [ 'ext.discussionTools.init.styles' ] ); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function onParserOutputPostCacheTransform( $parserOutput, &$text, &$options ): void { |
| 90 |
$isPreview = $parserOutput->getOutputFlag( ParserOutputFlags::IS_PREVIEW ); |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
if ( PageBundleParserOutputConverter::hasPageBundle( $parserOutput ) ) { |
| 95 |
$titleDbKey = $parserOutput->getExtensionData( ParsoidParser::PARSOID_TITLE_KEY ); |
| 96 |
$title = Title::newFromDBkey( $titleDbKey ); |
| 97 |
'@phan-var Title $title'; |
| 98 |
$this->transformHtml( $parserOutput, $text, $title, $isPreview ); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public function onParserAfterTidy( $parser, &$text ) { |
| 109 |
$pOpts = $parser->getOptions(); |
| 110 |
if ( $pOpts->getInterfaceMessage() ) { |
| 111 |
return; |
| 112 |
} |
| 113 |
|
| 114 |
$this->transformHtml( |
| 115 |
$parser->getOutput(), $text, $parser->getTitle(), $pOpts->getIsPreview() |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function onGetDoubleUnderscoreIDs( &$doubleUnderscoreIDs ) { |
| 126 |
$doubleUnderscoreIDs[] = 'archivedtalk'; |
| 127 |
$doubleUnderscoreIDs[] = 'notalk'; |
| 128 |
} |
| 129 |
} |