FANDOM


import pywikibot
from os.path import splitext
import re
 
# Global parameters, up to your wiki
DEFAULT_AUTHOR = "Square Enix"
FAMILY_NAME = "bravely"
FAMILY_LANG = "en"
DEFAULT_EDIT_COMMENT = "Running image standardization script."
WIKI_URL = "https://bravelydefault.fandom.com/wiki/"
OUTDATED_TEMPLATES = [r"\{\{Albumcover[^\}]*\}\}",\
	r"\{\{Audio sample[^\}]*\}\}",\
	r"\{\{Bookcover[^\}]*\}\}",\
	r"\{\{Copyright[^\}]*\}\}",\
	r"\{\{OCopyright[^\}]*\}\}",\
	r"\{\{Permission[^\}]*\}\}",\
	r"\{\{Cc-by-sa-3.0[^\}]*\}\}",\
	r"\{\{FCC[^\}]*\}\}",\
	r"\{\{FCopyright[^\}]*\}\}",\
	r"\{\{Fother[^\}]*\}\}",\
	r"\{\{FPD[^\}]*\}\}",\
	r"\{\{SECopyright[^\}]*\}\}",\
	r"\{\{From Wikimedia[^\}]*\}\}",\
	r"\{\{Gamecover[^\}]*\}\}",\
	r"\{\{Logo[^\}]*\}\}",\
	r"\{\{Lbox[^\}]*\}\}",\
	r"\{\{PD[^\}]*\}\}",\
	r"\{\{Promotional[^\}]*\}\}",\
	r"\{\{Scan[^\}]*\}\}",\
	r"\{\{Screenshot[^\}]*\}\}",\
	r"\{\{VideoCopyright[^\}]*\}\}",\
	r"\{\{Voice[^\}]*\}\}",\
	r"\[\[Category[^\]]*\]\]"]
 
# Main script starts here
options = input("Special settings (options=recurse,noredirect,test,relink): ")
site = pywikibot.Site(FAMILY_LANG, FAMILY_NAME)
edit_comment = input("Edit comment (optional): ") \
	or ((print("Edit comment (optional): " + DEFAULT_EDIT_COMMENT) and False) or DEFAULT_EDIT_COMMENT)
 
while True:
	cat_name, cat_type, cat_source = False, False, False
	while not cat_name:
		cat_name = input("Image category (type EXIT to exit): ")
	if cat_name == "EXIT":
		break
 
	cat = pywikibot.Category(site, "Category:" + cat_name)
	while not cat_type:
		cat_type = input("Category type: ")
	while not cat_source:
		cat_source = input("Category source: ")
	cat_subject = input("Category subject (optional): ")
	cat_author = input("Category author (optional): ") \
		or ((print("Category author (optional): " + DEFAULT_AUTHOR) and False) or DEFAULT_AUTHOR)
	cat_name = input("New category name (optional): ") \
		or ((print("New category name (optional): " + cat_name) and False) or cat_name)
 
	print("Now indexing over " + cat.title(with_ns=False))
 
	pages_to_relink = set()
	images_to_relink = {}
 
	for img in cat.articles(recurse=("recurse" in options), namespaces=6):
		img_oldname = img.title().replace(" ", "_")
		print("\n" + WIKI_URL + img_oldname)
 
		img_subject = False
		while not img_subject:
			img_subject = input("Image subject (type SKIP to skip this image): ")
		if "SKIP" in img_subject:
			continue
		if cat_subject:
			img_subject = img_subject + " " + cat_subject
		img_info = input("Extra info (optional): ")
		if img_info:
			img_info = " " + img_info
 
		img_newname = "File:" + img_subject \
			+ " from " + cat_source + " " + cat_type + img_info + splitext(img_oldname)[-1]
		img_infobox = "{{Infobox file\n"
		img_infobox = img_infobox + "| description = " + cat_type.capitalize() + " of " + img_subject + " from "
		img_infobox = img_infobox +  "''[[{{subst:#invoke:Codename|rel|" + cat_source + "|name}}]]''.\n"
		img_infobox = img_infobox + "| source = {{ref|game=" + cat_source + "}}\n}}"
 
		for template in OUTDATED_TEMPLATES:
			img.text = re.sub(template, "", img.text)
		img.text = img.text + img_infobox + "[[Category:" + cat_name + "]]"
 
		for page in img.usingPages(None, False):
			pages_to_relink.add(page)
 
		print("\nMoving to " + img_newname + " with image description:\n")
		print(img.text)
 
		if not "test" in options:
			try:
				img.save(edit_comment)
				img.move(img_newname, reason=edit_comment, noredirect=("noredirect" in options))
			except pywikibot.exceptions.Error as err:
				print(err)
 
	print("Images done!")
 
	for page in pages_to_relink:
		print("Relinking in " + page.title())
		for img_oldname in images_to_relink:
			img_newname = images_to_relink(img_oldname)
			page.text.replace(img_oldname, img_newname)
			page.text.replace(img_oldname.replace("_", " "), img_newname)
		if not "test" in options:
			page.save("Relinking image [Image edit summary: " + edit_summary + "]")
 
	if cat_name != cat.title(with_ns=False):
		print("Moving category to Category:" + cat_name)
		if not "test" in options:
			try:
				cat.move(cat_name, reason=edit_comment, noredirect=("noredirect" in options))
			except pywikibot.exceptions.Error as err:
				print(err)
Community content is available under CC-BY-SA unless otherwise noted.