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