source: main/tags/2.52/gsdl/perllib/mgbuildproc.pm@ 25422

Last change on this file since 25422 was 8220, checked in by cs025, 20 years ago

Extensions to underpin OAI - e.g. creation of the OAI classifier, adding
modified file dates and ensuring that documents know the parent classifiers
to which they belong.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 14.2 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) {
217 if ($gsdlthistype eq "Paged") {
218 $thistype = "Paged";
219 $childtype = "Paged";
220 return ($thistype, $childtype);
221 } elsif ($gsdlthistype eq "Hierarchy") {
222 return ($thistype, $childtype); # use VList, VList
223 }
224 }
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 starts with a slash, we put it relative to the assoc
256 # dir, otherwise it is relative to the HASH... directory
257 if ($assoc_file->[1] =~ m@^[/\\]@) {
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# $handle = "main::STDOUT";
271
272 my $doctype = $doc_obj->get_doc_type();
273
274 # only output this document if it is one to be indexed
275 return if ($doctype ne "indexed_doc");
276
277 my ($archivedir) = $filename =~ /^(.*?)(?:\/|\\)[^\/\\]*$/;
278 $archivedir = "" unless defined $archivedir;
279 $archivedir =~ s/\\/\//g;
280 $archivedir =~ s/^\/+//;
281 $archivedir =~ s/\/+$//;
282
283 # resolve the final filenames of the files associated with this document
284 $self->assoc_files ($doc_obj, $archivedir);
285
286 #GRB: moved 1/06/2004 from GRB01062004
287 #add this document to the browse structure
288 push(@{$self->{'doclist'}},$doc_obj->get_OID())
289 unless ($doctype eq "classification");
290
291 # classify this document
292 &classify::classify_doc ($self->{'classifiers'}, $doc_obj);
293 #GRB: end of moved block
294
295 # this is another document
296 $self->{'num_docs'} += 1 unless ($doctype eq "classification");
297
298 # is this a paged or a hierarchical document
299 my ($thistype, $childtype) = $self->get_document_type ($doc_obj);
300
301 my $section = $doc_obj->get_top_section ();
302 my $doc_OID = $doc_obj->get_OID();
303 my $first = 1;
304 my $url = "";
305 while (defined $section) {
306 # update a few statistics
307 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
308 $self->{'num_sections'} += 1 unless ($doctype eq "classification");
309
310 # output the section name
311 if ($section eq "") { print $handle "[$doc_OID]\n"; }
312 else { print $handle "[$doc_OID.$section]\n"; }
313
314 # output the fact that this document is a document (unless doctype
315 # has been set to something else from within a plugin
316 my $dtype = $doc_obj->get_metadata_element ($section, "doctype");
317 if (!defined $dtype || $dtype !~ /\w/) {
318 print $handle "<doctype>doc\n";
319 }
320
321 # output whether this node contains text
322 if ($doc_obj->get_text_length($section) > 0) {
323 print $handle "<hastxt>1\n";
324 } else {
325 print $handle "<hastxt>0\n";
326 }
327
328 # output all the section metadata
329 my $metadata = $doc_obj->get_all_metadata ($section);
330 foreach my $pair (@$metadata) {
331 my ($field, $value) = (@$pair);
332
333 if ($field ne "Identifier" && $field !~ /^gsdl/ &&
334 defined $value && $value ne "") {
335
336 # escape problematic stuff
337 $value =~ s/\\/\\\\/g;
338 $value =~ s/\n/\\n/g;
339 $value =~ s/\r/\\r/g;
340 if ($value =~ /-{70,}/) {
341 # if value contains 70 or more hyphens in a row we need
342 # to escape them to prevent txt2db from treating them
343 # as a separator
344 $value =~ s/-/&\#045;/gi;
345 }
346
347 # special case for URL metadata
348 if ($field =~ /^URL$/i) {
349 $url .= "[$value]\n";
350 if ($section eq "") {$url .= "<section>$doc_OID\n";}
351 else {$url .= "<section>$doc_OID.$section\n";}
352 $url .= '-' x 70 . "\n";
353 }
354
355 if (!defined $self->{'dontgdbm'}->{$field}) {
356 print $handle "<$field>$value\n";
357 }
358 }
359 }
360
361 # output archivedir if at top level
362 if ($section eq $doc_obj->get_top_section()) {
363 print $handle "<archivedir>$archivedir\n";
364 }
365
366 # output document display type
367 if ($first) {
368 print $handle "<thistype>$thistype\n";
369 }
370
371 # output a list of children
372 my $children = $doc_obj->get_children ($section);
373 if (scalar(@$children) > 0) {
374 print $handle "<childtype>$childtype\n";
375 print $handle "<contains>";
376 my $firstchild = 1;
377 foreach my $child (@$children) {
378 print $handle ";" unless $firstchild;
379 $firstchild = 0;
380 if ($child =~ /^.*?\.(\d+)$/) {
381 print $handle "\".$1";
382 } else {
383 print $handle "\".$child";
384 }
385# if ($child eq "") { print $handle "$doc_OID"; }
386# elsif ($section eq "") { print $handle "$doc_OID.$child"; }
387# else { print $handle "$doc_OID.$section.$child"; }
388 }
389 print $handle "\n";
390 }
391
392 # output the matching document number
393 print $handle "<docnum>$self->{'num_sections'}\n";
394
395 print $handle '-' x 70, "\n";
396
397
398 # output a database entry for the document number
399 print $handle "[$self->{'num_sections'}]\n";
400 if ($section eq "") { print $handle "<section>$doc_OID\n"; }
401 else { print $handle "<section>$doc_OID.$section\n"; }
402 print $handle '-' x 70, "\n";
403
404 # output entry for url
405 if ($url ne "") {
406 print $handle $url;
407 }
408
409 $first = 0;
410 $section = $doc_obj->get_next_section($section);
411 }
412
413 #GRB01062004: see code above moved from here
414}
415
416sub find_paragraphs {
417 $_[1] =~ s/(<p\b)/\cC$1/gi;
418}
419
420sub filter_text {
421 # $self->filter_text ($field, $new_text);
422 # don't want to do anything for this version, however,
423 # in a particular collection you might want to override
424 # this method to post-process certain fields depending on
425 # the field, or whether we are outputting it for indexing
426}
427
428sub text {
429 my $self = shift (@_);
430 my ($doc_obj) = @_;
431 my $handle = $self->{'output_handle'};
432 my $indexed_doc = 1;
433
434 # only output this document if it is one to be indexed
435 return if ($doc_obj->get_doc_type() ne "indexed_doc");
436
437 # see if this document belongs to this subcollection
438 foreach my $indexexp (@{$self->{'indexexparr'}}) {
439 $indexed_doc = 0;
440 my ($field, $exp, $options) = split /\//, $indexexp;
441 if (defined ($field) && defined ($exp)) {
442 my ($bool) = $field =~ /^(.)/;
443 $field =~ s/^.// if $bool eq '!';
444 if ($field =~ /^filename$/i) {
445 $field = $doc_obj->get_source_filename();
446 } else {
447 $field = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $field);
448 }
449 next unless defined $field;
450 if ($bool eq '!') {
451 if ($options =~ /^i$/i) {
452 if ($field !~ /$exp/i) {$indexed_doc = 1; last;}
453 } else {
454 if ($field !~ /$exp/) {$indexed_doc = 1; last;}
455 }
456 } else {
457 if ($options =~ /^i$/i) {
458 if ($field =~ /$exp/i) {$indexed_doc = 1; last;}
459 } else {
460 if ($field =~ /$exp/) {$indexed_doc = 1; last;}
461 }
462 }
463 }
464 }
465
466 # this is another document
467 $self->{'num_docs'} += 1;
468
469 # get the parameters for the output
470 my ($level, $fields) = split (/:/, $self->{'index'});
471 $fields =~ s/\ball\b/Title,Creator,text/;
472 $fields =~ s/\btopall\b/topTitle,topCreator,toptext/;
473
474 my $doc_section = 0; # just for this document
475 my $text = "";
476 my $text_extra = "";
477
478 # get the text for this document
479 my $section = $doc_obj->get_top_section();
480 while (defined $section) {
481 # update a few statistics
482 $doc_section++;
483 $self->{'num_sections'} += 1;
484
485 if ($indexed_doc) {
486 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
487 foreach my $field (split (/,/, $fields)) {
488 # only deal with this field if it doesn't start with top or
489 # this is the first section
490 my $real_field = $field;
491 if (!($real_field =~ s/^top//) || ($doc_section == 1)) {
492 my $new_text = "";
493 if ($level eq "dummy") {
494 # a dummy index is a special case used when no
495 # indexes are specified (since there must always be
496 # at least one index or we can't retrieve the
497 # compressed text) - we add a small amount of text
498 # to these dummy indexes which will never be seen
499 # but will overcome mg's problems with building
500 # empty indexes
501 $new_text = "this is dummy text to stop mg barfing";
502 $self->{'num_processed_bytes'} += length ($new_text);
503
504 } elsif ($real_field eq "text") {
505 $new_text = $doc_obj->get_text ($section) if $self->{'store_text'};
506 $self->{'num_processed_bytes'} += length ($new_text);
507 $new_text =~ s/[\cB\cC]//g;
508 $self->find_paragraphs($new_text);
509
510 } else {
511 my $first = 1;
512 foreach $meta (@{$doc_obj->get_metadata ($section, $real_field)}) {
513 $meta =~ s/[\cB\cC]//g;
514 $self->{'num_processed_bytes'} += length ($meta);
515 $new_text .= "\cC" unless $first;
516 $new_text .= $meta if $self->{'store_text'};
517 $first = 0;
518 }
519 }
520
521 # filter the text
522 $self->filter_text ($field, $new_text);
523
524 $text .= "$new_text\cC";
525 }
526 }
527 }
528
529 if ($level eq "document") { $text_extra .= "\cB"; }
530 else { $text .= "\cB"; }
531
532 $section = $doc_obj->get_next_section($section);
533 }
534
535 print $handle "$text$text_extra";
536}
537
5381;
539
Note: See TracBrowser for help on using the repository browser.