1 |
#!/usr/local/bin/perl -w |
2 |
|
3 |
# www-sql2php |
4 |
# |
5 |
# This is filter which converts www-sql files into php3 |
6 |
# Reports are dumped to STDERR |
7 |
# |
8 |
# 2000-01-10 Dobrica Pavlinusic <dpavlin@rot13.org> |
9 |
# |
10 |
# Warnings: |
11 |
# - parser it's pretty stiff about multi-line www-sql tabs. Only allowed |
12 |
# multiline tags are: print_rows, query |
13 |
# - like operators in PostgreSQL ARE case sensitive -- your appl. may not |
14 |
# work because of that! |
15 |
# - you WILL NEED TO CHANGE links and/or missed form actions by hand! |
16 |
# - www-sql permits variables with slash (-) in name, php doesn't! |
17 |
|
18 |
# configuration part |
19 |
|
20 |
$db_user=$ENV{'USER'}; # connect to database as current user |
21 |
$php_prefix='?php'; # type of php code prefix ('?' is also possible) |
22 |
|
23 |
# end of configuration part |
24 |
|
25 |
|
26 |
# report subroutine (future flag -q will shut this down) |
27 |
sub r { print STDERR @_,"\n"; } |
28 |
|
29 |
# read whole file into $wsql |
30 |
@wsql=<>; $wsql=join("",@wsql); |
31 |
|
32 |
# non-buffered output |
33 |
$|=1; |
34 |
|
35 |
# start relacements |
36 |
$php=$php_prefix; |
37 |
|
38 |
#---- variables convert |
39 |
$wsql=~s#\.sql\?#\.php_que_#smg; # protect vars in a href calls |
40 |
|
41 |
while ($wsql=~ s#(<!\s*sql.+)\@(\w+)\.(\d+)#$1\$row_${2}[$3]#sm) { |
42 |
$conv{var_db_handle}++; |
43 |
} |
44 |
while ($wsql=~ s#(<!\s*sql.+)\?(\w+)#$1\$${2}#sm) { |
45 |
$conv{var_mysql_escape}++; |
46 |
} |
47 |
while ($wsql=~ s,(<!\s*sql.+)\#(\w+),$1rawUrlEncode(\$${2}),sm) { |
48 |
$conv{var_http_escape}++; |
49 |
} |
50 |
|
51 |
|
52 |
#---- database |
53 |
$patt='<!\s*sql\s+database\s+(\w+)\s*>'; |
54 |
while ($wsql=~ m,$patt,mi) { |
55 |
$database=$1; |
56 |
$wsql=~s#$patt##mi; |
57 |
$conv{database}++; |
58 |
} |
59 |
|
60 |
#---- connect FIX: user/password opt. args |
61 |
$patt='<!\s*sql\s+connect\s*>'; |
62 |
while ($wsql=~ m,$patt,mi) { |
63 |
die "can't find database name!" if (!defined($database)); |
64 |
$wsql=~s#$patt# |
65 |
<$php \$conn = pg_connect("dbname=$database user=$db_user"); ?> |
66 |
#mi; |
67 |
$conv{connect}++; |
68 |
} |
69 |
|
70 |
#---- print |
71 |
$patt1='<!\s*sql\s+print\s+([^"][^>]+)\s*>'; # without quotes |
72 |
$patt2='<!\s*sql\s+print\s+("[^"]+")\s*>'; # with quotes |
73 |
while ($wsql=~m,$patt1,mi) { |
74 |
$wsql=~s#$patt1#<$php echo $1 ?>#mi; |
75 |
$conv{print1}++; |
76 |
} |
77 |
while ($wsql=~m,$patt2,mi) { |
78 |
$wsql=~s#$patt2#<$php echo $1 ?>#mi; |
79 |
$conv{print2}++; |
80 |
} |
81 |
|
82 |
#---- query |
83 |
|
84 |
# ugly clugde to replace \" inside sql queryies with ' |
85 |
$wsql=~s#\\"#_qm_#smg; |
86 |
|
87 |
$patt='<!\s*sql\s+query\s+"([^"]+)"\s*(\w*)\s*>'; |
88 |
while ($wsql=~ m,<!\s*sql\s+query\s+"([^"]+)"\s*(\w*)\s*>,smi) { |
89 |
die "can't find database name!" if (!defined($database)); |
90 |
r "can't find connect, you should insert one!" if (! $conv{connect}>0); |
91 |
$query=$1; |
92 |
if (defined($2)) { |
93 |
$q_handle=$2; |
94 |
} else { |
95 |
$q_handle="tmp"; |
96 |
} |
97 |
$wsql=~s#$patt# |
98 |
<$php |
99 |
\$result_${q_handle}=pg_Exec(\$conn,"$query"); |
100 |
\$NUM_FIELDS=pg_NumFields(\$result_${q_handle}); |
101 |
\$NUM_ROWS=pg_NumRows(\$result_${q_handle}); |
102 |
\$AFFECTED_ROWS=pg_CmdTuples(\$result_${q_handle}); |
103 |
// \$INSERT_ID=pg_GetLastOid(\$result_${q_handle}); // FIX |
104 |
if (\$NUM_ROWS > 0) { |
105 |
\$row_${q_handle}=pg_fetch_row(\$result_${q_handle},0); |
106 |
} |
107 |
?> |
108 |
#smi; |
109 |
$handles{$q_handle}++; |
110 |
$conv{query}++; |
111 |
} |
112 |
|
113 |
#---- print_rows |
114 |
$patt='<!\s*sql\s+print_rows\s+(\w+)\s+"([^"]+)"\s*>'; |
115 |
while ($wsql=~ m,$patt,smi) { |
116 |
die "can't find database name!" if (!defined($database)); |
117 |
r "can't find connect, you should insert one!" if (! $conv{connect} > 0); |
118 |
$q_handle=$1; |
119 |
$format=$2; |
120 |
$wsql=~s#$patt# |
121 |
<$php |
122 |
for (\$k = 0; \$k < pg_numrows(\$result_${q_handle}); \$k++) { |
123 |
\$row_${q_handle}=pg_fetch_row(\$result_${q_handle},\$k); |
124 |
echo "$format"; |
125 |
} //for ?> |
126 |
#smi; |
127 |
$handles{$q_handle}++; |
128 |
$conv{print_rows}++; |
129 |
} |
130 |
|
131 |
#---- include |
132 |
while ($wsql=~ m,<!\s*sql\s+include\s+(\S+)\s*>,mi) { |
133 |
$include=$1; |
134 |
$wsql=~s#<!\s*sql\s+include\s+\S+\s*># |
135 |
<$php include("$include"); ?> |
136 |
#mi; |
137 |
r "Convert include file $include also!"; |
138 |
$conv{include}++; |
139 |
} |
140 |
|
141 |
#---- if |
142 |
$patt='<!\s*sql\s+if\s+([^>]+)\s*>'; |
143 |
$wsql=~s#\\>#_gt_#smg; |
144 |
while ($wsql=~ m,$patt,mi) { |
145 |
$wsql=~s#$patt#<$php if ($1): ?>#mi; |
146 |
$conv{if}++; |
147 |
} |
148 |
$wsql=~s#_gt_#>#smg; |
149 |
|
150 |
#---- elsif |
151 |
$patt='<!\s*sql\s+elsif\s+([^>]+)\s*>'; |
152 |
$wsql=~s#\\>#_gt_#smg; |
153 |
while ($wsql=~ m,$patt,mi) { |
154 |
$wsql=~s#$patt#<$php elseif ($1): ?>#mi; |
155 |
$conv{elsif}++; |
156 |
} |
157 |
$wsql=~s#_gt_#>#smg; |
158 |
|
159 |
#---- else |
160 |
$patt='<!\s*sql\s+else\s*>'; |
161 |
while ($wsql=~ m,$patt,mi) { |
162 |
$wsql=~s#$patt#<$php else: ?>#mi; |
163 |
$conv{else}++; |
164 |
} |
165 |
|
166 |
#---- endif |
167 |
$patt='<!\s*sql\s+endif\s*>'; |
168 |
while ($wsql=~ m,$patt,mi) { |
169 |
$wsql=~s#$patt#<$php endif; ?>#mi; |
170 |
$conv{endif}++; |
171 |
} |
172 |
|
173 |
#---- ftime |
174 |
$patt='<!\s*sql\s+ftime\s+([^>]+?)\s*([+-]*)(\d*)\s*>'; |
175 |
while ($wsql=~ m,$patt,mi) { |
176 |
if (defined($2)) { |
177 |
$op=$2; |
178 |
} else { |
179 |
$op="+"; |
180 |
} |
181 |
$wsql=~s#$patt#<$php echo strftime($1,time()${op}$3) ?>#mi; |
182 |
$conv{ftime}++; |
183 |
} |
184 |
|
185 |
#---- qselect |
186 |
$patt='<!\s*sql\s+qselect\s+(\w+)\s+(\w+)\s*(\w*)\s*>'; |
187 |
while ($wsql=~ m,$patt,smi) { |
188 |
die "can't find database name!" if (!defined($database)); |
189 |
r "can't find connect, you should insert one!" if (! $conv{connect} > 0); |
190 |
($q_handle,$formvar)=($1,$2); |
191 |
warn "FIX: default value for qselect isn't impmeneted!" if (defined($3)); |
192 |
$wsql=~s#$patt# |
193 |
<$php |
194 |
echo "<select name=\\\"$formvar\\\">\n"; |
195 |
for (\$k = 0; \$k < pg_numrows(\$result_${q_handle}); \$k++) { |
196 |
\$row_${q_handle}=pg_fetch_row(\$result_${q_handle},\$k); |
197 |
echo "<option value=\\\"\$row_${q_handle}[0]\\\">\$row_${q_handle}[1]</option>\n"; |
198 |
} //for |
199 |
echo "</select>\n"; ?> |
200 |
#smi; |
201 |
$handles{$q_handle}++; |
202 |
$conv{qselect}++; |
203 |
} |
204 |
|
205 |
#---- setdefault |
206 |
$patt='<!\s*sql\s+setdefault\s+(\S+)[\s=](\S+)\s*>'; |
207 |
while ($wsql=~ m,$patt,mi) { |
208 |
($name,$value)=($1,$2); |
209 |
$wsql=~s#$patt#<$php if (!isset(\$$name)) { \$$name = $value; } ?>#mi; |
210 |
$conv{setdefault}++; |
211 |
} |
212 |
|
213 |
#---- set |
214 |
$patt='<!\s*sql\s+set\s+(\S+)[\s=](\S+)\s*>'; |
215 |
while ($wsql=~ m,$patt,mi) { |
216 |
($name,$value)=($1,$2); |
217 |
$wsql=~s#$patt#<$php \$$name = $value ?>#mi; |
218 |
$conv{set}++; |
219 |
} |
220 |
|
221 |
#---- setexpr |
222 |
$patt='<!\s*sql\s+setexpr\s+(\S+)[\s=]([^>]+)\s*>'; |
223 |
while ($wsql=~ m,$patt,mi) { |
224 |
($name,$value)=($1,$2); |
225 |
$wsql=~s#$patt#<$php \$$name = $value ?>#mi; |
226 |
$conv{setexpr}++; |
227 |
} |
228 |
|
229 |
|
230 |
|
231 |
# report some statistics |
232 |
|
233 |
r "Database used: $database" if (defined($database)); |
234 |
r "Handle usage:"; |
235 |
foreach $key (sort keys %handles) { r " $key: $handles{$key}"; } |
236 |
r "Converted www-sql tags:"; |
237 |
foreach $key (sort keys %conv) { r " $key: $conv{$key}"; } |
238 |
r "THERE ARE UNCONVERTED TAGS IN OUTPUT!" if ($wsql=~m,<!\s*sql,); |
239 |
|
240 |
# fix escaped chars bach |
241 |
$wsql=~s#_qm_#\\"#smg; |
242 |
$wsql=~s#_que_#\?#smg; |
243 |
|
244 |
# fix form action which points to .sql |
245 |
$wsql=~s#(form\s+action="*\w+)\.sql"*#$1.php#smg; |
246 |
|
247 |
# dump converted www-sql onto STDOUT |
248 |
print "$wsql"; |
249 |
|
250 |
print '<?php include("debug.inc") ?>'; |
251 |
|