source: trunk/gsdl/perllib/mgppbuildproc.pm@ 6761

Last change on this file since 6761 was 6553, checked in by kjdon, 20 years ago

If a plugin sets the gsdlthistype metadata to Paged, then the document will be treated as a paged doc - this allows Titles that are not totally numeric.

  • Property svn:keywords set to Author Date Id Revision
File size: 19.3 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 && $gsdlthistype eq "Paged") {
254 $thistype = "Paged";
255 $childtype = "Paged";
256 return ($thistype, $childtype);
257 }
258 my $first = 1;
259 while (defined $section) {
260 @tmp = split /\./, $section;
261 if (scalar(@tmp) > 1) {
262 return ($thistype, $childtype);
263 }
264 if (!$first) {
265 $title = $doc_obj->get_metadata_element ($section, "Title");
266 if (!defined $title || $title !~ /^\d+$/) {
267 return ($thistype, $childtype);
268 }
269 }
270 $first = 0;
271 $section = $doc_obj->get_next_section($section);
272 }
273 if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
274 $thistype = "Paged";
275 } else {
276 $thistype = "Invisible";
277 }
278 $childtype = "Paged";
279 return ($thistype, $childtype);
280}
281
282sub assoc_files {
283 my $self = shift (@_);
284 my ($doc_obj, $archivedir) = @_;
285 my ($afile);
286
287 foreach my $assoc_file (@{$doc_obj->get_assoc_files()}) {
288 # if assoc file starts with a slash, we put it relative to the assoc
289 # dir, otherwise it is relative to the HASH... directory
290 if ($assoc_file->[1] =~ m@^[/\\]@) {
291 $afile = &util::filename_cat($self->{'assocdir'},$assoc_file->[1]);
292 } else {
293 $afile = &util::filename_cat($self->{'assocdir'}, $archivedir, $assoc_file->[1]);
294 }
295 &util::hard_link ($assoc_file->[0], $afile);
296 }
297}
298
299sub infodb {
300 my $self = shift (@_);
301 my ($doc_obj, $filename) = @_;
302 my $handle = $self->{'output_handle'};
303
304 my $doctype = $doc_obj->get_doc_type();
305
306 # only output this document if it is one to be indexed
307 return if ($doctype ne "indexed_doc");
308
309 #if a Section level index is not built, the gdbm file should be at doc
310 #level not Section
311 my $docs_only = 1;
312 if ($self->{'levels'}->{'section'}) {
313 $docs_only = 0;
314 }
315
316 my ($archivedir) = $filename =~ /^(.*?)(?:\/|\\)[^\/\\]*$/;
317 $archivedir = "" unless defined $archivedir;
318 $archivedir =~ s/\\/\//g;
319 $archivedir =~ s/^\/+//;
320 $archivedir =~ s/\/+$//;
321
322 $self->assoc_files ($doc_obj, $archivedir);
323
324 # this is another document
325 $self->{'num_docs'} += 1 unless ($doctype eq "classification");
326
327 # is this a paged or a hierarchical document
328 my ($thistype, $childtype) = $self->get_document_type ($doc_obj);
329
330 my $section = $doc_obj->get_top_section ();
331 my $doc_OID = $doc_obj->get_OID();
332 my $first = 1;
333 my $url = "";
334 while (defined $section) {
335 # update a few statistics
336 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
337 $self->{'num_sections'} += 1 unless ($doctype eq "classification");
338
339 # output the section name
340 if ($section eq "") { print $handle "[$doc_OID]\n"; }
341 else { print $handle "[$doc_OID.$section]\n"; }
342
343 # output the fact that this document is a document (unless doctype
344 # has been set to something else from within a plugin
345 my $dtype = $doc_obj->get_metadata_element ($section, "doctype");
346 if (!defined $dtype || $dtype !~ /\w/) {
347 print $handle "<doctype>doc\n";
348 }
349
350 # output whether this node contains text
351 if ($doc_obj->get_text_length($section) > 0) {
352 print $handle "<hastxt>1\n";
353 } else {
354 print $handle "<hastxt>0\n";
355 }
356
357 # output all the section metadata
358 my $metadata = $doc_obj->get_all_metadata ($section);
359 foreach my $pair (@$metadata) {
360 my ($field, $value) = (@$pair);
361
362 if ($field ne "Identifier" && $field !~ /^gsdl/ &&
363 defined $value && $value ne "") {
364
365 # escape problematic stuff
366 $value =~ s/\\/\\\\/g;
367 $value =~ s/\n/\\n/g;
368 $value =~ s/\r/\\r/g;
369
370 # special case for URL metadata
371 if ($field =~ /^URL$/i) {
372 $url .= "[$value]\n";
373 if ($section eq "") {$url .= "<section>$doc_OID\n";}
374 else {$url .= "<section>$doc_OID.$section\n";}
375 $url .= '-' x 70 . "\n";
376 }
377
378 if (!defined $self->{'dontgdbm'}->{$field}) {
379 print $handle "<$field>$value\n";
380 }
381 }
382 }
383
384 # output archivedir if at top level
385 if ($section eq $doc_obj->get_top_section()) {
386 print $handle "<archivedir>$archivedir\n";
387 }
388
389 # output document display type
390 if ($first) {
391 print $handle "<thistype>$thistype\n";
392 }
393
394 if (!$docs_only) {
395 # output a list of children
396 my $children = $doc_obj->get_children ($section);
397 if (scalar(@$children) > 0) {
398 print $handle "<childtype>$childtype\n";
399 print $handle "<contains>";
400 my $firstchild = 1;
401 foreach $child (@$children) {
402 print $handle ";" unless $firstchild;
403 $firstchild = 0;
404 if ($child =~ /^.*?\.(\d+)$/) {
405 print $handle "\".$1";
406 } else {
407 print $handle "\".$child";
408 }
409# if ($child eq "") { print $handle "$doc_OID"; }
410# elsif ($section eq "") { print $handle "$doc_OID.$child"; }
411# else { print $handle "$doc_OID.$section.$child"; }
412 }
413 print $handle "\n";
414 }
415 #output the matching doc number
416 print $handle "<docnum>$self->{'num_sections'}\n";
417
418 } # if (!$docs_only)
419 else { #docs only, doc num is num_docs not num_sections
420 # output the matching document number
421 print $handle "<docnum>$self->{'num_docs'}\n";
422 }
423
424 print $handle '-' x 70, "\n";
425
426
427 # output a database entry for the document number
428 if ($docs_only) {
429 print $handle "[$self->{'num_docs'}]\n";
430 print $handle "<section>$doc_OID\n";
431 }
432 else {
433 print $handle "[$self->{'num_sections'}]\n";
434 if ($section eq "") { print $handle "<section>$doc_OID\n"; }
435 else { print $handle "<section>$doc_OID.$section\n"; }
436 }
437 print $handle '-' x 70, "\n";
438
439 # output entry for url
440 if ($url ne "") {
441 print $handle $url;
442 }
443
444 $first = 0;
445 $section = $doc_obj->get_next_section($section);
446 last if ($docs_only); # if no sections wanted, only gdbm the docs
447 }
448
449 #add this document to the browse structure
450 push(@{$self->{'doclist'}},$doc_obj->get_OID())
451 unless ($doctype eq "classification");
452
453 # classify this document
454 &classify::classify_doc ($self->{'classifiers'}, $doc_obj);
455
456}
457
458#sub find_paragraphs {
459# $_[1] =~ s/(<p\b)/<Paragraph>$1/gi;
460#}
461
462#this function strips the html tags from the doc if ($strip_html) and
463# if ($para) replaces <p> with <Paragraph> tags.
464# if both are false, the original text is returned
465#assumes that <pre> and </pre> have no spaces, and removes all < and > inside
466#these tags
467sub preprocess_text {
468 my $self = shift (@_);
469 my ($text, $strip_html, $para) = @_;
470 my ($outtext) = "";
471 if ($strip_html) {
472 while ($text =~ /<([^>]*)>/ && $text ne "") {
473
474 $tag = $1;
475 $outtext .= $`." "; #add everything before the matched tag
476 $text = $'; #everything after the matched tag
477 if ($para && $tag =~ /^\s*p\s/i) {
478 $outtext .= $para;
479 }
480 elsif ($tag =~ /^pre$/) { # a pre tag
481 $text =~ /<\/pre>/; # find the closing pre tag
482 my $tmp_text = $`; #everything before the closing pre tag
483 $text = $'; #everything after the </pre>
484 $tmp_text =~ s/[<>]//g; # remove all < and >
485 $outtext.= $tmp_text . " ";
486 }
487 }
488
489 $outtext .= $text; # add any remaining text
490 return $outtext;
491 } #if strip_html
492
493 #if ($para) {
494 #$text =~ s/(<p\b)/$para$1/gi;
495 #return $text;
496 # }
497 return $text;
498}
499
500
501
502sub filter_text {
503 # $self->filter_text ($field, $new_text);
504 # don't want to do anything for this version, however,
505 # in a particular collection you might want to override
506 # this method to post-process certain fields depending on
507 # the field, or whether we are outputting it for indexing
508}
509
510sub text {
511 my $self = shift (@_);
512 my ($doc_obj) = @_;
513 my $handle = $self->{'output_handle'};
514 my $outhandle = $self->{'outhandle'};
515 my $indexed_doc = 1;
516
517 # only output this document if it is one to be indexed
518 return if ($doc_obj->get_doc_type() ne "indexed_doc");
519
520 # see if this document belongs to this subcollection
521 foreach my $indexexp (@{$self->{'indexexparr'}}) {
522 $indexed_doc = 0;
523 my ($field, $exp, $options) = split /\//, $indexexp;
524 if (defined ($field) && defined ($exp)) {
525 my ($bool) = $field =~ /^(.)/;
526 $field =~ s/^.// if $bool eq '!';
527 if ($field =~ /^filename$/i) {
528 $field = $doc_obj->get_source_filename();
529 } else {
530 $field = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $field);
531 }
532 next unless defined $field;
533 if ($bool eq '!') {
534 if ($options =~ /^i$/i) {
535 if ($field !~ /$exp/i) {$indexed_doc = 1; last;}
536 } else {
537 if ($field !~ /$exp/) {$indexed_doc = 1; last;}
538 }
539 } else {
540 if ($options =~ /^i$/i) {
541 if ($field =~ /$exp/i) {$indexed_doc = 1; last;}
542 } else {
543 if ($field =~ /$exp/) {$indexed_doc = 1; last;}
544 }
545 }
546 }
547 }
548
549 # this is another document
550 $self->{'num_docs'} += 1;
551
552 # get the parameters for the output
553 # split on : just in case there is subcoll and lang stuff
554 my ($fields) = split (/:/, $self->{'index'});
555
556 my ($documenttag) = "";
557 my($documentendtag) = "";
558 if ($self->{'levels'}->{'document'}) {
559 $documenttag = "\n<". %level_map->{'document'} . ">\n";
560 $documentendtag = "\n</". %level_map->{'document'} . ">\n";
561 }
562 my ($sectiontag) = "";
563 if ($self->{'levels'}->{'section'}) {
564 $sectiontag = "\n<". %level_map->{'section'} . ">\n";
565 }
566 my ($paratag) = "";
567 if ($self->{'levels'}->{'paragraph'}) {
568 if ($self->{'strip_html'}) {
569 $paratag = "<". %level_map->{'paragraph'} . ">";
570 } else {
571 print $outhandle "Paragraph level can not be used with no_strip_html!. Not indexing Paragraphs.\n";
572 }
573 }
574
575 my $doc_section = 0; # just for this document
576
577 my $text = $documenttag;
578
579 # get the text for this document
580 my $section = $doc_obj->get_top_section();
581 while (defined $section) {
582 # update a few statistics
583 $doc_section++;
584 $self->{'num_sections'} += 1;
585 $text .= "$sectiontag";
586
587 if ($indexed_doc) {
588 if ($self->{'indexing_text'}) {
589 $text .= "$paratag"; # only add para tags for indexing
590 # 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
591 }
592 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
593 foreach my $field (split (/,/, $fields)) {
594 # only deal with this field if it doesn't start with top or
595 # this is the first section
596 my $real_field = $field;
597 if (!($real_field =~ s/^top//) || ($doc_section == 1)) {
598 my $new_text = "";
599 my $tmp_text = "";
600 if ($real_field eq "text") {
601 if ($self->{'indexing_text'}) { #tag the text with <Text>...</Text>, add the <Paragraph> tags and strip out html if needed
602 $new_text .= "$paratag<TX>\n";
603 $tmp_text .= $doc_obj->get_text ($section);
604 $tmp_text = $self->preprocess_text($tmp_text, $self->{'strip_html'}, "</TX>$paratag<TX>");
605
606 $new_text .= "$tmp_text</TX>\n";
607 #if (!defined $self->{'indexfields'}->{'TextOnly'}) {
608 #$self->{'indexfields'}->{'TextOnly'} = 1;
609 #}
610 }
611 else { # leave html stuff in, and dont add Paragraph tags - never retrieve paras at the moment
612 $new_text .= $doc_obj->get_text ($section) if $self->{'store_text'};
613 }
614 } else { # metadata field
615 if ($real_field eq "allfields") { #ignore
616 }
617 elsif ($real_field eq "metadata") { # insert all metadata
618 #except gsdl stuff
619 my $shortname = "";
620 my $metadata = $doc_obj->get_all_metadata ($section);
621 foreach $pair (@$metadata) {
622 my ($mfield, $mvalue) = (@$pair);
623 # check fields here, maybe others dont want - change to use dontindex!!
624 if ($mfield ne "Identifier"
625 && $mfield !~ /^gsdl/
626 && $mfield ne "classifytype"
627 && $mfield ne "assocfilepath"
628 && defined $mvalue && $mvalue ne "") {
629
630 if (defined $self->{'indexfieldmap'}->{$mfield}) {
631 $shortname = $self->{'indexfieldmap'}->{$mfield};
632 }
633 else {
634 $shortname = $self->create_shortname($mfield);
635 $self->{'indexfieldmap'}->{$mfield} = $shortname;
636 $self->{'indexfieldmap'}->{$shortname} = 1;
637 }
638 $new_text .= "$paratag<$shortname>$mvalue</$shortname>\n";
639 if (!defined $self->{'indexfields'}->{$mfield}) {
640 $self->{'indexfields'}->{$mfield} = 1;
641 }
642 }
643 }
644
645 }
646 else { #individual metadata specified
647 my $shortname="";
648 #if (!defined $self->{'indexfields'}->{$real_field}) {
649 #$self->{'indexfields'}->{$real_field} = 1;
650 #}
651 if (defined $self->{'indexfieldmap'}->{$real_field}) {
652 $shortname = $self->{'indexfieldmap'}->{$real_field};
653 }
654 else {
655 $shortname = $self->create_shortname($real_field);
656 $self->{'indexfieldmap'}->{$real_field} = $shortname;
657 $self->{'indexfieldmap'}->{$shortname} = 1;
658 }
659 foreach $item (@{$doc_obj->get_metadata ($section, $real_field)}) {
660 $new_text .= "$paratag<$shortname>$item</$shortname>\n";
661 }
662 }
663
664 }
665
666 # filter the text
667 $self->filter_text ($field, $new_text);
668
669 $self->{'num_processed_bytes'} += length ($new_text);
670 $text .= "$new_text";
671 }
672 }
673 } # if (indexed_doc)
674
675 $section = $doc_obj->get_next_section($section);
676 } #while defined section
677 print $handle "$text\n$documentendtag";
678
679}
680
681#chooses the first two letters or digits for the shortname
682#now ignores non-letdig characters
683sub create_shortname {
684 $self = shift(@_);
685
686 my ($realname) = @_;
687 #take the first two chars
688 my $shortname;
689 if ($realname =~ /^[^\w]*(\w)[^\w]*(\w)/) {
690 $shortname = "$1$2";
691 } else {
692 # there aren't two letdig's in the field - try arbitrary combinations
693 $realname = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
694 $shortname = "AB";
695 }
696 $shortname =~ tr/a-z/A-Z/;
697
698 #if already used, take the first and third letdigs and so on
699 $count = 1;
700 while (defined $self->{'indexfieldmap'}->{$shortname}) {
701 if ($realname =~ /^[^\w]*(\w)([^\w]*\w){$count}[^\w]*(\w)/) {
702 $shortname = "$1$3";
703 $count++;
704 $shortname =~ tr/a-z/A-Z/;
705
706 }
707 else {
708 #remove up to and incl the first letdig
709 $realname =~ s/^[^\w]*\w//;
710 $count = 0;
711 }
712 }
713
714 return $shortname;
715}
716
7171;
718
Note: See TracBrowser for help on using the repository browser.