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