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

Change subject: UA: use URL to user page in user agent
......................................................................

UA: use URL to user page in user agent

If a site does not start with "wiki" like in commons or "wiktionary"
or the site code has more than 2 characters (e.g, 'test'), the backend
does not accept (site; User:username) as contact information introduced
with T414173
(https://gerrit.wikimedia.org/r/c/operations/puppet/+/1224977)

Use the URL to the user page in that case.

Update tests accordingly.

Bug: T414201
Change-Id: Icd70607c7f9abb58d211b5e9fe6ac2dd875800ac
---
M pywikibot/comms/http.py
M tests/dry_site_tests.py
2 files changed, 33 insertions(+), 11 deletions(-)

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




diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py
index d248334..3b88f32 100644
--- a/pywikibot/comms/http.py
+++ b/pywikibot/comms/http.py
@@ -160,7 +160,10 @@
     """User-agent formatter to load version/revision only if necessary."""

     def get_value(self, key, args, kwargs):
-        """Lazy load revision key. Also replace deprecated variables."""
+        """Lazy load revision key. Also replace deprecated variables.
+
+        See :func:`user_agent` for the deprecated variables.
+        """
         replacements = {
             'script_product': 'script',
             'version': 'revision',
@@ -230,9 +233,16 @@
     """Generate the user agent string for a given site and format.

     .. versionchanged:: 11.0
-       ``code``, ``lang`` and ``family`` variables within format string
-       are no longer supported. They will be replaced  by ``site``
-       during deprecation period.
+       The ``code``, ``lang`` and ``family`` variables in the format
+       string are deprecated and replaced by ``site``. The
+       ``script_version`` and ``version`` variables are deprecated and
+       replaced by ``script`` and ``revision`` respectively.
+
+       If *site* is provided and a username is returned by the helper
+       function :func:`user_agent_username`, the URL to the user's wiki
+       page is added to ``script_comments`` when the sitename does not
+       start with "wiki" (e.g., commons or wiktionary) or the site code
+       has more than two characters.

     :param site: The site for which this user agent is intended. May be
         None.
@@ -241,6 +251,7 @@
         empty.
     :return: The formatted user agent
     """
+    # NOTE: only BaseSite methods can be used here
     values = USER_AGENT_PRODUCTS.copy()
     values['script'] = pywikibot.bot.calledModuleName()
     values['site'] = ''
@@ -253,10 +264,15 @@

     if site:
         values['site'] = site.sitename
-        script_comments.append(site.sitename)
-
-        if username:
-            script_comments.append('User:' + username)
+        if site.sitename.startswith('wiki') and len(site.code) == 2:
+            # use "sitename:code; User:username"
+            script_comments.append(site.sitename)
+            if username:
+                script_comments.append('User:' + username)
+        elif username:
+            # use url to user wikipage
+            full_url = site.base_url(f'wiki/User:{username}')
+            script_comments.append(full_url)

     values['username'] = username
     values['script_comments'] = '; '.join(script_comments)
diff --git a/tests/dry_site_tests.py b/tests/dry_site_tests.py
index 8b5e39e..c813e1f 100755
--- a/tests/dry_site_tests.py
+++ b/tests/dry_site_tests.py
@@ -108,10 +108,16 @@
         x._username = None

         # user_agent_username() may set ua_username from environment variable
-        ua_username = user_agent_username()
-        self.assertEqual(f'Foo {ua_username}'.strip(),
+        ua_user = user_agent_username()
+        self.assertEqual(f'Foo {ua_user}'.strip(),
                          user_agent(x, format_string='Foo {username}'))
-        res = f'Foo ({x}; User:{ua_username})' if ua_username else f'Foo ({x})'
+
+        if self.site.sitename.startswith('wiki') and len(self.site.code) == 2:
+            res = f'Foo ({x}; User:{ua_user})' if ua_user else f'Foo ({x})'
+        else:
+            full_url = self.site.base_url(f'wiki/User:{ua_user}')
+            res = f'Foo ({full_url})' if ua_user else 'Foo'
+
         self.assertEqual(
             res, user_agent(x, format_string='Foo ({script_comments})'))


--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1237694?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: Icd70607c7f9abb58d211b5e9fe6ac2dd875800ac
Gerrit-Change-Number: 1237694
Gerrit-PatchSet: 4
Gerrit-Owner: Xqt <[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