#!/usr/bin/perl -w
BEGIN {
die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins");
}
use strict;
no strict 'subs'; # allow barewords (eg STDERR) as function arguments
no strict 'refs'; # allow filehandles to be variables and vice versa
use encodings;
use printusage;
use parse2;
use colcfg;
use FileHandle;
use File::Spec;
use File::Basename;
my $unicode_list =
[ { 'name' => "auto",
'desc' => "{BasPlug.input_encoding.auto}" },
{ 'name' => "ascii",
'desc' => "{BasPlug.input_encoding.ascii}" },
{ 'name' => "utf8",
'desc' => "{BasPlug.input_encoding.utf8}" },
{ 'name' => "unicode",
'desc' => "{BasPlug.input_encoding.unicode}" } ];
my $e = $encodings::encodings;
foreach my $enc (sort {$e->{$a}->{'name'} cmp $e->{$b}->{'name'}} keys (%$e))
{
my $hashEncode =
{'name' => $enc,
'desc' => $e->{$enc}->{'name'}};
push(@{$unicode_list},$hashEncode);
}
my $arguments =
[
{ 'name' => "language",
'desc' => "{scripts.language}",
'type' => "string",
'reqd' => "no",
'hiddengli' => "yes" },
{ 'name' => "plugin",
'desc' => "{explode.plugin}",
'type' => "string",
'reqd' => "yes",
'hiddengli' => "yes"},
{ 'name' => "input_encoding",
'desc' => "{explode.encoding}",
'type' => "enum",
'deft' => "auto",
'list' => $unicode_list,
'reqd' => "no" },
{ 'name' => "metadata_set",
'desc' => "{explode.metadata_set}",
'type' => "string",
'reqd' => "no" },
{ 'name' => "document_field",
'desc' => "{explode.document_field}",
'type' => "string",
'reqd' => "no"},
{ 'name' => "document_prefix",
'desc' => "{explode.document_prefix}",
'type' => "string",
'reqd' => "no"},
{ 'name' => "document_suffix",
'desc' => "{explode.document_suffix}",
'type' => "string",
'reqd' => "no"},
{ 'name' => "records_per_folder",
'desc' => "{explode.records_per_folder}",
'type' => "int",
'range' => "0,",
'deft' => "100",
'reqd' => "no" },
{ 'name' => "plugin_options",
'desc' => "options for the plugin",
'type' => "string",
'reqd' => "no",
'modegli' => "3"},
{ 'name' => "collection",
'desc' => "collection for file",
'type' => "string",
'reqd' => "no",
'modegli' => "3"},
{ 'name' => "verbosity",
'desc' => "{import.verbosity}",
'type' => "int",
'range' => "0,",
'deft' => "1",
'reqd' => "no",
'modegli' => "4" },
{ 'name' => "xml",
'desc' => "",
'type' => "flag",
'reqd' => "no",
'hiddengli' => "yes" }
];
my $options = { 'name' => "explode_metadata_database.pl",
'desc' => "{explode.desc}",
'args' => $arguments };
sub main
{
my ($language, $input_encoding, $metadata_set, $plugin,
$document_field, $document_prefix, $document_suffix, $records_per_folder, $plugin_options, $collection, $verbosity);
my $xml = 0;
my $hashParsingResult = {};
# parse the options
my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$hashParsingResult,"allow_extra_options");
# If parse returns -1 then something has gone wrong
if ($intArgLeftinAfterParsing == -1)
{
&PrintUsage::print_txt_usage($options, "{explode.params}");
die "\n";
}
foreach my $strVariable (keys %$hashParsingResult)
{
eval "\$$strVariable = \$hashParsingResult->{\"\$strVariable\"}";
}
# If $language has been specified, load the appropriate resource bundle
# (Otherwise, the default resource bundle will be loaded automatically)
if ($language && $language =~ /\S/) {
&gsprintf::load_language_specific_resource_bundle($language);
}
if ($xml) {
&PrintUsage::print_xml_usage($options);
print "\n";
return;
}
# There should one arg left after parsing (the filename)
# Or the user may have specified -h, in which case we output the usage
if($intArgLeftinAfterParsing != 1 || (@ARGV && $ARGV[0] =~ /^\-+h/))
{
&PrintUsage::print_txt_usage($options, "{explode.params}");
die "\n";
}
# The metadata database filename is the first value that remains after the options have been parsed out
my $filename = $ARGV[0];
if (!defined $filename || $filename !~ /\w/) {
&PrintUsage::print_txt_usage($options, "{explode.params}");
print STDERR "You need to specify a filename";
die "\n";
}
# check that file exists
if (!-e $filename) {
print STDERR "File $filename doesn't exist...\n";
die "\n";
}
# check required options
if (!defined $plugin || $plugin !~ /\w/) {
&PrintUsage::print_txt_usage($options, "{explode.params}");
print STDERR "You need to specify a plugin";
die "\n";
}
# check metadata set
if (defined $metadata_set && $metadata_set =~ /\w/) {
$metadata_set .= ".";
} else {
$metadata_set = "";
}
if (defined $collection && $collection =~ /\w/) {
if (($collection = &colcfg::use_collection("", $collection, "")) eq "") {
print STDERR "Collection $collection does not exist\n";
die "\n";
}
}
my $plugobj;
require "$plugin.pm";
if (defined $plugin_options && $plugin_options =~ /\w/) {
my @options = split(/\s/, $plugin_options);
map { $_ = "\"$_\"" unless $_ =~ /^\"/; } @options;
$plugin_options = join (",", @options);
eval ("\$plugobj = new $plugin([], [$plugin_options])");
die "$@" if $@;
} else {
eval ("\$plugobj = new $plugin()");
die "$@" if $@;
}
# ...and initialize it
$plugobj->init($verbosity, "STDERR", "STDERR");
if ($input_encoding eq "auto") {
($language, $input_encoding) = $plugobj->textcat_get_language_encoding ($filename);
}
# Create a directory to store the document files...
my ($exploded_base_dir) = ($filename =~ /(.*)\.[^\.]+$/);
my $orig_base_dir = &File::Basename::dirname($filename);
my $split_exp = $plugobj->{'split_exp'};
if (defined $split_exp) {
# Read in file, and then split and process individual records
my $text = "";
# Use the plugin's read_file function to avoid duplicating code
$plugobj->read_file($filename, $input_encoding, undef, \$text);
# is there any text in the file??
die "\n" unless length($text);
# Split the text into records, using the plugin's split_exp
my @metadata_records = split(/$split_exp/, $text);
print STDERR "Number of records: " . scalar(@metadata_records) . "\n";
# Write the metadata from each record to the metadata.xml file
my $record_number = 1;
my $documents_directory;
foreach my $record_text (@metadata_records) {
# Check if we need to start a new directory for these records
check_need_new_directory($exploded_base_dir,$record_number,$records_per_folder,
\@metadata_records,\$documents_directory);
# Use the plugin's process function to avoid duplicating code
my $doc_obj = new doc($filename, "nonindexed_doc");
$plugobj->process(\$record_text, undef, undef, $filename, undef, $doc_obj, 0);
# Try to get a doc to attach the metadata to
# If no match found, create a dummy .nul file
attach_metadata_or_make_nul_doc($document_field, $doc_obj, $record_number,
$documents_directory, $orig_base_dir,
$document_prefix, $document_suffix, $metadata_set, $verbosity);
check_close_directory($record_number,$records_per_folder,\@metadata_records);
$record_number = $record_number + 1;
}
}
else {
# Call metadata_read to sets up associated metadata
my $pluginfo = undef;
my $metadata = {};
my $processor = undef;
my $maxdocs = undef;
my $gli = undef;
my $extrametakeys = {};
my $extrametadata = {};
$plugobj->metadata_read($pluginfo, "", $filename, $metadata,
$extrametakeys, $extrametadata, $processor, $maxdocs, $gli);
my $documents_directory = need_new_directory($exploded_base_dir);
# Attach metadata to object
# => use the plugin's extra_metadata function to avoid duplicating code
my $doc_obj = new doc($filename, "nonindexed_doc");
$plugobj->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
# Try to get a doc to attach the metadata to
# If no match found, create a dummy .nul file
attach_metadata_or_make_nul_doc($document_field, $doc_obj, undef,
$documents_directory, $orig_base_dir,
$document_prefix, $document_suffix, $metadata_set, $verbosity);
close_directory();
}
# Explode means just that: the original file is deleted
&util::rm($filename);
$plugobj->clean_up_after_exploding();
}
sub need_new_directory
{
my ($exploded_base_dir) = @_;
my $documents_directory = $exploded_base_dir;
if (-d $documents_directory) {
die "Error: document directory $documents_directory already exists (bailing).\n";
}
&util::mk_dir($documents_directory);
my $documents_metadata_xml_file = &util::filename_cat($documents_directory, "metadata.xml");
if (-e $documents_metadata_xml_file) {
die "Error: documents metadata.xml file $documents_metadata_xml_file already exists (bailing).\n";
}
# Start the metadata.xml file
open(METADATA_XML_FILE, ">$documents_metadata_xml_file");
print METADATA_XML_FILE
"\n" .
"\n" .
"\n";
return $documents_directory;
}
sub check_need_new_directory
{
my ($exploded_base_dir,$record_number, $records_per_folder,$metadata_records,
$documents_dir_ref) = @_;
# Check if we need to start a new directory for these records
if (($record_number % $records_per_folder) == 1) {
my $documents_directory = $exploded_base_dir;
if (scalar(@$metadata_records) > $records_per_folder) {
$documents_directory .= "." . sprintf("%8.8d", $record_number);
}
$$documents_dir_ref = need_new_directory($documents_directory);
}
}
sub attach_metadata_or_make_nul_doc
{
my ($document_field, $doc_obj, $record_number,
$documents_directory, $orig_base_dir,
$document_prefix, $document_suffix, $metadata_set, $verbosity) = @_;
my $record_metadata = $doc_obj->get_all_metadata($doc_obj->get_top_section());
my $document_file;
# try to get a doc to attach the metadata to
if (defined $document_field) {
foreach my $pair (@$record_metadata) {
my ($field, $value) = (@$pair);
$value =~ s/\\\\/\\/g;
# Does this metadata element specify a document to obtain?
if ($field eq $document_field) {
my $document_file_full = $document_prefix . $value . $document_suffix;
$document_file = &obtain_document($document_file_full, $documents_directory, $orig_base_dir, $verbosity);
&write_metadata_xml_file_entry(METADATA_XML_FILE, $document_file, $record_metadata, $metadata_set);
}
}
}
# Create a dummy .nul file if we haven't obtained any documents for this record
if (not defined $document_file) {
if (defined ($record_number)) {
$document_file = sprintf("%8.8d", $record_number) . ".nul";
}
else {
$document_file = "doc.nul";
}
open(DUMMY_FILE, ">$documents_directory/$document_file");
close(DUMMY_FILE);
&write_metadata_xml_file_entry(METADATA_XML_FILE, $document_file, $record_metadata, $metadata_set);
}
}
sub close_directory
{
# Finish and close the metadata.xml file
print METADATA_XML_FILE "\n\n";
close(METADATA_XML_FILE);
}
sub check_close_directory
{
my ($record_number,$records_per_folder,$metadata_records) = @_;
if (($record_number % $records_per_folder) == 0 || $record_number == scalar(@$metadata_records)) {
# Finish and close the metadata.xml file
close_directory();
}
}
sub write_metadata_xml_file_entry
{
my $metadata_xml_file = shift(@_);
my $file_name = shift(@_);
my $record_metadata = shift(@_);
my $meta_prefix = shift(@_);
# Make $file_name XML-safe
$file_name =~ s/&/&/g;
$file_name =~ s/</g;
$file_name =~ s/>/>/g;
# Convert $file_name into a regular expression that matches it
$file_name =~ s/\./\\\./g;
$file_name =~ s/\(/\\\(/g;
$file_name =~ s/\)/\\\)/g;
$file_name =~ s/\{/\\\{/g;
$file_name =~ s/\}/\\\}/g;
$file_name =~ s/\[/\\\[/g;
$file_name =~ s/\]/\\\]/g;
print $metadata_xml_file
"\n" .
" \n" .
" $file_name\n" .
" \n";
foreach my $pair (@$record_metadata) {
my ($field, $value) = (@$pair);
# We're only interested in metadata from the database
next if ($field eq "lastmodified");
next if ($field eq "gsdlsourcefilename");
next if ($field eq "gsdldoctype");
next if ($field eq "FileFormat");
# Ignore the ^all metadata, since it will be invalid if the source metadata is changed
next if ($field =~ /\^all$/); # ISISPlug specific!
# Make $value XML-safe
$value =~ s/&/&/g; # May mess up existing entities!
$value =~ s/</g;
$value =~ s/>/>/g;
# we are not allowed & in xml except in entities.
# if there are undefined entities then parsing will also crap out.
# should we be checking for them too?
# this may not get all possibilities
# $value =~ s/&([^;\s]*(\s|$))/&$1/g;
print $metadata_xml_file " $value\n";
}
print $metadata_xml_file
" \n" .
" \n";
}
sub obtain_document
{
my ($document_file_full,$documents_directory,$orig_base_dir,$verbosity) = @_;
print STDERR "Obtaining document file $document_file_full...\n" if ($verbosity > 1);
my $document_file_name;
my $local_document_file;
# Document specified is on the web
if ($document_file_full =~ /^https?:/ || $document_file_full =~ /^ftp:/) {
$document_file_full =~ /([^\/]+)$/;
$document_file_name = $1;
$local_document_file = &util::filename_cat($documents_directory, $document_file_name);
my $wget_options = "--quiet";
$wget_options = "--verbose" if ($verbosity > 2);
$wget_options .= " --timestamping"; # Only re-download files if they're newer
my $wget_command = "wget $wget_options \"$document_file_full\" --output-document \"$local_document_file\"";
`$wget_command`;
# Check the document was obtained successfully
if (!-e $local_document_file) {
print STDERR "WARNING: Could not obtain document file $document_file_full\n";
}
}
# Document specified is on the disk
else {
my $dir_sep = &util::get_os_dirsep();
$document_file_full =~ m/(.+$dir_sep)?(.*)$/;
$document_file_name = $2;
my $is_absolute = File::Spec->file_name_is_absolute($document_file_full);
print STDERR "doc file full = $document_file_full\n";
if (!$is_absolute) {
$document_file_full
= &util::filename_cat($orig_base_dir,$document_file_full);
}
$local_document_file = &util::filename_cat($documents_directory, $document_file_name);
&util::cp($document_file_full, $documents_directory);
# Check the document was obtained successfully
if (!-e $local_document_file) {
print STDERR "WARNING: Could not obtain document file $document_file_full\n";
}
else {
if ($document_file_full =~ m/^$orig_base_dir.*/) {
# file local to metadata record
# => copy has been made successfully, so remove original
&util::rm($document_file_full);
}
}
}
# If the document wasn't obtained successfully, create a .nul file for it
if (!-e $local_document_file) {
$document_file_name .= ".nul";
open(NULL_FILE, ">$local_document_file.nul");
close(NULL_FILE);
}
return $document_file_name;
}
&main(@ARGV);