source: trunk/gsdl/perllib/basebuildproc.pm@ 10271

Last change on this file since 10271 was 10251, checked in by kjdon, 19 years ago

removed a print statement

  • Property svn:keywords set to Author Date Id Revision
File size: 15.1 KB
Line 
1###########################################################################
2#
3# basebuildproc.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 for indexing (should be
27# implemented by subclass) and storing in gdbm database
28
29package basebuildproc;
30
31eval {require bytes};
32
33use classify;
34use doc;
35use docproc;
36use util;
37
38BEGIN {
39 @basebuildproc::ISA = ('docproc');
40}
41
42sub new {
43 my ($class, $collection, $source_dir, $build_dir, $keepold,
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->{'keepold'} = $keepold;
56 $self->{'verbosity'} = $verbosity;
57 $self->{'outhandle'} = $outhandle;
58
59 $self->{'classifiers'} = [];
60 $self->{'mode'} = "text";
61 $self->{'assocdir'} = $build_dir;
62 $self->{'dontgdbm'} = {};
63
64 $self->{'index'} = "section:text";
65 $self->{'indexexparr'} = [];
66
67 my $found_num_data = 0;
68 my $buildconfigfile = undef;
69
70 if ($keepold) {
71 # For incremental building need to seed num_docs etc from values
72 # stored in build.cfg (if present)
73
74 $buildconfigfile = &util::filename_cat($build_dir, "build.cfg");
75
76 if (-e $buildconfigfile) {
77 $found_num_data = 1;
78 }
79 else {
80 # try the index dir
81 $buildconfigfile = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},
82 "index", "build.cfg");
83 if (-e $buildconfigfile) {
84 $found_num_data = 1;
85 }
86 }
87
88 }
89
90 if ($found_num_data) {
91 my $buildcfg = &colcfg::read_build_cfg($buildconfigfile);
92
93 $self->{'starting_num_docs'} = $buildcfg->{'numdocs'};
94 $self->{'starting_num_sections'} = $buildcfg->{'numsections'};
95 $self->{'starting_num_bytes'} = $buildcfg->{'numbytes'};
96 }
97 else {
98 $self->{'starting_num_docs'} = 0;
99 $self->{'starting_num_sections'} = 0;
100 $self->{'starting_num_bytes'} = 0;
101 }
102
103 $self->{'output_handle'} = "STDOUT";
104 $self->{'num_docs'} = $self->{'starting_num_docs'};
105 $self->{'num_sections'} = $self->{'starting_num_sections'};
106 $self->{'num_bytes'} = $self->{'starting_num_bytes'};
107
108 $self->{'num_processed_bytes'} = 0;
109 $self->{'store_text'} = 1;
110
111 # what level (section/document) the gdbm database - indexer intersection is
112 $self->{'gdbm_level'} = "section";
113 #used by browse interface
114 $self->{'doclist'} = [];
115
116 $self->{'indexing_text'} = 0;
117
118 return bless $self, $class;
119
120}
121
122sub reset {
123 my $self = shift (@_);
124
125 $self->{'num_docs'} = $self->{'starting_num_docs'};
126 $self->{'num_sections'} = $self->{'starting_num_sections'};
127 $self->{'num_bytes'} = $self->{'starting_num_bytes'};
128
129 $self->{'num_processed_bytes'} = 0;
130}
131
132sub zero_reset {
133 my $self = shift (@_);
134
135 $self->{'num_docs'} = 0;
136 $self->{'num_sections'} = 0;
137 $self->{'num_bytes'} = 0;
138
139 $self->{'num_processed_bytes'} = 0;
140}
141
142sub get_num_docs {
143 my $self = shift (@_);
144
145 return $self->{'num_docs'};
146}
147
148sub get_num_sections {
149 my $self = shift (@_);
150
151 return $self->{'num_sections'};
152}
153
154# num_bytes is the actual number of bytes in the collection
155# this is normally the same as what's processed during text compression
156sub get_num_bytes {
157 my $self = shift (@_);
158
159 return $self->{'num_bytes'};
160}
161
162# num_processed_bytes is the number of bytes actually passed
163# to mg for the current index
164sub get_num_processed_bytes {
165 my $self = shift (@_);
166
167 return $self->{'num_processed_bytes'};
168}
169
170sub set_output_handle {
171 my $self = shift (@_);
172 my ($handle) = @_;
173
174 $self->{'output_handle'} = $handle;
175}
176
177
178sub set_mode {
179 my $self = shift (@_);
180 my ($mode) = @_;
181
182 $self->{'mode'} = $mode;
183}
184
185sub get_mode {
186 my $self = shift (@_);
187
188 return $self->{'mode'};
189}
190
191sub set_assocdir {
192 my $self = shift (@_);
193 my ($assocdir) = @_;
194
195 $self->{'assocdir'} = $assocdir;
196}
197
198sub set_dontgdbm {
199 my $self = shift (@_);
200 my ($dontgdbm) = @_;
201
202 $self->{'dontgdbm'} = $dontgdbm;
203}
204
205sub set_index {
206 my $self = shift (@_);
207 my ($index, $indexexparr) = @_;
208
209 $self->{'index'} = $index;
210 $self->{'indexexparr'} = $indexexparr if defined $indexexparr;
211}
212
213sub set_index_languages {
214 my $self = shift (@_);
215 my ($lang_meta, $langarr) = @_;
216 $self->{'lang_meta'} = $lang_meta;
217 $self->{'langarr'} = $langarr;
218}
219
220sub get_index {
221 my $self = shift (@_);
222
223 return $self->{'index'};
224}
225
226sub set_classifiers {
227 my $self = shift (@_);
228 my ($classifiers) = @_;
229
230 $self->{'classifiers'} = $classifiers;
231}
232
233sub set_indexing_text {
234 my $self = shift (@_);
235 my ($indexing_text) = @_;
236
237 $self->{'indexing_text'} = $indexing_text;
238}
239
240sub get_indexing_text {
241 my $self = shift (@_);
242
243 return $self->{'indexing_text'};
244}
245
246sub set_store_text {
247 my $self = shift (@_);
248 my ($store_text) = @_;
249
250 $self->{'store_text'} = $store_text;
251}
252sub get_doc_list {
253 my $self = shift(@_);
254
255 return @{$self->{'doclist'}};
256}
257
258# the standard gdbm level is section, but you may want to change it to document
259sub set_gdbm_level {
260 my $self= shift (@_);
261 my ($gdbm_level) = @_;
262
263 $self->{'gdbm_level'} = $gdbm_level;
264}
265
266sub process {
267 my $self = shift (@_);
268 my $method = $self->{'mode'};
269
270 $self->$method(@_);
271}
272
273sub infodb {
274 my $self = shift (@_);
275 my ($doc_obj, $filename) = @_;
276 my $handle = $self->{'output_handle'};
277
278 my $doctype = $doc_obj->get_doc_type();
279
280 # only output this document if it is one to be indexed
281 return if ($doctype ne "indexed_doc");
282
283 my ($archivedir) = $filename =~ /^(.*?)(?:\/|\\)[^\/\\]*$/;
284 $archivedir = "" unless defined $archivedir;
285 $archivedir =~ s/\\/\//g;
286 $archivedir =~ s/^\/+//;
287 $archivedir =~ s/\/+$//;
288
289 # resolve the final filenames of the files associated with this document
290 $self->assoc_files ($doc_obj, $archivedir);
291
292 #GRB: moved 1/06/2004 from GRB01062004
293 #add this document to the browse structure
294 push(@{$self->{'doclist'}},$doc_obj->get_OID())
295 unless ($doctype eq "classification");
296
297 # classify this document
298 &classify::classify_doc ($self->{'classifiers'}, $doc_obj);
299 #GRB: end of moved block
300
301 # this is another document
302 $self->{'num_docs'} += 1 unless ($doctype eq "classification");
303
304 # is this a paged or a hierarchical document
305 my ($thistype, $childtype) = $self->get_document_type ($doc_obj);
306
307 my $section = $doc_obj->get_top_section ();
308 my $doc_OID = $doc_obj->get_OID();
309 my $first = 1;
310 my $url = "";
311 while (defined $section) {
312 # update a few statistics
313 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
314 $self->{'num_sections'} += 1 unless ($doctype eq "classification");
315
316 # output the section name
317 if ($section eq "") { print $handle "[$doc_OID]\n"; }
318 else { print $handle "[$doc_OID.$section]\n"; }
319
320 # output the fact that this document is a document (unless doctype
321 # has been set to something else from within a plugin
322 my $dtype = $doc_obj->get_metadata_element ($section, "doctype");
323 if (!defined $dtype || $dtype !~ /\w/) {
324 print $handle "<doctype>doc\n";
325 }
326
327 # output whether this node contains text
328 if ($doc_obj->get_text_length($section) > 0) {
329 print $handle "<hastxt>1\n";
330 } else {
331 print $handle "<hastxt>0\n";
332 }
333
334 # output all the section metadata
335 my $metadata = $doc_obj->get_all_metadata ($section);
336 foreach my $pair (@$metadata) {
337 my ($field, $value) = (@$pair);
338
339 if ($field ne "Identifier" && $field !~ /^gsdl/ &&
340 defined $value && $value ne "") {
341
342 # escape problematic stuff
343 $value =~ s/\\/\\\\/g;
344 $value =~ s/\n/\\n/g;
345 $value =~ s/\r/\\r/g;
346 if ($value =~ /-{70,}/) {
347 # if value contains 70 or more hyphens in a row we need
348 # to escape them to prevent txt2db from treating them
349 # as a separator
350 $value =~ s/-/&\#045;/gi;
351 }
352
353 # special case for URL metadata
354 if ($field =~ /^URL$/i) {
355 $url .= "[$value]\n";
356 if ($section eq "") {$url .= "<section>$doc_OID\n";}
357 else {$url .= "<section>$doc_OID.$section\n";}
358 $url .= '-' x 70 . "\n";
359 }
360
361 if (!defined $self->{'dontgdbm'}->{$field}) {
362 print $handle "<$field>$value\n";
363 }
364 }
365 }
366
367 # output archivedir if at top level
368 if ($section eq $doc_obj->get_top_section()) {
369 print $handle "<archivedir>$archivedir\n";
370 }
371
372 # output document display type
373 if ($first) {
374 print $handle "<thistype>$thistype\n";
375 }
376
377 if ($self->{'gdbm_level'} eq "document") {
378 # doc num is num_docs not num_sections
379 # output the matching document number
380 print $handle "<docnum>$self->{'num_docs'}\n";
381 } else {
382 # output a list of children
383 my $children = $doc_obj->get_children ($section);
384 if (scalar(@$children) > 0) {
385 print $handle "<childtype>$childtype\n";
386 print $handle "<contains>";
387 my $firstchild = 1;
388 foreach my $child (@$children) {
389 print $handle ";" unless $firstchild;
390 $firstchild = 0;
391 if ($child =~ /^.*?\.(\d+)$/) {
392 print $handle "\".$1";
393 } else {
394 print $handle "\".$child";
395 }
396# if ($child eq "") { print $handle "$doc_OID"; }
397# elsif ($section eq "") { print $handle "$doc_OID.$child"; }
398# else { print $handle "$doc_OID.$section.$child"; }
399 }
400 print $handle "\n";
401 }
402 #output the matching doc number
403 print $handle "<docnum>$self->{'num_sections'}\n";
404
405 }
406
407 print $handle '-' x 70, "\n";
408
409
410 # output a database entry for the document number
411 if ($self->{'gdbm_level'} eq "document") {
412 print $handle "[$self->{'num_docs'}]\n";
413 print $handle "<section>$doc_OID\n";
414 }
415 else {
416 print $handle "[$self->{'num_sections'}]\n";
417 if ($section eq "") { print $handle "<section>$doc_OID\n"; }
418 else { print $handle "<section>$doc_OID.$section\n"; }
419 }
420 print $handle '-' x 70, "\n";
421
422 # output entry for url
423 if ($url ne "") {
424 print $handle $url;
425 }
426
427 $first = 0;
428 $section = $doc_obj->get_next_section($section);
429 last if ($self->{'gdbm_level'} eq "document"); # if no sections wanted, only gdbm the docs
430 }
431
432 #GRB01062004: see code above moved from here
433}
434
435
436sub text {
437 my $self = shift (@_);
438 my ($doc_obj) = @_;
439
440 my $handle = $self->{'outhandle'};
441 print $handle "basebuildproc::text function must be implemented in sub classes\n";
442 die "\n";
443}
444
445# should the document be indexed - according to the subcollection and language
446# specification.
447sub is_subcollection_doc {
448 my $self = shift (@_);
449 my ($doc_obj) = @_;
450
451 my $indexed_doc = 1;
452 foreach my $indexexp (@{$self->{'indexexparr'}}) {
453 $indexed_doc = 0;
454 my ($field, $exp, $options) = split /\//, $indexexp;
455 if (defined ($field) && defined ($exp)) {
456 my ($bool) = $field =~ /^(.)/;
457 $field =~ s/^.// if $bool eq '!';
458 my @metadata_values;
459 if ($field =~ /^filename$/i) {
460 push(@metadata_values, $doc_obj->get_source_filename());
461 }
462 else {
463 @metadata_values = @{$doc_obj->get_metadata($doc_obj->get_top_section(), $field)};
464 }
465 next unless @metadata_values;
466 foreach my $metadata_value (@metadata_values) {
467 if ($bool eq '!') {
468 if ($options =~ /^i$/i) {
469 if ($metadata_value !~ /$exp/i) {$indexed_doc = 1; last;}
470 } else {
471 if ($metadata_value !~ /$exp/) {$indexed_doc = 1; last;}
472 }
473 } else {
474 if ($options =~ /^i$/i) {
475 if ($metadata_value =~ /$exp/i) {$indexed_doc = 1; last;}
476 } else {
477 if ($metadata_value =~ /$exp/) {$indexed_doc = 1; last;}
478 }
479 }
480 }
481
482 last if ($indexed_doc == 1);
483 }
484 }
485
486 # if this doc is so far in the sub collection, and we have lang info,
487 # now we check the languages to see if it matches
488 if($indexed_doc && defined $self->{'lang_meta'}) {
489 $indexed_doc = 0;
490 my $field = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $self->{'lang_meta'});
491 if (defined $field) {
492 foreach my $lang (@{$self->{'langarr'}}) {
493 my ($bool) = $lang =~ /^(.)/;
494 if ($bool eq '!') {
495 $lang =~ s/^.//;
496 if ($field !~ /$lang/) {
497 $indexed_doc = 1; last;
498 }
499 } else {
500 if ($field =~ /$lang/) {
501 $indexed_doc = 1; last;
502 }
503 }
504 }
505 }
506 }
507 return $indexed_doc;
508
509}
510
511# use 'Paged' if document has no more than 2 levels
512# and each section at second level has a number for
513# Title metadata
514# also use Paged if gsdlthistype metadata is set to Paged
515sub get_document_type {
516 my $self = shift (@_);
517 my ($doc_obj) = @_;
518
519 my $thistype = "VList";
520 my $childtype = "VList";
521 my $title;
522 my @tmp = ();
523
524 my $section = $doc_obj->get_top_section ();
525
526 my $gsdlthistype = $doc_obj->get_metadata_element ($section, "gsdlthistype");
527 if (defined $gsdlthistype) {
528 if ($gsdlthistype eq "Paged") {
529 $childtype = "Paged";
530 if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
531 $thistype = "Paged";
532 } else {
533 $thistype = "Invisible";
534 }
535
536 return ($thistype, $childtype);
537 } elsif ($gsdlthistype eq "Hierarchy") {
538 return ($thistype, $childtype); # use VList, VList
539 }
540 }
541 my $first = 1;
542 while (defined $section) {
543 @tmp = split /\./, $section;
544 if (scalar(@tmp) > 1) {
545 return ($thistype, $childtype);
546 }
547 if (!$first) {
548 $title = $doc_obj->get_metadata_element ($section, "Title");
549 if (!defined $title || $title !~ /^\d+$/) {
550 return ($thistype, $childtype);
551 }
552 }
553 $first = 0;
554 $section = $doc_obj->get_next_section($section);
555 }
556 if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
557 $thistype = "Paged";
558 } else {
559 $thistype = "Invisible";
560 }
561 $childtype = "Paged";
562 return ($thistype, $childtype);
563}
564
565sub assoc_files {
566 my $self = shift (@_);
567 my ($doc_obj, $archivedir) = @_;
568 my ($afile);
569
570 foreach my $assoc_file (@{$doc_obj->get_assoc_files()}) {
571 # if assoc file starts with a slash, we put it relative to the assoc
572 # dir, otherwise it is relative to the HASH... directory
573 if ($assoc_file->[1] =~ m@^[/\\]@) {
574 $afile = &util::filename_cat($self->{'assocdir'},$assoc_file->[1]);
575 } else {
576 $afile = &util::filename_cat($self->{'assocdir'}, $archivedir, $assoc_file->[1]);
577 }
578 &util::hard_link ($assoc_file->[0], $afile);
579 }
580}
581
Note: See TracBrowser for help on using the repository browser.