| 1 | package Koha::Contrib::Tamil::Authority::EditorsBuilder; | ||
| 2 | $Koha::Contrib::Tamil::Authority::EditorsBuilder::VERSION = '0.074'; | ||
| 3 | use Moose; | ||
| 4 | |||
| 5 | extends 'AnyEvent::Processor'; | ||
| 6 | |||
| 7 | use 5.010; | ||
| 8 | use utf8; | ||
| 9 | use C4::Context; | ||
| 10 | use C4::Biblio; | ||
| 11 | use Business::ISBN; | ||
| 12 | use YAML; | ||
| 13 | |||
| 14 | |||
| 15 | has verbose => ( is => 'rw', isa => 'Bool' ); | ||
| 16 | |||
| 17 | has koha => ( is => 'rw', isa => 'Koha::Contrib::Tamil::Koha' ); | ||
| 18 | |||
| 19 | # Data structure containing isbn-code-collection found | ||
| 20 | # { | ||
| 21 | # '0-20' => [ | ||
| 22 | # "Tamil Press", | ||
| 23 | # [ | ||
| 24 | # "Koha Gold Collection", | ||
| 25 | # "Tamil success stories", | ||
| 26 | # "Forever happy collection", | ||
| 27 | # ] | ||
| 28 | # '2-930' => ... | ||
| 29 | has editor_from_isbn => ( is => 'rw', isa => 'HashRef', default => sub { {} } ); | ||
| 30 | |||
| 31 | # Tableau des éditeurs sans ISBN | ||
| 32 | has editor_without_isbn => ( is => 'rw', isa => 'HashRef', default => sub { {} } ); | ||
| 33 | |||
| 34 | # Tableau des éditeurs avec ISBN invalide | ||
| 35 | has editor_with_invalid_isbn => ( is => 'rw', isa => 'HashRef', default => sub { {} } ); | ||
| 36 | |||
| 37 | # Array of biblionumbers | ||
| 38 | has biblionumbers => ( is => 'rw', isa => 'ArrayRef' ); | ||
| 39 | |||
| 40 | |||
| 41 | |||
| 42 | before 'run' => sub { | ||
| 43 | my ($self, $delete) = @_; | ||
| 44 | |||
| 45 | my $sth = $self->koha->dbh->prepare( | ||
| 46 | "SELECT biblionumber | ||
| 47 | FROM biblioitems | ||
| 48 | "); | ||
| 49 | $sth->execute; | ||
| 50 | my @biblionumbers = (); | ||
| 51 | while (my ($biblionumber) = $sth->fetchrow) { | ||
| 52 | push @biblionumbers, $biblionumber; | ||
| 53 | } | ||
| 54 | $self->biblionumbers( \@biblionumbers ); | ||
| 55 | |||
| 56 | say "Step 1: Extracting isbn-editors-collections from biblio records" | ||
| 57 | if $self->verbose; | ||
| 58 | }; | ||
| 59 | |||
| 60 | |||
| 61 | override 'process' => sub { | ||
| 62 | my $self = shift; | ||
| 63 | |||
| 64 | if ( $self->count == @{$self->biblionumbers} ) { | ||
| 65 | # Nettoyage des listes d'ISBN | ||
| 66 | my %found; | ||
| 67 | for my $isbn ( keys %{ $self->editor_from_isbn } ) { | ||
| 68 | my $editor = $self->editor_from_isbn->{ $isbn }; | ||
| 69 | my $name = $editor->[0]; | ||
| 70 | $found{ $name } = 1; | ||
| 71 | } | ||
| 72 | for ( keys %{ $self->editor_without_isbn } ) { | ||
| 73 | delete $self->editor_without_isbn->{ $_ } if $found{$_}; | ||
| 74 | } | ||
| 75 | for ( keys %{ $self->editor_with_invalid_isbn } ) { | ||
| 76 | delete $self->editor_with_invalid_isbn->{ $_ } if $found{$_}; | ||
| 77 | } | ||
| 78 | return 0; | ||
| 79 | } | ||
| 80 | |||
| 81 | my $biblionumber = $self->biblionumbers->[$self->count]; | ||
| 82 | $self->SUPER::process(); | ||
| 83 | |||
| 84 | my $record = GetMarcBiblio($biblionumber); | ||
| 85 | return 1 unless $record; | ||
| 86 | |||
| 87 | # Si pas d'éditeur, on ne fait rien | ||
| 88 | my $name = $record->field('210'); | ||
| 89 | return 1 unless $name; | ||
| 90 | $name = $name->subfield('c'); | ||
| 91 | return 1 unless $name; | ||
| 92 | |||
| 93 | # Si on n'a pas d'ISBN, on ne fait rien et on met de côté le nom de | ||
| 94 | # l'éditeur | ||
| 95 | my $isbn = $record->field('010'); | ||
| 96 | unless ( $isbn ) { | ||
| 97 | $self->editor_without_isbn->{ $name } = 1; | ||
| 98 | return 1; | ||
| 99 | } | ||
| 100 | $isbn = $isbn->subfield('a'); | ||
| 101 | unless ( $isbn ) { | ||
| 102 | $self->editor_without_isbn->{ $name } = 1; | ||
| 103 | return 1; | ||
| 104 | } | ||
| 105 | |||
| 106 | # On normalise l'ISBN | ||
| 107 | $isbn = Business::ISBN->new($isbn); | ||
| 108 | unless ( $isbn ) { | ||
| 109 | $self->editor_with_invalid_isbn->{ $name } = 1; | ||
| 110 | return 1; | ||
| 111 | } | ||
| 112 | unless ( $isbn->is_valid ) { | ||
| 113 | $self->editor_with_invalid_isbn->{ $name } = 1; | ||
| 114 | return 1; | ||
| 115 | } | ||
| 116 | my $isbn_prefix = $isbn->group_code . '-' . $isbn->publisher_code; | ||
| 117 | return 1 unless $isbn_prefix; | ||
| 118 | |||
| 119 | my $collection = $record->field('225'); | ||
| 120 | $collection = $collection | ||
| 121 | ? $collection->subfield('a') || '_SANS_COLLECTION_' | ||
| 122 | : '_SANS_COLLECTION'; | ||
| 123 | |||
| 124 | my $editor = $self->editor_from_isbn->{ $isbn_prefix } || | ||
| 125 | ( $self->editor_from_isbn->{ $isbn_prefix } = [ $name, {}, ] ); | ||
| 126 | $editor->[1]->{$collection}++; | ||
| 127 | |||
| 128 | return 1; | ||
| 129 | }; | ||
| 130 | |||
| 131 | |||
| 132 | override 'process_message' => sub { | ||
| 133 | my $self = shift; | ||
| 134 | my $total = @{ $self->biblionumbers } + 0; | ||
| 135 | my $percent = $self->count * 100 / $total; | ||
| 136 | print sprintf(" %#6d / %d (%d", $self->count, $total, $percent) . "%)\n"; | ||
| 137 | }; | ||
| 138 | |||
| 139 | |||
| 140 | no Moose; | ||
| 141 | __PACKAGE__->meta->make_immutable; | ||
| 142 | 1; | ||
| 143 | |||
| 144 | __END__ |