jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1332484?usp=email )

Change subject: textlib: Preserve labels with different prefixes
......................................................................

textlib: Preserve labels with different prefixes

replace_links compared only parsed titles and namespace IDs when deciding
whether a label could become an unpiped link. Namespace aliases and local
interwiki prefixes were therefore sliced into malformed links.

Compare the preserved prefix with the replacement prefix before shortening
the link, and remove the expected-failure marker.

Bug: T396719
Change-Id: I7f8cefe4eb479e0d0c8e9135e13723d1071cb415
---
M pywikibot/textlib.py
M tests/textlib_tests.py
2 files changed, 9 insertions(+), 4 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified




diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index ddd3e54..0c0cfe6 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -1039,11 +1039,14 @@
             # compare title, but only with parts if linktrail works
             if not linktrail.sub('',
                                  parsed_link_title[len(new_link_title):]):
-                # TODO: This must also compare everything that was used as a
-                #       prefix (in case insensitive)
+                label_prefix = new_label[:-len(parsed_link_title)]
+                link_prefix = new_link.canonical_title()[:-len(new_link.title)]
+                if new_link.site != site:
+                    link_prefix = f':{new_link.site.code}:{link_prefix}'
                 must_piped = (
                     not parsed_link_title.startswith(new_link_title)
-                    or parsed_new_label.namespace != new_link.namespace)
+                    or parsed_new_label.namespace != new_link.namespace
+                    or label_prefix.lower() != link_prefix.lower())

         if must_piped:
             new_text = f'[[{new_title}|{new_label}]]'
diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py
index 9530379..ce008e1 100755
--- a/tests/textlib_tests.py
+++ b/tests/textlib_tests.py
@@ -922,14 +922,16 @@
                                   self.get_site('wt')),
             '[[Bar]] and [[Bar|bar]]')

-    @unittest.expectedFailure  # T396719
     def test_label_diff_namespace(self) -> None:
         """Test that it uses the old label when the new doesn't match."""
         # These tests require to get the actual part which is before the title
         # (interwiki and namespace prefixes) which could be then compared
         # case insensitive.
         tests = [
+            ('[[file:Foobar]]', '[[file:Foo]]bar'),
             ('[[Image:Foobar]]', '[[File:Foo|Image:Foobar]]'),
+            ('[[Image:Foobar#Part]]',
+             '[[File:Foo#Part|Image:Foobar]]'),
             ('[[en:File:Foobar]]', '[[File:Foo|en:File:Foobar]]'),
         ]
         for link, result in tests:

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1332484?usp=email
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings?usp=email

Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I7f8cefe4eb479e0d0c8e9135e13723d1071cb415
Gerrit-Change-Number: 1332484
Gerrit-PatchSet: 2
Gerrit-Owner: Mahveotm <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to