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

Revision 17106, 18.1 KB (checked in by mdewsnip, 11 years ago)

No longer writes out the document/section number entries for Lucene, since it doesn't need this information any more (now that Lucene uses the Greenstone OIDs directly). This will only work if a custom lucenebuildproc isn't used, but the worst case scenario is that these entries will be unused in the infodb file.

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