1
1
"""Objects representing API generators to MediaWiki site."""
2
2
#
3
- # (C) Pywikibot team, 2008-2024
3
+ # (C) Pywikibot team, 2008-2025
4
4
#
5
5
# Distributed under the terms of the MIT license.
6
6
#
7
7
from __future__ import annotations
8
8
9
9
import heapq
10
10
import itertools
11
+ import typing
11
12
from contextlib import suppress
12
13
from itertools import zip_longest
13
- from typing import TYPE_CHECKING , Any
14
+ from typing import Any
14
15
15
16
import pywikibot
16
17
from pywikibot .backports import Callable , Generator , Iterable , batched
29
30
from pywikibot .tools .itertools import filter_unique
30
31
31
32
32
- if TYPE_CHECKING :
33
+ if typing . TYPE_CHECKING :
33
34
from data .api import ParamInfo , Request
34
35
35
36
from pywikibot .site ._apisite import _RequestWrapperT
@@ -42,7 +43,7 @@ class GeneratorsMixin:
42
43
43
44
"""API generators mixin to MediaWiki site."""
44
45
45
- if TYPE_CHECKING :
46
+ if typing . TYPE_CHECKING :
46
47
_generator : Callable [..., _RequestWrapperT ]
47
48
_paraminfo : ParamInfo
48
49
_request : Callable [..., Request ]
@@ -1546,30 +1547,42 @@ def search(
1546
1547
searchstring : str ,
1547
1548
* ,
1548
1549
namespaces : NamespaceArgType = None ,
1549
- where : str | None = None ,
1550
+ where : typing . Literal [ 'text' , 'title' , 'nearmatch' ] | None = None ,
1550
1551
total : int | None = None ,
1551
1552
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' ,
1552
1559
) -> Iterable [pywikibot .Page ]:
1553
1560
"""Iterate Pages that contain the searchstring.
1554
1561
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.
1557
1564
1558
1565
.. versionchanged:: 7.0
1559
1566
Default of `where` parameter has been changed from 'text' to
1560
1567
None. The behaviour depends on the installed search engine
1561
1568
which is 'text' on CirrusSearch'.
1562
1569
raises APIError instead of Error if searchstring is not set
1563
1570
or what parameter is wrong.
1571
+ .. versionchanged:: 10.0
1572
+ The *sort* parameter was added.
1564
1573
1565
1574
.. seealso:: :api:`Search`
1566
1575
1567
1576
: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.
1573
1586
:raises KeyError: a namespace identifier was not resolved
1574
1587
:raises TypeError: a namespace identifier has an inappropriate
1575
1588
type such as NoneType or bool
@@ -1580,10 +1593,18 @@ def search(
1580
1593
"""
1581
1594
if not namespaces and namespaces != 0 :
1582
1595
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
1583
1604
srgen = self ._generator (api .PageGenerator , type_arg = 'search' ,
1584
- gsrsearch = searchstring , gsrwhat = where ,
1585
1605
namespaces = namespaces ,
1586
- total = total , g_content = content )
1606
+ total = total , g_content = content ,
1607
+ parameters = parameters )
1587
1608
return srgen
1588
1609
1589
1610
def usercontribs (
0 commit comments