Exemple #1
0
def content(form):
	data = {}

        no = int(form.getvalue('no', '-1'))
        if no < 0: return displaystate(form)

        mbox = mailbox.mbox('../../data/subster/mail_inbox')

        buf = []

        for i, message in enumerate(mbox):
            if (i != no): continue
            text = cgi.escape(message.as_string(True))
            text = text.replace("\n", "<br>\n")
            buf.append( text )
        buf.append( '\n' )

	data.update({   'oldlog':	"\n".join(buf),
			'refresh':	'',
			'title':	'DrTrigonBot subster mail queue',
			'tsnotice': 	style.print_tsnotice(),
			'p-status':	"<tr><td><td></tr>",
			'footer': 	style.footer + style.footer_w3c, # wiki (new) not CSS 2.1 compilant
	})
	data['content'] = displaystate_content % data

	return style.page % data
Exemple #2
0
def displaystate(form):
	data = {}

        mbox = mailbox.mbox(os.path.join(bot_path, '../data/subster/mail_inbox'))

        buf = []
        buf.append( '\n<table>' )
        buf.append( 
"""<tr>
<th>%s</th>
<th>%s</th>
<th>%s</th>
<th>%s</th>
</tr>""" % ('no.', 'sender', 'subject', 'timestmp') )

        for i, message in enumerate(mbox):
            sender   = message['from']          # Could possibly be None.
            subject  = message['subject']       # Could possibly be None.
            timestmp = message['date']       # Could possibly be None.

            if sender:
                # data found
                buf.append( a % (i, (b % (i, cgi.escape(sender))), subject, timestmp) )
        buf.append( '</table>\n\n' )

	data.update({   'oldlog':	"\n".join(buf),
			'refresh':	'',
			'title':	'DrTrigonBot subster mail queue',
			'tsnotice': 	style.print_tsnotice(),
			'p-status':	"<tr><td><td></tr>",
			'footer': 	style.footer + style.footer_w3c, # wiki (new) not CSS 2.1 compilant
	})
	data['content'] = displaystate_content % data

	return style.page % data
Exemple #3
0
def displayhtmlpage(form):
    titles = re.split('\|', form.getvalue('title', ''))
    sections = re.split('\|', form.getvalue('section', ''))
    chksum = re.split('\|', form.getvalue('chksum', ''))

    site = pywikibot.getSite()
    data = {'output': ''}

    #data['output'] += str(titles)
    #data['output'] += "<br>"
    #data['output'] += str(chksum)
    #data['output'] += "<br>"

    for i, title in enumerate(titles):
        try:
            section = sections[i]
        except:
            section = '0'

        # .../w/api.php?action=parse&page=%s&prop=sections&format=xml
        params = {
            u'action': u'parse',
            u'page': title,
            u'prop': u'sections',
        }
        try:
            test = query.GetData(params, site)[u'parse'][u'sections']
        except:
            # e.g. wrong params for page call ...
            data['output'] += str(query.GetData(params, site))
            data['output'] += "<br>"
            break
        test = dict([(item[u'line'], item[u'number']) for item in test])
        if section in test: section = test[section]

        # .../w/api.php?action=query&prop=revisions&titles=%s&rvprop=timestamp|user|comment|content&rvsection=%s&format=xml
        params = {
            u'action': u'query',
            u'prop': u'revisions',
            u'titles': title,
            u'rvprop': u'timestamp|user|comment|content',
            u'rvsection': section,
        }
        try:
            test = query.GetData(params, site)[u'query'][u'pages']
        except:
            # e.g. wrong params for page call ...
            data['output'] += str(query.GetData(params, site))
            data['output'] += "<br>"
            break
        test = test[test.keys()[0]][u'revisions'][0]
        dh_chars = test[u'*']
        m = re.search('^(=+)(.*?)(=+)(?=\s)', dh_chars, re.M)
        if m: section_name = m.groups()[1].strip()
        else: section_name = "?"

        data['output'] += "Page: <i>%s</i> / Section: <i>%s (%s)</i>" % (
            title, section_name.encode(textfile_encoding), section)
        data['output'] += "<br>"
        data['output'] += "Old checksum: %s" % chksum[i]
        data['output'] += "<br>"

        new_chksum = hashlib.md5(dh_chars.encode('utf8').strip()).hexdigest()
        data['output'] += "New checksum: %s" % new_chksum
        data['output'] += "<br>"
        data['output'] += "<b>Section changed: %s</b>" % (not (chksum[i]
                                                               == new_chksum))

        #data['output'] += "<br>"
        #data['output'] += "<div style=\"padding:20px; border:thin solid gray; margin:25px\">"
        #data['output'] += "<small>"
        ##data['output'] += re.sub('\n','<br>',text).encode('utf8')
        #data['output'] += text.encode('utf8')
        #data['output'] += "</small>"
        #data['output'] += "</div>"

        data['output'] += "<br>"

    data.update({
        'title': 'DrTrigonBot checksum checker',
        'refresh': '',
        'tsnotice': style.print_tsnotice(),
        'p-status': '<tr><td></td></tr>',
        'footer':
        style.footer + style.footer_w3c,  # wiki (new) not CSS 2.1 compilant
    })
    data['content'] = displaystate_content % data

    return style.page % data
