jenkins-bot has submitted this change and it was merged.
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'. Deprecate the old parameter. - enable 'nearmatch' with mw 1.17+ - 'title' search was deactivated for wm sites. Use "intitle:" inside searchstring and print a deprecate warning - some tests added
Bug: T127807 Change-Id: I20e8625115c894ee91e104e40833ea0af3f4ecf6 --- M pywikibot/site.py M tests/site_tests.py 2 files changed, 44 insertions(+), 7 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/site.py b/pywikibot/site.py index 4304a67..3b16319 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, @@ -4417,8 +4418,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", "title" 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 @@ -4431,10 +4432,29 @@ @raises TypeError: a namespace identifier has an inappropriate type such as NoneType or bool """ + where_types = ['text', 'title', 'titles'] + if MediaWikiVersion(self.version()) >= MediaWikiVersion('1.17'): + where_types.append('nearmatch') if not searchstring: raise Error("search: searchstring cannot be empty") - if where not in ("text", "titles"): + if where not in where_types: raise Error("search: unrecognized 'where' value: %s" % 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='{0}'".format(where), + "searchstring='{0}'".format(searchstring), 2) + where = None # default + else: + if where == 'titles': + issue_deprecation_warning("where='titles'", + "where='title'", 2) + if MediaWikiVersion(self.version()) < MediaWikiVersion('1.11'): + where = 'titles' + else: + where = 'title' if namespaces == []: namespaces = [ns_id for ns_id in self.namespaces if ns_id >= 0] if not namespaces: diff --git a/tests/site_tests.py b/tests/site_tests.py index a54b9ab..9922170 100644 --- a/tests/site_tests.py +++ b/tests/site_tests.py @@ -1362,12 +1362,16 @@
"""Test search method."""
+ def setUp(self): + """Skip tests for Wikia Search extension.""" + super(SearchTestCase, self).setUp() + 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) + mysite = self.site try: se = list(mysite.search("wiki", total=100)) self.assertLessEqual(len(se), 100) @@ -1392,6 +1396,19 @@ 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):
pywikibot-commits@lists.wikimedia.org