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