Exemple #4
0
def maindisplay():
    param_default = subster.bot_config['param_default']
    param_default.update(sim_param_default)
    params = {}
    for key in param_default.keys():
        params[key] = form.getvalue(key, param_default[key])

    # enhance with add. params
    try:
        params.update(eval(params['add_params']))
    except:
        pass

    bot_output = ["(no simulation runned yet...)"
                  ]  # index 0 is bot_output, all other are errors
    if (params['action'] == 'runsim'):
        # redirect stdout and stderr
        stdlog = StringIO.StringIO()
        (out_stream, err_stream) = (sys.stdout, sys.stderr)
        (sys.stdout, sys.stderr) = (stdlog, stdlog)
        pywikibot.ui.stdout = sys.stdout  # patch needed for pywikibot.output
        pywikibot.ui.stderr = sys.stderr  # (look at terminal_iterface_base.py and bot_control.py)

        # Set the signal handler and a ?-second alarm (request max. timeout)
        signal.signal(signal.SIGALRM, timeout_handler)
        signal.alarm(timeout)

        try:
            params['content'] = subster.SubsterBot().run(sim=params)
        except:
            #params['content'] = "ERROR OCCURRED DURING BOT SIMULATION"
            bot_output.append(gettraceback(sys.exc_info())[2])

        # Cancel the signal handler alarm
        signal.alarm(0)

        # restore stdout and stderr
        (sys.stdout, sys.stderr) = (out_stream, err_stream)
        bot_output[0] = re.sub('\x03', '', stdlog.getvalue())
        stdlog.close()

    bot_output = re.sub("\n{1,2}", "<br>\n", "\n".join(bot_output))

    data = {
        'panel_ver': __version__,
        'subster_bot_ver': subster.__version__,
        'bot_output': bot_output,
    }

    if type(params['content']) == type(u""):
        params['content'] = params['content'].encode(config.textfile_encoding)
        #params['content'] = params['content'].encode(config.textfile_encoding).decode("ISO-8859-1")

    # proper output format and prevent XSS vulnerabilities
    for key in params:
        if (type(params[key]) == type('')) or (type(params[key]) == type(u'')):
            params[key] = cgi.escape(params[key], quote=True)

    data.update(params)

    data.update({
        'refresh': '',
        'title': 'DrTrigonBot subster simulation panel',
        'tsnotice': style.print_tsnotice(),
        #'content':	displaystate_content,
        'p-status': "<tr><td></td></tr>",
        #'footer': 	style.footer + style.footer_w3c + style.footer_w3c_css,
        'footer':
        style.footer + style.footer_w3c,  # wiki (new) not CSS 2.1 compilant
    })
    data['content'] = maindisplay_content % data

    return style.page % data
Exemple #5
0
def displayhtmlpage(form):
	cat    = form.getvalue('cat', '')
#	start  = form.getvalue('start', datetime.datetime.utcnow().strftime(wikitime))
	start  = form.getvalue('start', pywikibot.Timestamp.utcnow().strftime(wikitime))
	period = form.getvalue('period', '24')

	lang = locale.locale_alias.get(wiki, locale.locale_alias['en']).split('.')[0]
	try:
		locale.setlocale(locale.LC_TIME, lang + '.utf8')
	except locale.Error:
		locale.setlocale(locale.LC_TIME, lang)

	#scheme = {'NO': 'http', 'YES': 'https'}[os.environ.get('HTTP_X_TS_SSL', 'NO')]

	s = datetime.datetime.strptime(start, wikitime)
	p = datetime.timedelta(hours=int(period))
	end = s - p

	data = {'output': ''}

	data['output'] += "<b>Start</b>: %s (%s)<br>\n" % (start, strftime(asctime_fmt, strptime(start, wikitime)))
	data['output'] += "<b>End</b>: %s (%s)<br>\n" % (end.strftime(wikitime), end.strftime(asctime_fmt))
	data['output'] += "<b>Period</b>: %sh<br>\n" % period

	end = long( end.strftime(wikitime) )

	data['output'] += "<br>\n"

