* @license GPL 2+
* @author Daniel Kinzler
*/
+use MediaWiki\Interwiki\InterwikiLookup;
+use MediaWiki\MediaWikiServices;
use MediaWiki\Linker\LinkTarget;
/**
*/
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();
}
/**
*/
public function formatTitle( $namespace, $text, $fragment = '', $interwiki = '' ) {
if ( $namespace !== false ) {
- $namespace = $this->getNamespaceName( $namespace, $text );
+ // Try to get a namespace name, but fallback
+ // to empty string if it doesn't exist
+ try {
+ $nsName = $this->getNamespaceName( $namespace, $text );
+ } catch ( InvalidArgumentException $e ) {
+ $nsName = '';
+ }
- if ( $namespace !== '' ) {
- $text = $namespace . ':' . $text;
+ if ( $namespace !== 0 ) {
+ $text = $nsName . ':' . $text;
}
}
if ( $this->language->getNsIndex( $x[1] ) ) {
# Disallow Talk:File:x type titles...
throw new MalformedTitleException( 'title-invalid-talk-namespace', $text );
- } elseif ( Interwiki::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 ( Interwiki::isValidInterwiki( $p ) ) {
+ } elseif ( $this->interwikiLookup->isValidInterwiki( $p ) ) {
# Interwiki link
$dbkey = $m[2];
$parts['interwiki'] = $this->language->lc( $p );