/usr/share/pyshared/MoinMoin/macro/PageHits.py is in python-moinmoin 1.9.3-1ubuntu2.
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 | # -*- coding: iso-8859-1 -*-
"""
MoinMoin - per page hit statistics
@copyright: 2004-2005 MoinMoin:ThomasWaldmann
@license: GNU GPL, see COPYING for details
"""
from MoinMoin import caching, logfile
from MoinMoin.Page import Page
from MoinMoin.logfile import eventlog
class PageHits:
def __init__(self, macro):
self.macro = macro
self.request = macro.request
self.cache = caching.CacheEntry(self.request, 'charts', 'pagehits', scope='wiki', use_pickle=True)
def execute(self):
""" Execute the macro and return output """
if self.request.isSpiderAgent: # reduce bot cpu usage
return ''
cacheDate, hits = self.cachedHits()
self.addHitsFromLog(hits, cacheDate)
self.filterReadableHits(hits)
hits = [(hits[pagename], pagename) for pagename in hits]
hits.sort()
hits.reverse()
return self.format(hits)
def cachedHits(self):
""" Return tuple (cache date, cached hits) for all pages """
date, hits = 0, {}
if self.cache.exists():
try:
date, hits = self.cache.content()
except caching.CacheError:
self.cache.remove()
return date, hits
def addHitsFromLog(self, hits, cacheDate):
""" Parse the log, add hits after cacheDate and update the cache """
event_log = eventlog.EventLog(self.request)
event_log.set_filter(['VIEWPAGE'])
changed = False
# don't use event_log.date()
latest = None
for event in event_log.reverse():
if latest is None:
latest = event[0]
if event[0] <= cacheDate:
break
page = event[2].get('pagename', None)
if page:
hits[page] = hits.get(page, 0) + 1
changed = True
if changed:
self.updateCache(latest, hits)
def updateCache(self, date, hits):
try:
self.cache.update((date, hits))
except caching.CacheError:
pass
def filterReadableHits(self, hits):
""" Filter out hits the user many not see """
userMayRead = self.request.user.may.read
for pagename in hits.keys(): # we need .keys() because we modify the dict
page = Page(self.request, pagename)
if page.exists() and userMayRead(pagename):
continue
del hits[pagename]
def format(self, hits):
""" Return formated output """
result = []
formatter = self.macro.formatter
result.append(formatter.number_list(1))
for hit, pagename in hits:
result.extend([
formatter.listitem(1),
formatter.code(1),
("%6d" % hit).replace(" ", " "), " ",
formatter.code(0),
formatter.pagelink(1, pagename, generated=1),
formatter.text(pagename),
formatter.pagelink(0, pagename),
formatter.listitem(0),
])
result.append(formatter.number_list(0))
return ''.join(result)
def macro_PageHits(macro):
return PageHits(macro).execute()
|