diff --git a/extension.json b/extension.json index 52b627bb0..c2c56d4d3 100644 --- a/extension.json +++ b/extension.json @@ -120,7 +120,9 @@ "Cite": "includes/Cite.php", "CiteHooks": "includes/CiteHooks.php", "CiteDataModule": "includes/CiteDataModule.php", - "CiteCSSFileModule": "includes/CiteCSSFileModule.php" + "CiteCSSFileModule": "includes/CiteCSSFileModule.php", + "CiteParserHooks": "includes/CiteParserHooks.php", + "CiteParserTagHooks": "includes/CiteParserTagHooks.php" }, "TrackingCategories": [ "cite-tracking-category-cite-error" diff --git a/includes/Cite.php b/includes/Cite.php index 1c38f1efa..07e014f3c 100644 --- a/includes/Cite.php +++ b/includes/Cite.php @@ -1144,8 +1144,8 @@ class Cite { } $parser->extCite = clone $this; - $parser->setHook( 'ref', [ $parser->extCite, 'ref' ] ); - $parser->setHook( 'references', [ $parser->extCite, 'references' ] ); + $parser->setHook( 'ref', 'CiteParserTagHooks::ref' ); + $parser->setHook( 'references', 'CiteParserTagHooks::references' ); // Clear the state, making sure it will actually work. $parser->extCite->mInCite = false; diff --git a/includes/CiteHooks.php b/includes/CiteHooks.php index df1fc236c..91172fe49 100644 --- a/includes/CiteHooks.php +++ b/includes/CiteHooks.php @@ -25,15 +25,15 @@ class CiteHooks { $parser->extCite = new Cite(); if ( !self::$hooksInstalled ) { - $wgHooks['ParserClearState'][] = [ $parser->extCite, 'clearState' ]; - $wgHooks['ParserCloned'][] = [ $parser->extCite, 'cloneState' ]; - $wgHooks['ParserAfterParse'][] = [ $parser->extCite, 'checkRefsNoReferences', true ]; - $wgHooks['ParserBeforeTidy'][] = [ $parser->extCite, 'checkRefsNoReferences', false ]; + $wgHooks['ParserClearState'][] = 'CiteParserHooks::onParserClearState'; + $wgHooks['ParserCloned'][] = 'CiteParserHooks::onParserCloned'; + $wgHooks['ParserAfterParse'][] = 'CiteParserHooks::onParserAfterParse'; + $wgHooks['ParserBeforeTidy'][] = 'CiteParserHooks::onParserBeforeTidy'; self::$hooksInstalled = true; } - $parser->setHook( 'ref', [ $parser->extCite, 'ref' ] ); - $parser->setHook( 'references', [ $parser->extCite, 'references' ] ); + $parser->setHook( 'ref', 'CiteParserTagHooks::ref' ); + $parser->setHook( 'references', 'CiteParserTagHooks::references' ); } /** diff --git a/includes/CiteParserHooks.php b/includes/CiteParserHooks.php new file mode 100644 index 000000000..278c6cddb --- /dev/null +++ b/includes/CiteParserHooks.php @@ -0,0 +1,52 @@ +extCite; + $cite->clearState( $parser ); + } + + /** + * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserCloned + * + * @param Parser $parser + */ + public static function onParserCloned( Parser $parser ) { + /** @var Cite $cite */ + $cite = $parser->extCite; + $cite->cloneState( $parser ); + } + + /** + * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserAfterParse + * + * @param Parser $parser + * @param string &$text + * @param StripState $stripState + */ + public static function onParserAfterParse( Parser $parser, &$text, $stripState ) { + /** @var Cite $cite */ + $cite = $parser->extCite; + $cite->checkRefsNoReferences( true, $parser, $text ); + } + + /** + * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserBeforeTidy + * + * @param Parser $parser + * @param string &$text + */ + public static function onParserBeforeTidy( Parser $parser, &$text ) { + /** @var Cite $cite */ + $cite = $parser->extCite; + $cite->checkRefsNoReferences( false, $parser, $text ); + } + +} diff --git a/includes/CiteParserTagHooks.php b/includes/CiteParserTagHooks.php new file mode 100644 index 000000000..3246c48d0 --- /dev/null +++ b/includes/CiteParserTagHooks.php @@ -0,0 +1,39 @@ + tag. + * + * @param string|null $content Raw wikitext content of the tag. + * @param string[] $attributes + * @param Parser $parser + * @param PPFrame $frame + * + * @return string + */ + public static function ref( $content, array $attributes, Parser $parser, PPFrame $frame ) { + /** @var Cite $cite */ + $cite = $parser->extCite; + // @phan-suppress-next-line SecurityCheck-XSS False positive + return $cite->ref( $content, $attributes, $parser, $frame ); + } + + /** + * Parser hook for the tag. + * + * @param string|null $content Raw wikitext content of the tag. + * @param string[] $attributes + * @param Parser $parser + * @param PPFrame $frame + * + * @return string + */ + public static function references( $content, array $attributes, Parser $parser, PPFrame $frame ) { + /** @var Cite $cite */ + $cite = $parser->extCite; + // @phan-suppress-next-line SecurityCheck-XSS False positive + return $cite->references( $content, $attributes, $parser, $frame ); + } + +}