#	talk_ns = (1, 3, 5, 9, 11, 13, 15, 91, 93, 101, 109)
#	res += str( read_db(_SQL_query_page_info, ('%DrTrigon',str(talk_ns),10)) )

#	res += str( read_db(_SQL_query_02, (cat,)) )
#	res += "<br>\n"

	if cat:
		tic = time()
		#out = checkRecentEdits_API(cat, end)
		#out = checkRecentEdits_db_rec(cat, end)                    # uses 'page_touched'
		out = checkRecentEdits_db_it(cat, end, str(int(period)+1))  # uses 'rc_timestamp'
		out_size = len(out)
		toc = time()
		if out:
			# sort items/entries by timestamp
			# (somehow cheap algor. - use SQL directly for sorting)
			out_dict = dict( [(item[0][2]+item[0][0]+str(item[0][1]), item) for item in out] )
			keys = out_dict.keys()
			keys.sort()
			out = [out_dict[key] for key in keys]

			data['output'] += "<table>\n"

			for subrow in out:
				data['output'] += "<tr>\n  <td>"
				title = subrow[0][0]
				title = site.namespace(subrow[0][1]).encode('utf8') + ':' + title
				data['output'] += '<a href="//%s.wikipedia.org/wiki/%s" target="_blank">%s</a>' % (wiki, title, title.replace('_', ' '))
				data['output'] += "</td>\n  <td>"
#				data['output'] += str(subrow[0][1:])
				try:
					tmsp = strftime(asctime_fmt, strptime(subrow[0][2], wikitime)) + " (UTC)"
				except ValueError:
					tmsp = subrow[0][2] + " (!)"
				data['output'] += tmsp
				data['output'] += "</td>\n  <td>"
				data['output'] += '<a href="//%s.wikipedia.org/wiki/User:%s" target="_blank">%s</a>' % (wiki, subrow[0][3], subrow[0][3])
				data['output'] += "</td>\n  <td>"
				data['output'] += str(subrow[0][4])
				data['output'] += "</td>\n</tr>\n"

			data['output'] += "</table>\n"
                else:
                        data['output'] += "<i>No pages matching criteria found.</i><br>\n"
		data['output'] += "<br>\n"
                data['output'] += "Time to process %i results: %fs\n" % (out_size, (toc-tic))
	else:
		data['output'] += "<i>No category (cat) defined!</i><br>\n"

	data['output'] += "<br>\n"

	data['output'] += '<small>%s</small><br>\n' % str( get_wikiinfo_db(wiki) )

	data.update({	'title':	'DrTrigonBot category discussion summary',
			'refresh':	'',
			'tsnotice': 	style.print_tsnotice(),
			'p-status':	'<tr><td><small><a href="http://status.toolserver.org/" target="_blank">DB status</a></small></td></tr>',
			#'p-status':	'<tr><td><small><a href="//status.toolserver.org/" target="_blank">DB status</a></small></td></tr>',
			'footer': 	style.footer + style.footer_w3c, # wiki (new) not CSS 2.1 compilant
			'wiki':		wiki,
			'cat':		cgi.escape(   cat, quote=True), # proper XSS secure output
			'start':	cgi.escape( start, quote=True), #
			'period':	cgi.escape(period, quote=True), #
	})
	data['content'] = displaystate_content % data

	return style.page % data
def logstat(form):
	import matplotlib
	matplotlib.rc('font', size=8)
	import matplotlib.pyplot as plt
	# http://matplotlib.sourceforge.net/api/dates_api.html?highlight=year%20out%20range#matplotlib.dates.epoch2num
	from matplotlib.dates import MonthLocator, DayLocator, DateFormatter, epoch2num

