Attachment 'MySQL.py'
Download 1 """
2 MoinMoin - Processor for MySQL data
3
4 @copyright: 2004 by Alexander Fischer <mail@fischer-alexander.net>
5 @license: GNU GPL, see http://www.gnu.org/licenses/gpl for details
6
7
8 Take care that a MySQL based database for source is assumed.
9 For additional information performing these processor without any data given.
10
11 """
12
13 from MoinMoin import wikiutil, config
14 import MySQLdb
15 import re
16
17
18 def process(request, formatter, lines):
19
20 result=[request.formatter.paragraph(0)]
21 cr=request.formatter.linebreak(0)
22
23 #### initialize flags ###################################################################
24 dbflag=0
25 qflag=0
26 hdflag=0
27 wlflag=0
28 hlflag=0
29 mflag=0
30 hflag=0
31
32 ##### section1 ###########################################################################
33
34 if lines[1:]:
35 parameterlist=[]
36 for index in range(len(lines[1:])):
37 parameterlist.append(lines[1:][index])
38 i=0
39 for parameterstring in parameterlist :
40 regexdb=re.compile("DBDATA")
41 matchdb=regexdb.search(parameterstring)
42 if matchdb:
43 dbflag=1
44 parameterdb=''''''
45 for char in range(matchdb.span(0)[1],len(parameterlist[i])):
46 if parameterlist[i][char]!=' ':
47 parameterdb=parameterdb+parameterlist[i][char]
48 parameterdb=parameterdb.split(',')
49
50
51 regexq=re.compile("QUERY")
52 matchq=regexq.search(parameterstring)
53 if matchq:
54 qflag=1
55 parameterq=''''''
56 for char in range(matchq.span(0)[1],len(parameterlist[i])):
57 parameterq=parameterq+parameterlist[i][char]
58
59
60 regexhd=re.compile("HEADLINE")
61 matchhd=regexhd.search(parameterstring)
62 if matchhd:
63 hdflag=1
64 parameterhd=''''''
65 for char in range(matchhd.span(0)[1],len(parameterlist[i])):
66 if parameterlist[i][char]!=' ':
67 parameterhd=parameterhd+parameterlist[i][char]
68 parameterhd=parameterhd.split(',')
69
70
71 regexwl=re.compile("WIKICOLUMN")
72 matchwl=regexwl.search(parameterstring)
73 if matchwl:
74 wlflag=1
75 parameterwl=''''''
76 for char in range(matchwl.span(0)[1],len(parameterlist[i])):
77 if parameterlist[i][char]!=' ':
78 parameterwl=parameterwl+parameterlist[i][char]
79 parameterwl=parameterwl.split(',')
80
81
82 regexhl=re.compile("HTMLCOLUMN")
83 matchhl=regexhl.search(parameterstring)
84 if matchhl:
85 hlflag=1
86 parameterhl=''''''
87 for char in range(matchhl.span(0)[1],len(parameterlist[i])):
88 if parameterlist[i][char]!=' ':
89 parameterhl=parameterhl+parameterlist[i][char]
90 parameterhl=parameterhl.split(',')
91
92
93 regexm=re.compile("MAILCOLUMN")
94 matchm=regexm.search(parameterstring)
95 if matchm:
96 mflag=1
97 parameterm=''''''
98 for char in range(matchm.span(0)[1],len(parameterlist[i])):
99 if parameterlist[i][char]!=' ':
100 parameterm=parameterm+parameterlist[i][char]
101 parameterm=parameterm.split(',')
102
103
104 regexh=re.compile("HIDDENCOLUMN")
105 matchh=regexh.search(parameterstring)
106 if matchh:
107 hflag=1
108 parameterh=''''''
109 for char in range(matchh.span(0)[1],len(parameterlist[i])):
110 if parameterlist[i][char]!=' ':
111 parameterh=parameterh+parameterlist[i][char]
112 parameterh=parameterh.split(',')
113 i=i+1
114
115 ##### sectiont 2 #######################################################################
116
117 output=[]
118 output.append(formatter.table(1))
119
120 if dbflag==1 and qflag==1:
121 if hdflag==1:
122 output.append(formatter.table_row(1))
123 for element in parameterhd:
124 output.append(formatter.table_cell(1))
125 output.append(formatter.strong(1))
126 output.append(formatter.text(element))
127 output.append(formatter.strong(0))
128 output.append(formatter.table_cell(0))
129 output.append(formatter.table_row(0))
130
131 h=parameterdb[0]
132 d=parameterdb[1]
133 u=parameterdb[2]
134 p=parameterdb[3]
135
136 query=parameterq
137
138 con=MySQLdb.connect(host=h,user=u,db=d,passwd=p)
139 c=con.cursor()
140 c.execute(parameterq)
141 list=c.fetchall()
142
143 for row in list:
144 output.append(formatter.table_row(1))
145 i=0
146 html=0
147 hidden=0
148 for cell in row:
149 i=i+1
150 element=str(cell)
151 if hlflag==1:
152 for value in parameterhl:
153 if i==int(value):
154 html=1
155 element='<a class=\"external\" href=\"'+str(cell)+'\"><img src=\"/wiki/rightsidebar/img/moin-www.png\" alt=\"[www]\" width=\"11\" height=\"11\">'+str(cell)+'</a>'
156
157 if wlflag==1:
158 for value in parameterwl:
159 if i==int(value):
160 html=1
161 element='<a href=\"'+str(cell)+'\">'+str(cell)+'</a>'
162
163 if mflag==1:
164 for value in parameterm:
165 if i==int(value):
166 html=1
167 element='<a href="mailto:%s">%s</a>'%(str(cell),str(cell))
168
169 if hflag==1:
170 for value in parameterh:
171 if i==int(value):
172 hidden=1
173
174 if html==1:
175 output.append(formatter.table_cell(1))
176 output.append(formatter.rawHTML(element))
177 output.append(formatter.table_cell(0))
178 html=0
179 elif hidden==1:
180 pass
181 hidden=0
182 else:
183 output.append(formatter.table_cell(1))
184 output.append(formatter.text(element))
185 output.append(formatter.table_cell(0))
186 output.append(formatter.table_row(0))
187 output.append(formatter.table(0))
188 result.extend(output)
189 con.close()
190
191
192 if dbflag==0 or not lines[1:] or qflag==0:
193
194 comment1=''''''
195 comment1="Transfer connectiondata and query for correct output!"
196 comment2=''''''
197 comment2="{{{#!MySQL"
198 comment3=''''''
199 comment3="DBDATA host,database,[user],[password]"
200 comment4=''''''
201 comment4="QUERY dbquery"
202 comment5=''''''
203 comment5="[HEADLINE columnname1,columnname2,...,...]"
204 comment6=''''''
205 comment6="[HTMLCOLUMN columnnumber1,columnnumber2,...,...]"
206 comment7=''''''
207 comment7="[WIKICOLUMN columnnumber1,columnnumber2,...,...]"
208 comment8=''''''
209 comment8="[MAILCOLUMN columnnumber1,columnnumber2,...,...]"
210 comment9=''''''
211 comment9="[HIDDENCOLUMN columnnumber1,columnnumber2,...,...]"
212 comment10=''''''
213 comment10="}}}"
214 comment11=''''''
215 comment11="Replace host, database, user, password, dbquery, columname, columnnumber with practical data!"
216 comment12=''''''
217 comment12="<b>Take care for lower and upper case. Squared bracket = Optional data, Large letters = Syntax</b>"
218
219 comment=[]
220 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]
221
222 result.extend(comment)
223 request.write(''.join(result))
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.You are not allowed to attach a file to this page.