source: gsdl/trunk/perllib/basebuildproc.pm@ 17579

Last change on this file since 17579 was 17579, checked in by kjdon, 16 years ago

removed a debug print statement

  • Property svn:keywords set to Author Date Id Revision
File size: 18.7 KB
RevLine 
[9919]1###########################################################################
2#
3# basebuildproc.pm --
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
26# This document processor outputs a document for indexing (should be
[15688]27# implemented by subclass) and storing in the database
[9919]28
29package basebuildproc;
30
31eval {require bytes};
32
33use classify;
[15699]34use dbutil;
[9919]35use doc;
36use docproc;
[15696]37use strict; no strict 'subs';
[9919]38use util;
39
40BEGIN {
41 @basebuildproc::ISA = ('docproc');
42}
43
[12844]44sub new()
45 {
46 my ($class, $collection, $source_dir, $build_dir, $keepold, $verbosity, $outhandle) = @_;
[9919]47 my $self = new docproc ();
48
49 # outhandle is where all the debugging info goes
50 # output_handle is where the output of the plugins is piped
[15688]51 # to (i.e. mg, database etc.)
[9919]52 $outhandle = STDERR unless defined $outhandle;
53
54 $self->{'collection'} = $collection;
55 $self->{'source_dir'} = $source_dir;
[10159]56 $self->{'build_dir'} = $build_dir;
57 $self->{'keepold'} = $keepold;
58 $self->{'verbosity'} = $verbosity;
59 $self->{'outhandle'} = $outhandle;
[9919]60
61 $self->{'classifiers'} = [];
62 $self->{'mode'} = "text";
63 $self->{'assocdir'} = $build_dir;
[15688]64 $self->{'dontdb'} = {};
[16222]65 $self->{'store_metadata_coverage'} = "false";
[9919]66
67 $self->{'index'} = "section:text";
68 $self->{'indexexparr'} = [];
69
[17110]70 $self->{'separate_cjk'} = 0;
71
[10159]72 my $found_num_data = 0;
73 my $buildconfigfile = undef;
74
75 if ($keepold) {
76 # For incremental building need to seed num_docs etc from values
77 # stored in build.cfg (if present)
78 $buildconfigfile = &util::filename_cat($build_dir, "build.cfg");
79 if (-e $buildconfigfile) {
80 $found_num_data = 1;
81 }
82 else {
83 # try the index dir
84 $buildconfigfile = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},
85 "index", "build.cfg");
86 if (-e $buildconfigfile) {
87 $found_num_data = 1;
88 }
89 }
90
[12844]91 }
[10159]92
[12844]93 if ($found_num_data)
94 {
95 #print STDERR "Found_Num_Data!\n";
[10159]96 my $buildcfg = &colcfg::read_build_cfg($buildconfigfile);
97 $self->{'starting_num_docs'} = $buildcfg->{'numdocs'};
[12844]98 #print STDERR "- num_docs: $self->{'starting_num_docs'}\n";
[10159]99 $self->{'starting_num_sections'} = $buildcfg->{'numsections'};
[12844]100 #print STDERR "- num_sections: $self->{'starting_num_sections'}\n";
[10159]101 $self->{'starting_num_bytes'} = $buildcfg->{'numbytes'};
[12844]102 #print STDERR "- num_bytes: $self->{'starting_num_bytes'}\n";
[10159]103 }
[12844]104 else
105 {
106 #print STDERR "NOT Found_Num_Data!\n";
107 $self->{'starting_num_docs'} = 0;
[10159]108 $self->{'starting_num_sections'} = 0;
109 $self->{'starting_num_bytes'} = 0;
[12844]110 }
[10159]111
[9919]112 $self->{'output_handle'} = "STDOUT";
[10159]113 $self->{'num_docs'} = $self->{'starting_num_docs'};
114 $self->{'num_sections'} = $self->{'starting_num_sections'};
115 $self->{'num_bytes'} = $self->{'starting_num_bytes'};
116
[9919]117 $self->{'num_processed_bytes'} = 0;
118 $self->{'store_text'} = 1;
119
[15685]120 # what level (section/document) the database - indexer intersection is
121 $self->{'db_level'} = "section";
[9919]122 #used by browse interface
123 $self->{'doclist'} = [];
124
125 $self->{'indexing_text'} = 0;
126
127 return bless $self, $class;
128
129}
130
131sub reset {
132 my $self = shift (@_);
[10159]133
134 $self->{'num_docs'} = $self->{'starting_num_docs'};
135 $self->{'num_sections'} = $self->{'starting_num_sections'};
136 $self->{'num_bytes'} = $self->{'starting_num_bytes'};
[9919]137
138 $self->{'num_processed_bytes'} = 0;
139}
140
[10159]141sub zero_reset {
142 my $self = shift (@_);
143
144 $self->{'num_docs'} = 0;
145 $self->{'num_sections'} = 0;
[17564]146 # reconstructed docs have no text, just metadata, so we need to
147 # remember how many bytes we had initially
148 $self->{'num_bytes'} = $self->{'starting_num_bytes'};
[10159]149
150 $self->{'num_processed_bytes'} = 0;
151}
152
[10419]153sub is_incremental_capable
[10304]154{
155 # By default we return 'no' as the answer
156 # Safer to assume non-incremental to start with, and then override in
157 # inherited classes that are.
158
159 return 0;
160}
161
[9919]162sub get_num_docs {
163 my $self = shift (@_);
164
165 return $self->{'num_docs'};
166}
167
168sub get_num_sections {
169 my $self = shift (@_);
170
171 return $self->{'num_sections'};
172}
173
174# num_bytes is the actual number of bytes in the collection
175# this is normally the same as what's processed during text compression
176sub get_num_bytes {
177 my $self = shift (@_);
178
179 return $self->{'num_bytes'};
180}
181
182# num_processed_bytes is the number of bytes actually passed
183# to mg for the current index
184sub get_num_processed_bytes {
185 my $self = shift (@_);
186
187 return $self->{'num_processed_bytes'};
188}
189
190sub set_output_handle {
191 my $self = shift (@_);
192 my ($handle) = @_;
193
194 $self->{'output_handle'} = $handle;
195}
196
197
198sub set_mode {
199 my $self = shift (@_);
200 my ($mode) = @_;
201
202 $self->{'mode'} = $mode;
203}
204
[10159]205sub get_mode {
206 my $self = shift (@_);
207
208 return $self->{'mode'};
209}
210
[9919]211sub set_assocdir {
212 my $self = shift (@_);
213 my ($assocdir) = @_;
214
215 $self->{'assocdir'} = $assocdir;
216}
217
[15688]218sub set_dontdb {
[9919]219 my $self = shift (@_);
[15688]220 my ($dontdb) = @_;
[9919]221
[15688]222 $self->{'dontdb'} = $dontdb;
[9919]223}
224
[15725]225sub set_infodbtype
226{
227 my $self = shift(@_);
228 my $infodbtype = shift(@_);
229 $self->{'infodbtype'} = $infodbtype;
230}
231
[9919]232sub set_index {
233 my $self = shift (@_);
234 my ($index, $indexexparr) = @_;
235
236 $self->{'index'} = $index;
237 $self->{'indexexparr'} = $indexexparr if defined $indexexparr;
238}
239
240sub set_index_languages {
241 my $self = shift (@_);
242 my ($lang_meta, $langarr) = @_;
243 $self->{'lang_meta'} = $lang_meta;
244 $self->{'langarr'} = $langarr;
245}
246
247sub get_index {
248 my $self = shift (@_);
249
250 return $self->{'index'};
251}
252
253sub set_classifiers {
254 my $self = shift (@_);
255 my ($classifiers) = @_;
256
257 $self->{'classifiers'} = $classifiers;
258}
259
260sub set_indexing_text {
261 my $self = shift (@_);
262 my ($indexing_text) = @_;
263
264 $self->{'indexing_text'} = $indexing_text;
265}
266
267sub get_indexing_text {
268 my $self = shift (@_);
269
270 return $self->{'indexing_text'};
271}
272
273sub set_store_text {
274 my $self = shift (@_);
275 my ($store_text) = @_;
276
277 $self->{'store_text'} = $store_text;
278}
[16222]279
280sub set_store_metadata_coverage {
281 my $self = shift (@_);
282 my ($store_metadata_coverage) = @_;
283
284 $self->{'store_metadata_coverage'} = $store_metadata_coverage || "";
285}
286
[9919]287sub get_doc_list {
288 my $self = shift(@_);
289
290 return @{$self->{'doclist'}};
291}
292
[15685]293# the standard database level is section, but you may want to change it to document
294sub set_db_level {
[9919]295 my $self= shift (@_);
[15685]296 my ($db_level) = @_;
[9919]297
[15685]298 $self->{'db_level'} = $db_level;
[9919]299}
300
[10469]301sub set_sections_index_document_metadata {
302 my $self= shift (@_);
303 my ($index_type) = @_;
304
305 $self->{'sections_index_document_metadata'} = $index_type;
306}
[17110]307
308sub set_separate_cjk {
309 my $self = shift (@_);
310 my ($sep_cjk) = @_;
311
312 $self->{'separate_cjk'} = $sep_cjk;
313}
314
[9919]315sub process {
316 my $self = shift (@_);
317 my $method = $self->{'mode'};
318
319 $self->$method(@_);
320}
321
[17110]322# post process text depending on field. Currently don't do anything here
[17111]323# except cjk separation, and only for indexing
324# should only do this for indexed text (if $self->{'indexing_text'}),
325# but currently search term highlighting doesn't work if you do that.
326# once thats fixed up, then fix this.
[17110]327sub filter_text {
328 my $self = shift (@_);
329 my ($field, $text) = @_;
[14934]330
[17110]331 # lets do cjk seg here
332 my $new_text =$text;
333 if ($self->{'separate_cjk'}) {
334 $new_text = &cnseg::segment($text);
335 }
336 return $new_text;
337}
[14934]338
[17110]339
[14934]340sub infodb_metadata_stats
341{
342 my $self = shift (@_);
343 my ($field) = @_;
344
345 # Keep some statistics relating to metadata sets used and
346 # frequency of particular metadata fields within each set
347
348 # Union of metadata prefixes and frequency of fields
349 # (both scoped for this document alone, and across whole collection)
350
351 if ($field =~ m/^(.+)\.(.*)$/) {
352 my $prefix = $1;
353 my $core_field = $2;
354
355 $self->{'doc_mdprefix_fields'}->{$prefix}->{$core_field}++;
356 $self->{'mdprefix_fields'}->{$prefix}->{$core_field}++;
357 }
358 elsif ($field =~ m/^[[:upper:]]/) {
359 # implicit 'ex' metadata set
360
361 $self->{'doc_mdprefix_fields'}->{'ex'}->{$field}++;
362 $self->{'mdprefix_fields'}->{'ex'}->{$field}++;
363 }
364
365}
366
367
[9919]368sub infodb {
369 my $self = shift (@_);
370 my ($doc_obj, $filename) = @_;
371
[15696]372 # only output this document if it is a "indexed_doc" or "info_doc" (database only) document
[9919]373 my $doctype = $doc_obj->get_doc_type();
[11793]374 return if ($doctype ne "indexed_doc" && $doctype ne "info_doc");
[9919]375
[11994]376 my $archivedir = "";
377 if (defined $filename)
378 {
379 # doc_obj derived directly from file
380 my ($dir) = $filename =~ /^(.*?)(?:\/|\\)[^\/\\]*$/;
381 $dir = "" unless defined $dir;
382 $dir =~ s/\\/\//g;
383 $dir =~ s/^\/+//;
384 $dir =~ s/\/+$//;
385
386 $archivedir = $dir;
387
388 # resolve the final filenames of the files associated with this document
389 $self->assoc_files ($doc_obj, $archivedir);
390 }
391 else
392 {
[15688]393 # doc_obj reconstructed from database (has metadata, doc structure but no text)
[11994]394 my $top_section = $doc_obj->get_top_section();
395 $archivedir = $doc_obj->get_metadata_element($top_section,"archivedir");
396 }
397
[9919]398 #add this document to the browse structure
399 push(@{$self->{'doclist'}},$doc_obj->get_OID())
400 unless ($doctype eq "classification");
401
402 # classify this document
403 &classify::classify_doc ($self->{'classifiers'}, $doc_obj);
404
405 # this is another document
406 $self->{'num_docs'} += 1 unless ($doctype eq "classification");
407
408 # is this a paged or a hierarchical document
409 my ($thistype, $childtype) = $self->get_document_type ($doc_obj);
410
411 my $section = $doc_obj->get_top_section ();
412 my $doc_OID = $doc_obj->get_OID();
413 my $first = 1;
[15699]414 my $infodb_handle = $self->{'output_handle'};
[14934]415
416 $self->{'doc_mdprefix_fields'} = {};
417
[15695]418 while (defined $section)
419 {
420 my $section_OID = $doc_OID;
421 if ($section ne "")
422 {
423 $section_OID = $doc_OID . "." . $section;
424 }
[15696]425 my %section_infodb = ();
[15695]426
[9919]427 # update a few statistics
428 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
429 $self->{'num_sections'} += 1 unless ($doctype eq "classification");
430
431 # output the fact that this document is a document (unless doctype
432 # has been set to something else from within a plugin
433 my $dtype = $doc_obj->get_metadata_element ($section, "doctype");
434 if (!defined $dtype || $dtype !~ /\w/) {
[15697]435 $section_infodb{"doctype"} = [ "doc" ];
[9919]436 }
437
[11994]438 # Output whether this node contains text
439 #
[15688]440 # If doc_obj reconstructed from database file then no need to
[11994]441 # explicitly add <hastxt> as this is preserved as metadata when
[15688]442 # the database file is loaded in
[11994]443 if (defined $filename)
444 {
445 # doc_obj derived directly from file
446 if ($doc_obj->get_text_length($section) > 0) {
[15697]447 $section_infodb{"hastxt"} = [ "1" ];
[11994]448 } else {
[15697]449 $section_infodb{"hastxt"} = [ "0" ];
[11994]450 }
[9919]451 }
452
453 # output all the section metadata
454 my $metadata = $doc_obj->get_all_metadata ($section);
455 foreach my $pair (@$metadata) {
456 my ($field, $value) = (@$pair);
457
458 if ($field ne "Identifier" && $field !~ /^gsdl/ &&
459 defined $value && $value ne "") {
460
461 # escape problematic stuff
462 $value =~ s/\\/\\\\/g;
463 $value =~ s/\n/\\n/g;
464 $value =~ s/\r/\\r/g;
465
466 # special case for URL metadata
467 if ($field =~ /^URL$/i) {
[15725]468 &dbutil::write_infodb_entry($self->{'infodbtype'}, $infodb_handle, $value, { 'section' => [ $section_OID ] });
[9919]469 }
470
[15688]471 if (!defined $self->{'dontdb'}->{$field}) {
[15697]472 push(@{$section_infodb{$field}}, $value);
[14934]473
[16222]474 if ($section eq "" && $self->{'store_metadata_coverage'} =~ /^true$/i)
[14934]475 {
476 $self->infodb_metadata_stats($field);
477 }
[9919]478 }
479 }
480 }
481
[14934]482 if ($section eq "")
483 {
484 my $doc_mdprefix_fields = $self->{'doc_mdprefix_fields'};
[11994]485
[14934]486 foreach my $prefix (keys %$doc_mdprefix_fields)
487 {
[15697]488 push(@{$section_infodb{"metadataset"}}, $prefix);
[14934]489
490 foreach my $field (keys %{$doc_mdprefix_fields->{$prefix}})
491 {
[15708]492 push(@{$section_infodb{"metadatalist-$prefix"}}, $field);
493
[14934]494 my $val = $doc_mdprefix_fields->{$prefix}->{$field};
[15697]495 push(@{$section_infodb{"metadatafreq-$prefix-$field"}}, $val);
[14934]496 }
497 }
498 }
499
[15688]500 # If doc_obj reconstructed from database file then no need to
[11994]501 # explicitly add <archivedir> as this is preserved as metadata when
[15688]502 # the database file is loaded in
[11994]503 if (defined $filename)
504 {
505 # output archivedir if at top level
506 if ($section eq $doc_obj->get_top_section()) {
[15697]507 $section_infodb{"archivedir"} = [ $archivedir ];
[11994]508 }
[9919]509 }
510
511 # output document display type
512 if ($first) {
[15697]513 $section_infodb{"thistype"} = [ $thistype ];
[9919]514 }
515
[15685]516 if ($self->{'db_level'} eq "document") {
[9919]517 # doc num is num_docs not num_sections
518 # output the matching document number
[15697]519 $section_infodb{"docnum"} = [ $self->{'num_docs'} ];
[15696]520 }
521 else {
[9919]522 # output a list of children
523 my $children = $doc_obj->get_children ($section);
524 if (scalar(@$children) > 0) {
[15697]525 $section_infodb{"childtype"} = [ $childtype ];
[15696]526 my $contains = "";
527 foreach my $child (@$children)
528 {
529 $contains .= ";" unless ($contains eq "");
530 if ($child =~ /^.*?\.(\d+)$/)
531 {
532 $contains .= "\".$1";
[9919]533 }
[15698]534 else
535 {
[15696]536 $contains .= "\".$child";
537 }
[9919]538 }
[15697]539 $section_infodb{"contains"} = [ $contains ];
[9919]540 }
[15696]541 # output the matching doc number
[15697]542 $section_infodb{"docnum"} = [ $self->{'num_sections'} ];
[9919]543 }
544
[15725]545 &dbutil::write_infodb_entry($self->{'infodbtype'}, $infodb_handle, $section_OID, \%section_infodb);
[9919]546
[17106]547 # output a database entry for the document number, except for Lucene (which no longer needs this information)
548 unless (ref($self) eq "lucenebuildproc")
549 {
550 if ($self->{'db_level'} eq "document") {
551 &dbutil::write_infodb_entry($self->{'infodbtype'}, $infodb_handle, $self->{'num_docs'}, { 'section' => [ $doc_OID ] });
552 }
553 else {
554 &dbutil::write_infodb_entry($self->{'infodbtype'}, $infodb_handle, $self->{'num_sections'}, { 'section' => [ $section_OID ] });
555 }
[9919]556 }
557
558 $first = 0;
559 $section = $doc_obj->get_next_section($section);
[15685]560 last if ($self->{'db_level'} eq "document"); # if no sections wanted, only add the docs
[9919]561 }
[15696]562}
[9919]563
[15696]564
[9919]565sub text {
566 my $self = shift (@_);
567 my ($doc_obj) = @_;
568
569 my $handle = $self->{'outhandle'};
570 print $handle "basebuildproc::text function must be implemented in sub classes\n";
571 die "\n";
572}
573
574# should the document be indexed - according to the subcollection and language
575# specification.
576sub is_subcollection_doc {
577 my $self = shift (@_);
578 my ($doc_obj) = @_;
579
580 my $indexed_doc = 1;
581 foreach my $indexexp (@{$self->{'indexexparr'}}) {
582 $indexed_doc = 0;
583 my ($field, $exp, $options) = split /\//, $indexexp;
584 if (defined ($field) && defined ($exp)) {
585 my ($bool) = $field =~ /^(.)/;
586 $field =~ s/^.// if $bool eq '!';
[10028]587 my @metadata_values;
[9919]588 if ($field =~ /^filename$/i) {
[10028]589 push(@metadata_values, $doc_obj->get_source_filename());
[9919]590 }
[10028]591 else {
592 @metadata_values = @{$doc_obj->get_metadata($doc_obj->get_top_section(), $field)};
593 }
594 next unless @metadata_values;
595 foreach my $metadata_value (@metadata_values) {
596 if ($bool eq '!') {
597 if ($options =~ /^i$/i) {
598 if ($metadata_value !~ /$exp/i) {$indexed_doc = 1; last;}
599 } else {
600 if ($metadata_value !~ /$exp/) {$indexed_doc = 1; last;}
601 }
[9919]602 } else {
[10028]603 if ($options =~ /^i$/i) {
604 if ($metadata_value =~ /$exp/i) {$indexed_doc = 1; last;}
605 } else {
606 if ($metadata_value =~ /$exp/) {$indexed_doc = 1; last;}
607 }
[9919]608 }
609 }
[10028]610
611 last if ($indexed_doc == 1);
[9919]612 }
613 }
614
615 # if this doc is so far in the sub collection, and we have lang info,
616 # now we check the languages to see if it matches
617 if($indexed_doc && defined $self->{'lang_meta'}) {
618 $indexed_doc = 0;
619 my $field = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $self->{'lang_meta'});
620 if (defined $field) {
621 foreach my $lang (@{$self->{'langarr'}}) {
622 my ($bool) = $lang =~ /^(.)/;
623 if ($bool eq '!') {
624 $lang =~ s/^.//;
625 if ($field !~ /$lang/) {
626 $indexed_doc = 1; last;
627 }
628 } else {
629 if ($field =~ /$lang/) {
630 $indexed_doc = 1; last;
631 }
632 }
633 }
634 }
635 }
636 return $indexed_doc;
637
638}
639
640# use 'Paged' if document has no more than 2 levels
641# and each section at second level has a number for
642# Title metadata
643# also use Paged if gsdlthistype metadata is set to Paged
644sub get_document_type {
645 my $self = shift (@_);
646 my ($doc_obj) = @_;
647
648 my $thistype = "VList";
649 my $childtype = "VList";
650 my $title;
651 my @tmp = ();
652
653 my $section = $doc_obj->get_top_section ();
654
655 my $gsdlthistype = $doc_obj->get_metadata_element ($section, "gsdlthistype");
656 if (defined $gsdlthistype) {
657 if ($gsdlthistype eq "Paged") {
658 $childtype = "Paged";
659 if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
660 $thistype = "Paged";
661 } else {
662 $thistype = "Invisible";
663 }
664
665 return ($thistype, $childtype);
666 } elsif ($gsdlthistype eq "Hierarchy") {
667 return ($thistype, $childtype); # use VList, VList
668 }
669 }
670 my $first = 1;
671 while (defined $section) {
672 @tmp = split /\./, $section;
673 if (scalar(@tmp) > 1) {
674 return ($thistype, $childtype);
675 }
676 if (!$first) {
677 $title = $doc_obj->get_metadata_element ($section, "Title");
678 if (!defined $title || $title !~ /^\d+$/) {
679 return ($thistype, $childtype);
680 }
681 }
682 $first = 0;
683 $section = $doc_obj->get_next_section($section);
684 }
685 if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
686 $thistype = "Paged";
687 } else {
688 $thistype = "Invisible";
689 }
690 $childtype = "Paged";
691 return ($thistype, $childtype);
692}
693
[12844]694sub assoc_files() {
[9919]695 my $self = shift (@_);
696 my ($doc_obj, $archivedir) = @_;
697 my ($afile);
698
699 foreach my $assoc_file (@{$doc_obj->get_assoc_files()}) {
[12844]700 #rint STDERR "Processing associated file - copy " . $assoc_file->[0] . " to " . $assoc_file->[1] . "\n";
[9919]701 # if assoc file starts with a slash, we put it relative to the assoc
702 # dir, otherwise it is relative to the HASH... directory
703 if ($assoc_file->[1] =~ m@^[/\\]@) {
[12844]704 $afile = &util::filename_cat($self->{'assocdir'}, $assoc_file->[1]);
[9919]705 } else {
706 $afile = &util::filename_cat($self->{'assocdir'}, $archivedir, $assoc_file->[1]);
707 }
708 &util::hard_link ($assoc_file->[0], $afile);
709 }
710}
711
Note: See TracBrowser for help on using the repository browser.