#	import platform, rrdtool	# for statistics '-stats'

	import numpy

	data = {'messages':		'', }

	format = form.getvalue('format', 'html')

	# filter: '' (all), 'default', 'subster', 'subster_irc', 'catimages'
	# (similar to the one used in adminlogs)
	current = ['ALL', 'default', 'subster', 'catimages',]# 'subster_irc', 'script_wui']
	filt = form.getvalue('filter', current[0])
	filt = ('' if filt == 'ALL' else filt)
	data['filter_options'] = "<option>%s</option>" % ("</option><option>".join(current))
	data['filter_options'] = data['filter_options'].replace("<option>%s</option>" % filt,
	                                                        "<option selected>%s</option>" % filt)

	(localdir, files, log) = oldlogfiles(all=True)

	stat = {}
	for date, item in files:
		logfiles = [f for f in item if filt in f]
		#if not logfiles:
		#	continue
		s, recent = logging_statistics(logfiles, botcontinuous)
		if s is None:	# includes 'if not logfiles'
			continue
		last = date	# a little bit hacky but needed for plot below
		stat[date] = s

	d = {'mcount': [], 'ecount': [], 'uptimes': []}
	keys = stat.keys()
	keys.sort()
	data['start_date'] = str(strftime("%a %b %d %Y", localtime( stat[keys[0]]['mainstart'] )))
	data['end_date']   = str(strftime("%a %b %d %Y", localtime( stat[keys[-1]]['mainend'] )))
	dkeys = dict([ (k, i) for item in keys for i, k in enumerate(stat[item]['ecount'].keys()) ])
	d['ecount'] = numpy.zeros((len(keys),len(dkeys)+1))
	for i, item in enumerate(keys):
		t = mktime(strptime(item.split('.')[-1], datefmt))

		d['mcount'].append( [t] + stat[item]['mcount'].values() )

		d['ecount'][i,0] = t
		for kj in dkeys:
			d['ecount'][i,(dkeys[kj]+1)] = stat[item]['ecount'].get(kj, 0)

		data['messages'] += "<b>%s</b>\n<i>used resources: %s</i>\n" % (item, stat[item]['resources'])
		data['messages'] += "\n".join(stat[item]['messages']) + ("\n"*2)

		end   = numpy.array(stat[item]['etiming']['end'])
		start = numpy.array(stat[item]['etiming']['start'])
		if end.shape == start.shape:
			runtime = end-start-7200			# -2*60*60 because of time jump during 'set TZ'
			if runtime.any() and (runtime.min() < 0):	# DST or not; e.g. ('CET', 'CEST')
				runtime += 3600
			d['uptimes'].append( list(runtime) )
		else:
			d['uptimes'].append( '-' )

	d['mcount'] = numpy.array(d['mcount'])
	#d['ecount'] = numpy.array(d['ecount'])
	d['mcount'][:,0] = epoch2num(d['mcount'][:,0])
	d['ecount'][:,0] = epoch2num(d['ecount'][:,0])
	keys = dkeys.keys()
	ks, ke = keys.index('start')+1, keys.index('end')+1

        plotlink = os.path.basename(sys.argv[0]) + (r"?action=logstat&amp;filter=%s" % filt)
	data.update({
		'run_diff':		"</td><td>".join( map(str, d['ecount'][:,ks]-d['ecount'][:,ke]) ),
		'start_count':		"</td><td>".join( map(str, d['ecount'][:,ks]) ),
		'end_count':		"</td><td>".join( map(str, d['ecount'][:,ke]) ),
		'uptimes':		"</td><td>".join( map(str, d['uptimes']) ),
		'graphlink-mcount':	plotlink + r"&amp;format=graph-mcount",
		'graphlink-mcount-i':	plotlink + r"&amp;format=graph-mcount-i",
		'graphlink-ecount':	plotlink + r"&amp;format=graph-ecount",
		'graphlink-ecount-sed':	plotlink + r"&amp;format=graph-ecount-sed",
		'backlink':		os.path.basename(sys.argv[0]),
	})

	# plot graphs output
	if   (format == 'graph-mcount'):
		d = d['mcount']
		fig = plt.figure(figsize=(5,3))
		#ax = fig.add_subplot(111)
		ax_size = [0.125, 0.15, 
			1-0.1-0.05, 1-0.15-0.05]
		ax = fig.add_axes(ax_size)
		#plot1 = ax.bar(range(len(xdata)), xdata)
		#p1 = ax.plot(d[:,0], d[:,1])	# 'info'
		p2 = ax.step(d[:,0], d[:,2], marker='x', where='mid')
		p3 = ax.step(d[:,0], d[:,3], marker='x', where='mid')
		p4 = ax.step(d[:,0], d[:,4], marker='x', where='mid')
		p5 = ax.step(d[:,0], d[:,5], marker='x', where='mid')
		p6 = ax.step(d[:,0], d[:,6], marker='x', where='mid')
		plt.legend([p2, p3, p4, p5, p6], stat[last]['mcount'].keys()[1:], loc='upper left', prop={'size':8})
		plt.grid(True, which='both')
		# format the ticks
		ax.xaxis.set_major_locator(MonthLocator())
		ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
		ax.xaxis.set_minor_locator(DayLocator())
		ax.autoscale_view()
		# format the coords message box
		ax.fmt_xdata = DateFormatter('%Y-%m-%d')
		# format axis
		fig.autofmt_xdate()
		# show plot
		return show_onwebpage(plt)
	elif (format == 'graph-mcount-i'):
		d = d['mcount']
		fig = plt.figure(figsize=(5,3))
		#ax = fig.add_subplot(111)
		ax_size = [0.125, 0.15, 
			1-0.1-0.05, 1-0.15-0.05]
		ax = fig.add_axes(ax_size)
		p1 = ax.step(d[:,0], d[:,1], marker='x', where='mid')
		# legend
		plt.legend([p1], stat[last]['mcount'].keys(), loc='upper left', prop={'size':8})
		# grid
		plt.grid(True, which='both')
		# format the ticks
		ax.xaxis.set_major_locator(MonthLocator())
		ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
		ax.xaxis.set_minor_locator(DayLocator())
		ax.autoscale_view()
		# format the coords message box
		ax.fmt_xdata = DateFormatter('%Y-%m-%d')
		# format axis
		fig.autofmt_xdate()
		# show plot
		return show_onwebpage(plt)
	elif (format == 'graph-ecount'):
		d = d['ecount']
		fig = plt.figure(figsize=(5,3))
		#ax = fig.add_subplot(111)
		ax_size = [0.125, 0.15, 
			1-0.1-0.05, 1-0.15-0.05]
		ax = fig.add_axes(ax_size)
		pa = []
		for i in range(1, d.shape[1]):
			pa.append( ax.step(d[:,0], d[:,i], where='mid') )#, marker='x') )
		plt.legend(pa, stat[last]['ecount'].keys(), loc='upper left', bbox_to_anchor=[-0.15, 1.0], ncol=2, prop={'size':8})
		plt.grid(True, which='both')
		#plt.ylim(ymax=10)
		# format the ticks
		ax.xaxis.set_major_locator(MonthLocator())
		ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
		ax.xaxis.set_minor_locator(DayLocator())
		ax.autoscale_view()
		# format the coords message box
		ax.fmt_xdata = DateFormatter('%Y-%m-%d')
		# format axis
		fig.autofmt_xdate()
		# show plot
		return show_onwebpage(plt)
	elif (format == 'graph-ecount-sed'):
		d = d['ecount']
		fig = plt.figure(figsize=(5,3))
		#ax = fig.add_subplot(111)
		ax_size = [0.125, 0.15, 
			1-0.1-0.05, 1-0.15-0.05]
		ax = fig.add_axes(ax_size)
		p1 = ax.step(d[:,0], (d[:,ks]-d[:,ke]), marker='x', where='mid')
		p2 = ax.step(d[:,0], d[:,ke], marker='x', where='mid')
		plt.legend([p1, p2], ['runs failed', 'successful'], prop={'size':8})
		plt.grid(True, which='both')
		# format the ticks
		ax.xaxis.set_major_locator(MonthLocator())
		ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
		ax.xaxis.set_minor_locator(DayLocator())
		ax.autoscale_view()
		# format the coords message box
		ax.fmt_xdata = DateFormatter('%Y-%m-%d')
		# format axis
		fig.autofmt_xdate()
		# show plot
		return show_onwebpage(plt)

	data.update({	'refresh':	'',
			'title':	'DrTrigonBot log statistics',
			'tsnotice': 	style.print_tsnotice(),
			#'content':	logstat_content,
			'p-status':	'<tr><td></td></tr>',
			'footer': 	style.footer + style.footer_w3c,
	})
	data['content'] = logstat_content % data

	# default output
	return style.page % data
def displaystate(form):
	data = {}

	(localdir, files, current) = oldlogfiles()
	files = [item for key, value in files for item in value]	# flatten

	# < prev | next > browsing
	date = form.getvalue('date', None)
	if date:
		current = [item for item in files if date in item]
		today = datetime.datetime.strptime(date, '%Y-%m-%d')
	else:
		today = datetime.datetime.today()
	yesterday = (today - datetime.timedelta(days=1)).strftime('%Y-%m-%d')
	tomorrow  = (today + datetime.timedelta(days=1)).strftime('%Y-%m-%d')
	data['logbrowse'] = "<a href='?date=%s'>< prev</a> | <a href='?'>now</a>" % yesterday
	if (datetime.datetime.today() - today) >= datetime.timedelta(days=2):
		data['logbrowse'] += " | <a href='?date=%s'>next ></a><br><br>" % tomorrow
	data['logbrowse'] += "<br><br>"
	today = mktime(today.timetuple())

	stat, recent = logging_statistics(current, botcontinuous)
	if stat is None:
		data['botlog']      = 'n/a'
		data['messages']    = 'n/a'
		data['successfull'] = "n/a"
	else:
		data['botlog']      = stat['lastmessage']
		data['messages']    = "\n".join(stat['messages'])
		data['successfull'] = "%s of %s" % (stat['ecount']['end'], stat['ecount']['start'])
	lastrun = max([os.stat(os.path.join(localdir, item)).st_mtime for item in current]+[0])
	botmsg = data['botlog'].strip()

	data['botstate_daily'] = botstate_img['red']
	color = html_color['red']
	state_text = "n/a"
	if lastrun and ((today-lastrun) <= (bottimeout*60*60)):
		if   (botmsg == botdonemsg) and not (stat['ecount']['end'] - stat['ecount']['start']):
			data['botstate_daily'] = botstate_img['green']
			color = html_color['green']
			state_text = "OK"
		elif (botmsg.find(botdonemsg) == 0):
			data['botstate_daily'] = botstate_img['orange']
			color = html_color['orange']
			state_text = "problem"
		else:
			data['botstate_daily'] = botstate_img['orange']
			color = html_color['orange']

	users = irc_status()

	data['botstate_subster'] = botstate_img['red']
	if ("DrTrigonBot" in users) or (":DrTrigonBot" in users):
		data['botstate_subster'] = botstate_img['green']
		irc_subster_color = html_color['green']
		irc_subster_state_text = "OK"
	else:
		data['botstate_subster'] = botstate_img['orange']
		irc_subster_color = html_color['orange']
		irc_subster_state_text = "problem"

	data['botstate_wui'] = botstate_img['red']
	if ("DrTrigonBot_WUI" in users) or (":DrTrigonBot_WUI" in users):
		data['botstate_wui'] = botstate_img['green']
		irc_wui_color = html_color['green']
		irc_wui_state_text = "OK"
	else:
		data['botstate_wui'] = botstate_img['orange']
		irc_wui_color = html_color['orange']
		irc_wui_state_text = "problem"

	status  = "<tr style='background-color: %(color)s'><td>%(bot)s</td><td>%(state)s</td></tr>\n" % {'color': color, 'bot': 'regular:', 'state': state_text}
	status += "<tr><td></td><td></td></tr>\n"    # separator (cheap)
	status += "<tr style='background-color: %(color)s'><td>%(bot)s</td><td>%(state)s</td></tr>\n" % {'color': irc_subster_color, 'bot': 'subster:', 'state': irc_subster_state_text}
	status += "<tr style='background-color: %(color)s'><td>%(bot)s</td><td>%(state)s</td></tr>\n" % {'color': irc_wui_color, 'bot': 'wui:', 'state': irc_wui_state_text}

	# ('replace' below is a lapbs patch)
	data['currentlog'] = []
	for item in current:
		s, r = logging_statistics([item], [])#botcontinuous)
		if not s:
			s = {'lastmessage': 'n/a', 'ecount': {'end': 'n/a', 'start': 'n/a'}}
		logfile = os.path.join(localdir, item)
		lasttime = os.stat(logfile).st_mtime
		logstate = botstate_img['red']
		if (today-lasttime) <= (bottimeout*60*60):
			if   (s['lastmessage'] == botdonemsg) and not (s['ecount']['end'] - s['ecount']['start']):
				logstate = botstate_img['green']
			elif (botmsg.find(botdonemsg) == 0):
				logstate = botstate_img['orange']
			else:
				logstate = botstate_img['orange']
		data['currentlog'].append( '<tr%s><td><a href="%s">%s</a></td><td>%s</td><td>%s of %s</td><td>%s</td><td><img src="%s" width="15" height="15" alt=""></td></tr>' %
                                       (' bgcolor="#CCCCCC"' if item == recent else '', 
                                        logfile.replace('public_html/', ''), item,
                                        s['lastmessage'], 
                                        s['ecount']['end'], s['ecount']['start'],
                                        asctime(localtime(lasttime)),
                                        logstate) )
	data['currentlog'].append( '<tr%s><td><b>%s</b></td><td>%s</td><td>%s of %s</td><td></td><td><img src="%s" width="15" height="15" alt=""></td></tr>' %
                                  (' style="font-weight:bold;"', 
                                   'Summary / Total',
                                   stat['lastmessage'], 
                                   stat['ecount']['end'], stat['ecount']['start'],
                                   data['botstate_daily']) )
	data['currentlog'] = "\n".join(data['currentlog'])

	data.update({	'time':		asctime(localtime(time())),
			'oldlog':	", ".join(files),
			'oldlink':	links['old'][style.host(os.environ)],
			'logstat':	os.path.basename(sys.argv[0]) + r"?action=logstat",
			'logstatraw':	os.path.basename(sys.argv[0]) + r"?action=logstat&amp;format=plain",
			'refresh':	'15',
			'title':	'DrTrigonBot status panel',
			'tsnotice': 	style.print_tsnotice(),
			'p-status':	status,
			'footer': 	style.footer + style.footer_w3c, # wiki (new) not CSS 2.1 compilant
	})
	data['content'] = displaystate_content % data

	return style.page % data
def maindisplay():
	param_default = subster.bot_config['param_default']
	param_default.update(sim_param_default)
	params = {}
	for key in param_default.keys():
		params[key] = form.getvalue(key, param_default[key])

	# enhance with add. params
	try:	params.update( eval(params['add_params']) )
	except:	pass

	bot_output = ["(no simulation runned yet...)"]		# index 0 is bot_output, all other are errors
	if (params['action'] == 'runsim'):
		# redirect stdout and stderr
		stdlog = StringIO.StringIO()
		(out_stream, err_stream) = (sys.stdout, sys.stderr)
		(sys.stdout, sys.stderr) = (stdlog, stdlog)
		pywikibot.ui.stdout = sys.stdout    # patch needed for pywikibot.output
		pywikibot.ui.stderr = sys.stderr    # (look at terminal_iterface_base.py and bot_control.py)

		# Set the signal handler and a ?-second alarm (request max. timeout)
		signal.signal(signal.SIGALRM, timeout_handler)
		signal.alarm(timeout)

		try:
			params['content'] = subster.SubsterBot().run(sim=params)
		except:
			#params['content'] = "ERROR OCCURRED DURING BOT SIMULATION"
			bot_output.append(gettraceback(sys.exc_info())[2])

		# Cancel the signal handler alarm
		signal.alarm(0)

		# restore stdout and stderr
		(sys.stdout, sys.stderr) = (out_stream, err_stream)
		bot_output[0] = re.sub('\x03', '', stdlog.getvalue())
		stdlog.close()

	bot_output = re.sub("\n{1,2}", "<br>\n", "\n".join(bot_output))

	data = {'panel_ver':		__version__,
		'subster_bot_ver':	subster.__version__,
		'bot_output':		bot_output,
	}

	if type(params['content']) == type(u""):
		params['content'] = params['content'].encode(config.textfile_encoding)
		#params['content'] = params['content'].encode(config.textfile_encoding).decode("ISO-8859-1")

	# proper output format and prevent XSS vulnerabilities
	for key in params:
		if (type(params[key]) == type('')) or (type(params[key]) == type(u'')):
			params[key] = cgi.escape(params[key], quote=True)

	data.update( params )

	data.update({	'refresh':	'',
			'title':	'DrTrigonBot subster simulation panel',
			'tsnotice': 	style.print_tsnotice(),
			#'content':	displaystate_content,
			'p-status':	"<tr><td></td></tr>",
			#'footer': 	style.footer + style.footer_w3c + style.footer_w3c_css,
			'footer': 	style.footer + style.footer_w3c, # wiki (new) not CSS 2.1 compilant
	})
	data['content'] = maindisplay_content % data

	return style.page % data
