This script shortens local links to a section on the same page (from [[Page#Section|Section]]
to [[#Section|Section]]
). It does not have any parameters and will always process all pages on the wiki.
# -*- coding: utf-8 -*- r""" This bot will remove the page part from section links that are on the same page. """ import pywikibot import re from pywikibot import pagegenerators def link_bot(page): text = page.text title = page.title() # Regex to replace old_string = '\[\[' + re.escape(title) + '#(.+?)\|(.+?)\]\]' new_string = '[[#\g<1>|\g<2>]]' # Replace new_text = re.sub(old_string, new_string ,text) # Check if something changed if text != new_text: page.text = new_text page.save(u"Bot: Shortening section links") print('\x1b[6;30;42m' + title + ': updated.\x1b[0m') else: print(title + ': No changes needed.') def main(): gen = pywikibot.pagegenerators.AllpagesPageGenerator() for page in gen: link_bot(page) if __name__ == '__main__': main()