Legoktm has uploaded a new change for review. https://gerrit.wikimedia.org/r/82263
Change subject: Use the language of the page, not necessarily the general site ...................................................................... Use the language of the page, not necessarily the general site It's possible that the user specificies a generator like -cat:zh:Category:Blah", in which case the value of pywikibot.Site().language() may not be "zh", depending on the user's config. It's also possible that in the future a generator will yield pages on multiple projects, so this guards against that as well. Change-Id: Ic1f10118a2dff93b71861b7ea97f7da22908cb04 --- M scripts/claimit.py M scripts/harvest_template.py 2 files changed, 46 insertions(+), 29 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core refs/changes/63/82263/1 diff --git a/scripts/claimit.py b/scripts/claimit.py index 0f50db0..e72e804 100755 --- a/scripts/claimit.py +++ b/scripts/claimit.py @@ -33,23 +33,29 @@ self.generator = generator self.claims = claims self.repo = pywikibot.Site().data_repository() - self.source = None - self.setSource(pywikibot.Site().language()) + self.cacheSources() - def setSource(self, lang): + def getSource(self, lang): """ - Get the source + Get the source for the specified language, + if possible """ - page = pywikibot.Page(self.repo, 'Wikidata:List of wikis/python') - source_values = json.loads(page.get()) - source_values = source_values['wikipedia'] - for source_lang in source_values: - source_values[source_lang] = pywikibot.ItemPage(self.repo, - source_values[source_lang]) + if lang in self.source_values: + source = pywikibot.Claim(self.repo, 'p143') + source.setTarget(self.source_values.get(lang)) + return source - if lang in source_values: - self.source = pywikibot.Claim(self.repo, 'p143') - self.source.setTarget(source_values.get(lang)) + def cacheSources(self): + """ + Fetches the sources from the onwiki list + and stores it internally + """ + page = pywikibot.Page(self.repo, u'Wikidata:List of wikis/python') + self.source_values = json.loads(page.get()) + self.source_values = self.source_values['wikipedia'] + for source_lang in self.source_values: + self.source_values[source_lang] = pywikibot.ItemPage(self.repo, + self.source_values[source_lang]) def run(self): """ @@ -72,8 +78,10 @@ pywikibot.output('Adding %s --> %s' % (claim.getID(), claim.getTarget())) item.addClaim(claim) - if self.source: - claim.addSource(self.source, bot=True) + # A generator might yield pages from multiple languages + source = self.getSource(page.site.language()) + if source: + claim.addSource(source, bot=True) # TODO FIXME: We need to check that we aren't adding a # duplicate diff --git a/scripts/harvest_template.py b/scripts/harvest_template.py index 2bc5ace..40e43a3 100755 --- a/scripts/harvest_template.py +++ b/scripts/harvest_template.py @@ -45,22 +45,29 @@ # TODO: Make it a list which also includes the redirects to the template self.fields = fields self.repo = pywikibot.Site().data_repository() - self.source = None - self.setSource(pywikibot.Site().language()) + self.cacheSources() - def setSource(self, lang): + def getSource(self, lang): """ - Get the source + Get the source for the specified language, + if possible """ - page = pywikibot.Page(self.repo, 'Wikidata:List of wikis/python') - source_values = json.loads(page.get()) - source_values = source_values['wikipedia'] - for source_lang in source_values: - source_values[source_lang] = pywikibot.ItemPage(self.repo, source_values[source_lang]) + if lang in self.source_values: + source = pywikibot.Claim(self.repo, 'p143') + source.setTarget(self.source_values.get(lang)) + return source - if lang in source_values: - self.source = pywikibot.Claim(self.repo, 'p143') - self.source.setTarget(source_values.get(lang)) + def cacheSources(self): + """ + Fetches the sources from the onwiki list + and stores it internally + """ + page = pywikibot.Page(self.repo, u'Wikidata:List of wikis/python') + self.source_values = json.loads(page.get()) + self.source_values = self.source_values['wikipedia'] + for source_lang in self.source_values: + self.source_values[source_lang] = pywikibot.ItemPage(self.repo, + self.source_values[source_lang]) def run(self): """ @@ -135,8 +142,10 @@ pywikibot.output('Adding %s --> %s' % (claim.getID(), claim.getTarget())) item.addClaim(claim) - if self.source: - claim.addSource(self.source, bot=True) + # A generator might yield pages from multiple sites + source = self.getSource(page.site.language()) + if source: + claim.addSource(source, bot=True) def main(): -- To view, visit https://gerrit.wikimedia.org/r/82263 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ic1f10118a2dff93b71861b7ea97f7da22908cb04 Gerrit-PatchSet: 1 Gerrit-Project: pywikibot/core Gerrit-Branch: master Gerrit-Owner: Legoktm <legoktm.wikipe...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits