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

Last change on this file since 16841 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
Line 
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
27# implemented by subclass) and storing in the database
28
29package basebuildproc;
30
31eval {require bytes};
32
33use classify;
34use dbutil;
35use doc;
36use docproc;
37use strict; no strict 'subs';
38use util;
39
40BEGIN {
41 @basebuildproc::ISA = ('docproc');
42}
43
44sub new()
45 {
46 my ($class, $collection, $source_dir, $build_dir, $keepold, $verbosity, $outhandle) = @_;
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
51 # to (i.e. mg, database etc.)
52 $outhandle = STDERR unless defined $outhandle;
53
54 $self->{'collection'} = $collection;
55 $self->{'source_dir'} = $source_dir;
56 $self->{'build_dir'} = $build_dir;
57 $self->{'keepold'} = $keepold;
58 $self->{'verbosity'} = $verbosity;
59 $self->{'outhandle'} = $outhandle;
60
61 $self->{'classifiers'} = [];
62 $self->{'mode'} = "text";
63 $self->{'assocdir'} = $build_dir;
64 $self->{'dontdb'} = {};
65 $self->{'store_metadata_coverage'} = "false";
66
67 $self->{'index'} = "section:text";
68 $self->{'indexexparr'} = [];
69
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)
76 print STDERR "Keepold!\n";
77 $buildconfigfile = &util::filename_cat($build_dir, "build.cfg");
78 print STDERR "Build cfg: $buildconfigfile\n";
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 print STDERR "Index cfg: $buildconfigfile\n";
87 if (-e $buildconfigfile) {
88 $found_num_data = 1;
89 }
90 }
91
92 }
93 #else
94 # {
95 # print STDERR "Removeold!\n";
96 # }
97
98 if ($found_num_data)
99 {
100 #print STDERR "Found_Num_Data!\n";
101 my $buildcfg = &colcfg::read_build_cfg($buildconfigfile);
102 $self->{'starting_num_docs'} = $buildcfg->{'numdocs'};
103 #print STDERR "- num_docs: $self->{'starting_num_docs'}\n";
104 $self->{'starting_num_sections'} = $buildcfg->{'numsections'};
105 #print STDERR "- num_sections: $self->{'starting_num_sections'}\n";
106 $self->{'starting_num_bytes'} = $buildcfg->{'numbytes'};
107 #print STDERR "- num_bytes: $self->{'starting_num_bytes'}\n";
108 }
109 else
110 {
111 #print STDERR "NOT Found_Num_Data!\n";
112 $self->{'starting_num_docs'} = 0;
113 $self->{'starting_num_sections'} = 0;
114 $self->{'starting_num_bytes'} = 0;
115 }
116
117 $self->{'output_handle'} = "STDOUT";
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
122 $self->{'num_processed_bytes'} = 0;
123 $self->{'store_text'} = 1;
124
125 # what level (section/document) the database - indexer intersection is
126 $self->{'db_level'} = "section";
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 (@_);
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'};
142
143 $self->{'num_processed_bytes'} = 0;
144}
145
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
156sub is_incremental_capable
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
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
208sub get_mode {
209 my $self = shift (@_);
210
211 return $self->{'mode'};
212}
213
214sub set_assocdir {
215 my $self = shift (@_);
216 my ($assocdir) = @_;
217
218 $self->{'assocdir'} = $assocdir;
219}
220
221sub set_dontdb {
222 my $self = shift (@_);
223 my ($dontdb) = @_;
224
225 $self->{'dontdb'} = $dontdb;
226}
227
228sub set_infodbtype
229{
230 my $self = shift(@_);
231 my $infodbtype = shift(@_);
232 $self->{'infodbtype'} = $infodbtype;
233}
234
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}
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
290sub get_doc_list {
291 my $self = shift(@_);
292
293 return @{$self->{'doclist'}};
294}
295
296# the standard database level is section, but you may want to change it to document
297sub set_db_level {
298 my $self= shift (@_);
299 my ($db_level) = @_;
300
301 $self->{'db_level'} = $db_level;
302}
303
304sub set_sections_index_document_metadata {
305 my $self= shift (@_);
306 my ($index_type) = @_;
307
308 $self->{'sections_index_document_metadata'} = $index_type;
309}
310sub process {
311 my $self = shift (@_);
312 my $method = $self->{'mode'};
313
314 $self->$method(@_);
315}
316
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
347sub infodb {
348 my $self = shift (@_);
349 my ($doc_obj, $filename) = @_;
350
351 # only output this document if it is a "indexed_doc" or "info_doc" (database only) document
352 my $doctype = $doc_obj->get_doc_type();
353 return if ($doctype ne "indexed_doc" && $doctype ne "info_doc");
354
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 {
372 # doc_obj reconstructed from database (has metadata, doc structure but no text)
373 my $top_section = $doc_obj->get_top_section();
374 $archivedir = $doc_obj->get_metadata_element($top_section,"archivedir");
375 }
376
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;
393 my $infodb_handle = $self->{'output_handle'};
394
395 $self->{'doc_mdprefix_fields'} = {};
396
397 while (defined $section)
398 {
399 my $section_OID = $doc_OID;
400 if ($section ne "")
401 {
402 $section_OID = $doc_OID . "." . $section;
403 }
404 my %section_infodb = ();
405
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/) {
414 $section_infodb{"doctype"} = [ "doc" ];
415 }
416
417 # Output whether this node contains text
418 #
419 # If doc_obj reconstructed from database file then no need to
420 # explicitly add <hastxt> as this is preserved as metadata when
421 # the database file is loaded in
422 if (defined $filename)
423 {
424 # doc_obj derived directly from file
425 if ($doc_obj->get_text_length($section) > 0) {
426 $section_infodb{"hastxt"} = [ "1" ];
427 } else {
428 $section_infodb{"hastxt"} = [ "0" ];
429 }
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) {
447 &dbutil::write_infodb_entry($self->{'infodbtype'}, $infodb_handle, $value, { 'section' => [ $section_OID ] });
448 }
449
450 if (!defined $self->{'dontdb'}->{$field}) {
451 push(@{$section_infodb{$field}}, $value);
452
453 if ($section eq "" && $self->{'store_metadata_coverage'} =~ /^true$/i)
454 {
455 $self->infodb_metadata_stats($field);
456 }
457 }
458 }
459 }
460
461 if ($section eq "")
462 {
463 my $doc_mdprefix_fields = $self->{'doc_mdprefix_fields'};
464
465 foreach my $prefix (keys %$doc_mdprefix_fields)
466 {
467 push(@{$section_infodb{"metadataset"}}, $prefix);
468
469 foreach my $field (keys %{$doc_mdprefix_fields->{$prefix}})
470 {
471 push(@{$section_infodb{"metadatalist-$prefix"}}, $field);
472
473 my $val = $doc_mdprefix_fields->{$prefix}->{$field};
474 push(@{$section_infodb{"metadatafreq-$prefix-$field"}}, $val);
475 }
476 }
477 }
478
479 # If doc_obj reconstructed from database file then no need to
480 # explicitly add <archivedir> as this is preserved as metadata when
481 # the database file is loaded in
482 if (defined $filename)
483 {
484 # output archivedir if at top level
485 if ($section eq $doc_obj->get_top_section()) {
486 $section_infodb{"archivedir"} = [ $archivedir ];
487 }
488 }
489
490 # output document display type
491 if ($first) {
492 $section_infodb{"thistype"} = [ $thistype ];
493 }
494
495 if ($self->{'db_level'} eq "document") {
496 # doc num is num_docs not num_sections
497 # output the matching document number
498 $section_infodb{"docnum"} = [ $self->{'num_docs'} ];
499 }
500 else {
501 # output a list of children
502 my $children = $doc_obj->get_children ($section);
503 if (scalar(@$children) > 0) {
504 $section_infodb{"childtype"} = [ $childtype ];
505 my $contains = "";
506 foreach my $child (@$children)
507 {
508 $contains .= ";" unless ($contains eq "");
509 if ($child =~ /^.*?\.(\d+)$/)
510 {
511 $contains .= "\".$1";
512 }
513 else
514 {
515 $contains .= "\".$child";
516 }
517 }
518 $section_infodb{"contains"} = [ $contains ];
519 }
520 # output the matching doc number
521 $section_infodb{"docnum"} = [ $self->{'num_sections'} ];
522 }
523
524 &dbutil::write_infodb_entry($self->{'infodbtype'}, $infodb_handle, $section_OID, \%section_infodb);
525
526 # output a database entry for the document number
527 if ($self->{'db_level'} eq "document") {
528 &dbutil::write_infodb_entry($self->{'infodbtype'}, $infodb_handle, $self->{'num_docs'}, { 'section' => [ $doc_OID ] });
529 }
530 else {
531 &dbutil::write_infodb_entry($self->{'infodbtype'}, $infodb_handle, $self->{'num_sections'}, { 'section' => [ $section_OID ] });
532 }
533
534 $first = 0;
535 $section = $doc_obj->get_next_section($section);
536 last if ($self->{'db_level'} eq "document"); # if no sections wanted, only add the docs
537 }
538}
539
540
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 '!';
563 my @metadata_values;
564 if ($field =~ /^filename$/i) {
565 push(@metadata_values, $doc_obj->get_source_filename());
566 }
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 }
578 } else {
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 }
584 }
585 }
586
587 last if ($indexed_doc == 1);
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
670sub assoc_files() {
671 my $self = shift (@_);
672 my ($doc_obj, $archivedir) = @_;
673 my ($afile);
674
675 foreach my $assoc_file (@{$doc_obj->get_assoc_files()}) {
676 #rint STDERR "Processing associated file - copy " . $assoc_file->[0] . " to " . $assoc_file->[1] . "\n";
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@^[/\\]@) {
680 $afile = &util::filename_cat($self->{'assocdir'}, $assoc_file->[1]);
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.