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

Last change on this file since 17111 was 17111, checked in by kjdon, 16 years ago

added a comment

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