Liangent has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/62600


Change subject: Introduce a new function: LanguageConverter::findVariantTitle().
......................................................................

Introduce a new function: LanguageConverter::findVariantTitle().

The existing LanguageConverter::findVariantLink() may mess up namespace
names (for example, when a prefix of $link happens to be a some valid
namespace name). The new function uses Title::makeTitleSafe() to avoid
this (internally this fix still happened in findVariantLink()...).

Generally $link is not really useful, because it can be extracted from
$nt. In the new function, it's dropped. Users have to create a Title
object from user input, feed it into findVariantTitle(), then use the
title as usual. Anyway this new interface even looks cleaner.

Change-Id: I28e4db5c91e6fa722e6829ef2b4f0de33a87df14
---
M languages/Language.php
M languages/LanguageConverter.php
2 files changed, 54 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/00/62600/1

diff --git a/languages/Language.php b/languages/Language.php
index 2fd4bb7..54390e8 100644
--- a/languages/Language.php
+++ b/languages/Language.php
@@ -60,6 +60,7 @@
        function getURLVariant() { return ''; }
        function getConvRuleTitle() { return false; }
        function findVariantLink( &$l, &$n, $ignoreOtherCond = false ) { }
+       function findVariantTitle( &$nt, $ignoreOtherCond = false ) { }
        function getExtraHashOptions() { return ''; }
        function getParsedTitle() { return ''; }
        function markNoConversion( $text, $noParse = false ) { return $text; }
@@ -3851,6 +3852,15 @@
        }
 
        /**
+        * See LanguageConverter::findVariantTitle().
+        *
+        * @since 1.21
+        */
+       public function findVariantTitle( &$nt, $ignoreOtherCond = false ) {
+               $this->mConverter->findVariantTitle( $nt, $ignoreOtherCond );
+       }
+
+       /**
         * If a language supports multiple variants, converts text
         * into an array of all possible variants of the text:
         *  'variant' => text in that variant
diff --git a/languages/LanguageConverter.php b/languages/LanguageConverter.php
index 4969a7c..d73101a 100644
--- a/languages/LanguageConverter.php
+++ b/languages/LanguageConverter.php
@@ -746,11 +746,16 @@
         * non-existing link in one variant actually exists in another variant.
         * This function tries to find it. See e.g. LanguageZh.php
         *
-        * @param $link String: the name of the link
-        * @param $nt Mixed: the title object of the link
-        * @param $ignoreOtherCond Boolean: to disable other conditions when
-        *              we need to transclude a template or update a category's 
link
-        * @return Null, the input parameters may be modified upon return
+        * $link may have its namespace prefix stripped when it's converted,
+        * or get the whole thing messed up when some page is double prefixed.
+        * To avoid this (and other troubles), use self::findVariantTitle().
+        *
+        * @param $link String: Link text written by user.
+        *                Use '' to extract it from $nt. This is a semi-private 
usage.
+        * @param $nt Mixed: Title object.
+        * @param $ignoreOtherCond Boolean: Disable user or request specific 
checks.
+        *                Mainly used in parser (eg. transclusion and link 
updates).
+        * @return Null: Parameters may be modified upon return
         */
        public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false 
) {
                # If the article has already existed, there is no need to
@@ -784,7 +789,17 @@
                        $ns = $nt->getNamespace();
                }
 
-               $variants = $this->autoConvertToAllVariants( $link );
+               if ( $link === '' ) {
+                       if ( is_object( $nt ) ) {
+                               $linkText = $nt->getText();
+                       } else {
+                               return;
+                       }
+               } else {
+                       $linkText = $link;
+               }
+
+               $variants = $this->autoConvertToAllVariants( $linkText );
                if ( !$variants ) { // give up
                        return;
                }
@@ -793,7 +808,13 @@
 
                foreach ( $variants as $v ) {
                        if ( $v != $link ) {
-                               $varnt = Title::newFromText( $v, $ns );
+                               if ( $link === '' ) {
+                                       $varnt = Title::makeTitle( $ns, $v );
+                               } else {
+                                       // Generally Title::newFromText() may 
mess up namespace names
+                                       // in some cases. It's used here only 
for backward compatibility.
+                                       $varnt = Title::newFromText( $v, $ns );
+                               }
                                if ( !is_null( $varnt ) ) {
                                        $linkBatch->addObj( $varnt );
                                        $titles[] = $varnt;
@@ -814,6 +835,22 @@
        }
 
        /**
+        * Similar to self::findVariantLink(), but $link is not needed and 
extracted from $nt.
+        *
+        * This function was created to avoid namespace issues. See comments in 
that function.
+        *
+        * @since 1.21
+        * @param $nt Mixed: Title object.
+        * @param $ignoreOtherCond Boolean: Disable user or request specific 
checks.
+        *              Mainly used in parser (eg. transclusion and link 
updates).
+        * @return Null: Parameters may be modified upon return
+        */
+       public function findVariantTitle( &$nt, $ignoreOtherConds = false ) {
+               $_ = '';
+               $this->findVariantLink( $_, $nt, $ignoreOtherConds );
+       }
+
+       /**
         * Returns language specific hash options.
         *
         * @return string

-- 
To view, visit https://gerrit.wikimedia.org/r/62600
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I28e4db5c91e6fa722e6829ef2b4f0de33a87df14
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Liangent <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to