* @license GPL 2+
* @author Daniel Kinzler
*/
+use MediaWiki\Linker\LinkTarget;
/**
* A codec for %MediaWiki page titles.
* @param string[]|string $localInterwikis
*/
public function __construct( Language $language, GenderCache $genderCache,
- $localInterwikis = array()
+ $localInterwikis = []
) {
$this->language = $language;
$this->genderCache = $genderCache;
* @param string $text The page title. Should be valid. Only minimal normalization is applied.
* Underscores will be replaced.
* @param string $fragment The fragment name (may be empty).
+ * @param string $interwiki The interwiki name (may be empty).
*
* @throws InvalidArgumentException If the namespace is invalid
* @return string
*/
- public function formatTitle( $namespace, $text, $fragment = '' ) {
+ public function formatTitle( $namespace, $text, $fragment = '', $interwiki = '' ) {
if ( $namespace !== false ) {
$namespace = $this->getNamespaceName( $namespace, $text );
$text = $text . '#' . $fragment;
}
+ if ( $interwiki !== '' ) {
+ $text = $interwiki . ':' . $text;
+ }
+
$text = str_replace( '_', ' ', $text );
return $text;
// be refactored to avoid this.
$parts = $this->splitTitleString( $text, $defaultNamespace );
- // Interwiki links are not supported by TitleValue
- if ( $parts['interwiki'] !== '' ) {
- throw new MalformedTitleException( 'title-invalid-interwiki', $text );
- }
-
// Relative fragment links are not supported by TitleValue
if ( $parts['dbkey'] === '' ) {
throw new MalformedTitleException( 'title-invalid-empty', $text );
}
- return new TitleValue( $parts['namespace'], $parts['dbkey'], $parts['fragment'] );
+ return new TitleValue(
+ $parts['namespace'],
+ $parts['dbkey'],
+ $parts['fragment'],
+ $parts['interwiki']
+ );
}
/**
* @see TitleFormatter::getText()
*
- * @param TitleValue $title
+ * @param LinkTarget $title
*
* @return string $title->getText()
*/
- public function getText( TitleValue $title ) {
+ public function getText( LinkTarget $title ) {
return $this->formatTitle( false, $title->getText(), '' );
}
/**
* @see TitleFormatter::getText()
*
- * @param TitleValue $title
+ * @param LinkTarget $title
*
* @return string
*/
- public function getPrefixedText( TitleValue $title ) {
- return $this->formatTitle( $title->getNamespace(), $title->getText(), '' );
+ public function getPrefixedText( LinkTarget $title ) {
+ return $this->formatTitle(
+ $title->getNamespace(),
+ $title->getText(),
+ '',
+ $title->getInterwiki()
+ );
}
/**
* @see TitleFormatter::getText()
*
- * @param TitleValue $title
+ * @param LinkTarget $title
*
* @return string
*/
- public function getFullText( TitleValue $title ) {
- return $this->formatTitle( $title->getNamespace(), $title->getText(), $title->getFragment() );
+ public function getFullText( LinkTarget $title ) {
+ return $this->formatTitle(
+ $title->getNamespace(),
+ $title->getText(),
+ $title->getFragment(),
+ $title->getInterwiki()
+ );
}
/**
$dbkey = str_replace( ' ', '_', $text );
# Initialisation
- $parts = array(
+ $parts = [
'interwiki' => '',
'local_interwiki' => false,
'fragment' => '',
'namespace' => $defaultNamespace,
'dbkey' => $dbkey,
'user_case_dbkey' => $dbkey,
- );
+ ];
# Strip Unicode bidi override characters.
# Sometimes they slip into cut-n-pasted page titles, where the
# Namespace or interwiki prefix
$prefixRegexp = "/^(.+?)_*:_*(.*)$/S";
do {
- $m = array();
+ $m = [];
if ( preg_match( $prefixRegexp, $dbkey, $m ) ) {
$p = $m[1];
$ns = $this->language->getNsIndex( $p );
# Empty self-links should point to the Main Page, to ensure
# compatibility with cross-wiki transclusions and the like.
$mainPage = Title::newMainPage();
- return array(
+ return [
'interwiki' => $mainPage->getInterwiki(),
'local_interwiki' => true,
'fragment' => $mainPage->getFragment(),
'namespace' => $mainPage->getNamespace(),
'dbkey' => $mainPage->getDBkey(),
'user_case_dbkey' => $mainPage->getUserCaseDBKey()
- );
+ ];
}
$parts['interwiki'] = '';
# local interwikis should behave like initial-colon links
# Reject illegal characters.
$rxTc = self::getTitleInvalidRegex();
- $matches = array();
+ $matches = [];
if ( preg_match( $rxTc, $dbkey, $matches ) ) {
- throw new MalformedTitleException( 'title-invalid-characters', $text, array( $matches[0] ) );
+ throw new MalformedTitleException( 'title-invalid-characters', $text, [ $matches[0] ] );
}
# Pages with "/./" or "/../" appearing in the URLs will often be un-
$maxLength = ( $parts['namespace'] != NS_SPECIAL ) ? 255 : 512;
if ( strlen( $dbkey ) > $maxLength ) {
throw new MalformedTitleException( 'title-invalid-too-long', $text,
- array( Message::numParam( $maxLength ) ) );
+ [ Message::numParam( $maxLength ) ] );
}
# Normally, all wiki links are forced to have an initial capital letter so [[foo]]