source: main/trunk/greenstone2/perllib/lucenebuildproc.pm@ 33369

Last change on this file since 33369 was 33369, checked in by kjdon, 5 years ago

instead of create_shortname, now have get_or_create_shortname. this does the working of looking in fieldnamemap to see if a shortname has already been defined, and saving the new shortnames into the map. get_or_create_sortfield_shortname does the same thing with sortfieldnamemap

  • Property svn:keywords set to Author Date Id Revision
File size: 21.0 KB
Line 
1###########################################################################
2#
3# lucenebuildproc.pm -- perl wrapper for building index with Lucene
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package lucenebuildproc;
27
28# This document processor outputs a document
29# for lucene to process
30
31# Use same basic XML structure setup by mgppbuilder/mgppbuildproc
32
33use mgppbuildproc;
34use ghtml;
35use strict;
36no strict 'refs'; # allow filehandles to be variables and viceversa
37
38
39use IncrementalBuildUtils;
40use FileUtils;
41
42sub BEGIN {
43 @lucenebuildproc::ISA = ('mgppbuildproc');
44}
45
46
47sub new {
48 my $class = shift @_;
49 my $self = new mgppbuildproc (@_);
50
51 $self->{'numincdocs'} = 0;
52 $self->{'specified_fields'} = (); # list of fields actually specified in the index, in a map
53 $self->{'allfields_index'} = 0; # do we need allfields index?
54 $self->{'all_metadata_specified'} = 0; # are we indexing all metadata?
55 $self->{'actualsortfields'} = {}; # sort fields that have actually been used
56 $self->{'sortfieldnamemap'} = {}; # mapping between field name and field shortname, eg dc.Title->byTI
57 return bless $self, $class;
58}
59
60sub set_index {
61 my $self = shift (@_);
62 my ($index, $indexexparr) = @_;
63
64 $self->mgppbuildproc::set_index($index, $indexexparr);
65
66 # just get the list of index fields without any subcoll stuff
67 my ($fields) = split (/:/, $self->{'index'});
68
69 foreach my $field (split (/;/, $fields)) {
70 if ($field eq "allfields") {
71 $self->{'allfields_index'} = 1;
72 } elsif ($field eq "metadata") {
73 $self->{'all_metadata_specified'} = 1;
74 } else {
75 $field =~ s/^top//;
76 $self->{'specified_fields'} ->{$field} = 1;
77 }
78 }
79}
80
81sub set_sections_sort_on_document_metadata {
82 my $self= shift (@_);
83 my ($index_type) = @_;
84
85 $self->{'sections_sort_on_document_metadata'} = $index_type;
86}
87
88sub set_sortfields {
89 my $self = shift (@_);
90
91 my ($sortfields) = @_;
92 $self->{'sortfields'} = ();
93 # lets just go through and check for text, allfields, metadata which are only valid for indexes, not for sortfields
94 foreach my $s (@$sortfields) {
95 if ($s !~ /^(text|allfields|metadata)$/) {
96 push (@{$self->{'sortfields'}}, $s);
97 }
98 }
99}
100
101sub is_incremental_capable
102{
103 my $self = shift (@_);
104
105 # Unlike MG and MGPP, Lucene supports incremental building
106 return 1;
107}
108
109
110sub textedit {
111 my $self = shift (@_);
112 my ($doc_obj,$file,$edit_mode) = @_;
113
114 my $lucenehandle = $self->{'output_handle'};
115 my $outhandle = $self->{'outhandle'};
116
117 # only output this document if it is one to be indexed
118 return if ($doc_obj->get_doc_type() ne "indexed_doc");
119
120 # skip this document if in "compress-text" mode and asked to delete it
121 return if (!$self->get_indexing_text() && ($edit_mode eq "delete"));
122
123 # 0/1 to indicate whether this doc is part of the specified subcollection
124 my $indexed_doc = $self->is_subcollection_doc($doc_obj);
125
126 # this is another document
127 if (($edit_mode eq "add") || ($edit_mode eq "update")) {
128 $self->{'num_docs'} += 1;
129 }
130 else {
131 $self->{'num_docs'} -= 1;
132 }
133
134
135 # get the parameters for the output
136 # split on : just in case there is subcoll and lang stuff
137 my ($fields) = split (/:/, $self->{'index'});
138
139 my $doc_tag_name = $mgppbuildproc::level_map{'document'};
140
141 my $levels = $self->{'levels'};
142 my $ldoc_level = $levels->{'document'};
143 my $lsec_level = $levels->{'section'};
144
145 my $gs2_docOID = $doc_obj->get_OID();
146 my $documenttag = undef;
147 my $documentendtag = undef;
148
149 $documenttag = "<$doc_tag_name xmlns:gs2=\"http://www.greenstone.org/gs2\" file=\"$file\" gs2:docOID=\"$gs2_docOID\" gs2:mode=\"$edit_mode\">\n";
150 $documentendtag = "\n</$doc_tag_name>\n";
151
152 my $sec_tag_name = "";
153 if ($lsec_level)
154 {
155 $sec_tag_name = $mgppbuildproc::level_map{'section'};
156 }
157
158 my $doc_section = 0; # just for this document
159
160 my $text = "";
161 $text .= $documenttag;
162 # get the text for this document
163 my $section = $doc_obj->get_top_section();
164 while (defined $section)
165 {
166 # update a few statistics
167 $doc_section++;
168 $self->{'num_sections'}++;
169
170 my $sec_gs2_id = $self->{'num_sections'};
171 my $sec_gs2_docOID = $gs2_docOID;
172 $sec_gs2_docOID .= ".$section" if ($section ne "");
173
174 # if we are doing subcollections, then some docs shouldn't be indexed.
175 # but we need to put the section tag placeholders in there so the
176 # sections match up with database
177 my $indexed_section = $doc_obj->get_metadata_element($section, "gsdldoctype") || "indexed_section";
178 if (($indexed_doc == 0) || ($indexed_section ne "indexed_section" && $indexed_section ne "indexed_doc")) {
179 if ($sec_tag_name ne "") {
180 $text .= "\n<$sec_tag_name gs2:docOID=\"$sec_gs2_docOID\" gs2:mode=\"ignore\">\n";
181 $text .= "\n</$sec_tag_name>\n"
182 }
183 $section = $doc_obj->get_next_section($section);
184 next;
185 }
186
187 if ($sec_tag_name ne "")
188 {
189 $text .= "\n<$sec_tag_name gs2:docOID=\"$sec_gs2_docOID\" gs2:mode=\"$edit_mode\">\n";
190 }
191
192 if (($edit_mode eq "add") || ($edit_mode eq "update")) {
193 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
194 }
195 else {
196 # delete
197 $self->{'num_bytes'} -= $doc_obj->get_text_length ($section);
198 }
199
200
201 # collect up all the text for allfields index in here (if there is one)
202 my $allfields_text = "";
203
204 foreach my $field (split (/;/, $fields)) {
205
206 # only deal with this field if it doesn't start with top or
207 # this is the first section
208 my $real_field = $field;
209 next if (($real_field =~ s/^top//) && ($doc_section != 1));
210
211 # process these two later
212 next if ($real_field eq "allfields" || $real_field eq "metadata");
213
214 #individual metadata and or text specified - could be a comma separated list
215 #$specified_fields->{$real_field} = 1;
216 my $shortname="";
217 my $new_field = 0; # have we found a new field name?
218 $shortname = $self->get_or_create_shortname($real_field);
219 my @metadata_list = (); # put any metadata values in here
220 my $section_text = ""; # put the text in here
221 foreach my $submeta (split /,/, $real_field) {
222 if ($submeta eq "text") {
223 # no point in indexing text more than once
224 if ($section_text eq "") {
225 $section_text = $doc_obj->get_text($section);
226 if ($self->{'indexing_text'}) {
227 # we always strip html
228 $section_text = $self->preprocess_text($section_text, 1, "");
229 }
230 else {
231 # leave html stuff in, but escape the tags
232 &ghtml::htmlsafe($section_text);
233 }
234 }
235 }
236 else {
237 $submeta =~ s/^ex\.([^.]+)$/$1/; #strip off ex. iff it's the only metadata set prefix (will leave ex.dc.* intact)
238
239 # its a metadata element
240 my @section_metadata = @{$doc_obj->get_metadata ($section, $submeta)};
241 if ($section ne $doc_obj->get_top_section() && $self->{'indexing_text'} && defined ($self->{'sections_index_document_metadata'})) {
242 if ($self->{'sections_index_document_metadata'} eq "always" || ( scalar(@section_metadata) == 0 && $self->{'sections_index_document_metadata'} eq "unless_section_metadata_exists")) {
243 push (@section_metadata, @{$doc_obj->get_metadata ($doc_obj->get_top_section(), $submeta)});
244 }
245 }
246 push (@metadata_list, @section_metadata);
247 }
248 } # for each field in this one index
249
250
251 # now we add the text and/or metadata into new_text
252 if ($section_text ne "" || scalar(@metadata_list)) {
253 my $new_text = "";
254
255 if ($section_text ne "") {
256
257 if ($self->{'allfields_index'}) {
258 $allfields_text .= "$section_text ";
259 }
260
261 if ($self->{'indexing_text'}) {
262 # add the tag
263 $new_text .= "<$shortname index=\"1\">$section_text</$shortname>";
264 $self->{'allindexfields'}->{$real_field} = 1;
265 } else {
266 $new_text .= "$section_text ";
267 }
268 }
269
270 foreach my $item (@metadata_list) {
271 &ghtml::htmlsafe($item);
272
273 if ($self->{'allfields_index'}) {
274 $allfields_text .= "$item ";
275 }
276
277 if ($self->{'indexing_text'}) {
278 # add the tag
279 $new_text .= "<$shortname index=\"1\">$item</$shortname>";
280 $self->{'allindexfields'}->{$real_field} = 1;
281 } else {
282 $new_text .= "$item ";
283 }
284 } # end for loop processing @metadata_list
285
286 # filter the text
287 $new_text = $self->filter_text ($field, $new_text);
288
289 if (($edit_mode eq "add") || ($edit_mode eq "update")) {
290 $self->{'num_processed_bytes'} += length ($new_text);
291 $text .= "$new_text";
292 }
293 else {
294 # delete
295 $self->{'num_processed_bytes'} -= length ($new_text);
296 }
297 }
298
299 } # foreach field
300
301 if ($self->{'all_metadata_specified'}) {
302
303 my $new_text = "";
304 my $shortname = "";
305 my $metadata = $doc_obj->get_all_metadata ($section);
306 foreach my $pair (@$metadata) {
307 my ($mfield, $mvalue) = (@$pair);
308 # no value
309 next unless defined $mvalue && $mvalue ne "";
310 # we have already indexed this
311 next if defined ($self->{'specified_fields'}->{$mfield});
312 # check fields here, maybe others dont want - change to use dontindex!!
313 next if ($mfield eq "Identifier" || $mfield eq "classifytype" || $mfield eq "assocfilepath");
314 next if ($mfield =~ /^gsdl/);
315
316 &ghtml::htmlsafe($mvalue);
317
318 $shortname = $self->get_or_create_shortname($mfield);
319 $self->{'allindexfields'}->{$mfield} = 1;
320 $new_text .= "<$shortname index=\"1\">$mvalue</$shortname>\n";
321 if ($self->{'allfields_index'}) {
322 $allfields_text .= "$mvalue ";
323 }
324
325 if (!defined $self->{'extraindexfields'}->{$mfield}) {
326 $self->{'extraindexfields'}->{$mfield} = 1;
327 }
328
329 }
330 # filter the text
331 $new_text = $self->filter_text ("metadata", $new_text);
332
333 if (($edit_mode eq "add") || ($edit_mode eq "update")) {
334 $self->{'num_processed_bytes'} += length ($new_text);
335 $text .= "$new_text";
336 }
337 else {
338 # delete
339 $self->{'num_processed_bytes'} -= length ($new_text);
340 }
341 }
342
343 if ($self->{'allfields_index'}) {
344
345 my $new_text = "<ZZ index=\"1\">$allfields_text</ZZ>\n";
346 # filter the text
347 $new_text = $self->filter_text ("allfields", $new_text);
348
349 if (($edit_mode eq "add") || ($edit_mode eq "update")) {
350 $self->{'num_processed_bytes'} += length ($new_text);
351 $text .= "$new_text";
352 }
353 else {
354 # delete
355 $self->{'num_processed_bytes'} -= length ($new_text);
356 }
357 }
358 # only add sort fields for this section if we are indexing this section, we are doing section level indexing or this is the top section
359 if ($self->{'indexing_text'} && ($sec_tag_name ne "" || $doc_section == 1 )) {
360 # add sort fields if there are any
361
362 foreach my $sfield (@{$self->{'sortfields'}}) {
363 # ignore special field rank
364 next if ($sfield eq "rank" || $sfield eq "none");
365 my $sf_shortname = $self->get_or_create_sortfield_shortname($sfield);
366
367 my @metadata_list = (); # put any metadata values in here
368 foreach my $submeta (split /,/, $sfield) {
369 $submeta =~ s/^ex\.([^.]+)$/$1/; #strip off ex. iff it's the only metadata set prefix (will leave ex.dc.* intact)
370
371 my @section_metadata = @{$doc_obj->get_metadata ($section, $submeta)};
372 if ($section ne $doc_obj->get_top_section() && defined ($self->{'sections_sort_on_document_metadata'})) {
373 if ($self->{'sections_sort_on_document_metadata'} eq "always" || ( scalar(@section_metadata) == 0 && $self->{'sections_sort_on_document_metadata'} eq "unless_section_metadata_exists")) {
374 push (@section_metadata, @{$doc_obj->get_metadata ($doc_obj->get_top_section(), $submeta)});
375 }
376 }
377 push (@metadata_list, @section_metadata);
378 }
379
380 my $new_text = "";
381 foreach my $item (@metadata_list) {
382 &ghtml::htmlsafe($item);
383 $new_text .= "$item ";
384 }
385 if ($new_text =~ /\S/) {
386 $new_text = "<$sf_shortname index=\"1\" tokenize=\"0\">$new_text</$sf_shortname>";
387 $text .= "$new_text"; # add it to the main text block
388 $self->{'actualsortfields'}->{$sfield} = 1;
389 }
390 }
391 }
392 $text .= "\n</$sec_tag_name>\n" if ($sec_tag_name ne "");
393
394 $section = $doc_obj->get_next_section($section);
395 } # for each section
396
397 #open (TEXTOUT, ">text.out");
398 #print TEXTOUT "$text\n$documentendtag";
399 #close TEXTOUT;
400
401 print $lucenehandle "$text\n$documentendtag";
402
403## if ($edit_mode eq "delete") {
404## print STDERR "$text\n$documentendtag";
405## }
406
407}
408
409sub text {
410 my $self = shift (@_);
411 my ($doc_obj,$file) = @_;
412
413 $self->textedit($doc_obj,$file,"add");
414}
415
416sub textreindex
417{
418 my $self = shift (@_);
419 my ($doc_obj,$file) = @_;
420
421 $self->textedit($doc_obj,$file,"update");
422}
423
424sub textdelete
425{
426 my $self = shift (@_);
427 my ($doc_obj,$file) = @_;
428
429 $self->textedit($doc_obj,$file,"delete");
430}
431
432
433
434
435
436# /** We make this builder pretend to be a document processor so we can get
437# * information back from the plugins.
438# *
439# * @param $self A reference to this Lucene builder
440# * @param $doc_obj A reference to a document object representing what was
441# * parsed by the GAPlug
442# * @param $file The name of the file parsed as a string
443# *
444# * @author John Thompson, DL Consulting Ltd
445# */
446sub process()
447 {
448 my $self = shift (@_);
449 my ($doc_obj, $file) = @_;
450
451 # If this is called from any stage other than an incremental infodb we want
452 # to pass through to the superclass of build
453 if ($self->get_mode() eq "incinfodb")
454 {
455 print STDERR "*** Processing a document added using INCINFODB ***\n" if ($self->{'verbosity'} > 3);
456 my ($archivedir) = $file =~ /^(.*?)(?:\/|\\)[^\/\\]*$/;
457 $archivedir = "" unless defined $archivedir;
458 $archivedir =~ s/\\/\//g;
459 $archivedir =~ s/^\/+//;
460 $archivedir =~ s/\/+$//;
461
462 # Number of files
463 print STDERR "There are " . scalar(@{$doc_obj->get_assoc_files()}) . " associated documents...\n" if ($self->{'verbosity'} > 3);
464
465 # resolve the final filenames of the files associated with this document
466 $self->assoc_files ($doc_obj, $archivedir);
467
468 # is this a paged or a hierarchical document
469 my ($thistype, $childtype) = $self->get_document_type ($doc_obj);
470
471 # Determine the actual docnum by checking if we've processed any
472 # previous incrementally added documents. If so, carry on from there.
473 # Otherwise we set the counter to be the same as the number of
474 # sections encountered during the previous build
475 if ($self->{'numincdocs'} == 0)
476 {
477 $self->{'numincdocs'} = $self->{'starting_num_sections'} + 1;
478 }
479
480 my $section = $doc_obj->get_top_section ();
481 print STDERR "+ top section: '$section'\n" if ($self->{'verbosity'} > 3);
482 my $doc_OID = $doc_obj->get_OID();
483 my $url = "";
484 while (defined $section)
485 {
486 print STDERR "+ processing section: '$section'\n" if ($self->{'verbosity'} > 3);
487 # Attach all the other metadata to this document
488 # output the fact that this document is a document (unless doctype
489 # has been set to something else from within a plugin
490 my $dtype = $doc_obj->get_metadata_element ($section, "doctype");
491 if (!defined $dtype || $dtype !~ /\w/)
492 {
493 #$doc_obj->add_utf8_metadata($section, "doctype", $dtype);
494 $doc_obj->add_utf8_metadata($section, "doctype", "doc");
495 }
496 # output whether this node contains text
497 if ($doc_obj->get_text_length($section) > 0)
498 {
499 $doc_obj->add_utf8_metadata($section, "hastxt", 1);
500 }
501 else
502 {
503 $doc_obj->add_utf8_metadata($section, "hastxt", 0);
504 }
505
506 # output archivedir if at top level
507 if ($section eq $doc_obj->get_top_section())
508 {
509 $doc_obj->add_utf8_metadata($section, "archivedir", $archivedir);
510 $doc_obj->add_utf8_metadata($section, "thistype", $thistype);
511 }
512
513 # output a list of children
514 my $children = $doc_obj->get_children ($section);
515 if (scalar(@$children) > 0)
516 {
517 $doc_obj->add_utf8_metadata($section, "childtype", $childtype);
518 my @contains = ();
519 foreach my $child (@$children)
520 {
521 if ($child =~ /^.*?\.(\d+)$/)
522 {
523 push (@contains, "\".$1");
524 }
525 else
526 {
527 push (@contains, "\".$child");
528 }
529 }
530 $doc_obj->add_utf8_metadata($section, "contains", join(";", @contains));
531 }
532 #output the matching doc number
533 print STDERR "+ docnum=" . $self->{'numincdocs'} . "\n" if ($self->{'verbosity'} > 3);
534 $doc_obj->add_utf8_metadata($section, "docnum", $self->{'numincdocs'});
535
536 $self->{'numincdocs'}++;
537 $section = $doc_obj->get_next_section($section);
538 # if no sections wanted, only add the docs
539 last if ($self->{'db_level'} eq "document");
540 }
541 print STDERR "\n*** incrementally add metadata from document at: " . $file . "\n" if ($self->{'verbosity'} > 3);
542 &IncrementalBuildUtils::addDocument($self->{'collection'}, $self->{'infodbtype'}, $doc_obj, $doc_obj->get_top_section());
543 }
544 else
545 {
546 $self->mgppbuildproc::process(@_);
547 }
548 }
549# /** process() **/
550
551
552# Following methods seem to be no different to those defined in basebuildproc.pm
553# From inspection, it looks like these ones can be removed
554
555
556sub get_num_docs {
557 my $self = shift (@_);
558 #rint STDERR "get_num_docs(): $self->{'num_docs'}\n";
559 return $self->{'num_docs'};
560}
561
562sub get_num_sections {
563 my $self = shift (@_);
564 #rint STDERR "get_num_sections(): $self->{'num_sections'}\n";
565 return $self->{'num_sections'};
566}
567
568# num_bytes is the actual number of bytes in the collection
569# this is normally the same as what's processed during text compression
570sub get_num_bytes {
571 my $self = shift (@_);
572 #rint STDERR "get_num_bytes(): $self->{'num_bytes'}\n";
573 return $self->{'num_bytes'};
574}
575
576
577# This is similar to mgppbuildproc's preprocess_text but adds extra spaces
578# Otherwise the removal of tags below might lead to Lucene turning
579# "...farming</p>\n<p>EDWARD.." into "farmingedward"
580# (example from demo collection b20cre)
581# Many thanks to John Thompson, DL Consulting Ltd. (www.dlconsulting.com)
582sub preprocess_text
583{
584 my $self = shift (@_);
585 my ($text, $strip_html, $para) = @_;
586 # at this stage, we do not do paragraph tags unless have strip_html -
587 # it will result in a huge mess of non-xml
588 return unless $strip_html;
589
590 my $new_text = $text;
591
592 # if we have <pre> tags, we can have < > inside them, need to delete
593 # the <> before stripping tags
594 $new_text =~ s/<pre>(.*?)<\/pre>/$self->remove_gtlt($1,$para)/gse;
595
596 if ($para eq "") {
597 # just remove all tags
598 $new_text =~ s/<[^>]*>/ /gs;
599 } else {
600 # strip all tags except <p> tags which get turned into $para
601 $new_text =~ s/<([^>]*)>/$self->process_tags($1, $para)/gse;
602 }
603
604 # It's important that we remove name entities because otherwise the text passed to Lucene for indexing
605 # may not be valid XML (eg. if HTML-only entities like &nbsp; are used)
606 $new_text =~ s/&\w{1,10};//g;
607 # Remove stray '&' characters, except in &#nnnn; or &#xhhhh; entities (which are valid XML)
608 $new_text =~ s/&([^\#])/ $1/g;
609
610 return $new_text;
611}
612
613sub delete_assoc_files
614{
615 my $self = shift (@_);
616 my ($archivedir, $edit_mode) = @_;
617
618 $self->basebuildproc::delete_assoc_files(@_);
619
620 if ($edit_mode eq "delete") {
621 # if we are deleting the doc, then also delete the lucene text version
622 my $assoc_dir = &FileUtils::filenameConcatenate($self->{'build_dir'},"text", $archivedir);
623 if (-d $assoc_dir) {
624 &FileUtils::removeFilesRecursive($assoc_dir);
625 }
626 }
627}
628
629sub get_or_create_sortfield_shortname {
630 my $self = shift(@_);
631
632 my ($realname) = @_;
633
634 if (defined $self->{'sortfieldnamemap'}->{$realname}) {
635 return $self->{'sortfieldnamemap'}->{$realname};
636 }
637
638 # get the shortname made for this index field
639
640 my $shortname = $self->get_or_create_shortname($realname);
641 $shortname = "by".$shortname;
642
643 $self->{'sortfieldnamemap'}->{$realname} = $shortname;
644 $self->{'sortfieldnamemap'}->{$shortname} = 1;
645 return $shortname;
646}
647
648
6491;
650
651
Note: See TracBrowser for help on using the repository browser.