From 28dd373d249d606077e36dea3d3559bd9c27887c Mon Sep 17 00:00:00 2001 From: Thiemo Kreuz Date: Fri, 25 Oct 2019 10:34:35 +0200 Subject: [PATCH] Move misplaced ParserFirstCallInit hook handler to CiteHooks All other hook handlers are in the dedicated CiteHooks class. Main motivation here is to make the huge Cite class smaller, especially by removing static code that does not rely on anything else the class does. Bug: T236260 Change-Id: If0b3f6c989e44283428cda4b2c4d8d5303385d22 --- extension.json | 2 +- includes/Cite.php | 33 +++------------------------------ includes/CiteHooks.php | 28 ++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 31 deletions(-) diff --git a/extension.json b/extension.json index 3d57b4599..c3bfb649b 100644 --- a/extension.json +++ b/extension.json @@ -31,7 +31,7 @@ } }, "Hooks": { - "ParserFirstCallInit": "Cite::setHooks", + "ParserFirstCallInit": "CiteHooks::onParserFirstCallInit", "ContentHandlerDefaultModelFor": "CiteHooks::onContentHandlerDefaultModelFor", "ResourceLoaderTestModules": "CiteHooks::onResourceLoaderTestModules", "ResourceLoaderRegisterModules": "CiteHooks::onResourceLoaderRegisterModules", diff --git a/includes/Cite.php b/includes/Cite.php index 4a874039a..75d02a8e1 100644 --- a/includes/Cite.php +++ b/includes/Cite.php @@ -145,7 +145,7 @@ class Cite { * True when the ParserAfterParse hook has been called. * Used to avoid doing anything in ParserBeforeTidy. * - * @var boolean + * @var bool */ private $mHaveAfterParse = false; @@ -153,7 +153,7 @@ class Cite { * True when a tag is being processed. * Used to avoid infinite recursion * - * @var boolean + * @var bool */ public $mInCite = false; @@ -161,7 +161,7 @@ class Cite { * True when a tag is being processed. * Used to detect the use of to define refs * - * @var boolean + * @var bool */ public $mInReferences = false; @@ -193,12 +193,6 @@ class Cite { */ private $mBumpRefData = false; - /** - * Did we install us into $wgHooks yet? - * @var Boolean - */ - private static $hooksInstalled = false; - /** * Callback function for * @@ -1292,27 +1286,6 @@ class Cite { $this->mParser->getOutput()->setExtensionData( self::EXT_DATA_KEY, $savedRefs ); } - /** - * Initialize the parser hooks - * - * @param Parser $parser - */ - public static function setHooks( Parser $parser ) { - global $wgHooks; - - $parser->extCite = new self(); - - 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 ]; - self::$hooksInstalled = true; - } - $parser->setHook( 'ref', [ $parser->extCite, 'ref' ] ); - $parser->setHook( 'references', [ $parser->extCite, 'references' ] ); - } - /** * Return an error message based on an error ID and parses it * diff --git a/includes/CiteHooks.php b/includes/CiteHooks.php index ab50ee8a4..d1ebbf4bd 100644 --- a/includes/CiteHooks.php +++ b/includes/CiteHooks.php @@ -8,6 +8,34 @@ use MediaWiki\MediaWikiServices; class CiteHooks { + /** + * Did we install us into $wgHooks yet? + * @var bool + */ + private static $hooksInstalled = false; + + /** + * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserFirstCallInit + * + * @param Parser $parser + */ + public static function onParserFirstCallInit( Parser $parser ) { + global $wgHooks; + + $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 ]; + self::$hooksInstalled = true; + } + + $parser->setHook( 'ref', [ $parser->extCite, 'ref' ] ); + $parser->setHook( 'references', [ $parser->extCite, 'references' ] ); + } + /** * Convert the content model of a message that is actually JSON to JSON. This * only affects validation and UI when saving and editing, not loading the