/usr/share/pyshared/MoinMoin/stats/useragents.py is in python-moinmoin 1.9.3-1ubuntu2.3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | # -*- coding: iso-8859-1 -*-
"""
MoinMoin - User-Agent Statistics
This macro creates a pie chart of the type of user agents
accessing the wiki.
TODO: should be refactored after hitcounts.
@copyright: 2002-2004 Juergen Hermann <jh@web.de>,
2007 MoinMoin:ThomasWaldmann
@license: GNU GPL, see COPYING for details.
"""
_debug = 0
from MoinMoin import wikiutil, caching, logfile
from MoinMoin.Page import Page
from MoinMoin.logfile import eventlog
def linkto(pagename, request, params=''):
_ = request.getText
if not request.cfg.chart_options:
return text(pagename, request)
if _debug:
return draw(pagename, request)
page = Page(request, pagename)
# Create escaped query string from dict and params
querystr = {'action': 'chart', 'type': 'useragents'}
querystr = wikiutil.makeQueryString(querystr)
querystr = wikiutil.escape(querystr)
if params:
querystr += '&' + params
data = {'url': page.url(request, querystr)}
data.update(request.cfg.chart_options)
result = ('<img src="%(url)s" width="%(width)d" height="%(height)d"'
' alt="useragents chart">') % data
return result
def get_data(request):
# get results from cache
cache = caching.CacheEntry(request, 'charts', 'useragents', scope='wiki', use_pickle=True)
cache_date, data = 0, {}
if cache.exists():
try:
cache_date, data = cache.content()
except:
cache.remove() # cache gone bad
log = eventlog.EventLog(request)
try:
new_date = log.date()
except logfile.LogMissing:
new_date = None
if new_date is not None:
log.set_filter(['VIEWPAGE', 'SAVEPAGE'])
for event in log.reverse():
if event[0] <= cache_date:
break
ua = event[2].get('HTTP_USER_AGENT')
if ua:
try:
pos = ua.index(" (compatible; ")
ua = ua[pos:].split(';')[1].strip()
except ValueError:
ua = ua.split()[0]
#ua = ua.replace(';', '\n')
data[ua] = data.get(ua, 0) + 1
# write results to cache
cache.update((new_date, data))
data = [(cnt, ua) for ua, cnt in data.items()]
data.sort()
data.reverse()
return data
def text(pagename, request):
from MoinMoin.util.dataset import TupleDataset, Column
from MoinMoin.widget.browser import DataBrowserWidget
_ = request.getText
data = get_data(request)
total = 0.0
for cnt, ua in data:
total += cnt
agents = TupleDataset()
agents.columns = [Column('agent', label=_("User agent"), align='left'),
Column('value', label='%', align='right')]
cnt_printed = 0
data = data[:10]
if total:
for cnt, ua in data:
try:
ua = unicode(ua)
agents.addRow((ua, "%.2f" % (100.0 * cnt / total)))
cnt_printed += cnt
except UnicodeError:
pass
if total > cnt_printed:
agents.addRow((_('Others'), "%.2f" % (100 * (total - cnt_printed) / total)))
table = DataBrowserWidget(request)
table.setData(agents)
return table.render(method="GET")
def draw(pagename, request):
import shutil, cStringIO
from MoinMoin.stats.chart import Chart, ChartData, Color
_ = request.getText
style = Chart.GDC_3DPIE
# get data
colors = ['red', 'mediumblue', 'yellow', 'deeppink', 'aquamarine', 'purple', 'beige',
'blue', 'forestgreen', 'orange', 'cyan', 'fuchsia', 'lime']
colors = ([Color(c) for c in colors])
data = get_data(request)
maxdata = len(colors) - 1
if len(data) > maxdata:
others = [x[0] for x in data[maxdata:]]
data = data[:maxdata] + [(sum(others), _('Others').encode('iso-8859-1', 'replace'))] # gdchart can't do utf-8
# shift front to end if others is very small
if data[-1][0] * 10 < data[0][0]:
data = data[1:] + data[0:1]
labels = [x[1] for x in data]
data = [x[0] for x in data]
# give us a chance to develop this
if _debug:
return "<p>data = %s</p>" % \
'<br>'.join([wikiutil.escape(repr(x)) for x in [labels, data]])
# create image
image = cStringIO.StringIO()
c = Chart()
c.addData(data)
title = ''
if request.cfg.sitename: title = "%s: " % request.cfg.sitename
title = title + _('Distribution of User-Agent Types')
c.option(
pie_color=colors,
label_font=Chart.GDC_SMALL,
label_line=1,
label_dist=20,
threed_depth=20,
threed_angle=225,
percent_labels=Chart.GDCPIE_PCT_RIGHT,
title_font=c.GDC_GIANT,
title=title.encode('iso-8859-1', 'replace')) # gdchart can't do utf-8
labels = [label.encode('iso-8859-1', 'replace') for label in labels]
c.draw(style,
(request.cfg.chart_options['width'], request.cfg.chart_options['height']),
image, labels)
request.content_type = 'image/gif'
request.content_length = len(image.getvalue())
# copy the image
image.reset()
shutil.copyfileobj(image, request, 8192)
|