########################################################################### # # lucenebuildproc.pm -- perl wrapper for building index with 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. # ########################################################################### package lucenebuildproc; # This document processor outputs a document # for lucene to process # Use same basic XML structure setup by mgppbuilder/mgppbuildproc use mgppbuildproc; use ghtml; sub BEGIN { @lucenebuildproc::ISA = ('mgppbuildproc'); } sub new { my $class = shift @_; my $self = new mgppbuildproc (@_); return bless $self, $class; } sub is_incremental_capable { my $self = shift (@_); # Unlike MG and MGPP, Lucene supports incremental building return 1; } sub preprocess_text { my $self = shift (@_); my ($text, $strip_html, $para) = @_; # call the mgpp method first my ($new_text) = $self->SUPER::preprocess_text($text, $strip_html, $para); # remove entities $new_text =~ s/&\w{1,10};//g; # remove & $new_text =~ s/&//g; return $new_text; } sub text { my $self = shift (@_); my ($doc_obj,$file) = @_; my $handle = $self->{'output_handle'}; my $outhandle = $self->{'outhandle'}; # only output this document if it is one to be indexed return if ($doc_obj->get_doc_type() ne "indexed_doc"); my $indexed_doc = $self->is_subcollection_doc($doc_obj); # this is another document $self->{'num_docs'} += 1; # get the parameters for the output # split on : just in case there is subcoll and lang stuff my ($fields) = split (/:/, $self->{'index'}); my $doc_level = $mgppbuildproc::level_map{'document'}; my $gs2ns = 'xmlns:gs2="http://www.greenstone.org/gs2"'; my $levels = $self->{'levels'}; my $ldoc_level = $levels->{'document'}; my $lsec_level = $levels->{'section'}; my $lpar_level = $levels->{'paragraph'}; my $docid=""; if ($ldoc_level) { if ($self->{'gdbm_level'} eq 'document') { my $doc_sec_num = $self->{'num_docs'}; $docid = "gs2:id=\"$doc_sec_num\""; } else { # default is section level my $doc_sec_num = $self->{'num_sections'}+1; $docid = "gs2:id=\"$doc_sec_num\""; } } my $documenttag = "<$doc_level $gs2ns file=\"$file\" $docid >\n"; my $documentendtag = "\n\n"; my ($sectiontag) = ""; if ($lsec_level) { $sectiontag = $mgppbuildproc::level_map{'section'}; } my ($parastarttag) = ""; my ($paraendtag) = ""; if ($self->{'levels'}->{'paragraph'}) { if ($self->{'strip_html'}) { $parastarttag = "<".$mgppbuildproc::level_map{'paragraph'}.">"; $paraendtag = ""; } else { print $outhandle "Paragraph level can not be used with no_strip_html!. Not indexing Paragraphs.\n"; } } my $doc_section = 0; # just for this document my $text = ""; $text .= $documenttag; # get the text for this document my $section = $doc_obj->get_top_section(); while (defined $section) { # update a few statistics $doc_section++; $self->{'num_sections'} += 1; if ($sectiontag ne "") { my $secid = "gs2:id=\"".$self->{'num_sections'}."\""; $text .= "\n<$sectiontag $secid >\n"; } # if we are doing subcollections, then some docs shouldn't be indexed. # but we need to put the section tag placeholders in there so the # sections match up with gdbm db if (!$indexed_doc) { $text .= "\n\n" if ($sectiontag ne ""); $section = $doc_obj->get_next_section($section); next; } $self->{'num_bytes'} += $doc_obj->get_text_length ($section); foreach my $field (split (/;/, $fields)) { # only deal with this field if it doesn't start with top or # this is the first section my $real_field = $field; next if (($real_field =~ s/^top//) && ($doc_section != 1)); my $new_text = ""; my $tmp_text = ""; # we get allfields by default - do nothing if ($real_field eq "allfields") { } # metadata - output all metadata we know about except gsdl stuff elsif ($real_field eq "metadata") { my $shortname = ""; my $metadata = $doc_obj->get_all_metadata ($section); foreach $pair (@$metadata) { my ($mfield, $mvalue) = (@$pair); # check fields here, maybe others dont want - change to use dontindex!! if ($mfield ne "Identifier" && $mfield !~ /^gsdl/ && $mfield ne "classifytype" && $mfield ne "assocfilepath" && defined $mvalue && $mvalue ne "") { if (defined $self->{'indexfieldmap'}->{$mfield}) { $shortname = $self->{'indexfieldmap'}->{$mfield}; } else { $shortname = $self->create_shortname($mfield); $self->{'indexfieldmap'}->{$mfield} = $shortname; $self->{'indexfieldmap'}->{$shortname} = 1; } $new_text .= "$parastarttag<$shortname index=\"1\">$mvalue$paraendtag\n"; if (!defined $self->{'indexfields'}->{$mfield}) { $self->{'indexfields'}->{$mfield} = 1; } } } } else { #individual metadata and or text specified - could be a comma separated list my $shortname=""; if (defined $self->{'indexfieldmap'}->{$real_field}) { $shortname = $self->{'indexfieldmap'}->{$real_field}; } else { $shortname = $self->create_shortname($real_field); $self->{'indexfieldmap'}->{$real_field} = $shortname; $self->{'indexfieldmap'}->{$shortname} = 1; } my @metadata_list = (); foreach $submeta (split /,/, $real_field) { if ($submeta eq "text") { if ($self->{'indexing_text'}) { #tag the text with ..., add the tags and strip out html if needed $new_text .= "$parastarttag<$shortname index=\"1\">\n"; $tmp_text .= $doc_obj->get_text ($section); if ($parastarttag ne "") { $tmp_text = $self->preprocess_text($tmp_text, $self->{'strip_html'}, "$paraendtag$parastarttag<$shortname index=\"1\">"); } else { # we don't want to individually tag each paragraph if not doing para indexing $tmp_text = $self->preprocess_text($tmp_text, $self->{'strip_html'}, ""); } $new_text .= "$tmp_text$paraendtag\n"; } else { # leave html stuff in, but escape the tags, and dont add Paragraph tags - never retrieve paras at the moment $tmp_text .= $doc_obj->get_text ($section); &ghtml::htmlsafe($tmp_text); $new_text .= $tmp_text; } } else { my @section_metadata = @{$doc_obj->get_metadata ($section, $submeta)}; if ($self->{'indexing_text'} && defined ($self->{'sections_index_document_metadata'})) { if ($self->{'sections_index_document_metadata'} eq "always" || ( scalar(@section_metadata) == 0 && $self->{'sections_index_document_metadata'} eq "unless_section_metadata_exists")) { push (@section_metadata, @{$doc_obj->get_metadata ($doc_obj->get_top_section(), $submeta)}); } } push (@metadata_list, @section_metadata); } } foreach my $item (@metadata_list) { $new_text .= "$parastarttag<$shortname index=\"1\">$item$paraendtag\n"; # remove entities $new_text =~ s/&\w{1,10};//g; # remove & $new_text =~ s/&//g; } } # filter the text $self->filter_text ($field, $new_text); $self->{'num_processed_bytes'} += length ($new_text); $text .= "$new_text"; } # foreach field $text .= "\n\n" if ($sectiontag ne ""); $section = $doc_obj->get_next_section($section); } #while defined section print $handle "$text\n$documentendtag"; #print STDOUT "$text\n$documentendtag"; } 1;