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
This commit is contained in:
Thiemo Kreuz 2019-10-25 10:34:35 +02:00
parent a037f76317
commit 28dd373d24
3 changed files with 32 additions and 31 deletions

View file

@ -31,7 +31,7 @@
} }
}, },
"Hooks": { "Hooks": {
"ParserFirstCallInit": "Cite::setHooks", "ParserFirstCallInit": "CiteHooks::onParserFirstCallInit",
"ContentHandlerDefaultModelFor": "CiteHooks::onContentHandlerDefaultModelFor", "ContentHandlerDefaultModelFor": "CiteHooks::onContentHandlerDefaultModelFor",
"ResourceLoaderTestModules": "CiteHooks::onResourceLoaderTestModules", "ResourceLoaderTestModules": "CiteHooks::onResourceLoaderTestModules",
"ResourceLoaderRegisterModules": "CiteHooks::onResourceLoaderRegisterModules", "ResourceLoaderRegisterModules": "CiteHooks::onResourceLoaderRegisterModules",

View file

@ -145,7 +145,7 @@ class Cite {
* True when the ParserAfterParse hook has been called. * True when the ParserAfterParse hook has been called.
* Used to avoid doing anything in ParserBeforeTidy. * Used to avoid doing anything in ParserBeforeTidy.
* *
* @var boolean * @var bool
*/ */
private $mHaveAfterParse = false; private $mHaveAfterParse = false;
@ -153,7 +153,7 @@ class Cite {
* True when a <ref> tag is being processed. * True when a <ref> tag is being processed.
* Used to avoid infinite recursion * Used to avoid infinite recursion
* *
* @var boolean * @var bool
*/ */
public $mInCite = false; public $mInCite = false;
@ -161,7 +161,7 @@ class Cite {
* True when a <references> tag is being processed. * True when a <references> tag is being processed.
* Used to detect the use of <references> to define refs * Used to detect the use of <references> to define refs
* *
* @var boolean * @var bool
*/ */
public $mInReferences = false; public $mInReferences = false;
@ -193,12 +193,6 @@ class Cite {
*/ */
private $mBumpRefData = false; private $mBumpRefData = false;
/**
* Did we install us into $wgHooks yet?
* @var Boolean
*/
private static $hooksInstalled = false;
/** /**
* Callback function for <ref> * Callback function for <ref>
* *
@ -1292,27 +1286,6 @@ class Cite {
$this->mParser->getOutput()->setExtensionData( self::EXT_DATA_KEY, $savedRefs ); $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 * Return an error message based on an error ID and parses it
* *

View file

@ -8,6 +8,34 @@ use MediaWiki\MediaWikiServices;
class CiteHooks { 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 * 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 * only affects validation and UI when saving and editing, not loading the