| 1 | package Koha::Contrib::Tamil::Authority::FromBiblioTask; | ||
| 2 | # ABSTRACT: Task extracting authorities from biblio records | ||
| 3 | $Koha::Contrib::Tamil::Authority::FromBiblioTask::VERSION = '0.074'; | ||
| 4 | use Moose; | ||
| 5 | |||
| 6 | use Koha::Contrib::Tamil::Koha; | ||
| 7 | use Koha::Contrib::Tamil::RecordReader; | ||
| 8 | use MARC::Record; | ||
| 9 | use File::Temp qw( tempfile ); | ||
| 10 | use YAML::Syck; | ||
| 11 | use List::Util qw( first ); | ||
| 12 | use Locale::TextDomain 'Koha-Contrib-Tamil'; | ||
| 13 | |||
| 14 | |||
| 15 | extends 'Koha::Contrib::Tamil::Authority::Task'; | ||
| 16 | |||
| 17 | has tmp_file_fh => ( is => 'rw' ); | ||
| 18 | |||
| 19 | has tmp_file => ( is => 'rw', isa => 'Str' ); | ||
| 20 | |||
| 21 | has reader => ( is => 'rw', isa => 'Koha::Contrib::Tamil::RecordReader' ); | ||
| 22 | |||
| 23 | has output => ( is => 'rw', isa => 'Str' ); | ||
| 24 | |||
| 25 | |||
| 26 | |||
| 27 | sub run { | ||
| 28 | my $self = shift; | ||
| 29 | |||
| 30 | my ( $tmp_file_fh, $tmp_file ) = tempfile( $self->output . '.XXXX' ); | ||
| 31 | binmode( $tmp_file_fh, ":utf8" ); | ||
| 32 | $self->tmp_file_fh( $tmp_file_fh ); | ||
| 33 | $self->tmp_file( $tmp_file ); | ||
| 34 | |||
| 35 | my $reader = Koha::Contrib::Tamil::RecordReader->new( | ||
| 36 | koha => Koha::Contrib::Tamil::Koha->new() ) ; | ||
| 37 | $self->reader( $reader ); | ||
| 38 | |||
| 39 | my $output = $self->output; | ||
| 40 | print __x("Create authorities from biblio records\n" . | ||
| 41 | " source: Koha DB\n" . | ||
| 42 | " target: {output}\n" . | ||
| 43 | " temporary file: {tmp_file}\n", | ||
| 44 | output => $output, tmp_file => $tmp_file); | ||
| 45 | |||
| 46 | $self->SUPER::run(); | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | sub process { | ||
| 51 | my $self = shift; | ||
| 52 | my $record = $self->reader->read(); | ||
| 53 | if ( $record ) { | ||
| 54 | $self->SUPER::process(); | ||
| 55 | # print $record->as_formatted(), "\n"; | ||
| 56 | foreach my $authority ( @{ $self->conf_authorities } ) { # loop on all authority types | ||
| 57 | #print "authority: ", $authority->{authcode}, "\n"; | ||
| 58 | my @bibliotags = @{$authority->{bibliotags}}; | ||
| 59 | foreach my $tag ( @bibliotags ) { | ||
| 60 | # loop on all biblio tags related to the current authority | ||
| 61 | foreach my $field ( $record->field( $tag ) ) { | ||
| 62 | # All field repetitions | ||
| 63 | my $concat = ''; | ||
| 64 | foreach my $subfield ( $field->subfields() ) { | ||
| 65 | my ($letter, $value) = @$subfield; | ||
| 66 | next if $letter !~ /[a-zA-Z0-9]/; | ||
| 67 | #chop $value; | ||
| 68 | $value =~ s/^\s+//; | ||
| 69 | $value =~ s/\s+$//; | ||
| 70 | $value =~ /([\w ,.'-_]+)/; | ||
| 71 | $value = $1; | ||
| 72 | $value =~ s/^\s+//; | ||
| 73 | $value =~ s/\s+$//; | ||
| 74 | $value =~ s/(;|,|;|!|\?)$//; | ||
| 75 | $value =~ s/\s+$//; | ||
| 76 | $value = ucfirst $value; | ||
| 77 | if ( $authority->{authletters} =~ /$letter/ ) { | ||
| 78 | $concat .= "\t" if $concat; | ||
| 79 | $concat .= "$letter|$value"; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | my $fh = $self->tmp_file_fh; | ||
| 83 | print $fh $authority->{authcode}, "\t$concat\n" | ||
| 84 | if $concat; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | return 1; | ||
| 90 | } | ||
| 91 | |||
| 92 | print __x(" bibios count: {count}", | ||
| 93 | count => $self->count), "\n"; | ||
| 94 | |||
| 95 | my $tmp_file = $self->tmp_file; | ||
| 96 | my $output = $self->output; | ||
| 97 | |||
| 98 | print __x( | ||
| 99 | "Sort and de-duplicate\n" . | ||
| 100 | " source: {tmp_file}\n" . | ||
| 101 | " target: {output}\n", | ||
| 102 | tmp_file => $tmp_file, | ||
| 103 | output => $output ); | ||
| 104 | system( "sort -f $tmp_file | uniq -i >$output" ); | ||
| 105 | |||
| 106 | return 0; | ||
| 107 | } | ||
| 108 | |||
| 109 | no Moose; | ||
| 110 | __PACKAGE__->meta->make_immutable; | ||
| 111 | |||
| 112 | 1; | ||
| 113 | |||
| 114 | __END__ |