diff --git a/extension.json b/extension.json index 53f7405..1a9444f 100644 --- a/extension.json +++ b/extension.json @@ -16,8 +16,19 @@ "MessagesDirs": { "SecureLinkFixer": "i18n" }, - "Hooks": { - "LinkerMakeExternalLink": "MediaWiki\\SecureLinkFixer\\Hooks::onLinkerMakeExternalLink" + "HookHandlers": { + "main": { + "class": "MediaWiki\\SecureLinkFixer\\Hooks", + "services": [ + "HSTSPreloadLookup" + ] + } }, + "Hooks": { + "LinkerMakeExternalLink": "main" + }, + "ServiceWiringFiles": [ + "includes/ServiceWiring.php" + ], "manifest_version": 2 } diff --git a/includes/HSTSPreloadLookup.php b/includes/HSTSPreloadLookup.php index ca2fd98..72904fa 100644 --- a/includes/HSTSPreloadLookup.php +++ b/includes/HSTSPreloadLookup.php @@ -25,20 +25,6 @@ class HSTSPreloadLookup { */ private $domains; - /** - * @todo turn into proper MWServices thing - * @codeCoverageIgnore - * @return HSTSPreloadLookup - */ - public static function getInstance() { - static $instance; - if ( !$instance ) { - $instance = new self( require __DIR__ . '/../domains.php' ); - } - - return $instance; - } - /** * @param array $domains */ diff --git a/includes/Hooks.php b/includes/Hooks.php index d270dd5..9af6617 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -18,16 +18,35 @@ namespace MediaWiki\SecureLinkFixer; -class Hooks { +use MediaWiki\Hook\LinkerMakeExternalLinkHook; + +class Hooks implements LinkerMakeExternalLinkHook { + + /** @var HSTSPreloadLookup */ + private $lookup; + + /** + * @param HSTSPreloadLookup $lookup + */ + public function __construct( HSTSPreloadLookup $lookup ) { + $this->lookup = $lookup; + } /** * Hook: LinkerMakeExternalLink * * Changes the scheme of the URL to HTTPS if necessary * - * @param string &$url + * @param string &$url Link URL + * @param string &$text Link text + * @param string &$link New link HTML (if returning false) + * @param string[] &$attribs Attributes to be applied + * @param string $linkType External link type + * @return bool|void True or no return value to continue or false to abort */ - public static function onLinkerMakeExternalLink( &$url ) { + public function onLinkerMakeExternalLink( + &$url, &$text, &$link, &$attribs, $linkType + ) { if ( strpos( $url, 'https://' ) === 0 ) { // Already HTTPS return; @@ -43,7 +62,7 @@ class Hooks { return; } - if ( HSTSPreloadLookup::getInstance()->isPreloaded( $parsed['host'] ) ) { + if ( $this->lookup->isPreloaded( $parsed['host'] ) ) { $parsed['scheme'] = 'https'; $parsed['delimiter'] = '://'; $url = wfAssembleUrl( $parsed ); diff --git a/includes/ServiceWiring.php b/includes/ServiceWiring.php new file mode 100644 index 0000000..8f08486 --- /dev/null +++ b/includes/ServiceWiring.php @@ -0,0 +1,11 @@ + + +namespace MediaWiki\SecureLinkFixer; + +return [ + 'HSTSPreloadLookup' => static function () { + return new HSTSPreloadLookup( require __DIR__ . '/../domains.php' ); + } +]; diff --git a/maintenance/benchLookup.php b/maintenance/benchLookup.php index f460964..4d0ae1a 100644 --- a/maintenance/benchLookup.php +++ b/maintenance/benchLookup.php @@ -19,6 +19,7 @@ namespace MediaWiki\SecureLinkFixer; use Benchmarker; +use MediaWiki\MediaWikiServices; use const RUN_MAINTENANCE_IF_MAIN; $IP = getenv( 'MW_INSTALL_PATH' ); @@ -40,7 +41,7 @@ class BenchLookup extends Benchmarker { } public function execute() { - $lookup = HSTSPreloadLookup::getInstance(); + $lookup = MediaWikiServices::getInstance()->getService( 'HSTSPreloadLookup' ); $domains = [ // Need to traverse up one domain to find it 'foobar.dev', diff --git a/tests/phpunit/HooksTest.php b/tests/phpunit/HooksTest.php index 86152da..9e11c5e 100644 --- a/tests/phpunit/HooksTest.php +++ b/tests/phpunit/HooksTest.php @@ -30,7 +30,10 @@ class HooksTest extends MediaWikiIntegrationTestCase { * @dataProvider provideOnLinkerMakeExternalLink */ public function testOnLinkerMakeExternalLink( $input, $expected ) { - Hooks::onLinkerMakeExternalLink( $input ); + $hooks = new Hooks( $this->getServiceContainer()->getService( 'HSTSPreloadLookup' ) ); + $dummy = ''; + $dummy2 = []; + $hooks->onLinkerMakeExternalLink( $input, $dummy, $dummy, $dummy2, $dummy ); $this->assertSame( $expected, $input ); }