jenkins-bot has submitted this change and it was merged.
Change subject: site.py: Support titles parameter in watch action ......................................................................
site.py: Support titles parameter in watch action
Deprecate 'watchpage' method and add 'watch' as replacement.
In the 'watch' method: - Handle multiple pages. - Use 'titles' instead of 'title' for mw 1.23+ API calls. - Iterate over all pages for mw versions < 1.23.
Use the 'watch' method in BasePage().watch.
In the new implementation, an exception is raised if an unexpected API response is received (instead of just showing a UI error as 'watchpage' does).
Changing the UI error to an exception could not be implemented in the old 'watchpage' method without introducing a, although slight, breaking change. See John Vandenberg's comment at T103736#1917344.
Other than that, 's.watch(pages)' feels more intuitive than 's.watchpage(pages)'.
Bug: T103736 Change-Id: I75621972f23509a7e6c843e69abad52025546526 --- M pywikibot/page.py M pywikibot/site.py 2 files changed, 45 insertions(+), 3 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py index 34f30b6..811bc46 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -1240,7 +1240,7 @@
@return: bool; True if successful, False otherwise. """ - return self.site.watchpage(self, unwatch) + return self.site.watch(self, unwatch)
def purge(self, **kwargs): """Purge the server's cache for this page. diff --git a/pywikibot/site.py b/pywikibot/site.py index a7c0bd4..8cd2aa8 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -5387,16 +5387,58 @@ return data
@must_be(group='user') + def watch(self, pages, unwatch=False): + """Add or remove pages from watchlist. + + @param pages: A single page or a sequence of pages. + @type pages: A page object, a page-title string, or sequence of them. + Also accepts a single pipe-separated string like 'title1|title2'. + @param unwatch: If True, remove pages from watchlist; + if False add them (default). + @return: True if API returned expected response; False otherwise + @rtype: bool + + """ + parameters = {'action': 'watch', + 'token': self.tokens['watch'], + 'unwatch': unwatch} + unwatch = 'unwatched' if unwatch else 'watched' + if MediaWikiVersion(self.version()) >= MediaWikiVersion('1.23'): + parameters['titles'] = pages + req = self._simple_request(**parameters) + results = req.submit() + return all(unwatch in r for r in results['watch']) + + # MW version < 1.23 + if isinstance(pages, str): + if '|' in pages: + pages = pages.split('|') + else: + pages = (pages,) + + for page in pages: + parameters['title'] = page + req = self._simple_request(**parameters) + result = req.submit() + if unwatch not in result['watch']: + return False + return True + + @must_be(group='user') + @deprecated('Site().watch') def watchpage(self, page, unwatch=False): """Add or remove page from watchlist.
+ DEPRECATED: Use Site().watch() instead. + + @param page: A single page. + @type page: A page object, a page-title string. @param unwatch: If True, remove page from watchlist; if False (default), add it. @return: True if API returned expected response; False otherwise + @rtype: bool
""" - # TODO: Separated parameters to allow easy conversion to 'watchpages' - # as the API now allows 'titles'. parameters = {'action': 'watch', 'title': page, 'token': self.tokens['watch'],
pywikibot-commits@lists.wikimedia.org