source: main/tags/2.60/gsdl/perllib/mgbuildproc.pm@ 32313

Last change on this file since 32313 was 9669, checked in by kjdon, 19 years ago

fixed up the case where you have subcollection partitions and language partitions - was doing an OR on both, but really needed an OR for partitions ANDed with an OR for langs

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