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

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

(Adding new DB support) Moved a bit more GDBM-specific code into write_infodb_entry_gdbm().

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