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

Last change on this file since 10159 was 10159, checked in by davidb, 19 years ago

Package upgraded to support incremental building. Main change it to set
doc_num counters etc to that of previous build (loaded in from build.cfg if
present). 'reset' also returns their values to the pre-loaded value.

  • Property svn:keywords set to Author Date Id Revision
File size: 15.2 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 print STDERR "***** infodb called with doc_obj = $doc_obj\n";
279
280 my $doctype = $doc_obj->get_doc_type();
281
282 # only output this document if it is one to be indexed
283 return if ($doctype ne "indexed_doc");
284
285 my ($archivedir) = $filename =~ /^(.*?)(?:\/|\\)[^\/\\]*$/;
286 $archivedir = "" unless defined $archivedir;
287 $archivedir =~ s/\\/\//g;
288 $archivedir =~ s/^\/+//;
289 $archivedir =~ s/\/+$//;
290
291 # resolve the final filenames of the files associated with this document
292 $self->assoc_files ($doc_obj, $archivedir);
293
294 #GRB: moved 1/06/2004 from GRB01062004
295 #add this document to the browse structure
296 push(@{$self->{'doclist'}},$doc_obj->get_OID())
297 unless ($doctype eq "classification");
298
299 # classify this document
300 &classify::classify_doc ($self->{'classifiers'}, $doc_obj);
301 #GRB: end of moved block
302
303 # this is another document
304 $self->{'num_docs'} += 1 unless ($doctype eq "classification");
305
306 # is this a paged or a hierarchical document
307 my ($thistype, $childtype) = $self->get_document_type ($doc_obj);
308
309 my $section = $doc_obj->get_top_section ();
310 my $doc_OID = $doc_obj->get_OID();
311 my $first = 1;
312 my $url = "";
313 while (defined $section) {
314 # update a few statistics
315 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
316 $self->{'num_sections'} += 1 unless ($doctype eq "classification");
317
318 # output the section name
319 if ($section eq "") { print $handle "[$doc_OID]\n"; }
320 else { print $handle "[$doc_OID.$section]\n"; }
321
322 # output the fact that this document is a document (unless doctype
323 # has been set to something else from within a plugin
324 my $dtype = $doc_obj->get_metadata_element ($section, "doctype");
325 if (!defined $dtype || $dtype !~ /\w/) {
326 print $handle "<doctype>doc\n";
327 }
328
329 # output whether this node contains text
330 if ($doc_obj->get_text_length($section) > 0) {
331 print $handle "<hastxt>1\n";
332 } else {
333 print $handle "<hastxt>0\n";
334 }
335
336 # output all the section metadata
337 my $metadata = $doc_obj->get_all_metadata ($section);
338 foreach my $pair (@$metadata) {
339 my ($field, $value) = (@$pair);
340
341 if ($field ne "Identifier" && $field !~ /^gsdl/ &&
342 defined $value && $value ne "") {
343
344 # escape problematic stuff
345 $value =~ s/\\/\\\\/g;
346 $value =~ s/\n/\\n/g;
347 $value =~ s/\r/\\r/g;
348 if ($value =~ /-{70,}/) {
349 # if value contains 70 or more hyphens in a row we need
350 # to escape them to prevent txt2db from treating them
351 # as a separator
352 $value =~ s/-/&\#045;/gi;
353 }
354
355 # special case for URL metadata
356 if ($field =~ /^URL$/i) {
357 $url .= "[$value]\n";
358 if ($section eq "") {$url .= "<section>$doc_OID\n";}
359 else {$url .= "<section>$doc_OID.$section\n";}
360 $url .= '-' x 70 . "\n";
361 }
362
363 if (!defined $self->{'dontgdbm'}->{$field}) {
364 print $handle "<$field>$value\n";
365 }
366 }
367 }
368
369 # output archivedir if at top level
370 if ($section eq $doc_obj->get_top_section()) {
371 print $handle "<archivedir>$archivedir\n";
372 }
373
374 # output document display type
375 if ($first) {
376 print $handle "<thistype>$thistype\n";
377 }
378
379 if ($self->{'gdbm_level'} eq "document") {
380 # doc num is num_docs not num_sections
381 # output the matching document number
382 print $handle "<docnum>$self->{'num_docs'}\n";
383 } else {
384 # output a list of children
385 my $children = $doc_obj->get_children ($section);
386 if (scalar(@$children) > 0) {
387 print $handle "<childtype>$childtype\n";
388 print $handle "<contains>";
389 my $firstchild = 1;
390 foreach my $child (@$children) {
391 print $handle ";" unless $firstchild;
392 $firstchild = 0;
393 if ($child =~ /^.*?\.(\d+)$/) {
394 print $handle "\".$1";
395 } else {
396 print $handle "\".$child";
397 }
398# if ($child eq "") { print $handle "$doc_OID"; }
399# elsif ($section eq "") { print $handle "$doc_OID.$child"; }
400# else { print $handle "$doc_OID.$section.$child"; }
401 }
402 print $handle "\n";
403 }
404 #output the matching doc number
405 print $handle "<docnum>$self->{'num_sections'}\n";
406
407 }
408
409 print $handle '-' x 70, "\n";
410
411
412 # output a database entry for the document number
413 if ($self->{'gdbm_level'} eq "document") {
414 print $handle "[$self->{'num_docs'}]\n";
415 print $handle "<section>$doc_OID\n";
416 }
417 else {
418 print $handle "[$self->{'num_sections'}]\n";
419 if ($section eq "") { print $handle "<section>$doc_OID\n"; }
420 else { print $handle "<section>$doc_OID.$section\n"; }
421 }
422 print $handle '-' x 70, "\n";
423
424 # output entry for url
425 if ($url ne "") {
426 print $handle $url;
427 }
428
429 $first = 0;
430 $section = $doc_obj->get_next_section($section);
431 last if ($self->{'gdbm_level'} eq "document"); # if no sections wanted, only gdbm the docs
432 }
433
434 #GRB01062004: see code above moved from here
435}
436
437
438sub text {
439 my $self = shift (@_);
440 my ($doc_obj) = @_;
441
442 my $handle = $self->{'outhandle'};
443 print $handle "basebuildproc::text function must be implemented in sub classes\n";
444 die "\n";
445}
446
447# should the document be indexed - according to the subcollection and language
448# specification.
449sub is_subcollection_doc {
450 my $self = shift (@_);
451 my ($doc_obj) = @_;
452
453 my $indexed_doc = 1;
454 foreach my $indexexp (@{$self->{'indexexparr'}}) {
455 $indexed_doc = 0;
456 my ($field, $exp, $options) = split /\//, $indexexp;
457 if (defined ($field) && defined ($exp)) {
458 my ($bool) = $field =~ /^(.)/;
459 $field =~ s/^.// if $bool eq '!';
460 my @metadata_values;
461 if ($field =~ /^filename$/i) {
462 push(@metadata_values, $doc_obj->get_source_filename());
463 }
464 else {
465 @metadata_values = @{$doc_obj->get_metadata($doc_obj->get_top_section(), $field)};
466 }
467 next unless @metadata_values;
468 foreach my $metadata_value (@metadata_values) {
469 if ($bool eq '!') {
470 if ($options =~ /^i$/i) {
471 if ($metadata_value !~ /$exp/i) {$indexed_doc = 1; last;}
472 } else {
473 if ($metadata_value !~ /$exp/) {$indexed_doc = 1; last;}
474 }
475 } else {
476 if ($options =~ /^i$/i) {
477 if ($metadata_value =~ /$exp/i) {$indexed_doc = 1; last;}
478 } else {
479 if ($metadata_value =~ /$exp/) {$indexed_doc = 1; last;}
480 }
481 }
482 }
483
484 last if ($indexed_doc == 1);
485 }
486 }
487
488 # if this doc is so far in the sub collection, and we have lang info,
489 # now we check the languages to see if it matches
490 if($indexed_doc && defined $self->{'lang_meta'}) {
491 $indexed_doc = 0;
492 my $field = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $self->{'lang_meta'});
493 if (defined $field) {
494 foreach my $lang (@{$self->{'langarr'}}) {
495 my ($bool) = $lang =~ /^(.)/;
496 if ($bool eq '!') {
497 $lang =~ s/^.//;
498 if ($field !~ /$lang/) {
499 $indexed_doc = 1; last;
500 }
501 } else {
502 if ($field =~ /$lang/) {
503 $indexed_doc = 1; last;
504 }
505 }
506 }
507 }
508 }
509 return $indexed_doc;
510
511}
512
513# use 'Paged' if document has no more than 2 levels
514# and each section at second level has a number for
515# Title metadata
516# also use Paged if gsdlthistype metadata is set to Paged
517sub get_document_type {
518 my $self = shift (@_);
519 my ($doc_obj) = @_;
520
521 my $thistype = "VList";
522 my $childtype = "VList";
523 my $title;
524 my @tmp = ();
525
526 my $section = $doc_obj->get_top_section ();
527
528 my $gsdlthistype = $doc_obj->get_metadata_element ($section, "gsdlthistype");
529 if (defined $gsdlthistype) {
530 if ($gsdlthistype eq "Paged") {
531 $childtype = "Paged";
532 if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
533 $thistype = "Paged";
534 } else {
535 $thistype = "Invisible";
536 }
537
538 return ($thistype, $childtype);
539 } elsif ($gsdlthistype eq "Hierarchy") {
540 return ($thistype, $childtype); # use VList, VList
541 }
542 }
543 my $first = 1;
544 while (defined $section) {
545 @tmp = split /\./, $section;
546 if (scalar(@tmp) > 1) {
547 return ($thistype, $childtype);
548 }
549 if (!$first) {
550 $title = $doc_obj->get_metadata_element ($section, "Title");
551 if (!defined $title || $title !~ /^\d+$/) {
552 return ($thistype, $childtype);
553 }
554 }
555 $first = 0;
556 $section = $doc_obj->get_next_section($section);
557 }
558 if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
559 $thistype = "Paged";
560 } else {
561 $thistype = "Invisible";
562 }
563 $childtype = "Paged";
564 return ($thistype, $childtype);
565}
566
567sub assoc_files {
568 my $self = shift (@_);
569 my ($doc_obj, $archivedir) = @_;
570 my ($afile);
571
572 foreach my $assoc_file (@{$doc_obj->get_assoc_files()}) {
573 # if assoc file starts with a slash, we put it relative to the assoc
574 # dir, otherwise it is relative to the HASH... directory
575 if ($assoc_file->[1] =~ m@^[/\\]@) {
576 $afile = &util::filename_cat($self->{'assocdir'},$assoc_file->[1]);
577 } else {
578 $afile = &util::filename_cat($self->{'assocdir'}, $archivedir, $assoc_file->[1]);
579 }
580 &util::hard_link ($assoc_file->[0], $afile);
581 }
582}
583
Note: See TracBrowser for help on using the repository browser.