source: main/trunk/greenstone2/bin/script/explode_metadata_database.pl--for-gs311@ 34879

Last change on this file since 34879 was 34879, checked in by davidb, 3 years ago

Updated to work with the method for splitting, rather than the split_re (so more general); and to look for an Identifier in the metatada, and use that as the filename, if present

  • Property svn:executable set to *
File size: 20.4 KB
Line 
1#!/usr/bin/perl -w
2
3
4BEGIN {
5 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
6 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
7 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan");
8 unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins");
9}
10
11use strict;
12no strict 'subs'; # allow barewords (eg STDERR) as function arguments
13no strict 'refs'; # allow filehandles to be variables and vice versa
14
15use encodings;
16use extrametautil;
17use util;
18use FileUtils;
19use printusage;
20use parse2;
21use colcfg;
22
23use FileHandle;
24
25use File::Spec;
26use File::Basename;
27
28my $unicode_list =
29 [ { 'name' => "auto",
30 'desc' => "{ReadTextFile.input_encoding.auto}" },
31 { 'name' => "ascii",
32 'desc' => "{BasePlugin.encoding.ascii}" },
33 { 'name' => "utf8",
34 'desc' => "{BasePlugin.encoding.utf8}" },
35 { 'name' => "unicode",
36 'desc' => "{BasePlugin.encoding.unicode}" } ];
37
38my $e = $encodings::encodings;
39foreach my $enc (sort {$e->{$a}->{'name'} cmp $e->{$b}->{'name'}} keys (%$e))
40{
41 my $hashEncode =
42 {'name' => $enc,
43 'desc' => $e->{$enc}->{'name'}};
44
45 push(@{$unicode_list},$hashEncode);
46}
47
48my $arguments =
49 [
50 { 'name' => "language",
51 'desc' => "{scripts.language}",
52 'type' => "string",
53 'reqd' => "no",
54 'hiddengli' => "yes" },
55 { 'name' => "plugin",
56 'desc' => "{explode.plugin}",
57 'type' => "string",
58 'reqd' => "yes",
59 'hiddengli' => "yes"},
60 { 'name' => "input_encoding",
61 'desc' => "{explode.encoding}",
62 'type' => "enum",
63 'deft' => "auto",
64 'list' => $unicode_list,
65 'reqd' => "no" },
66 { 'name' => "metadata_set",
67 'desc' => "{explode.metadata_set}",
68 'type' => "string",
69 'reqd' => "no" },
70 { 'name' => "document_field",
71 'desc' => "{explode.document_field}",
72 'type' => "string",
73 'reqd' => "no"},
74 { 'name' => "document_prefix",
75 'desc' => "{explode.document_prefix}",
76 'type' => "string",
77 'reqd' => "no"},
78 { 'name' => "document_suffix",
79 'desc' => "{explode.document_suffix}",
80 'type' => "string",
81 'reqd' => "no"},
82 { 'name' => "records_per_folder",
83 'desc' => "{explode.records_per_folder}",
84 'type' => "int",
85 'range' => "0,",
86 'deft' => "100",
87 'reqd' => "no" },
88 { 'name' => "collectdir",
89 'desc' => "{import.collectdir}",
90 'type' => "string",
91 # parsearg left "" as default
92 #'deft' => &FileUtils::filenameConcatenate($ENV{'GSDLHOME'}, "collect"),
93 'deft' => "",
94 'reqd' => "no",
95 'hiddengli' => "yes" },
96 { 'name' => "site",
97 'desc' => "{import.site}",
98 'type' => "string",
99 'deft' => "",
100 'reqd' => "no",
101 'hiddengli' => "yes" },
102 { 'name' => "collection",
103 'desc' => "{explode.collection}",
104 'type' => "string",
105 'reqd' => "no",
106 'hiddengli' => "yes"},
107 { 'name' => "use_collection_plugin_options",
108 'desc' => "{explode.use_collection_plugin_options}",
109 'type' => "flag",
110 'reqd' => "no",
111 'hiddengli' => "yes"},
112 { 'name' => "plugin_options",
113 'desc' => "{explode.plugin_options}",
114 'type' => "string",
115 'reqd' => "no",
116 'hiddengli' => "yes"},
117 { 'name' => "verbosity",
118 'desc' => "{import.verbosity}",
119 'type' => "int",
120 'range' => "0,",
121 'deft' => "1",
122 'reqd' => "no",
123 'modegli' => "3" },
124 { 'name' => "xml",
125 'desc' => "",
126 'type' => "flag",
127 'reqd' => "no",
128 'hiddengli' => "yes" }
129 ];
130
131my $options = { 'name' => "explode_metadata_database.pl",
132 'desc' => "{explode.desc}",
133 'args' => $arguments };
134
135
136
137sub main
138{
139 my ($language, $input_encoding, $metadata_set, $plugin,
140 $document_field, $document_prefix, $document_suffix,
141 $records_per_folder, $plugin_options, $collectdir, $site, $collection,
142 $use_collection_plugin_options, $verbosity);
143
144 my $xml = 0;
145
146 my $hashParsingResult = {};
147 # parse the options
148 my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
149
150 # If parse returns -1 then something has gone wrong
151 if ($intArgLeftinAfterParsing == -1)
152 {
153 &PrintUsage::print_txt_usage($options, "{explode.params}");
154 die "\n";
155 }
156
157 foreach my $strVariable (keys %$hashParsingResult)
158 {
159 eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
160 }
161
162 # If $language has been specified, load the appropriate resource bundle
163 # (Otherwise, the default resource bundle will be loaded automatically)
164 if ($language && $language =~ /\S/) {
165 &gsprintf::load_language_specific_resource_bundle($language);
166 }
167
168 if ($xml) {
169 &PrintUsage::print_xml_usage($options);
170 print "\n";
171 return;
172 }
173
174
175 # There should one arg left after parsing (the filename)
176 # Or the user may have specified -h, in which case we output the usage
177 if($intArgLeftinAfterParsing != 1 || (@ARGV && $ARGV[0] =~ /^\-+h/))
178 {
179 &PrintUsage::print_txt_usage($options, "{explode.params}");
180 die "\n";
181 }
182
183 # The metadata database filename is the first value that remains after the options have been parsed out
184 my $filename = $ARGV[0];
185 if (!defined $filename || $filename !~ /\w/) {
186 &PrintUsage::print_txt_usage($options, "{explode.params}");
187 print STDERR "You need to specify a filename";
188 die "\n";
189 }
190 # check that file exists
191 if (!-e $filename) {
192 print STDERR "File $filename doesn't exist...\n";
193 die "\n";
194 }
195 # check required options
196 if (!defined $plugin || $plugin !~ /\w/) {
197 &PrintUsage::print_txt_usage($options, "{explode.params}");
198 print STDERR "You need to specify a plugin";
199 die "\n";
200 }
201
202 # check metadata set
203 if (defined $metadata_set && $metadata_set =~ /\w/) {
204 $metadata_set .= ".";
205 } else {
206 $metadata_set = "";
207 }
208 if (defined $collection && $collection =~ /\w/) {
209 if (($collection = &colcfg::use_collection($site, $collection, $collectdir)) eq "") {
210 print STDERR "Collection $collection does not exist\n";
211 die "\n";
212 }
213 } else {
214 undef $collection;
215 }
216
217 if ($use_collection_plugin_options) {
218 if (defined $plugin_options && $plugin_options =~ /\w/) {
219 print STDERR "Error: you cannot have -use_collection_plugin_options and -plugin_options set at the same time\n";
220 die "\n";
221 }
222 if (not defined $collection) {
223 print STDERR "Error: you must specify a collection using -collection to use -use_collection_plugin_options\n";
224 die "\n";
225 }
226 }
227
228 if (defined $collectdir) {
229 unshift (@INC, "$collectdir/$collection/perllib/plugins");
230 }
231
232 my $plugobj;
233 require "$plugin.pm";
234
235 my $plugin_options_string = "";
236 if ($use_collection_plugin_options) {
237 # read in the collect.cfg file
238 # Read in the collection configuration file.
239 my $gs_mode = "gs2";
240 if ((defined $site) && ($site ne "")) { # GS3
241 $gs_mode = "gs3";
242 }
243 my $configfilename = &colcfg::get_collect_cfg_name(STDERR, $gs_mode);
244 my $collectcfg = &colcfg::read_collect_cfg ($configfilename, $gs_mode);
245 $plugin_options_string = &get_plugin_options($collectcfg, $plugin);
246 }
247 elsif (defined $plugin_options && $plugin_options =~ /\w/) {
248 my @options = split(/\s/, $plugin_options);
249 map { $_ = "\"$_\"" unless $_ =~ /^\"/; } @options;
250 $plugin_options_string= join (",", @options);
251 }
252
253 if ($plugin_options_string eq "") {
254 eval ("\$plugobj = new $plugin()");
255 die "$@" if $@;
256 } else {
257 eval ("\$plugobj = new $plugin([], [$plugin_options_string])");
258 die "$@" if $@;
259 }
260
261 # ...and initialize it
262 $plugobj->init($verbosity, "STDERR", "STDERR");
263
264 if ($input_encoding eq "auto") {
265 ($language, $input_encoding) = $plugobj->textcat_get_language_encoding ($filename);
266 }
267
268 # Create a directory to store the document files...
269 my ($exploded_base_dir) = ($filename =~ /(.*)\.[^\.]+$/);
270
271 my $orig_base_dir = &File::Basename::dirname($filename);
272
273
274 my $split_exp = $plugobj->{'split_exp'};
275 if (defined $split_exp) {
276 print STDERR "***** using 'split_exp' code path\n";
277
278 # Read in file, and then split and process individual records
279
280 my $text = "";
281 # Use the plugin's read_file function to avoid duplicating code
282 $plugobj->read_file($filename, $input_encoding, undef, \$text);
283 # is there any text in the file??
284 die "\n" unless length($text);
285
286 # Split the text into records, using the plugin's split_exp
287
288 ##my @metadata_records = split(/$split_exp/, $text);
289 my $metadata_records_ref = $plugobj->split_text_into_segments(\$text);
290 my @metadata_records = @$metadata_records_ref;
291
292 my $total_num_records = scalar(@metadata_records);
293 print STDERR "Number of records: $total_num_records\n";
294
295 # Write the metadata from each record to the metadata.xml file
296 my $record_number = 1;
297 my $documents_directory;
298 foreach my $record_text (@metadata_records) {
299
300 # Check if we need to start a new directory for these records
301 check_need_new_directory($exploded_base_dir,$record_number,
302 $records_per_folder,$total_num_records,
303 \$documents_directory);
304 # Use the plugin's process function to avoid duplicating code
305 my $doc_obj = new doc($filename, "nonindexed_doc", $plugobj->get_file_rename_method());
306 $plugobj->process(\$record_text, undef, undef, $filename, undef, $doc_obj, 0);
307
308
309 # Try to get a doc to attach the metadata to
310 # If no match found, create a dummy .nul file
311 attach_metadata_or_make_nul_doc($document_field, $doc_obj, $record_number,
312 $documents_directory, $orig_base_dir,
313 $document_prefix, $document_suffix, $metadata_set, $verbosity);
314
315
316 check_close_directory($record_number,$records_per_folder,$total_num_records);
317
318 $record_number = $record_number + 1;
319 }
320 }
321 else {
322 # Call metadata_read to set up associated metadata
323
324 my $pluginfo = undef;
325 my $block_hash = {};
326
327 my $processor = undef;
328 my $maxdocs = undef;
329 my $gli = undef;
330
331 my $extrametakeys = [];
332 my $extrametadata = {};
333 my $extrametafile = {};
334
335 $plugobj->metadata_read($pluginfo, "", $filename, $block_hash,
336 $extrametakeys, $extrametadata, $extrametafile,
337 $processor, $maxdocs, $gli);
338
339 my $total_num_records = scalar (@$extrametakeys);
340 print STDERR "Number of records: $total_num_records\n";
341 my $record_number = 1;
342 my $documents_directory;
343 foreach my $record (@$extrametakeys) {
344 &check_need_new_directory($exploded_base_dir, $record_number, $records_per_folder, $total_num_records, \$documents_directory);
345
346 # Attach metadata to object
347 # => use the plugin's extra_metadata function to avoid duplicating code
348 my $doc_obj = new doc($filename, "nonindexed_doc", $plugobj->get_file_rename_method());
349 # all the metadata has been extracted into extrametadata
350 $plugobj->extra_metadata ($doc_obj, $doc_obj->get_top_section(), &extrametautil::getmetadata($extrametadata, $record));
351
352 # Try to get a doc to attach the metadata to
353 # If no match found, create a dummy .nul file
354 attach_metadata_or_make_nul_doc($document_field, $doc_obj, $record_number, $documents_directory, $orig_base_dir, $document_prefix, $document_suffix, $metadata_set, $verbosity);
355
356 &check_close_directory($record_number,$records_per_folder,$total_num_records);
357
358 $record_number = $record_number + 1;
359
360 }
361 }
362
363 # Explode means just that: the original file is deleted
364 &FileUtils::removeFiles($filename);
365 $plugobj->clean_up_after_exploding();
366
367}
368
369
370sub need_new_directory
371{
372 my ($exploded_base_dir) = @_;
373
374 my $documents_directory = $exploded_base_dir;
375
376 if (-d $documents_directory) {
377 die "Error: document directory $documents_directory already exists (bailing).\n";
378 }
379 &FileUtils::makeDirectory($documents_directory);
380
381 my $documents_metadata_xml_file = &FileUtils::filenameConcatenate($documents_directory, "metadata.xml");
382 if (-e $documents_metadata_xml_file) {
383 die "Error: documents metadata.xml file $documents_metadata_xml_file already exists (bailing).\n";
384 }
385
386 # Start the metadata.xml file
387 open(METADATA_XML_FILE, ">$documents_metadata_xml_file");
388 binmode METADATA_XML_FILE, ":utf8";
389 print METADATA_XML_FILE
390 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" .
391 "<!DOCTYPE DirectoryMetadata SYSTEM \"http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd\">\n" .
392 "<DirectoryMetadata>\n";
393
394 return $documents_directory;
395}
396
397sub check_need_new_directory
398{
399 my ($exploded_base_dir,$record_number, $records_per_folder,
400 $total_num_records, $documents_dir_ref) = @_;
401
402
403 # Check if we need to start a new directory for these records
404 if ($records_per_folder == 1 || ($record_number % $records_per_folder) == 1) {
405 my $documents_directory = $exploded_base_dir;
406
407 if ($total_num_records > $records_per_folder) {
408 $documents_directory .= "." . sprintf("%8.8d", $record_number);
409 }
410
411 $$documents_dir_ref = need_new_directory($documents_directory);
412 }
413}
414
415
416
417
418
419sub attach_metadata_or_make_nul_doc
420{
421 my ($document_field, $doc_obj, $record_number,
422 $documents_directory, $orig_base_dir,
423 $document_prefix, $document_suffix, $metadata_set, $verbosity) = @_;
424
425 my $record_metadata = $doc_obj->get_all_metadata($doc_obj->get_top_section());
426 my $document_file;
427
428 # try to get a doc to attach the metadata to
429 if (defined $document_field) {
430 foreach my $pair (@$record_metadata) {
431 my ($field, $value) = (@$pair);
432 $field =~ s/^ex\.([^.]+)$/$1/; #remove any ex. iff it's the only metadata set prefix (will leave ex.dc.* intact)
433 $value =~ s/\\\\/\\/g; # don't regex brackets () here though!
434 my $document_file_full;
435
436 # Does this metadata element specify a document to obtain?
437 if ($field eq $document_field) {
438 if(-d $document_prefix && $document_prefix !~ m@^(http|ftp|https)://@ ) {
439 # if the document-prefix refers to a directory but not URL, ensure it has a file-separator at the end
440 # by first of all stripping any trailing slash and then always ensuring one is used through filename_cat
441 $document_prefix =~ s/(\/|\\)$//;
442 $document_file_full = &FileUtils::filenameConcatenate($document_prefix, "$value$document_suffix");
443 } else { # the doc prefix may also contain the prefix of the actual *filename* following the directory
444 $document_file_full = $document_prefix . $value . $document_suffix;
445 }
446
447 # this either downloads/copies the document, or creates a nul file for it.
448 $document_file = &obtain_document($document_file_full, $documents_directory, $orig_base_dir, $verbosity);
449 &write_metadata_xml_file_entry(METADATA_XML_FILE, $document_file, $record_metadata, $metadata_set);
450 }
451 }
452 }
453
454 # Create a dummy .nul file if we haven't obtained a document (or null file) for this record
455 if (not defined $document_file) {
456
457 my $doc_oid = $doc_obj->get_OID();
458
459 if (defined ($doc_oid)) {
460 $document_file = "$doc_oid.nul";
461 }
462 elsif (defined ($record_number)) {
463 $document_file = sprintf("%8.8d", $record_number) . ".nul";
464 }
465 else {
466 $document_file = "doc.nul";
467 }
468 open(DUMMY_FILE, ">$documents_directory/$document_file");
469 close(DUMMY_FILE);
470 &write_metadata_xml_file_entry(METADATA_XML_FILE, $document_file, $record_metadata, $metadata_set);
471 }
472
473}
474
475sub close_directory
476{
477 # Finish and close the metadata.xml file
478 print METADATA_XML_FILE "\n</DirectoryMetadata>\n";
479 close(METADATA_XML_FILE);
480
481}
482
483
484sub check_close_directory
485{
486 my ($record_number,$records_per_folder,$total_num_records) = @_;
487
488 if (($record_number % $records_per_folder) == 0 || $record_number == $total_num_records) {
489 # Finish and close the metadata.xml file
490 close_directory();
491 }
492}
493
494
495
496sub write_metadata_xml_file_entry
497{
498 my $metadata_xml_file = shift(@_);
499 my $file_name = shift(@_);
500 my $record_metadata = shift(@_);
501 my $meta_prefix = shift(@_);
502
503 # Make $file_name XML-safe
504 $file_name =~ s/&/&amp;/g;
505 $file_name =~ s/</&lt;/g;
506 $file_name =~ s/>/&gt;/g;
507
508 # Convert $file_name into a regular expression that matches it
509 $file_name =~ s/\./\\\./g;
510 $file_name =~ s/\(/\\\(/g;
511 $file_name =~ s/\)/\\\)/g;
512 $file_name =~ s/\{/\\\{/g;
513 $file_name =~ s/\}/\\\}/g;
514 $file_name =~ s/\[/\\\[/g;
515 $file_name =~ s/\]/\\\]/g;
516
517 print $metadata_xml_file
518 "\n" .
519 " <FileSet>\n" .
520 " <FileName>$file_name</FileName>\n" .
521 " <Description>\n";
522
523 foreach my $pair (@$record_metadata) {
524 my ($field, $value) = (@$pair);
525
526 # We're only interested in metadata from the database
527 next if ($field eq "lastmodified");
528 next if ($field eq "gsdlsourcefilename");
529 next if ($field eq "gsdldoctype");
530 next if ($field eq "FileFormat");
531
532 # Ignore the ^all metadata, since it will be invalid if the source metadata is changed
533 next if ($field =~ /\^all$/); # ISISPlug specific!
534
535 $field =~ s/^ex\.([^.]+)$/$1/; #remove any ex. iff it's the only metadata set prefix (will leave ex.dc.* intact)
536
537 # Square brackets in metadata values need to be escaped so they don't confuse Greenstone/GLI
538 $value =~ s/\[/&\#091;/g;
539 $value =~ s/\]/&\#093;/g;
540
541 # Make $value XML-safe
542 $value =~ s/&/&amp;/g; # May mess up existing entities!
543 $value =~ s/</&lt;/g;
544 $value =~ s/>/&gt;/g;
545
546 # we are not allowed & in xml except in entities.
547 # if there are undefined entities then parsing will also crap out.
548 # should we be checking for them too?
549 # this may not get all possibilities
550 # $value =~ s/&([^;\s]*(\s|$))/&amp;$1/g;
551
552 # do we already have a namespace specified?
553 my $full_field = $field;
554 if ($meta_prefix ne "") {
555 $full_field =~ s/^\w+\.//;
556 $full_field = $meta_prefix.$full_field;
557 }
558
559 print $metadata_xml_file " <Metadata mode=\"accumulate\" name=\"$full_field\">$value</Metadata>\n";
560 }
561
562 print $metadata_xml_file
563 " </Description>\n" .
564 " </FileSet>\n";
565}
566
567sub obtain_document
568{
569 my ($document_file_full,$documents_directory,$orig_base_dir,$verbosity) = @_;
570
571 print STDERR "Obtaining document file $document_file_full...\n" if ($verbosity > 1);
572
573 my $document_file_name;
574 my $local_document_file;
575
576 # Document specified is on the web
577 if ($document_file_full =~ /^https?:/ || $document_file_full =~ /^ftp:/) {
578 $document_file_full =~ /([^\/]+)$/;
579 $document_file_name = $1;
580 $local_document_file = &FileUtils::filenameConcatenate($documents_directory, $document_file_name);
581
582 # the wget binary is dependent on the gnomelib_env (particularly lib/libiconv2.dylib) being set, particularly on Mac Lions (android too?)
583 &util::set_gnomelib_env(); # this will set the gnomelib env once for each subshell launched, by first checking if GEXTGNOME is not already set
584
585 my $wget_options = "--quiet";
586 $wget_options = "--verbose" if ($verbosity > 2);
587 $wget_options .= " --timestamping"; # Only re-download files if they're newer
588 my $wget_command = "wget $wget_options \"$document_file_full\" --output-document \"$local_document_file\"";
589 `$wget_command`;
590
591 # Check the document was obtained successfully
592 if (!-e $local_document_file) {
593 print STDERR "WARNING: Could not obtain document file $document_file_full\n";
594 }
595 }
596 # Document specified is on the disk
597 else {
598 # convert the dirseps in filepath to correct dir sep for OS
599 $document_file_full = &FileUtils::filenameConcatenate($document_file_full);
600 my $dir_sep = &util::get_os_dirsep();
601
602 $document_file_full =~ m/(.+$dir_sep)?(.*)$/;
603 $document_file_name = $2;
604
605
606 my $is_absolute = File::Spec->file_name_is_absolute($document_file_full);
607 print STDERR "doc file full = $document_file_full\n";
608
609 if (!$is_absolute) {
610 $document_file_full
611 = &FileUtils::filenameConcatenate($orig_base_dir,$document_file_full);
612 }
613
614 $local_document_file = &FileUtils::filenameConcatenate($documents_directory, $document_file_name);
615
616 if (-e $document_file_full) {
617 &FileUtils::copyFiles($document_file_full, $documents_directory);
618 }
619
620 # Check the document was obtained successfully
621 if (!-e $local_document_file) {
622 print STDERR "WARNING: Could not obtain document file $document_file_full\n";
623 }
624 else {
625 $orig_base_dir = &util::filename_to_regex($orig_base_dir); # escape windows style slashes for the regex below
626 if ($document_file_full =~ m/^$orig_base_dir.*/) {
627 # file local to metadata record
628 # => copy has been made successfully, so remove original
629 &FileUtils::removeFiles($document_file_full);
630 }
631 }
632 }
633
634 # If the document wasn't obtained successfully, create a .nul file for it
635 if (!-e $local_document_file) {
636 $document_file_name .= ".nul";
637 open(NULL_FILE, ">$local_document_file.nul");
638 close(NULL_FILE);
639 print STDERR "Creating a nul document $document_file_name\n";
640 }
641
642 return $document_file_name;
643}
644
645sub get_plugin_options {
646 my ($collectcfg, $plugin) = @_;
647
648 my $plugin_list = $collectcfg ->{'plugin'};
649
650 foreach my $pluginoptions (@$plugin_list) {
651 my $pluginname = shift @$pluginoptions;
652 next unless $pluginname eq $plugin;
653 map { $_ = "\"$_\""; } @$pluginoptions;
654 my $options = join (",", @$pluginoptions);
655 return $options;
656 }
657 return "";
658}
659
660&main(@ARGV);
661
Note: See TracBrowser for help on using the repository browser.