5757$dbh -> prepare(" SET NAMES 'utf8mb4'" )-> execute();
5858
5959my $libraryRoot = " $ENV {GITHUB_WORKSPACE}/OpenProblemLibrary" ;
60+ my $contribRoot = " $ENV {GITHUB_WORKSPACE}/Contrib" ;
6061
6162my $verbose = 0;
6263my $cnt2 = 0;
@@ -123,6 +124,7 @@ sub dbug {
123124 DBsection_id int(15) NOT NULL,
124125 author_id int(15),
125126 institution tinyblob,
127+ libraryroot varchar(255) NOT NULL,
126128 path_id int(15) NOT NULL,
127129 filename varchar(255) NOT NULL,
128130 morelt_id int(127) DEFAULT 0 NOT NULL,
@@ -258,24 +260,22 @@ sub safe_get_id {
258260sub isvalid {
259261 my $tags = shift ;
260262 if (!defined $taxo -> { $tags -> {DBsubject } }) {
261- print " \n Invalid subject $tags ->{DBsubject}\n " ;
263+ # print "\nInvalid subject $tags->{DBsubject}\n";
262264 return 0;
263265 }
264266 if (!($tags -> {DBchapter } eq ' Misc.' ) && !defined $taxo -> { $tags -> {DBsubject } }{ $tags -> {DBchapter } }) {
265- print " \n Invalid chapter $tags ->{DBchapter}\n " ;
267+ # print "\nInvalid chapter $tags->{DBchapter}\n";
266268 return 0;
267269 }
268270 if (!($tags -> {DBsection } eq ' Misc.' )
269271 && !defined $taxo -> { $tags -> {DBsubject } }{ $tags -> {DBchapter } }{ $tags -> {DBsection } })
270272 {
271- print " \n Invalid section $tags ->{DBsection}\n " ;
273+ # print "\nInvalid section $tags->{DBsection}\n";
272274 return 0;
273275 }
274276 return 1;
275277}
276278
277- my ($name , $pgfile , $pgpath );
278-
279279# First read in textbook information
280280if (open (my $fh , ' <:encoding(UTF-8)' , " $libraryRoot /Textbooks" )) {
281281 print " Reading in textbook data from Textbooks in the library $libraryRoot .\n " ;
@@ -463,7 +463,8 @@ sub isvalid {
463463print " Number of files processed:\n " ;
464464
465465# Now search for tagged problems
466- File::Find::find({ wanted => \&pgfiles, follow_fast => 1 }, $libraryRoot );
466+ File::Find::find( { wanted => \&pgfiles, follow_fast => 1 }, $libraryRoot );
467+ File::Find::find( { wanted => \&pgfiles, follow_fast => 1 }, $contribRoot );
467468
468469sub trim {
469470 return shift =~ s / ^\s +|\s +$// gr ;
@@ -503,19 +504,24 @@ sub pgfiles {
503504 my @textproblems = (-1);
504505
505506 if ($name =~ / \. pg$ / ) {
506- $pgfile = basename($name );
507- $pgpath = dirname($name );
508507 ++$cnt2 ;
509508 printf (' %6d' , $cnt2 ) if ($cnt2 % 100) == 0;
510509 print " \n " if ($cnt2 % 1000) == 0;
511510
512- $pgpath =~ s | ^$libraryRoot/|| ;
511+ my $pgfile = basename($name );
512+ my $pgpath = dirname($name );
513+ $pgpath =~ s | ^$libraryRoot| Library| ;
514+ $pgpath =~ s | ^$contribRoot| Contrib| ;
515+ $pgpath =~ m | ^([^/]*)/(.*)| ;
516+ my ($pglib , $pgpath ) = ($1 , $2 );
517+
513518 my $tags = Tags-> new($name );
514519
515520 if ($tags -> istagged()) {
516521 # Fill in missing data with Misc. instead of blank
517- print " \n NO SUBJECT $name \n " unless ($tags -> {DBsubject } =~ / \S / );
522+ # print "\nNO SUBJECT $name\n" unless ($tags->{DBsubject} =~ /\S/);
518523
524+ $tags -> {DBsubject } = ' Misc.' unless $tags -> {DBchapter } =~ / \S / ;
519525 $tags -> {DBchapter } = ' Misc.' unless $tags -> {DBchapter } =~ / \S / ;
520526 $tags -> {DBsection } = ' Misc.' unless $tags -> {DBsection } =~ / \S / ;
521527
@@ -559,8 +565,8 @@ sub pgfiles {
559565 }
560566 } else {
561567 # Tags are not valid, error printed by validation part.
562- print " File $name \n " ;
563- next ;
568+ # print "File $name\n";
569+ return ; # next is not valid for subs
564570 }
565571
566572 my @DBsection_ids = ($aDBsection_id );
@@ -610,23 +616,21 @@ sub pgfiles {
610616 my $path_id = safe_get_id($tables {path }, ' path_id' , qq( WHERE path = ?) , [$pgpath ], 1, ' ' , $pgpath , ' ' , ' ' );
611617
612618 # pgfile table -- set 4 defaults first
613-
614- # TODO this is where we have to deal with crosslists, and pgfileid will be an array of id's
615- # Make an array of DBsection-id's above
616- my $level = $tags -> {Level } || 0;
617- # Default language is English
619+ my $level = ($tags -> {Level } =~ / \d / ) ? $tags -> {Level } : 0;
618620 my $lang = $tags -> {Language } || ' en' ;
619621 my $mathobj = $tags -> {MO } || 0;
620622 my $static = $tags -> {Static } || 0;
621623
622- my @pgfile_ids ;
623624
625+ # TODO this is where we have to deal with crosslists, and pgfileid will be an array of id's
626+ # Make an array of DBsection-id's above
627+ my @pgfile_ids ;
624628 for my $DBsection_id (@DBsection_ids ) {
625629 my $pgfile_id = safe_get_id(
626630 $tables {pgfile }, ' pgfile_id' ,
627- qq( WHERE filename = ? AND path_id = ? AND DBsection_id = ? ) ,
628- [ $pgfile , $path_id , $DBsection_id ], 1, ' ' , $DBsection_id , $author_id , $tags -> {Institution },
629- $path_id , $pgfile , 0, $level , $lang , $static , $mathobj
631+ qq( WHERE filename = ? AND path_id = ? AND DBsection_id = ? AND libraryroot = ? ) ,
632+ [ $pgfile , $path_id , $DBsection_id , $pglib ], 1, ' ' , $DBsection_id , $author_id , $tags -> {Institution },
633+ $pglib , $ path_id , $pgfile , 0, $level , $lang , $static , $mathobj
630634 );
631635 push @pgfile_ids , $pgfile_id ;
632636 }
@@ -701,16 +705,18 @@ sub pgfiles {
701705 $edition = 0 unless $edition ;
702706 $dbh -> do(qq{ INSERT INTO `$tables {textbook}`
703707 VALUES( NULL, "$text ", "$edition ", "$textauthor ", NULL, NULL, NULL)} );
704- dbug qq{ INSERT INTO textbook VALUES("", "$text ", "$edition ", "$textauthor ", "", "", "")\n } ;
705- dbug qq{ \n Late add into $tables {textbook} "$text ", "$edition ", "$textauthor "\n } , 1;
706708 $textbook_id = $dbh -> selectrow_array($textbook_id_query );
709+ unless ($textbook_id ) {
710+ warn qq{ INSERT INTO textbook VALUES("", "$text ", "$edition ", "$textauthor ", "", "", "")\n } ;
711+ warn qq{ \n Late add into $tables {textbook} "$text ", "$edition ", "$textauthor "\n } ;
712+ }
707713 }
708714
709715 # chapter weak table of textbook
710716 my $chapter_id_query = qq{ SELECT chapter_id FROM `$tables {chapter}`
711717 WHERE textbook_id="$textbook_id " AND number="$chapnum "} ;
712718 my $chapter_id = $dbh -> selectrow_array($chapter_id_query );
713- if (!defined ($chapter_id )) {
719+ if (!defined ($chapter_id ) && defined ( $textbook_id ) ) {
714720 $dbh -> do(qq{ INSERT INTO `$tables {chapter}`
715721 VALUES(NULL, "$textbook_id ", "$chapnum ", "$tags ->{DBchapter}", NULL)} );
716722 dbug qq{ \n Late add into $tables {chapter} "$text ", "$edition ",
@@ -724,7 +730,7 @@ sub pgfiles {
724730 my $section_id_query = qq{ SELECT section_id FROM `$tables {section}`
725731 WHERE chapter_id="$chapter_id " AND number="$secnum "} ;
726732 my $section_id = $dbh -> selectrow_array($section_id_query );
727- if (!defined ($section_id )) {
733+ if (!defined ($section_id ) && defined ( $chapter_id ) && defined ( $textbook_id ) ) {
728734 $dbh -> do(qq{ INSERT INTO `$tables {section}`
729735 VALUES(NULL, "$chapter_id ", "$secnum ", "$tags ->{DBsection}", NULL)} );
730736 dbug qq{ INSERT INTO section VALUES("", "$textbook_id ", "$secnum ", "$tags ->{DBsection}", "" )\n } ;
@@ -734,23 +740,25 @@ sub pgfiles {
734740 }
735741
736742 @textproblems = @{ $texthashref -> {problems } };
737- for my $tp (@textproblems ) {
738- my $problem_id_query = qq{ SELECT problem_id FROM `$tables {problem}`
739- WHERE section_id="$section_id " AND number="$tp "} ;
740- my $problem_id = $dbh -> selectrow_array($problem_id_query );
741- if (!defined ($problem_id )) {
742- $dbh -> do(qq{ INSERT INTO `$tables {problem}` VALUES(NULL, "$section_id ", "$tp ", NULL)} );
743- dbug qq{ INSERT INTO problem VALUES("", "$section_id ", "$tp ", "")\n } ;
744- $problem_id = $dbh -> selectrow_array($problem_id_query );
745- }
743+ if ($section_id ) {
744+ for my $tp (@textproblems ) {
745+ my $problem_id_query = qq{ SELECT problem_id FROM `$tables {problem}`
746+ WHERE section_id="$section_id " AND number="$tp "} ;
747+ my $problem_id = $dbh -> selectrow_array($problem_id_query );
748+ if (!defined ($problem_id )) {
749+ $dbh -> do(qq{ INSERT INTO `$tables {problem}` VALUES(NULL, "$section_id ", "$tp ", NULL)} );
750+ dbug qq{ INSERT INTO problem VALUES("", "$section_id ", "$tp ", "")\n } ;
751+ $problem_id = $dbh -> selectrow_array($problem_id_query );
752+ }
746753
747- # pgfile_problem table associates pgfiles with textbook problems
748- for my $pgfile_id (@pgfile_ids ) {
749- my $pg_problem_id = $dbh -> selectrow_array(qq{ SELECT problem_id FROM `$tables {pgfile_problem}`
750- WHERE problem_id="$problem_id " AND pgfile_id="$pgfile_id "} );
751- if (!defined ($pg_problem_id )) {
752- $dbh -> do(qq{ INSERT INTO `$tables {pgfile_problem}` VALUES("$pgfile_id ", "$problem_id ")} );
753- dbug qq{ INSERT INTO pgfile_problem VALUES("$pgfile_id ", "$problem_id ")\n } ;
754+ # pgfile_problem table associates pgfiles with textbook problems
755+ for my $pgfile_id (@pgfile_ids ) {
756+ my $pg_problem_id = $dbh -> selectrow_array(qq{ SELECT problem_id FROM `$tables {pgfile_problem}`
757+ WHERE problem_id="$problem_id " AND pgfile_id="$pgfile_id "} );
758+ if (!defined ($pg_problem_id )) {
759+ $dbh -> do(qq{ INSERT INTO `$tables {pgfile_problem}` VALUES("$pgfile_id ", "$problem_id ")} );
760+ dbug qq{ INSERT INTO pgfile_problem VALUES("$pgfile_id ", "$problem_id ")\n } ;
761+ }
754762 }
755763 }
756764 }
0 commit comments