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

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

had to add 'use FileHandle' to this file - was getting an error about can't locate method ungetc in IO::Handle. Once this use FIleHandle was added, then the complaint went away.

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