mediawiki-extensions-Secure.../includes/Hooks.php
thiemowmde e5df1df69e Lazy load large domains.php file only when it's really needed
I have seen this popping up once in a recent flame graph. I think most
services should be designed in a way that they can be instantiated
very quickly and only start doing expensive stuff when we are sure we
really need it. This was not the case here. The file domains.php is
about 150,000 lines long and can take several 100ms to load in the
worst case.

Bug: T200758
Change-Id: I7c0c787230e8cff40e8e73fede0ac1dce63ea1ca
2023-06-14 22:19:40 -04:00

68 lines
1.9 KiB
PHP

<?php
/**
* Copyright (C) 2018 Kunal Mehta <legoktm@debian.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace MediaWiki\SecureLinkFixer;
use MediaWiki\Hook\LinkerMakeExternalLinkHook;
class Hooks implements LinkerMakeExternalLinkHook {
private 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 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 function onLinkerMakeExternalLink(
&$url, &$text, &$link, &$attribs, $linkType
) {
if ( str_starts_with( $url, 'https://' ) ) {
// Already HTTPS
return;
}
$parsed = wfParseUrl( $url );
if ( !$parsed ) {
return;
}
if ( $parsed['scheme'] !== 'http' && $parsed['scheme'] !== '' ) {
// We only want http:// and proto-rel
return;
}
if ( $this->lookup->isPreloaded( $parsed['host'] ) ) {
$parsed['scheme'] = 'https';
$parsed['delimiter'] = '://';
$url = wfAssembleUrl( $parsed );
}
}
}