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

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

(Adding new DB support) Moved the GDBM-specific stuff out of infodb() into a new write_infodb_entry_gdbm() function, in preparation for adding sqlite support.

  • 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 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 if ($value =~ /-{70,}/) {
427 # if value contains 70 or more hyphens in a row we need
428 # to escape them to prevent txt2db from treating them
429 # as a separator
430 $value =~ s/-/&\#045;/gi;
431 }
432
433 # special case for URL metadata
434 if ($field =~ /^URL$/i) {
435 $self->write_infodb_entry($value, { 'section' => $section_OID });
436 }
437
438 if (!defined $self->{'dontdb'}->{$field}) {
439 $section_infodb{$field} = $value;
440
441 if ($section eq "")
442 {
443 $self->infodb_metadata_stats($field);
444 }
445 }
446 }
447 }
448
449 if ($section eq "")
450 {
451 my $doc_mdprefix_fields = $self->{'doc_mdprefix_fields'};
452
453 foreach my $prefix (keys %$doc_mdprefix_fields)
454 {
455 $section_infodb{"metadataset"} = $prefix;
456
457 foreach my $field (keys %{$doc_mdprefix_fields->{$prefix}})
458 {
459 my $val = $doc_mdprefix_fields->{$prefix}->{$field};
460
461 $section_infodb{"metadatalist-$prefix"} = $field;
462 $section_infodb{"metadatafreq-$prefix-$field"} = $val;
463 }
464 }
465 }
466
467 # If doc_obj reconstructed from database file then no need to
468 # explicitly add <archivedir> as this is preserved as metadata when
469 # the database file is loaded in
470 if (defined $filename)
471 {
472 # output archivedir if at top level
473 if ($section eq $doc_obj->get_top_section()) {
474 $section_infodb{"archivedir"} = $archivedir;
475 }
476 }
477
478 # output document display type
479 if ($first) {
480 $section_infodb{"thistype"} = $thistype;
481 }
482
483 if ($self->{'db_level'} eq "document") {
484 # doc num is num_docs not num_sections
485 # output the matching document number
486 $section_infodb{"docnum"} = $self->{'num_docs'};
487 }
488 else {
489 # output a list of children
490 my $children = $doc_obj->get_children ($section);
491 if (scalar(@$children) > 0) {
492 $section_infodb{"childtype"} = $childtype;
493 my $contains = "";
494 foreach my $child (@$children)
495 {
496 $contains .= ";" unless ($contains eq "");
497 if ($child =~ /^.*?\.(\d+)$/)
498 {
499 $contains .= "\".$1";
500 }
501 else {
502 $contains .= "\".$child";
503 }
504 }
505 $section_infodb{"contains"} = $contains;
506 }
507 # output the matching doc number
508 $section_infodb{"docnum"} = $self->{'num_sections'};
509 }
510
511 $self->write_infodb_entry($section_OID, \%section_infodb);
512
513 # output a database entry for the document number
514 if ($self->{'db_level'} eq "document") {
515 $self->write_infodb_entry($self->{'num_docs'}, { 'section' => $doc_OID });
516 }
517 else {
518 $self->write_infodb_entry($self->{'num_sections'}, { 'section' => $section_OID });
519 }
520
521 $first = 0;
522 $section = $doc_obj->get_next_section($section);
523 last if ($self->{'db_level'} eq "document"); # if no sections wanted, only add the docs
524 }
525}
526
527
528sub write_infodb_entry
529{
530 my $self = shift(@_);
531
532 $self->write_infodb_entry_gdbm(@_);
533}
534
535
536sub write_infodb_entry_gdbm
537{
538 my $self = shift(@_);
539 my $infodb_key = shift(@_);
540 my $infodb_map = shift(@_);
541
542 my $handle = $self->{'output_handle'};
543
544 print $handle "[$infodb_key]\n";
545 foreach my $infodb_value_key (keys(%$infodb_map))
546 {
547 print $handle "<$infodb_value_key>" . $infodb_map->{$infodb_value_key} . "\n";
548 }
549 print $handle '-' x 70, "\n";
550}
551
552
553sub text {
554 my $self = shift (@_);
555 my ($doc_obj) = @_;
556
557 my $handle = $self->{'outhandle'};
558 print $handle "basebuildproc::text function must be implemented in sub classes\n";
559 die "\n";
560}
561
562# should the document be indexed - according to the subcollection and language
563# specification.
564sub is_subcollection_doc {
565 my $self = shift (@_);
566 my ($doc_obj) = @_;
567
568 my $indexed_doc = 1;
569 foreach my $indexexp (@{$self->{'indexexparr'}}) {
570 $indexed_doc = 0;
571 my ($field, $exp, $options) = split /\//, $indexexp;
572 if (defined ($field) && defined ($exp)) {
573 my ($bool) = $field =~ /^(.)/;
574 $field =~ s/^.// if $bool eq '!';
575 my @metadata_values;
576 if ($field =~ /^filename$/i) {
577 push(@metadata_values, $doc_obj->get_source_filename());
578 }
579 else {
580 @metadata_values = @{$doc_obj->get_metadata($doc_obj->get_top_section(), $field)};
581 }
582 next unless @metadata_values;
583 foreach my $metadata_value (@metadata_values) {
584 if ($bool eq '!') {
585 if ($options =~ /^i$/i) {
586 if ($metadata_value !~ /$exp/i) {$indexed_doc = 1; last;}
587 } else {
588 if ($metadata_value !~ /$exp/) {$indexed_doc = 1; last;}
589 }
590 } else {
591 if ($options =~ /^i$/i) {
592 if ($metadata_value =~ /$exp/i) {$indexed_doc = 1; last;}
593 } else {
594 if ($metadata_value =~ /$exp/) {$indexed_doc = 1; last;}
595 }
596 }
597 }
598
599 last if ($indexed_doc == 1);
600 }
601 }
602
603 # if this doc is so far in the sub collection, and we have lang info,
604 # now we check the languages to see if it matches
605 if($indexed_doc && defined $self->{'lang_meta'}) {
606 $indexed_doc = 0;
607 my $field = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $self->{'lang_meta'});
608 if (defined $field) {
609 foreach my $lang (@{$self->{'langarr'}}) {
610 my ($bool) = $lang =~ /^(.)/;
611 if ($bool eq '!') {
612 $lang =~ s/^.//;
613 if ($field !~ /$lang/) {
614 $indexed_doc = 1; last;
615 }
616 } else {
617 if ($field =~ /$lang/) {
618 $indexed_doc = 1; last;
619 }
620 }
621 }
622 }
623 }
624 return $indexed_doc;
625
626}
627
628# use 'Paged' if document has no more than 2 levels
629# and each section at second level has a number for
630# Title metadata
631# also use Paged if gsdlthistype metadata is set to Paged
632sub get_document_type {
633 my $self = shift (@_);
634 my ($doc_obj) = @_;
635
636 my $thistype = "VList";
637 my $childtype = "VList";
638 my $title;
639 my @tmp = ();
640
641 my $section = $doc_obj->get_top_section ();
642
643 my $gsdlthistype = $doc_obj->get_metadata_element ($section, "gsdlthistype");
644 if (defined $gsdlthistype) {
645 if ($gsdlthistype eq "Paged") {
646 $childtype = "Paged";
647 if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
648 $thistype = "Paged";
649 } else {
650 $thistype = "Invisible";
651 }
652
653 return ($thistype, $childtype);
654 } elsif ($gsdlthistype eq "Hierarchy") {
655 return ($thistype, $childtype); # use VList, VList
656 }
657 }
658 my $first = 1;
659 while (defined $section) {
660 @tmp = split /\./, $section;
661 if (scalar(@tmp) > 1) {
662 return ($thistype, $childtype);
663 }
664 if (!$first) {
665 $title = $doc_obj->get_metadata_element ($section, "Title");
666 if (!defined $title || $title !~ /^\d+$/) {
667 return ($thistype, $childtype);
668 }
669 }
670 $first = 0;
671 $section = $doc_obj->get_next_section($section);
672 }
673 if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
674 $thistype = "Paged";
675 } else {
676 $thistype = "Invisible";
677 }
678 $childtype = "Paged";
679 return ($thistype, $childtype);
680}
681
682sub assoc_files() {
683 my $self = shift (@_);
684 my ($doc_obj, $archivedir) = @_;
685 my ($afile);
686
687 foreach my $assoc_file (@{$doc_obj->get_assoc_files()}) {
688 #rint STDERR "Processing associated file - copy " . $assoc_file->[0] . " to " . $assoc_file->[1] . "\n";
689 # if assoc file starts with a slash, we put it relative to the assoc
690 # dir, otherwise it is relative to the HASH... directory
691 if ($assoc_file->[1] =~ m@^[/\\]@) {
692 $afile = &util::filename_cat($self->{'assocdir'}, $assoc_file->[1]);
693 } else {
694 $afile = &util::filename_cat($self->{'assocdir'}, $archivedir, $assoc_file->[1]);
695 }
696 &util::hard_link ($assoc_file->[0], $afile);
697 }
698}
699
Note: See TracBrowser for help on using the repository browser.