source: branches/gsdl-2_70-distribution-branch/gsdl/bin/script/explode_metadata_database.pl@ 11706

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

null files names are now 8 digits long

  • 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;
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 # is there any text in the file??
170 die "\n" unless length($text);
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)
179 my $documents_metadata_xml_file = &util::filename_cat($documents_directory, "metadata.xml");
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;
198 foreach my $record_text (@metadata_records) {
199
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);
203 # Get all the metadata assigned to this record
204 my $record_metadata = $doc_obj->get_all_metadata($doc_obj->get_top_section());
205 my $document_file;
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;
215 $document_file = &obtain_document($document_file_full, $documents_directory, $verbosity);
216 }
217 }
218 }
219 # do we need to create a dummy doc??
220 if (not defined $document_file) {
221 $record_number = $record_number + 1;
222 $document_file = sprintf("%8.8d", $record_number) . ".nul";
223
224 open(DUMMY_FILE, ">$documents_directory/$document_file");
225 close(DUMMY_FILE);
226 }
227
228 &write_metadata_xml_file_entry(METADATA_XML_FILE, $document_file, $record_metadata, $metadata_set);
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
236 &util::rm($filename);
237 $plugobj->clean_up_after_exploding();
238}
239
240
241sub write_metadata_xml_file_entry
242{
243 my $metadata_xml_file = shift(@_);
244 my $file_name = shift(@_);
245 my $record_metadata = shift(@_);
246 my $meta_prefix = shift(@_);
247
248 # Make $file_name XML-safe
249 $file_name =~ s/</&lt;/g;
250 $file_name =~ s/>/&gt;/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;
260
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
280 $value =~ s/&/&amp;/g; # May mess up existing entities!
281 $value =~ s/</&lt;/g;
282 $value =~ s/>/&gt;/g;
283
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
288 # $value =~ s/&([^;\s]*(\s|$))/&amp;$1/g;
289
290 print $metadata_xml_file " <Metadata mode=\"accumulate\" name=\"$meta_prefix$field\">$value</Metadata>\n";
291 }
292
293 print $metadata_xml_file
294 " </Description>\n" .
295 " </FileSet>\n";
296}
297
298sub obtain_document
299{
300 my $document_file_full = shift(@_);
301 my $documents_directory = shift(@_);
302 my $verbosity = shift(@_);
303
304 print STDERR "Obtaining document file $document_file_full...\n" if ($verbosity > 1);
305
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";
316 $wget_options = "--verbose" if ($verbosity > 2);
317 $wget_options .= " --timestamping"; # Only re-download files if they're newer
318 `wget $wget_options $document_file_full --output-document $local_document_file`;
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 }
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
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 }
341 }
342
343 # If the document wasn't obtained successfully, create a .nul file for it
344 if (!-e $local_document_file) {
345 $document_file_name .= ".nul";
346 open(NULL_FILE, ">$local_document_file.nul");
347 close(NULL_FILE);
348 }
349
350 return $document_file_name;
351}
352
353&main(@ARGV);
Note: See TracBrowser for help on using the repository browser.