Attachment 'ViewHistory-1.3.py'

Download

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

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