source: main/trunk/greenstone2/bin/script/lucene_passes.pl@ 28461

Last change on this file since 28461 was 28461, checked in by davidb, 11 years ago

Further tweak needed to support operation of script from Cygwin

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 6.0 KB
RevLine 
[8520]1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# lucene_passes.pl -- perl wrapper, akin to mgpp_passes, for Lucene
6# A component of the Greenstone digital library software
[12844]7# from the New Zealand Digital Library Project at the
[8520]8# University of Waikato, New Zealand.
9#
10# Copyright (C) 1999 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
28
29BEGIN {
30 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
31 die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
32 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
33}
34
[16263]35
36use strict;
[8520]37use util;
[27331]38use FileUtils;
[8520]39
40sub open_java_lucene
41{
[16264]42 my ($doc_tag_level,$full_builddir,$indexdir,$java_lucene_options) = @_;
[8520]43
[27331]44 # Is there a collection-specific bin/java/LuceneWrapper3.jar file?
45 my $bin_java = &FileUtils::filenameConcatenate($ENV{'GSDLCOLLECTDIR'},"bin","java");
[28402]46 my $classpath = &FileUtils::javaFilenameConcatenate($bin_java,"LuceneWrapper3.jar");
[19764]47 if (!-f $classpath)
48 {
49 # No, so use the Greenstone one
[27331]50 $bin_java = &FileUtils::filenameConcatenate($ENV{'GSDLHOME'},"bin","java");
[28402]51 $classpath = &FileUtils::javaFilenameConcatenate($bin_java,"LuceneWrapper3.jar");
[21323]52 if(!-f $classpath) {
53 die "***** ERROR: $classpath does not exist\n";
54 }
[19764]55 }
[8520]56
[28461]57 $full_builddir = &util::makeFilenameJavaCygwinCompatible($full_builddir);
58
[27331]59 my $java_lucene = "java -classpath \"$classpath\" org.greenstone.LuceneWrapper3.GS2LuceneIndexer";
[16264]60 my $java_cmd = "$java_lucene $java_lucene_options $doc_tag_level \"$full_builddir\" $indexdir";
[8520]61
[16263]62 open (PIPEOUT, "| $java_cmd") or die "lucene_passes.pl - couldn't run $java_cmd\n";
[8520]63}
64
[16263]65
[8520]66sub close_java_lucene
67{
[12844]68 close(PIPEOUT);
[8520]69}
70
[16263]71
[8520]72sub save_xml_doc
73{
74 my ($full_textdir,$output_filename,$doc_xml) = @_;
75
[16263]76 my $dir_sep = &util::get_os_dirsep();
77
[27331]78 my $full_output_filename = &FileUtils::filenameConcatenate($full_textdir,$output_filename);
[8520]79 my ($full_output_dir) = ($full_output_filename =~ m/^(.*$dir_sep)/x);
[27331]80 &FileUtils::makeAllDirectories($full_output_dir);
[12844]81
82 open(DOCOUT,">$full_output_filename")
[8520]83 || die "Unable to open $full_output_filename";
84
85 print DOCOUT $doc_xml;
86 close(DOCOUT);
[10163]87
88 my @secs = ($doc_xml =~ m/<Sec\s+gs2:id="\d+"\s*>.*?<\/Sec>/sg);
[8520]89}
90
[16263]91
[8520]92sub compress_xml_doc
93{
94 my ($full_textdir,$output_filename) = @_;
95
[12844]96 my $full_output_filename
[27331]97 = &FileUtils::filenameConcatenate($full_textdir,$output_filename);
[8520]98
99 `gzip $full_output_filename`;
100}
101
[16263]102
[12844]103# This appears to be the callback that gets the xml stream during the
104# build process, so I need to intercept it here and call my XML RPC
105# to insert into the Lucene database.
[8520]106sub monitor_xml_stream
107{
[9177]108 my ($mode, $full_textdir) = @_;
[8520]109
110 my $doc_xml = "";
111 my $output_filename = "";
112
113 my $line;
114 while (defined ($line = <STDIN>)) {
115 $doc_xml .= $line;
116 if ($line =~ m/^<Doc.+file=\"(.*?)\".*>$/) {
117 $output_filename = $1;
118
119 }
[9177]120
[8520]121 if ($line =~ m/^<\/Doc>$/) {
[9177]122 if ($mode eq "text") {
123 save_xml_doc($full_textdir,$output_filename,$doc_xml);
124 } elsif ($mode eq "index") {
125 # notify lucene indexer
[12003]126
127 # SAX parser seems to be sensitive to blank lines
128 # => remove them
129 $doc_xml =~ s/\n+/\n/g;
130
131# print STDERR $doc_xml;
132
[8520]133## print PIPEOUT "$output_filename\n";
[12003]134
[9177]135 print PIPEOUT "$doc_xml";
[12003]136
137
[9918]138 #save_xml_doc($full_textdir, "$output_filename.txt", $doc_xml);
[9177]139 }
[8520]140 # compress file
141### compress_xml_doc($full_textdir,$output_filename);
142
143 $doc_xml = "";
144 $output_filename = "";
145 }
146 }
147}
148
[12844]149
150# /** This checks the arguments on the command line, filters the
151# * unknown command line arguments and then calls the open_java_lucene
152# * function to begin processing. Most of the arguments are passed on
153# * the command line of the java wrapper.
154# *
155# */
[8520]156sub main
157{
[12844]158 my (@argv) = @_;
159 my $argc = scalar(@argv);
[8520]160
[16264]161 my $java_lucene_options = "";
[12844]162 my @filtered_argv = ();
[10163]163
[12844]164 my $i = 0;
165 while ($i<$argc) {
166 if ($argv[$i] =~ m/^\-(.*)$/) {
[10163]167
[12844]168 my $option = $1;
[10163]169
[16264]170 # -removeold causes the existing index to be overwritten
171 if ($option eq "removeold") {
172 print STDERR "\n-removeold set\n";
173 $java_lucene_options .= "-removeold ";
[12844]174 }
[16264]175 # -verbosity <num>
[12844]176 elsif ($option eq "verbosity") {
177 $i++;
[16264]178 if ($i<$argc)
179 {
180 $java_lucene_options .= "-verbosity " . $argv[$i];
[12844]181 }
182 }
183 else {
184 print STDERR "Unrecognised minus option: -$option\n";
185 }
[10163]186 }
[12844]187 else {
188 push(@filtered_argv,$argv[$i]);
189 }
190 $i++;
191 }
[10163]192
[12844]193 my $filtered_argc = scalar(@filtered_argv);
[10163]194
[12844]195 if ($filtered_argc < 4) {
[16264]196 print STDERR "Usage: lucene_passes.pl [-removeold|-verbosity num] \"text\"|\"index\" doc-tag-level build-dir index-name\n";
[12844]197 exit 1;
198 }
[8520]199
[12844]200 my $mode = $filtered_argv[0];
201 my $doc_tag_level = $filtered_argv[1];
202 my $full_builddir = $filtered_argv[2];
203 my $indexdir = $filtered_argv[3];
[10163]204### print STDERR "**** ARGS = ", join(" ", @argv), "\n";
[8520]205
[16263]206 # We only need the Lucene handle opened if we are indexing the documents, not if we are just storing the text
[12844]207 if ($mode eq "index") {
[16264]208 open_java_lucene($doc_tag_level, $full_builddir, $indexdir, $java_lucene_options);
[12844]209 }
[16263]210
[12844]211 print STDERR "Monitoring for input!\n";
[27331]212 my $full_textdir = &FileUtils::filenameConcatenate($full_builddir,"text");
[28461]213
[12844]214 monitor_xml_stream($mode, $full_textdir);
[16263]215
[12844]216 if ($mode eq "index") {
217 close_java_lucene();
218 }
[8520]219}
220
221
222&main(@ARGV);
Note: See TracBrowser for help on using the repository browser.