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

Last change on this file since 12656 was 12545, checked in by kjdon, 18 years ago

changed parse2::parse so that it returns -1 on error, 0 on success, or if allow_extra_options is specified, then on success returns the number of args left over. allowed the use of -h to print the usage for all scripts

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