Remove lazy registration of Parser related hooks

To be honest I don't get why this lazy registration was done in the
first place. None of the 4 other hooks should ever be called before
the ParserFirstCallInit hook got called.

Also, under which circumstances can the ParserFirstCallInit hook be
called more than once?

Both scenarios would be wrong, as far as I'm concerned. Either I'm
missing something, or this code can indeed be simplified. Maybe it was
something to make it more compatible with older MediaWiki versions?

The only reason I can think of is: in all situations that do not
involve a parser, having the 4 extra hooks registered is pointless.
Does this waste space and/or runtime in the $wgHooks registry?

Change-Id: I5ef1495f4ce7bce940fa5f8e700af3d2c4851a01
This commit is contained in:
Thiemo Kreuz 2019-11-04 17:23:29 +01:00
parent c75fe5dccd
commit 7ce10d7539
5 changed files with 46 additions and 46 deletions

View file

@ -31,7 +31,11 @@
} }
}, },
"Hooks": { "Hooks": {
"ParserFirstCallInit": "CiteHooks::onParserFirstCallInit", "ParserAfterParse": "CiteParserHooks::onParserAfterParse",
"ParserBeforeTidy": "CiteParserHooks::onParserBeforeTidy",
"ParserClearState": "CiteParserHooks::onParserClearState",
"ParserCloned": "CiteParserHooks::onParserCloned",
"ParserFirstCallInit": "CiteParserHooks::onParserFirstCallInit",
"ContentHandlerDefaultModelFor": "CiteHooks::onContentHandlerDefaultModelFor", "ContentHandlerDefaultModelFor": "CiteHooks::onContentHandlerDefaultModelFor",
"ResourceLoaderTestModules": "CiteHooks::onResourceLoaderTestModules", "ResourceLoaderTestModules": "CiteHooks::onResourceLoaderTestModules",
"ResourceLoaderRegisterModules": "CiteHooks::onResourceLoaderRegisterModules", "ResourceLoaderRegisterModules": "CiteHooks::onResourceLoaderRegisterModules",

View file

@ -9,33 +9,6 @@ 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'][] = 'CiteParserHooks::onParserClearState';
$wgHooks['ParserCloned'][] = 'CiteParserHooks::onParserCloned';
$wgHooks['ParserAfterParse'][] = 'CiteParserHooks::onParserAfterParse';
$wgHooks['ParserBeforeTidy'][] = 'CiteParserHooks::onParserBeforeTidy';
self::$hooksInstalled = true;
}
CiteParserTagHooks::initialize( $parser );
}
/** /**
* 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

View file

@ -2,6 +2,16 @@
class CiteParserHooks { class CiteParserHooks {
/**
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserFirstCallInit
*
* @param Parser $parser
*/
public static function onParserFirstCallInit( Parser $parser ) {
$parser->extCite = new Cite();
CiteParserTagHooks::initialize( $parser );
}
/** /**
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserClearState * @see https://www.mediawiki.org/wiki/Manual:Hooks/ParserClearState
* *

View file

@ -6,7 +6,6 @@ use Cite;
use CiteHooks; use CiteHooks;
use HashConfig; use HashConfig;
use LinksUpdate; use LinksUpdate;
use Parser;
use ParserOutput; use ParserOutput;
use ResourceLoader; use ResourceLoader;
use Title; use Title;
@ -23,23 +22,6 @@ class CiteHooksTest extends \MediaWikiUnitTestCase {
$wgCiteStoreReferencesData = true; $wgCiteStoreReferencesData = true;
} }
/**
* @covers ::onParserFirstCallInit
*/
public function testOnParserFirstCallInit() {
$parser = $this->createMock( Parser::class );
$parser->expects( $this->exactly( 2 ) )
->method( 'setHook' )
->withConsecutive(
[ 'ref', $this->isType( 'callable' ) ],
[ 'references', $this->isType( 'callable' ) ]
);
CiteHooks::onParserFirstCallInit( $parser );
$this->assertInstanceOf( Cite::class, $parser->extCite );
}
/** /**
* @covers ::onContentHandlerDefaultModelFor * @covers ::onContentHandlerDefaultModelFor
*/ */

View file

@ -0,0 +1,31 @@
<?php
namespace Cite\Test\Unit;
use Cite;
use CiteParserHooks;
use Parser;
/**
* @coversDefaultClass \CiteParserHooks
*/
class CiteParserHooksTest extends \MediaWikiUnitTestCase {
/**
* @covers ::onParserFirstCallInit
*/
public function testOnParserFirstCallInit() {
$parser = $this->createMock( Parser::class );
$parser->expects( $this->exactly( 2 ) )
->method( 'setHook' )
->withConsecutive(
[ 'ref', $this->isType( 'callable' ) ],
[ 'references', $this->isType( 'callable' ) ]
);
CiteParserHooks::onParserFirstCallInit( $parser );
$this->assertInstanceOf( Cite::class, $parser->extCite );
}
}