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

Last change on this file since 11089 was 10469, checked in by kjdon, 19 years ago

added sections_index_document_metadata variable

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