"""
    MoinMoin - Parser for MySQL data
    
    @copyright: 2005 by Alexander Fischer <mail@fischer-alexander.net>
    @license: GNU GPL, see http://www.gnu.org/licenses/gpl for details
    

    Take care that a MySQL based database for source is assumed.
    For additional information performing take parser without any data given. 
    
"""

from MoinMoin import wikiutil, config
import MySQLdb
import re

class Parser:
    def __init__(self, raw, request, **kw):
          self.raw = raw
          self.request = request
         
    def format(self, formatter):

          lines = self.raw.splitlines()
                     
          result=[self.request.formatter.paragraph(0)]                                                   
          cr=self.request.formatter.linebreak(0)                                                             
          
          #### initialize flags ################################################################### 
          dbflag=0
          qflag=0
          hdflag=0
          wlflag=0
          hlflag=0
          mflag=0
          hflag=0
           
          ##### section1 ###########################################################################
          
          if lines[0:]:
            parameterlist=[]                                                                                                
            for index in range(len(lines[0:])):                                                                 
              parameterlist.append(lines[0:][index])
            i=0                                                                                                                       
            for parameterstring in parameterlist :                                                          
              regexdb=re.compile("DBDATA")                                                                 
              matchdb=regexdb.search(parameterstring)                                           
              if matchdb:                                                                                                     
                dbflag=1                                                                                                        
                parameterdb=''''''                                                                                          
                for char in range(matchdb.span(0)[1],len(parameterlist[i])):            
                  if parameterlist[i][char]!=' ':                                                                   
                    parameterdb=parameterdb+parameterlist[i][char]                         
                parameterdb=parameterdb.split(',')                                                      

              
              regexq=re.compile("QUERY")
              matchq=regexq.search(parameterstring)
              if matchq:
                qflag=1
                parameterq=''''''
                for char in range(matchq.span(0)[1],len(parameterlist[i])):
                  parameterq=parameterq+parameterlist[i][char] 

              
              regexhd=re.compile("HEADLINE")
              matchhd=regexhd.search(parameterstring)
              if matchhd:
                hdflag=1
                parameterhd=''''''
                for char in range(matchhd.span(0)[1],len(parameterlist[i])):
                  if parameterlist[i][char]!=' ':
                    parameterhd=parameterhd+parameterlist[i][char] 
                parameterhd=parameterhd.split(',')

              
              regexwl=re.compile("WIKICOLUMN")
              matchwl=regexwl.search(parameterstring)
              if matchwl:
                wlflag=1
                parameterwl=''''''
                for char in range(matchwl.span(0)[1],len(parameterlist[i])):
                  if parameterlist[i][char]!=' ':
                    parameterwl=parameterwl+parameterlist[i][char]
                parameterwl=parameterwl.split(',')
         
              
              regexhl=re.compile("HTMLCOLUMN")
              matchhl=regexhl.search(parameterstring)
              if matchhl:
                hlflag=1
                parameterhl=''''''
                for char in range(matchhl.span(0)[1],len(parameterlist[i])):
                  if parameterlist[i][char]!=' ':
                    parameterhl=parameterhl+parameterlist[i][char]
                parameterhl=parameterhl.split(',')
              
             
              regexm=re.compile("MAILCOLUMN")
              matchm=regexm.search(parameterstring)
              if matchm:
                mflag=1
                parameterm=''''''
                for char in range(matchm.span(0)[1],len(parameterlist[i])):
                  if parameterlist[i][char]!=' ':
                    parameterm=parameterm+parameterlist[i][char]
                parameterm=parameterm.split(',')
              
              
              regexh=re.compile("HIDDENCOLUMN")
              matchh=regexh.search(parameterstring)
              if matchh:
                hflag=1
                parameterh=''''''
                for char in range(matchh.span(0)[1],len(parameterlist[i])):
                  if parameterlist[i][char]!=' ':
                    parameterh=parameterh+parameterlist[i][char]
                parameterh=parameterh.split(',')
              i=i+1

            ##### sectiont 2 #######################################################################

            output=[]                                                                                                               
            output.append(formatter.table(1))                                                                   
            
            if dbflag==1 and qflag==1:
             if hdflag==1:                                                                                                       
                 output.append(formatter.table_row(1))                                                     
                 for element in parameterhd:
                     output.append(formatter.table_cell(1))                                               
                     output.append(formatter.strong(1))                                                     
                     output.append(formatter.text(element))                                               
                     output.append(formatter.strong(0))                                                      
                     output.append(formatter.table_cell(0))                                                
                 output.append(formatter.table_row(0))                                                 
            
             h=parameterdb[0]
             d=parameterdb[1]
             u=parameterdb[2]
             p=parameterdb[3]
           
             query=parameterq

             con=MySQLdb.connect(host=h,user=u,db=d,passwd=p)                      
             c=con.cursor()                                                                                                 
             c.execute(parameterq)                                                                                  
             list=c.fetchall()                                                                                              

             for row in list:                                                                                               
                 output.append(formatter.table_row(1))                                                  
                 i=0                                                                                                                
                 html=0                                                                                                         
                 hidden=0
                 for cell in row:                                                                                               
                     i=i+1                                                                                                          
                     element=str(cell)                                                                                  
                     if hlflag==1:                                                                                          
                         for value in parameterhl:                                                                  
                             if i==int(value):                                                                             
                                 html=1                                                                                         
                                 element='<a class=\"external\" href=\"'+str(cell)+'\"><img src=\"/wiki/rightsidebar/img/moin-www.png\" alt=\"[www]\" width=\"11\" height=\"11\">'+str(cell)+'</a>'                                      
                               
                     if wlflag==1:                                                      
                         for value in parameterwl:                             
                             if i==int(value):                               
                                 html=1
                                 element='<a href=\"'+str(cell)+'\">'+str(cell)+'</a>'     
                      
                     if mflag==1:  
                         for value in parameterm:                          
                             if i==int(value):                               
                                 html=1
                                 element='<a href="mailto:%s">%s</a>'%(str(cell),str(cell))
                     
                     if hflag==1:  
                         for value in parameterh:                          
                             if i==int(value):                               
                                 hidden=1
                                                
                     if html==1:                                                                                            
                         output.append(formatter.table_cell(1))  
                         output.append(formatter.rawHTML(element))
                         output.append(formatter.table_cell(0))
                         html=0
                     elif hidden==1:
                         pass
                         hidden=0
                     else:
                         output.append(formatter.table_cell(1))
                         output.append(formatter.text(element))
                         output.append(formatter.table_cell(0))
                 output.append(formatter.table_row(0)) 
             output.append(formatter.table(0))
             result.extend(output)                                                                                 
             con.close()                                                                                                    

          
          if dbflag==0 or not lines[1:] or qflag==0:

             comment1=''''''
             comment1="Transfer connectiondata and query for correct output!"
             comment2=''''''
             comment2="{{{#!MySQL"
             comment3=''''''
             comment3="DBDATA host,database,[user],[password]"
             comment4=''''''
             comment4="QUERY dbquery"
             comment5=''''''
             comment5="[HEADLINE columnname1,columnname2,...,...]"
             comment6=''''''
             comment6="[HTMLCOLUMN columnnumber1,columnnumber2,...,...]"
             comment7=''''''
             comment7="[WIKICOLUMN columnnumber1,columnnumber2,...,...]"
             comment8=''''''
             comment8="[MAILCOLUMN columnnumber1,columnnumber2,...,...]"
             comment9=''''''
             comment9="[HIDDENCOLUMN columnnumber1,columnnumber2,...,...]"
             comment10=''''''
             comment10="}}}"
             comment11=''''''
             comment11="Replace host, database, user, password, dbquery, columname, columnnumber with practical data!"
             comment12=''''''
             comment12="<b>Take care for lower and upper case. Squared bracket = Optional data, Large letters = Syntax</b>"
          
             comment=[]
             comment=[cr,comment1,cr,cr,comment2,cr,comment3,cr,comment4,cr,comment5,cr,comment6,cr,comment7,cr,comment8,cr,comment9,cr,comment10,cr,cr,comment11,cr,cr,comment12]

             result.extend(comment)
          self.request.write(''.join(result))



