Attachment 'ViewLog-1.1.py'
Download 1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - ViewLog macro Version 1.1, 31.12.2012
4
5 Syntax:
6 <<ViewLog>>; <<ViewLog()>>
7 <<ViewLog(silent='1'|'true'|'silent',
8 all='1'|'true'|'all',
9 clear='1'|'true'|'clear',
10 offset=integer>0)>> default is false, false, false, 600
11
12 Prerequisites:
13
14 ViewLog class is imported from viewlog.py in
15 wiki/data/plugin/logfile. This folder must also contain the
16 standard __init__.py for plugin modules.
17
18 Related macros:
19
20 <<ViewHistory>> marco diplays a table of views by viewer.
21
22 <<Views>> marco displays view counts for self, other or all viewers.
23
24 History:
25
26 Version 1.1 - 31.12.2012: minor fix for <<ViewLog>> syntax.
27
28 Version 1.0 - 18.12.2012: initial version.
29
30 @copyright: 2012 Ian Riley <ian.riley@internode.on.net>
31
32 incorporting code from:
33
34 @copyright: 2006-2008 MoinMoin:ThomasWaldmann,
35 2007 MoinMoin:ReimarBauer
36
37 license: GNU GPL, see COPYING for details.
38 """
39
40 import time
41 from MoinMoin import user, wikiutil
42 from MoinMoin.Page import Page
43 from MoinMoin.util import timefuncs
44
45 class SessionViewRecord:
46 """To be held in request session as record of page views.
47 page_name
48 rev
49 mtime_usecs
50 """
51 def __init__(self):
52 self.mtime_usecs = wikiutil.timestamp2version(time.time())
53
54 def __repr__(self):
55 t = time.strftime("%Y-%m-%d %H:%M:%S GMT",
56 time.gmtime(wikiutil.version2timestamp(self.mtime_usecs)))
57 #return 'Page: {0} Rev: {1} Viewed: {2}'.format(self.page_name, self.rev, t)
58 return 'Page: %s Rev: %s Viewed: %s' % (self.page_name, self.rev, t)
59
60 def timediff(self, other):
61 return self.mtime_usecs - other
62
63 def timeago(self):
64 other = wikiutil.timestamp2version(time.time())
65 return -self.timediff(other)
66
67 def is_viewed_page(self, viewed = []):
68 for r in viewed:
69 if self.page_name == r.page_name and self.rev == r.rev:
70 return True
71 return False
72
73 def is_viewed_recently(self, viewed = [], offset=600):
74 offset = wikiutil.timestamp2version(offset)
75 for r in viewed:
76 if self.page_name == r.page_name and self.rev == r.rev:
77 if self.timediff(r.mtime_usecs) < offset:
78 return True
79 return False
80
81 def execute(macro, args):
82 request = macro.request
83
84 ViewLog = wikiutil.importWikiPlugin(request.cfg, 'logfile', 'viewlog', 'ViewLog')
85
86 args_dict = {'silent': '', 'all': '', 'clear': '', 'offset': ''}
87 if args:
88 try:
89 args_parser = wikiutil.ParameterParser('%(silent)s%(all)s%(clear)s%(offset)s')
90 (count, args_dict) = args_parser.parse_parameters(args)
91 except:
92 return 'Error: ViewLog macro has bad arguments'
93
94 allViews = False
95 val = args_dict['all']
96 if val and val.lower() in ['1', 'true', 'all']:
97 allViews = True
98
99 silent = False
100 val = args_dict['silent']
101 if val and val.lower() in ['1', 'true', 'silent']:
102 silent = True
103
104 clearHistory = False
105 val = args_dict['clear']
106 if val and val.lower() in ['1', 'true', 'clear']:
107 clearHistory = True
108
109 offset = 600
110 val = args_dict['offset']
111 if val and int(val) >= 0:
112 offset = int(val)
113
114 user = request.user
115 if user.valid:
116 user_name = user.name
117 else:
118 user_name = 'anon'
119
120 session = request.session
121
122 page = macro.formatter.page
123 page_name = page.page_name
124 rev = page.get_real_rev()
125
126 mtime_usecs = wikiutil.timestamp2version(time.time())
127
128 action = 'VIEW'
129 logIt = False
130
131 viewed = request.session.get('viewed', [])
132 record = SessionViewRecord()
133 record.page_name = page_name
134 record.rev = rev
135
136 #only add record if not viewed or not within seconds offset
137 if not record.is_viewed_recently(viewed, offset=offset):
138 viewed.append(record)
139 request.session['viewed'] = viewed[-100:] #trim after 100 in session
140 logIt = True
141 #only log if recorded above, or logging all views
142 if allViews or logIt:
143 pagelog = page.getPagePath('view-log', use_underlay=0, isfile=1)
144 viewlog = ViewLog(request, filename=pagelog, uid_override=None)
145 viewlog.add(request, mtime_usecs, rev, action, page_name, user_name, session.sid)
146
147 result = 'Viewed %s by %s.' % (user.getFormattedDateTime(
148 wikiutil.version2timestamp(mtime_usecs)), user_name)
149 if clearHistory:
150 request.session['viewed'] = []
151 result += ' Session history cleared.'
152 if silent:
153 result = ''
154 return result
You are not allowed to attach a file to this page.