source: trunk/gsdl/perllib/mgbuildproc.pm@ 780

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

added dontgdbm configuration option

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 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
37
38BEGIN {
39 @ISA = ('docproc');
40}
41
42
43sub new {
44 my ($class, $collection, $source_dir, $build_dir, $verbosity) = @_;
45 my $self = new docproc ();
46
47 $self->{'collection'} = $collection;
48 $self->{'source_dir'} = $source_dir;
49 $self->{'build_dir'} = $build_dir;
50 $self->{'verbosity'} = $verbosity;
51 $self->{'classifiers'} = [];
52 $self->{'mode'} = "text";
53 $self->{'dontgdbm'} = {};
54 $self->{'index'} = "section:text";
55 $self->{'indexexparr'} = [];
56 $self->{'output_handle'} = "STDOUT";
57 $self->{'num_docs'} = 0;
58 $self->{'num_sections'} = 0;
59 $self->{'num_bytes'} = 0;
60
61 $self->{'indexing_text'} = 0;
62
63 return bless $self, $class;
64}
65
66sub reset {
67 my $self = shift (@_);
68
69 $self->{'num_docs'} = 0;
70 $self->{'num_sections'} = 0;
71 $self->{'num_bytes'} = 0;
72}
73
74sub get_num_docs {
75 my $self = shift (@_);
76
77 return $self->{'num_docs'};
78}
79
80sub get_num_sections {
81 my $self = shift (@_);
82
83 return $self->{'num_sections'};
84}
85
86sub get_num_bytes {
87 my $self = shift (@_);
88
89 return $self->{'num_bytes'};
90}
91
92sub set_output_handle {
93 my $self = shift (@_);
94 my ($handle) = @_;
95
96 $self->{'output_handle'} = $handle;
97}
98
99sub set_mode {
100 my $self = shift (@_);
101 my ($mode) = @_;
102
103 $self->{'mode'} = $mode;
104}
105
106sub set_dontgdbm {
107 my $self = shift (@_);
108 my ($dontgdbm) = @_;
109
110 $self->{'dontgdbm'} = $dontgdbm;
111}
112
113sub set_index {
114 my $self = shift (@_);
115 my ($index, $indexexparr) = @_;
116
117 $self->{'index'} = $index;
118 $self->{'indexexparr'} = $indexexparr if defined $indexexparr;
119}
120
121sub set_classifiers {
122 my $self = shift (@_);
123 my ($classifiers) = @_;
124
125 $self->{'classifiers'} = $classifiers;
126}
127
128sub set_indexing_text {
129 my $self = shift (@_);
130 my ($indexing_text) = @_;
131
132 $self->{'indexing_text'} = $indexing_text;
133}
134
135sub process {
136 my $self = shift (@_);
137 my $method = $self->{'mode'};
138
139 $self->$method(@_);
140}
141
142# use 'Paged' if document has no more than 2 levels
143# and each section at second level has a number for
144# Title metadata
145sub get_document_type {
146 my $self = shift (@_);
147 my ($doc_obj) = @_;
148
149 my $thistype = "VList";
150 my $childtype = "VList";
151 my $title;
152 my @tmp = ();
153
154 my $section = $doc_obj->get_top_section ();
155 my $first = 1;
156 while (defined $section) {
157 @tmp = split /\./, $section;
158 if (scalar(@tmp) > 1) {
159 return ($thistype, $childtype);
160 }
161 if (!$first) {
162 $title = $doc_obj->get_metadata_element ($section, "Title");
163 if (!defined $title || $title !~ /^\d+$/) {
164 return ($thistype, $childtype);
165 }
166 }
167 $first = 0;
168 $section = $doc_obj->get_next_section($section);
169 }
170 if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
171 $thistype = "Paged";
172 } else {
173 $thistype = "Invisible";
174 }
175 $childtype = "Paged";
176 return ($thistype, $childtype);
177}
178
179sub infodb {
180 my $self = shift (@_);
181 my ($doc_obj, $filename) = @_;
182 my $handle = $self->{'output_handle'};
183# $handle = "main::STDOUT";
184
185 my $doctype = $doc_obj->get_doc_type();
186
187 # only output this document if it is one to be indexed
188 return if ($doctype ne "indexed_doc");
189
190 # this is another document
191 $self->{'num_docs'} += 1 unless ($doctype eq "classification");
192
193 # is this a paged or a hierarchical document
194 my ($thistype, $childtype) = $self->get_document_type ($doc_obj);
195
196 my $section = $doc_obj->get_top_section ();
197 my $doc_OID = $doc_obj->get_OID();
198 my $first = 1;
199 while (defined $section) {
200 # update a few statistics
201 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
202 $self->{'num_sections'} += 1 unless ($doctype eq "classification");
203
204 # output the section name
205 if ($section eq "") { print $handle "[$doc_OID]\n"; }
206 else { print $handle "[$doc_OID.$section]\n"; }
207
208 # output the fact that this document is a document
209 print $handle "<doctype>doc\n";
210
211 # output whether this node contains text
212 if ($doc_obj->get_text_length($section) > 0) {
213 print $handle "<hastxt>1\n";
214 } else {
215 print $handle "<hastxt>0\n";
216 }
217
218 # output all the section metadata
219 my $metadata = $doc_obj->get_all_metadata ($section);
220 foreach $pair (@$metadata) {
221 my ($field, $value) = (@$pair);
222
223 if ($field ne "Identifier" && $field !~ /^gsdl/ &&
224 defined $value && $value ne "" &&
225 !defined $self->{'dontgdbm'}->{$field}) {
226 # escape problematic stuff
227 $value =~ s/\\/\\\\/g;
228 $value =~ s/\n/\\n/g;
229 $value =~ s/\r/\\r/g;
230
231 print $handle "<$field>$value\n";
232 }
233 }
234
235 # output archivedir if at top level
236 if ($section eq $doc_obj->get_top_section()) {
237 my ($archivedir) = $filename =~ /^(.*?)(?:\/|\\)[^\/\\]*$/;
238 $archivedir = "" unless defined $archivedir;
239 $archivedir =~ s/\\/\//g;
240 $archivedir =~ s/^\/+//;
241 $archivedir =~ s/\/+$//;
242 print $handle "<archivedir>$archivedir\n";
243 }
244
245 # output document display type
246 if ($first) {
247 print $handle "<thistype>$thistype\n";
248 }
249
250 # output a list of children
251 my $children = $doc_obj->get_children ($section);
252 if (scalar(@$children) > 0) {
253 print $handle "<childtype>$childtype\n";
254 print $handle "<contains>";
255 my $firstchild = 1;
256 foreach $child (@$children) {
257 print $handle ";" unless $firstchild;
258 $firstchild = 0;
259 if ($child =~ /^.*?\.(\d+)$/) {
260 print $handle "\".$1";
261 } else {
262 print $handle "\".$child";
263 }
264# if ($child eq "") { print $handle "$doc_OID"; }
265# elsif ($section eq "") { print $handle "$doc_OID.$child"; }
266# else { print $handle "$doc_OID.$section.$child"; }
267 }
268 print $handle "\n";
269 }
270
271 # output the matching document number
272 print $handle "<docnum>$self->{'num_sections'}\n";
273
274 print $handle '-' x 70, "\n";
275
276
277 # output a database entry for the document number
278 print $handle "[$self->{'num_sections'}]\n";
279 if ($section eq "") { print $handle "<section>$doc_OID\n"; }
280 else { print $handle "<section>$doc_OID.$section\n"; }
281 print $handle '-' x 70, "\n";
282
283 $first = 0;
284 $section = $doc_obj->get_next_section($section);
285 }
286
287 # classify this document
288 &classify::classify_doc ($self->{'classifiers'}, $doc_obj);
289
290}
291
292sub find_paragraphs {
293 $_[1] =~ s/(<p\b)/\cC$1/gi;
294}
295
296sub filter_text {
297 # $self->filter_text ($field, $new_text);
298 # don't want to do anything for this version, however,
299 # in a particular collection you might want to override
300 # this method to post-process certain fields depending on
301 # the field, or whether we are outputting it for indexing
302}
303
304sub text {
305 my $self = shift (@_);
306 my ($doc_obj) = @_;
307 my $handle = $self->{'output_handle'};
308 my $indexed_doc = 1;
309
310 # only output this document if it is one to be indexed
311 return if ($doc_obj->get_doc_type() ne "indexed_doc");
312
313 # see if this document belongs to this subcollection
314 foreach $indexexp (@{$self->{'indexexparr'}}) {
315 $indexed_doc = 0;
316 my ($field, $exp, $options) = split /\//, $indexexp;
317 if (defined ($field) && defined ($exp)) {
318 my ($bool) = $field =~ /^(.)/;
319 $field =~ s/^.// if $bool eq '!';
320 if ($field =~ /^filename$/i) {
321 $field = $doc_obj->get_source_filename();
322 } else {
323 $field = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $field);
324 }
325 next unless defined $field;
326 if ($bool eq '!') {
327 if ($options =~ /^i$/i) {
328 if ($field !~ /$exp/i) {$indexed_doc = 1; last;}
329 } else {
330 if ($field !~ /$exp/) {$indexed_doc = 1; last;}
331 }
332 } else {
333 if ($options =~ /^i$/i) {
334 if ($field =~ /$exp/i) {$indexed_doc = 1; last;}
335 } else {
336 if ($field =~ /$exp/) {$indexed_doc = 1; last;}
337 }
338 }
339 }
340 }
341
342 # this is another document
343 $self->{'num_docs'} += 1;
344
345 # get the parameters for the output
346 my ($level, $fields) = split (/:/, $self->{'index'});
347 $fields =~ s/\ball\b/Title,Creator,text/;
348 $fields =~ s/\btopall\b/topTitle,topCreator,toptext/;
349
350 my $doc_section = 0; # just for this document
351 my $text = "";
352 my $text_extra = "";
353
354 # get the text for this document
355 my $section = $doc_obj->get_top_section();
356 while (defined $section) {
357 # update a few statistics
358 $doc_section++;
359 $self->{'num_sections'} += 1;
360
361 if ($indexed_doc) {
362 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
363 foreach $field (split (/,/, $fields)) {
364 # only deal with this field if it doesn't start with top or
365 # this is the first section
366 my $real_field = $field;
367 if (!($real_field =~ s/^top//) || ($doc_section == 1)) {
368 my $new_text = "";
369 if ($real_field eq "text") {
370 $new_text = $doc_obj->get_text ($section);
371 $new_text =~ s/[\cB\cC]//g;
372 $self->find_paragraphs($new_text);
373
374 } else {
375 $new_text = join ("\cC", @{$doc_obj->get_metadata ($section, $real_field)});
376 }
377
378 # filter the text
379 $self->filter_text ($field, $new_text);
380
381 if ($self->{'indexing_text'} &&
382 $new_text =~ /[\(\)\{\}]/) {
383 }
384
385 $text .= "$new_text\cC";
386 }
387 }
388 }
389
390 if ($level eq "document") { $text_extra .= "\cB"; }
391 else { $text .= "\cB"; }
392
393 $section = $doc_obj->get_next_section($section);
394 }
395
396 print $handle "$text$text_extra";
397}
398
3991;
400
Note: See TracBrowser for help on using the repository browser.