Merge "deferred: make DeferredUpdates::attemptUpdate() use callback owners for $fname...
[lhc/web/wiklou.git] / includes / title / MediaWikiTitleCodec.php
index b2c8521..3bd66d4 100644 (file)
@@ -57,19 +57,35 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
        protected $interwikiLookup;
 
        /**
-        * @param Language $language The language object to use for localizing namespace names.
+        * @var NamespaceInfo
+        */
+       protected $nsInfo;
+
+       /**
+        * @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
+        * @param NamespaceInfo|null $nsInfo
         */
        public function __construct( Language $language, GenderCache $genderCache,
-               $localInterwikis = [], $interwikiLookup = null
+               $localInterwikis = [], InterwikiLookup $interwikiLookup = null,
+               NamespaceInfo $nsInfo = null
        ) {
+               if ( !$interwikiLookup ) {
+                       wfDeprecated( __METHOD__ . ' with no InterwikiLookup argument', '1.34' );
+                       $interwikiLookup = MediaWikiServices::getInstance()->getInterwikiLookup();
+               }
+               if ( !$nsInfo ) {
+                       wfDeprecated( __METHOD__ . ' with no NamespaceInfo argument', '1.34' );
+                       $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
+               }
                $this->language = $language;
                $this->genderCache = $genderCache;
                $this->localInterwikis = (array)$localInterwikis;
-               $this->interwikiLookup = $interwikiLookup ?:
-                       MediaWikiServices::getInstance()->getInterwikiLookup();
+               $this->interwikiLookup = $interwikiLookup;
+               $this->nsInfo = $nsInfo;
        }
 
        /**
@@ -83,7 +99,7 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
         */
        public function getNamespaceName( $namespace, $text ) {
                if ( $this->language->needsGenderDistinction() &&
-                       MWNamespace::hasGenderDistinction( $namespace )
+                       $this->nsInfo->hasGenderDistinction( $namespace )
                ) {
                        // NOTE: we are assuming here that the title text is a user name!
                        $gender = $this->genderCache->getGenderOf( $text, __METHOD__ );
@@ -139,8 +155,7 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
        }
 
        /**
-        * Parses the given text and constructs a TitleValue. Normalization
-        * is applied according to the rules appropriate for the form specified by $form.
+        * Parses the given text and constructs a TitleValue.
         *
         * @param string $text The text to parse
         * @param int $defaultNamespace Namespace to assume per default (usually NS_MAIN)
@@ -157,8 +172,9 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
                // be refactored to avoid this.
                $parts = $this->splitTitleString( $filteredText, $defaultNamespace );
 
-               // Relative fragment links are not supported by TitleValue
-               if ( $parts['dbkey'] === '' ) {
+               // Fragment-only is okay, but only with no namespace
+               if ( $parts['dbkey'] === '' &&
+               ( $parts['fragment'] === '' || $parts['namespace'] !== NS_MAIN ) ) {
                        throw new MalformedTitleException( 'title-invalid-empty', $text );
                }
 
@@ -170,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()
         *
@@ -268,7 +318,7 @@ class MediaWikiTitleCodec implements TitleFormatter, TitleParser {
                # Strip Unicode bidi override characters.
                # Sometimes they slip into cut-n-pasted page titles, where the
                # override chars get included in list displays.
-               $dbkey = preg_replace( '/\xE2\x80[\x8E\x8F\xAA-\xAE]/S', '', $dbkey );
+               $dbkey = preg_replace( '/[\x{200E}\x{200F}\x{202A}-\x{202E}]+/u', '', $dbkey );
 
                # Clean up whitespace
                # Note: use of the /u option on preg_replace here will cause
@@ -418,8 +468,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