root/gsdl/trunk/perllib/basebuildproc.pm @ 15725

Revision 15725, 17.7 KB (checked in by mdewsnip, 12 years ago)

(Adding new DB support) Added $infodb_type as first argument to all the dbutil functions.

  • Property svn:keywords set to Author Date Id Revision
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_infodbtype
228{
229    my $self = shift(@_);
230    my $infodbtype = shift(@_);
231    $self->{'infodbtype'} = $infodbtype;
232}
233
234sub set_index {
235    my $self = shift (@_);
236    my ($index, $indexexparr) = @_;
237
238    $self->{'index'} = $index;
239    $self->{'indexexparr'} = $indexexparr if defined $indexexparr;
240}
241
242sub set_index_languages {
243    my $self = shift (@_);
244    my ($lang_meta, $langarr) = @_;
245    $self->{'lang_meta'} = $lang_meta;
246    $self->{'langarr'} = $langarr;
247}
248
249sub get_index {
250    my $self = shift (@_);
251
252    return $self->{'index'};
253}
254
255sub set_classifiers {
256    my $self = shift (@_);
257    my ($classifiers) = @_;
258
259    $self->{'classifiers'} = $classifiers;
260}
261
262sub set_indexing_text {
263    my $self = shift (@_);
264    my ($indexing_text) = @_;
265
266    $self->{'indexing_text'} = $indexing_text;
267}
268
269sub get_indexing_text {
270    my $self = shift (@_);
271
272    return $self->{'indexing_text'};
273}
274
275sub set_store_text {
276    my $self = shift (@_);
277    my ($store_text) = @_;
278
279    $self->{'store_text'} = $store_text;
280}
281sub get_doc_list {
282    my $self = shift(@_);
283   
284    return @{$self->{'doclist'}};
285}
286
287# the standard database level is section, but you may want to change it to document
288sub set_db_level {
289    my $self= shift (@_);
290    my ($db_level) = @_;
291
292    $self->{'db_level'} = $db_level;
293}
294
295sub set_sections_index_document_metadata {
296    my $self= shift (@_);
297    my ($index_type) = @_;
298   
299    $self->{'sections_index_document_metadata'} = $index_type;
300}
301sub process {
302    my $self = shift (@_);
303    my $method = $self->{'mode'};
304
305    $self->$method(@_);
306}
307
308
309
310sub infodb_metadata_stats
311{
312    my $self = shift (@_);
313    my ($field) = @_;
314
315    # Keep some statistics relating to metadata sets used and
316    # frequency of particular metadata fields within each set
317
318    # Union of metadata prefixes and frequency of fields
319    # (both scoped for this document alone, and across whole collection)
320   
321    if ($field =~ m/^(.+)\.(.*)$/) {
322    my $prefix = $1;
323    my $core_field = $2;
324
325    $self->{'doc_mdprefix_fields'}->{$prefix}->{$core_field}++;
326    $self->{'mdprefix_fields'}->{$prefix}->{$core_field}++;
327    }
328    elsif ($field =~ m/^[[:upper:]]/) {
329    # implicit 'ex' metadata set
330
331    $self->{'doc_mdprefix_fields'}->{'ex'}->{$field}++;
332    $self->{'mdprefix_fields'}->{'ex'}->{$field}++;
333    }
334
335}
336
337
338sub infodb {
339    my $self = shift (@_);
340    my ($doc_obj, $filename) = @_;
341
342    # only output this document if it is a "indexed_doc" or "info_doc" (database only) document
343    my $doctype = $doc_obj->get_doc_type();
344    return if ($doctype ne "indexed_doc" && $doctype ne "info_doc");
345
346    my $archivedir = "";
347    if (defined $filename)
348    {
349    # doc_obj derived directly from file
350    my ($dir) = $filename =~ /^(.*?)(?:\/|\\)[^\/\\]*$/;
351    $dir = "" unless defined $dir;
352    $dir =~ s/\\/\//g;
353    $dir =~ s/^\/+//;
354    $dir =~ s/\/+$//;
355
356    $archivedir = $dir;
357
358    # resolve the final filenames of the files associated with this document
359    $self->assoc_files ($doc_obj, $archivedir);
360    }
361    else
362    {
363    # doc_obj reconstructed from database (has metadata, doc structure but no text)
364    my $top_section = $doc_obj->get_top_section();
365    $archivedir = $doc_obj->get_metadata_element($top_section,"archivedir");
366    }
367
368    #add this document to the browse structure
369    push(@{$self->{'doclist'}},$doc_obj->get_OID())
370    unless ($doctype eq "classification");
371
372    # classify this document
373    &classify::classify_doc ($self->{'classifiers'}, $doc_obj);
374
375    # this is another document
376    $self->{'num_docs'} += 1 unless ($doctype eq "classification");
377
378    # is this a paged or a hierarchical document
379    my ($thistype, $childtype) = $self->get_document_type ($doc_obj);
380
381    my $section = $doc_obj->get_top_section ();
382    my $doc_OID = $doc_obj->get_OID();
383    my $first = 1;
384    my $infodb_handle = $self->{'output_handle'};
385
386    $self->{'doc_mdprefix_fields'} = {};
387
388    while (defined $section)
389    {
390    my $section_OID = $doc_OID;
391    if ($section ne "")
392    {
393        $section_OID = $doc_OID . "." . $section;
394    }
395    my %section_infodb = ();
396
397    # update a few statistics
398    $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
399    $self->{'num_sections'} += 1 unless ($doctype eq "classification");
400
401    # output the fact that this document is a document (unless doctype
402    # has been set to something else from within a plugin
403    my $dtype = $doc_obj->get_metadata_element ($section, "doctype");
404    if (!defined $dtype || $dtype !~ /\w/) {
405        $section_infodb{"doctype"} = [ "doc" ];
406    }
407
408    # Output whether this node contains text
409    #
410    # If doc_obj reconstructed from database file then no need to
411    # explicitly add <hastxt> as this is preserved as metadata when
412    # the database file is loaded in
413    if (defined $filename)
414    {
415        # doc_obj derived directly from file
416        if ($doc_obj->get_text_length($section) > 0) {
417        $section_infodb{"hastxt"} = [ "1" ];
418        } else {
419        $section_infodb{"hastxt"} = [ "0" ];
420        }
421    }
422
423    # output all the section metadata
424    my $metadata = $doc_obj->get_all_metadata ($section);
425    foreach my $pair (@$metadata) {
426        my ($field, $value) = (@$pair);
427
428        if ($field ne "Identifier" && $field !~ /^gsdl/ &&
429        defined $value && $value ne "") {       
430
431        # escape problematic stuff
432        $value =~ s/\\/\\\\/g;
433        $value =~ s/\n/\\n/g;
434        $value =~ s/\r/\\r/g;
435
436        # special case for URL metadata
437        if ($field =~ /^URL$/i) {
438            &dbutil::write_infodb_entry($self->{'infodbtype'}, $infodb_handle, $value, { 'section' => [ $section_OID ] });
439        }
440
441        if (!defined $self->{'dontdb'}->{$field}) {
442            push(@{$section_infodb{$field}}, $value);
443
444            if ($section eq "")
445            {
446            $self->infodb_metadata_stats($field);
447            }
448        }
449        }
450    }
451
452    if ($section eq "")
453    {
454        my $doc_mdprefix_fields = $self->{'doc_mdprefix_fields'};
455
456        foreach my $prefix (keys %$doc_mdprefix_fields)
457        {
458        push(@{$section_infodb{"metadataset"}}, $prefix);
459
460        foreach my $field (keys %{$doc_mdprefix_fields->{$prefix}})
461        {
462            push(@{$section_infodb{"metadatalist-$prefix"}}, $field);
463
464            my $val = $doc_mdprefix_fields->{$prefix}->{$field};
465            push(@{$section_infodb{"metadatafreq-$prefix-$field"}}, $val);
466        }
467        }
468    }
469
470    # If doc_obj reconstructed from database file then no need to
471    # explicitly add <archivedir> as this is preserved as metadata when
472    # the database file is loaded in
473    if (defined $filename)
474    {
475        # output archivedir if at top level
476        if ($section eq $doc_obj->get_top_section()) {
477        $section_infodb{"archivedir"} = [ $archivedir ];
478        }
479    }
480
481    # output document display type
482    if ($first) {
483        $section_infodb{"thistype"} = [ $thistype ];
484    }
485
486    if ($self->{'db_level'} eq "document") {
487        # doc num is num_docs not num_sections
488        # output the matching document number
489        $section_infodb{"docnum"} = [ $self->{'num_docs'} ];
490    }
491    else {
492        # output a list of children
493        my $children = $doc_obj->get_children ($section);
494        if (scalar(@$children) > 0) {
495        $section_infodb{"childtype"} = [ $childtype ];
496        my $contains = "";
497        foreach my $child (@$children)
498        {
499            $contains .= ";" unless ($contains eq "");
500            if ($child =~ /^.*?\.(\d+)$/)
501            {
502            $contains .= "\".$1";
503            }
504            else
505            {
506            $contains .= "\".$child";
507            }
508        }
509        $section_infodb{"contains"} = [ $contains ];
510        }
511        # output the matching doc number
512        $section_infodb{"docnum"} = [ $self->{'num_sections'} ];
513    }
514   
515    &dbutil::write_infodb_entry($self->{'infodbtype'}, $infodb_handle, $section_OID, \%section_infodb);
516   
517    # output a database entry for the document number
518    if ($self->{'db_level'} eq "document") {
519        &dbutil::write_infodb_entry($self->{'infodbtype'}, $infodb_handle, $self->{'num_docs'}, { 'section' => [ $doc_OID ] });
520    }
521    else {
522        &dbutil::write_infodb_entry($self->{'infodbtype'}, $infodb_handle, $self->{'num_sections'}, { 'section' => [ $section_OID ] });
523    }
524
525    $first = 0;
526    $section = $doc_obj->get_next_section($section);
527    last if ($self->{'db_level'} eq "document"); # if no sections wanted, only add the docs
528    }
529}
530
531
532sub text {
533    my $self = shift (@_);
534    my ($doc_obj) = @_;
535   
536    my $handle = $self->{'outhandle'};
537    print $handle "basebuildproc::text function must be implemented in sub classes\n";
538    die "\n";
539}
540
541# should the document be indexed - according to the subcollection and language
542# specification.
543sub is_subcollection_doc {
544    my $self = shift (@_);
545    my ($doc_obj) = @_;
546   
547    my $indexed_doc = 1;
548    foreach my $indexexp (@{$self->{'indexexparr'}}) {
549    $indexed_doc = 0;
550    my ($field, $exp, $options) = split /\//, $indexexp;
551    if (defined ($field) && defined ($exp)) {
552        my ($bool) = $field =~ /^(.)/;
553        $field =~ s/^.// if $bool eq '!';
554        my @metadata_values;
555        if ($field =~ /^filename$/i) {
556        push(@metadata_values, $doc_obj->get_source_filename());
557        }
558        else {
559        @metadata_values = @{$doc_obj->get_metadata($doc_obj->get_top_section(), $field)};
560        }
561        next unless @metadata_values;
562        foreach my $metadata_value (@metadata_values) {
563        if ($bool eq '!') {
564            if ($options =~ /^i$/i) {
565            if ($metadata_value !~ /$exp/i) {$indexed_doc = 1; last;}
566            } else {
567            if ($metadata_value !~ /$exp/) {$indexed_doc = 1; last;}
568            }
569        } else {
570            if ($options =~ /^i$/i) {
571            if ($metadata_value =~ /$exp/i) {$indexed_doc = 1; last;}
572            } else {
573            if ($metadata_value =~ /$exp/) {$indexed_doc = 1; last;}
574            }
575        }
576        }
577
578        last if ($indexed_doc == 1);
579    }
580    }
581   
582    # if this doc is so far in the sub collection, and we have lang info,
583    # now we check the languages to see if it matches
584    if($indexed_doc && defined $self->{'lang_meta'}) {
585    $indexed_doc = 0;
586    my $field = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $self->{'lang_meta'});
587    if (defined $field) {
588        foreach my $lang (@{$self->{'langarr'}}) {
589        my ($bool) = $lang =~ /^(.)/;
590        if ($bool eq '!') {
591            $lang =~ s/^.//;
592            if ($field !~ /$lang/) {
593            $indexed_doc = 1; last;
594            }
595        } else {
596            if ($field =~ /$lang/) {
597            $indexed_doc = 1; last;
598            }
599        }
600        }
601    }
602    }
603    return $indexed_doc;
604   
605}
606
607# use 'Paged' if document has no more than 2 levels
608# and each section at second level has a number for
609# Title metadata
610# also use Paged if gsdlthistype metadata is set to Paged
611sub get_document_type {
612    my $self = shift (@_);
613    my ($doc_obj) = @_;
614
615    my $thistype = "VList";
616    my $childtype = "VList";
617    my $title;
618    my @tmp = ();
619   
620    my $section = $doc_obj->get_top_section ();
621   
622    my $gsdlthistype = $doc_obj->get_metadata_element ($section, "gsdlthistype");
623    if (defined $gsdlthistype) {
624    if ($gsdlthistype eq "Paged") {
625        $childtype = "Paged";
626        if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
627        $thistype = "Paged";
628        } else {
629        $thistype = "Invisible";
630        }
631       
632        return ($thistype, $childtype);
633    } elsif ($gsdlthistype eq "Hierarchy") {
634        return ($thistype, $childtype); # use VList, VList
635    }
636    }
637    my $first = 1;
638    while (defined $section) {
639    @tmp = split /\./, $section;
640    if (scalar(@tmp) > 1) {
641        return ($thistype, $childtype);
642    }
643    if (!$first) {
644        $title = $doc_obj->get_metadata_element ($section, "Title");
645        if (!defined $title || $title !~ /^\d+$/) {
646        return ($thistype, $childtype);
647        }
648    }
649    $first = 0;
650    $section = $doc_obj->get_next_section($section);
651    }
652    if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
653    $thistype = "Paged";
654    } else {
655    $thistype = "Invisible";
656    }
657    $childtype = "Paged";
658    return ($thistype, $childtype);
659}
660
661sub assoc_files() {
662    my $self = shift (@_);
663    my ($doc_obj, $archivedir) = @_;
664    my ($afile);
665   
666    foreach my $assoc_file (@{$doc_obj->get_assoc_files()}) {
667      #rint STDERR "Processing associated file - copy " . $assoc_file->[0] . " to " . $assoc_file->[1] . "\n";
668    # if assoc file starts with a slash, we put it relative to the assoc
669    # dir, otherwise it is relative to the HASH... directory
670    if ($assoc_file->[1] =~ m@^[/\\]@) {
671        $afile = &util::filename_cat($self->{'assocdir'}, $assoc_file->[1]);
672    } else {
673        $afile = &util::filename_cat($self->{'assocdir'}, $archivedir, $assoc_file->[1]);
674    }
675    &util::hard_link ($assoc_file->[0], $afile);
676    }
677}
678
Note: See TracBrowser for help on using the browser.