import pywikibot
site = pywikibot.Site()
DEBUG = False
SUMMARY = "Replacing 'remake' and 'rebirth' parameters in {{infobox field}} with classcodes (Catuse)"
TEMPLATE = "infobox field"
manual = True
for page in pywikibot.Page(site, TEMPLATE, ns=10).getReferences(only_template_inclusion=True):
if 'rebirth' not in page.text and 'remake' not in page.text:
continue
title = page.title()
edited = False
if DEBUG:
print("Reading page", title)
tmp_stack = []
classcodes = []
for i in range(0, len(page.text)):
if page.text[i:i+2:1] == '{{':
# At the start of a new template
i = i + 2 # skip the next {
# Determine what template we're in
tmp_name = ''
while not (page.text[i] == '|' or page.text[i] == '\n' or page.text[i:i+2:1] == '<!' or page.text[i:i+2:1] == '}}'):
tmp_name = tmp_name + page.text[i]
i = i + 1
tmp_stack.append(tmp_name.lower())
if DEBUG and TEMPLATE in tmp_stack:
print("Current template stack:", str(tmp_stack))
elif len(tmp_stack) > 0 and page.text[i:i+2:1] == '}}':
# At the end of a template
if len(tmp_stack) > 0 and tmp_stack[-1] == TEMPLATE:
for j in range(len(classcodes)):
code = classcodes[j]
page.text = page.text[0:i] + '|version game ' + str(j + 1) + ' = ' + code + '\n}}' + page.text[i+2:]
i = i + 20 + len(code)
edited = True
tmp_stack.pop()
elif len(tmp_stack) > 0 and tmp_stack[-1] == TEMPLATE:
if page.text[i:i+6:1] == 'remake':
page.text = page.text[0:i] + 'version ' + str(len(classcodes) + 1) + page.text[i+6:]
i = i + 5
classcodes.append('FFVIIR')
edited = True
if page.text[i:i+7:1] == 'rebirth':
page.text = page.text[0:i] + 'version ' + str(len(classcodes) + 1) + page.text[i+7:]
i = i + 5
classcodes.append('FFVIIR2')
edited = True
if not edited:
continue
if DEBUG:
print(page.text)
break
if manual:
print('-- ' + title + ' --')
print(page.text)
t = ''
while not (t in ['a', 'y', 'n', 'q']):
print("What to do? a: Automatic, y: Accept this edit only, n: Skip this edit, q: Quit")
t = input(">\t")
if t == 'a':
manual = False
if t in ['a', 'y']:
page.save(SUMMARY)
if t == 'q':
break
else:
print("Edited", title)
page.save(SUMMARY)