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

Change subject: [bugfix] Normalize IPv6 addresses in user titles like MediaWiki
......................................................................

[bugfix] Normalize IPv6 addresses in user titles like MediaWiki

MediaWiki writes IPv6 addresses in User and User talk titles in full,
in uppercase and without leading zeros, for example
"2409:40F3:2B:72D:8000:0:0:0". Link.parse only uppercased them, so
"User talk:2409:40f3:2b:72d:8000::" got another title than the one
the API returned, and loading the page raised InconsistentTitleError.

Expand the address and remove leading zeros as well. MediaWiki does
not treat addresses with an IPv4 part or a zone index as IP addresses,
so these stay as they are.

Bug: T329819
Change-Id: Iba71311e95b38f8d8df42093afb49af442de66c8
---
M pywikibot/page/_links.py
M tests/link_tests.py
2 files changed, 35 insertions(+), 0 deletions(-)

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




diff --git a/pywikibot/page/_links.py b/pywikibot/page/_links.py
index c480cfa..4650dc8 100644
--- a/pywikibot/page/_links.py
+++ b/pywikibot/page/_links.py
@@ -12,6 +12,7 @@
 """
 from __future__ import annotations

+import ipaddress
 import re
 import unicodedata
 from html.entities import name2codepoint
@@ -369,6 +370,9 @@

         .. version-changed:: 11.9
            Validate :attr:`title` length in UTF-8 bytes.
+        .. version-changed:: 11.10
+           IPv6 addresses in user namespaces are written in full and
+           without leading zeros, as MediaWiki does.
         """
         self._site = self._source
         self._namespace = self._defaultns
@@ -480,6 +484,12 @@
         # MediaWiki uses uppercase IP addresses
         if self._namespace in (2, 3) and is_ip_address(t):
             t = t.upper()
+            # and writes IPv6 addresses in full without leading zeros;
+            # addresses with an IPv4 part or a zone index are left as
+            # they are (T329819)
+            if re.fullmatch('[0-9A-F:]+', t):
+                words = ipaddress.IPv6Address(t).exploded.split(':')
+                t = ':'.join(f'{int(word, 16):X}' for word in words)
         elif self._site.namespaces[self._namespace].case == 'first-letter':
             t = first_upper(t)

diff --git a/tests/link_tests.py b/tests/link_tests.py
index 6fb5173..d4cefb6 100755
--- a/tests/link_tests.py
+++ b/tests/link_tests.py
@@ -125,6 +125,31 @@
         self.assertEqual(section_link.title, 'A')
         self.assertEqual(section_link.section, 'B')

+    def test_ip_address(self) -> None:
+        """Test that IP addresses in user namespaces are normalized."""
+        ip_tests = [
+            (2, '1.2.3.4', '1.2.3.4'),
+            (2, '2001:db8::1', '2001:DB8:0:0:0:0:0:1'),
+            (2, '2001:0db8::0001', '2001:DB8:0:0:0:0:0:1'),
+            (2, 'fe80::', 'FE80:0:0:0:0:0:0:0'),
+            (2, '1:2:3:4:5:6:7:8', '1:2:3:4:5:6:7:8'),
+            (2, '1::1.2.3.4', '1::1.2.3.4'),
+            (3, '2409:40f3:2b:72d:8000::', '2409:40F3:2B:72D:8000:0:0:0'),
+            (1, '2001:db8::1', '2001:db8::1'),
+        ]
+        site = self.site
+        for ns, ip, title in ip_tests:
+            with self.subTest(ns=ns, ip=ip):
+                link = Link(f'{site.namespace(ns)}:{ip}', site)
+                self.assertEqual(link.namespace, ns)
+                self.assertEqual(link.title, title)
+
+        # the title has to match the title returned by the API (T329819)
+        user = pywikibot.User(site, '2409:40f3:2b:72d:8000::')
+        self.assertTrue(site.sametitle(
+            user.getUserTalkPage().title(),
+            f'{site.namespace(3)}:2409:40F3:2B:72D:8000:0:0:0'))
+
     def test_invalid(self) -> None:
         """Test that invalid titles raise InvalidTitleError."""
         # Bad characters forbidden regardless of wgLegalTitleChars

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1358390?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: Iba71311e95b38f8d8df42093afb49af442de66c8
Gerrit-Change-Number: 1358390
Gerrit-PatchSet: 2
Gerrit-Owner: AkashKumar7902 <[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