def isRecreate(title):
	global site
	apiquery = {'action': 'query',
		'list': 'logevents',
		'letype': 'delete',
		'lelimit':'1',
		'letitle': title}
	response, data = site.postForm(site.apipath(), apiquery)
	if re.search('item', data):
		print title + ' is a recreate.'
		return True
	else:
		print title + ' is new.'
		return False
def addPages(titles):
	global site, noticeboard
	text = noticeboard.get(True)
	oldText = text
	added = set()
	for title in titles:
		if re.search(title, text) == None:
			text = text + '\n\n* {{Article|' + title + '}}'
			added.add(title)
		else:
			print title + ' is already present on noticeboard.'
	removed = set()
	for title in re.findall(r'\{\{[ ]*Article[ ]*\|[ ]*(.*?)[ ]*\}\}', text, re.I):
		page = wikipedia.Page(site, title)
		if not page.exists() or re.search(getConfigParameter('RemovalRegex'), page.get(), re.I):
			toRemove = re.search('\n\*[ ]*\{\{[ ]*Article[ ]*\|[ ]*' + title + '[ ]*\}\}.*?(\n|\Z)', text)
			text = text[:toRemove.start()] + text[toRemove.end():]
			removed.add(title)
	toRemove = set()
	for title in added:
		if title in removed:
			toRemove.add(title)
	for title in toRemove:
		added.remove(title)
		removed.remove(title)
	summary = getConfigParameter('BaseEditSummary')
	if added:
		summary = summary + 'added: '
		for title in added:
			summary = summary + '[[' + title + ']]; '
		summary = summary[:-2] + ', '
	if removed:
		summary = summary + 'removed: '
		for title in removed:
			summary = summary + '[[' + title + ']]; '
	summary = summary[:-2]
	if len(summary) > int(getConfigParameter('MaxSummaryLength')):
		summary = getConfigParameter('FallbackEditSummary')
	if added or removed:
		noticeboard.put(text, summary, None, False)
		print 'Saving changes.'
	else:
		print 'No changes made.'
def getConfigParameter(parameter):
	global configText
	return re.search('\n;[ ]*' + parameter + '[ ]*\'(.*?)\'[ ]*', configText).group(1)
import wikipedia, userlib, re
from time import sleep
site = wikipedia.getSite()
configPage = wikipedia.Page(site, 'User:DodoBot/Config')
configText = configPage.get()
noticeboard = wikipedia.Page(site, getConfigParameter('NoticeboardPage'))
seen = set()
counter = 0
while getConfigParameter('Run') == 'Yes':
	if (counter >= int(getConfigParameter('RecheckConfigEvery'))):
		configText = configPage.get(True)
		if getConfigParameter('NoticeboardPage') != noticeboard.title():
			noticeboard = wikipedia.Page(site, getConfigParameter('NoticeboardPage'))
		counter = 0
	seenThis = set()
	pagesToAdd = set()
	for pageInfo in site.newpages(int(getConfigParameter('PagesToLoad'))):
		title = pageInfo[0].title()
		username = pageInfo[4]
		seenThis.add(title)
		if title not in seen:
			print 'Checking ' + title
			seen.add(title)
			if isRecreate(title) and userlib.User(site, username).editCount() < getConfigParameter('MaximumEdits'):
				pagesToAdd.add(title)
	addPages(pagesToAdd)
	toRemove = set()
	for title in seen:
		if title not in seenThis:
			toRemove.add(title)
	for title in toRemove:
		seen.remove(title)
	counter += 1
	sleep(int(getConfigParameter('SleepTime')))