jenkins-bot has submitted this change and it was merged.
Change subject: Fix newitem.py to instantiate a new item ......................................................................
Fix newitem.py to instantiate a new item
Ibea8f84242a6e938882059f5d5fa28394b8a7a23 changed the ItemPage.fromPage semantics to raise NoPage if the page does not exist, or is not linked.
Catch this exception, and instantiate a new item to call editEntity with. Also add test case to create a new item on testwikidata.
Bug: 71095 Change-Id: I612f56d844684d85890af1c7d33051c292552253 --- M scripts/newitem.py M tests/wikibase_edit_tests.py 2 files changed, 43 insertions(+), 5 deletions(-)
Approvals: XZise: Looks good to me, approved jenkins-bot: Verified
diff --git a/scripts/newitem.py b/scripts/newitem.py index 99e83dd..7210d2a 100644 --- a/scripts/newitem.py +++ b/scripts/newitem.py @@ -65,13 +65,18 @@ pywikibot.output(u'%s does not exist anymore. Skipping...' % page) continue - item = pywikibot.ItemPage.fromPage(page) - if item.exists(): + try: + item = pywikibot.ItemPage.fromPage(page) + except pywikibot.NoPage: + pass + else: pywikibot.output(u'%s already has an item: %s.' % (page, item)) if self.getOption('touch'): pywikibot.output(u'Doing a null edit on the page.') page.put(page.text) - elif page.isRedirectPage(): + continue + + if page.isRedirectPage(): pywikibot.output(u'%s is a redirect page. Skipping.' % page) elif page.editTime() > self.lastEditBefore: pywikibot.output( @@ -96,8 +101,8 @@ % page.title(asLink=True, insite=self.repo))
data = {'sitelinks': - {item.getdbName(page.site): - {'site': item.getdbName(page.site), + {page.site.dbName(): + {'site': page.site.dbName(), 'title': page.title()} }, 'labels': @@ -107,6 +112,9 @@ } } pywikibot.output(summary) + + # Create empty item object and add 'data' + item = pywikibot.ItemPage(page.site.data_repository()) item.editEntity(data, summary=summary) # And do a null edit to force update page.put(page.text) diff --git a/tests/wikibase_edit_tests.py b/tests/wikibase_edit_tests.py index b4e7247..a060fd5 100644 --- a/tests/wikibase_edit_tests.py +++ b/tests/wikibase_edit_tests.py @@ -97,6 +97,36 @@ item = pywikibot.ItemPage(testsite) item.editEntity(data)
+ def test_edit_entity_new_linked_item(self): + ts = str(time.time()) + + # Create a new page, which is unlinked + site = self.get_site() + title = 'Wikidata:Test ' + ts + page = pywikibot.Page(site, title) + page.text = ts + page.save() + + data = { + 'labels': { + 'en': { + 'language': 'en', + 'value': 'Pywikibot test new linked item', + } + }, + 'sitelinks': { + page.site.dbName(): { + 'site': page.site.dbName(), + 'title': page.title() + } + }, + } + + repo = self.get_repo() + item = pywikibot.ItemPage(repo) + self.assertEqual(item._defined_by(), dict()) + item.editEntity(data) +
if __name__ == '__main__': try:
pywikibot-commits@lists.wikimedia.org