Attachment 'Hits-1.5.3-7.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - Hits Macro
   4 
   5     PURPOSE:
   6         This macro is used to show the cummulative hits of the wikipage where the Macro is called from.
   7         Optional you could count how much a page was altered on this or all pages.
   8 
   9     CALLING SEQUENCE:
  10         [[Hits([text],[bgcolor=bgcolor],[all=(0,1)],[filter=(VIEWPAGE,SAVEPAGE)],[divid=(logo,searchform)],[noframe=(0,1)])]]
  11 
  12     OPTIONAL INPUTS:
  13         text: the text which is used as description of counter number
  14 
  15     KEYWORD PARAMETERS:
  16         bgcolor: if set to the rgb color triple this color is used. Default color is #FFFFFF
  17         all: if set to 1 then cummulative hits over all wiki pages is returned. Default is 0
  18         filter: if set to SAVEPAGE then the saved pages are counted. Default is VIEWPAGE.
  19         divid: if set this divid is used. Default is "". You could use each defined in screen.css.
  20                I have tried logo and searchform.
  21         noframe: if set to 1 only text is written without a table border. Default is 0.
  22 
  23     EXAMPLE:
  24        [[Hits]]
  25 
  26        [[Hits(counts)]]
  27 
  28        [[Hits(counts,divid=searchform,noframe=0)]]
  29 
  30        [[Hits(counts,bgcolor=#CCCCCC,noframe=0)]]
  31 
  32        [[Hits(counts,all=1,noframe=0)]]
  33 
  34        [[Hits(X pages altered,all=1,filter=SAVEPAGE,noframe=0)]]
  35 
  36        Now we have [[Hits]] hits on this page.
  37 
  38 
  39     PROCEDURE:
  40       It must be in "MoinMoin/macro"
  41 
  42       Please remove the version number from the file name!
  43 
  44     MODIFICATION HISTORY:
  45         @copyright: 2004 by Reimar Bauer (R.Bauer@fz-juelich.de)
  46         @license: GNU GPL, see COPYING for details.
  47     2004-12-29 RB bug fixed eventlog is in logfile
  48     2005-07-15 BenjaminVrolijk exchange of filename for event-log by getPagePath
  49     2006-05-01 1.5.3-4 RB bug fixed for quoting pagenames and some parts refactored
  50     2006-05-02 1.5.3-5 RB bug fixed quoting changed to url_quote_plus because of the '+' sign used for a blank 
  51                in a Wiki Name instead of the '_' in the log file
  52                
  53     2006-05-04 1.5.3-6 RB: divid is by now on default "" 
  54                            noframe is default 1
  55                            if no keyword is used only the value is returned and could be used 
  56                            in normal text
  57                            
  58     2006-05-05 1.5.3-7 RB:bug fixed only pagenames should be counted
  59 
  60 
  61 """
  62 
  63 
  64 from MoinMoin import wikiutil
  65 from MoinMoin.logfile import eventlog
  66 import os
  67 
  68 def execute(macro,args):
  69     request = macro.request
  70     _ = request.getText
  71     formatter = macro.formatter
  72     
  73     kw = {} # create a dictionary for the formatter.image call
  74     kw["bgcolor"] = "#FFFFFF"
  75     kw["all"] = "0"
  76     kw["filter"] = "VIEWPAGE"
  77     kw["divid"] = ""
  78     kw["noframe"] = "1"
  79 
  80     if args:
  81         args = args.split(',')
  82         args = [arg.strip() for arg in args]
  83     else:
  84         args = []
  85 
  86     argc = len(args)
  87     kw_count = 0
  88     for arg in args :
  89         if '=' in arg:
  90             kw_count += 1
  91             key, value = arg.split('=', 1)
  92             kw[str(key)] = wikiutil.escape(value,quote=1)
  93 
  94     argc -= kw_count
  95     if argc == 1:
  96          descr = args[0]
  97     else:
  98          descr = ''
  99 
 100     #pagename = wikiutil.quoteWikinameURL(formatter.page.page_name)
 101     pagename = wikiutil.url_quote_plus(formatter.page.page_name)
 102     thispage_name = "pagename=%s&" % (pagename)
 103     
 104     filename = request.rootpage.getPagePath('event-log',isfile=1)
 105     file = open(filename, 'r')
 106     events = file.readlines()
 107     file.close()
 108     
 109     count = 0
 110     for event in events:
 111         try:
 112             line = event.rstrip()
 113             time, eventtype, kvpairs = line.split('\t')
 114         except ValueError:
 115                 # badly formatted line in file, skip it
 116             continue
 117         if kw["filter"] and eventtype not in kw["filter"]: 
 118             continue
 119             
 120         if kw["all"] == "0":
 121             if line.find(thispage_name) > -1:
 122                count += 1
 123         else:
 124             count += 1
 125 
 126     if kw["divid"] == "" and  kw["noframe"] == "1":   
 127         return "%s %s" % (str(count),descr)     
 128     elif kw["noframe"] == "0":
 129         return '<div id="%(divid)s"><table><tr><td bgcolor="%(bgcolor)s">%(count)s %(descr)s </td></tr></table></div>' % {
 130          "divid": kw["divid"],
 131          "bgcolor": kw["bgcolor"],
 132          "count": str(count),
 133          "descr": descr }
 134     elif kw["noframe"] == "1" :
 135         return '<div id="%(divid)s">%(count)s %(descr)s</div>' % {
 136             "divid": kw["divid"],
 137             "count": str(count),
 138             "descr": descr }
 139        
 140        
 141 
 142     

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2004-12-29 22:19:37, 3.0 KB) [[attachment:Hits-1.3.1-2.py]]
  • [get | view] (2005-08-04 13:28:13, 3.1 KB) [[attachment:Hits-1.3.4-3.py]]
  • [get | view] (2006-05-01 16:31:14, 3.6 KB) [[attachment:Hits-1.5.3-4.py]]
  • [get | view] (2006-05-02 20:40:48, 3.8 KB) [[attachment:Hits-1.5.3-5.py]]
  • [get | view] (2006-05-04 18:21:13, 4.3 KB) [[attachment:Hits-1.5.3-6.py]]
  • [get | view] (2006-05-05 04:35:45, 4.5 KB) [[attachment:Hits-1.5.3-7.py]]
  • [get | view] (2006-08-16 17:29:52, 3.6 KB) [[attachment:Hits-1.6.0-8.py]]
  • [get | view] (2006-08-16 17:37:53, 6.1 KB) [[attachment:Hits1.png]]
 All files | Selected Files: delete move to page copy to page

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