Exemple #9
0
def displayhtmlpage(form):
	site = pywikibot.getSite()
	data = {'output': ''}

	wikis = wikilist
	param_wiki = form.getvalue('wiki', None)
	if param_wiki:
		wikis = param_wiki.split("|")

	for wiki in wikis:
		data['output'] += "<b>%s</b><br>\n" % wiki

		# .../w/api.php?action=help&format=xml
		params = {
			u'action'	: u'help',
		}
		test = query.GetData(params, site)[u'error'][u'*']
		test = re.split('\n', test)[29]
		full_actions = re.split('[:,]\s', test)[1:]
		actions = [item for item in full_actions if item not in blacklist]

		# .../w/api.php?action=paraminfo&modules=%s&format=xml
		params = {
			u'action'	: u'paraminfo',
			u'modules'	: u'|'.join(actions),
		}
		test = query.GetData(params, site)[u'paraminfo'][u'modules']
		test = [t.get(u'version', u'> PROBLEM <') for t in test]
		#test = str(test)
		#test = "<br>\n".join(test)
		#test = str(re.search('\.php (\d*?) ', test[0]).groups())
		test = ["<tr>\n  <td>%s</td>\n</tr>\n" % item for item in test]
		test = "".join(test)

		data['output'] += "<table>\n"
		data['output'] += "<tr>\n  <td>"
		data['output'] += "actions:"
		data['output'] += "</td>\n  <td>"
		data['output'] += str(full_actions)
		data['output'] += "</td>\n</tr>\n"
		data['output'] += "<tr>\n  <td>"
		data['output'] += "blacklist:"
		data['output'] += "</td>\n  <td>"
		data['output'] += str(blacklist)
		data['output'] += "</td>\n</tr>\n"
		data['output'] += "</table>\n"

		data['output'] += "<br>\n"

		data['output'] += "<table>\n"
		data['output'] += test
		data['output'] += "</table>\n"

		data['output'] += "<br>\n"

	data.update({	'title':	'Global Wiki API Version',
			'refresh':	'',
			'tsnotice': 	style.print_tsnotice(),
			'p-status':	'<tr><td></td></tr>',
			'footer': 	style.footer + style.footer_w3c, # wiki (new) not CSS 2.1 compilant
	})
	data['content'] = displaystate_content % data

	return style.page % data
