Attachment 'ViewHistory-1.2.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - ViewHistory macro Version 1.2, 15.01.2013
   4     
   5     Syntax:
   6        <<ViewHistory>>; <<ViewHistory()>>
   7        <<ViewHistory(page=pagename)>> default is current page
   8        <<ViewHistory(page='^regex_pagenames')>> all matching pages with logs
   9 
  10     Prerequisites:
  11     
  12         ViewLog class is imported from viewlog.py in 
  13         wiki/data/plugin/logfile. This folder must also contain the 
  14         standard __init__.py for plugin modules.    
  15     
  16         <<ViewLog>> marco must run on the page for views to be logged. 
  17 
  18     History:
  19     
  20         Version 1.2 - 15.01.2013: allowed a summary from multiple pages,
  21                                   restricted to pages use.may.admin.          
  22     
  23         Version 1.1 - 31.12.2012: minor fix for <<ViewHistory>> syntax.
  24     
  25         Version 1.0 - 18.12.2012: initial version.
  26 
  27     @copyright: 2012-2013 Ian Riley <ian.riley@internode.on.net>
  28     
  29     incorporting code from:
  30     
  31     @copyright: 2006-2008 MoinMoin:ThomasWaldmann,
  32                 2007 MoinMoin:ReimarBauer
  33                 2000-2004 Juergen Hermann <jh@web.de>
  34                 2000-2001 Richard Jones <richard@bizarsoftware.com.au>
  35 
  36     license: GNU GPL, see COPYING for details.
  37 """
  38 
  39 import os, re, time
  40 from MoinMoin import user, wikiutil
  41 from MoinMoin.Page import Page
  42 
  43 def execute(macro, args):
  44     request = macro.request
  45     _ = request.getText
  46     f = macro.request.formatter
  47     user = request.user
  48  
  49     ViewLog = wikiutil.importWikiPlugin(request.cfg, 'logfile', 'viewlog', 'ViewLog')
  50 
  51     result = []
  52     first_result  = True
  53     thisPage = macro.formatter.page
  54 
  55     args_dict = {'page': ''}
  56     if args:
  57         try:
  58             args_parser = wikiutil.ParameterParser('%(page)s')
  59             (count, args_dict) = args_parser.parse_parameters(args)
  60         except:
  61             return 'Error: ViewHistory macro has bad arguments.'
  62     
  63     targetPage = args_dict['page']
  64     if not targetPage:
  65         targetPage = thisPage.page_name
  66         
  67     # get list of pages to report
  68     inc_name = wikiutil.AbsPageName(thisPage.page_name, targetPage)
  69     pageList = [inc_name]
  70     if inc_name.startswith("^"):
  71         try:
  72             inc_match = re.compile(inc_name)
  73         except re.error:
  74             pass # treat as plain page name
  75         else:
  76             # get user filtered readable page list
  77             pageList = request.rootpage.getPageList(filter = inc_match.match)
  78 
  79     checkedList = []
  80     for inc_name in pageList:
  81         if request.user.may.admin(inc_name):
  82             checkedList.append(inc_name)
  83     pageList = sorted(checkedList)
  84 
  85     for inc_name in pageList:
  86         fmt = macro.formatter.__class__(request, is_included=True)
  87         fmt._base_depth = macro.formatter._base_depth
  88         inc_page = Page(request, inc_name, formatter=fmt)
  89         # not needed, handled by getPageList by default
  90         #if not inc_page.exists():
  91         #    continue #just skip pages that don't exist, handle later if none
  92   
  93         page_name = inc_page.page_name
  94         pagelog = inc_page.getPagePath('view-log', use_underlay=0, isfile=1)
  95         if not os.path.exists(pagelog): 
  96             continue #just skip pages with out a view-log
  97         viewlog = ViewLog(request, filename=pagelog, uid_override=None)
  98 
  99         userViews = {}
 100         for view in viewlog.reverse():
 101             viewer = view.username
 102             dated = user.getFormattedDateTime(
 103                     wikiutil.version2timestamp(view.view_time_usecs))
 104             if viewer in userViews.keys(): 
 105                 userViews[viewer][1] += 1
 106             else:
 107                 userViews[viewer] = [dated, 1]
 108         if first_result:
 109             result.extend([
 110                 f.heading(1, 3, id='view_history'),
 111                 f.text(_("Page view history")),
 112                 f.heading(0, 3),
 113                 f.paragraph(1),
 114                 f.text(_('Page views by user with time of most recently logged view.')),
 115                 f.paragraph(0),
 116                 f.table(1),
 117                 f.table_row(1),
 118                 f.table_cell(1), f.strong(1), f.text(_('Page')), f.strong(0), f.table_cell(0),
 119                 f.table_cell(1), f.strong(1), f.text(_('User')), f.strong(0), f.table_cell(0),
 120                 f.table_cell(1), f.strong(1), f.text(_('Viewed')), f.strong(0), f.table_cell(0),
 121                 f.table_cell(1), f.strong(1), f.text(_('Views')), f.strong(0), f.table_cell(0),
 122                 f.table_row(0), ])
 123             first_result = False
 124         viewers = userViews.keys()
 125         viewers.sort()
 126         for viewer in viewers:
 127             result.extend([
 128                 f.table_row(1),
 129                 f.table_cell(1), f.text(page_name), f.table_cell(0),
 130                 f.table_cell(1), f.text(viewer), f.table_cell(0),
 131                 f.table_cell(1), f.text(userViews[viewer][0]), f.table_cell(0),
 132                 f.table_cell(1), f.text(str(userViews[viewer][1])), f.table_cell(0),
 133                 f.table_row(0), ])             
 134 
 135     if not first_result:
 136         result.extend([
 137             f.table(0), ])
 138     else:
 139         page_names = ', '.join([str(p) for p in pageList])
 140         result.extend([
 141             f.heading(1, 3, id='view_history'),
 142             f.text(_("Page view history")),
 143             f.heading(0, 3),
 144             f.paragraph(1),
 145             f.text(_('No view-log for requested page(s): %s.' % (page_names))),
 146             f.paragraph(0), ])
 147 
 148     return ''.join(result)
 149     

You are not allowed to attach a file to this page.