# -*- coding: iso-8859-1 -*-
"""
    MoinMoin - Hits Macro

    PURPOSE:
        This macro is used to show the cummulative hits of the wikipage where the Macro is called from.
	Optional you could count how much a page was altered on this or all pages.

    CALLING SEQUENCE:
        [[Hits([text],[bgcolor=bgcolor],[all=(0,1)],[filter=(VIEWPAGE,SAVEPAGE)],[divid=(logo,searchform)],[noframe=(0,1)])]]

    OPTIONAL INPUTS:
        text: the text which is used as description of counter number

    KEYWORD PARAMETERS:
        bgcolor: if set to the rgb color triple this color is used. Default color is #FFFFFF
	all: if set to 1 then cummulative hits over all wiki pages is returned. Default is 0
	filter: if set to SAVEPAGE then the saved pages are counted. Default is VIEWPAGE.
	divid: if set this divid is used. Default is logo. You could use each defined in screen.css.
	       I have tried logo and searchform.
	noframe: if set to 1 only text is written without a table border. Default is 0.

    EXAMPLE:
      [[Hits]]

      [[Hits(counts)]]

      [[Hits(counts,divid=searchform)]]

      [[Hits(counts,bgcolor=#CCCCCC)]]

      [[Hits(counts,all=1)]]

      [[Hits(X pages altered,all=1,filter=SAVEPAGE)]]

    PROCEDURE:

      It must be in "MoinMoin/macro"

      Please remove the version number from the file name!

    MODIFICATION HISTORY:
        @copyright: 2004 by Reimar Bauer (R.Bauer@fz-juelich.de)
        @license: GNU GPL, see COPYING for details.
	2004-12-29 RB bug fixed eventlog is in logfile
	2005-07-15 BenjaminVrolijk exchange of filename for event-log by getPagePath


"""


from MoinMoin import wikiutil
from MoinMoin.logfile import eventlog
import os,string


def execute(macro, text):
    kw ={} # create a dictionary for the formatter.image call
    if text:
      args=text.split(',')
    else:
      args=[]

    number_args=len(args)
    count=0
    kw["bgcolor"]="#FFFFFF"
    kw["all"]=0
    kw["filter"]="VIEWPAGE"
    kw["divid"]="logo"
    kw["noframe"]=0

    for a in args :
        if (a.find('=') > -1):
           count=count+1
	   key=a.split('=')

	   kw[str(key[0])]=wikiutil.escape(string.join(key[1],''), quote=1)

    number_args=number_args-count

    if (number_args == 1):
         descr=' '+string.strip(args[0])
    else:
         descr=''


    pagename=macro.formatter.page.page_name
    
    filename = macro.request.rootpage.getPagePath('event-log', isfile=1)

    file = open(filename, 'r')
    events = file.readlines()
    file.close()



    count=0
    for event in events:

        try:
            time, eventtype, kvpairs = string.split(string.rstrip(event), '\t')
        except ValueError:
                # badly formatted line in file, skip it
            continue
        if kw["filter"] and eventtype not in kw["filter"]: continue
	if (kw["all"] == 0) :
	   if (event.find(pagename+'&') > -1):
	       count=count+1
	else:
           count=count+1

    if( kw["noframe"] == 0) :
        result='<div id="'+kw["divid"]+'"><table><tr><td bgcolor="'+kw["bgcolor"]+'">'+str(count)+descr+' </td></tr></table></div>'
    else:
       result='<div id="'+kw["divid"]+'">'+str(count)+descr+'</div>'

    return result
