jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/505409 )
Change subject: Implement create_short_link function to use Extension:UrlShortener ......................................................................
Implement create_short_link function to use Extension:UrlShortener
Implement create_short_link in APISite then in BasePage.
Bug: T220876 Change-Id: I57280c24e0049cda751bf5646f58079763316b55 --- M pywikibot/family.py M pywikibot/page.py M pywikibot/site.py 3 files changed, 52 insertions(+), 0 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/family.py b/pywikibot/family.py index 5f5c932..97ed83e 100644 --- a/pywikibot/family.py +++ b/pywikibot/family.py @@ -933,6 +933,10 @@ # 'pt': { '_default': [0]} # }
+ # Some wiki farms have UrlShortener extension enabled only on the main + # site. This value can specify this last one with (lang, family) tuple. + shared_urlshortner_wiki = None + _families = {}
def __getattribute__(self, name): @@ -1676,6 +1680,10 @@ # wikibase code and item number: disambcatname = {'wikidata': 'Q1982926'}
+ # UrlShortener extension is only usable on metawiki, and this wiki can + # process links to all WM domains. + shared_urlshortner_wiki = ('meta', 'meta') + @classproperty def domain(cls): """Domain property.""" diff --git a/pywikibot/page.py b/pywikibot/page.py index 05c05f8..b4cfd5f 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -2212,6 +2212,33 @@ """ return self.content_model == 'flow-board'
+ def create_short_link(self, permalink=False, with_protocol=False): + """ + Return a shortened link that points to that page. + + If shared_urlshortner_wiki is defined in family config, it'll use + that site to create the link instead of the current wiki. + + @param permalink: If true, the link will point to the actual revision + of the page. + @type permalink: bool + @param with_protocol: If true, the link will have https propotol + prepend. + @type with_protocol: bool + @return: The reduced link. + @rtype: str + """ + wiki = self.site + if self.site.family.shared_urlshortner_wiki: + wiki = pywikibot.Site(*self.site.family.shared_urlshortner_wiki) + + url = self.permalink() if permalink else self.full_url() + + link = wiki.create_short_link(url) + if with_protocol: + return '{}://{}'.format(wiki.protocol(), link) + return link + # ####### DEPRECATED METHODS ########
@deprecated('Site.encoding()', since='20090307') diff --git a/pywikibot/site.py b/pywikibot/site.py index 0373533..12de127 100644 --- a/pywikibot/site.py +++ b/pywikibot/site.py @@ -7454,6 +7454,23 @@ gen.set_maximum_items(total) return gen
+ @need_extension('UrlShortener') + def create_short_link(self, url): + """ + Return a shortened link. + + Note that on Wikimedia wikis only metawiki supports this action, + and this wiki can process links to all WM domains. + + @param url: The link to reduce, with propotol prefix. + @type url: str + @return: The reduced link, without protocol prefix. + @rtype: str + """ + req = self._simple_request(action='shortenurl', url=url) + data = req.submit() + return data['shortenurl']['shorturl'] + # aliases for backwards compatibility isBlocked = redirect_func(is_blocked, old_name='isBlocked', class_name='APISite', since='20141218')