Attachment 'ViewHistory-1.4.py'

Download

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

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