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

Revision 17579, 18.7 KB (checked in by kjdon, 12 years ago)

removed a debug print statement

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