source: trunk/gsdl/perllib/mgbuildproc.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:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 13.9 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
32eval {require bytes};
33
34use classify;
35use doc;
36use docproc;
37use util;
38
39BEGIN {
40 @ISA = ('docproc');
41}
42
43
44sub new {
45 my ($class, $collection, $source_dir, $build_dir,
46 $verbosity, $outhandle) = @_;
47 my $self = new docproc ();
48
49 # outhandle is where all the debugging info goes
50 # output_handle is where the output of the plugins is piped
51 # to (i.e. mg, gdbm etc.)
52 $outhandle = STDERR unless defined $outhandle;
53
54 $self->{'collection'} = $collection;
55 $self->{'source_dir'} = $source_dir;
56 $self->{'build_dir'} = $build_dir;
57 $self->{'verbosity'} = $verbosity;
58 $self->{'classifiers'} = [];
59 $self->{'mode'} = "text";
60 $self->{'assocdir'} = $build_dir;
61 $self->{'dontgdbm'} = {};
62 $self->{'index'} = "section:text";
63 $self->{'indexexparr'} = [];
64 $self->{'output_handle'} = "STDOUT";
65 $self->{'num_docs'} = 0;
66 $self->{'num_sections'} = 0;
67 $self->{'num_bytes'} = 0;
68 $self->{'num_processed_bytes'} = 0;
69 $self->{'store_text'} = 1;
70 $self->{'outhandle'} = $outhandle;
71
72 #used by browse interface
73 $self->{'doclist'} = [];
74
75 $self->{'indexing_text'} = 0;
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 mg 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_store_text {
180 my $self = shift (@_);
181 my ($store_text) = @_;
182
183 $self->{'store_text'} = $store_text;
184}
185
186sub get_doc_list {
187 my $self = shift(@_);
188
189 return @{$self->{'doclist'}};
190}
191
192
193sub process {
194 my $self = shift (@_);
195 my $method = $self->{'mode'};
196
197 $self->$method(@_);
198}
199
200# use 'Paged' if document has no more than 2 levels
201# and each section at second level has a number for
202# Title metadata
203#also use Paged if gsdlthistype metadata is set to Paged
204sub get_document_type {
205 my $self = shift (@_);
206 my ($doc_obj) = @_;
207
208 my $thistype = "VList";
209 my $childtype = "VList";
210 my $title;
211 my @tmp = ();
212
213 my $section = $doc_obj->get_top_section ();
214
215 my $gsdlthistype = $doc_obj->get_metadata_element ($section, "gsdlthistype");
216 if (defined $gsdlthistype && $gsdlthistype eq "Paged") {
217 $thistype = "Paged";
218 $childtype = "Paged";
219 return ($thistype, $childtype);
220 }
221
222 my $first = 1;
223 while (defined $section) {
224 @tmp = split /\./, $section;
225 if (scalar(@tmp) > 1) {
226 return ($thistype, $childtype);
227 }
228 if (!$first) {
229 $title = $doc_obj->get_metadata_element ($section, "Title");
230 if (!defined $title || $title !~ /^\d+$/) {
231 return ($thistype, $childtype);
232 }
233 }
234 $first = 0;
235 $section = $doc_obj->get_next_section($section);
236 }
237 if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
238 $thistype = "Paged";
239 } else {
240 $thistype = "Invisible";
241 }
242 $childtype = "Paged";
243 return ($thistype, $childtype);
244}
245
246sub assoc_files {
247 my $self = shift (@_);
248 my ($doc_obj, $archivedir) = @_;
249 my ($afile);
250
251 foreach my $assoc_file (@{$doc_obj->get_assoc_files()}) {
252 # if assoc file starts with a slash, we put it relative to the assoc
253 # dir, otherwise it is relative to the HASH... directory
254 if ($assoc_file->[1] =~ m@^[/\\]@) {
255 $afile = &util::filename_cat($self->{'assocdir'},$assoc_file->[1]);
256 } else {
257 $afile = &util::filename_cat($self->{'assocdir'}, $archivedir, $assoc_file->[1]);
258 }
259 &util::hard_link ($assoc_file->[0], $afile);
260 }
261}
262
263sub infodb {
264 my $self = shift (@_);
265 my ($doc_obj, $filename) = @_;
266 my $handle = $self->{'output_handle'};
267# $handle = "main::STDOUT";
268
269 my $doctype = $doc_obj->get_doc_type();
270
271 # only output this document if it is one to be indexed
272 return if ($doctype ne "indexed_doc");
273
274 my ($archivedir) = $filename =~ /^(.*?)(?:\/|\\)[^\/\\]*$/;
275 $archivedir = "" unless defined $archivedir;
276 $archivedir =~ s/\\/\//g;
277 $archivedir =~ s/^\/+//;
278 $archivedir =~ s/\/+$//;
279
280 $self->assoc_files ($doc_obj, $archivedir);
281
282 # this is another document
283 $self->{'num_docs'} += 1 unless ($doctype eq "classification");
284
285 # is this a paged or a hierarchical document
286 my ($thistype, $childtype) = $self->get_document_type ($doc_obj);
287
288 my $section = $doc_obj->get_top_section ();
289 my $doc_OID = $doc_obj->get_OID();
290 my $first = 1;
291 my $url = "";
292 while (defined $section) {
293 # update a few statistics
294 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
295 $self->{'num_sections'} += 1 unless ($doctype eq "classification");
296
297 # output the section name
298 if ($section eq "") { print $handle "[$doc_OID]\n"; }
299 else { print $handle "[$doc_OID.$section]\n"; }
300
301 # output the fact that this document is a document (unless doctype
302 # has been set to something else from within a plugin
303 my $dtype = $doc_obj->get_metadata_element ($section, "doctype");
304 if (!defined $dtype || $dtype !~ /\w/) {
305 print $handle "<doctype>doc\n";
306 }
307
308 # output whether this node contains text
309 if ($doc_obj->get_text_length($section) > 0) {
310 print $handle "<hastxt>1\n";
311 } else {
312 print $handle "<hastxt>0\n";
313 }
314
315 # output all the section metadata
316 my $metadata = $doc_obj->get_all_metadata ($section);
317 foreach my $pair (@$metadata) {
318 my ($field, $value) = (@$pair);
319
320 if ($field ne "Identifier" && $field !~ /^gsdl/ &&
321 defined $value && $value ne "") {
322
323 # escape problematic stuff
324 $value =~ s/\\/\\\\/g;
325 $value =~ s/\n/\\n/g;
326 $value =~ s/\r/\\r/g;
327 if ($value =~ /-{70,}/) {
328 # if value contains 70 or more hyphens in a row we need
329 # to escape them to prevent txt2db from treating them
330 # as a separator
331 $value =~ s/-/&\#045;/gi;
332 }
333
334 # special case for URL metadata
335 if ($field =~ /^URL$/i) {
336 $url .= "[$value]\n";
337 if ($section eq "") {$url .= "<section>$doc_OID\n";}
338 else {$url .= "<section>$doc_OID.$section\n";}
339 $url .= '-' x 70 . "\n";
340 }
341
342 if (!defined $self->{'dontgdbm'}->{$field}) {
343 print $handle "<$field>$value\n";
344 }
345 }
346 }
347
348 # output archivedir if at top level
349 if ($section eq $doc_obj->get_top_section()) {
350 print $handle "<archivedir>$archivedir\n";
351 }
352
353 # output document display type
354 if ($first) {
355 print $handle "<thistype>$thistype\n";
356 }
357
358 # output a list of children
359 my $children = $doc_obj->get_children ($section);
360 if (scalar(@$children) > 0) {
361 print $handle "<childtype>$childtype\n";
362 print $handle "<contains>";
363 my $firstchild = 1;
364 foreach my $child (@$children) {
365 print $handle ";" unless $firstchild;
366 $firstchild = 0;
367 if ($child =~ /^.*?\.(\d+)$/) {
368 print $handle "\".$1";
369 } else {
370 print $handle "\".$child";
371 }
372# if ($child eq "") { print $handle "$doc_OID"; }
373# elsif ($section eq "") { print $handle "$doc_OID.$child"; }
374# else { print $handle "$doc_OID.$section.$child"; }
375 }
376 print $handle "\n";
377 }
378
379 # output the matching document number
380 print $handle "<docnum>$self->{'num_sections'}\n";
381
382 print $handle '-' x 70, "\n";
383
384
385 # output a database entry for the document number
386 print $handle "[$self->{'num_sections'}]\n";
387 if ($section eq "") { print $handle "<section>$doc_OID\n"; }
388 else { print $handle "<section>$doc_OID.$section\n"; }
389 print $handle '-' x 70, "\n";
390
391 # output entry for url
392 if ($url ne "") {
393 print $handle $url;
394 }
395
396 $first = 0;
397 $section = $doc_obj->get_next_section($section);
398 }
399
400 #add this document to the browse structure
401 push(@{$self->{'doclist'}},$doc_obj->get_OID())
402 unless ($doctype eq "classification");
403
404 # classify this document
405 &classify::classify_doc ($self->{'classifiers'}, $doc_obj);
406
407}
408
409sub find_paragraphs {
410 $_[1] =~ s/(<p\b)/\cC$1/gi;
411}
412
413sub filter_text {
414 # $self->filter_text ($field, $new_text);
415 # don't want to do anything for this version, however,
416 # in a particular collection you might want to override
417 # this method to post-process certain fields depending on
418 # the field, or whether we are outputting it for indexing
419}
420
421sub text {
422 my $self = shift (@_);
423 my ($doc_obj) = @_;
424 my $handle = $self->{'output_handle'};
425 my $indexed_doc = 1;
426
427 # only output this document if it is one to be indexed
428 return if ($doc_obj->get_doc_type() ne "indexed_doc");
429
430 # see if this document belongs to this subcollection
431 foreach my $indexexp (@{$self->{'indexexparr'}}) {
432 $indexed_doc = 0;
433 my ($field, $exp, $options) = split /\//, $indexexp;
434 if (defined ($field) && defined ($exp)) {
435 my ($bool) = $field =~ /^(.)/;
436 $field =~ s/^.// if $bool eq '!';
437 if ($field =~ /^filename$/i) {
438 $field = $doc_obj->get_source_filename();
439 } else {
440 $field = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $field);
441 }
442 next unless defined $field;
443 if ($bool eq '!') {
444 if ($options =~ /^i$/i) {
445 if ($field !~ /$exp/i) {$indexed_doc = 1; last;}
446 } else {
447 if ($field !~ /$exp/) {$indexed_doc = 1; last;}
448 }
449 } else {
450 if ($options =~ /^i$/i) {
451 if ($field =~ /$exp/i) {$indexed_doc = 1; last;}
452 } else {
453 if ($field =~ /$exp/) {$indexed_doc = 1; last;}
454 }
455 }
456 }
457 }
458
459 # this is another document
460 $self->{'num_docs'} += 1;
461
462 # get the parameters for the output
463 my ($level, $fields) = split (/:/, $self->{'index'});
464 $fields =~ s/\ball\b/Title,Creator,text/;
465 $fields =~ s/\btopall\b/topTitle,topCreator,toptext/;
466
467 my $doc_section = 0; # just for this document
468 my $text = "";
469 my $text_extra = "";
470
471 # get the text for this document
472 my $section = $doc_obj->get_top_section();
473 while (defined $section) {
474 # update a few statistics
475 $doc_section++;
476 $self->{'num_sections'} += 1;
477
478 if ($indexed_doc) {
479 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
480 foreach my $field (split (/,/, $fields)) {
481 # only deal with this field if it doesn't start with top or
482 # this is the first section
483 my $real_field = $field;
484 if (!($real_field =~ s/^top//) || ($doc_section == 1)) {
485 my $new_text = "";
486 if ($level eq "dummy") {
487 # a dummy index is a special case used when no
488 # indexes are specified (since there must always be
489 # at least one index or we can't retrieve the
490 # compressed text) - we add a small amount of text
491 # to these dummy indexes which will never be seen
492 # but will overcome mg's problems with building
493 # empty indexes
494 $new_text = "this is dummy text to stop mg barfing";
495 $self->{'num_processed_bytes'} += length ($new_text);
496
497 } elsif ($real_field eq "text") {
498 $new_text = $doc_obj->get_text ($section) if $self->{'store_text'};
499 $self->{'num_processed_bytes'} += length ($new_text);
500 $new_text =~ s/[\cB\cC]//g;
501 $self->find_paragraphs($new_text);
502
503 } else {
504 my $first = 1;
505 foreach $meta (@{$doc_obj->get_metadata ($section, $real_field)}) {
506 $meta =~ s/[\cB\cC]//g;
507 $self->{'num_processed_bytes'} += length ($meta);
508 $new_text .= "\cC" unless $first;
509 $new_text .= $meta if $self->{'store_text'};
510 $first = 0;
511 }
512 }
513
514 # filter the text
515 $self->filter_text ($field, $new_text);
516
517 $text .= "$new_text\cC";
518 }
519 }
520 }
521
522 if ($level eq "document") { $text_extra .= "\cB"; }
523 else { $text .= "\cB"; }
524
525 $section = $doc_obj->get_next_section($section);
526 }
527
528 print $handle "$text$text_extra";
529}
530
5311;
532
Note: See TracBrowser for help on using the repository browser.