[8858] | 1 | #!/usr/bin/perl
|
---|
| 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;
|
---|
[8858] | 17 |
|
---|
[9121] | 18 | my $unicode_list =
|
---|
| 19 | [ { 'name' => "auto",
|
---|
| 20 | 'desc' => "{BasPlug.input_encoding.auto}" },
|
---|
| 21 | { 'name' => "ascii",
|
---|
| 22 | 'desc' => "{BasPlug.input_encoding.ascii}" },
|
---|
| 23 | { 'name' => "utf8",
|
---|
| 24 | 'desc' => "{BasPlug.input_encoding.utf8}" },
|
---|
| 25 | { 'name' => "unicode",
|
---|
| 26 | 'desc' => "{BasPlug.input_encoding.unicode}" } ];
|
---|
[8858] | 27 |
|
---|
[10999] | 28 | my $e = $encodings::encodings;
|
---|
| 29 | foreach my $enc (sort {$e->{$a}->{'name'} cmp $e->{$b}->{'name'}} keys (%$e))
|
---|
| 30 | {
|
---|
| 31 | my $hashEncode =
|
---|
| 32 | {'name' => $enc,
|
---|
| 33 | 'desc' => $e->{$enc}->{'name'}};
|
---|
| 34 |
|
---|
| 35 | push(@{$unicode_list},$hashEncode);
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[9041] | 38 | my $arguments =
|
---|
[10338] | 39 | [
|
---|
[10470] | 40 | { 'name' => "language",
|
---|
| 41 | 'desc' => "{scripts.language}",
|
---|
| 42 | 'type' => "string",
|
---|
[10471] | 43 | 'reqd' => "no",
|
---|
| 44 | 'hiddengli' => "yes" },
|
---|
[10338] | 45 | { 'name' => "plugin",
|
---|
| 46 | 'desc' => "{explode.plugin}",
|
---|
| 47 | 'type' => "string",
|
---|
| 48 | 'reqd' => "yes",
|
---|
| 49 | 'hiddengli' => "yes"},
|
---|
| 50 | { 'name' => "input_encoding",
|
---|
[9041] | 51 | 'desc' => "{explode.encoding}",
|
---|
[9121] | 52 | 'type' => "enum",
|
---|
[9147] | 53 | 'deft' => "auto",
|
---|
[9121] | 54 | 'list' => $unicode_list,
|
---|
[9147] | 55 | 'reqd' => "no" },
|
---|
[9041] | 56 | { 'name' => "metadata_set",
|
---|
| 57 | 'desc' => "{explode.metadata_set}",
|
---|
| 58 | 'type' => "string",
|
---|
[11342] | 59 | 'reqd' => "no" },
|
---|
[9147] | 60 | { 'name' => "document_field",
|
---|
| 61 | 'desc' => "{explode.document_field}",
|
---|
| 62 | 'type' => "string",
|
---|
| 63 | 'reqd' => "no"},
|
---|
| 64 | { 'name' => "document_prefix",
|
---|
| 65 | 'desc' => "{explode.document_prefix}",
|
---|
| 66 | 'type' => "string",
|
---|
| 67 | 'reqd' => "no"},
|
---|
| 68 | { 'name' => "document_suffix",
|
---|
| 69 | 'desc' => "{explode.document_suffix}",
|
---|
| 70 | 'type' => "string",
|
---|
| 71 | 'reqd' => "no"},
|
---|
[10338] | 72 | { 'name' => "verbosity",
|
---|
| 73 | 'desc' => "{import.verbosity}",
|
---|
| 74 | 'type' => "int",
|
---|
| 75 | 'range' => "0,",
|
---|
| 76 | 'deft' => "1",
|
---|
| 77 | 'reqd' => "no",
|
---|
| 78 | 'modegli' => "4" },
|
---|
| 79 | { 'name' => "xml",
|
---|
| 80 | 'desc' => "",
|
---|
| 81 | 'type' => "flag",
|
---|
| 82 | 'reqd' => "no",
|
---|
| 83 | 'hiddengli' => "yes" }
|
---|
[9041] | 84 | ];
|
---|
| 85 |
|
---|
| 86 | my $options = { 'name' => "explode_metadata_database.pl",
|
---|
| 87 | 'desc' => "{explode.desc}",
|
---|
| 88 | 'args' => $arguments };
|
---|
[8858] | 89 |
|
---|
[9041] | 90 |
|
---|
[8858] | 91 | sub main
|
---|
| 92 | {
|
---|
[11350] | 93 | my ($language, $input_encoding, $metadata_set, $plugin,
|
---|
[10338] | 94 | $document_field, $document_prefix, $document_suffix, $verbosity);
|
---|
[9041] | 95 |
|
---|
[9147] | 96 | my $xml = 0;
|
---|
[10338] | 97 |
|
---|
| 98 | my $hashParsingResult = {};
|
---|
| 99 | my $blnParseFailed = "false";
|
---|
| 100 | # parse the options
|
---|
| 101 | my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
|
---|
| 102 |
|
---|
| 103 | foreach my $strVariable (keys %$hashParsingResult)
|
---|
| 104 | {
|
---|
| 105 | eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
|
---|
| 106 | }
|
---|
[10470] | 107 |
|
---|
[9147] | 108 | # If $language has been specified, load the appropriate resource bundle
|
---|
| 109 | # (Otherwise, the default resource bundle will be loaded automatically)
|
---|
[10338] | 110 | if ($language && $language =~ /\S/) {
|
---|
[9147] | 111 | &gsprintf::load_language_specific_resource_bundle($language);
|
---|
| 112 | }
|
---|
[8858] | 113 |
|
---|
[9147] | 114 | if ($xml) {
|
---|
| 115 | &PrintUsage::print_xml_usage($options);
|
---|
| 116 | print "\n";
|
---|
| 117 | return;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[10470] | 120 | # There should one arg left after parsing
|
---|
| 121 | if($intArgLeftinAfterParsing > 1)
|
---|
| 122 | {
|
---|
| 123 | &PrintUsage::print_txt_usage($options, "{explode.params}");
|
---|
| 124 | die "\n";
|
---|
| 125 | }
|
---|
| 126 |
|
---|
[8858] | 127 | # The metadata database filename is the first value that remains after the options have been parsed out
|
---|
| 128 | my $filename = $ARGV[0];
|
---|
[9041] | 129 | if (!defined $filename || $filename !~ /\w/) {
|
---|
| 130 | &PrintUsage::print_txt_usage($options, "{explode.params}");
|
---|
| 131 | print STDERR "You need to specify a filename";
|
---|
| 132 | die "\n";
|
---|
| 133 | }
|
---|
| 134 | # check that file exists
|
---|
| 135 | if (!-e $filename) {
|
---|
| 136 | print STDERR "File $filename doesn't exist...\n";
|
---|
| 137 | die "\n";
|
---|
| 138 | }
|
---|
| 139 | # check required options
|
---|
| 140 | if (!defined $plugin || $plugin !~ /\w/) {
|
---|
| 141 | &PrintUsage::print_txt_usage($options, "{explode.params}");
|
---|
| 142 | print STDERR "You need to specify a plugin";
|
---|
| 143 | die "\n";
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | # check metadata set
|
---|
| 147 | if (defined $metadata_set && $metadata_set =~ /\w/) {
|
---|
| 148 | $metadata_set .= ".";
|
---|
| 149 | } else {
|
---|
| 150 | $metadata_set = "";
|
---|
| 151 | }
|
---|
[8858] | 152 |
|
---|
| 153 | my $plugobj;
|
---|
| 154 | require "$plugin.pm";
|
---|
| 155 | eval ("\$plugobj = new $plugin()");
|
---|
| 156 | die "$@" if $@;
|
---|
| 157 |
|
---|
| 158 | # ...and initialize it
|
---|
| 159 | $plugobj->init(1, "STDERR", "STDERR");
|
---|
[9121] | 160 |
|
---|
[10338] | 161 | if ($input_encoding eq "auto") {
|
---|
| 162 | $plugobj->{'input_encoding'} = $input_encoding;
|
---|
| 163 | ($language, $input_encoding) = $plugobj->textcat_get_language_encoding ($filename);
|
---|
[9121] | 164 | }
|
---|
[10338] | 165 | my $text = "";
|
---|
[8858] | 166 | # Use the plugin's read_file function to avoid duplicating code
|
---|
[10338] | 167 | $plugobj->read_file($filename, $input_encoding, undef, \$text);
|
---|
[8858] | 168 |
|
---|
[11442] | 169 | # is there any text in the file??
|
---|
| 170 | die "\n" unless length($text);
|
---|
[8858] | 171 | # Create a directory to store the document files...
|
---|
| 172 | my ($documents_directory) = ($filename =~ /(.*)\.[^\.]+$/);
|
---|
| 173 | if (-d $documents_directory) {
|
---|
| 174 | die "Error: document directory $documents_directory already exists (bailing).\n";
|
---|
| 175 | }
|
---|
| 176 | &util::mk_dir($documents_directory);
|
---|
| 177 |
|
---|
| 178 | # ...and a metadata.xml file for the document metadata (extracted from the database)
|
---|
[10338] | 179 | my $documents_metadata_xml_file = &util::filename_cat($documents_directory, "metadata.xml");
|
---|
[8858] | 180 | if (-e $documents_metadata_xml_file) {
|
---|
| 181 | die "Error: document metadata.xml file $documents_metadata_xml_file already exists (bailing).\n";
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | # Start the metadata.xml file
|
---|
| 185 | open(METADATA_XML_FILE, ">$documents_metadata_xml_file");
|
---|
| 186 | print METADATA_XML_FILE
|
---|
| 187 | "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" .
|
---|
| 188 | "<!DOCTYPE DirectoryMetadata SYSTEM \"http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd\">\n" .
|
---|
| 189 | "<DirectoryMetadata>\n";
|
---|
| 190 |
|
---|
| 191 | # Split the text into records, using the plugin's split_exp
|
---|
| 192 | my $split_exp = $plugobj->{'split_exp'};
|
---|
| 193 | my @metadata_records = split(/$split_exp/, $text);
|
---|
| 194 | print STDERR "Number of records: " . @metadata_records . "\n";
|
---|
| 195 |
|
---|
| 196 | # Write the metadata from each record to the metadata.xml file
|
---|
| 197 | my $record_number = 0;
|
---|
[10338] | 198 | foreach my $record_text (@metadata_records) {
|
---|
[9041] | 199 |
|
---|
[8858] | 200 | # Use the plugin's process function to avoid duplicating code
|
---|
| 201 | my $doc_obj = new doc($filename, "nonindexed_doc");
|
---|
| 202 | $plugobj->process(\$record_text, undef, undef, $filename, undef, $doc_obj, 0);
|
---|
[9147] | 203 | # Get all the metadata assigned to this record
|
---|
| 204 | my $record_metadata = $doc_obj->get_all_metadata($doc_obj->get_top_section());
|
---|
[9041] | 205 | my $document_file;
|
---|
[9147] | 206 |
|
---|
| 207 | # try to get a doc to attach the metadata to
|
---|
| 208 | if (defined $document_field) {
|
---|
| 209 | foreach my $pair (@$record_metadata) {
|
---|
| 210 | my ($field, $value) = (@$pair);
|
---|
| 211 |
|
---|
| 212 | # Does this metadata element specify a document to obtain?
|
---|
| 213 | if ($field eq $document_field) {
|
---|
| 214 | my $document_file_full = $document_prefix . $value . $document_suffix;
|
---|
[10338] | 215 | $document_file = &obtain_document($document_file_full, $documents_directory, $verbosity);
|
---|
[9041] | 216 | }
|
---|
[9147] | 217 | }
|
---|
| 218 | }
|
---|
| 219 | # do we need to create a dummy doc??
|
---|
| 220 | if (not defined $document_file) {
|
---|
[11350] | 221 | $record_number = $record_number + 1;
|
---|
[11706] | 222 | $document_file = sprintf("%8.8d", $record_number) . ".nul";
|
---|
[11302] | 223 |
|
---|
[9147] | 224 | open(DUMMY_FILE, ">$documents_directory/$document_file");
|
---|
| 225 | close(DUMMY_FILE);
|
---|
[9041] | 226 | }
|
---|
[8858] | 227 |
|
---|
[9041] | 228 | &write_metadata_xml_file_entry(METADATA_XML_FILE, $document_file, $record_metadata, $metadata_set);
|
---|
[8858] | 229 | }
|
---|
| 230 |
|
---|
| 231 | # Finish and close the metadata.xml file
|
---|
| 232 | print METADATA_XML_FILE "\n</DirectoryMetadata>\n";
|
---|
| 233 | close(METADATA_XML_FILE);
|
---|
| 234 |
|
---|
| 235 | # Explode means just that: the original file is deleted
|
---|
[9147] | 236 | &util::rm($filename);
|
---|
[11332] | 237 | $plugobj->clean_up_after_exploding();
|
---|
[8858] | 238 | }
|
---|
| 239 |
|
---|
| 240 |
|
---|
| 241 | sub write_metadata_xml_file_entry
|
---|
| 242 | {
|
---|
| 243 | my $metadata_xml_file = shift(@_);
|
---|
| 244 | my $file_name = shift(@_);
|
---|
| 245 | my $record_metadata = shift(@_);
|
---|
[9041] | 246 | my $meta_prefix = shift(@_);
|
---|
| 247 |
|
---|
[8858] | 248 | # Make $file_name XML-safe
|
---|
| 249 | $file_name =~ s/</</g;
|
---|
| 250 | $file_name =~ s/>/>/g;
|
---|
| 251 |
|
---|
| 252 | # Convert $file_name into a regular expression that matches it
|
---|
| 253 | $file_name =~ s/\./\\\./g;
|
---|
| 254 | $file_name =~ s/\(/\\\(/g;
|
---|
| 255 | $file_name =~ s/\)/\\\)/g;
|
---|
| 256 | $file_name =~ s/\{/\\\{/g;
|
---|
| 257 | $file_name =~ s/\}/\\\}/g;
|
---|
| 258 | $file_name =~ s/\[/\\\[/g;
|
---|
| 259 | $file_name =~ s/\]/\\\]/g;
|
---|
[9119] | 260 |
|
---|
[8858] | 261 | print $metadata_xml_file
|
---|
| 262 | "\n" .
|
---|
| 263 | " <FileSet>\n" .
|
---|
| 264 | " <FileName>$file_name</FileName>\n" .
|
---|
| 265 | " <Description>\n";
|
---|
| 266 |
|
---|
| 267 | foreach my $pair (@$record_metadata) {
|
---|
| 268 | my ($field, $value) = (@$pair);
|
---|
| 269 |
|
---|
| 270 | # We're only interested in metadata from the database
|
---|
| 271 | next if ($field eq "lastmodified");
|
---|
| 272 | next if ($field eq "gsdlsourcefilename");
|
---|
| 273 | next if ($field eq "gsdldoctype");
|
---|
| 274 | next if ($field eq "FileFormat");
|
---|
| 275 |
|
---|
| 276 | # Ignore the ^all metadata, since it will be invalid if the source metadata is changed
|
---|
| 277 | next if ($field =~ /\^all$/); # ISISPlug specific!
|
---|
| 278 |
|
---|
| 279 | # Make $value XML-safe
|
---|
[11238] | 280 | $value =~ s/&/&/g; # May mess up existing entities!
|
---|
[8858] | 281 | $value =~ s/</</g;
|
---|
| 282 | $value =~ s/>/>/g;
|
---|
| 283 |
|
---|
[9119] | 284 | # we are not allowed & in xml except in entities.
|
---|
| 285 | # if there are undefined entities then parsing will also crap out.
|
---|
| 286 | # should we be checking for them too?
|
---|
| 287 | # this may not get all possibilities
|
---|
[11238] | 288 | # $value =~ s/&([^;\s]*(\s|$))/&$1/g;
|
---|
[9119] | 289 |
|
---|
[9041] | 290 | print $metadata_xml_file " <Metadata mode=\"accumulate\" name=\"$meta_prefix$field\">$value</Metadata>\n";
|
---|
[8858] | 291 | }
|
---|
| 292 |
|
---|
| 293 | print $metadata_xml_file
|
---|
| 294 | " </Description>\n" .
|
---|
| 295 | " </FileSet>\n";
|
---|
| 296 | }
|
---|
| 297 |
|
---|
[9147] | 298 | sub obtain_document
|
---|
| 299 | {
|
---|
| 300 | my $document_file_full = shift(@_);
|
---|
| 301 | my $documents_directory = shift(@_);
|
---|
[10338] | 302 | my $verbosity = shift(@_);
|
---|
| 303 |
|
---|
| 304 | print STDERR "Obtaining document file $document_file_full...\n" if ($verbosity > 1);
|
---|
[8858] | 305 |
|
---|
[9147] | 306 | my $document_file_name;
|
---|
| 307 | my $local_document_file;
|
---|
| 308 |
|
---|
| 309 | # Document specified is on the web
|
---|
| 310 | if ($document_file_full =~ /^http:/ || $document_file_full =~ /^ftp:/) {
|
---|
| 311 | $document_file_full =~ /([^\/]+)$/;
|
---|
| 312 | $document_file_name = $1;
|
---|
| 313 | $local_document_file = &util::filename_cat($documents_directory, $document_file_name);
|
---|
| 314 |
|
---|
| 315 | my $wget_options = "--quiet";
|
---|
[10338] | 316 | $wget_options = "--verbose" if ($verbosity > 2);
|
---|
[9147] | 317 | $wget_options .= " --timestamping"; # Only re-download files if they're newer
|
---|
| 318 | `wget $wget_options $document_file_full --output-document $local_document_file`;
|
---|
[11348] | 319 |
|
---|
| 320 | # Check the document was obtained successfully
|
---|
| 321 | if (!-e $local_document_file) {
|
---|
| 322 | print STDERR "WARNING: Could not obtain document file $document_file_full\n";
|
---|
| 323 | }
|
---|
[9147] | 324 | }
|
---|
| 325 | # Document specified is on the disk
|
---|
| 326 | else {
|
---|
| 327 | my $dir_sep = &util::get_os_dirsep();
|
---|
| 328 | $document_file_full =~ /(.+$dir_sep)?(.*)$/;
|
---|
| 329 | $document_file_name = $2;
|
---|
| 330 | $local_document_file = &util::filename_cat($documents_directory, $document_file_name);
|
---|
| 331 |
|
---|
[11348] | 332 | # Only bother trying to copy the file if it contained some path information
|
---|
| 333 | if ($document_file_full ne $document_file_name) {
|
---|
| 334 | &util::cp($document_file_full, $documents_directory);
|
---|
| 335 |
|
---|
| 336 | # Check the document was obtained successfully
|
---|
| 337 | if (!-e $local_document_file) {
|
---|
| 338 | print STDERR "WARNING: Could not obtain document file $document_file_full\n";
|
---|
| 339 | }
|
---|
| 340 | }
|
---|
[9147] | 341 | }
|
---|
| 342 |
|
---|
[11348] | 343 | # If the document wasn't obtained successfully, create a .nul file for it
|
---|
[9147] | 344 | if (!-e $local_document_file) {
|
---|
[11302] | 345 | $document_file_name .= ".nul";
|
---|
| 346 | open(NULL_FILE, ">$local_document_file.nul");
|
---|
| 347 | close(NULL_FILE);
|
---|
[9147] | 348 | }
|
---|
| 349 |
|
---|
| 350 | return $document_file_name;
|
---|
| 351 | }
|
---|
| 352 |
|
---|
[8858] | 353 | &main(@ARGV);
|
---|