source: trunk/bbc/collect/bbc/perllib/plugins/BRSPlug.pm@ 4647

Last change on this file since 4647 was 4647, checked in by sjboddie, 18 years ago

Initial revision

  • Property svn:keywords set to Author Date Id Revision
File size: 25.3 KB
Line 
1
2# plugin which processes an the BBC Sound archive exported in an ASCII format
3
4package BRSPlug;
5
6use BasPlug;
7use ghtml;
8
9sub BEGIN
10{
11 @ISA = ('BasPlug');
12 unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib");
13}
14
15%Exclude = ();
16
17
18# Note: sext is short for scrollable text
19
20#field_id label disp_type crit_type width height brs_para
21#----------------------------------------------------------------
22#10 sport prdcr text text 30 1 T006
23#1 text sext text 40 16 TEXT
24#16 scol ref text text text 30 1 SCOL
25#19 date text date 10 1 D001
26#25 Item title text text 20 1 T001
27#26 B/W Seqs text text 30 1 S001
28#27 Medium text text 30 1 S002
29#28 shot type text text 30 1 S003
30#29 B/W Prog/Item text text 30 1 S004
31#30 Seqs text text 10 1 S005
32#31 Format text text 30 1 S006
33#32 prog/item text text 30 1 S007
34#35 reg libno text text 30 1 S008
35#36 reg tape no text text 30 1 S009
36#37 C Indicator text text 30 1 S010
37#38 C Holder text text 30 1 T002
38#39 cprd ind text text 30 1 S011
39#40 cprd name text text 30 1 T003
40#44 reference sext text 30 5 R001
41#49 prog num text text 10 1 PRNO
42#59 news title text text 30 1 T005
43#65 cat num text text 10 1 CATN
44#72 Record number text text 30 1 T007
45#73 rx_date text date 10 1 S013
46#74 Music performer text text 30 2 T008
47#75 Composer text text 30 2 T009
48#76 mono/stereo text text 30 1 S014
49#77 music medium text text 30 1 S015
50#82 Sound Tech text text 30 1 S016
51#83 Music publisher text text 30 1 S017
52#84 music tape text text 30 1 T012
53#89 catalogue text text 3 1 CODE
54#104 subcat text text 10 1 SUBC
55#106 prog title text text 40 1 TTLE
56#109 screen type text text 10 1 DOCT
57#110 stock title text text 30 1 T013
58#113 stock ln 4 text text 30 1 T015
59#114 stock lib 1 text text 30 1 S020
60#115 stock lib 2 text text 30 1 S021
61#----------------------------------------------------------------
62
63
64my @brs_field_table
65 = (
66 { field_name => "TTLE", field_id => 106, label => "Prog title",
67 disp_type => "text", crit_type => "text",
68 width => 40, height => 1 },
69 { field_name => "T001", field_id => 25, label => "Item title",
70 disp_type => "text", crit_type => "text",
71 width => 20, height => 1 },
72 { field_name => "T005", field_id => 59, label => "News title",
73 disp_type => "text", crit_type => "text",
74 width => 30, height => 1 },
75 { field_name => "T013", field_id => 110, label => "Stock title",
76 disp_type => "text", crit_type => "text",
77 width => 30, height => 1 },
78 { field_name => "T007", field_id => 72, label => "Record number",
79 disp_type => "text", crit_type => "text",
80 width => 30, height => 1 },
81 { field_name => "TEXT", field_id => 1, label => "Text",
82 disp_type => "scrl", crit_type => "text",
83 width => 40, height => 16 },
84
85 { field_name => "T006", field_id => 10, label => "Sport prdcr",
86 disp_type => "text", crit_type => "text",
87 width => 30, height => 1 },
88 { field_name => "SCOL", field_id => 16, label => "Scol ref text",
89 disp_type => "text", crit_type => "text",
90 width => 30, height => 1 },
91 { field_name => "D001", field_id => 19, label => "Date",
92 disp_type => "text", crit_type => "date",
93 width => 10, height => 1 },
94 { field_name => "S001", field_id => 26, label => "B/W Seqs",
95 disp_type => "text", crit_type => "text",
96 width => 30, height => 1 },
97 { field_name => "S002", field_id => 27, label => "Medium",
98 disp_type => "text", crit_type => "text",
99 width => 30, height => 1 },
100 { field_name => "S003", field_id => 28, label => "Shot type",
101 disp_type => "text", crit_type => "text",
102 width => 30, height => 1 },
103 { field_name => "S004", field_id => 29, label => "B/W Prog/Item",
104 disp_type => "text", crit_type => "text",
105 width => 30, height => 1 },
106 { field_name => "S005", field_id => 30, label => "Seqs",
107 disp_type => "text", crit_type => "text",
108 width => 10, height => 1 },
109 { field_name => "S006", field_id => 31, label => "Format",
110 disp_type => "text", crit_type => "text",
111 width => 30, height => 1 },
112 { field_name => "S007", field_id => 32, label => "Prog/item",
113 disp_type => "text", crit_type => "text",
114 width => 30, height => 1 },
115 { field_name => "S008", field_id => 35, label => "Reg libno",
116 disp_type => "text", crit_type => "text",
117 width => 30, height => 1 },
118 { field_name => "S009", field_id => 36, label => "Reg tape no",
119 disp_type => "text", crit_type => "text",
120 width => 30, height => 1 },
121 { field_name => "S010", field_id => 37, label => "C Indicator",
122 disp_type => "text", crit_type => "text",
123 width => 30, height => 1 },
124 { field_name => "T002", field_id => 38, label => "C Holder",
125 disp_type => "text", crit_type => "text",
126 width => 30, height => 1 },
127 { field_name => "S011", field_id => 39, label => "Cprd ind",
128 disp_type => "text", crit_type => "text",
129 width => 30, height => 1 },
130 { field_name => "T003", field_id => 40, label => "Cprd name",
131 disp_type => "text", crit_type => "text",
132 width => 30, height => 1 },
133 { field_name => "R001", field_id => 44, label => "Reference",
134 disp_type => "scrl", crit_type => "text",
135 width => 30, height => 5 },
136 { field_name => "PRNO", field_id => 49, label => "Prog num",
137 disp_type => "text", crit_type => "text",
138 width => 10, height => 1 },
139 { field_name => "CATN", field_id => 65, label => "Cat num",
140 disp_type => "text", crit_type => "text",
141 width => 10, height => 1 },
142 { field_name => "S013", field_id => 73, label => "Rx_date",
143 disp_type => "text", crit_type => "date",
144 width => 10, height => 1 },
145 { field_name => "T008", field_id => 74, label => "Music performer",
146 disp_type => "text", crit_type => "text",
147 width => 30, height => 2 },
148 { field_name => "T009", field_id => 75, label => "Composer",
149 disp_type => "text", crit_type => "text",
150 width => 30, height => 2 },
151 { field_name => "S014", field_id => 76, label => "Mono/stereo",
152 disp_type => "text", crit_type => "text",
153 width => 30, height => 1 },
154 { field_name => "S015", field_id => 77, label => "Music medium",
155 disp_type => "text", crit_type => "text",
156 width => 30, height => 1 },
157 { field_name => "S016", field_id => 82, label => "Sound Tech",
158 disp_type => "text", crit_type => "text",
159 width => 30, height => 1 },
160 { field_name => "S017", field_id => 83, label => "Music publisher",
161 disp_type => "text", crit_type => "text",
162 width => 30, height => 1 },
163 { field_name => "T012", field_id => 84, label => "Music tape",
164 disp_type => "text", crit_type => "text",
165 width => 30, height => 1 },
166 { field_name => "CODE", field_id => 89, label => "Catalogue",
167 disp_type => "text", crit_type => "text",
168 width => 3, height => 1 },
169 { field_name => "SUBC", field_id => 104, label => "Subcat",
170 disp_type => "text", crit_type => "text",
171 width => 10, height => 1 },
172 { field_name => "DOCT", field_id => 109, label => "Screen type",
173 disp_type => "text", crit_type => "text",
174 width => 10, height => 1 },
175 { field_name => "T015", field_id => 113, label => "Stock ln 4",
176 disp_type => "text", crit_type => "text",
177 width => 30, height => 1 },
178 { field_name => "S020", field_id => 114, label => "Stock lib 1",
179 disp_type => "text", crit_type => "text",
180 width => 30, height => 1 },
181 { field_name => "S021", field_id => 115, label => "Stock lib 2",
182 disp_type => "text", crit_type => "text",
183 width => 30, height => 1 }
184 );
185
186
187my @brs_title_list = ("TTLE", "T001", "T005", "T013", "T007", "RFTI");
188my %cat_num_list = ();
189
190sub new {
191 my ($class) = @_;
192 my $self = new BasPlug ($class, @_);
193
194 return bless $self, $class;
195}
196
197sub get_default_process_exp {
198 my $self = shift (@_);
199
200# return q^(?i)\.brs$^;
201 return q^b.+^;
202}
203
204my $brs_doc_count = 0;
205my $brs_dot_count = 0;
206my $brs_line_no = 0;
207
208sub brs_error
209{
210 my ($file, $mess, $outhandle) = @_;
211
212 print $outhandle "Malformed BRS recorded at line $brs_line_no in $file:\n";
213 print $outhandle " $mess\n";
214}
215
216sub read_brs_record
217{
218 my ($recordref, $file, $outhandle) = @_;
219
220 my $extra_trigger = '\s*>';
221 my $text_divider = "-" x 78;
222 my $text_end = '^\.\.[^:]+:$';
223
224 # Print "." to signify processing if enough records have been read in
225 $brs_doc_count++;
226 if (($brs_doc_count % 10) == 0)
227 {
228 print $outhandle ".";
229 $brs_dot_count++;
230 print $outhandle "\n" if (($brs_dot_count % 80) == 0);
231 }
232
233 my $brs_rec = undef;
234 my $line = undef;
235
236 # read in the record
237 my @brs_lines = split /\n/, $$recordref;
238
239 while (scalar(@brs_lines)>0)
240 {
241 $line = shift(@brs_lines);
242 $line =~ s/\cM//g;
243
244 $brs_line_no++;
245
246 if ($line =~ m/^\.\.([^:]+):$/)
247 {
248 my $field_name = $1;
249
250 if ($field_name eq "TEXT")
251 {
252 my $text_above = "";
253 my $text_extra = "";
254 my $text_below = "";
255
256 my $add_mode = "above";
257
258 while (scalar(@brs_lines)>0)
259 {
260 last if ($brs_lines[0] =~ m/$text_end/);
261
262 $line = shift(@brs_lines);
263 $brs_line_no++;
264
265 if ($line =~ m/^$text_divider/)
266 {
267 $add_mode = "below";
268 next;
269 }
270
271 if ($line =~ m/^$extra_trigger/)
272 {
273 $text_extra .= "$line\n";
274 $add_mode = "below";
275 next;
276 }
277
278 if ($line =~ m/^\.\.[^:]+:/)
279 {
280 if ($line =~ m/^\.\.TEXT/)
281 {
282 $add_mode = "above";
283 next;
284 }
285 else
286 {
287 brs_error($file, "Unexpected field $line", $outhandle);
288 # put field name back
289 unshift(@brs_lines, $line);
290 $brs_line_no--;
291 last;
292 }
293 }
294
295 if ($line !~ m/^\s+(-){10,}$/)
296 {
297 if ($add_mode eq "above")
298 {
299 $text_above .= "$line ";
300 }
301 else
302 {
303 $text_below .= "$line ";
304 }
305 }
306 }
307
308 $brs_rec->{TEXTA} .= $text_above if ($text_above ne "");
309 $brs_rec->{TEXTB} .= $text_below if ($text_below ne "");
310 $brs_rec->{TEXTE} .= $text_extra if ($text_extra ne "");
311 }
312 elsif ($field_name =~ /^RF/)
313 {
314 # read zz fields until end of record
315
316 while (scalar(@brs_lines)>0)
317 {
318 my $line_ahead = $brs_lines[0];
319 last if ($line_ahead =~ m/^\.\.[^:]+:$/);
320
321 $line = shift(@brs_lines);
322 $brs_line_no++;
323
324 if (!defined($brs_rec->{$field_name}))
325 {
326 $brs_rec->{$field_name} = [$line];
327 }
328 else
329 {
330 push(@{$brs_rec->{$field_name}},$line);
331 }
332 }
333 }
334 else
335 {
336 my $field_entry = "";
337 while ($brs_lines[0] !~ m/^\.\.[^:]+:$/)
338 {
339 $field_entry .= shift(@brs_lines);
340 $brs_line_no++;
341 last if (scalar(@brs_lines)==0);
342 }
343
344 if (!defined($brs_rec->{$field_name}))
345 {
346 $brs_rec->{$field_name} = $field_entry;
347 }
348 else
349 {
350 brs_error($file, "$field_name already defined.", $outhandle);
351 }
352 }
353 }
354 else
355 {
356 brs_error($file, "Malformed field: $line.", $outhandle);
357 }
358 }
359
360 # format the programme number if required
361 &format_prognum($brs_rec);
362
363 return ($brs_rec);
364}
365
366my $brs_processed_count = 0;
367
368sub brs_full_record_in_html
369{
370 my ($doc_obj,$cursection,$brs_rec) = @_;
371
372 my $html_table = "";
373 my $table_line = "";
374 my $cell_count = 0;
375 my $long_lines = "";
376
377 my $i;
378 for ($i=0; $i<=$#brs_field_table; $i++)
379 {
380 my $field_name = $brs_field_table[$i]->{'field_name'};
381 if (defined($brs_rec->{$field_name}))
382 {
383 my $field_label = $brs_field_table[$i]->{'label'};
384 my $field_value = $brs_rec->{$field_name};
385
386 # these fields get their own line
387 if ($field_label =~ /^(Prog|Item|News|Stock) title$/) {
388 my $safe_value = $field_value;
389 &ghtml::urlsafe ($safe_value);
390 $long_lines .= "<tr valign=top><td><b>$field_label:</b></td><td colspan=5>";
391 $long_lines .= "<a href=\"_httpquerytitle_&q=$safe_value\">$field_value</a></td></tr>\n";
392 $long_lines .= "$field_value</td></tr>\n";
393
394 } else {
395
396 if (($cell_count>0) && ($cell_count%3 == 0))
397 {
398 $html_table .= "\n<tr valign=top>\n$table_line\n</tr>\n";
399 $table_line = "";
400 }
401 $cell_count++;
402 if ($field_name eq "D001") # a Dublin core style date
403 {
404 my @mon_convert
405 = ("Jan", "Feb", "Mar", "Apr", "May", "Jun",
406 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
407
408 my ($year,$monnum,$day)
409 = ($field_value =~ m/(\d{4})(\d{2})(\d{2})/);
410 my $moneng = $mon_convert[$monnum-1];
411
412 $field_value = "$day $moneng $year";
413
414 } elsif ($field_name eq "PRNO") {
415 my $safe_value = $field_value;
416 &ghtml::urlsafe ($safe_value);
417 $field_value = "<a href=\"_httpqueryprogname_&q=$safe_value\">$field_value</a>";
418
419 } elsif ($field_name eq "CATN") {
420 my $safe_value = $field_value;
421 &ghtml::urlsafe ($safe_value);
422 $field_value = "<a href=\"_httpquerycatnum_&q=$safe_value\">$field_value</a>";
423
424 } elsif ($field_name eq "T003") {
425 my $safe_value = $field_value;
426 &ghtml::urlsafe ($safe_value);
427 $field_value = "<a href=\"_httpqueryzzabn_&q=$safe_value\">$field_value</a>";
428 }
429
430 $table_line .= "<td><b>$field_label:</b></td><td>$field_value</td>\n";
431 }
432 }
433 }
434
435 if ($cell_count%3 != 0) {
436 $table_line .= "<td colspan=" . (3-($cell_count%3))*2 . "></td>";
437 }
438
439 $html_table .= "\n<tr>\n$table_line\n</tr>\n";
440 $html_table = "<table>\n$long_lines$html_table\n</table>\n";
441
442 $doc_obj->add_utf8_text($cursection, $html_table);
443}
444
445sub process_brs_record
446{
447 my ($processor, $brs_rec, $file, $outhandle) = @_;
448
449 if (defined($brs_rec))
450 {
451
452 # only include those records in the chosen subcats - these lines
453 # should be commented out to build the entire collection
454
455 # National Sound Archive collection
456# if (!defined ($brs_rec->{'SUBC'}) ||
457# $brs_rec->{'SUBC'} !~ /^(RADCOMP|RADIOX|RADPROG|RADNEWS|BDSPROG|UNCATRAD|RADIOREF|WILDSOUND)$/) {
458# return;
459# }
460
461 # British Film Institute collection
462# if (!defined ($brs_rec->{'SUBC'}) ||
463# $brs_rec->{'SUBC'} !~ /^(LONPROG|NEWS)$/) {
464# return;
465# }
466
467 # "the rest" collection
468 if (!defined ($brs_rec->{'SUBC'}) ||
469 $brs_rec->{'SUBC'} =~ /^(LONPROG|NEWS|RADCOMP|RADIOX|RADPROG|RADNEWS|BDSPROG|UNCATRAD|RADIOREF|WILDSOUND)$/) {
470 return;
471 }
472
473 # don't include the secret stuff ;-)
474 if (defined ($brs_rec->{'SUBC'}) && $brs_rec->{'SUBC'} eq "HISTORY") {
475 print STDERR "\nexcluding secret stuff - line $brs_line_no in $file\n";
476 return;
477 }
478
479
480 my $doc_obj = new doc ($file, "indexed_doc");
481 $brs_processed_count++;
482
483 my $cursection = $doc_obj->get_top_section();
484
485 my $found_match = "no";
486
487 my $pot_title;
488 foreach $pot_title ( @brs_title_list )
489 {
490 if (defined ($brs_rec->{$pot_title}))
491 {
492 my $title = "";
493
494 if ($pot_title =~ m/^zz/)
495 {
496 my $zz_join = join(" ", @{$brs_rec->{$pot_title}});
497 my @zz_split = split(" ", $zz_join);
498 map { $title .= "$_ " if ($_ !~ m/^xx/); } @zz_split;
499 }
500 else
501 {
502 $title = $brs_rec->{$pot_title};
503 }
504
505 my $tl_ref = $doc_obj->get_metadata ($cursection, "Title");
506
507 if (scalar(@$tl_ref==0))
508 {
509 $doc_obj->add_utf8_metadata ($cursection, "Title", $title);
510 }
511 else
512 {
513 my $exists = "no";
514
515 map { $exists = "yes" if ($title eq $_); } @$tl_ref;
516 if ($exists eq "no")
517 {
518 $doc_obj->add_utf8_metadata($cursection, "Title", $title);
519 }
520 }
521 $found_match = "yes";
522 }
523 }
524 if ($found_match eq "no")
525 {
526 if (defined($brs_rec->{TEXTA}))
527 {
528 my $sub_title = substr($brs_rec->{TEXTA},0,60) . " ... ";
529 $doc_obj->add_utf8_metadata ($cursection, "Title", $sub_title);
530 }
531 elsif (defined($brs_rec->{TEXTB}))
532 {
533 my $sub_title = substr($brs_rec->{TEXTB},0,60) . " ... ";
534 $doc_obj->add_utf8_metadata ($cursection, "Title", $sub_title);
535 }
536 else
537 {
538 print $outhandle "\nNo title or text for record ending at line ";
539 print $outhandle $brs_line_no-1, " in $file\n";
540 }
541 }
542
543 if (defined($brs_rec->{'T001'})) # item title
544 {
545 $doc_obj->add_utf8_metadata ($cursection, "ItemTitle", $brs_rec->{'T001'});
546 }
547
548 if (defined($brs_rec->{'D001'})) # date
549 {
550 $doc_obj->add_utf8_metadata ($cursection, "Date", $brs_rec->{'D001'});
551 }
552 else
553 {
554 print $outhandle "\nNo date for record ending at line ";
555 print $outhandle $brs_line_no-1, " in $file\n";
556 }
557
558 if (defined($brs_rec->{'PRNO'})) # programme number
559 {
560 $doc_obj->add_utf8_metadata ($cursection, "ProgNumber", $brs_rec->{'PRNO'});
561 }
562
563 if (defined($brs_rec->{'CATN'})) # catalogue number
564 {
565 $doc_obj->add_utf8_metadata ($cursection, "CatNum", $brs_rec->{'CATN'});
566 }
567
568 # RFAN, RFAB and T003 fields
569 if (defined($brs_rec->{'RFAB'}) || defined($brs_rec->{'RFAN'}) || defined($brs_rec->{'T003'}))
570 {
571 my $zzabn = "";
572 if (defined($brs_rec->{'RFAB'})) {
573 foreach my $a (@{$brs_rec->{'RFAB'}}) {
574 $a =~ s/[^a-zA-Z0-9]//g;
575 $zzabn .= " " . $a;
576 }
577 }
578 if (defined($brs_rec->{'RFAN'})) {
579 foreach my $a (@{$brs_rec->{'RFAN'}}) {
580 $a =~ s/[^a-zA-Z0-9]//g;
581 $zzabn .= " " . $a;
582 }
583 }
584 if (defined($brs_rec->{'T003'})) {
585 my $value = $brs_rec->{'T003'};
586 $value =~ s/[^a-zA-Z0-9]//g;
587 $zzabn .= " " . $value;
588 }
589 $doc_obj->add_utf8_metadata ($cursection, "zzabn", $zzabn);
590 }
591
592 if (defined($brs_rec->{'RFN'}))
593 {
594 my $name;
595 foreach $name (@{$brs_rec->{'RFN'}})
596 {
597 $doc_obj->add_utf8_metadata ($cursection, "People", $name);
598 }
599 }
600
601 brs_full_record_in_html($doc_obj,$cursection,$brs_rec);
602
603 if (defined($brs_rec->{TEXTA}))
604 {
605
606 my $desc_texta = "<h3>Description</h3>$brs_rec->{TEXTA}";
607 $doc_obj->add_utf8_text ($cursection, $desc_texta);
608
609 if (defined($brs_rec->{TEXTE}))
610 {
611 my $raw_texte = $brs_rec->{TEXTE};
612 $raw_texte =~ s/^\s*>\s*/<li>/mg;
613
614 my $test_empty = $raw_texte;
615 $test_empty =~ s/(<li>)+//g;
616
617 if ($test_empty ne "")
618 {
619 my $desc_texte = $raw_texte;
620 $desc_texte =~ s/^<li>(.*)$/<li><i>$1<\/i>/mg;
621 $desc_texte = "<p>Additional:<ul>$desc_texte</ul>";
622 $doc_obj->add_utf8_text ($cursection, $desc_texte);
623 }
624 }
625 }
626
627 if (defined($brs_rec->{TEXTB}))
628 {
629 my $desc_textb = "<h3>Comments</h3>$brs_rec->{TEXTB}";
630 $doc_obj->add_utf8_text($cursection, $desc_textb);
631 }
632
633 my $full_text = "";
634 $full_text .= "$brs_rec->{TEXTA}\n" if (defined($brs_rec->{TEXTA}));
635 $full_text .= "$brs_rec->{TEXTE}\n" if (defined($brs_rec->{TEXTE}));
636 $full_text .= "$brs_rec->{TEXTB}\n" if (defined($brs_rec->{TEXTB}));
637
638 my @zz_list = ( "RFPG", "RFLO", "RFN" );
639 my $zz;
640 foreach $zz ( @zz_list)
641 {
642 if (defined($brs_rec->{$zz}))
643 {
644 my $filtered_text = "";
645
646 my $name;
647 foreach $name (@{$brs_rec->{$zz}})
648 {
649 my @split_zz = split(' ',$name);
650 map { $filtered_text .= "$_ " if ($_ =~ m/^xx/) } @split_zz
651 }
652
653 $full_text .= "$filtered_text\n";
654 }
655 }
656
657 # format zz fields (tail end of record)
658 my $zz_html = "<table>\n";
659
660 my $pot_zz;
661 foreach $pot_zz (keys %{$brs_rec})
662 {
663 if ($pot_zz =~ m/^RF/)
664 {
665 my $zz = $pot_zz;
666 my $name;
667 foreach $name (@{$brs_rec->{$zz}})
668 {
669 if ($zz eq "RFN") {
670 my $safe_name = $name;
671 &ghtml::urlsafe ($safe_name);
672 $zz_html .= "<tr><td><b>$zz:</b></td><td><a href=\"_httpquerypeople_&q=$safe_name\">$name</a></td></tr>\n";
673 } elsif ($zz =~ /^RFA[BN]$/) {
674 my $safe_name = $name;
675 &ghtml::urlsafe ($safe_name);
676 $zz_html .= "<tr><td><b>$zz:</b></td><td><a href=\"_httpqueryzzabn_&q=$safe_name\">$name</a></td></tr>\n";
677
678 } else {
679 $zz_html .= "<tr><td><b>$zz:</b></td><td>$name</td></tr>\n";
680 }
681 }
682 }
683 }
684 $zz_html .= "</table>\n";
685 $doc_obj->add_utf8_text($cursection, $zz_html);
686
687 # add OID - we'll use the catalog number (hoping it's unique)
688 my $cat_num = $brs_rec->{'CATN'};
689 if (!defined $cat_num) {
690 print $outhandle "\n***** No catalogue number for record ending at line ";
691 print $outhandle $brs_line_no-1, " in $file - THIS RECORD WILL BE IGNORED\n";
692 foreach my $v (keys(%$brs_rec)) {
693 if (ref($brs_rec->{$v}) eq "ARRAY") {
694 foreach my $i (@{$brs_rec->{$v}}) {
695 print STDERR "$v -> $i\n";
696 }
697 } else {
698 print STDERR $v . " -> " . $brs_rec->{$v} . "\n";
699 }
700 }
701 return;
702 }
703 if (defined ($cat_num_list{$cat_num})) {
704 print $outhandle "WARNING: catalog number $cat_num used more than once\n";
705 }
706 $doc_obj->set_OID ("bbc" . $cat_num);
707 $cat_num_list{$cat_num} = 1;
708
709 # process the document
710 $processor->process($doc_obj);
711 }
712 else
713 {
714 print $outhandle "BRS record empty\n";
715 }
716
717}
718
719sub read {
720 my $self = shift (@_);
721 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs) = @_;
722 my $outhandle = $self->{'outhandle'};
723
724 my $filename = &util::filename_cat($base_dir, $file);
725# return 0 if $self->{'block_exp'} ne "" && $filename =~ /$self->{'block_exp'}/;
726 if ($filename !~ /$self->{'process_exp'}/ || !-f $filename) {
727 return undef;
728 }
729# my $plugin_name = ref ($self);
730 $file =~ s/^[\/\\]+//; # $file often begins with / so we'll tidy it up
731
732 # create a new document
733# my $doc_obj = new doc ($filename, "indexed_doc");
734
735 # read in file ($text will be in utf8)
736# my $text = "";
737# $self->read_file ($filename, \$text);
738
739# if ($text !~ /\w/) {
740# print $outhandle "$plugin_name: ERROR: $file contains no text\n" if $self->{'verbosity'};
741# return 0;
742# }
743
744 # text is always plain ascii
745 undef $/;
746 open (FILE, $filename) || die;
747 my $text = <FILE>;
748 close FILE;
749 $/ = "\n";
750
751
752 # include any metadata passed in from previous plugins
753 # note that this metadata is associated with the top level section
754# $self->extra_metadata ($doc_obj, $doc_obj->get_top_section(), $metadata);
755
756 print $outhandle "BBC Sound Archive Plug for BRS format: processing $file\n"
757 if $self->{'verbosity'} > 1;
758
759 # reset line number count
760 $brs_line_no = 0;
761
762 my @records = split /\*\*\* BRS DOCUMENT BOUNDARY \*\*\*\s+/, $text;
763 $text = "";
764
765 foreach $record (@records) {
766 next if $record !~ /\w/; # first record will be empty
767 my $brs_rec = &read_brs_record(\$record, $file, $self->{'outhandle'});
768 &process_brs_record ($processor, $brs_rec, $file, $self->{'outhandle'});
769 }
770
771 print $outhandle "\nNumber of BRS records = $brs_doc_count\n";
772 print $outhandle "Number of BRS records processed = $brs_processed_count\n";
773
774 return 1; # processed the file
775}
776
777sub end {
778 my $self = shift (@_);
779 my ($processor) = @_;
780
781 $processor->close_file_output() if defined $processor;
782}
783
784sub format_prognum {
785 my ($brs_rec) = @_;
786
787 return unless defined $brs_rec->{'PRNO'};
788
789 my $old_num = $brs_rec->{'PRNO'};
790
791 if (defined $brs_rec->{'magazine'}) {
792 if ($brs_rec->{'PRNO'} =~ /^\S\S /) { # space at position 3
793 if ($brs_rec->{'magazine'} =~ /(\d+)/) {
794 # algorithm A
795 my $magnumber = $1;
796 $brs_rec->{'PRNO'} =~ /(\S\S) (\S\S)(\S+)/;
797 $brs_rec->{'PRNO'} = $2 . $1 . $3 . $magnumber;
798 } else {
799 # common algorithm
800 $brs_rec->{'PRNO'} =~ /(\S\S) (\S\S)(\S+)/;
801 $brs_rec->{'PRNO'} = $2 . $1 . $3;
802 }
803 }
804 } else {
805 if ($brs_rec->{'PRNO'} =~ /^\S\S / && $brs_rec->{'PRNO'} !~ /:/) { # space at position 3 and no colon
806 # common algorithm
807 $brs_rec->{'PRNO'} =~ /(\S\S) (\S\S)(\S+)/;
808 $brs_rec->{'PRNO'} = $2 . $1 . $3;
809 } elsif ($brs_rec->{'PRNO'} =~ /^(\S):(\S\S) (\S\S)(\S+)/) { # colon at position 2 and space at position 5
810 # algorithm B
811 $brs_rec->{'PRNO'} = $3 . $2 . $4 . $1;
812 }
813 }
814
815# if ($brs_rec->{'PRNO'} ne $old_num) {
816# print STDERR "\n$old_num ... " . $brs_rec->{'PRNO'} . "\n";
817# }
818}
819
8201;
Note: See TracBrowser for help on using the repository browser.