Attachment 'Hits-1.6.0-8.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 this page was changed. It could be used to show all page interactions too.
   8 
   9     CALLING SEQUENCE:
  10         [[Hits([all=(0,1)],[filter=(VIEWPAGE,SAVEPAGE)]]
  11 
  12     KEYWORD PARAMETERS:
  13         all: if set to 1 then cummulative hits over all wiki pages is returned. Default is 0
  14         filter: if set to SAVEPAGE then the saved pages are counted. Default is VIEWPAGE.
  15 
  16     EXAMPLE:
  17        [[Hits]]
  18 
  19        [[Hits(all=1)]]
  20 
  21        [[Hits(all=1,filter=SAVEPAGE)]]
  22 
  23        Now we have [[Hits]] hits on this page.
  24 
  25        Or you can use the Frame parser to use alignment and others
  26        {{{#!Frame align=float:right,background=#efefef
  27        [[Hits]] hits
  28        }}}
  29 
  30       {{{#!Frame align=clear
  31       }}}
  32     PROCEDURE:
  33       It must be in "MoinMoin/macro"
  34 
  35       Please remove the version number from the file name!
  36 
  37     MODIFICATION HISTORY:
  38         @copyright: 2004 by Reimar Bauer (R.Bauer@fz-juelich.de)
  39         @license: GNU GPL, see COPYING for details.
  40     2004-12-29 RB bug fixed eventlog is in logfile
  41     2005-07-15 BenjaminVrolijk exchange of filename for event-log by getPagePath
  42     2006-05-01 1.5.3-4 RB bug fixed for quoting pagenames and some parts refactored
  43     2006-05-02 1.5.3-5 RB bug fixed quoting changed to url_quote_plus because of the '+' sign used for a blank 
  44                in a Wiki Name instead of the '_' in the log file
  45                
  46     2006-05-04 1.5.3-6 RB: divid is by now on default "" 
  47                            noframe is default 1
  48                            if no keyword is used only the value is returned and could be used 
  49                            in normal text
  50                            
  51     2006-05-05 1.5.3-7 RB:bug fixed only pagenames should be counted
  52     2006-08-16 1.6.0-8 RB:PEP8 and  
  53                        removed all Keyword Parameters which could be used by the Frame parser 
  54                        or the wiki markup itselfs
  55                        
  56 
  57 """
  58 
  59 
  60 from MoinMoin import wikiutil
  61 from MoinMoin.logfile import eventlog
  62 import os
  63 
  64 Dependencies = ['time'] # do not cache
  65 
  66 def execute(macro, args):
  67     request = macro.request
  68     _ = request.getText
  69     formatter = macro.formatter
  70 
  71     kw = {} # create a dictionary for the formatter.image call
  72     kw["all"] = "0"
  73     kw["filter"] = "VIEWPAGE"
  74 
  75     if args:
  76         args = args.split(',')
  77         args = [arg.strip() for arg in args]
  78     else:
  79         args = []
  80 
  81     argc = len(args)
  82     kw_count = 0
  83     for arg in args:
  84         if '=' in arg:
  85             kw_count += 1
  86             key, value = arg.split('=', 1)
  87             kw[str(key)] = wikiutil.escape(value, quote=1)
  88 
  89     argc -= kw_count
  90     #pagename = wikiutil.quoteWikinameURL(formatter.page.page_name)
  91     pagename = wikiutil.url_quote_plus(formatter.page.page_name)
  92     thispage_name = "pagename=%s&" % (pagename)
  93     
  94     filename = request.rootpage.getPagePath('event-log', isfile=1)
  95     
  96     file = open(filename, 'r')
  97     events = file.readlines()
  98     file.close()
  99 
 100     
 101     count = 0
 102     for event in events:
 103         try:
 104             line = event.rstrip()
 105             time, eventtype, kvpairs = line.split('\t')
 106         except ValueError:
 107                 # badly formatted line in file, skip it
 108             continue
 109         if kw["filter"] and eventtype not in kw["filter"]:
 110             continue
 111 
 112         if kw["all"] == "0":
 113             if line.find(thispage_name) > -1:
 114                count += 1
 115         else:
 116             count += 1
 117 
 118     return "%s" % str(count)

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.