用户:P-bot/Internal link helper模板清理

import wikipedia

def linkcleaner(text):
    elems = text.split('|');
    title = elems[1].strip();
    dscrp = ''
    if len(elems) == 4:
        dscrp = elems[3].replace('}', '').strip();
    page = wikipedia.Page(wikipedia.getSite(), title);
    if (page.exists()):
        if dscrp:
            return '[[%s|%s]]' % (title, dscrp)
        else:
            return '[[%s]]' % title
    else:
        return text

def linkclean():
    return lambda m:linkcleaner(m.group(0))

fixes['link-en'] = {
    'regex': True,
    'msg': {
        'zh': u'机器人:清理已存在但仍使用内部链接助手的链接',
    },
    'replacements': [
        (ur'\{\{\s*(([iI]nternal link helper/|[lL]ink-)(de|en|es|fr|ja|ru|sv)|([Dd]e|[Ee]n|[Ee]s|[Ff]r|[Jj]a|[Rr]u|[Ss]v)-link)[\s\S]*?\}\}',
         linkclean())
    ],
    'exceptions': {
        'title': [
            ur'Template:Internal_link_helper.*',
        ],
    }
}