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