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

Last change on this file since 11994 was 11994, checked in by davidb, 18 years ago

Improved support for incremental addition: instead of having to run the
classifier pass of buildcol.pl from scratch (i.e. read in all documents
from the archives folder) so correct browse structures are formed -- a
simple to implement strategy, but not very efficient -- the first layer
of a classifier structure is now reconstructed from the GDBM file. Then
the new files in the archives directory are added, and then finally the
completed browser structure is formed.

  • Property svn:keywords set to Author Date Id Revision
File size: 16.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 is_incremental_capable
143{
144 # By default we return 'no' as the answer
145 # Safer to assume non-incremental to start with, and then override in
146 # inherited classes that are.
147
148 return 0;
149}
150
151sub get_num_docs {
152 my $self = shift (@_);
153
154 return $self->{'num_docs'};
155}
156
157sub get_num_sections {
158 my $self = shift (@_);
159
160 return $self->{'num_sections'};
161}
162
163# num_bytes is the actual number of bytes in the collection
164# this is normally the same as what's processed during text compression
165sub get_num_bytes {
166 my $self = shift (@_);
167
168 return $self->{'num_bytes'};
169}
170
171# num_processed_bytes is the number of bytes actually passed
172# to mg for the current index
173sub get_num_processed_bytes {
174 my $self = shift (@_);
175
176 return $self->{'num_processed_bytes'};
177}
178
179sub set_output_handle {
180 my $self = shift (@_);
181 my ($handle) = @_;
182
183 $self->{'output_handle'} = $handle;
184}
185
186
187sub set_mode {
188 my $self = shift (@_);
189 my ($mode) = @_;
190
191 $self->{'mode'} = $mode;
192}
193
194sub get_mode {
195 my $self = shift (@_);
196
197 return $self->{'mode'};
198}
199
200sub set_assocdir {
201 my $self = shift (@_);
202 my ($assocdir) = @_;
203
204 $self->{'assocdir'} = $assocdir;
205}
206
207sub set_dontgdbm {
208 my $self = shift (@_);
209 my ($dontgdbm) = @_;
210
211 $self->{'dontgdbm'} = $dontgdbm;
212}
213
214sub set_index {
215 my $self = shift (@_);
216 my ($index, $indexexparr) = @_;
217
218 $self->{'index'} = $index;
219 $self->{'indexexparr'} = $indexexparr if defined $indexexparr;
220}
221
222sub set_index_languages {
223 my $self = shift (@_);
224 my ($lang_meta, $langarr) = @_;
225 $self->{'lang_meta'} = $lang_meta;
226 $self->{'langarr'} = $langarr;
227}
228
229sub get_index {
230 my $self = shift (@_);
231
232 return $self->{'index'};
233}
234
235sub set_classifiers {
236 my $self = shift (@_);
237 my ($classifiers) = @_;
238
239 $self->{'classifiers'} = $classifiers;
240}
241
242sub set_indexing_text {
243 my $self = shift (@_);
244 my ($indexing_text) = @_;
245
246 $self->{'indexing_text'} = $indexing_text;
247}
248
249sub get_indexing_text {
250 my $self = shift (@_);
251
252 return $self->{'indexing_text'};
253}
254
255sub set_store_text {
256 my $self = shift (@_);
257 my ($store_text) = @_;
258
259 $self->{'store_text'} = $store_text;
260}
261sub get_doc_list {
262 my $self = shift(@_);
263
264 return @{$self->{'doclist'}};
265}
266
267# the standard gdbm level is section, but you may want to change it to document
268sub set_gdbm_level {
269 my $self= shift (@_);
270 my ($gdbm_level) = @_;
271
272 $self->{'gdbm_level'} = $gdbm_level;
273}
274
275sub set_sections_index_document_metadata {
276 my $self= shift (@_);
277 my ($index_type) = @_;
278
279 $self->{'sections_index_document_metadata'} = $index_type;
280}
281sub process {
282 my $self = shift (@_);
283 my $method = $self->{'mode'};
284
285 $self->$method(@_);
286}
287
288sub infodb {
289 my $self = shift (@_);
290 my ($doc_obj, $filename) = @_;
291 my $handle = $self->{'output_handle'};
292
293 my $doctype = $doc_obj->get_doc_type();
294
295 # only output this document if it is a "indexed_doc" or "info_doc" (GDBM database only) document
296 return if ($doctype ne "indexed_doc" && $doctype ne "info_doc");
297
298 my $archivedir = "";
299
300 if (defined $filename)
301 {
302 # doc_obj derived directly from file
303
304 my ($dir) = $filename =~ /^(.*?)(?:\/|\\)[^\/\\]*$/;
305 $dir = "" unless defined $dir;
306 $dir =~ s/\\/\//g;
307 $dir =~ s/^\/+//;
308 $dir =~ s/\/+$//;
309
310 $archivedir = $dir;
311
312 # resolve the final filenames of the files associated with this document
313 $self->assoc_files ($doc_obj, $archivedir);
314 }
315 else
316 {
317 # doc_obj reconstructed from GDBM (has metadata, doc structure but no text)
318 my $top_section = $doc_obj->get_top_section();
319 $archivedir = $doc_obj->get_metadata_element($top_section,"archivedir");
320 }
321
322
323 #GRB: moved 1/06/2004 from GRB01062004
324 #add this document to the browse structure
325 push(@{$self->{'doclist'}},$doc_obj->get_OID())
326 unless ($doctype eq "classification");
327
328 # classify this document
329 &classify::classify_doc ($self->{'classifiers'}, $doc_obj);
330 #GRB: end of moved block
331
332 # this is another document
333 $self->{'num_docs'} += 1 unless ($doctype eq "classification");
334
335 # is this a paged or a hierarchical document
336 my ($thistype, $childtype) = $self->get_document_type ($doc_obj);
337
338 my $section = $doc_obj->get_top_section ();
339 my $doc_OID = $doc_obj->get_OID();
340 my $first = 1;
341 my $url = "";
342 while (defined $section) {
343 # update a few statistics
344 $self->{'num_bytes'} += $doc_obj->get_text_length ($section);
345 $self->{'num_sections'} += 1 unless ($doctype eq "classification");
346
347 # output the section name
348 if ($section eq "") { print $handle "[$doc_OID]\n"; }
349 else { print $handle "[$doc_OID.$section]\n"; }
350
351 # output the fact that this document is a document (unless doctype
352 # has been set to something else from within a plugin
353 my $dtype = $doc_obj->get_metadata_element ($section, "doctype");
354 if (!defined $dtype || $dtype !~ /\w/) {
355 print $handle "<doctype>doc\n";
356 }
357
358 # Output whether this node contains text
359 #
360 # If doc_obj reconstructed from GDBM file then no need to
361 # explicitly add <hastxt> as this is preserved as metadata when
362 # the GDBM file is loaded in
363
364 if (defined $filename)
365 {
366 # doc_obj derived directly from file
367 if ($doc_obj->get_text_length($section) > 0) {
368 print $handle "<hastxt>1\n";
369 } else {
370 print $handle "<hastxt>0\n";
371 }
372 }
373
374 # output all the section metadata
375 my $metadata = $doc_obj->get_all_metadata ($section);
376 foreach my $pair (@$metadata) {
377 my ($field, $value) = (@$pair);
378
379 if ($field ne "Identifier" && $field !~ /^gsdl/ &&
380 defined $value && $value ne "") {
381
382 # escape problematic stuff
383 $value =~ s/\\/\\\\/g;
384 $value =~ s/\n/\\n/g;
385 $value =~ s/\r/\\r/g;
386 if ($value =~ /-{70,}/) {
387 # if value contains 70 or more hyphens in a row we need
388 # to escape them to prevent txt2db from treating them
389 # as a separator
390 $value =~ s/-/&\#045;/gi;
391 }
392
393 # special case for URL metadata
394 if ($field =~ /^URL$/i) {
395 $url .= "[$value]\n";
396 if ($section eq "") {$url .= "<section>$doc_OID\n";}
397 else {$url .= "<section>$doc_OID.$section\n";}
398 $url .= '-' x 70 . "\n";
399 }
400
401 if (!defined $self->{'dontgdbm'}->{$field}) {
402 print $handle "<$field>$value\n";
403 }
404 }
405 }
406
407
408 # If doc_obj reconstructed from GDBM file then no need to
409 # explicitly add <archivedir> as this is preserved as metadata when
410 # the GDBM file is loaded in
411
412 if (defined $filename)
413 {
414 # output archivedir if at top level
415 if ($section eq $doc_obj->get_top_section()) {
416 print $handle "<archivedir>$archivedir\n";
417 }
418 }
419
420 # output document display type
421 if ($first) {
422 print $handle "<thistype>$thistype\n";
423 }
424
425
426 if ($self->{'gdbm_level'} eq "document") {
427 # doc num is num_docs not num_sections
428 # output the matching document number
429 print $handle "<docnum>$self->{'num_docs'}\n";
430 } else {
431 # output a list of children
432 my $children = $doc_obj->get_children ($section);
433 if (scalar(@$children) > 0) {
434 print $handle "<childtype>$childtype\n";
435 print $handle "<contains>";
436 my $firstchild = 1;
437 foreach my $child (@$children) {
438 print $handle ";" unless $firstchild;
439 $firstchild = 0;
440 if ($child =~ /^.*?\.(\d+)$/) {
441 print $handle "\".$1";
442 } else {
443 print $handle "\".$child";
444 }
445# if ($child eq "") { print $handle "$doc_OID"; }
446# elsif ($section eq "") { print $handle "$doc_OID.$child"; }
447# else { print $handle "$doc_OID.$section.$child"; }
448 }
449 print $handle "\n";
450 }
451 #output the matching doc number
452 print $handle "<docnum>$self->{'num_sections'}\n";
453
454 }
455
456 print $handle '-' x 70, "\n";
457
458
459 # output a database entry for the document number
460 if ($self->{'gdbm_level'} eq "document") {
461 print $handle "[$self->{'num_docs'}]\n";
462 print $handle "<section>$doc_OID\n";
463 }
464 else {
465 print $handle "[$self->{'num_sections'}]\n";
466 if ($section eq "") { print $handle "<section>$doc_OID\n"; }
467 else { print $handle "<section>$doc_OID.$section\n"; }
468 }
469 print $handle '-' x 70, "\n";
470
471 # output entry for url
472 if ($url ne "") {
473 print $handle $url;
474 }
475
476 $first = 0;
477 $section = $doc_obj->get_next_section($section);
478 last if ($self->{'gdbm_level'} eq "document"); # if no sections wanted, only gdbm the docs
479 }
480
481 #GRB01062004: see code above moved from here
482}
483
484
485sub text {
486 my $self = shift (@_);
487 my ($doc_obj) = @_;
488
489 my $handle = $self->{'outhandle'};
490 print $handle "basebuildproc::text function must be implemented in sub classes\n";
491 die "\n";
492}
493
494# should the document be indexed - according to the subcollection and language
495# specification.
496sub is_subcollection_doc {
497 my $self = shift (@_);
498 my ($doc_obj) = @_;
499
500 my $indexed_doc = 1;
501 foreach my $indexexp (@{$self->{'indexexparr'}}) {
502 $indexed_doc = 0;
503 my ($field, $exp, $options) = split /\//, $indexexp;
504 if (defined ($field) && defined ($exp)) {
505 my ($bool) = $field =~ /^(.)/;
506 $field =~ s/^.// if $bool eq '!';
507 my @metadata_values;
508 if ($field =~ /^filename$/i) {
509 push(@metadata_values, $doc_obj->get_source_filename());
510 }
511 else {
512 @metadata_values = @{$doc_obj->get_metadata($doc_obj->get_top_section(), $field)};
513 }
514 next unless @metadata_values;
515 foreach my $metadata_value (@metadata_values) {
516 if ($bool eq '!') {
517 if ($options =~ /^i$/i) {
518 if ($metadata_value !~ /$exp/i) {$indexed_doc = 1; last;}
519 } else {
520 if ($metadata_value !~ /$exp/) {$indexed_doc = 1; last;}
521 }
522 } else {
523 if ($options =~ /^i$/i) {
524 if ($metadata_value =~ /$exp/i) {$indexed_doc = 1; last;}
525 } else {
526 if ($metadata_value =~ /$exp/) {$indexed_doc = 1; last;}
527 }
528 }
529 }
530
531 last if ($indexed_doc == 1);
532 }
533 }
534
535 # if this doc is so far in the sub collection, and we have lang info,
536 # now we check the languages to see if it matches
537 if($indexed_doc && defined $self->{'lang_meta'}) {
538 $indexed_doc = 0;
539 my $field = $doc_obj->get_metadata_element($doc_obj->get_top_section(), $self->{'lang_meta'});
540 if (defined $field) {
541 foreach my $lang (@{$self->{'langarr'}}) {
542 my ($bool) = $lang =~ /^(.)/;
543 if ($bool eq '!') {
544 $lang =~ s/^.//;
545 if ($field !~ /$lang/) {
546 $indexed_doc = 1; last;
547 }
548 } else {
549 if ($field =~ /$lang/) {
550 $indexed_doc = 1; last;
551 }
552 }
553 }
554 }
555 }
556 return $indexed_doc;
557
558}
559
560# use 'Paged' if document has no more than 2 levels
561# and each section at second level has a number for
562# Title metadata
563# also use Paged if gsdlthistype metadata is set to Paged
564sub get_document_type {
565 my $self = shift (@_);
566 my ($doc_obj) = @_;
567
568 my $thistype = "VList";
569 my $childtype = "VList";
570 my $title;
571 my @tmp = ();
572
573 my $section = $doc_obj->get_top_section ();
574
575 my $gsdlthistype = $doc_obj->get_metadata_element ($section, "gsdlthistype");
576 if (defined $gsdlthistype) {
577 if ($gsdlthistype eq "Paged") {
578 $childtype = "Paged";
579 if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
580 $thistype = "Paged";
581 } else {
582 $thistype = "Invisible";
583 }
584
585 return ($thistype, $childtype);
586 } elsif ($gsdlthistype eq "Hierarchy") {
587 return ($thistype, $childtype); # use VList, VList
588 }
589 }
590 my $first = 1;
591 while (defined $section) {
592 @tmp = split /\./, $section;
593 if (scalar(@tmp) > 1) {
594 return ($thistype, $childtype);
595 }
596 if (!$first) {
597 $title = $doc_obj->get_metadata_element ($section, "Title");
598 if (!defined $title || $title !~ /^\d+$/) {
599 return ($thistype, $childtype);
600 }
601 }
602 $first = 0;
603 $section = $doc_obj->get_next_section($section);
604 }
605 if ($doc_obj->get_text_length ($doc_obj->get_top_section())) {
606 $thistype = "Paged";
607 } else {
608 $thistype = "Invisible";
609 }
610 $childtype = "Paged";
611 return ($thistype, $childtype);
612}
613
614sub assoc_files {
615 my $self = shift (@_);
616 my ($doc_obj, $archivedir) = @_;
617 my ($afile);
618
619 foreach my $assoc_file (@{$doc_obj->get_assoc_files()}) {
620 # if assoc file starts with a slash, we put it relative to the assoc
621 # dir, otherwise it is relative to the HASH... directory
622 if ($assoc_file->[1] =~ m@^[/\\]@) {
623 $afile = &util::filename_cat($self->{'assocdir'},$assoc_file->[1]);
624 } else {
625 $afile = &util::filename_cat($self->{'assocdir'}, $archivedir, $assoc_file->[1]);
626 }
627 &util::hard_link ($assoc_file->[0], $afile);
628 }
629}
630
Note: See TracBrowser for help on using the repository browser.