source: main/trunk/greenstone2/perllib/plugouts/GreenstoneSQLPlugout.pm@ 32541

Last change on this file since 32541 was 32541, checked in by ak19, 5 years ago

Using proper parameters to GreenstoneSQLPlugin/Plugout instead of hardcoded values for params.

File size: 14.6 KB
RevLine 
[32518]1###########################################################################
2#
[32527]3# GreenstoneSQLPlugout.pm -- plugout module for writing all or some the
4# Greenstone document format (metadata and/or fulltext) into a (My)SQL db.
[32526]5# The rest is then still written out by GreenstoneXMLPlugout as usual.
[32518]6# A component of the Greenstone digital library software
7# from the New Zealand Digital Library Project at the
8# University of Waikato, New Zealand.
9#
10# Copyright (C) 2006 New Zealand Digital Library Project
11#
12# This program is free software; you can redistribute it and/or modify
13# it under the terms of the GNU General Public License as published by
14# the Free Software Foundation; either version 2 of the License, or
15# (at your option) any later version.
16#
17# This program is distributed in the hope that it will be useful,
18# but WITHOUT ANY WARRANTY; without even the implied warranty of
19# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20# GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License
23# along with this program; if not, write to the Free Software
24# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25#
26###########################################################################
27
[32527]28package GreenstoneSQLPlugout;
[32518]29
30use strict;
31no strict 'refs';
32no strict 'subs';
33
[32520]34use GreenstoneXMLPlugout;
[32518]35use docprint;
[32529]36use gssql;
[32518]37
[32524]38use DBI; # the central package for this plugout
39
[32518]40
[32521]41# TODO: SIGTERM rollback and disconnect?
[32536]42# TODO Q: what about verbosity for debugging
[32541]43# TODO Q: introduced site_name param to plugins and plugouts. Did I do it right? And should they have hiddengli = "yes"
[32521]44
[32518]45# this plugout does not output xml to a file, but outputs rows into a mysql table
46sub BEGIN {
[32527]47 @GreenstoneSQLPlugout::ISA = ('GreenstoneXMLPlugout');
[32518]48}
49
[32529]50# NOTTODO: die() statements need to be replaced with premature_termination
51# which should ensure the GreenstoneXMLPlugin (group)'s stuff is closed and cleaned up SOMEHOW
52# It's fine: the die() stmts all take place before setting up the super class' begin
[32520]53
54# TODO: deal with -removeold and everything? Or type out instructions for user
55
[32518]56# TODO Q: what is "group" in GreenstoneXMLPlugout?
[32520]57# TODO Q: site_name only exists for GS3. What about GS2?
[32518]58
59my $process_mode_list =
60 [ { 'name' => "meta_only",
[32537]61 'desc' => "{GreenstoneSQLPlug.process_mode.meta_only}" },
[32518]62 { 'name' => "text_only",
[32537]63 'desc' => "{GreenstoneSQLPlug.process_mode.text_only}" },
[32518]64 { 'name' => "all",
[32537]65 'desc' => "{GreenstoneSQLPlug.process_mode.all}" } ];
[32518]66
[32541]67# The following are the saveas.options:
[32518]68my $arguments = [
[32541]69 { 'name' => "process_mode",
70 'desc' => "{GreenstoneSQLPlug.process_mode}",
71 'type' => "enum",
72 'list' => $process_mode_list,
73 'deft' => "all",
74 'reqd' => "no",
75 'hiddengli' => "no"},
76 { 'name' => "db_driver",
77 'desc' => "{GreenstoneSQLPlug.db_driver}",
78 'type' => "string",
79 'deft' => "mysql",
80 'reqd' => "yes"},
81 { 'name' => "db_client_user",
82 'desc' => "{GreenstoneSQLPlug.db_client_user}",
83 'type' => "string",
84 'deft' => "root",
85 'reqd' => "yes"},
86 { 'name' => "db_client_pwd",
87 'desc' => "{GreenstoneSQLPlug.db_client_pwd}",
88 'type' => "string",
89 'deft' => "",
90 'reqd' => "yes"}, # pwd required?
91 { 'name' => "db_host",
92 'desc' => "{GreenstoneSQLPlug.db_host}",
93 'type' => "string",
94 'deft' => "127.0.0.1",
95 'reqd' => "yes"},
96 { 'name' => "db_encoding",
97 'desc' => "{GreenstoneSQLPlug.db_encoding}",
98 'type' => "string",
99 'deft' => "utf8",
100 'reqd' => "yes"}
101 ];
[32518]102
[32527]103my $options = { 'name' => "GreenstoneSQLPlugout",
104 'desc' => "{GreenstoneSQLPlugout.desc}",
[32518]105 'abstract' => "no",
106 'inherits' => "yes",
107 'args' => $arguments };
108
109sub new {
110 my ($class) = shift (@_);
111 my ($plugoutlist, $inputargs,$hashArgOptLists) = @_;
112 push(@$plugoutlist, $class);
113
114 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
115 push(@{$hashArgOptLists->{"OptList"}},$options);
116
117 my $self = new GreenstoneXMLPlugout($plugoutlist,$inputargs,$hashArgOptLists);
118
119 if ($self->{'info_only'}) {
120 # don't worry about any options etc
121 return bless $self, $class;
122 }
[32527]123 print STDERR "***** GreenstoneSQLPlugout process mode = \"", $self->{'process_mode'}, "\"\n";
[32518]124
125 return bless $self, $class;
126}
127
[32520]128# connect here and ensure all tables and databases exist
129sub begin {
130
131 my $self= shift (@_);
132
[32541]133 # The saveas.options
134 #print STDERR "@@@@ PLUGOUT db_pwd: " . $self->{'db_client_pwd'} . "\n";
135 #print STDERR "@@@@ user: " . $self->{'db_client_user'} . "\n";
136 #print STDERR "@@@@ db_host: " . $self->{'db_host'} . "\n";
137 #print STDERR "@@@@ db_enc: " . $self->{'db_encoding'} . "\n";
138 #print STDERR "@@@@ db_driver: " . $self->{'db_driver'} . "\n";
139 #print STDERR "@@@@ proc_mode: " . $self->{'process_mode'} . "\n";
140
[32520]141 ########### TODO: these should be set from cmdline/GLI options to plugout #########
[32541]142
[32524]143 $self->{'build_mode'} = "removeold";
[32541]144
[32520]145 ############ LOAD NECESSARY OPTIONS ###########
[32541]146 #print "@@@ plugout SITE NAME: ". $self->{'site_name'} . "\n" if defined $self->{'site_name'};
147 #print STDERR "########## COLLECTION: ". $ENV{'GSDLCOLLECTION'}."\n";
148
[32527]149 print STDERR "***** GreenstoneSQLPlugout process mode = \"", $self->{'process_mode'}, "\"\n";
[32529]150
151 my $db_params = {
[32530]152 'collection_name' => $ENV{'GSDLCOLLECTION'},
[32529]153 'db_encoding' => $self->{'db_encoding'}
154 };
155
156 my $gs_sql = new gssql($db_params);
[32520]157
[32530]158 # try connecting to the mysql db, if that fails it will die
159 # so don't bother preparing GreenstoneXMLPlugout by calling superclass' begin()
160 if(!$gs_sql->connect_to_db({
161 'db_driver' => $self->{'db_driver'},
162 'db_client_user' => $self->{'db_client_user'},
163 'db_client_pwd' => $self->{'db_client_pwd'},
164 'db_host' => $self->{'db_host'}
165 })
166 )
167 {
[32520]168 # This is fatal for the plugout, let's terminate here
169 # PrintError would already have displayed the warning message on connection fail
170 die("Could not connect to db. Can't proceed.\n");
171 }
[32524]172
[32541]173 my $db_name = $self->{'site_name'} || "greenstone2"; # one database per GS3 site, for GS2 the db is called greenstone2
[32524]174 my $build_mode = $self->{'build_mode'} || "removeold";
[32529]175 if(!$gs_sql->load_db_and_tables($db_name, $build_mode)) {
[32524]176
[32536]177 # This is fatal for the plugout, let's terminate here after disconnecting again
178 # PrintError would already have displayed the warning message on load fail
179 $gs_sql->disconnect_from_db()
[32541]180 || warn("Unable to disconnect from database.\n");
[32536]181 die("Could not use db $db_name and/or prepare its tables. Can't proceed.\n");
[32520]182 }
183
184 # prepare the shared/common HANDLES to SQL insert statements that contain placeholders
[32521]185 # and which we will reuse repeatedly when actually executing the insert statements
[32520]186 my $proc_mode = $self->{'process_mode'};
187 if($proc_mode eq "all" || $proc_mode eq "meta_only" ) {
[32529]188 $self->{'metadata_prepared_insert_statement_handle'} = $gs_sql->prepare_insert_metadata_row_stmthandle();
[32520]189 }
190 if($proc_mode eq "all" || $proc_mode eq "text_only" ) {
[32529]191 $self->{'fulltxt_prepared_insert_statement_handle'} = $gs_sql->prepare_insert_fulltxt_row_stmthandle();
[32520]192 }
[32521]193
[32529]194 # store the DBI wrapper instance
195 $self->{'gs_sql'} = $gs_sql;
196
[32524]197 print STDERR "#### Meta stmt: " . $self->{'metadata_prepared_insert_statement_handle'}->{'Statement'} . "\n";
198 print STDERR "#### Full stmt: " . $self->{'fulltxt_prepared_insert_statement_handle'}->{'Statement'} . "\n";
199
[32521]200 # if setting up to work with sql db failed, we'd have terminated and wouldn't come up to here:
201 # won't bother preparing GreenstoneXMLPlugout by calling superclass' begin()
202 # finally, can call begin on super - important as doc.xml is opened as a group etc
[32523]203
[32526]204 $self->SUPER::begin(@_);
[32520]205}
206
207# disconnect from database here, see inexport.pm
208sub end
209{
210 my $self = shift(@_);
211
[32521]212 # do the superclass stuff first, as any sql db failures should not prevent superclass cleanup
[32526]213 $self->SUPER::end(@_);
[32521]214
[32529]215 $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
[32520]216}
[32533]217
[32536]218# produce files called docsql-<OID>.xml instead of doc.xml
219sub get_doc_xml_filename {
[32533]220 my $self = shift (@_);
[32536]221 my ($doc_obj) = @_;
222
223 my $doc_id = $doc_obj->get_OID();
224 my $filename = $doc_id ? "docsql-".$doc_id.".xml" : "docsql.xml";
225 return $filename;
[32533]226}
[32521]227
[32518]228# TODO: check arc-inf.db for whether each entry is to be deleted/indexed/reindexed/been indexed
229sub saveas {
230 my $self = shift (@_);
231 my ($doc_obj, $doc_dir) = @_;
232
[32536]233# print STDERR "\n\n@@@ In saveas\n\n";
[32524]234
[32522]235 my $proc_mode = $self->{'process_mode'};
236
[32521]237 # 1. pre save out and saving debug handle
238
[32523]239 # must call superclass (pre/post) saveas methods, as they handle assoc_files too
[32526]240 my ($docxml_outhandler, $output_file) = $self->SUPER::pre_saveas(@_);
[32523]241
242 $self->{'debug_outhandle'} = $docxml_outhandler if ($self->{'debug'}); # STDOUT if debug
243
244 # TODO: also set debugging in begin()? Then stmts creating db and tables also sent to debug out and not executed
245
246 # TODO: remove unused old_unused_saveas from GreenstoneXMLPlugout
[32520]247
[32521]248
249 # 2. overriding saving behaviour to do what the superclass does PLUS saving to sql db
[32518]250
[32523]251 #NOTE: if proc_mode == all, then "breadcrumbs" go into both meta and txt elements of doc.xml:
252 # statements pointing viewer to the sql db for contents
[32522]253
[32523]254 # write the INVERSE into doc.xml as to what is written to the db
255 my $docxml_output_options = { 'output' => docprint::OUTPUT_NONE };
256 if($proc_mode eq "meta_only" ) { # since only meta to go into MySQL db, text will go into docxml
257 $docxml_output_options->{'output'} = docprint::OUTPUT_TEXT_ONLY;
258 } elsif($proc_mode eq "text_only" ) { # since only full text to go into MySQL db, meta will go into docxml
259 $docxml_output_options->{'output'} = docprint::OUTPUT_META_ONLY;
[32518]260 }
[32521]261
[32523]262 # now we've prepared to write out whatever is meant to go into docxml
263 # and can do actual the steps superclass GreenstoneXMLPlugout carries out to write out docxml
264 # So: write out the doc xml file for the current document
265 my $section_text = &docprint::get_section_xml($doc_obj, $docxml_output_options);
266 print $docxml_outhandler $section_text;
267
268
[32518]269 # We also write out whatever needs to go into the MySQL database
270 $self->write_meta_and_text($doc_obj);
271
[32520]272
[32521]273 # 3. post save out
[32526]274 #$self->SUPER::post_saveas(@_);
275 $self->SUPER::post_saveas($doc_obj, $doc_dir, $docxml_outhandler, $output_file);
[32521]276
[32523]277
[32521]278 # database connection is closed in end() method
279 # so we don't open and close over and over for each doc during a single build
[32518]280}
281
282
[32520]283# write meta and/or text PER DOC out to DB
[32518]284sub write_meta_and_text {
285 my $self = shift (@_);
286 my ($doc_obj) = @_;
[32531]287 my $doc_oid = $doc_obj->get_OID(); # this method processes a single doc at a time, so it uses the same OID throughout
[32518]288 my $root_section = $doc_obj->get_top_section();
289
[32520]290 # 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)
291 my $metadata_table_sth = $self->{'metadata_prepared_insert_statement_handle'};
292 my $fulltxt_table_sth = $self->{'fulltxt_prepared_insert_statement_handle'};
[32518]293
[32524]294 $self->recursive_write_meta_and_text($doc_obj, $doc_oid, $root_section, $metadata_table_sth, $fulltxt_table_sth);
[32521]295}
[32518]296
297# Perl: Reading or Writing to Another Program
298# https://nnc3.com/mags/Perl3/cookbook/ch16_05.htm
299sub recursive_write_meta_and_text {
[32520]300 my $self = shift (@_);
301 my ($doc_obj, $doc_oid, $section, $metadata_table_sth, $fulltxt_table_sth) = @_;
302
303 # If section=ROOT, write "root" as section name into table
304 # doc->get_top_section() is the name of the doc root section, which is ""
305 my $section_name = ($section eq "") ? "root" : $section;
[32518]306
307 my $section_ptr = $doc_obj->_lookup_section ($section);
308 return "" unless defined $section_ptr;
309
[32520]310 my $debug_out = $self->{'debug_outhandle'};
[32531]311# print STDERR "#### Meta stmt: " . $metadata_table_sth->{'Statement'} . "\n";
312# print STDERR "#### Full stmt: " . $fulltxt_table_sth->{'Statement'} . "\n";
[32520]313
314 #my $proc_mode = $self->{'process_mode'};
315 #if($proc_mode eq "all" || $proc_mode eq "meta_only" ) {
316 if($metadata_table_sth) { # meta insert statement handle will be undef if not writing meta
317
318 foreach my $data (@{$section_ptr->{'metadata'}}) {
319 my $meta_name = $data->[0];
[32536]320 # TODO: does it need to be stored escaped, as it requires unescaping when read back in
321 # from db (unlike for reading back in from doc.xml)
[32524]322 my $escaped_meta_value = &docprint::escape_text($data->[1]);
[32518]323
[32520]324 # Write out the current section's meta to collection db's METADATA table
325
326 # for each set of values to write to meta table, execute the prepared statement, filling in the values
327
328 if($self->{'debug'}) {
329 # just print the statement we were going to execute
330
[32524]331 print $debug_out $metadata_table_sth->{'Statement'} . "($doc_oid, $section_name, $meta_name, $escaped_meta_value)\n";
[32520]332 }
333 else {
[32533]334
335 $metadata_table_sth->execute($doc_oid, $section_name, $meta_name, $escaped_meta_value)
336 || warn ("Unable to write metadata row to db:\n\tOID $doc_oid, section $section_name,\n\tmeta name: $meta_name, val: $escaped_meta_value");
[32520]337 # Execution failure will print out info anyway: since db connection sets PrintError
338 }
[32518]339 }
340 }
[32520]341
342 #if($proc_mode eq "all" || $proc_mode eq "text_only" ) {
343 if($fulltxt_table_sth) { # fulltxt insert statement handle will be undef if not writing fulltxt
344
345 if($self->{'debug'}) {
346 # just print the statement we were going to execute, minus the fulltxt value
347 my $txt_repr = $section_ptr->{'text'} ? "<TXT>" : "NULL";
[32524]348 print $debug_out $fulltxt_table_sth->{'Statement'} . "($doc_oid, $section_name, $txt_repr)\n";
[32520]349 } else {
[32524]350 my $section_text = &docprint::escape_text($section_ptr->{'text'});
[32520]351
352 # fulltxt column can be SQL NULL. undef value gets written out as NULL:
353 # https://stackoverflow.com/questions/12708633/which-one-represents-null-undef-or-empty-string
354
355 # Write out the current section's text to collection db's FULLTeXT table
[32533]356 $fulltxt_table_sth->execute($doc_oid, $section_name, $section_text)
357 || warn ("Unable to write fulltxt row to db for row:\n\tOID $doc_oid, section $section_name");
[32520]358 # Execution failure will print out info anyway: since db connection sets PrintError
359 }
[32518]360 }
361
362 # output all subsections: RECURSIVE CALL
363 foreach my $subsection (@{$section_ptr->{'subsection_order'}}) {
[32531]364 $self->recursive_write_meta_and_text($doc_obj, $doc_oid, "$section.$subsection", $metadata_table_sth, $fulltxt_table_sth);
[32518]365 }
366}
367
368
3691;
Note: See TracBrowser for help on using the repository browser.