Xqt has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/273015

Change subject: [bugfix] gsrwhat parameter for site.search differs for several 
mw versions
......................................................................

[bugfix] gsrwhat parameter for site.search differs for several mw versions

- 'titles' was changes to 'title'. Enable both but show a warning.
- enable 'nearmatch' with mw 1.17+
- 'title' search was deactivated for wm sites.
  Use "intitle:" inside searchstring and print a warning
- some tests added

Bug: T127807

Change-Id: I20e8625115c894ee91e104e40833ea0af3f4ecf6
---
M pywikibot/site.py
M tests/site_tests.py
2 files changed, 53 insertions(+), 15 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core 
refs/changes/15/273015/1

diff --git a/pywikibot/site.py b/pywikibot/site.py
index 39ac5cb..b97d1f1 100644
--- a/pywikibot/site.py
+++ b/pywikibot/site.py
@@ -61,6 +61,7 @@
     UserBlocked,
     EntityTypeUnknownException,
 )
+from pywikibot.family import WikimediaFamily
 from pywikibot.throttle import Throttle
 from pywikibot.tools import (
     itergroup, UnicodeMixin, ComparableMixin, SelfCallMixin, SelfCallString,
@@ -1208,6 +1209,7 @@
         """Return list of language codes for ordering of interwiki links."""
         return self.family.interwiki_putfirst.get(self.code, None)
 
+    @deprecated("family attribute")
     def getSite(self, code):
         """Return Site object for language 'code' in this Family."""
         return pywikibot.Site(code=code, fam=self.family, user=self.user())
@@ -4408,8 +4410,8 @@
 
         @param searchstring: the text to search for
         @type searchstring: unicode
-        @param where: Where to search; value must be "text" or "titles" (many
-            wikis do not support title search)
+        @param where: Where to search; value must be "text", "titles" or
+            "nearmatch" (many wikis do not support title or nearmatch search)
         @param namespaces: search only in these namespaces (defaults to all)
         @type namespaces: iterable of basestring or Namespace key,
             or a single instance of those types.  May be a '|' separated
@@ -4422,17 +4424,35 @@
         @raises TypeError: a namespace identifier has an inappropriate
             type such as NoneType or bool
         """
+        what_types = ['text', 'title', 'titles']
+        if MediaWikiVersion(self.version()) >= MediaWikiVersion('1.17'):
+            what_types.append('nearmatch') 
         if not searchstring:
             raise Error("search: searchstring cannot be empty")
-        if where not in ("text", "titles"):
+        if where not in what_types:
             raise Error("search: unrecognized 'where' value: %s" % where)
+        what = where
+        if where in ('title', 'titles'):
+            if isinstance(self.family, WikimediaFamily):
+                # 'title' search was disabled, use intitle instead
+                searchstring = 'intitle:' + searchstring
+                issue_deprecation_warning(
+                    'where=' + where,
+                    'search string "intitle:{0}"'.format(what), 2)
+                where = what = None  # default
+            elif MediaWikiVersion(self.version()) < MediaWikiVersion('1.11'):
+                what = 'titles'
+            else:
+                what = 'title'
+            if what != where:
+                issue_deprecation_warning('where=' + where, 'where=' + what, 2)
         if namespaces == []:
             namespaces = [ns_id for ns_id in self.namespaces if ns_id >= 0]
         if not namespaces:
             pywikibot.warning(u"search: namespaces cannot be empty; using 
[0].")
             namespaces = [0]
         srgen = self._generator(api.PageGenerator, type_arg="search",
-                                gsrsearch=searchstring, gsrwhat=where,
+                                gsrsearch=searchstring, gsrwhat=what,
                                 namespaces=namespaces, step=step,
                                 total=total, g_content=content)
         if MediaWikiVersion(self.version()) < MediaWikiVersion('1.23'):
diff --git a/tests/site_tests.py b/tests/site_tests.py
index 989e940..7bf8fbe 100644
--- a/tests/site_tests.py
+++ b/tests/site_tests.py
@@ -1355,36 +1355,54 @@
 
     """Test search method."""
 
+    def setUp(self):
+        super(SearchTestCase, self).setUp()
+        self.site = self.get_site()
+        if self.site.has_extension("Wikia Search"):
+            raise unittest.SkipTest(
+                'The site %r does not use MediaWiki search' % self.site)       
+
     def testSearch(self):
         """Test the site.search() method."""
-        mysite = self.get_site()
-        if mysite.has_extension("Wikia Search"):
-            raise unittest.SkipTest(
-                'The site %r does not use MediaWiki search' % mysite)
         try:
-            se = list(mysite.search("wiki", total=100))
+            se = list(self.site.search("wiki", total=100))
             self.assertLessEqual(len(se), 100)
             self.assertTrue(all(isinstance(hit, pywikibot.Page)
                                 for hit in se))
             self.assertTrue(all(hit.namespace() == 0 for hit in se))
-            for hit in mysite.search("common", namespaces=4, total=5):
+            for hit in self.site.search("common", namespaces=4, total=5):
                 self.assertIsInstance(hit, pywikibot.Page)
                 self.assertEqual(hit.namespace(), 4)
-            for hit in mysite.search("word", namespaces=[5, 6, 7], total=5):
+            for hit in self.site.search("word", namespaces=[5, 6, 7], total=5):
                 self.assertIsInstance(hit, pywikibot.Page)
                 self.assertIn(hit.namespace(), [5, 6, 7])
-            for hit in mysite.search("another", namespaces="8|9|10", total=5):
+            for hit in self.site.search('another', namespaces='8|9|10',
+                                        total=5):
                 self.assertIsInstance(hit, pywikibot.Page)
                 self.assertIn(hit.namespace(), [8, 9, 10])
-            for hit in mysite.search("wiki", namespaces=0, total=10,
-                                     get_redirects=True):
+            for hit in self.site.search("wiki", namespaces=0, total=10,
+                                        get_redirects=True):
                 self.assertIsInstance(hit, pywikibot.Page)
                 self.assertEqual(hit.namespace(), 0)
         except pywikibot.data.api.APIError as e:
             if e.code == "gsrsearch-error" and "timed out" in e.info:
-                raise unittest.SkipTest("gsrsearch returned timeout on site: 
%r" % e)
+                raise unittest.SkipTest(
+                    'gsrsearch returned timeout on site: %r' % e)
             raise
 
+    def test_search_where(self):
+        """Test the site.search() method with 'where' parameter."""
+        self.assertEqual(list(self.site.search("wiki", total=10)),
+                         list(self.site.search("wiki", total=10, 
where='text')))
+        self.assertLessEqual(len(list(self.site.search('wiki', total=10,
+                                                       where='nearmatch'))),
+                             len(list(self.site.search('wiki', total=10))))
+        for hit in self.site.search('wiki', namespaces=0, total=10,
+                                    get_redirects=True, where='title'):
+            self.assertIsInstance(hit, pywikibot.Page)
+            self.assertEqual(hit.namespace(), 0)
+            self.assertTrue('wiki' in hit.title().lower())
+
 
 class TestUserContribsAsUser(DefaultSiteTestCase):
 

-- 
To view, visit https://gerrit.wikimedia.org/r/273015
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I20e8625115c894ee91e104e40833ea0af3f4ecf6
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Xqt <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to