jenkins-bot has submitted this change and it was merged.
Change subject: set_redirect_target method for ItemPage objects ......................................................................
set_redirect_target method for ItemPage objects
It lets people to set a redirect page
Bug: T77971 Change-Id: Iedf546ff2f7045ce175cf75603a94a4f53308595 --- M pywikibot/page.py M pywikibot/site.py 2 files changed, 51 insertions(+), 0 deletions(-)
Approvals: John Vandenberg: Looks good to me, approved XZise: Looks good to me, but someone else must approve jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py index 9c71b95..a1b3c68 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -3368,6 +3368,15 @@ data = {'aliases': aliases} self.editEntity(data, **kwargs)
+ def set_redirect_target(self, target_page, create=False, force=False, + keep_section=False, save=True, **kwargs): + """ + Set target of a redirect for a Wikibase page. + + Has not been implemented in the Wikibase API yet, except for ItemPage. + """ + raise NotImplementedError +
class ItemPage(WikibasePage):
@@ -3697,6 +3706,29 @@ """ self.repo.mergeItems(fromItem=self, toItem=item, **kwargs)
+ def set_redirect_target(self, target_page, create=False, force=False, + keep_section=False, save=True, **kwargs): + """ + Make the item redirect to another item. + + You need to define an extra argument to make this work, like save=True + @param target_page: target of the redirect, this argument is required. + @type target_page: pywikibot.Item or string + @param force: if true, it sets the redirect target even the page + is not redirect. + @type force: bool + """ + if isinstance(target_page, basestring): + target_page = pywikibot.ItemPage(self.repo, target_page) + elif self.repo != target_page.repo: + raise pywikibot.InterwikiRedirectPage(self, target_page) + if self.exists() and not self.isRedirectPage() and not force: + raise pywikibot.IsNotRedirectPage(self) + if not save or keep_section or create: + raise NotImplementedError + self.repo.set_redirect_target( + from_item=self, to_item=target_page) +
class Property():
diff --git a/pywikibot/site.py b/pywikibot/site.py index bbac719..ebaa8cf 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -5876,6 +5876,25 @@ data = req.submit() return data
+ def set_redirect_target(self, from_item, to_item): + """ + Make a redirect to another item. + + @param to_item: title of target item. + @type to_item: pywikibot.ItemPage + @param from_item: Title of the item to be redirected. + @type from_item: pywikibot.ItemPage + """ + params = { + 'action': 'wbcreateredirect', + 'from': from_item.getID(), + 'to': to_item.getID(), + 'token': self.tokens['edit'] + } + req = api.Request(site=self, **params) + data = req.submit() + return data + def createNewItemFromPage(self, page, bot=True, **kwargs): """ Create a new Wikibase item for a provided page.