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

Change subject: Implement param with_sort_key in Page.categories()
......................................................................

Implement param with_sort_key in Page.categories()

In pywikibot.page._pages.BasePage.categories():
When with_sort_key, pass it to self.site.pagecategories() instead of throwing 
NotImplementedError.

In pywikibot.site._generators.GeneratorsMixin.pagecategories():
If with_sort_key:
- Use pywikibot.data.api.PropertyGenerator instead of PageGenerator
- Create Category items (with sortkey) from the titles in the API response
- If content (load category page content) then pass them through 
self.preloadpages()

Bug: T75561
Change-Id: Ie62c921029492fe3ea86cf90c483764131aae23a
---
M pywikibot/page/_basepage.py
M pywikibot/site/_generators.py
2 files changed, 24 insertions(+), 9 deletions(-)

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




diff --git a/pywikibot/page/_basepage.py b/pywikibot/page/_basepage.py
index 05941b9..91e8c7b 100644
--- a/pywikibot/page/_basepage.py
+++ b/pywikibot/page/_basepage.py
@@ -1737,10 +1737,6 @@
         :return: a generator that yields Category objects.
         :rtype: generator
         """
-        # FIXME: bug T75561: with_sort_key is ignored by Site.pagecategories
-        if with_sort_key:
-            raise NotImplementedError('with_sort_key is not implemented')
-
         # Data might have been preloaded
         # Delete cache if content is needed and elements have no content
         if hasattr(self, '_categories'):
@@ -1750,7 +1746,8 @@
             else:
                 return itertools.islice(self._categories, total)

-        return self.site.pagecategories(self, total=total, content=content)
+        return self.site.pagecategories(self, with_sort_key=with_sort_key,
+                                        total=total, content=content)

     def extlinks(self, total: int | None = None) -> Iterable[str]:
         """Iterate all external URLs (not interwiki links) from this page.
diff --git a/pywikibot/site/_generators.py b/pywikibot/site/_generators.py
index ba5d464..4d1ecc6 100644
--- a/pywikibot/site/_generators.py
+++ b/pywikibot/site/_generators.py
@@ -451,11 +451,11 @@
                                g_content=content, redirects=follow_redirects,
                                **plargs)

-    # Sortkey doesn't work with generator
     def pagecategories(
         self,
         page: pywikibot.Page,
         *,
+        with_sort_key: bool = False,
         total: int | None = None,
         content: bool = False,
     ) -> Iterable[pywikibot.Page]:
@@ -463,6 +463,7 @@

         .. seealso:: :api:`Categories`

+        :param with_sort_key: if True, include the sort key in each Category
         :param content: if True, load the current content of each iterated page
             (default False); note that this means the contents of the
             category description page, not the pages contained in the category
@@ -473,9 +474,26 @@
         else:
             clargs['titles'] = page.title(
                 with_section=False).encode(self.encoding())
-        return self._generator(api.PageGenerator,
-                               type_arg='categories', total=total,
-                               g_content=content, **clargs)
+        if with_sort_key:
+            page_dict = next(iter(self._generator(
+                api.PropertyGenerator,
+                type_arg='categories',
+                total=total,
+                clprop='sortkey|timestamp|hidden',
+                **clargs)))
+            cats = (
+                pywikibot.Category(self,
+                                   cat_dict['title'],
+                                   sort_key=cat_dict['sortkeyprefix'] or None)
+                for cat_dict in page_dict.get('categories', [])
+            )
+            return self.preloadpages(cats) if content else cats
+        else:
+            return self._generator(api.PageGenerator,
+                                   type_arg='categories',
+                                   total=total,
+                                   g_content=content,
+                                   **clargs)

     def pageimages(
         self,

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/787784?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: Ie62c921029492fe3ea86cf90c483764131aae23a
Gerrit-Change-Number: 787784
Gerrit-PatchSet: 15
Gerrit-Owner: Tol <t...@tol.sh>
Gerrit-Reviewer: Xqt <i...@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: Mpaa <mpaa.w...@gmail.com>
_______________________________________________
Pywikibot-commits mailing list -- pywikibot-commits@lists.wikimedia.org
To unsubscribe send an email to pywikibot-commits-le...@lists.wikimedia.org

Reply via email to