From 83499ed3a5a6d6b8381191b3fb9c3b90f6a15a56 Mon Sep 17 00:00:00 2001 From: Brian Keegan Date: Thu, 21 Apr 2016 10:56:11 -0400 Subject: [PATCH 1/2] Implement getLinksHere function modeled on getLinks Implements functionality from https://www.mediawiki.org/wiki/API:Linkshere --- wikitools/page.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/wikitools/page.py b/wikitools/page.py index d4a0db1..3e37258 100644 --- a/wikitools/page.py +++ b/wikitools/page.py @@ -373,6 +373,35 @@ def getLinks(self, force=False): self.links.extend(self.__extractToList(data, 'links')) return self.links + def getLinksHere(self, namespace=0, lhshow='redirect', force=False): + """Gets a list of all the internal links elsewhere on Wikipedia pointing *to* the page + + force - load the list even if we already loaded it before + + """ + if self.links and not force: + return self.links + if self.pageid == 0 and not self.title: + self.setPageInfo() + if not self.exists: + raise NoPage + params = { + 'action': 'query', + 'prop': 'linkshere', + 'lhlimit': self.site.limit, + 'lhnamespace':namespace, + 'lhshow':lhshow, + } + if self.pageid > 0: + params['pageids'] = self.pageid + else: + params['titles'] = self.title + req = api.APIRequest(self.site, params) + self.links = [] + for data in req.queryGen(): + self.links.extend(self.__extractToList(data, 'linkshere')) + return self.links + def getProtection(self, force=False): """Returns the current protection status of the page""" if self.protection and not force: From 6b6f3b7d1a2df9c6cbfee87746a71d0ee1a80545 Mon Sep 17 00:00:00 2001 From: Brian Keegan Date: Fri, 29 Apr 2016 11:35:07 -0400 Subject: [PATCH 2/2] Clarify call parameters, default to all --- wikitools/page.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/wikitools/page.py b/wikitools/page.py index 3e37258..de543a2 100644 --- a/wikitools/page.py +++ b/wikitools/page.py @@ -373,9 +373,11 @@ def getLinks(self, force=False): self.links.extend(self.__extractToList(data, 'links')) return self.links - def getLinksHere(self, namespace=0, lhshow='redirect', force=False): + def getLinksHere(self, namespace='all', lhshow='all', force=False): """Gets a list of all the internal links elsewhere on Wikipedia pointing *to* the page + namespace - pipe-separated list of namespaces to collect, default is "all" + lhshow - include redirects with 'redirect', exclude with '!redirect', default is "all" force - load the list even if we already loaded it before """ @@ -389,9 +391,11 @@ def getLinksHere(self, namespace=0, lhshow='redirect', force=False): 'action': 'query', 'prop': 'linkshere', 'lhlimit': self.site.limit, - 'lhnamespace':namespace, - 'lhshow':lhshow, } + if namespace != 'all': + params['lhnamespace'] = namespace + if namespace != 'all': + params['lhshow'] = lhshow if self.pageid > 0: params['pageids'] = self.pageid else: