#!/usr/bin/perl -w ########################################################################### # # lucene_passes.pl -- perl wrapper, akin to mgpp_passes, for Lucene # A component of the Greenstone digital library software # from the New Zealand Digital Library Project at the # University of Waikato, New Zealand. # # Copyright (C) 1999 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. # ########################################################################### BEGIN { die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'}; die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'}; unshift (@INC, "$ENV{'GSDLHOME'}/perllib"); unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan"); unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins"); unshift (@INC, "$ENV{'GSDLHOME'}/perllib/classify"); } use util; use ghtml; sub open_java_lucene { my ($doc_tag_level,$full_builddir,$indexdir,$create,$verbosity) = @_; my $bin_java = &util::filename_cat($ENV{'GSDLHOME'},"bin","java"); my $classpath = &util::filename_cat($bin_java,"LuceneWrap.jar"); my $java_lucene = "java -classpath \"$classpath\" GS2LuceneIndexer"; my $cmd_options = "$create -verbosity $verbosity"; my $java_cmd = "$java_lucene $cmd_options $doc_tag_level \"$full_builddir\" $indexdir"; if (!open (PIPEOUT, "| $java_cmd")) { die "$PROGNAME - couldn't run $java_cmd\n"; } } sub close_java_lucene { close(PIPEOUT); } sub save_xml_doc { my ($full_textdir,$output_filename,$doc_xml) = @_; $dir_sep = &util::get_os_dirsep(); my $full_output_filename = &util::filename_cat($full_textdir,$output_filename); my ($full_output_dir) = ($full_output_filename =~ m/^(.*$dir_sep)/x); &util::mk_all_dir($full_output_dir); open(DOCOUT,">$full_output_filename") || die "Unable to open $full_output_filename"; print DOCOUT $doc_xml; close(DOCOUT); my @secs = ($doc_xml =~ m/.*?<\/Sec>/sg); # Currently not used, but consult with DB before removing # foreach my $sec (@secs) { # my ($docnum,$sec_text) = ($sec =~ m/(.*?)<\/Sec>/s); # my $docnum_filename # = &util::filename_cat($full_textdir,"$docnum.xml"); # # open(SECOUT,">$docnum_filename") # || die "Unable to open $docnum_filename"; # print SECOUT &ghtml::unescape_html($sec_text); # close(SECOUT); # } } sub compress_xml_doc { my ($full_textdir,$output_filename) = @_; my $full_output_filename = &util::filename_cat($full_textdir,$output_filename); `gzip $full_output_filename`; } sub monitor_xml_stream { my ($mode, $full_textdir) = @_; my $doc_xml = ""; my $output_filename = ""; my $line; while (defined ($line = )) { $line =~ s/ /&nbsp;/g; $doc_xml .= $line; if ($line =~ m/^$/) { $output_filename = $1; } if ($line =~ m/^<\/Doc>$/) { if ($mode eq "text") { save_xml_doc($full_textdir,$output_filename,$doc_xml); } elsif ($mode eq "index") { # notify lucene indexer # print STDERR $doc_xml; ## print PIPEOUT "$output_filename\n"; print PIPEOUT "$doc_xml"; #save_xml_doc($full_textdir, "$output_filename.txt", $doc_xml); } # compress file ### compress_xml_doc($full_textdir,$output_filename); $doc_xml = ""; $output_filename = ""; } } } sub main { my (@argv) = @_; my $argc = scalar(@argv); my $create = ""; my $verbosity = 1; my @filtered_argv = (); my $i = 0; while ($i<$argc) { if ($argv[$i] =~ m/^-(.*)$/) { my $option = $1; # -create causes build to be incremental if ($option eq ("create")) { $create = "-create"; } # -verbosity num elsif ($option eq "verbosity") { $i++; if ($i<$argc) { $verbosity = $argv[$i]; } } else { print STDERR "Unrecognised minus option: -$option\n"; } } else { push(@filtered_argv,$argv[$i]); } $i++; } my $filtered_argc = scalar(@filtered_argv); if ($filtered_argc < 4) { print STDERR "Usage: $PROGNAME [-create|-verbosity num] \"text\"|\"index\" doc-tag-level build-dir index-name\n"; exit 1; } my $mode = $filtered_argv[0]; my $doc_tag_level = $filtered_argv[1]; my $full_builddir = $filtered_argv[2]; my $indexdir = $filtered_argv[3]; ### print STDERR "**** ARGS = ", join(" ", @argv), "\n"; my $full_textdir = &util::filename_cat($full_builddir,"text"); if ($mode eq "index") { # don't need the lucene stuff if we are just storing the docs open_java_lucene($doc_tag_level,$full_builddir,$indexdir,$create,$verbosity); } monitor_xml_stream($mode, $full_textdir); if ($mode eq "index") { close_java_lucene(); } } $PROGNAME = $0; $PROGNAME =~ s/^.*\/(.*)$/$1/; &main(@ARGV);