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

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

Added a "store_metadata_coverage" option to the collect.cfg file to specify that the "metadatalist-*" and "metadatafreq-*" values should be added to the infodb file. This is disabled by default, as I can't find where this metadata is used and it really bloats the database for collections with a lot of metadata (e.g. bibliographic collections).

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