########################################################################### # # GreenstoneSQLPlugout.pm -- plugout module for writing all or some the # Greenstone document format (metadata and/or fulltext) into a (My)SQL db. # The rest is then still written out by GreenstoneXMLPlugout as usual. # A component of the Greenstone digital library software # from the New Zealand Digital Library Project at the # University of Waikato, New Zealand. # # Copyright (C) 2006 New Zealand Digital Library Project # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. # ########################################################################### package GreenstoneSQLPlugout; use strict; no strict 'refs'; no strict 'subs'; use GreenstoneXMLPlugout; use docprint; use gssql; use DBI; # the central package for this plugout # TODO: SIGTERM rollback and disconnect? # this plugout does not output xml to a file, but outputs rows into a mysql table sub BEGIN { @GreenstoneSQLPlugout::ISA = ('GreenstoneXMLPlugout'); } # NOTTODO: die() statements need to be replaced with premature_termination # which should ensure the GreenstoneXMLPlugin (group)'s stuff is closed and cleaned up SOMEHOW # It's fine: the die() stmts all take place before setting up the super class' begin # TODO: deal with -removeold and everything? Or type out instructions for user # TODO Q: what is "group" in GreenstoneXMLPlugout? # TODO Q: site_name only exists for GS3. What about GS2? my $process_mode_list = [ { 'name' => "meta_only", 'desc' => "{GreenstoneSQLPlugout.process_mode.meta_only}" }, { 'name' => "text_only", 'desc' => "{GreenstoneSQLPlugout.process_mode.text_only}" }, { 'name' => "all", 'desc' => "{GreenstoneSQLPlugout.process_mode.all}" } ]; my $arguments = [ { 'name' => "process_mode", 'desc' => "{GreenstoneSQLPlugout.process_mode}", 'type' => "enum", 'list' => $process_mode_list, 'deft' => "all", 'reqd' => "no", 'hiddengli' => "no"} ]; my $options = { 'name' => "GreenstoneSQLPlugout", 'desc' => "{GreenstoneSQLPlugout.desc}", 'abstract' => "no", 'inherits' => "yes", 'args' => $arguments }; sub new { my ($class) = shift (@_); my ($plugoutlist, $inputargs,$hashArgOptLists) = @_; push(@$plugoutlist, $class); push(@{$hashArgOptLists->{"ArgList"}},@{$arguments}); push(@{$hashArgOptLists->{"OptList"}},$options); my $self = new GreenstoneXMLPlugout($plugoutlist,$inputargs,$hashArgOptLists); if ($self->{'info_only'}) { # don't worry about any options etc return bless $self, $class; } print STDERR "***** GreenstoneSQLPlugout process mode = \"", $self->{'process_mode'}, "\"\n"; return bless $self, $class; } # connect here and ensure all tables and databases exist sub begin { my $self= shift (@_); ########### TODO: these should be set from cmdline/GLI options to plugout ######### $self->{'db_driver'} = "mysql"; $self->{'site_name'} = "localsite"; $self->{'db_client_user'} = "root"; $self->{'db_client_pwd'} = "6reenstone3"; $self->{'build_mode'} = "removeold"; #$self->{'db_host'} = "127.0.0.1"; #$self->{'db_encoding'} = "utf8"; #TODO: proc_mode is also a saveas option ############ LOAD NECESSARY OPTIONS ########### print STDERR "########## COLLECTION: ". $ENV{'GSDLCOLLECTION'}."\n"; #$self->{'collection_name'} = $ENV{'GSDLCOLLECTION'}; print STDERR "***** GreenstoneSQLPlugout process mode = \"", $self->{'process_mode'}, "\"\n"; my $db_params = { 'collection_name' => $ENV{'GSDLCOLLECTION'}, 'db_encoding' => $self->{'db_encoding'} #'db_name' => $self->{'site_name'}, #'build_mode' => $self->{'build_mode'}, }; my $gs_sql = new gssql($db_params); # try connecting to the mysql db, if that fails it will die # so don't bother preparing GreenstoneXMLPlugout by calling superclass' begin() if(!$gs_sql->connect_to_db({ 'db_driver' => $self->{'db_driver'}, 'db_client_user' => $self->{'db_client_user'}, 'db_client_pwd' => $self->{'db_client_pwd'}, 'db_host' => $self->{'db_host'} }) ) { # This is fatal for the plugout, let's terminate here # PrintError would already have displayed the warning message on connection fail die("Could not connect to db. Can't proceed.\n"); } my $db_name = $self->{'site_name'} || "localsite"; # one database per GS3 site my $build_mode = $self->{'build_mode'} || "removeold"; if(!$gs_sql->load_db_and_tables($db_name, $build_mode)) { # This is fatal for the plugout, let's terminate here # PrintError would already have displayed the warning message on connection fail die("Could not use db or prepare its tables. Can't proceed.\n"); } # prepare the shared/common HANDLES to SQL insert statements that contain placeholders # and which we will reuse repeatedly when actually executing the insert statements my $proc_mode = $self->{'process_mode'}; if($proc_mode eq "all" || $proc_mode eq "meta_only" ) { $self->{'metadata_prepared_insert_statement_handle'} = $gs_sql->prepare_insert_metadata_row_stmthandle(); } if($proc_mode eq "all" || $proc_mode eq "text_only" ) { $self->{'fulltxt_prepared_insert_statement_handle'} = $gs_sql->prepare_insert_fulltxt_row_stmthandle(); } # store the DBI wrapper instance $self->{'gs_sql'} = $gs_sql; print STDERR "#### Meta stmt: " . $self->{'metadata_prepared_insert_statement_handle'}->{'Statement'} . "\n"; print STDERR "#### Full stmt: " . $self->{'fulltxt_prepared_insert_statement_handle'}->{'Statement'} . "\n"; # if setting up to work with sql db failed, we'd have terminated and wouldn't come up to here: # won't bother preparing GreenstoneXMLPlugout by calling superclass' begin() # finally, can call begin on super - important as doc.xml is opened as a group etc $self->SUPER::begin(@_); } # disconnect from database here, see inexport.pm sub end { my $self = shift(@_); # do the superclass stuff first, as any sql db failures should not prevent superclass cleanup $self->SUPER::end(@_); $self->{'gs_sql'}->disconnect_from_db() || warn("Unable to disconnect from database " . $self->{'site_name'} . "\n"); # disconnect_from_db() will also issue a warning, but this may be clearer } # produce files called docsql.xml instead of doc.xml sub get_short_doc_file { my $self = shift (@_); my ($doc_dir) = @_; return &FileUtils::filenameConcatenate($doc_dir, "docsql.xml"); } # produce files called docsql.xml instead of doc.xml sub get_output_file { my $self = shift (@_); my ($doc_dir) = @_; return &FileUtils::filenameConcatenate($self->{'output_dir'}, $doc_dir, "docsql.xml"); } # TODO: check arc-inf.db for whether each entry is to be deleted/indexed/reindexed/been indexed sub saveas { my $self = shift (@_); my ($doc_obj, $doc_dir) = @_; print STDERR "\n\n@@@ In saveas\n\n"; my $proc_mode = $self->{'process_mode'}; # 1. pre save out and saving debug handle # must call superclass (pre/post) saveas methods, as they handle assoc_files too my ($docxml_outhandler, $output_file) = $self->SUPER::pre_saveas(@_); $self->{'debug_outhandle'} = $docxml_outhandler if ($self->{'debug'}); # STDOUT if debug # TODO: also set debugging in begin()? Then stmts creating db and tables also sent to debug out and not executed # TODO: remove unused old_unused_saveas from GreenstoneXMLPlugout # 2. overriding saving behaviour to do what the superclass does PLUS saving to sql db #NOTE: if proc_mode == all, then "breadcrumbs" go into both meta and txt elements of doc.xml: # statements pointing viewer to the sql db for contents # write the INVERSE into doc.xml as to what is written to the db my $docxml_output_options = { 'output' => docprint::OUTPUT_NONE }; if($proc_mode eq "meta_only" ) { # since only meta to go into MySQL db, text will go into docxml $docxml_output_options->{'output'} = docprint::OUTPUT_TEXT_ONLY; } elsif($proc_mode eq "text_only" ) { # since only full text to go into MySQL db, meta will go into docxml $docxml_output_options->{'output'} = docprint::OUTPUT_META_ONLY; } # now we've prepared to write out whatever is meant to go into docxml # and can do actual the steps superclass GreenstoneXMLPlugout carries out to write out docxml # So: write out the doc xml file for the current document my $section_text = &docprint::get_section_xml($doc_obj, $docxml_output_options); print $docxml_outhandler $section_text; # We also write out whatever needs to go into the MySQL database $self->write_meta_and_text($doc_obj); # 3. post save out #$self->SUPER::post_saveas(@_); $self->SUPER::post_saveas($doc_obj, $doc_dir, $docxml_outhandler, $output_file); # database connection is closed in end() method # so we don't open and close over and over for each doc during a single build } # write meta and/or text PER DOC out to DB sub write_meta_and_text { my $self = shift (@_); my ($doc_obj) = @_; my $doc_oid = $doc_obj->get_OID(); # this method processes a single doc at a time, so it uses the same OID throughout my $root_section = $doc_obj->get_top_section(); # load the prepared INSERT statement handles for both tables (can be undef for any table depending on whether meta_only or txt_only are set) my $metadata_table_sth = $self->{'metadata_prepared_insert_statement_handle'}; my $fulltxt_table_sth = $self->{'fulltxt_prepared_insert_statement_handle'}; $self->recursive_write_meta_and_text($doc_obj, $doc_oid, $root_section, $metadata_table_sth, $fulltxt_table_sth); } # Perl: Reading or Writing to Another Program # https://nnc3.com/mags/Perl3/cookbook/ch16_05.htm sub recursive_write_meta_and_text { my $self = shift (@_); my ($doc_obj, $doc_oid, $section, $metadata_table_sth, $fulltxt_table_sth) = @_; # If section=ROOT, write "root" as section name into table # doc->get_top_section() is the name of the doc root section, which is "" my $section_name = ($section eq "") ? "root" : $section; my $section_ptr = $doc_obj->_lookup_section ($section); return "" unless defined $section_ptr; my $debug_out = $self->{'debug_outhandle'}; # print STDERR "#### Meta stmt: " . $metadata_table_sth->{'Statement'} . "\n"; # print STDERR "#### Full stmt: " . $fulltxt_table_sth->{'Statement'} . "\n"; #my $proc_mode = $self->{'process_mode'}; #if($proc_mode eq "all" || $proc_mode eq "meta_only" ) { if($metadata_table_sth) { # meta insert statement handle will be undef if not writing meta foreach my $data (@{$section_ptr->{'metadata'}}) { my $meta_name = $data->[0]; my $escaped_meta_value = &docprint::escape_text($data->[1]); # Write out the current section's meta to collection db's METADATA table # for each set of values to write to meta table, execute the prepared statement, filling in the values if($self->{'debug'}) { # just print the statement we were going to execute print $debug_out $metadata_table_sth->{'Statement'} . "($doc_oid, $section_name, $meta_name, $escaped_meta_value)\n"; } else { $metadata_table_sth->execute($doc_oid, $section_name, $meta_name, $escaped_meta_value) || warn ("Unable to write metadata row to db:\n\tOID $doc_oid, section $section_name,\n\tmeta name: $meta_name, val: $escaped_meta_value"); # Execution failure will print out info anyway: since db connection sets PrintError } } } #if($proc_mode eq "all" || $proc_mode eq "text_only" ) { if($fulltxt_table_sth) { # fulltxt insert statement handle will be undef if not writing fulltxt if($self->{'debug'}) { # just print the statement we were going to execute, minus the fulltxt value my $txt_repr = $section_ptr->{'text'} ? "" : "NULL"; print $debug_out $fulltxt_table_sth->{'Statement'} . "($doc_oid, $section_name, $txt_repr)\n"; } else { my $section_text = &docprint::escape_text($section_ptr->{'text'}); # fulltxt column can be SQL NULL. undef value gets written out as NULL: # https://stackoverflow.com/questions/12708633/which-one-represents-null-undef-or-empty-string # Write out the current section's text to collection db's FULLTeXT table $fulltxt_table_sth->execute($doc_oid, $section_name, $section_text) || warn ("Unable to write fulltxt row to db for row:\n\tOID $doc_oid, section $section_name"); # Execution failure will print out info anyway: since db connection sets PrintError } } # output all subsections: RECURSIVE CALL foreach my $subsection (@{$section_ptr->{'subsection_order'}}) { $self->recursive_write_meta_and_text($doc_obj, $doc_oid, "$section.$subsection", $metadata_table_sth, $fulltxt_table_sth); } } 1;