source: main/tags/2.39/gsdl/perllib/mgppbuildproc.pm@ 25322

Last change on this file since 25322 was 3834, checked in by sjboddie, 21 years ago

Prevent "use bytes" from causing errors for older perls

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