/[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

Diff of /make_poll.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.3 by dpavlin, Thu Apr 24 16:43:09 2003 UTC revision 1.9 by dpavlin, Wed Sep 24 20:35:48 2003 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl -w  #!/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;  use strict;
10    
11  use XML::Parser;  use XML::Parser;
12  use Text::Iconv;  use common;
   
 # output charset  
 my $charset='ISO-8859-2';  
   
 Text::Iconv->raise_error(0);    # Conversion errors raise exceptions  
 my $from_utf8 = Text::Iconv->new('UTF8', $charset);  
 sub x {  
         return $from_utf8->convert($_[0]);  
 }  
13    
14  $|=1;  $|=1;
15    
16  my $Usage =<<'End_of_Usage;';  my $Usage =<<'End_of_Usage;';
17  slides [-h] [-d dir] [-mode mode] slide-doc  I will write usage information here. I promise!
   
 Convert a slideshow document into html, with a separate html document  
 for each slide and an index to all of them.  
   
         -h      Print this message and exit  
   
         -w      warn about unrecognized tags  
   
         -d      Use dir as directory into which to write html pages. Defaults  
                 to basename of supplied doc file.  
   
         -mode   Output mode. Choices are html, html-style. Default is  
                 html-style.  
   
18  End_of_Usage;  End_of_Usage;
19    
20  my @Modes = qw(object pass skip);  my @Modes = qw(object pass skip);
21    
22  my $dir;  my $poll;
23  my $dowarn = 1;  my $dowarn = 1;
 my $dostyle = 0;  
24    
25  my $pitanje_nr = 0;             # curr. pitanje  my $pitanje_nr = 0;             # curr. pitanje
26  my $pitanje_tag = "";           # originalni oblik broja pitanja  my $pitanje_tag = "";           # originalni oblik broja pitanja
# Line 72  my $prefix="wopi_"; Line 53  my $prefix="wopi_";
53  # this is usename in database  # this is usename in database
54  my $db_user="dpavlin";  my $db_user="dpavlin";
55    
56    # This option allows users to fill poll without using invitation URL.
57    # That also means it's unpossible for them to return to exiting poll
58    # because they don't have thair own unique ID. Howver, it enables simple
59    # polls to be conducted by just publishing URL to them.
60    my $without_invitation=0;
61    
62  #------------------------------------------------------------------  #------------------------------------------------------------------
63    
64  sub suck_file {  sub suck_file {
# Line 93  my $html_footer=suck_file("footer.html") Line 80  my $html_footer=suck_file("footer.html")
80  sub php_header {  sub php_header {
81  my ($page_nr,@sql_update) = @_;  my ($page_nr,@sql_update) = @_;
82  my $out='<?php  my $out='<?php
83          include("common.php");          include_once("common.php");
84          if (isset($update)) {          if (isset($update)) {
85                  $member_id=id_decode($a);                  $member_id=id_decode($a);
86                  ';                  ';
87  $out.=$php_addon[$page_nr-2] if (defined $php_addon[$page_nr-2]);  $out.=$php_addon[$page_nr-2] if (defined $php_addon[$page_nr-2]);
88  $out.='  $out.='
89                  $sql="update '.$dir.' set '.join(",\n",@sql_update).',                  $sql="update '.$poll.' set '.join(",\n",@sql_update).',
90                          do_stranice=\'$PHP_SELF\'                          do_stranice=\'$PHP_SELF\'
91                          where id=$id";                          where id=$id";
92  #               print "<pre>$sql</pre>";  #               print "<pre>$sql</pre>";
93                  $result=pg_Exec($conn,fix_sql($sql));                  $result=pg_Exec($conn,fix_sql($sql));
94          } elseif($do_stranice != $PHP_SELF) {          } elseif($do_stranice != $PHP_SELF && isset($do_uri) && isset($a)) {
95                  Header("Location: $do_uri?a=$a");                  Header("Location: $do_uri?a=$a");
96                  exit;                  exit;
97          }          }
# Line 134  while (defined($ARGV[0]) and $ARGV[0] =~ Line 121  while (defined($ARGV[0]) and $ARGV[0] =~
121      print $Usage;      print $Usage;
122      exit;      exit;
123    }    }
   elsif ($opt eq '-d') {  
     $dir = shift;  
   }  
   elsif ($opt eq '-w') {  
     $dowarn = 1;  
   }  
   elsif ($opt eq '-mode') {  
     my $marg = shift;  
     if ($marg eq 'html') {  
       $dostyle = 0;  
     }  
     else {  
       die "Unrecognized mode: $marg\n$Usage";  
     }  
   }  
   else {  
     die "Unrecognized option: $opt\n$Usage";  
   }  
124  }  # End of option processing  }  # End of option processing
125    
126  my $docfile = shift;  my $xmlfile = shift;
127    
128  die "No docfile provided:\n$Usage" unless defined $docfile;  die "No poll xml file provided!\n$Usage" unless defined $xmlfile;
129    
130  die "Can't read $docfile" unless -r $docfile;  die "Can't read $xmlfile" unless -r $xmlfile;
131    
132  if (defined $dir) {  if (defined $poll) {
133    die "$dir isn't a directory" unless -d $dir;    die "$poll isn't a directory" unless -d $poll;
134  }  }
135  else {  else {
136    $docfile =~ m!([^/.]+)(?:\.[^/.]*)?$!;    $xmlfile =~ m!([^/.]+)(?:\.[^/.]*)?$!;
137    $dir = $1;    $poll = $1;
138    if (-e $dir) {    if (-e $poll) {
139      die "$dir exists but isn't a directory"      die "$poll exists but isn't a directory"
140        unless -d $dir;        unless -d $poll;
141    }    }
142    else {    else {
143      mkdir $dir, 0755;      mkdir $poll, 0755;
144    }    }
145  }  }
146    
147  my $in_slideshow = 0;  my $in_poll = 0;
148  my $after_head = 0;  my $after_head = 0;
149    
150  my $Mode = 0;  my $Mode = 0;
# Line 186  my $Markedup_Text; Line 155  my $Markedup_Text;
155  my $Object;  my $Object;
156  my @Ostack = ();  my @Ostack = ();
157    
158  my $intext = 0;  #my $intext = 0;
159  my $closure;  my $closure;
160  my @closure_stack = ();  my @closure_stack = ();
161    
162  my $style_link = '';  #my $style_link = '';
163    
164  my $index = 'index.html';  #my $index = 'index.html';
165  my @slidetitle;  #my @slidetitle;
166  my $body;  my $body;
167  my $inlist = 0;  #my $inlist = 0;
168    
169  my @Titles;  #my @Titles;
170    
171  my $header;  my $header;
172    
 my $prolog = "<html><head>\n";  
 $prolog .= "<!-- Generated by $0 on " . gmtime() . " GMT -->\n";  
   
173  my $page_number = 0;  my $page_number = 0;
174    
175  my $p = new XML::Parser(ErrorContext => 3,  my $p = new XML::Parser(ErrorContext => 3,
176                          Handlers => {Start => \&starthndl,                          Handlers => {Start => \&starthndl,
177                                       End   => \&endhndl,                                       End   => \&endhndl,
178                                       Char  => \&text});                                       Char  => \&text});
179  $p->parsefile($docfile);  $p->parsefile($xmlfile);
180    
181  #----------------------------------------------------------  #----------------------------------------------------------
182    
# Line 218  $p->parsefile($docfile); Line 184  $p->parsefile($docfile);
184    
185  print "p[$page_nr] ";  print "p[$page_nr] ";
186    
187  open(PAGE, ">$dir/$last_fn") or die "Couldn't open $last_fn for writing:\n$!";  open(PAGE, ">$poll/$last_fn") or die "Couldn't open $last_fn for writing:\n$!";
188  print PAGE php_header($page_nr,@prelast_sql_update);  print PAGE php_header($page_nr,@prelast_sql_update);
189  my $next_fn=sprintf("%02d.php",$page_nr);  my $next_fn=sprintf("%02d.php",$page_nr);
190  $last_page=~s/##NEXTPAGE##/$next_fn/;  $last_page=~s/##NEXTPAGE##/$next_fn/;
# Line 226  print PAGE $last_page; Line 192  print PAGE $last_page;
192  close(PAGE);  close(PAGE);
193    
194  $page_nr++;  $page_nr++;
195  open(PAGE, ">$dir/$next_fn") or die "Couldn't open $next_fn for writing:\n$!";  open(PAGE, ">$poll/$next_fn") or die "Couldn't open $next_fn for writing:\n$!";
196  print PAGE php_header($page_nr,@last_sql_update);  print PAGE php_header($page_nr,@last_sql_update);
197  print PAGE "$html_header $html_kraj $html_footer";  print PAGE "$html_header $html_kraj $html_footer";
198  close(PAGE);  close(PAGE);
199    
200  # dump sql structure  # dump sql structure
201    
202  open(SQL,">$dir/$dir.sql") || die "$dir.sql: $!";  open(SQL,">$poll/$poll.sql") || die "$poll.sql: $!";
203    print SQL "drop database ".$prefix.$poll.";\n";
204    print SQL "create database ".$prefix.$poll.";\n";
205    print SQL "\\connect ".$prefix.$poll.";\n";
206  print SQL "create table poslani ( member_id int4 not null, unesen timestamp default now() );\n";  print SQL "create table poslani ( member_id int4 not null, unesen timestamp default now() );\n";
207  print SQL "create table $dir (do_stranice text default null, ",join(",\n",@sql_create),");\n";  print SQL "create table $poll (do_stranice text default null, ",join(",\n",@sql_create),");\n";
208  close(SQL);  close(SQL);
209    
210  # dump common.php  # dump common.php
211    
212  open(PHP,">$dir/common.php") || die "common.php: $!";  open(PHP,">$poll/common.php") || die "common.php: $!";
213  $common_php =~ s/##DB##/$dir/g;  $common_php =~ s/##DB##/$poll/g;
214  my $db_name = $prefix.$dir;  my $db_name = $prefix.$poll;
215  $common_php =~ s/##DB_NAME##/$db_name/g;  $common_php =~ s/##DB_NAME##/$db_name/g;
216  $common_php =~ s/##PREFIX##/$prefix/g;  $common_php =~ s/##PREFIX##/$prefix/g;
217  $common_php =~ s/##DB_USER##/$db_user/g;  $common_php =~ s/##DB_USER##/$db_user/g;
218  $common_php =~ s/##PREFIX##/$prefix/g;  $common_php =~ s/##PREFIX##/$prefix/g;
219  my $members_db = $prefix."members";  my $members_db = $prefix."members";
220  $common_php =~ s/##MEMBERS_DB##/$members_db/g;  $common_php =~ s/##MEMBERS_DB##/$members_db/g;
221    $common_php =~ s/##WITHOUT_INVITATION##/$without_invitation/g;
222    
223  print PHP $common_php;  print PHP $common_php;
224  close(PHP);  close(PHP);
225    
226  open(PHP,">$dir/head.php") || die "head.php: $!";  open(PHP,">$poll/head.php") || die "head.php: $!";
227  my $max_page = $page_nr - 1;  my $max_page = $page_nr - 1;
228  $head_php=~ s/##MAXPAGE##/$max_page/;  $head_php=~ s/##MAXPAGE##/$max_page/;
229  $head_php=~ s/##TEXT##/Ispunili ste %02d%% ankete/;  $head_php=~ s/##TEXT##/Ispunili ste %02d%% ankete/;
230  print PHP $head_php;  print PHP $head_php;
231  close(PHP);  close(PHP);
232    
233    # 01.php -> index.php
234    rename "$poll/01.php","$poll/index.php" || die "can't rename '$poll/01.php' to index.php";
235    
236  ################  ################
237  ## End of main  ## End of main
238  ################  ################
239    
240  # return unique name of pitanje  # return unique name of pitanje
241  sub new_pit {  sub new_pit {
242          my $out="p".$pitanje_nr.$p_suffix;          my $out="p".$pitanje_nr;
243            $out .= "_".$p_suffix if ($p_suffix);
244          $curr_suffix=$p_suffix;          $curr_suffix=$p_suffix;
245          $p_suffix++;          $p_suffix++;
246          return $out;          return $out;
# Line 277  sub curr_pit { Line 251  sub curr_pit {
251          return "p".$pitanje_nr.$curr_suffix;          return "p".$pitanje_nr.$curr_suffix;
252  }  }
253    
254    #----------------------------------------------------------
255    
256  sub starthndl {  sub starthndl {
257    my ($xp, $el, %atts) = @_;    my ($xp, $el, %atts) = @_;
258    
259  #  return unless ($in_slideshow or $el eq 'slideshow');  #  return unless ($in_poll or $el eq 'slideshow');
260    
261    unless ($in_slideshow) {    unless ($in_poll) {
262      $in_slideshow = $xp->depth + 1;      $in_poll = $xp->depth + 1;
263      return;      return;
264    }    }
265    
# Line 330  sub starthndl { Line 306  sub starthndl {
306    
307    my $new_closure;    my $new_closure;
308    
309    my $subname = "Slideshow::$el";    my $subname = "Poll::$el";
310    
311    if (defined &$subname) {    if (defined &$subname) {
312      no strict 'refs';      no strict 'refs';
# Line 338  sub starthndl { Line 314  sub starthndl {
314      &$subname($xp, $el, \%atts, \$new_closure);      &$subname($xp, $el, \%atts, \$new_closure);
315    }    }
316    else {    else {
317      $body .= $xp->recognized_string;      $body .= x($xp->recognized_string);
318      $new_closure =      $new_closure =
319        sub {        sub {
320          my ($xp, $text) = @_;          my ($xp, $text) = @_;
321                    
322          if (defined $text) {          if (defined $text) {
323            $body .= $text;            $body .= x($text);
324          }          }
325          else {          else {
326            $body .= "</$el>";            $body .= x("</$el>");
327          }          }
328        };        };
329    }    }
# Line 359  sub starthndl { Line 335  sub starthndl {
335  sub endhndl {  sub endhndl {
336    my ($xp, $el) = @_;    my ($xp, $el) = @_;
337    
338    return unless $in_slideshow;    return unless $in_poll;
339    
340    my $lev = $xp->depth;    my $lev = $xp->depth;
341    
342    if ($lev == $in_slideshow - 1) {    if ($lev == $in_poll - 1) {
343      $in_slideshow = 0;      $in_poll = 0;
344      $xp->finish;      $xp->finish;
345      return;      return;
346    }    }
# Line 414  sub endhndl { Line 390  sub endhndl {
390    $closure = pop(@closure_stack);    $closure = pop(@closure_stack);
391  }  # End endhndl  }  # End endhndl
392    
393    #----------------------------------------------------------
394    
395  sub text {  sub text {
396    my ($xp, $data) = @_;    my ($xp, $data) = @_;
397    
398    return unless $in_slideshow;    return unless $in_poll;
399    
400    if ($Mode ) {    if ($Mode ) {
401    
# Line 467  sub sgml_escape { Line 445  sub sgml_escape {
445    $str;    $str;
446  }  # End sgml_escape  }  # End sgml_escape
447    
 sub slidename {  
   my ($num) = @_;  
   
   sprintf("slide%03d.html", $num);  
 }  # End slidename  
448    
449  ################################################################  ################################################################
450    
451  package Slideshow;  package Poll;
452    
453  sub page {  sub page {
454          package main;          package main;
# Line 490  sub page { Line 463  sub page {
463                          print "p[$page_nr] ";                          print "p[$page_nr] ";
464    
465                          if (defined $last_fn) {                          if (defined $last_fn) {
466                                  # 01.php -> index.php                                  open(PAGE, ">$poll/$last_fn") or die "Couldn't open $last_fn for writing:\n$!";
467                                  $last_fn="index.php" if ($last_fn eq "01.php");                                  print PAGE php_header($page_nr,@prelast_sql_update);
                                 open(PAGE, ">$dir/$last_fn") or die "Couldn't open $last_fn for writing:\n$!";  
                                 if ($page_nr == 2) {  
                                         print PAGE '<?php  
 include("common.php");  
 if (isset($do_stranice) && $do_stranice !="") {  
         Header("Location: $do_uri?a=$a");  
         exit;  
 }  
 $member_id=id_decode($a);  
 $sql="insert into '.$dir.' ( http_referer,remote_addr,user_agent, member_id ) values (\'$HTTP_REFERER\',\'$REMOTE_ADDR\',\'$HTTP_USER_AGENT\',$member_id)";  
   
 #       print "<pre>$sql</pre>";  
         $result=pg_Exec($conn,fix_sql($sql));  
         $lastoid=pg_getlastoid($result);  
         $result = pg_Exec($conn,fix_sql("select id from '.$dir.' where oid=$lastoid"));  
         $row=pg_fetch_row($result,0);  
         $id=$row[0];  
 ?>';  
   
                                 } else {  
                                         print PAGE php_header($page_nr,@prelast_sql_update);  
                                 } # last_sql_update  
   
   
468                                  my $next_fn=sprintf("%02d.php",$page_nr);                                  my $next_fn=sprintf("%02d.php",$page_nr);
469                                  $last_page=~s/##NEXTPAGE##/$next_fn/;                                  $last_page=~s/##NEXTPAGE##/$next_fn/;
470                                  print PAGE $last_page;                                  print PAGE $last_page;
# Line 545  sub nr { Line 494  sub nr {
494          $$ncref = sub {          $$ncref = sub {
495                  my ($xp, $text) = @_;                  my ($xp, $text) = @_;
496                  if (defined($text)) {                  if (defined($text)) {
497                          $body.=$text;                          $body.=x($text);
498                          chomp $text;                          chomp $text;
499                          $pitanje_tag .= $text;                          $pitanje_tag .= x($text);
500                  } else {                  } else {
501                          $pitanje_nr = $pitanje_tag;                          $pitanje_nr = $pitanje_tag;
502                          $pitanje_nr =~ s/[^0-9a-zA-Z]//g;                          $pitanje_nr =~ s/[^0-9a-zA-Z]//g;
# Line 805  sub checkboxes { Line 754  sub checkboxes {
754          }          }
755  }  }
756    
757    print "\n\nTo create database for poll $poll use:\n\n";
758    print "\$ psql template1 < $poll/$poll.sql\n\n";
759    print "THIS WILL DISTROY ALL DATA IN EXISTING DATABASE ".$prefix.$poll." !!\n";
760    
761    # read configuration data
762    #
763    # FIX: write actually this :-)
764    sub config {
765            package main;
766            my ($xp, $el, $attref, $ncref) = @_;
767    
768            $$ncref = sub {
769                    my ($xp, $text) = @_;
770                    $db_user=x($attref->{db_user});
771                    $prefix=x($attref->{prefix});
772                    $without_invitation=x($attref->{without_invitation});
773            }
774    }
775    
776  #---------------------------------------------------------------  #---------------------------------------------------------------

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.9

  ViewVC Help
Powered by ViewVC 1.1.26