Attachment 'ViewHistory-1.0.py'
Download 1 # -*- coding: iso-8859-1 -*-
2 """
3 MoinMoin - ViewHistory macro Version 1.0, 18.12.2012
4
5 Syntax:
6 <<ViewHistory>>; <<ViewHistory()>>
7 <<ViewHistory(page=pagename)>> default is current page
8
9 Prerequisites:
10
11 ViewLog class is imported from viewlog.py in
12 wiki/data/plugin/logfile. This folder must also contain the
13 standard __init__.py for plugin modules.
14
15 <<ViewLog>> marco must run on the page for views to be logged.
16
17 @copyright: 2012 Ian Riley <ian.riley@internode.on.net>
18
19 incorporting code from:
20
21 @copyright: 2006-2008 MoinMoin:ThomasWaldmann,
22 2007 MoinMoin:ReimarBauer
23
24 license: GNU GPL, see COPYING for details.
25 """
26
27 import os, time
28 from MoinMoin import user, wikiutil
29 from MoinMoin.Page import Page
30
31 def execute(macro, args):
32 request = macro.request
33 _ = request.getText
34 f = macro.request.formatter
35 user = request.user
36
37 ViewLog = wikiutil.importWikiPlugin(request.cfg, 'logfile', 'viewlog', 'ViewLog')
38
39 result = []
40
41 try:
42 args_parser = wikiutil.ParameterParser('%(page)s')
43 (count, args_dict) = args_parser.parse_parameters(args)
44 except:
45 return 'Error: ViewHistory macro has bad arguments.'
46
47 page = macro.formatter.page
48
49 targetPage = args_dict['page']
50 if targetPage:
51 otherPage = Page(request, targetPage)
52 if otherPage.exists():
53 page = otherPage
54 else:
55 return 'Error: ViewHistory macro page does not exist.'
56
57 page_name = page.page_name
58 pagelog = page.getPagePath('view-log', use_underlay=0, isfile=1)
59
60 if os.path.exists(pagelog):
61 viewlog = ViewLog(request, filename=pagelog, uid_override=None)
62 userViews = {}
63 for view in viewlog.reverse():
64 viewer = view.username
65 dated = user.getFormattedDateTime(
66 wikiutil.version2timestamp(view.view_time_usecs))
67 if viewer in userViews.keys():
68 userViews[viewer][1] += 1
69 else:
70 userViews[viewer] = [dated, 1]
71 result.extend([
72 f.heading(1, 3, id='view_history'),
73 f.text(_("Page view history")),
74 f.heading(0, 3),
75 f.paragraph(1),
76 f.text(_('Page views by user with time of most recently logged view.')),
77 f.paragraph(0),
78 f.table(1),
79 f.table_row(1),
80 f.table_cell(1), f.strong(1), f.text(_('Page')), f.strong(0), f.table_cell(0),
81 f.table_cell(1), f.strong(1), f.text(_('User')), f.strong(0), f.table_cell(0),
82 f.table_cell(1), f.strong(1), f.text(_('Viewed')), f.strong(0), f.table_cell(0),
83 f.table_cell(1), f.strong(1), f.text(_('Views')), f.strong(0), f.table_cell(0),
84 f.table_row(0), ])
85 viewers = userViews.keys()
86 viewers.sort()
87 for viewer in viewers:
88 result.extend([
89 f.table_row(1),
90 f.table_cell(1), f.text(page_name), f.table_cell(0),
91 f.table_cell(1), f.text(viewer), f.table_cell(0),
92 f.table_cell(1), f.text(userViews[viewer][0]), f.table_cell(0),
93 f.table_cell(1), f.text(str(userViews[viewer][1])), f.table_cell(0),
94 f.table_row(0), ])
95 result.extend([
96 f.table(0), ])
97
98 else:
99 result.extend([
100 f.heading(1, 3, id='view_history'),
101 f.text(_("Page view history")),
102 f.heading(0, 3),
103 f.paragraph(1),
104 f.text(_('No view-log for %s.' % (page_name))),
105 f.paragraph(0), ])
106
107 return ''.join(result)
You are not allowed to attach a file to this page.