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

Last change on this file since 1852 was 1852, checked in by kjm18, 23 years ago

heaps of changes

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