/[wopi]/make_poll.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /make_poll.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (show annotations)
Thu Apr 24 19:39:14 2003 UTC (21 years ago) by dpavlin
Branch: MAIN
Changes since 1.6: +2 -30 lines
File MIME type: text/plain
fix for multiple entries

1 #!/usr/bin/perl -w
2 #
3 # Dobrica Pavlinusic <dpavlin@rot13.org>
4 #
5 # Originally made for proof. during April 2001; later released under GPL v2
6 #
7 # 2003-04-dd general cleanup in preparation of release
8
9 use strict;
10
11 use XML::Parser;
12 use common;
13
14 $|=1;
15
16 my $Usage =<<'End_of_Usage;';
17 I will write usage information here. I promise!
18 End_of_Usage;
19
20 my @Modes = qw(object pass skip);
21
22 my $poll;
23 my $dowarn = 1;
24
25 my $pitanje_nr = 0; # curr. pitanje
26 my $pitanje_tag = ""; # originalni oblik broja pitanja
27 my $page_nr = 1; # prvo pitanje na strani
28
29 my $p_suffix=""; # if more than one box per question
30
31 my $curr_suffix=""; # trenutni suffix
32
33 my @stack_pit; # stack pitanja (pitanje, suffix)
34
35 my @sql_create = ("id serial",
36 "http_referer character varying(500)",
37 "remote_addr character varying(15)",
38 "user_agent character varying(300)",
39 "unesen timestamp DEFAULT now()",
40 "member_id int4 NOT NULL"
41 );
42 my @sql_update;
43 my @last_sql_update;
44 my @prelast_sql_update;
45
46 my @php_addon; # php code to add on page header
47
48 my ($last_fn,$last_page);
49
50 # this is unique prefix for this installation
51 my $prefix="wopi_";
52
53 # this is usename in database
54 my $db_user="dpavlin";
55
56 #------------------------------------------------------------------
57
58 sub suck_file {
59 my $file = shift @_;
60 open(H,$file) || die "can't open '$file': $!";
61 my $content;
62 while (<H>) { $content .= $_; } ;
63 close(H);
64 return $content;
65 }
66
67 my $html_header=suck_file("header.html");
68 my $html_separator=suck_file("separator.html");
69 my $html_next=suck_file("next.html");
70 my $html_footer=suck_file("footer.html");
71
72 #------------------------------------------------------------------
73
74 sub php_header {
75 my ($page_nr,@sql_update) = @_;
76 my $out='<?php
77 include_once("common.php");
78 if (isset($update)) {
79 $member_id=id_decode($a);
80 ';
81 $out.=$php_addon[$page_nr-2] if (defined $php_addon[$page_nr-2]);
82 $out.='
83 $sql="update '.$poll.' set '.join(",\n",@sql_update).',
84 do_stranice=\'$PHP_SELF\'
85 where id=$id";
86 # print "<pre>$sql</pre>";
87 $result=pg_Exec($conn,fix_sql($sql));
88 } elseif($do_stranice != $PHP_SELF && isset($do_uri) && isset($a)) {
89 Header("Location: $do_uri?a=$a");
90 exit;
91 }
92 ?>';
93 return $out;
94 }
95
96 #------------------------------------------------------------------
97
98 # first, define some constants
99 my $common_php = suck_file("common.php");
100
101 #------------------------------------------------------------------
102
103 my $head_php=suck_file("head.php");
104
105 #------------------------------------------------------------------
106
107 my $html_kraj=suck_file("thanks.html");
108
109 #------------------------------------------------------------------
110
111 while (defined($ARGV[0]) and $ARGV[0] =~ /^-/) {
112 my $opt = shift;
113
114 if ($opt eq '-h') {
115 print $Usage;
116 exit;
117 }
118 } # End of option processing
119
120 my $xmlfile = shift;
121
122 die "No poll xml file provided!\n$Usage" unless defined $xmlfile;
123
124 die "Can't read $xmlfile" unless -r $xmlfile;
125
126 if (defined $poll) {
127 die "$poll isn't a directory" unless -d $poll;
128 }
129 else {
130 $xmlfile =~ m!([^/.]+)(?:\.[^/.]*)?$!;
131 $poll = $1;
132 if (-e $poll) {
133 die "$poll exists but isn't a directory"
134 unless -d $poll;
135 }
136 else {
137 mkdir $poll, 0755;
138 }
139 }
140
141 my $in_poll = 0;
142 my $after_head = 0;
143
144 my $Mode = 0;
145 my $Mode_level = 0;
146
147 my $Text;
148 my $Markedup_Text;
149 my $Object;
150 my @Ostack = ();
151
152 #my $intext = 0;
153 my $closure;
154 my @closure_stack = ();
155
156 #my $style_link = '';
157
158 #my $index = 'index.html';
159 #my @slidetitle;
160 my $body;
161 #my $inlist = 0;
162
163 #my @Titles;
164
165 my $header;
166
167 my $page_number = 0;
168
169 my $p = new XML::Parser(ErrorContext => 3,
170 Handlers => {Start => \&starthndl,
171 End => \&endhndl,
172 Char => \&text});
173 $p->parsefile($xmlfile);
174
175 #----------------------------------------------------------
176
177 # dump last php page....
178
179 print "p[$page_nr] ";
180
181 open(PAGE, ">$poll/$last_fn") or die "Couldn't open $last_fn for writing:\n$!";
182 print PAGE php_header($page_nr,@prelast_sql_update);
183 my $next_fn=sprintf("%02d.php",$page_nr);
184 $last_page=~s/##NEXTPAGE##/$next_fn/;
185 print PAGE $last_page;
186 close(PAGE);
187
188 $page_nr++;
189 open(PAGE, ">$poll/$next_fn") or die "Couldn't open $next_fn for writing:\n$!";
190 print PAGE php_header($page_nr,@last_sql_update);
191 print PAGE "$html_header $html_kraj $html_footer";
192 close(PAGE);
193
194 # dump sql structure
195
196 open(SQL,">$poll/$poll.sql") || die "$poll.sql: $!";
197 print SQL "create table poslani ( member_id int4 not null, unesen timestamp default now() );\n";
198 print SQL "create table $poll (do_stranice text default null, ",join(",\n",@sql_create),");\n";
199 close(SQL);
200
201 # dump common.php
202
203 open(PHP,">$poll/common.php") || die "common.php: $!";
204 $common_php =~ s/##DB##/$poll/g;
205 my $db_name = $prefix.$poll;
206 $common_php =~ s/##DB_NAME##/$db_name/g;
207 $common_php =~ s/##PREFIX##/$prefix/g;
208 $common_php =~ s/##DB_USER##/$db_user/g;
209 $common_php =~ s/##PREFIX##/$prefix/g;
210 my $members_db = $prefix."members";
211 $common_php =~ s/##MEMBERS_DB##/$members_db/g;
212
213 print PHP $common_php;
214 close(PHP);
215
216 open(PHP,">$poll/head.php") || die "head.php: $!";
217 my $max_page = $page_nr - 1;
218 $head_php=~ s/##MAXPAGE##/$max_page/;
219 $head_php=~ s/##TEXT##/Ispunili ste %02d%% ankete/;
220 print PHP $head_php;
221 close(PHP);
222
223 # 01.php -> index.php
224 rename "$poll/01.php","$poll/index.php" || die "can't rename '$poll/01.php' to index.php";
225
226 ################
227 ## End of main
228 ################
229
230 # return unique name of pitanje
231 sub new_pit {
232 my $out="p".$pitanje_nr.$p_suffix;
233 $curr_suffix=$p_suffix;
234 $p_suffix++;
235 return $out;
236 }
237
238 # current pitanje
239 sub curr_pit {
240 return "p".$pitanje_nr.$curr_suffix;
241 }
242
243 #----------------------------------------------------------
244
245 sub starthndl {
246 my ($xp, $el, %atts) = @_;
247
248 # return unless ($in_poll or $el eq 'slideshow');
249
250 unless ($in_poll) {
251 $in_poll = $xp->depth + 1;
252 return;
253 }
254
255 if ($Mode) {
256
257 if ($Mode eq 'pass') {
258 $Markedup_Text .= "\n" . $xp->recognized_string;
259 }
260 elsif ($Mode eq 'object') {
261 push(@Ostack, $Object);
262
263 $Object = {_Atts => \%atts,
264 _Text => ''
265 };
266 bless $Object, "Slideobj::$el";
267 }
268
269 # skip does nothing
270 return;
271 }
272
273 unless ($after_head) {
274 if ($el eq 'head') {
275 $after_head = 1;
276 start_mode($xp, 'object');
277
278 push(@closure_stack, $closure);
279 $closure =
280 sub {
281 my ($xp, $text) = @_;
282
283 unless (defined $text) {
284
285 $header = $Object;
286 }
287 };
288
289 return;
290 }
291
292 # die "The head element must be the first thing in the slideshow";
293 }
294
295
296 my $new_closure;
297
298 my $subname = "Poll::$el";
299
300 if (defined &$subname) {
301 no strict 'refs';
302
303 &$subname($xp, $el, \%atts, \$new_closure);
304 }
305 else {
306 $body .= x($xp->recognized_string);
307 $new_closure =
308 sub {
309 my ($xp, $text) = @_;
310
311 if (defined $text) {
312 $body .= x($text);
313 }
314 else {
315 $body .= x("</$el>");
316 }
317 };
318 }
319
320 push(@closure_stack, $closure);
321 $closure = $new_closure;
322 } # End starthndl
323
324 sub endhndl {
325 my ($xp, $el) = @_;
326
327 return unless $in_poll;
328
329 my $lev = $xp->depth;
330
331 if ($lev == $in_poll - 1) {
332 $in_poll = 0;
333 $xp->finish;
334 return;
335 }
336
337 if ($Mode_level == $lev) {
338
339 if ($Mode eq 'pass') {
340 &$closure($xp, $Markedup_Text)
341 if (defined $closure);
342 }
343
344 $Mode = $Mode_level = 0;
345 }
346
347 if ($Mode) {
348 if ($Mode eq 'pass') {
349 $Markedup_Text .= "</$el>";
350 }
351 elsif ($Mode eq 'object') {
352 my $this = $Object;
353 if (2 == keys %$this) {
354 $this = $this->{_Text};
355 }
356
357 $Object = pop(@Ostack);
358
359 my $slot = $Object->{$el};
360 if (defined $slot) {
361 if (ref($slot) eq 'ARRAY') {
362 push(@$slot, $this);
363 }
364 else {
365 $Object->{$el} = [$slot, $this];
366 }
367 }
368 else {
369 $Object->{$el} = $this;
370 }
371 }
372
373 return;
374 }
375
376 &$closure($xp)
377 if defined $closure;
378
379 $closure = pop(@closure_stack);
380 } # End endhndl
381
382 #----------------------------------------------------------
383
384 sub text {
385 my ($xp, $data) = @_;
386
387 return unless $in_poll;
388
389 if ($Mode ) {
390
391 if ($Mode eq 'pass') {
392 my $safe = sgml_escape($data);
393
394 $Text .= $safe;
395 $Markedup_Text .= $safe;
396 }
397 elsif ($Mode eq 'object') {
398 $Object->{_Text} .= $data
399 if $data =~ /\S/;
400 }
401
402 return;
403 }
404
405 &$closure($xp, sgml_escape($data))
406 if (defined $closure);
407
408 } # End text
409
410 sub start_mode {
411 my ($xp, $mode) = @_;
412
413 if ($mode eq 'pass') {
414 $Text = '';
415 $Markedup_Text = '';
416 }
417 elsif ($mode eq 'object') {
418 $Object = {_Atts => undef,
419 _Text => undef
420 };
421 }
422
423 $Mode = $mode;
424 $Mode_level = $xp->depth;
425 } # End start_mode
426
427 sub sgml_escape {
428 my ($str) = @_;
429
430 $str =~ s/\&/\&amp;/g;
431 $str =~ s/</\&lt;/g;
432 $str =~ s/>/\&gt;/g;
433
434 $str;
435 } # End sgml_escape
436
437
438 ################################################################
439
440 package Poll;
441
442 sub page {
443 package main;
444
445 my ($xp, $el, $attref, $ncref) = @_;
446
447 $$ncref = sub {
448 my ($xp, $text) = @_;
449
450 if (! defined $text) {
451
452 print "p[$page_nr] ";
453
454 if (defined $last_fn) {
455 open(PAGE, ">$poll/$last_fn") or die "Couldn't open $last_fn for writing:\n$!";
456 print PAGE php_header($page_nr,@prelast_sql_update);
457 my $next_fn=sprintf("%02d.php",$page_nr);
458 $last_page=~s/##NEXTPAGE##/$next_fn/;
459 print PAGE $last_page;
460 close(PAGE);
461
462 }
463 @prelast_sql_update=@last_sql_update;
464 @last_sql_update=@sql_update;
465 @sql_update = ();
466
467 $last_fn=sprintf("%02d.php",$page_nr);
468 $last_page="$html_header $body $html_next $html_footer";
469 # delete vars for next page
470 $page_nr++;
471 $body="";
472 }
473 }
474 } # page
475
476 sub nr {
477 package main;
478
479 my ($xp, $el, $attref, $ncref) = @_;
480
481 $pitanje_tag="";
482
483 $$ncref = sub {
484 my ($xp, $text) = @_;
485 if (defined($text)) {
486 $body.=x($text);
487 chomp $text;
488 $pitanje_tag .= x($text);
489 } else {
490 $pitanje_nr = $pitanje_tag;
491 $pitanje_nr =~ s/[^0-9a-zA-Z]//g;
492 print "$pitanje_nr ";
493 }
494 $p_suffix="";
495 };
496 } # nr
497
498
499 sub hr {
500 $body .= "<br></td></tr>$html_separator<tr><td></td><td><br>";
501 }
502
503 sub br {
504 $body .= "<br>\n";
505 }
506
507 sub pit {
508 package main;
509
510 my ($xp, $el, $attref, $ncref) = @_;
511
512 $body.="<p>";
513
514 $$ncref = sub {
515 my ($xp, $text) = @_;
516
517 if (defined $text) {
518 $body.=x($text);
519 } else {
520 $body.="</p>";
521 }
522 }
523 }
524
525 sub podpit {
526 package main;
527
528 my ($xp, $el, $attref, $ncref) = @_;
529
530 $body.='<table width="100%" cellspacing="0" cellpadding="2" border="0">';
531 $$ncref = sub {
532 my ($xp, $text) = @_;
533
534 if (defined $text) {
535 $body.=x($text);
536 } else {
537 $body.="</table>";
538 }
539 }
540 }
541
542
543 sub odg {
544 package main;
545
546 my ($xp, $el, $attref, $ncref) = @_;
547
548 $body .= "<p>";
549
550 $$ncref = sub {
551 my ($xp, $text) = @_;
552
553 if (defined $text) {
554 $body .= x($text);
555 } else {
556 $body .= "</p>";
557 }
558 }
559 }
560
561 sub php {
562 package main;
563 my ($xp, $el, $attref, $ncref) = @_;
564
565 $body.="<?php\n";
566
567 $$ncref = sub {
568 my ($xp, $text) = @_;
569
570 if (defined $text) {
571 $text=~s/ lt / < /g;
572 $text=~s/ le / <= /g;
573 $text=~s/ gt / > /g;
574 $text=~s/ ge / >= /g;
575 $body.=x($text);
576 } else {
577 $body.="\n?>\n";
578 }
579 }
580 }
581
582 sub dropdown {
583 package main;
584
585 my ($xp, $el, $attref, $ncref) = @_;
586
587 my @dropdown_data;
588
589 $$ncref = sub {
590 my ($xp, $text) = @_;
591
592 if (defined $text) {
593 chomp $text;
594 $text=~s/^\s*//g;
595 $text=~s/^[\d\.\s]+//g;
596 $text=~s/\s*$//g;
597 push @dropdown_data,x($text) if ($text ne "");
598 } else {
599 my $opt;
600 my $id=1;
601 my $p=new_pit();
602 $body.="<select name=$p >\n";
603 $body.="<option value=null>-</option>\n";
604 foreach $opt (@dropdown_data) {
605 if (defined($opt) && $opt ne "") {
606 $body.="<option value=$id>$opt</option>\n";
607 $id++;
608 }
609 }
610 $body.="</select>\n";
611
612 push @sql_create,"$p int4";
613 push @sql_update,"$p=\$$p";
614 }
615 }
616 }
617
618 sub textbox {
619 package main;
620 my ($xp, $el, $attref, $ncref) = @_;
621
622 $$ncref = sub {
623 my ($xp, $text) = @_;
624 my $size=$attref->{size};
625 $size = 25 if (! defined $size || $size == 0); # default
626 my $p=new_pit();
627 $body.="<input type=text name=$p size=".x($size)." >\n";
628 push @sql_create,"$p text";
629 push @sql_update,"$p='\$$p'";
630 }
631 }
632
633 sub radiobuttons_tab {
634 package main;
635 my ($xp, $el, $attref, $ncref) = @_;
636
637 $$ncref = sub {
638 my ($xp, $text) = @_;
639 if (! defined $text) {
640 my $nr=$attref->{nr};
641 my $p=new_pit();
642 for (my $i=1; $i<=$nr; $i++) {
643 $body.="<td><input type=radio name=$p value=$i></td> ";
644 }
645 push @sql_create,"$p int4";
646 push @sql_update,"$p=\$$p";
647 }
648 }
649 }
650
651 sub radiobuttons {
652 package main;
653 my ($xp, $el, $attref, $ncref) = @_;
654
655 my @radiobuttons_data;
656
657 $$ncref = sub {
658 my ($xp, $text) = @_;
659
660 if (defined $text) {
661 chomp $text;
662 $text=~s/^\s*//g;
663 $text=~s/^[\d\.\s]+//g;
664 $text=~s/\s*$//g;
665 push @radiobuttons_data,x($text) if ($text ne "");
666 } else {
667 my $opt;
668 my $p=new_pit();
669 my $id=1;
670 foreach $opt (@radiobuttons_data) {
671 if (defined($opt) && $opt ne "") {
672 $body.="<input type=radio name=$p value=$id> $opt<br>\n";
673 $id++;
674 }
675 }
676 push @sql_create,"$p int4";
677 push @sql_update,"$p=\$$p";
678 }
679 }
680 }
681 sub checkbox {
682 package main;
683 my ($xp, $el, $attref, $ncref) = @_;
684
685 $$ncref = sub {
686 my ($xp, $text) = @_;
687 my $p=new_pit();
688 $body.="<input type=checkbox name=$p >\n";
689 push @sql_create,"$p text";
690 push @sql_update,"$p='\$$p'";
691 }
692 }
693
694 sub checkboxes {
695 package main;
696
697 my ($xp, $el, $attref, $ncref) = @_;
698
699 my @checkboxes_data;
700
701 $$ncref = sub {
702 my ($xp, $text) = @_;
703
704
705 if (defined $text) {
706 chomp $text;
707 $text=~s/^\s*//g;
708 $text=~s/^[\d\.\s]+//g;
709 $text=~s/\s*$//g;
710 push @checkboxes_data,x($text) if ($text ne "");
711 } else {
712 my $opt;
713 my $base_p=new_pit();
714 my $id=1;
715
716 my $before=$attref->{before};
717 my $after=$attref->{after};
718 my $middle=$attref->{middle};
719 if (! $before && ! $after && ! $middle) {
720 $middle="&nbsp;";
721 $after="<br>";
722 }
723 my $hide_description=$attref->{hide_description};
724
725 foreach $opt (@checkboxes_data) {
726 if (defined($opt) && $opt ne "") {
727 $p=$base_p."_".$id;
728 $id++;
729 $body .= x($before) if ($before);
730 $body.="<input type=checkbox name=$p>";
731 $body .= x($middle) if ($middle);
732 $body .= "$opt" if (! $hide_description);
733 $body .= x($after) if ($after);
734 $body.="\n";
735
736 push @sql_create,"$p boolean";
737 push @sql_update,"$p=\$$p";
738 }
739 }
740 $php_addon[$page_nr].="fix_checkboxes($base_p,".($id-1).");";
741
742 }
743 }
744 }
745
746 # read configuration data
747 #
748 # FIX: write actually this :-)
749 sub config {
750 package main;
751 my ($xp, $el, $attref, $ncref) = @_;
752
753 $$ncref = sub {
754 my ($xp, $text) = @_;
755 $db_user=x($attref->{db_user});
756 $prefix=x($attref->{prefix});
757 }
758 }
759
760 #---------------------------------------------------------------

  ViewVC Help
Powered by ViewVC 1.1.26