source: trunk/gsdl/perllib/plugins/ConvertToRogPlug.pm@ 10218

Last change on this file since 10218 was 10218, checked in by kjdon, 19 years ago

Jeffrey's new parsing modifications, committed approx 6 July, 15.16

  • Property svn:keywords set to Author Date Id Revision
File size: 14.0 KB
Line 
1###########################################################################
2#
3# ConvertToRogPlug.pm -- plugin that inherits from RogPlug
4#
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# Copyright (C) 1999 New Zealand Digital Library Project
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24#
25###########################################################################
26
27
28package ConvertToRogPlug;
29
30use BasPlug;
31use RogPlug;
32
33
34sub BEGIN {
35 @ISA = ('RogPlug');
36}
37
38my $options = { 'name' => "ConvertToRogPlug",
39 'desc' => "{ConvertToRogPlug.desc}",
40 'abstract' => "yes",
41 'inherits' => "yes" };
42
43
44sub parse_args
45{
46 my $class = shift (@_);
47 my ($args) = @_;
48
49 my $plugin_name = $class;
50 $plugin_name =~ s/\.pm$//;
51
52 if (!parsargv::parse($args,
53 "allow_extra_options")) {
54
55 print STDERR "\nIncorrect options passed to $plugin_name, ";
56 print STDERR "check your collect.cfg configuration file\n";
57 $self->print_txt_usage(""); # Use default resource bundle
58 die "\n";
59 }
60
61 return ($plugin_name);
62}
63
64sub new {
65 my ($class) = shift (@_);
66 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
67 push(@$pluginlist, $class);
68
69 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
70 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
71
72 my $self = (defined $hashArgOptLists)? new RogPlug($pluginlist,$inputargs,$hashArgOptLists): new RogPlug($pluginlist,$inputargs);
73
74 $self->{'convert_to'} = "Rog";
75 $self->{'convert_to_ext'} = "rog";
76
77 return bless $self, $class;
78}
79
80
81sub begin {
82 my $self = shift (@_);
83
84 $self->SUPER::begin(@_);
85
86 $self->{'docnum'} = 0;
87}
88
89sub end {
90 my ($self) = @_;
91
92 # nothing to do, but keep symmetric with begin function
93 $self->SUPER::end(@_);
94}
95
96
97# Run conversion utility on the input file.
98#
99# The conversion takes place in a collection specific 'tmp' directory so
100# that we don't accidentally damage the input.
101#
102# The desired output type is indicated by $output_ext. This is usually
103# something like "html" or "word", but can be "best" (or the empty string)
104# to indicate that the conversion utility should do the best it can.
105
106sub tmp_area_convert_file {
107 my $self = shift (@_);
108 my ($output_ext, $input_filename, $textref) = @_;
109
110 my $outhandle = $self->{'outhandle'};
111 my $convert_to = $self->{'convert_to'};
112 my $failhandle = $self->{'failhandle'};
113
114 # softlink to collection tmp dir
115 my $tmp_dirname
116 = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "tmp");
117 &util::mk_dir($tmp_dirname) if (!-e $tmp_dirname);
118
119 # derive tmp filename from input filename
120 my ($tailname, $dirname, $suffix)
121 = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
122
123 # Remove any white space from filename -- no risk of name collision, and
124 # makes later conversion by utils simpler. Leave spaces in path...
125 $tailname =~ s/\s+//g;
126
127 my $tmp_filename = &util::filename_cat($tmp_dirname, "$tailname$suffix");
128
129 &util::soft_link($input_filename, $tmp_filename);
130
131 my $verbosity = $self->{'verbosity'};
132 if ($verbosity > 0) {
133 print $outhandle "Converting $tailname$suffix to $convert_to format\n";
134 }
135
136 my $errlog = &util::filename_cat($tmp_dirname, "err.log");
137
138 # Execute the conversion command and get the type of the result,
139 # making sure the converter gives us the appropriate output type
140 my $output_type = lc($convert_to);
141 my $cmd = "perl -S gsMusicConvert.pl -verbose $verbosity -errlog \"$errlog\" -output $output_type \"$tmp_filename\"";
142 $output_type = `$cmd`;
143
144 # remove symbolic link to original file
145 &util::rm($tmp_filename);
146
147 # Check STDERR here
148 chomp $output_type;
149 if ($output_type eq "fail") {
150 print $outhandle "Could not convert $tailname$suffix to $convert_to format\n";
151 print $failhandle "$tailname$suffix: " . ref($self) . " failed to convert to $convert_to\n";
152 $self->{'num_not_processed'} ++;
153 if (-s "$errlog") {
154 open(ERRLOG, "$errlog");
155 while (<ERRLOG>) {
156 print $outhandle "$_";
157 }
158 print $outhandle "\n";
159 close ERRLOG;
160 }
161 &util::rm("$errlog") if (-e "$errlog");
162 return "";
163 }
164
165 # store the *actual* output type and return the output filename
166 # it's possible we requested conversion to html, but only to text succeeded
167
168 $self->{'convert_to_ext'} = $output_type;
169 $self->{'converted_to'} = "Rog";
170
171 my $output_filename = $tmp_filename;
172
173 $output_filename =~ s/$suffix$//;
174
175 return $output_filename;
176}
177
178
179# Remove collection specific tmp directory and all its contents.
180
181sub cleanup_tmp_area {
182 my $self = shift (@_);
183
184 my $tmp_dirname
185 = &util::filename_cat($ENV{'GSDLCOLLECTDIR'}, "tmp");
186 &util::rm_r($tmp_dirname);
187 &util::mk_dir($tmp_dirname);
188}
189
190
191# Exact copy of read_rog_record from RogPlug
192# Needed for FILE in right scope
193
194sub read_rog_record
195{
196 my ($self,$file_buffer, $docnum, $seclevel) = @_;
197
198 my $next_line = $file_buffer->{'next_line'};
199
200 return 0 if (!defined $next_line);
201
202 if ($next_line eq "")
203 {
204 my $line;
205 while(defined($line=<FILE>))
206 {
207 $line =~ s/\r$//;
208 $file_buffer->{'line_no'}++;
209 next if ($line =~ m/^\#/);
210 $next_line = $line;
211 last;
212 }
213 }
214
215 if ($next_line !~ m/^song( +)\"([^\"]*)\"( +)\"([^\"]*)\"( +)(\d+)( *)$/)
216 {
217 print STDERR "Error: Malformed Rog file: $next_line";
218 return 0;
219 }
220 else
221 {
222 # init default values
223 $file_buffer->{'song'}->{'tempo'} = 120;
224 $file_buffer->{'song'}->{'ks_type'} = 0;
225 $file_buffer->{'song'}->{'ks_num'} = 0;
226 $file_buffer->{'song'}->{'metadata'} = [];
227 $file_buffer->{'song'}->{'content'} = "";
228
229 $file_buffer->{'song'}->{'subcol'} = $2;
230 $file_buffer->{'song'}->{'title'} = $4;
231 $file_buffer->{'song'}->{'tval'} = $6;
232
233 chomp($next_line);
234 my $content = $next_line;
235 if (defined $docnum)
236 {
237 $content.= " $docnum $seclevel";
238 }
239 $content .= "\n";
240
241 $file_buffer->{'song'}->{'content'} = $content;
242
243
244 my $line;
245 while(defined($line=<FILE>))
246 {
247 $line =~ s/\r$//;
248
249 $file_buffer->{'line_no'}++;
250 next if ($line =~ m/^\#/);
251
252 if ($line =~ m/^song/)
253 {
254 $file_buffer->{'next_line'} = $line;
255 return 1;
256 }
257 elsif ($line =~ m/^tempo( +)(\d+)( *)$/)
258 {
259 $file_buffer->{'song'}->{'tempo'} = $2;
260 $file_buffer->{'song'}->{'content'} .= $line;
261 }
262 elsif ($line =~ m/^keysig( +)(\d+)( +)(\d+)( *)$/)
263 {
264 $file_buffer->{'song'}->{'ks_type'} = $2;
265 $file_buffer->{'song'}->{'ks_num'} = $4;
266 $file_buffer->{'song'}->{'content'} .= $line;
267 }
268 elsif ($line =~ m/^timesig( +)(\d+)( +)(\d+)( *)$/)
269 {
270 $file_buffer->{'song'}->{'ts_numer'} = $2;
271 $file_buffer->{'song'}->{'ts_denom'} = $4;
272 $file_buffer->{'song'}->{'content'} .= $line;
273 }
274 elsif ($line =~ m/^metadata ([^:]*): (.*)/)
275 {
276 push(@{$file_buffer->{'song'}->{'metadata'}},[$1,$2]);
277 $file_buffer->{'song'}->{'content'} .= $line;
278 }
279 else
280 {
281 $file_buffer->{'song'}->{'content'} .= $line;
282 }
283 }
284
285 $file_buffer->{'next_line'} = undef;
286 }
287
288 return 1;
289}
290
291# Override RogPlug function so rog files are stored as sections (not docs)
292
293sub process_rog_record
294{
295 my ($self,$doc_obj,$cursection,$song) = @_;
296
297 $cursection =
298 $doc_obj->insert_section($cursection);
299 $self->{'docnum'}++;
300
301 my $title = $song->{'title'};
302 my $title_safe = $title;
303 $title_safe =~ s/\'/\\\\&apos;/g;
304
305 # add metadata
306 $doc_obj->add_metadata($cursection, "Tempo", $song->{'tempo'});
307 $doc_obj->add_metadata($cursection, "KeySigType", $song->{'ks_type'});
308 $doc_obj->add_metadata($cursection, "KeySigNum", $song->{'ks_num'});
309 $doc_obj->add_metadata($cursection, "SubCollection", $song->{'subcol'});
310 $doc_obj->add_metadata($cursection, "Title", $title);
311 $doc_obj->add_metadata($cursection, "TitleSafe", $title_safe);
312 $doc_obj->add_metadata($cursection, "TVal", $song->{'tval'});
313
314 foreach $md ( @{$song->{'metadata'}} )
315 {
316 $doc_obj->add_metadata($cursection, $md->[0], $md->[1]);
317 }
318
319 # add contents as text
320 $doc_obj->add_text($cursection,$song->{'content'});
321
322 return $cursection;
323}
324
325
326
327# Override BasPlug read
328# We don't want to get language encoding stuff until after we've converted
329# our file to Rog format
330sub read {
331 my $self = shift (@_);
332 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $total_count, $gli) = @_;
333# if ($self->is_recursive()) {
334# die "BasPlug::read function must be implemented in sub-class for recursive plugins\n";
335# }
336
337 my $outhandle = $self->{'outhandle'};
338
339 my $filename = $file;
340 $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
341
342 if ($self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/) {
343 $self->{'num_blocked'} ++;
344 return 0;
345 }
346 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
347 return undef;
348 }
349 $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
350
351 # read in file ($text will be in utf8)
352 my $text = "";
353
354 my $output_ext = $self->{'convert_to_ext'};
355 my $conv_filename = $self->tmp_area_convert_file($output_ext, $filename);
356
357 if ("$conv_filename" eq "") {return 0;} # allows continue on errors
358 $self->{'conv_filename'} = $conv_filename;
359
360
361 # create a new document
362 #my $doc_obj = new doc ($conv_filename, "indexed_doc");
363 # the original filename is used now
364 my $doc_obj = new doc ($filename, "indexed_doc");
365 # the converted filename is set separately
366 $doc_obj->set_converted_filename($conv_filename);
367
368 my $topsection = $doc_obj->get_top_section();
369 my $cursection = $topsection;
370
371 $self->{'docnum'}++;
372 my $docnum = $self->{'docnum'};
373
374 $doc_obj->set_OIDtype ($processor->{'OIDtype'});
375 my ($filemeta) = $file =~ /([^\\\/]+)$/;
376 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Source", &ghtml::dmsafe($filemeta));
377 if ($self->{'cover_image'}) {
378 $self->associate_cover_image($doc_obj, $filename);
379 }
380 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "Plugin", "$self->{'plugin_type'}");
381 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), "FileSize", (-s $filename));
382
383 my $track_no = "1";
384 my $rog_filename = "$conv_filename$track_no.$output_ext";
385 while (1)
386 {
387 last unless open (FILE, $rog_filename) ;
388
389 my $file_buffer = { line_no => 0, next_line => "", song => {} };
390
391 while ($self->read_rog_record($file_buffer, $docnum, $track_no))
392 {
393 my $song = $file_buffer->{'song'};
394 my $content = $song->{'content'};
395 $content =~ s/^song\w+(.*)$/song $1 X.$track_no/;
396
397 $cursection
398 = $self->process_rog_record($doc_obj,$cursection,
399 $file_buffer->{'song'});
400 }
401
402 close FILE;
403
404 $track_no++;
405 $rog_filename = "$conv_filename$track_no.$output_ext";
406 }
407
408 print STDERR "\n";
409
410 # include any metadata passed in from previous plugins
411 # note that this metadata is associated with the top level section
412 $self->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
413 # do plugin specific processing of doc_obj
414 unless (defined ($self->process(\$text, $pluginfo, $base_dir, $file, $metadata, $doc_obj))) {
415 print STDERR "<ProcessingError n='$file'>\n" if ($gli);
416 return -1;
417 }
418 # do any automatic metadata extraction
419 $self->auto_extract_metadata ($doc_obj);
420 # add an OID
421 $doc_obj->set_OID();
422
423 my $oid = $doc_obj->get_OID();
424 my $appletlink = "<a href=\"javascript:meldexout(\'$oid\','[TitleSafe]')\">";
425
426 $doc_obj->add_utf8_metadata ($topsection, "audiolink", $appletlink);
427 $doc_obj->add_utf8_metadata ($topsection, "audioicon", "_iconaudio_");
428 $doc_obj->add_utf8_metadata ($topsection, "/audiolink", "</a>");
429
430 # if no title metadata defined, set it to filename minus extension
431 my $existing_title = $doc_obj->get_metadata_element($topsection,"Title");
432 if (!defined $existing_title)
433 {
434 my $title = $doc_obj->get_metadata_element($topsection,"Source");
435 $title =~ s/\..*?$//g;
436 $doc_obj->add_utf8_metadata ($topsection, "Title", $title);
437
438 my $title_safe = $title;
439 $title_safe =~ s/\'/\\\\&apos;/g;
440 $doc_obj->add_utf8_metadata ($topsection, "TitleSafe", $title_safe);
441 }
442
443 # process the document
444 $processor->process($doc_obj);
445 $self->cleanup_tmp_area();
446
447 $self->{'num_processed'} ++;
448
449 return 1;
450}
451
452
453# do plugin specific processing of doc_obj for HTML type
454sub process_type {
455 my $self = shift (@_);
456 my ($doc_ext, $textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
457
458 my $conv_filename = $self->{'conv_filename'};
459 my $tmp_dirname = File::Basename::dirname($conv_filename);
460 my $tmp_tailname = File::Basename::basename($conv_filename);
461
462 my $converted_to = $self->{'converted_to'};
463 my $ret_val = 1;
464
465# $ret_val = &RogPlug::process($self, $textref, $pluginfo,
466# $tmp_dirname, $tmp_tailname,
467# $metadata, $doc_obj);
468
469 # associate original file with doc object
470 my $cursection = $doc_obj->get_top_section();
471 my $filename = &util::filename_cat($base_dir, $file);
472 $doc_obj->associate_file($filename, "doc.$doc_ext", undef, $cursection);
473
474 my $doclink = "<a href=\"_httpcollection_/index/assoc/[archivedir]/doc.$doc_ext\">";
475 $doc_obj->add_utf8_metadata ($cursection, "srclink", $doclink);
476 $doc_obj->add_utf8_metadata ($cursection, "srcicon", "_icon".$doc_ext."_");
477 $doc_obj->add_utf8_metadata ($cursection, "/srclink", "</a>");
478 return $ret_val;
479}
480
4811;
Note: See TracBrowser for help on using the repository browser.