source: main/tags/2.52/gsdl/perllib/mgppbuildproc.pm@ 25422

Last change on this file since 25422 was 8220, checked in by cs025, 20 years ago

Extensions to underpin OAI - e.g. creation of the OAI classifier, adding
modified file dates and ensuring that documents know the parent classifiers
to which they belong.

  • Property svn:keywords set to Author Date Id Revision
File size: 19.6 KB
Line 
1###########################################################################
2#
3# mgppbuildproc.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
27# for mgpp to process
28
29
30package mgppbuildproc;
31
32eval {require bytes};
33
34use classify;
35use doc;
36use docproc;
37use util;
38
39
40BEGIN {
41 @ISA = ('docproc');
42}
43
44#this must be the same as in mgppbuilder
45%level_map = ('document'=>'Doc',
46 'section'=>'Sec',
47 'paragraph'=>'Para');
48
49sub new {
50 my ($class, $collection, $source_dir, $build_dir,
51 $verbosity, $outhandle) = @_;
52 my $self = new docproc ();
53
54 # outhandle is where all the debugging info goes
55 # output_handle is where the output of the plugins is piped
56 # to (i.e. mg, gdbm etc.)
57 $outhandle = STDERR unless defined $outhandle;
58
59 $self->{'collection'} = $collection;
60 $self->{'source_dir'} = $source_dir;
61 $self->{'build_dir'} = $build_dir;
62 $self->{'verbosity'} = $verbosity;
63 $self->{'classifiers'} = [];
64 $self->{'mode'} = "text";
65 $self->{'assocdir'} = $build_dir;
66 $self->{'dontgdbm'} = {};
67 $self->{'index'} = "text";
68 $self->{'indexexparr'} = [];
69 $self->{'output_handle'} = "STDOUT";
70 $self->{'num_docs'} = 0;
71 $self->{'num_sections'} = 0;
72 $self->{'num_bytes'} = 0;
73 $self->{'num_processed_bytes'} = 0;
74 $self->{'store_text'} = 1;
75 $self->{'outhandle'} = $outhandle;
76
77 #used by browse interface
78 $self->{'doclist'} = [];
79
80 $self->{'indexing_text'} = 0;
81
82 #new ones for mgpp
83 $self->{'dontindex'} = {};
84 $self->{'indexfieldmap'} = {};
85 $self->{'indexfields'} = {}; # only put in the ones that are not specified directly in the index
86 $self->{'strip_html'}=1;
87
88
89 return bless $self, $class;
90}
91
92sub reset {
93 my $self = shift (@_);
94
95 $self->{'num_docs'} = 0;
96 $self->{'num_sections'} = 0;
97 $self->{'num_processed_bytes'} = 0;
98 $self->{'num_bytes'} = 0;
99}
100
101sub get_num_docs {
102 my $self = shift (@_);
103
104 return $self->{'num_docs'};
105}
106
107sub get_num_sections {
108 my $self = shift (@_);
109
110 return $self->{'num_sections'};
111}
112
113# num_bytes is the actual number of bytes in the collection
114# this is normally the same as what's processed during text compression
115sub get_num_bytes {
116 my $self = shift (@_);
117
118 return $self->{'num_bytes'};
119}
120
121# num_processed_bytes is the number of bytes actually passed
122# to mgpp for the current index
123sub get_num_processed_bytes {
124 my $self = shift (@_);
125
126 return $self->{'num_processed_bytes'};
127}
128
129sub set_output_handle {
130 my $self = shift (@_);
131 my ($handle) = @_;
132
133 $self->{'output_handle'} = $handle;
134}
135
136sub set_mode {
137 my $self = shift (@_);
138 my ($mode) = @_;
139
140 $self->{'mode'} = $mode;
141}
142
143sub set_assocdir {
144 my $self = shift (@_);
145 my ($assocdir) = @_;
146
147 $self->{'assocdir'} = $assocdir;
148}
149
150sub set_dontgdbm {
151 my $self = shift (@_);
152 my ($dontgdbm) = @_;
153
154 $self->{'dontgdbm'} = $dontgdbm;
155}
156
157sub set_index {
158 my $self = shift (@_);
159 my ($index, $indexexparr) = @_;
160
161 $self->{'index'} = $index;
162 $self->{'indexexparr'} = $indexexparr if defined $indexexparr;
163}
164
165sub get_index {
166 my $self = shift (@_);
167
168 return $self->{'index'};
169}
170
171sub set_classifiers {
172 my $self = shift (@_);
173 my ($classifiers) = @_;
174
175 $self->{'classifiers'} = $classifiers;
176}
177
178sub set_indexing_text {
179 my $self = shift (@_);
180 my ($indexing_text) = @_;
181
182 $self->{'indexing_text'} = $indexing_text;
183}
184
185sub get_indexing_text {
186 my $self = shift (@_);
187
188 return $self->{'indexing_text'};
189}
190
191sub set_store_text {
192 my $self = shift (@_);
193 my ($store_text) = @_;
194
195 $self->{'store_text'} = $store_text;
196}
197
198sub get_doc_list {
199 my $self = shift(@_);
200
201 return @{$self->{'doclist'}};
202}
203
204sub set_indexfieldmap {
205 my $self = shift (@_);
206 my ($indexmap) = @_;
207
208 $self->{'indexfieldmap'} = $indexmap;
209}
210
211sub get_indexfieldmap {
212 my $self = shift (@_);
213
214 return $self->{'indexfieldmap'};
215}
216
217sub set_levels {
218 my $self = shift (@_);
219 my ($levels) = @_;
220
221 $self->{'levels'} = $levels;
222}
223
224sub set_strip_html {
225 my $self = shift (@_);
226 my ($strip) = @_;
227 $self->{'strip_html'}=$strip;
228}
229
230sub process {
231 my $self = shift (@_);
232 my $method = $self->{'mode'};
233
234 $self->$method(@_);
235}
236
237# use 'Paged' if document has no more than 2 levels
238# and each section at second level has a number for
239# Title metadata
240# also use Paged if gsdlthistype metadata is set to Paged
241sub get_document_type {
242 my $self = shift (@_);
243 my ($doc_obj) = @_;
244
245 my $thistype = "VList";
246 my $childtype = "VList";
247 my $title;
248 my @tmp = ();
249
250 my $section = $doc_obj->get_top_section ();
251
252 my $gsdlthistype = $doc_obj->get_metadata_element ($section, "gsdlthistype");
253 if (defined $gsdlthistype) {
254 if ($gsdlthistype eq "Paged") {
255 $thistype = "Paged";
256 $childtype = "Paged";
257 return ($thistype, $childtype);
258 } elsif ($gsdlthistype eq "Hierarchy") {
259 return ($thistype, $childtype); # use VList, VList
260 }
261 }
262 my $first = 1;
263 while (defined $section) {
264 @tmp = split /\./, $section;
265 if (scalar(@tmp) > 1) {
266 return ($thistype, $childtype);
267 }
268 if (!$first) {
269 $title = $doc_obj->get_metadata_element ($section, "Title");
270 if (!defined $title || $title !~ /^\d+$/) {
271 return ($thistype, $childtype);
272 }
273 }
274 $first = 0;
275 $section = $doc_obj->get_next_section($section);
276 }
277 if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
278 $thistype = "Paged";
279 } else {
280 $thistype = "Invisible";
281 }
282 $childtype = "Paged";
283 return ($thistype, $childtype);
284}
285
286sub assoc_files {
287 my $self = shift (@_);
288 my ($doc_obj, $archivedir) = @_;
289 my ($afile);
290
291 foreach my $assoc_file (@{$doc_obj->get_assoc_files()}) {
292 # if assoc file starts with a slash, we put it relative to the assoc
293 # dir, otherwise it is relative to the HASH... directory
294 if ($assoc_file->[1] =~ m@^[/\\]@) {
295 $afile = &util::filename_cat($self->{'assocdir'},$assoc_file->[1]);
296 } else {
297 $afile = &util::filename_cat($self->{'assocdir'}, $archivedir, $assoc_file->[1]);
298 }
299 &util::hard_link ($assoc_file->[0], $afile);
300 }
301}
302
303sub infodb {
304 my $self = shift (@_);
305 my ($doc_obj, $filename) = @_;
306 my $handle = $self->{'output_handle'};
307
308 my $doctype = $doc_obj->get_doc_type();
309
310 # only output this document if it is one to be indexed
311 return if ($doctype ne "indexed_doc");
312
313 #if a Section level index is not built, the gdbm file should be at doc
314 #level not Section
315 my $docs_only = 1;
316 if ($self->{'levels'}->{'section'}) {
317 $docs_only = 0;
318 }
319
320 my ($archivedir) = $filename =~ /^(.*?)(?:\/|\\)[^\/\\]*$/;
321 $archivedir = "" unless defined $archivedir;
322 $archivedir =~ s/\\/\//g;
323 $archivedir =~ s/^\/+//;
324 $archivedir =~ s/\/+$//;
325
326 # resolve the final filenames of the files associated with this document
327 $self->assoc_files ($doc_obj, $archivedir);
328
329 #GRB: moved 1/06/2004 from GRB01062004
330 #add this document to the browse structure
331 push(@{$self->{'doclist'}},$doc_obj->get_OID())
332 unless ($doctype eq "classification");
333
334 # classify this document
335 &classify::classify_doc ($self->{'classifiers'}, $doc_obj);
336 #GRB: end of moved block
337
338 # this is another document
339 $self->{'num_docs'} += 1 unless ($doctype eq "classification");
340
341 # is this a paged or a hierarchical document
342 my ($thistype, $childtype) = $self->get_document_type ($doc_obj);
343
344 my $section = $doc_obj->get_top_section ();
345 my $doc_OID = $doc_obj->get_OID();
346 my $first = 1;
347 my $url = "";
348 while (defined $section) {
349 # update a few statistics
350 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
351 $self->{'num_sections'} += 1 unless ($doctype eq "classification");
352
353 # output the section name
354 if ($section eq "") { print $handle "[$doc_OID]\n"; }
355 else { print $handle "[$doc_OID.$section]\n"; }
356
357 # output the fact that this document is a document (unless doctype
358 # has been set to something else from within a plugin
359 my $dtype = $doc_obj->get_metadata_element ($section, "doctype");
360 if (!defined $dtype || $dtype !~ /\w/) {
361 print $handle "<doctype>doc\n";
362 }
363
364 # output whether this node contains text
365 if ($doc_obj->get_text_length($section) > 0) {
366 print $handle "<hastxt>1\n";
367 } else {
368 print $handle "<hastxt>0\n";
369 }
370
371 # output all the section metadata
372 my $metadata = $doc_obj->get_all_metadata ($section);
373 foreach my $pair (@$metadata) {
374 my ($field, $value) = (@$pair);
375
376 if ($field ne "Identifier" && $field !~ /^gsdl/ &&
377 defined $value && $value ne "") {
378
379 # escape problematic stuff
380 $value =~ s/\\/\\\\/g;
381 $value =~ s/\n/\\n/g;
382 $value =~ s/\r/\\r/g;
383
384 # special case for URL metadata
385 if ($field =~ /^URL$/i) {
386 $url .= "[$value]\n";
387 if ($section eq "") {$url .= "<section>$doc_OID\n";}
388 else {$url .= "<section>$doc_OID.$section\n";}
389 $url .= '-' x 70 . "\n";
390 }
391
392 if (!defined $self->{'dontgdbm'}->{$field}) {
393 print $handle "<$field>$value\n";
394 }
395 }
396 }
397
398 # output archivedir if at top level
399 if ($section eq $doc_obj->get_top_section()) {
400 print $handle "<archivedir>$archivedir\n";
401 }
402
403 # output document display type
404 if ($first) {
405 print $handle "<thistype>$thistype\n";
406 }
407
408 if (!$docs_only) {
409 # output a list of children
410 my $children = $doc_obj->get_children ($section);
411 if (scalar(@$children) > 0) {
412 print $handle "<childtype>$childtype\n";
413 print $handle "<contains>";
414 my $firstchild = 1;
415 foreach $child (@$children) {
416 print $handle ";" unless $firstchild;
417 $firstchild = 0;
418 if ($child =~ /^.*?\.(\d+)$/) {
419 print $handle "\".$1";
420 } else {
421 print $handle "\".$child";
422 }
423# if ($child eq "") { print $handle "$doc_OID"; }
424# elsif ($section eq "") { print $handle "$doc_OID.$child"; }
425# else { print $handle "$doc_OID.$section.$child"; }
426 }
427 print $handle "\n";
428 }
429 #output the matching doc number
430 print $handle "<docnum>$self->{'num_sections'}\n";
431
432 } # if (!$docs_only)
433 else { #docs only, doc num is num_docs not num_sections
434 # output the matching document number
435 print $handle "<docnum>$self->{'num_docs'}\n";
436 }
437
438 print $handle '-' x 70, "\n";
439
440
441 # output a database entry for the document number
442 if ($docs_only) {
443 print $handle "[$self->{'num_docs'}]\n";
444 print $handle "<section>$doc_OID\n";
445 }
446 else {
447 print $handle "[$self->{'num_sections'}]\n";
448 if ($section eq "") { print $handle "<section>$doc_OID\n"; }
449 else { print $handle "<section>$doc_OID.$section\n"; }
450 }
451 print $handle '-' x 70, "\n";
452
453 # output entry for url
454 if ($url ne "") {
455 print $handle $url;
456 }
457
458 $first = 0;
459 $section = $doc_obj->get_next_section($section);
460 last if ($docs_only); # if no sections wanted, only gdbm the docs
461 }
462
463 #GRB01062004: see code above moved from here
464}
465
466#sub find_paragraphs {
467# $_[1] =~ s/(<p\b)/<Paragraph>$1/gi;
468#}
469
470#this function strips the html tags from the doc if ($strip_html) and
471# if ($para) replaces <p> with <Paragraph> tags.
472# if both are false, the original text is returned
473#assumes that <pre> and </pre> have no spaces, and removes all < and > inside
474#these tags
475sub preprocess_text {
476 my $self = shift (@_);
477 my ($text, $strip_html, $para) = @_;
478 my ($outtext) = "";
479 if ($strip_html) {
480 while ($text =~ /<([^>]*)>/ && $text ne "") {
481
482 $tag = $1;
483 $outtext .= $`." "; #add everything before the matched tag
484 $text = $'; #everything after the matched tag
485 if ($para && $tag =~ /^\s*p\s/i) {
486 $outtext .= $para;
487 }
488 elsif ($tag =~ /^pre$/) { # a pre tag
489 $text =~ /<\/pre>/; # find the closing pre tag
490 my $tmp_text = $`; #everything before the closing pre tag
491 $text = $'; #everything after the </pre>
492 $tmp_text =~ s/[<>]//g; # remove all < and >
493 $outtext.= $tmp_text . " ";
494 }
495 }
496
497 $outtext .= $text; # add any remaining text
498 return $outtext;
499 } #if strip_html
500
501 #if ($para) {
502 #$text =~ s/(<p\b)/$para$1/gi;
503 #return $text;
504 # }
505 return $text;
506}
507
508
509
510sub filter_text {
511 # $self->filter_text ($field, $new_text);
512 # don't want to do anything for this version, however,
513 # in a particular collection you might want to override
514 # this method to post-process certain fields depending on
515 # the field, or whether we are outputting it for indexing
516}
517
518sub text {
519 my $self = shift (@_);
520 my ($doc_obj) = @_;
521 my $handle = $self->{'output_handle'};
522 my $outhandle = $self->{'outhandle'};
523 my $indexed_doc = 1;
524
525 # only output this document if it is one to be indexed
526 return if ($doc_obj->get_doc_type() ne "indexed_doc");
527
528 # see if this document belongs to this subcollection
529 foreach my $indexexp (@{$self->{'indexexparr'}}) {
530 $indexed_doc = 0;
531 my ($field, $exp, $options) = split /\//, $indexexp;
532 if (defined ($field) && defined ($exp)) {
533 my ($bool) = $field =~ /^(.)/;
534 $field =~ s/^.// if $bool eq '!';
535 if ($field =~ /^filename$/i) {
536 $field = $doc_obj->get_source_filename();
537 } else {
538 $field = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $field);
539 }
540 next unless defined $field;
541 if ($bool eq '!') {
542 if ($options =~ /^i$/i) {
543 if ($field !~ /$exp/i) {$indexed_doc = 1; last;}
544 } else {
545 if ($field !~ /$exp/) {$indexed_doc = 1; last;}
546 }
547 } else {
548 if ($options =~ /^i$/i) {
549 if ($field =~ /$exp/i) {$indexed_doc = 1; last;}
550 } else {
551 if ($field =~ /$exp/) {$indexed_doc = 1; last;}
552 }
553 }
554 }
555 }
556
557 # this is another document
558 $self->{'num_docs'} += 1;
559
560 # get the parameters for the output
561 # split on : just in case there is subcoll and lang stuff
562 my ($fields) = split (/:/, $self->{'index'});
563
564 my ($documenttag) = "";
565 my($documentendtag) = "";
566 if ($self->{'levels'}->{'document'}) {
567 $documenttag = "\n<". $level_map{'document'} . ">\n";
568 $documentendtag = "\n</". $level_map{'document'} . ">\n";
569 }
570 my ($sectiontag) = "";
571 if ($self->{'levels'}->{'section'}) {
572 $sectiontag = "\n<". $level_map{'section'} . ">\n";
573 }
574 my ($paratag) = "";
575 if ($self->{'levels'}->{'paragraph'}) {
576 if ($self->{'strip_html'}) {
577 $paratag = "<". $level_map{'paragraph'} . ">";
578 } else {
579 print $outhandle "Paragraph level can not be used with no_strip_html!. Not indexing Paragraphs.\n";
580 }
581 }
582
583 my $doc_section = 0; # just for this document
584
585 my $text = $documenttag;
586
587 # get the text for this document
588 my $section = $doc_obj->get_top_section();
589 while (defined $section) {
590 # update a few statistics
591 $doc_section++;
592 $self->{'num_sections'} += 1;
593 $text .= "$sectiontag";
594
595 if ($indexed_doc) {
596 if ($self->{'indexing_text'}) {
597 $text .= "$paratag"; # only add para tags for indexing
598 # note that we assume that metadata will not be asked for for the compressed text, so we add para tags without checking for indexing_text
599 }
600 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
601 foreach my $field (split (/,/, $fields)) {
602 # only deal with this field if it doesn't start with top or
603 # this is the first section
604 my $real_field = $field;
605 if (!($real_field =~ s/^top//) || ($doc_section == 1)) {
606 my $new_text = "";
607 my $tmp_text = "";
608 if ($real_field eq "text") {
609 if ($self->{'indexing_text'}) { #tag the text with <Text>...</Text>, add the <Paragraph> tags and strip out html if needed
610 $new_text .= "$paratag<TX>\n";
611 $tmp_text .= $doc_obj->get_text ($section);
612 $tmp_text = $self->preprocess_text($tmp_text, $self->{'strip_html'}, "</TX>$paratag<TX>");
613
614 $new_text .= "$tmp_text</TX>\n";
615 #if (!defined $self->{'indexfields'}->{'TextOnly'}) {
616 #$self->{'indexfields'}->{'TextOnly'} = 1;
617 #}
618 }
619 else { # leave html stuff in, and dont add Paragraph tags - never retrieve paras at the moment
620 $new_text .= $doc_obj->get_text ($section) if $self->{'store_text'};
621 }
622 } else { # metadata field
623 if ($real_field eq "allfields") { #ignore
624 }
625 elsif ($real_field eq "metadata") { # insert all metadata
626 #except gsdl stuff
627 my $shortname = "";
628 my $metadata = $doc_obj->get_all_metadata ($section);
629 foreach $pair (@$metadata) {
630 my ($mfield, $mvalue) = (@$pair);
631 # check fields here, maybe others dont want - change to use dontindex!!
632 if ($mfield ne "Identifier"
633 && $mfield !~ /^gsdl/
634 && $mfield ne "classifytype"
635 && $mfield ne "assocfilepath"
636 && defined $mvalue && $mvalue ne "") {
637
638 if (defined $self->{'indexfieldmap'}->{$mfield}) {
639 $shortname = $self->{'indexfieldmap'}->{$mfield};
640 }
641 else {
642 $shortname = $self->create_shortname($mfield);
643 $self->{'indexfieldmap'}->{$mfield} = $shortname;
644 $self->{'indexfieldmap'}->{$shortname} = 1;
645 }
646 $new_text .= "$paratag<$shortname>$mvalue</$shortname>\n";
647 if (!defined $self->{'indexfields'}->{$mfield}) {
648 $self->{'indexfields'}->{$mfield} = 1;
649 }
650 }
651 }
652
653 }
654 else { #individual metadata specified
655 my $shortname="";
656 #if (!defined $self->{'indexfields'}->{$real_field}) {
657 #$self->{'indexfields'}->{$real_field} = 1;
658 #}
659 if (defined $self->{'indexfieldmap'}->{$real_field}) {
660 $shortname = $self->{'indexfieldmap'}->{$real_field};
661 }
662 else {
663 $shortname = $self->create_shortname($real_field);
664 $self->{'indexfieldmap'}->{$real_field} = $shortname;
665 $self->{'indexfieldmap'}->{$shortname} = 1;
666 }
667 foreach $item (@{$doc_obj->get_metadata ($section, $real_field)}) {
668 $new_text .= "$paratag<$shortname>$item</$shortname>\n";
669 }
670 }
671
672 }
673
674 # filter the text
675 $self->filter_text ($field, $new_text);
676
677 $self->{'num_processed_bytes'} += length ($new_text);
678 $text .= "$new_text";
679 }
680 }
681 } # if (indexed_doc)
682
683 $section = $doc_obj->get_next_section($section);
684 } #while defined section
685 print $handle "$text\n$documentendtag";
686
687}
688
689#chooses the first two letters or digits for the shortname
690#now ignores non-letdig characters
691sub create_shortname {
692 $self = shift(@_);
693
694 my ($realname) = @_;
695 #take the first two chars
696 my $shortname;
697 if ($realname =~ /^[^\w]*(\w)[^\w]*(\w)/) {
698 $shortname = "$1$2";
699 } else {
700 # there aren't two letdig's in the field - try arbitrary combinations
701 $realname = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
702 $shortname = "AB";
703 }
704 $shortname =~ tr/a-z/A-Z/;
705
706 #if already used, take the first and third letdigs and so on
707 $count = 1;
708 while (defined $self->{'indexfieldmap'}->{$shortname}) {
709 if ($realname =~ /^[^\w]*(\w)([^\w]*\w){$count}[^\w]*(\w)/) {
710 $shortname = "$1$3";
711 $count++;
712 $shortname =~ tr/a-z/A-Z/;
713
714 }
715 else {
716 #remove up to and incl the first letdig
717 $realname =~ s/^[^\w]*\w//;
718 $count = 0;
719 }
720 }
721
722 return $shortname;
723}
724
7251;
726
Note: See TracBrowser for help on using the repository browser.