source: tags/gsdl-2_30d-distribution/gsdl/perllib/mgbuildproc.pm@ 2308

Last change on this file since 2308 was 1424, checked in by sjboddie, 24 years ago

Added a -out option to most of the perl building scripts to allow output
debug information to be directed to a file.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 12.4 KB
Line 
1###########################################################################
2#
3# mgbuildproc.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 mg to process
28
29
30package mgbuildproc;
31
32use classify;
33use doc;
34use docproc;
35use util;
36
37BEGIN {
38 @ISA = ('docproc');
39}
40
41
42sub new {
43 my ($class, $collection, $source_dir, $build_dir,
44 $verbosity, $outhandle) = @_;
45 my $self = new docproc ();
46
47 # outhandle is where all the debugging info goes
48 # output_handle is where the output of the plugins is piped
49 # to (i.e. mg, gdbm etc.)
50 $outhandle = STDERR unless defined $outhandle;
51
52 $self->{'collection'} = $collection;
53 $self->{'source_dir'} = $source_dir;
54 $self->{'build_dir'} = $build_dir;
55 $self->{'verbosity'} = $verbosity;
56 $self->{'classifiers'} = [];
57 $self->{'mode'} = "text";
58 $self->{'assocdir'} = $build_dir;
59 $self->{'dontgdbm'} = {};
60 $self->{'index'} = "section:text";
61 $self->{'indexexparr'} = [];
62 $self->{'output_handle'} = "STDOUT";
63 $self->{'num_docs'} = 0;
64 $self->{'num_sections'} = 0;
65 $self->{'num_bytes'} = 0;
66 $self->{'num_processed_bytes'} = 0;
67 $self->{'outhandle'} = $outhandle;
68
69 $self->{'indexing_text'} = 0;
70
71 return bless $self, $class;
72}
73
74sub reset {
75 my $self = shift (@_);
76
77 $self->{'num_docs'} = 0;
78 $self->{'num_sections'} = 0;
79 $self->{'num_processed_bytes'} = 0;
80 $self->{'num_bytes'} = 0;
81}
82
83sub get_num_docs {
84 my $self = shift (@_);
85
86 return $self->{'num_docs'};
87}
88
89sub get_num_sections {
90 my $self = shift (@_);
91
92 return $self->{'num_sections'};
93}
94
95# num_bytes is the actual number of bytes in the collection
96# this is normally the same as what's processed during text compression
97sub get_num_bytes {
98 my $self = shift (@_);
99
100 return $self->{'num_bytes'};
101}
102
103# num_processed_bytes is the number of bytes actually passed
104# to mg for the current index
105sub get_num_processed_bytes {
106 my $self = shift (@_);
107
108 return $self->{'num_processed_bytes'};
109}
110
111sub set_output_handle {
112 my $self = shift (@_);
113 my ($handle) = @_;
114
115 $self->{'output_handle'} = $handle;
116}
117
118sub set_mode {
119 my $self = shift (@_);
120 my ($mode) = @_;
121
122 $self->{'mode'} = $mode;
123}
124
125sub set_assocdir {
126 my $self = shift (@_);
127 my ($assocdir) = @_;
128
129 $self->{'assocdir'} = $assocdir;
130}
131
132sub set_dontgdbm {
133 my $self = shift (@_);
134 my ($dontgdbm) = @_;
135
136 $self->{'dontgdbm'} = $dontgdbm;
137}
138
139sub set_index {
140 my $self = shift (@_);
141 my ($index, $indexexparr) = @_;
142
143 $self->{'index'} = $index;
144 $self->{'indexexparr'} = $indexexparr if defined $indexexparr;
145}
146
147sub get_index {
148 my $self = shift (@_);
149
150 return $self->{'index'};
151}
152
153sub set_classifiers {
154 my $self = shift (@_);
155 my ($classifiers) = @_;
156
157 $self->{'classifiers'} = $classifiers;
158}
159
160sub set_indexing_text {
161 my $self = shift (@_);
162 my ($indexing_text) = @_;
163
164 $self->{'indexing_text'} = $indexing_text;
165}
166
167sub get_indexing_text {
168 my $self = shift (@_);
169
170 return $self->{'indexing_text'};
171}
172
173sub process {
174 my $self = shift (@_);
175 my $method = $self->{'mode'};
176
177 $self->$method(@_);
178}
179
180# use 'Paged' if document has no more than 2 levels
181# and each section at second level has a number for
182# Title metadata
183sub get_document_type {
184 my $self = shift (@_);
185 my ($doc_obj) = @_;
186
187 my $thistype = "VList";
188 my $childtype = "VList";
189 my $title;
190 my @tmp = ();
191
192 my $section = $doc_obj->get_top_section ();
193 my $first = 1;
194 while (defined $section) {
195 @tmp = split /\./, $section;
196 if (scalar(@tmp) > 1) {
197 return ($thistype, $childtype);
198 }
199 if (!$first) {
200 $title = $doc_obj->get_metadata_element ($section, "Title");
201 if (!defined $title || $title !~ /^\d+$/) {
202 return ($thistype, $childtype);
203 }
204 }
205 $first = 0;
206 $section = $doc_obj->get_next_section($section);
207 }
208 if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
209 $thistype = "Paged";
210 } else {
211 $thistype = "Invisible";
212 }
213 $childtype = "Paged";
214 return ($thistype, $childtype);
215}
216
217sub assoc_files {
218 my $self = shift (@_);
219 my ($doc_obj, $archivedir) = @_;
220 my ($afile);
221
222 foreach my $assoc_file (@{$doc_obj->get_assoc_files()}) {
223 # if assoc file contains directory structure of
224 # its own use it, otherwise use HASH... directory
225 if ($assoc_file->[1] =~ /[\/\\]/) {
226 $afile = &util::filename_cat($self->{'assocdir'}, $assoc_file->[1]);
227 } else {
228 $afile = &util::filename_cat($self->{'assocdir'}, $archivedir, $assoc_file->[1]);
229 }
230 &util::hard_link ($assoc_file->[0], $afile);
231 }
232}
233
234sub infodb {
235 my $self = shift (@_);
236 my ($doc_obj, $filename) = @_;
237 my $handle = $self->{'output_handle'};
238# $handle = "main::STDOUT";
239
240 my $doctype = $doc_obj->get_doc_type();
241
242 # only output this document if it is one to be indexed
243 return if ($doctype ne "indexed_doc");
244
245 my ($archivedir) = $filename =~ /^(.*?)(?:\/|\\)[^\/\\]*$/;
246 $archivedir = "" unless defined $archivedir;
247 $archivedir =~ s/\\/\//g;
248 $archivedir =~ s/^\/+//;
249 $archivedir =~ s/\/+$//;
250
251 $self->assoc_files ($doc_obj, $archivedir);
252
253 # this is another document
254 $self->{'num_docs'} += 1 unless ($doctype eq "classification");
255
256 # is this a paged or a hierarchical document
257 my ($thistype, $childtype) = $self->get_document_type ($doc_obj);
258
259 my $section = $doc_obj->get_top_section ();
260 my $doc_OID = $doc_obj->get_OID();
261 my $first = 1;
262 my $url = "";
263 while (defined $section) {
264 # update a few statistics
265 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
266 $self->{'num_sections'} += 1 unless ($doctype eq "classification");
267
268 # output the section name
269 if ($section eq "") { print $handle "[$doc_OID]\n"; }
270 else { print $handle "[$doc_OID.$section]\n"; }
271
272 # output the fact that this document is a document (unless doctype
273 # has been set to something else from within a plugin
274 my $dtype = $doc_obj->get_metadata_element ($section, "doctype");
275 if (!defined $dtype || $dtype !~ /\w/) {
276 print $handle "<doctype>doc\n";
277 }
278
279 # output whether this node contains text
280 if ($doc_obj->get_text_length($section) > 0) {
281 print $handle "<hastxt>1\n";
282 } else {
283 print $handle "<hastxt>0\n";
284 }
285
286 # output all the section metadata
287 my $metadata = $doc_obj->get_all_metadata ($section);
288 foreach my $pair (@$metadata) {
289 my ($field, $value) = (@$pair);
290
291 if ($field ne "Identifier" && $field !~ /^gsdl/ &&
292 defined $value && $value ne "") {
293
294 # escape problematic stuff
295 $value =~ s/\\/\\\\/g;
296 $value =~ s/\n/\\n/g;
297 $value =~ s/\r/\\r/g;
298
299 # special case for URL metadata
300 if ($field =~ /^URL$/i) {
301 $url .= "[$value]\n";
302 if ($section eq "") {$url .= "<section>$doc_OID\n";}
303 else {$url .= "<section>$doc_OID.$section\n";}
304 $url .= '-' x 70 . "\n";
305 }
306
307 if (!defined $self->{'dontgdbm'}->{$field}) {
308 print $handle "<$field>$value\n";
309 }
310 }
311 }
312
313 # output archivedir if at top level
314 if ($section eq $doc_obj->get_top_section()) {
315 print $handle "<archivedir>$archivedir\n";
316 }
317
318 # output document display type
319 if ($first) {
320 print $handle "<thistype>$thistype\n";
321 }
322
323 # output a list of children
324 my $children = $doc_obj->get_children ($section);
325 if (scalar(@$children) > 0) {
326 print $handle "<childtype>$childtype\n";
327 print $handle "<contains>";
328 my $firstchild = 1;
329 foreach my $child (@$children) {
330 print $handle ";" unless $firstchild;
331 $firstchild = 0;
332 if ($child =~ /^.*?\.(\d+)$/) {
333 print $handle "\".$1";
334 } else {
335 print $handle "\".$child";
336 }
337# if ($child eq "") { print $handle "$doc_OID"; }
338# elsif ($section eq "") { print $handle "$doc_OID.$child"; }
339# else { print $handle "$doc_OID.$section.$child"; }
340 }
341 print $handle "\n";
342 }
343
344 # output the matching document number
345 print $handle "<docnum>$self->{'num_sections'}\n";
346
347 print $handle '-' x 70, "\n";
348
349
350 # output a database entry for the document number
351 print $handle "[$self->{'num_sections'}]\n";
352 if ($section eq "") { print $handle "<section>$doc_OID\n"; }
353 else { print $handle "<section>$doc_OID.$section\n"; }
354 print $handle '-' x 70, "\n";
355
356 # output entry for url
357 if ($url ne "") {
358 print $handle $url;
359 }
360
361 $first = 0;
362 $section = $doc_obj->get_next_section($section);
363 }
364
365 # classify this document
366 &classify::classify_doc ($self->{'classifiers'}, $doc_obj);
367
368}
369
370sub find_paragraphs {
371 $_[1] =~ s/(<p\b)/\cC$1/gi;
372}
373
374sub filter_text {
375 # $self->filter_text ($field, $new_text);
376 # don't want to do anything for this version, however,
377 # in a particular collection you might want to override
378 # this method to post-process certain fields depending on
379 # the field, or whether we are outputting it for indexing
380}
381
382sub text {
383 my $self = shift (@_);
384 my ($doc_obj) = @_;
385 my $handle = $self->{'output_handle'};
386 my $indexed_doc = 1;
387
388 # only output this document if it is one to be indexed
389 return if ($doc_obj->get_doc_type() ne "indexed_doc");
390
391 # see if this document belongs to this subcollection
392 foreach my $indexexp (@{$self->{'indexexparr'}}) {
393 $indexed_doc = 0;
394 my ($field, $exp, $options) = split /\//, $indexexp;
395 if (defined ($field) && defined ($exp)) {
396 my ($bool) = $field =~ /^(.)/;
397 $field =~ s/^.// if $bool eq '!';
398 if ($field =~ /^filename$/i) {
399 $field = $doc_obj->get_source_filename();
400 } else {
401 $field = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $field);
402 }
403 next unless defined $field;
404 if ($bool eq '!') {
405 if ($options =~ /^i$/i) {
406 if ($field !~ /$exp/i) {$indexed_doc = 1; last;}
407 } else {
408 if ($field !~ /$exp/) {$indexed_doc = 1; last;}
409 }
410 } else {
411 if ($options =~ /^i$/i) {
412 if ($field =~ /$exp/i) {$indexed_doc = 1; last;}
413 } else {
414 if ($field =~ /$exp/) {$indexed_doc = 1; last;}
415 }
416 }
417 }
418 }
419
420 # this is another document
421 $self->{'num_docs'} += 1;
422
423 # get the parameters for the output
424 my ($level, $fields) = split (/:/, $self->{'index'});
425 $fields =~ s/\ball\b/Title,Creator,text/;
426 $fields =~ s/\btopall\b/topTitle,topCreator,toptext/;
427
428 my $doc_section = 0; # just for this document
429 my $text = "";
430 my $text_extra = "";
431
432 # get the text for this document
433 my $section = $doc_obj->get_top_section();
434 while (defined $section) {
435 # update a few statistics
436 $doc_section++;
437 $self->{'num_sections'} += 1;
438
439 if ($indexed_doc) {
440 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
441 foreach my $field (split (/,/, $fields)) {
442 # only deal with this field if it doesn't start with top or
443 # this is the first section
444 my $real_field = $field;
445 if (!($real_field =~ s/^top//) || ($doc_section == 1)) {
446 my $new_text = "";
447 if ($real_field eq "text") {
448 $new_text = $doc_obj->get_text ($section);
449 $self->{'num_processed_bytes'} += length ($new_text);
450 $new_text =~ s/[\cB\cC]//g;
451 $self->find_paragraphs($new_text);
452
453 } else {
454 my $first = 1;
455 foreach $meta (@{$doc_obj->get_metadata ($section, $real_field)}) {
456 $meta =~ s/[\cB\cC]//g;
457 $self->{'num_processed_bytes'} += length ($meta);
458 $new_text .= "\cC" unless $first;
459 $new_text .= $meta;
460 $first = 0;
461 }
462 }
463
464 # filter the text
465 $self->filter_text ($field, $new_text);
466
467 $text .= "$new_text\cC";
468 }
469 }
470 }
471
472 if ($level eq "document") { $text_extra .= "\cB"; }
473 else { $text .= "\cB"; }
474
475 $section = $doc_obj->get_next_section($section);
476 }
477
478 print $handle "$text$text_extra";
479}
480
4811;
482
Note: See TracBrowser for help on using the repository browser.