Attachment 'viewlog-logfile-1.0.py'
Download 1 """
2 MoinMoin - ViewLog class Version 1.0, 18.12.2012
3
4 This is used for accessing the page view-log.
5
6 To be installed wiki/data/plugin/logfile along with the
7 standard __init__.py for plugin modules.
8
9 @copyright: 2012 Ian Riley <ian.riley@internode.on.net>
10
11 Based on: editlog
12
13 @copyright: 2006 MoinMoin:ThomasWaldmann
14 @license: GNU GPL, see COPYING for details.
15 """
16
17 from MoinMoin import log
18 logging = log.getLogger(__name__)
19
20 from MoinMoin.logfile import LogFile
21 from MoinMoin import wikiutil, user, config
22 from MoinMoin.Page import Page
23
24 class ViewLogLine:
25 """
26 Given the following attributes
27
28 ed_time_usecs
29 pagename
30 rev
31 action
32 userid
33 username
34 sid
35 addr
36 hostname
37 """
38 def __init__(self, usercache):
39 self._usercache = usercache
40
41 class ViewLog(LogFile):
42 """ Used for accessing page view-log.
43 """
44 def __init__(self, request, filename=None, buffer_size=4096, **kw):
45 if filename is None:
46 rootpagename = kw.get('rootpagename', None)
47 if rootpagename:
48 filename = Page(request, rootpagename).getPagePath('view-log', isfile=1)
49 else:
50 filename = request.rootpage.getPagePath('view-log', isfile=1)
51 if filename:
52 LogFile.__init__(self, filename, buffer_size)
53
54 self._NUM_FIELDS = 9
55 self._usercache = {}
56
57 # Used by antispam in order to show an internal name instead
58 # of a confusing userid
59 self.uid_override = kw.get('uid_override', None)
60
61 # force inclusion of ip address, even if config forbids it.
62 # this is needed so PageLock can work correctly for locks of anon editors.
63 self.force_ip = kw.get('force_ip', False)
64
65 def add(self, request, mtime, rev, action, pagename, username, sid, host=None):
66 """ Generate (and add) a line to the view-log.
67
68 If `host` is None, it's read from request vars.
69 """
70 if request.cfg.log_remote_addr or self.force_ip:
71 if host is None:
72 host = request.remote_addr or ''
73
74 if request.cfg.log_reverse_dns_lookups:
75 import socket
76 try:
77 hostname = socket.gethostbyaddr(host)[0]
78 hostname = unicode(hostname, config.charset)
79 except (socket.error, UnicodeError):
80 hostname = host
81 else:
82 hostname = host
83 else:
84 host = ''
85 hostname = ''
86
87 user_id = request.user.valid and request.user.id or ''
88
89 if self.uid_override is not None:
90 user_id = ''
91 hostname = self.uid_override
92 host = ''
93
94 line = u"\t".join((str(long(mtime)), # has to be long for py 2.2.x
95 wikiutil.quoteWikinameFS(pagename),
96 "%08d" % rev,
97 action,
98 user_id,
99 username,
100 sid,
101 host,
102 hostname,
103 )) + "\n"
104 self._add(line)
105
106 def parser(self, line):
107 """ Parse veiw-log line into fields """
108 fields = line.strip().split('\t')
109 # Pad empty fields
110 missing = self._NUM_FIELDS - len(fields)
111 if missing:
112 fields.extend([''] * missing)
113 result = ViewLogLine(self._usercache)
114 (result.view_time_usecs,
115 result.pagename, result.rev, result.action,
116 result.userid, result.username, result.sid,
117 result.addr, result.hostname, ) = fields[:self._NUM_FIELDS]
118 if not result.hostname:
119 result.hostname = result.addr
120 result.pagename = wikiutil.unquoteWikiname(result.pagename.encode('ascii'))
121 result.view_time_usecs = long(result.view_time_usecs or '0') # has to be long for py 2.2.x
122 return result
You are not allowed to attach a file to this page.