+setlocale: LC_ALL=cs_CZ.iso-8859-2
[www.energie.vellum.cz.git] / obsah_init.pl
index 89d40a7..d2ded01 100755 (executable)
@@ -6,7 +6,7 @@ use strict;
 use DBI;
 
 use vars qw/$db_driver $db_host $db_user $db_pwd $DB_PWD $db_name $db/;
-use vars qw/$tb_obsah/;
+use vars qw/$tb_obsah $tb_clanek/;
 
 $db_driver="mysql";
 $db_host="";
@@ -14,6 +14,7 @@ $db_user="short";
 $DB_PWD=$ENV{"HOME"}."/priv/mysql.${db_user}.pwd";
 $db_name="short";
 $tb_obsah="energie_obsah";
+$tb_clanek="energie_clanek";
 
 open DB_PWD or die "Failed open \"$DB_PWD\": $!";
 $db_pwd=<DB_PWD>;
@@ -35,13 +36,25 @@ eval { &db_do("drop table $tb_obsah") };
                ."year year(4) not null,"
                ."month tinyint not null,"
                ."month_last tinyint not null,"
+               ."sequential smallint not null,"
                ."contents text not null"
                .")");
 
 &db_do("alter table $tb_obsah add unique (year,month)");
 
+eval { &db_do("drop table $tb_clanek") };
 
-use vars qw/$insert_tb_obsah $year $month $month_last $contents/;
+&db_do("create table $tb_clanek ("
+               ."year year(4) not null,"
+               ."month tinyint not null,"
+               ."id tinyint not null,"
+               ."name text not null,"
+               ."contents text not null"
+               .")");
+
+&db_do("alter table $tb_clanek add unique (year,month,id)");
+
+use vars qw/$insert_tb_obsah $insert_tb_clanek $year $month $month_last $sequential $contents $first $article_id/;
 
 sub where
 {
@@ -58,35 +71,70 @@ sub flush_month
        else {
                tr/ \t\n/  \n/s;
                s/([\001\n]) | ([\002\n])/$1$2/g;
-               s/\nstrana \.\.\. ([\d ,]+\002)/\003$1/g;
-               die "Page marker not found somewhere in this month".&where() if (/\001[^\003]*\002/);
+               die "Page marker not found somewhere in this month".&where() if (/^[^\004]*\001[^\003\004]*\002/);
+               die "Duplicate page markers in this month".&where() if (/strana /);
                tr/\002//d;
                $_=substr($_,1);
                }
-       $insert_tb_obsah->execute($year,$month,$month_last,$_) or die "SQL insert failure: $!";
+       $insert_tb_obsah->execute($year,$month,$month_last,$sequential,$_) or die "SQL insert failure: $!";
        undef $year,$month;
        undef $contents;
+       undef $first;
+       undef $article_id;
 }
 
-$insert_tb_obsah=$db->prepare("insert into $tb_obsah (year,month,month_last,contents) values (?,?,?,?)")
+$insert_tb_obsah=$db->prepare("insert into $tb_obsah (year,month,month_last,sequential,contents) values (?,?,?,?,?)")
+               or die "Prepare fail: $!";
+$insert_tb_clanek=$db->prepare("insert into $tb_clanek (year,month,id,name,contents) values (?,?,?,?,?)")
                or die "Prepare fail: $!";
 
 while (<>) {
        chomp;
-       if (m#^EaP (\d+)(-(\d+))?/(\d+)$#) {
+       if (m#^EaP (\d+)(-(\d+))?/(\d+) \((\d+)(-\d+)?\)$#) {
                &flush_month();
                $month=$1;
                $month_last=($3 ? $3 : $1);
                $year =$4;
+               $sequential=$5;
                $contents="";
                next;
                }
-       if (/^\.\s(.*)$/) {
-               $contents.="\001$1\002";
+       if (/^#(.+)$/) {
+my( $clanek )="";
+my( $line );
+               while (($line=<>)!~/^#$/) {
+                       die "Article EOF marker not found".&where() if (!$line);
+                       $clanek.=$line;
+                       }
+               # $article_id probably should not be zero
+               $insert_tb_clanek->execute($year,$month,++$article_id,$1,$clanek) or die "SQL insert failure: $!";
+               next;
+               }
+       if (/^Pøíloha:$/) {
+               die "Multiple \"Pøíloha\"s".&where() if ($contents=~/\004/);
+               $contents.="\004";
+               next;
+               }
+       if (/^[^ \t]/ || (/^\s/ && $contents=~/\004/)) {
+               $contents.="\001$_\002";
+               $first=$_;
+               next;
+               }
+       if (/^\s+strana \.\.\. ([\d ,-]+)$/) {
+               die "Page number without start".&where() if ("\002"!=substr($contents,-1,1));
+               die "Page number already specified".&where() if ($contents=~/\003[^\001\002\004]*\002$/);
+               $contents=substr($contents,0,-1)."\003$1\002";
                next;
                }
        if (/^\s(.*)$/) {
-               $contents=substr($contents,0,-1)."\n$1\002";
+               my( $s )=$1;
+               die "INTERNAL: continuation for \"Pøíloha\"".&where() if ($contents=~/\004/);
+               die "Continuation contents without start".&where() if ("\002"!=substr($contents,-1,1));
+               if ($contents=~/\003[^\001\002\004]*\002$/) {
+                       die "First line not found".&where() if !defined($first);
+                       $contents.="\001$first\002";
+                       }
+               $contents=substr($contents,0,-1)."\n$s\002";
                next;
                }
        die "Unexpected text".&where().": $_" if (/\S/);