Details

Applies to
Purpose
Description
This is add the eventlog to mysql database, I think, If the site is very busy, Record the eventlog to mysql, it is easy to do datamind.

Patch

   1 * looking for arch@arch.thinkmo.de--2003-archives/moin--main--1.5--patch-374 to compare with
   2 * comparing to arch@arch.thinkmo.de--2003-archives/moin--main--1.5--patch-374
   3 M  MoinMoin/Page.py
   4 M  wiki/config/wikiconfig.py
   5 A  MoinMoin/logfile/logmysql.py
   6 A  MoinMoin/logfile/eventmysqllog.py
   7 
   8 * modified files
   9 
  10 --- orig/MoinMoin/Page.py
  11 +++ mod/MoinMoin/Page.py
  12 @@ -9,7 +9,7 @@
  13  import StringIO, os, re, random, codecs
  14  
  15  from MoinMoin import config, caching, user, util, wikiutil
  16 -from MoinMoin.logfile import eventlog
  17 +from MoinMoin.logfile import eventlog, eventmysqllog
  18  from MoinMoin.util import filesys
  19  from MoinMoin.util.datetime import formathttpdate
  20  
  21 @@ -980,6 +980,7 @@
  22          # count hit?
  23          if keywords.get('count_hit', 0):
  24              eventlog.EventLog(request).add(request, 'VIEWPAGE', {'pagename': self.page_name})
  25 +            eventmysqllog.EventLog(request).add(request, 'VIEWPAGE', {'pagename': self.page_name})
  26  
  27          # load the text
  28          body = self.get_raw_body()
  29 
  30 
  31 --- orig/wiki/config/wikiconfig.py
  32 +++ mod/wiki/config/wikiconfig.py
  33 @@ -157,3 +157,9 @@
  34      # Enable graphical charts, requires gdchart.
  35      #chart_options = {'width': 600, 'height': 300}
  36  
  37 +    # For MySQL log.
  38 +    mysqlhost = 'localhost'
  39 +    mysqluser = 'root'
  40 +    mysqlpasswd = ''
  41 +    mysqldb = 'moin'
  42 +
  43 
  44 
  45 
  46 --- orig/MoinMoin/logfile/logmysql.py
  47 +++ mod/MoinMoin/logfile/logmysql.py
  48 @@ -0,0 +1,35 @@
  49 +"""
  50 +    MoinMoin log class for mysql
  51 +
  52 +    @license: GNU GPL, see COPYING for details.
  53 +"""
  54 +
  55 +import MySQLdb
  56 +
  57 +class LogMySQL:
  58 +    
  59 +    def __init__(self, request):
  60 +        self.request = request
  61 +        self.mysqlhost = request.cfg.mysqlhost
  62 +        self.mysqluser = request.cfg.mysqluser
  63 +        self.mysqlpasswd = request.cfg.mysqlpasswd
  64 +        self.mysqldb = request.cfg.mysqldb
  65 +
  66 +    def connect(self):
  67 +        # If cann't connect to mysql, just return None.
  68 +        try:
  69 +            self.mysqlconnect_db = MySQLdb.connect(
  70 +                     host=self.mysqlhost,
  71 +                     user=self.mysqluser,
  72 +                     passwd=self.mysqlpasswd,
  73 +                     db=self.mysqldb )
  74 +        except: return 0
  75 +        self.mysql_cursor = self.mysqlconnect_db.cursor()
  76 +        return 1
  77 +
  78 +    def close(self):
  79 +        # close the connect.
  80 +        self.mysqlconnect_db.close()
  81 +
  82 +    def add(self, sqlquery):
  83 +        self.mysql_cursor.execute(sqlquery)
  84 
  85 
  86 
  87 --- orig/MoinMoin/logfile/eventmysqllog.py
  88 +++ mod/MoinMoin/logfile/eventmysqllog.py
  89 @@ -0,0 +1,54 @@
  90 +"""
  91 +    MoinMoin event log class for mysql
  92 +
  93 +    @license: GNU GPL, see COPYING for details.
  94 +
  95 +    Will log to a table name is moinmoinlog,
  96 +    And the table schema is like this:
  97 +    CREATE TABLE `moinmoinlog` (
  98 +      `id` int(20) NOT NULL auto_increment,
  99 +      `logtime` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
 100 +      `username` text,
 101 +      `httpagent` text,
 102 +      `eventtype` text,
 103 +      `pagename` text,
 104 +      `httpreferer` text,
 105 +      `site` text,
 106 +      `action` text,
 107 +      `remoteip` text,
 108 +      PRIMARY KEY  (`id`)
 109 +    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
 110 +"""
 111 +
 112 +import MySQLdb
 113 +from logmysql import LogMySQL
 114 +
 115 +class EventLog(LogMySQL):
 116 +    
 117 +    def __init__(self, request, **kw):
 118 +        LogMySQL.__init__(self, request)
 119 +
 120 +    def add(self, request, eventtype, values=None):
 121 +        if values is None:
 122 +            values = {}
 123 +        valuesdict = values
 124 +        if self.connect():
 125 +            # All these are ascii
 126 +            remote_addr = getattr(request, 'remote_addr', '')
 127 +            http_user_agent = getattr(request, 'http_user_agent', '')
 128 +            http_referer = getattr(request, 'http_referer', '')
 129 +            username = request.user.valid and request.user.name or ''
 130 +            site = request.cfg.sitename
 131 +            if request.form.has_key('action'):
 132 +                action = request.form['action'][0]
 133 +            else: action = ''
 134 +            if not valuesdict.has_key('pagename'): valuesdict['pagename'] = ""
 135 +	    pagename = valuesdict['pagename']
 136 +            sqlquery = u"""
 137 +INSERT INTO moinmoinlog ( username, httpagent, eventtype, pagename, httpreferer, site, action, remoteip
 138 +) VALUES ( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');
 139 +""" %(username, http_user_agent, eventtype, pagename, http_referer, site, action, remote_addr)
 140 +            self.mysql_cursor.execute(sqlquery.encode("utf-8"))
 141 +            self.close()
 142 +            return 1
 143 +        return 0
 144 
mysql_eventlog.patch

Discussion

Maybe It need to move to anothe place.

Plan


CategoryMoinMoinPatch

MoinMoin: MoinMoinPatch/MySqlEventLog (last edited 2007-10-29 19:16:44 by localhost)