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

Change subject: page: Use the page site when changing categories
......................................................................

page: Use the page site when changing categories

Pass self.site to replaceCategoryLinks so category namespaces and footer
rules come from the page's wiki. Falling back to the default site can leave
old categories in the text and duplicate retained categories in multi-site
bots.

Change-Id: I1ddaa01e29ad9b077222a2523249859dc18f1542
---
M pywikibot/page/_basepage.py
M tests/page_tests.py
2 files changed, 37 insertions(+), 1 deletion(-)

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




diff --git a/pywikibot/page/_basepage.py b/pywikibot/page/_basepage.py
index 6b70f38..88af00d 100644
--- a/pywikibot/page/_basepage.py
+++ b/pywikibot/page/_basepage.py
@@ -2620,7 +2620,8 @@
                 cats.pop(old_cat_pos)

             try:
-                newtext = textlib.replaceCategoryLinks(oldtext, cats)
+                newtext = textlib.replaceCategoryLinks(oldtext, cats,
+                                                       site=self.site)
             except ValueError:
                 # Make sure that the only way replaceCategoryLinks() can return
                 # a ValueError is in the case of interwiki links to self.
diff --git a/tests/page_tests.py b/tests/page_tests.py
index f0e3769..1a0e72f 100755
--- a/tests/page_tests.py
+++ b/tests/page_tests.py
@@ -776,6 +776,41 @@
                         self.assertIsNone(p.sortKey)


+class TestPageChangeCategory(TestCase):
+
+    """Test changing categories on a page."""
+
+    sites = {
+        'de': {'family': 'wikipedia', 'code': 'de'},
+        'fr': {'family': 'wikipedia', 'code': 'fr'},
+    }
+
+    dry = True
+
+    @mock.patch.dict('pywikibot.textlib._regex_cache', clear=True)
+    def test_different_default_site(self) -> None:
+        """Test category replacement uses the page's localized namespace."""
+        site = self.get_site('de')
+        namespaces = site._build_namespaces()
+        namespaces[14].custom_name = 'Kategorie'
+        site._namespaces = pywikibot.site.NamespacesDict(namespaces)
+        page = pywikibot.Page(site, 'Test page')
+        page._text = 'Text.\n\n[[Kategorie:Alt]]\n[[Kategorie:Bleibt]]'
+        old_cat = pywikibot.Category(site, 'Alt')
+        new_cat = pywikibot.Category(site, 'Neu')
+
+        with mock.patch.object(pywikibot, 'Site',
+                               return_value=self.get_site('fr')), \
+                mock.patch.object(page, 'has_permission', return_value=True), \
+                mock.patch.object(page, 'put') as put:
+            self.assertTrue(page.change_category(old_cat, new_cat,
+                                                 in_place=False))
+
+        put.assert_called_once_with(
+            'Text.\n\n[[Kategorie:Neu]]\n[[Kategorie:Bleibt]]',
+            None, show_diff=False)
+
+
 class TestPageCoordinates(TestCase):

     """Test Page Object using German Wikipedia."""

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1354179?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: I1ddaa01e29ad9b077222a2523249859dc18f1542
Gerrit-Change-Number: 1354179
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