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

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

Introduction of is_incremental() function in base class (not incremental
by default). Over-ridden by inherited classes if incremental building
possible.

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