Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Script to add a language
# -*- coding: utf-8 -*-
Automatically adds a new language to the KSP wiki. It follows
the instructions from :
1. Create MediaWiki:langlink-##, where ## is the two-letter language code. The content should be {{mw-langlink|##}}
2. Add {{mp-lang|##}} to Template:Main Page Layout/Language Box.
3. Add ** langlink-##|Language Name to MediaWiki:Sidebar under Languages.
4. Check the templates in Category:Language code templates if the language code needs to be added.
import pywikibot
import re
import sys
from string import Template
from import Page, Category
#TODO: Last place find
def insert_alphabetically(page, regex, language_code, new, comment, suffix="\n"):
Inserts the "new" into the list given in site. Each entry of the list, is matched by
"regex". This regex must have a group named "code" OR a placeholder named "code" ($code).
This code is then checked against language_code, and if the current code is after the
language_code it adds the "new" prior to that. The new entry needs a placeholde named code
On both "regex" and "new" is string.Template.substitute applied. Any trailing $-signs are
automatically escaped if not done so.
if"[^$](\$\$)*\$$", regex):
regex += '$'
oldtext = page.text
matches = list(re.finditer(Template(regex).substitute(code='(?P<code>[a-z]{2}(-[a-z]{2})?)'), oldtext, re.M))
# print Template(regex).substitute(code='(?P<code>[a-z]{2}(-[a-z]{2})?)')
if len(matches) == 0:
print("No matches found.")
return False
insert_at = 0
for match in matches:
if'code') == language_code:
print("Language is already available")
return False
elif'code') > language_code:
insert_at = match.start()
insert_at = match.end() + 1 # Skip over \n
if insert_at > 0 and len(oldtext) == insert_at - len(suffix) + 1:
oldtext += suffix
page.text = oldtext[:insert_at]
if callable(new):
page.text += new(language_code, match)
page.text += Template(new).substitute(code=language_code) + suffix
page.text += oldtext[insert_at:]
print("Changes in {}:".format(page.title()))
pywikibot.showDiff(oldtext, page.text)
if pywikibot.inputChoice("Save changes?", ['Yes', 'No'], ['y', 'n'], 'n') == 'y':
return True
site = pywikibot.getSite()
language_code = None
args = iter(pywikibot.handleArgs())
for arg in args:
if arg == "--language" or arg == "-l":
language_code = next(args, None)
if language_code is None:
print "Found language code parameter but no definition"
if language_code is None:
print("Language code is not given.")
elif not"^[A-Za-z]{2}(-[A-Za-z]{2})?$", language_code):
print("Invalid language code format. Must be XX or XX-XX!")
elif language_code.lower() != language_code:
language_code = language_code.lower()
print("Found capital letters in language code. Changed it to '{}'".format(language_code))
comment_new = "+added {}".format(language_code)
mw_langlink = Page(site, "MediaWiki:langlink-{}".format(language_code))
if mw_langlink.exists():
print("'{}' already exists. Skipped creation.".format(mw_langlink.title()))
mw_langlink.text = "{{{{mw-langlink|{}}}}}".format(language_code)
insert_alphabetically(Page(site, "MediaWiki:Sidebar"), r"^\*\* langlink-$code\|.*$", language_code, "** langlink-$code|{{subst:#language:$code}}", comment_new)
def optional_prefix(code, match=None):
ret = Template("{{mp-lang|$code}}").substitute(code=code) + "\n"
if match is None:
return ret
elif match.start() == 0:
return ret + "&middot; "
return "&middot; " + ret
if len(language_code) == 5 and pywikibot.inputChoice("Is this a dialect language?", ['Yes', 'No'], ['y', 'n'], 'n') == 'y':
dialects = Page(site, "Template:Main Page Layout/Language Box/dialects {}".format(language_code[:2]))
if dialects.exists():
insert_alphabetically(dialects, r"^(?:&middot; )?{{mp-lang\|$code}}$", language_code, optional_prefix, comment_new)
dialects.text = optional_prefix(language_code)
print dialects.text
insert_alphabetically(Page(site, "Template:Main Page Layout/Language Box"), r"^&middot; {{mp-lang\|$code\|{{{1\|}}}}}$", language_code, "&middot; {{mp-lang|$code|{{{1|}}}}}", comment_new)
code_templates = {
"lang": (r"^\|\s*$code\s*=\s*{{{[a-z]{2}(-[a-z]{2})\|}}}$", "| $code = {{{$code|}}}"),
"if lang": (r"\|\s*$code", "|$code", ""),
for k, v in code_templates.iteritems():
cat = Category(site, "Category:BobBot {} type".format(k))
print("Going through '{}'.".format(cat.title()))
for article in cat.articles():
if article.title().startswith("Template:"):
insert_alphabetically(article, v[0], language_code, v[1], comment_new, "\n" if len(v) < 3 else v[2])
end = article.text.rfind("</includeonly>")
if end < 0:
end = article.text.rfind("<noinclude>")
end += len("</includeonly>")
if end < 0:
end = len(article.text)
end += len("<noinclude>")
print article.text[:end]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.