source: gsdl/trunk/bin/script/lucene_passes.pl@ 14924

Last change on this file since 14924 was 14924, checked in by mdewsnip, 16 years ago

Removed a hack for escaping " " entities -- this (and all the other HTML entities) are now handled better in lucenebuildproc.pm.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 KB
Line 
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
7# from the New Zealand Digital Library Project at the
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 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
34 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins");
35 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/classify");
36}
37
38use util;
39use ghtml;
40
41sub open_java_lucene
42{
43 my ($doc_tag_level,$full_builddir,$indexdir,$create,$verbosity) = @_;
44
45 my $bin_java = &util::filename_cat($ENV{'GSDLHOME'},"bin","java");
46 my $classpath = &util::filename_cat($bin_java,"LuceneWrapper.jar");
47
48 my $java_lucene = "java -classpath \"$classpath\" org.greenstone.LuceneWrapper.GS2LuceneIndexer";
49 my $cmd_options = "$create -verbosity $verbosity";
50 my $java_cmd = "$java_lucene $cmd_options $doc_tag_level \"$full_builddir\" $indexdir";
51
52 open (PIPEOUT, "| $java_cmd") or die "$PROGNAME - couldn't run $java_cmd\n";
53}
54
55sub close_java_lucene
56{
57 close(PIPEOUT);
58}
59
60sub save_xml_doc
61{
62 my ($full_textdir,$output_filename,$doc_xml) = @_;
63 $dir_sep = &util::get_os_dirsep();
64
65 my $full_output_filename = &util::filename_cat($full_textdir,$output_filename);
66 my ($full_output_dir) = ($full_output_filename =~ m/^(.*$dir_sep)/x);
67 &util::mk_all_dir($full_output_dir);
68
69 open(DOCOUT,">$full_output_filename")
70 || die "Unable to open $full_output_filename";
71
72 print DOCOUT $doc_xml;
73 close(DOCOUT);
74
75 my @secs = ($doc_xml =~ m/<Sec\s+gs2:id="\d+"\s*>.*?<\/Sec>/sg);
76}
77
78sub compress_xml_doc
79{
80 my ($full_textdir,$output_filename) = @_;
81
82 my $full_output_filename
83 = &util::filename_cat($full_textdir,$output_filename);
84
85 `gzip $full_output_filename`;
86}
87
88# This appears to be the callback that gets the xml stream during the
89# build process, so I need to intercept it here and call my XML RPC
90# to insert into the Lucene database.
91sub monitor_xml_stream
92{
93 my ($mode, $full_textdir) = @_;
94
95 my $doc_xml = "";
96 my $output_filename = "";
97
98 my $line;
99 while (defined ($line = <STDIN>)) {
100 $doc_xml .= $line;
101 if ($line =~ m/^<Doc.+file=\"(.*?)\".*>$/) {
102 $output_filename = $1;
103
104 }
105
106 if ($line =~ m/^<\/Doc>$/) {
107 if ($mode eq "text") {
108 save_xml_doc($full_textdir,$output_filename,$doc_xml);
109 } elsif ($mode eq "index") {
110 # notify lucene indexer
111
112 # SAX parser seems to be sensitive to blank lines
113 # => remove them
114 $doc_xml =~ s/\n+/\n/g;
115
116# print STDERR $doc_xml;
117
118## print PIPEOUT "$output_filename\n";
119
120 print PIPEOUT "$doc_xml";
121
122
123 #save_xml_doc($full_textdir, "$output_filename.txt", $doc_xml);
124 }
125 # compress file
126### compress_xml_doc($full_textdir,$output_filename);
127
128 $doc_xml = "";
129 $output_filename = "";
130 }
131 }
132}
133
134
135# /** This checks the arguments on the command line, filters the
136# * unknown command line arguments and then calls the open_java_lucene
137# * function to begin processing. Most of the arguments are passed on
138# * the command line of the java wrapper.
139# *
140# * Do not set -create and -remove at the same time, although -create is
141# * required for -remove, -remove will set it it's self, if you set -create
142# * after -remove the create will be ignored.
143# *
144# * @version 2.0 Added support for removing documents from the index by John Rowe
145# *
146# * @author John Rowe, DL Consulting
147# */
148sub main
149{
150 my (@argv) = @_;
151 my $argc = scalar(@argv);
152
153 my $create = "";
154 my $verbosity = 1;
155
156 my @filtered_argv = ();
157
158 my $i = 0;
159 while ($i<$argc) {
160 if ($argv[$i] =~ m/^\-(.*)$/) {
161
162 my $option = $1;
163
164 # -create causes build to be incremental
165 if ($option eq ("create")) {
166 print STDERR "\n\n-create set\n";
167 $create = "-create";
168 }
169 # In a blinding flash of unintuitiveness -remove causes
170 # -create to be set (we don't want to remove the old indexes)
171 elsif($option eq "remove")
172 {
173 # Look at the next arg for the oid and if that doesn't exist then
174 $i++;
175 if(!defined $argv[$i])
176 {
177 print STDERR "Remove was specified but the OID was not specified";
178 die "\n\nCannot continue";
179 }
180 $removeoid = $argv[$i];
181 print STDERR "\n\nWe're removing the document with id: '$removeoid'\n";
182
183 # Now, to make sure this gets through to the Java executable
184 $create = "-create -remove '$removeoid'";
185 }
186
187 # -verbosity num
188 elsif ($option eq "verbosity") {
189 $i++;
190 if ($i<$argc) {
191 $verbosity = $argv[$i];
192 }
193 }
194 else {
195 print STDERR "Unrecognised minus option: -$option\n";
196 }
197 }
198 else {
199 push(@filtered_argv,$argv[$i]);
200 }
201 $i++;
202 }
203
204 my $filtered_argc = scalar(@filtered_argv);
205
206 if ($filtered_argc < 4) {
207 print STDERR "Usage: $PROGNAME [-create|-verbosity num] \"text\"|\"index\" doc-tag-level build-dir index-name\n";
208 exit 1;
209 }
210
211 my $mode = $filtered_argv[0];
212 my $doc_tag_level = $filtered_argv[1];
213 my $full_builddir = $filtered_argv[2];
214 my $indexdir = $filtered_argv[3];
215### print STDERR "**** ARGS = ", join(" ", @argv), "\n";
216
217 my $full_textdir = &util::filename_cat($full_builddir,"text");
218
219 if ($mode eq "index") {
220# don't need the lucene stuff if we are just storing the docs
221 open_java_lucene($doc_tag_level,$full_builddir,$indexdir,$create,$verbosity);
222 }
223 print STDERR "Monitoring for input!\n";
224 monitor_xml_stream($mode, $full_textdir);
225 if ($mode eq "index") {
226 close_java_lucene();
227 }
228}
229
230$PROGNAME = $0;
231$PROGNAME =~ s/^.*\/(.*)$/$1/;
232
233&main(@ARGV);
234
Note: See TracBrowser for help on using the repository browser.