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