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

Change subject: [IMPR] Add sort parameter to SearchPageGenerator and 
site.search()
......................................................................

[IMPR] Add sort parameter to SearchPageGenerator and site.search()

Bug: T386594
Change-Id: If30e930d9afc5847c0642b13aac2868fa8d2a1d8
---
M pywikibot/pagegenerators/_generators.py
M pywikibot/site/_generators.py
2 files changed, 55 insertions(+), 18 deletions(-)

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




diff --git a/pywikibot/pagegenerators/_generators.py 
b/pywikibot/pagegenerators/_generators.py
index 42a54c6..e07544f 100644
--- a/pywikibot/pagegenerators/_generators.py
+++ b/pywikibot/pagegenerators/_generators.py
@@ -1,6 +1,6 @@
 """Page filter generators provided by the pagegenerators module."""
 #
-# (C) Pywikibot team, 2008-2024
+# (C) Pywikibot team, 2008-2025
 #
 # Distributed under the terms of the MIT license.
 #
@@ -815,16 +815,32 @@
     query: str,
     total: int | None = None,
     namespaces: NamespaceArgType = None,
-    site: BaseSite | None = None
+    site: BaseSite | None = None,
+    **kwargs
 ) -> Iterable[pywikibot.page.Page]:
-    """Yield pages from the MediaWiki internal search engine.
+    r"""Yield pages from the MediaWiki internal search engine.

+    .. versionchanged:: 10.0
+       Keyword arguments *content*, *sort* and *where* was added.
+
+    .. seealso:: :meth:`site.search()
+       <pywikibot.site._generators.GeneratorsMixin.search>`
+
+    :param query: the text to search for
     :param total: Maximum number of pages to retrieve in total
+    :param namespaces: search only in these namespaces (defaults to all)
     :param site: Site for generator results.
+    :keyword str \| None where: Where to search; value must be one of the
+        given literals or None (many wikis do not support all search
+        types)
+    :keyword bool content: if True, load the current content of each
+        iterated page (default False)
+    :keyword sort: Set the sort order of returned results. If None is
+        given, 'none' is used. Default is sort by relevance.
     """
     if site is None:
         site = pywikibot.Site()
-    return site.search(query, total=total, namespaces=namespaces)
+    return site.search(query, total=total, namespaces=namespaces, **kwargs)


 def LiveRCPageGenerator(site: BaseSite | None = None,
diff --git a/pywikibot/site/_generators.py b/pywikibot/site/_generators.py
index 7e0bae6..92a2229 100644
--- a/pywikibot/site/_generators.py
+++ b/pywikibot/site/_generators.py
@@ -1,6 +1,6 @@
 """Objects representing API generators to MediaWiki site."""
 #
-# (C) Pywikibot team, 2008-2024
+# (C) Pywikibot team, 2008-2025
 #
 # Distributed under the terms of the MIT license.
 #
@@ -8,9 +8,10 @@

 import heapq
 import itertools
+import typing
 from contextlib import suppress
 from itertools import zip_longest
-from typing import TYPE_CHECKING, Any
+from typing import Any

 import pywikibot
 from pywikibot.backports import Callable, Generator, Iterable, batched
@@ -29,7 +30,7 @@
 from pywikibot.tools.itertools import filter_unique


-if TYPE_CHECKING:
+if typing.TYPE_CHECKING:
     from data.api import ParamInfo, Request

     from pywikibot.site._apisite import _RequestWrapperT
@@ -42,7 +43,7 @@
 
     """API generators mixin to MediaWiki site."""

-    if TYPE_CHECKING:
+    if typing.TYPE_CHECKING:
         _generator: Callable[..., _RequestWrapperT]
         _paraminfo: ParamInfo
         _request: Callable[..., Request]
@@ -1546,14 +1547,20 @@
         searchstring: str,
         *,
         namespaces: NamespaceArgType = None,
-        where: str | None = None,
+        where: typing.Literal['text', 'title', 'nearmatch'] | None = None,
         total: int | None = None,
         content: bool = False,
+        sort: typing.Literal[
+            'create_timestamp_asc', 'create_timestamp_desc',
+            'incoming_links_asc', 'incoming_links_desc', 'just_match',
+            'last_edit_asc', 'last_edit_desc', 'none', 'random', 'relevance',
+            'user_random'
+        ] | None = 'relevance',
     ) -> Iterable[pywikibot.Page]:
         """Iterate Pages that contain the searchstring.

-        Note that this may include non-existing Pages if the wiki's database
-        table contains outdated entries.
+        Note that this may include non-existing Pages if the wiki's
+        database table contains outdated entries.

         .. versionchanged:: 7.0
            Default of `where` parameter has been changed from 'text' to
@@ -1561,15 +1568,21 @@
            which is 'text' on CirrusSearch'.
            raises APIError instead of Error if searchstring is not set
            or what parameter is wrong.
+        .. versionchanged:: 10.0
+           The *sort* parameter was added.

         .. seealso:: :api:`Search`

         :param searchstring: the text to search for
-        :param where: Where to search; value must be "text", "title",
-            "nearmatch" or None (many wikis do not support all search types)
-        :param namespaces: search only in these namespaces (defaults to all)
-        :param content: if True, load the current content of each iterated page
-            (default False)
+        :param namespaces: search only in these namespaces (defaults to
+            all)
+        :param where: Where to search; value must be one of the given
+            literals or None (many wikis do not support all search types)
+        :param total: limit result to this number of pages
+        :param content: if True, load the current content of each
+            iterated page (default False)
+        :param sort: Set the sort order of returned results. If None is
+            given, 'none' is used. Default is sort by relevance.
         :raises KeyError: a namespace identifier was not resolved
         :raises TypeError: a namespace identifier has an inappropriate
             type such as NoneType or bool
@@ -1580,10 +1593,18 @@
         """
         if not namespaces and namespaces != 0:
             namespaces = [ns_id for ns_id in self.namespaces if ns_id >= 0]
+        parameters = {
+            'gsrsearch': searchstring,
+            'gsrwhat': where,
+        }
+        if sort is None:
+            sort = 'none'
+        if sort != 'relevance':
+            parameters['gsrsort'] = sort
         srgen = self._generator(api.PageGenerator, type_arg='search',
-                                gsrsearch=searchstring, gsrwhat=where,
                                 namespaces=namespaces,
-                                total=total, g_content=content)
+                                total=total, g_content=content,
+                                parameters=parameters)
         return srgen

     def usercontribs(

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