Attachment 'WikiLog.py'
Download 1 # -*- coding: utf-8 -*-
2 """
3 MoinMoin - Browse Wiki event-log for page requests.
4
5 The macro also tries to resolve host names, if not found then host IP is given
6
7 Usage:
8 <<WikiLog(yyyy, mm, dd)>>
9
10 where yyyy, mm, dd - date from which to start showing logs
11 if no arguments given then showing for the last month
12
13 @copyright: 2007-2008 by Alexander "Loki" Agibalov
14 @license: GNU GPL, see COPYING for details.
15
16 changes:
17 12.2007 - conversion to new syntax by Bolesław Kulbabiński
18 11.2008 - fixed bug with hostname resolution (Alexander Agibalov)
19 """
20
21 import os, socket
22 from MoinMoin.Page import Page
23 from MoinMoin.logfile import eventlog
24 from MoinMoin import wikiutil
25 from datetime import datetime
26
27 def macro_WikiLog(macro, year=int, month=int, day=int):
28
29 request = macro.request
30 _ = macro.request.getText
31 if not request.user.isSuperUser():
32 return request.formatter.text('<<WikiLog: Only a SuperUser can view this page!>>')
33
34 if year is None or month is None or day is None:
35 dNow0 = datetime.now()
36 if dNow0.month > 1:
37 sargs = (dNow0.year, dNow0.month - 1, dNow0.day)
38 else:
39 sargs = (dNow0.year - 1, 12, dNow0.day)
40 retTxt = "<p><b>Page visits for the last month</b></p>"
41 else:
42 retTxt = "<p><b>Page visits since %i-%i-%i</b></p>" % (year, month, day)
43 sargs = [year, month, day]
44
45 event_log = eventlog.EventLog(request)
46 try:
47 logDate = event_log.date()
48 except logfile.LogMissing:
49 return request.formatter.text('<<WikiLog: Event log not found!>>')
50
51 event_log.set_filter(['VIEWPAGE'])
52 sRet = retTxt
53 sRet += "<table><tr><td>Date</td><td>Page</td><td>Host or IP</td><tr>"
54 dHosts = {}
55 for event in event_log.reverse():
56 date_tup = request.user.getTime(wikiutil.version2timestamp(event[0]))
57 if int(date_tup[0]) < int(sargs[0]):
58 break
59 if (int(date_tup[0]) == int(sargs[0])) and (int(date_tup[1]) < int(sargs[1])):
60 break
61 if (int(date_tup[0]) == int(sargs[0])) and (int(date_tup[1]) == int(sargs[1])) and (int(date_tup[2]) <= int(sargs[2])):
62 break
63
64 sHost = str(event[2].get('REMOTE_ADDR', None))
65 if not dHosts.has_key(sHost):
66 try:
67 lHost = socket.gethostbyaddr(sHost)
68 dHosts[sHost] = lHost[0]
69 except:
70 dHosts[sHost] = sHost
71
72 sHost = dHosts[sHost]
73 tmpSt = "<tr><td>%s</td><td>%s</td><td>%s</td></tr>" % (request.user.getFormattedDateTime(wikiutil.version2timestamp(event[0])),
74 event[2].get('pagename', None),
75 sHost)
76 sRet += tmpSt
77 sRet += "</table>"
78 return macro.formatter.rawHTML(sRet)
79
80
81 def execute(macro, args):
82 try:
83 return wikiutil.invoke_extension_function(
84 macro.request, macro_WikiLog,
85 args, [macro])
86 except ValueError, err:
87 return macro.request.formatter.text(
88 "<<WikiLog: %s>>" % err.args[0])
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.