Protoball:Retitle Civil War Entries.py

From Protoball
Jump to navigation Jump to search
def retitle_civil_war_entries(chron_site):
	import mwclient
	site = chron_site
	cws = site.get_titles("[[Tags::Civil War]]")
	cws_with_suffix = site.get_titles("[[Tags::Civil War]][[Year Suffix::+]]")
	years = site.get_titles("[[Tags::Civil War]]|?Year#-|headers=hide|mainlabel=-")
	cwyear = {}
	for i, title in enumerate(cws):
		cwyear[int(title[3:])] = int(years[i])
	for year in range(1861, 1866):
		lasts = {'': 0, 's': 0, 'c': 0}
		chrons = site.get_titles("[[Year::{0}]]".format(year))
		for title in chrons:
			if title.startswith("CW"):
				continue
			number = int(title[title.find('.')+1:])
			suffix = 's' if 's.' in title else 'c' if 'c.' in title else ''
			lasts[suffix] = max(lasts[suffix], number)
		for cw_num, cw_year in cwyear.iteritems():
			if cw_year != year:
				continue
			title = "CW-{0}".format(cw_num)
			page = site.Pages[title]
			page_info = site.get_page_info(page)
			suffix = page_info.get("Year Suffix", "")
			moved = False
			while not moved:
				lasts[suffix] += 1
				new_title = "{0}{1}.{2}".format(year, suffix, lasts[suffix])
				print "moving {0} to {1}".format(title, new_title)
				try:
					page.move(new_title, "integrate civil war entries to chronology numbering")
				except mwclient.APIError:
					print new_title + " already used"
				else:
					moved = True
					new_page = site.Pages[new_title]
					text = new_page.edit()
					newpage.save(text)