Attachment 'Hits-1.5.3-6.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                            if no keyword is used only the value is returned and could be used 
  55                            in normal text
  56 
  57 
  58 """
  59 
  60 
  61 from MoinMoin import wikiutil
  62 from MoinMoin.logfile import eventlog
  63 import os
  64 
  65 def execute(macro,args):
  66     request = macro.request
  67     _ = request.getText
  68     formatter = macro.formatter
  69     
  70     kw = {} # create a dictionary for the formatter.image call
  71     kw["bgcolor"] = "#FFFFFF"
  72     kw["all"] = "0"
  73     kw["filter"] = "VIEWPAGE"
  74     kw["divid"] = ""
  75     kw["noframe"] = "1"
  76 
  77     if args:
  78         args = args.split(',')
  79         args = [arg.strip() for arg in args]
  80     else:
  81         args = []
  82 
  83     argc = len(args)
  84     kw_count = 0
  85     for arg in args :
  86         if '=' in arg:
  87             kw_count += 1
  88             key, value = arg.split('=', 1)
  89             kw[str(key)] = wikiutil.escape(value,quote=1)
  90 
  91     argc -= kw_count
  92 
  93     if argc == 1:
  94          descr = args[0]
  95     else:
  96          descr = ''
  97 
  98     #pagename = wikiutil.quoteWikinameURL(formatter.page.page_name)
  99     pagename = wikiutil.url_quote_plus(formatter.page.page_name)
 100     filename = request.rootpage.getPagePath('event-log',isfile=1)
 101     
 102     file = open(filename, 'r')
 103     events = file.readlines()
 104     file.close()
 105     
 106     count = 0
 107     for event in events:
 108         try:
 109             line = event.rstrip()
 110             time, eventtype, kvpairs = line.split('\t')
 111         except ValueError:
 112                 # badly formatted line in file, skip it
 113             continue
 114         if kw["filter"] and eventtype not in kw["filter"]: continue
 115         if kw["all"] == "0":
 116             if event.find(pagename+'&') > -1:
 117                count += 1
 118         else:
 119             count += 1
 120 
 121     if kw["divid"] == "" and  kw["noframe"] == "1":   
 122         return "%s %s" % (str(count),descr)     
 123     
 124     elif kw["noframe"] == "0":
 125         return '<div id="%(divid)s"><table><tr><td bgcolor="%(bgcolor)s">%(count)s %(descr)s </td></tr></table></div>' % {
 126          "divid": kw["divid"],
 127          "bgcolor": kw["bgcolor"],
 128          "count": str(count),
 129          "descr": descr }
 130     elif kw["noframe"] == "1" :
 131         return '<div id="%(divid)s">%(count)s %(descr)s</div>' % {
 132             "divid": kw["divid"],
 133             "count": str(count),
 134             "descr": descr }
 135        
 136        
 137 
 138     

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.