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

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

Minor code rearrange.

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