X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Ftitle%2FMediaWikiTitleCodec.php;h=3ebb4430ece8c1d6eaeeffa7cf7ffdcbe9c5f670;hb=7afced64454ad30d688540f7626448ac2faefebb;hp=5021a1c9fe9b008c9257207c89a30b50218de1b2;hpb=1d1bb122942d2ca6e557dc13e2d198276ce65ba6;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/title/MediaWikiTitleCodec.php b/includes/title/MediaWikiTitleCodec.php index 5021a1c9fe..3ebb4430ec 100644 --- a/includes/title/MediaWikiTitleCodec.php +++ b/includes/title/MediaWikiTitleCodec.php @@ -62,7 +62,8 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser { protected $nsInfo; /** - * @param Language $language The language object to use for localizing namespace names. + * @param Language $language The language object to use for localizing namespace names, + * capitalization, etc. * @param GenderCache $genderCache The gender cache for generating gendered namespace names * @param string[]|string $localInterwikis * @param InterwikiLookup|null $interwikiLookup @@ -185,6 +186,40 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser { ); } + /** + * Given a namespace and title, return a TitleValue if valid, or null if invalid. + * + * @param int $namespace + * @param string $text + * @param string $fragment + * @param string $interwiki + * + * @return TitleValue|null + */ + public function makeTitleValueSafe( $namespace, $text, $fragment = '', $interwiki = '' ) { + if ( !$this->nsInfo->exists( $namespace ) ) { + return null; + } + + $canonicalNs = $this->nsInfo->getCanonicalName( $namespace ); + $fullText = $canonicalNs == '' ? $text : "$canonicalNs:$text"; + if ( strval( $interwiki ) != '' ) { + $fullText = "$interwiki:$fullText"; + } + if ( strval( $fragment ) != '' ) { + $fullText .= '#' . $fragment; + } + + try { + $parts = $this->splitTitleString( $fullText ); + } catch ( MalformedTitleException $e ) { + return null; + } + + return new TitleValue( + $parts['namespace'], $parts['dbkey'], $parts['fragment'], $parts['interwiki'] ); + } + /** * @see TitleFormatter::getText() * @@ -202,6 +237,7 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser { * @param LinkTarget $title * * @return string + * @suppress PhanUndeclaredProperty */ public function getPrefixedText( LinkTarget $title ) { if ( !isset( $title->prefixedText ) ) { @@ -433,8 +469,8 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser { # and [[Foo]] point to the same place. Don't force it for interwikis, since the # other site might be case-sensitive. $parts['user_case_dbkey'] = $dbkey; - if ( $parts['interwiki'] === '' ) { - $dbkey = Title::capitalize( $dbkey, $parts['namespace'] ); + if ( $parts['interwiki'] === '' && $this->nsInfo->isCapitalized( $parts['namespace'] ) ) { + $dbkey = $this->language->ucfirst( $dbkey ); } # Can't make a link to a namespace alone... "empty" local links can only be