source: trunk/gsdl/bin/script/explode_metadata_database.pl@ 11350

Last change on this file since 11350 was 11350, checked in by mdewsnip, 18 years ago

Removed the "-filename_field" option from explode_metadata_database.pl. A similar effect can now be achieved using the "-document_field" option.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 10.6 KB
Line 
1#!/usr/bin/perl
2
3
4BEGIN {
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
10use strict;
11no strict 'subs'; # allow barewords (eg STDERR) as function arguments
12no strict 'refs'; # allow filehandles to be variables and vice versa
13
14use encodings;
15use printusage;
16use parse2;
17
18my $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}" } ];
27
28my $e = $encodings::encodings;
29foreach 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
38my $arguments =
39 [
40 { 'name' => "language",
41 'desc' => "{scripts.language}",
42 'type' => "string",
43 'reqd' => "no",
44 'hiddengli' => "yes" },
45 { 'name' => "plugin",
46 'desc' => "{explode.plugin}",
47 'type' => "string",
48 'reqd' => "yes",
49 'hiddengli' => "yes"},
50 { 'name' => "input_encoding",
51 'desc' => "{explode.encoding}",
52 'type' => "enum",
53 'deft' => "auto",
54 'list' => $unicode_list,
55 'reqd' => "no" },
56 { 'name' => "metadata_set",
57 'desc' => "{explode.metadata_set}",
58 'type' => "string",
59 'reqd' => "no" },
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"},
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" }
84 ];
85
86my $options = { 'name' => "explode_metadata_database.pl",
87 'desc' => "{explode.desc}",
88 'args' => $arguments };
89
90
91sub main
92{
93 my ($language, $input_encoding, $metadata_set, $plugin,
94 $document_field, $document_prefix, $document_suffix, $verbosity);
95
96 my $xml = 0;
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 }
107
108 # If $language has been specified, load the appropriate resource bundle
109 # (Otherwise, the default resource bundle will be loaded automatically)
110 if ($language && $language =~ /\S/) {
111 &gsprintf::load_language_specific_resource_bundle($language);
112 }
113
114 if ($xml) {
115 &PrintUsage::print_xml_usage($options);
116 print "\n";
117 return;
118 }
119
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
127 # The metadata database filename is the first value that remains after the options have been parsed out
128 my $filename = $ARGV[0];
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 }
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");
160
161 if ($input_encoding eq "auto") {
162 $plugobj->{'input_encoding'} = $input_encoding;
163 ($language, $input_encoding) = $plugobj->textcat_get_language_encoding ($filename);
164 }
165 my $text = "";
166 # Use the plugin's read_file function to avoid duplicating code
167 $plugobj->read_file($filename, $input_encoding, undef, \$text);
168
169 # Create a directory to store the document files...
170 my ($documents_directory) = ($filename =~ /(.*)\.[^\.]+$/);
171 if (-d $documents_directory) {
172 die "Error: document directory $documents_directory already exists (bailing).\n";
173 }
174 &util::mk_dir($documents_directory);
175
176 # ...and a metadata.xml file for the document metadata (extracted from the database)
177 my $documents_metadata_xml_file = &util::filename_cat($documents_directory, "metadata.xml");
178 if (-e $documents_metadata_xml_file) {
179 die "Error: document metadata.xml file $documents_metadata_xml_file already exists (bailing).\n";
180 }
181
182 # Start the metadata.xml file
183 open(METADATA_XML_FILE, ">$documents_metadata_xml_file");
184 print METADATA_XML_FILE
185 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\n" .
186 "<!DOCTYPE DirectoryMetadata SYSTEM \"http://greenstone.org/dtd/DirectoryMetadata/1.0/DirectoryMetadata.dtd\">\n" .
187 "<DirectoryMetadata>\n";
188
189 # Split the text into records, using the plugin's split_exp
190 my $split_exp = $plugobj->{'split_exp'};
191 my @metadata_records = split(/$split_exp/, $text);
192 print STDERR "Number of records: " . @metadata_records . "\n";
193
194 # Write the metadata from each record to the metadata.xml file
195 my $record_number = 0;
196 foreach my $record_text (@metadata_records) {
197
198 # Use the plugin's process function to avoid duplicating code
199 my $doc_obj = new doc($filename, "nonindexed_doc");
200 $plugobj->process(\$record_text, undef, undef, $filename, undef, $doc_obj, 0);
201 # Get all the metadata assigned to this record
202 my $record_metadata = $doc_obj->get_all_metadata($doc_obj->get_top_section());
203 my $document_file;
204
205 # try to get a doc to attach the metadata to
206 if (defined $document_field) {
207 foreach my $pair (@$record_metadata) {
208 my ($field, $value) = (@$pair);
209
210 # Does this metadata element specify a document to obtain?
211 if ($field eq $document_field) {
212 my $document_file_full = $document_prefix . $value . $document_suffix;
213 $document_file = &obtain_document($document_file_full, $documents_directory, $verbosity);
214 }
215 }
216 }
217 # do we need to create a dummy doc??
218 if (not defined $document_file) {
219 $record_number = $record_number + 1;
220 $document_file = sprintf("%4.4d", $record_number) . ".nul";
221
222 open(DUMMY_FILE, ">$documents_directory/$document_file");
223 close(DUMMY_FILE);
224 }
225
226 &write_metadata_xml_file_entry(METADATA_XML_FILE, $document_file, $record_metadata, $metadata_set);
227 }
228
229 # Finish and close the metadata.xml file
230 print METADATA_XML_FILE "\n</DirectoryMetadata>\n";
231 close(METADATA_XML_FILE);
232
233 # Explode means just that: the original file is deleted
234 &util::rm($filename);
235 $plugobj->clean_up_after_exploding();
236}
237
238
239sub write_metadata_xml_file_entry
240{
241 my $metadata_xml_file = shift(@_);
242 my $file_name = shift(@_);
243 my $record_metadata = shift(@_);
244 my $meta_prefix = shift(@_);
245
246 # Make $file_name XML-safe
247 $file_name =~ s/</&lt;/g;
248 $file_name =~ s/>/&gt;/g;
249
250 # Convert $file_name into a regular expression that matches it
251 $file_name =~ s/\./\\\./g;
252 $file_name =~ s/\(/\\\(/g;
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
259 print $metadata_xml_file
260 "\n" .
261 " <FileSet>\n" .
262 " <FileName>$file_name</FileName>\n" .
263 " <Description>\n";
264
265 foreach my $pair (@$record_metadata) {
266 my ($field, $value) = (@$pair);
267
268 # We're only interested in metadata from the database
269 next if ($field eq "lastmodified");
270 next if ($field eq "gsdlsourcefilename");
271 next if ($field eq "gsdldoctype");
272 next if ($field eq "FileFormat");
273
274 # Ignore the ^all metadata, since it will be invalid if the source metadata is changed
275 next if ($field =~ /\^all$/); # ISISPlug specific!
276
277 # Make $value XML-safe
278 $value =~ s/&/&amp;/g; # May mess up existing entities!
279 $value =~ s/</&lt;/g;
280 $value =~ s/>/&gt;/g;
281
282 # we are not allowed & in xml except in entities.
283 # if there are undefined entities then parsing will also crap out.
284 # should we be checking for them too?
285 # this may not get all possibilities
286 # $value =~ s/&([^;\s]*(\s|$))/&amp;$1/g;
287
288 print $metadata_xml_file " <Metadata mode=\"accumulate\" name=\"$meta_prefix$field\">$value</Metadata>\n";
289 }
290
291 print $metadata_xml_file
292 " </Description>\n" .
293 " </FileSet>\n";
294}
295
296sub obtain_document
297{
298 my $document_file_full = shift(@_);
299 my $documents_directory = shift(@_);
300 my $verbosity = shift(@_);
301
302 print STDERR "Obtaining document file $document_file_full...\n" if ($verbosity > 1);
303
304 my $document_file_name;
305 my $local_document_file;
306
307 # Document specified is on the web
308 if ($document_file_full =~ /^http:/ || $document_file_full =~ /^ftp:/) {
309 $document_file_full =~ /([^\/]+)$/;
310 $document_file_name = $1;
311 $local_document_file = &util::filename_cat($documents_directory, $document_file_name);
312
313 my $wget_options = "--quiet";
314 $wget_options = "--verbose" if ($verbosity > 2);
315 $wget_options .= " --timestamping"; # Only re-download files if they're newer
316 `wget $wget_options $document_file_full --output-document $local_document_file`;
317
318 # Check the document was obtained successfully
319 if (!-e $local_document_file) {
320 print STDERR "WARNING: Could not obtain document file $document_file_full\n";
321 }
322 }
323 # Document specified is on the disk
324 else {
325 my $dir_sep = &util::get_os_dirsep();
326 $document_file_full =~ /(.+$dir_sep)?(.*)$/;
327 $document_file_name = $2;
328 $local_document_file = &util::filename_cat($documents_directory, $document_file_name);
329
330 # Only bother trying to copy the file if it contained some path information
331 if ($document_file_full ne $document_file_name) {
332 &util::cp($document_file_full, $documents_directory);
333
334 # Check the document was obtained successfully
335 if (!-e $local_document_file) {
336 print STDERR "WARNING: Could not obtain document file $document_file_full\n";
337 }
338 }
339 }
340
341 # If the document wasn't obtained successfully, create a .nul file for it
342 if (!-e $local_document_file) {
343 $document_file_name .= ".nul";
344 open(NULL_FILE, ">$local_document_file.nul");
345 close(NULL_FILE);
346 }
347
348 return $document_file_name;
349}
350
351&main(@ARGV);
Note: See TracBrowser for help on using the repository browser.