def displayhtmlpage(form):
	titles   = re.split('\|', form.getvalue('title', ''))
	sections = re.split('\|', form.getvalue('section', ''))
	chksum   = re.split('\|', form.getvalue('chksum', ''))

        site = pywikibot.getSite()
        data = {'output': ''}

	#data['output'] += str(titles)
	#data['output'] += "<br>"
	#data['output'] += str(chksum)
	#data['output'] += "<br>"

	for i, title in enumerate(titles):
		try:	section = sections[i]
		except:	section = '0'

		# .../w/api.php?action=parse&page=%s&prop=sections&format=xml
		params = {
			u'action'       : u'parse',
			u'page'		: title,
			u'prop'		: u'sections',
		}
		try:
			test = query.GetData(params, site)[u'parse'][u'sections']
		except:
			# e.g. wrong params for page call ...
			data['output'] += str(query.GetData(params, site))
			data['output'] += "<br>"
			break
		test = dict( [(item[u'line'], item[u'number']) for item in test] )
		if section in test: section = test[section]

		# .../w/api.php?action=query&prop=revisions&titles=%s&rvprop=timestamp|user|comment|content&rvsection=%s&format=xml
		params = {
			u'action'       : u'query',
			u'prop'		: u'revisions',
			u'titles'	: title,
			u'rvprop'	: u'timestamp|user|comment|content',
			u'rvsection'	: section,
		}
		try:
			test = query.GetData(params, site)[u'query'][u'pages']
		except:
			# e.g. wrong params for page call ...
			data['output'] += str(query.GetData(params, site))
			data['output'] += "<br>"
			break
		test = test[test.keys()[0]][u'revisions'][0]
		dh_chars = test[u'*']
		m = re.search('^(=+)(.*?)(=+)(?=\s)', dh_chars, re.M)
		if m:	section_name = m.groups()[1].strip()
		else:	section_name = "?"

		data['output'] += "Page: <i>%s</i> / Section: <i>%s (%s)</i>" % (title, section_name.encode(textfile_encoding), section)
		data['output'] += "<br>"
		data['output'] += "Old checksum: %s" % chksum[i]
		data['output'] += "<br>"

		new_chksum = hashlib.md5(dh_chars.encode('utf8').strip()).hexdigest()
		data['output'] += "New checksum: %s" % new_chksum
		data['output'] += "<br>"
		data['output'] += "<b>Section changed: %s</b>" % (not (chksum[i] == new_chksum))

		#data['output'] += "<br>"
		#data['output'] += "<div style=\"padding:20px; border:thin solid gray; margin:25px\">"
		#data['output'] += "<small>"
		##data['output'] += re.sub('\n','<br>',text).encode('utf8')
		#data['output'] += text.encode('utf8')
		#data['output'] += "</small>"
		#data['output'] += "</div>"

		data['output'] += "<br>"

        data.update({   'title':        'DrTrigonBot checksum checker',
                        'refresh':      '',
                        'tsnotice':     style.print_tsnotice(),
                        'p-status':     '<tr><td></td></tr>',
                        'footer':       style.footer + style.footer_w3c, # wiki (new) not CSS 2.1 compilant
        })
        data['content'] = displaystate_content % data

        return style.page % data