Add custom InterwikiLookup to MediaWikiTitleCodec
This patch allows for a custom InterwikiLookup service when constructing MediaWikiTitleCodec instances. If not specified, it continues to get the service from MediaWikiServices::getInstance()->getInterwikiLookup(). This patch will allow a fully customized instance creation, without any global state. Change-Id: Ica87aff5df4534aae0a32e307b27d88b3df023b4
This commit is contained in:
parent
b27aceb218
commit
3f7615be58
1 changed files with 12 additions and 4 deletions
|
|
@ -21,6 +21,7 @@
|
|||
* @license GPL 2+
|
||||
* @author Daniel Kinzler
|
||||
*/
|
||||
use MediaWiki\Interwiki\InterwikiLookup;
|
||||
use MediaWiki\MediaWikiServices;
|
||||
use MediaWiki\Linker\LinkTarget;
|
||||
|
||||
|
|
@ -51,17 +52,25 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
|
|||
*/
|
||||
protected $localInterwikis;
|
||||
|
||||
/**
|
||||
* @var InterwikiLookup
|
||||
*/
|
||||
protected $interwikiLookup;
|
||||
|
||||
/**
|
||||
* @param Language $language The language object to use for localizing namespace names.
|
||||
* @param GenderCache $genderCache The gender cache for generating gendered namespace names
|
||||
* @param string[]|string $localInterwikis
|
||||
* @param InterwikiLookup|null $interwikiLookup
|
||||
*/
|
||||
public function __construct( Language $language, GenderCache $genderCache,
|
||||
$localInterwikis = []
|
||||
$localInterwikis = [], $interwikiLookup = null
|
||||
) {
|
||||
$this->language = $language;
|
||||
$this->genderCache = $genderCache;
|
||||
$this->localInterwikis = (array)$localInterwikis;
|
||||
$this->interwikiLookup = $interwikiLookup ?:
|
||||
MediaWikiServices::getInstance()->getInterwikiLookup();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -310,7 +319,6 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
|
|||
if ( preg_match( $prefixRegexp, $dbkey, $m ) ) {
|
||||
$p = $m[1];
|
||||
$ns = $this->language->getNsIndex( $p );
|
||||
$interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup();
|
||||
if ( $ns !== false ) {
|
||||
# Ordinary namespace
|
||||
$dbkey = $m[2];
|
||||
|
|
@ -320,13 +328,13 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
|
|||
if ( $this->language->getNsIndex( $x[1] ) ) {
|
||||
# Disallow Talk:File:x type titles...
|
||||
throw new MalformedTitleException( 'title-invalid-talk-namespace', $text );
|
||||
} elseif ( $interwikiLookup->isValidInterwiki( $x[1] ) ) {
|
||||
} elseif ( $this->interwikiLookup->isValidInterwiki( $x[1] ) ) {
|
||||
// TODO: get rid of global state!
|
||||
# Disallow Talk:Interwiki:x type titles...
|
||||
throw new MalformedTitleException( 'title-invalid-talk-namespace', $text );
|
||||
}
|
||||
}
|
||||
} elseif ( $interwikiLookup->isValidInterwiki( $p ) ) {
|
||||
} elseif ( $this->interwikiLookup->isValidInterwiki( $p ) ) {
|
||||
# Interwiki link
|
||||
$dbkey = $m[2];
|
||||
$parts['interwiki'] = $this->language->lc( $p );
|
||||
|
|
|
|||
Loading…
Reference in a new issue