Skip to content

Commit 20e1ef9

Browse files
jenkins-botGerrit Code Review
jenkins-bot
authored and
Gerrit Code Review
committed
Merge "[IMPR] Add sort parameter to SearchPageGenerator and site.search()"
2 parents 7bfdda9 + 8723542 commit 20e1ef9

File tree

2 files changed

+55
-18
lines changed

2 files changed

+55
-18
lines changed

pywikibot/pagegenerators/_generators.py

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Page filter generators provided by the pagegenerators module."""
22
#
3-
# (C) Pywikibot team, 2008-2024
3+
# (C) Pywikibot team, 2008-2025
44
#
55
# Distributed under the terms of the MIT license.
66
#
@@ -815,16 +815,32 @@ def SearchPageGenerator(
815815
query: str,
816816
total: int | None = None,
817817
namespaces: NamespaceArgType = None,
818-
site: BaseSite | None = None
818+
site: BaseSite | None = None,
819+
**kwargs
819820
) -> Iterable[pywikibot.page.Page]:
820-
"""Yield pages from the MediaWiki internal search engine.
821+
r"""Yield pages from the MediaWiki internal search engine.
822+
823+
.. versionchanged:: 10.0
824+
Keyword arguments *content*, *sort* and *where* was added.
825+
826+
.. seealso:: :meth:`site.search()
827+
<pywikibot.site._generators.GeneratorsMixin.search>`
821828
829+
:param query: the text to search for
822830
:param total: Maximum number of pages to retrieve in total
831+
:param namespaces: search only in these namespaces (defaults to all)
823832
:param site: Site for generator results.
833+
:keyword str \| None where: Where to search; value must be one of the
834+
given literals or None (many wikis do not support all search
835+
types)
836+
:keyword bool content: if True, load the current content of each
837+
iterated page (default False)
838+
:keyword sort: Set the sort order of returned results. If None is
839+
given, 'none' is used. Default is sort by relevance.
824840
"""
825841
if site is None:
826842
site = pywikibot.Site()
827-
return site.search(query, total=total, namespaces=namespaces)
843+
return site.search(query, total=total, namespaces=namespaces, **kwargs)
828844

829845

830846
def LiveRCPageGenerator(site: BaseSite | None = None,

pywikibot/site/_generators.py

+35-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
"""Objects representing API generators to MediaWiki site."""
22
#
3-
# (C) Pywikibot team, 2008-2024
3+
# (C) Pywikibot team, 2008-2025
44
#
55
# Distributed under the terms of the MIT license.
66
#
77
from __future__ import annotations
88

99
import heapq
1010
import itertools
11+
import typing
1112
from contextlib import suppress
1213
from itertools import zip_longest
13-
from typing import TYPE_CHECKING, Any
14+
from typing import Any
1415

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

3132

32-
if TYPE_CHECKING:
33+
if typing.TYPE_CHECKING:
3334
from data.api import ParamInfo, Request
3435

3536
from pywikibot.site._apisite import _RequestWrapperT
@@ -42,7 +43,7 @@ class GeneratorsMixin:
4243

4344
"""API generators mixin to MediaWiki site."""
4445

45-
if TYPE_CHECKING:
46+
if typing.TYPE_CHECKING:
4647
_generator: Callable[..., _RequestWrapperT]
4748
_paraminfo: ParamInfo
4849
_request: Callable[..., Request]
@@ -1546,30 +1547,42 @@ def search(
15461547
searchstring: str,
15471548
*,
15481549
namespaces: NamespaceArgType = None,
1549-
where: str | None = None,
1550+
where: typing.Literal['text', 'title', 'nearmatch'] | None = None,
15501551
total: int | None = None,
15511552
content: bool = False,
1553+
sort: typing.Literal[
1554+
'create_timestamp_asc', 'create_timestamp_desc',
1555+
'incoming_links_asc', 'incoming_links_desc', 'just_match',
1556+
'last_edit_asc', 'last_edit_desc', 'none', 'random', 'relevance',
1557+
'user_random'
1558+
] | None = 'relevance',
15521559
) -> Iterable[pywikibot.Page]:
15531560
"""Iterate Pages that contain the searchstring.
15541561
1555-
Note that this may include non-existing Pages if the wiki's database
1556-
table contains outdated entries.
1562+
Note that this may include non-existing Pages if the wiki's
1563+
database table contains outdated entries.
15571564
15581565
.. versionchanged:: 7.0
15591566
Default of `where` parameter has been changed from 'text' to
15601567
None. The behaviour depends on the installed search engine
15611568
which is 'text' on CirrusSearch'.
15621569
raises APIError instead of Error if searchstring is not set
15631570
or what parameter is wrong.
1571+
.. versionchanged:: 10.0
1572+
The *sort* parameter was added.
15641573
15651574
.. seealso:: :api:`Search`
15661575
15671576
:param searchstring: the text to search for
1568-
:param where: Where to search; value must be "text", "title",
1569-
"nearmatch" or None (many wikis do not support all search types)
1570-
:param namespaces: search only in these namespaces (defaults to all)
1571-
:param content: if True, load the current content of each iterated page
1572-
(default False)
1577+
:param namespaces: search only in these namespaces (defaults to
1578+
all)
1579+
:param where: Where to search; value must be one of the given
1580+
literals or None (many wikis do not support all search types)
1581+
:param total: limit result to this number of pages
1582+
:param content: if True, load the current content of each
1583+
iterated page (default False)
1584+
:param sort: Set the sort order of returned results. If None is
1585+
given, 'none' is used. Default is sort by relevance.
15731586
:raises KeyError: a namespace identifier was not resolved
15741587
:raises TypeError: a namespace identifier has an inappropriate
15751588
type such as NoneType or bool
@@ -1580,10 +1593,18 @@ def search(
15801593
"""
15811594
if not namespaces and namespaces != 0:
15821595
namespaces = [ns_id for ns_id in self.namespaces if ns_id >= 0]
1596+
parameters = {
1597+
'gsrsearch': searchstring,
1598+
'gsrwhat': where,
1599+
}
1600+
if sort is None:
1601+
sort = 'none'
1602+
if sort != 'relevance':
1603+
parameters['gsrsort'] = sort
15831604
srgen = self._generator(api.PageGenerator, type_arg='search',
1584-
gsrsearch=searchstring, gsrwhat=where,
15851605
namespaces=namespaces,
1586-
total=total, g_content=content)
1606+
total=total, g_content=content,
1607+
parameters=parameters)
15871608
return srgen
15881609

15891610
def usercontribs(

0 commit comments

Comments
 (0)