Xqt has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/758082 )

Change subject: [IMPR] Remove deprecation warning in APISite.search()
......................................................................

[IMPR] Remove deprecation warning in APISite.search()

APISite.search():
- remove search parameter checks in APISite.search() and delegate it
  to the API request.
- change default of where parameter to None which is 'text' for
  search engines where title search is deactivated
- update documentation

Request.submit():
- print a warning if 'title' or 'text' value of 'where' search
  parameter is disabled and fall back to the search engine's default.
  if CirrusSearch is installed and 'title' is used, modify the search
  string and use intitle: directive
- update tests

Change-Id: Ie36d141752d42f669d4bb0f3e3522026a9f91c83
---
M pywikibot/data/api.py
M pywikibot/site/_generators.py
M tests/site_tests.py
3 files changed, 33 insertions(+), 42 deletions(-)

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



diff --git a/pywikibot/data/api.py b/pywikibot/data/api.py
index 48f4888..47c1a6e 100644
--- a/pywikibot/data/api.py
+++ b/pywikibot/data/api.py
@@ -1849,6 +1849,17 @@
                 self.wait()
                 continue

+            if code in ('search-title-disabled', 'search-text-disabled'):
+                prefix = 'gsr' if 'gsrsearch' in self._params else 'sr'
+                del self._params[prefix + 'what']
+                # use intitle: search instead
+                if code == 'search-title-disabled' \
+                   and self.site.has_extension('CirrusSearch'):
+                    key = prefix + 'search'
+                    self._params[key] = ['intitle:' + search
+                                         for search in self._params[key]]
+                continue
+
             if code == 'urlshortener-blocked':  # T244062
                 # add additional informations to result['error']
                 result['error']['current site'] = self.site
diff --git a/pywikibot/site/_generators.py b/pywikibot/site/_generators.py
index 679a276..f3aaa0f 100644
--- a/pywikibot/site/_generators.py
+++ b/pywikibot/site/_generators.py
@@ -1311,7 +1311,7 @@

     def search(self, searchstring: str, *,
                namespaces=None,
-               where: str = 'text',
+               where: Optional[str] = None,
                total: Optional[int] = None,
                content: bool = False):
         """Iterate Pages that contain the searchstring.
@@ -1319,11 +1319,18 @@
         Note that this may include non-existing Pages if the wiki's database
         table contains outdated entries.

-        :see: https://www.mediawiki.org/wiki/API:Search
+        .. versionchanged:: 7.0
+           Default of `where` parameter has been changed from 'text' to
+           None. The behaviour depends on the installed search engine
+           which is 'text' on CirrusSearch'.
+           raises APIError instead of Error if searchstring is not set
+           or what parameter is wrong.
+
+        .. seealso:: https://www.mediawiki.org/wiki/API:Search

         :param searchstring: the text to search for
-        :param where: Where to search; value must be "text", "title" or
-            "nearmatch" (many wikis do not support title or nearmatch search)
+        :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)
         :type namespaces: iterable of str or Namespace key,
             or a single instance of those types. May be a '|' separated
@@ -1333,25 +1340,11 @@
         :raises KeyError: a namespace identifier was not resolved
         :raises TypeError: a namespace identifier has an inappropriate
             type such as NoneType or bool
+        :raises APIError: The "gsrsearch" parameter must be set:
+            searchstring parameter is not set
+        :raises APIError: Unrecognized value for parameter "gsrwhat":
+            wrong where parameter is given
         """
-        where_types = ['nearmatch', 'text', 'title']
-        if not searchstring:
-            raise Error('search: searchstring cannot be empty')
-        if where not in where_types:
-            raise Error("search: unrecognized 'where' value: {}".format(where))
-
-        if where == 'title' \
-           and self.has_extension('CirrusSearch') \
-           and isinstance(self.family, pywikibot.family.WikimediaFamily):
-            # 'title' search was disabled, use intitle instead
-            searchstring = 'intitle:' + searchstring
-            issue_deprecation_warning(
-                "where='{}'".format(where),
-                "searchstring='{}'".format(searchstring),
-                since='20160224')
-
-            where = None  # default
-
         if not namespaces and namespaces != 0:
             namespaces = [ns_id for ns_id in self.namespaces if ns_id >= 0]
         srgen = self._generator(api.PageGenerator, type_arg='search',
diff --git a/tests/site_tests.py b/tests/site_tests.py
index 13ed4ed..faf457c 100644
--- a/tests/site_tests.py
+++ b/tests/site_tests.py
@@ -1460,7 +1460,6 @@
                 self.skipTest('gsrsearch is diabled on site {}:\n{!r}'
                               .format(mysite, e))

-    @suppress_warnings("where='title' is deprecated", FutureWarning)
     def test_search_where_title(self):
         """Test site.search() method with 'where' parameter set to title."""
         search_gen = self.site.search(
@@ -1468,27 +1467,15 @@
         expected_params = {
             'prop': ['info', 'imageinfo', 'categoryinfo'],
             'inprop': ['protection'],
-            'iiprop': [
-                'timestamp', 'user', 'comment', 'url', 'size', 'sha1',
-                'metadata'],
+            'iiprop': ['timestamp', 'user', 'comment', 'url', 'size', 'sha1',
+                       'metadata'],
             'iilimit': ['max'], 'generator': ['search'], 'action': ['query'],
-            'indexpageids': [True], 'continue': [True], 'gsrnamespace': [0]}
-        if self.site.has_extension('CirrusSearch'):
-            expected_params.update({
-                'gsrsearch': ['intitle:wiki'], 'gsrwhat': [None]})
-        else:
-            expected_params.update({
-                'gsrsearch': ['wiki'], 'gsrwhat': ['title']})
+            'indexpageids': [True], 'continue': [True],
+            'gsrnamespace': [0], 'gsrsearch': ['wiki'], 'gsrwhat': ['title']}
         self.assertEqual(search_gen.request._params, expected_params)
-        try:
-            for hit in search_gen:
-                self.assertIsInstance(hit, pywikibot.Page)
-                self.assertEqual(hit.namespace(), 0)
-        except APIError as e:
-            if e.code in ('search-title-disabled', 'gsrsearch-title-disabled'):
-                self.skipTest(
-                    'Title search disabled on site: {}'.format(self.site))
-            raise
+        for hit in search_gen:
+            self.assertIsInstance(hit, pywikibot.Page)
+            self.assertEqual(hit.namespace(), 0)


 class TestUserContribsAsUser(DefaultSiteTestCase):

--
To view, visit https://gerrit.wikimedia.org/r/c/pywikibot/core/+/758082
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.wikimedia.org/r/settings

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ie36d141752d42f669d4bb0f3e3522026a9f91c83
Gerrit-Change-Number: 758082
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to