| 1 | package Koha::Contrib::Tamil::Biblio::Dumper; | ||
| 2 | # ABSTRACT: Class dumping a Koha Catalog | ||
| 3 | $Koha::Contrib::Tamil::Biblio::Dumper::VERSION = '0.074'; | ||
| 4 | use Moose; | ||
| 5 | |||
| 6 | extends 'AnyEvent::Processor'; | ||
| 7 | |||
| 8 | use Modern::Perl; | ||
| 9 | use utf8; | ||
| 10 | use Koha::Contrib::Tamil::Koha; | ||
| 11 | use MARC::Moose::Record; | ||
| 12 | use MARC::Moose::Writer; | ||
| 13 | use MARC::Moose::Formater::Iso2709; | ||
| 14 | use C4::Biblio; | ||
| 15 | use C4::Items qw/ Item2Marc PrepareItemrecordDisplay /; | ||
| 16 | use Koha::Items; | ||
| 17 | use Locale::TextDomain 'Koha-Contrib-Tamil'; | ||
| 18 | |||
| 19 | |||
| 20 | has file => ( is => 'rw', isa => 'Str', default => 'dump.mrc' ); | ||
| 21 | |||
| 22 | has branches => ( | ||
| 23 | is => 'rw', | ||
| 24 | isa => 'ArrayRef', | ||
| 25 | default => sub { [] } | ||
| 26 | ); | ||
| 27 | |||
| 28 | |||
| 29 | has query => ( is => 'rw', isa => 'Str', default => '' ); | ||
| 30 | |||
| 31 | has convert => ( | ||
| 32 | is => 'rw', | ||
| 33 | default => sub { sub { | ||
| 34 | return shift; | ||
| 35 | } }, | ||
| 36 | ); | ||
| 37 | |||
| 38 | has formater => ( | ||
| 39 | is => 'rw', | ||
| 40 | isa => 'Str', | ||
| 41 | default => 'marcxml', | ||
| 42 | ); | ||
| 43 | |||
| 44 | has koha => ( | ||
| 45 | is => 'rw', | ||
| 46 | isa => 'Koha::Contrib::Tamil::Koha', | ||
| 47 | required => 0, | ||
| 48 | ); | ||
| 49 | |||
| 50 | has decode => ( is => 'rw', isa => 'Bool', default => 0 ); | ||
| 51 | |||
| 52 | has verbose => ( is => 'rw', isa => 'Bool', default => 0 ); | ||
| 53 | |||
| 54 | has sth => ( is => 'rw' ); | ||
| 55 | has sth_marcxml => ( is => 'rw' ); | ||
| 56 | has sth_item => ( is => 'rw' ); | ||
| 57 | |||
| 58 | has writer => ( is => 'rw', isa => 'MARC::Moose::Writer' ); | ||
| 59 | |||
| 60 | has code_to_value => ( is => 'rw', isa => 'HashRef' ); | ||
| 61 | |||
| 62 | before 'run' => sub { | ||
| 63 | my $self = shift; | ||
| 64 | |||
| 65 | $self->koha( Koha::Contrib::Tamil::Koha->new() ) unless $self->koha; | ||
| 66 | |||
| 67 | my $query = $self->query; | ||
| 68 | my $where = ""; | ||
| 69 | if ( my $branches = $self->branches ) { | ||
| 70 | $where = 'homebranch IN (' . | ||
| 71 | join(',', map {"'$_'" } @$branches) . ')' | ||
| 72 | if @$branches; | ||
| 73 | } | ||
| 74 | unless ($query) { | ||
| 75 | $query = $where | ||
| 76 | ? "SELECT DISTINCT biblionumber FROM items WHERE $where" | ||
| 77 | : "SELECT biblionumber FROM biblioitems"; | ||
| 78 | } | ||
| 79 | #say $query; | ||
| 80 | my $sth = $self->koha->dbh->prepare($query); | ||
| 81 | $sth->execute(); | ||
| 82 | $self->sth($sth); | ||
| 83 | |||
| 84 | $self->sth_marcxml( $self->koha->dbh->prepare( | ||
| 85 | "SELECT metadata FROM biblio_metadata WHERE biblionumber=?" | ||
| 86 | ) ); | ||
| 87 | |||
| 88 | $query = "SELECT * FROM items WHERE biblionumber=?"; | ||
| 89 | $query .= " AND $where" if $where; | ||
| 90 | #say $query; | ||
| 91 | $self->sth_item( $self->koha->dbh->prepare($query)); | ||
| 92 | |||
| 93 | # Récupération des décodages | ||
| 94 | if ($self->decode) { | ||
| 95 | my $av; | ||
| 96 | my $decode; | ||
| 97 | $query = "SELECT category, authorised_value, lib FROM authorised_values"; | ||
| 98 | for (@{$self->koha->dbh->selectall_arrayref($query)}) { | ||
| 99 | my ($category, $code, $decode) = @$_; | ||
| 100 | $av->{$category}->{$code} = $decode; | ||
| 101 | } | ||
| 102 | $query = "SELECT branchcode, branchname FROM branches"; | ||
| 103 | for (@{$self->koha->dbh->selectall_arrayref($query)}) { | ||
| 104 | my ($code, $decode) = @$_; | ||
| 105 | $av->{branches}->{$code} = $decode; | ||
| 106 | } | ||
| 107 | $query = "SELECT itemtype, description FROM itemtypes"; | ||
| 108 | for (@{$self->koha->dbh->selectall_arrayref($query)}) { | ||
| 109 | my ($code, $decode) = @$_; | ||
| 110 | $av->{itemtypes}->{$code} = $decode; | ||
| 111 | } | ||
| 112 | $query = "SELECT tagfield, tagsubfield, authorised_value | ||
| 113 | FROM marc_subfield_structure | ||
| 114 | WHERE frameworkcode='' AND authorised_value<>''"; | ||
| 115 | for (@{$self->koha->dbh->selectall_arrayref($query)}) { | ||
| 116 | my ($tag, $letter, $category) = @$_; | ||
| 117 | $decode->{$tag}->{$letter} = $av->{$category}; | ||
| 118 | } | ||
| 119 | $self->code_to_value($decode); | ||
| 120 | } | ||
| 121 | |||
| 122 | my $fh = new IO::File '> ' . $self->file; | ||
| 123 | binmode($fh, ':encoding(utf8)'); | ||
| 124 | $self->writer( MARC::Moose::Writer->new( | ||
| 125 | formater => $self->formater =~ /marcxml/i | ||
| 126 | ? MARC::Moose::Formater::Marcxml->new() | ||
| 127 | : MARC::Moose::Formater::Iso2709->new(), | ||
| 128 | fh => $fh ) ); | ||
| 129 | }; | ||
| 130 | |||
| 131 | |||
| 132 | before 'start_process' => sub { | ||
| 133 | shift->writer->begin(); | ||
| 134 | }; | ||
| 135 | |||
| 136 | |||
| 137 | override 'process' => sub { | ||
| 138 | my $self = shift; | ||
| 139 | |||
| 140 | my ($biblionumber) = $self->sth->fetchrow; | ||
| 141 | return unless $biblionumber; | ||
| 142 | |||
| 143 | # Get the biblio record | ||
| 144 | $self->sth_marcxml->execute($biblionumber); | ||
| 145 | my ($marcxml) = $self->sth_marcxml->fetchrow; | ||
| 146 | my $record = MARC::Moose::Record::new_from($marcxml, 'marcxml'); | ||
| 147 | |||
| 148 | # Construct item fields | ||
| 149 | $self->sth_item->execute($biblionumber); | ||
| 150 | while ( my $item = $self->sth_item->fetchrow_hashref ) { | ||
| 151 | my $imarc = Item2Marc($item, $biblionumber); | ||
| 152 | my $field = $imarc->field('952|995'); | ||
| 153 | my $f = MARC::Moose::Field::Std->new( | ||
| 154 | tag => $field->tag, | ||
| 155 | subf => [ $field->subfields ]); | ||
| 156 | $record->append($f); | ||
| 157 | } | ||
| 158 | |||
| 159 | if ($self->decode) { | ||
| 160 | my $decode = $self->code_to_value; | ||
| 161 | for my $field (@{$record->fields}) { | ||
| 162 | my $dec_tag = $decode->{$field->tag}; | ||
| 163 | next unless $dec_tag; | ||
| 164 | for (@{$field->subf}) { | ||
| 165 | my ($letter, $value) = @$_; | ||
| 166 | my $dec_letter = $dec_tag->{$letter}; | ||
| 167 | next unless $dec_letter; | ||
| 168 | my $decode = $dec_letter->{$value}; | ||
| 169 | next unless defined $decode; | ||
| 170 | $_->[1] = $decode; | ||
| 171 | |||
| 172 | } | ||
| 173 | } | ||
| 174 | } | ||
| 175 | |||
| 176 | # Specific conversion | ||
| 177 | $record = $self->convert->($record); | ||
| 178 | |||
| 179 | $self->writer->write($record) if $record; | ||
| 180 | return super(); | ||
| 181 | }; | ||
| 182 | |||
| 183 | |||
| 184 | before 'end_process' => sub { | ||
| 185 | shift->writer->end(); | ||
| 186 | }; | ||
| 187 | |||
| 188 | |||
| 189 | override 'start_message' => sub { | ||
| 190 | my $self = shift; | ||
| 191 | say __x("Dump of Koha Catalog into file: {file}", | ||
| 192 | file => $self->file); | ||
| 193 | }; | ||
| 194 | |||
| 195 | |||
| 196 | override 'process_message' => sub { | ||
| 197 | my $self = shift; | ||
| 198 | say $self->count; | ||
| 199 | }; | ||
| 200 | |||
| 201 | |||
| 202 | override 'end_message' => sub { | ||
| 203 | my $self = shift; | ||
| 204 | say __x("Number of biblio records exported: {biblios}", | ||
| 205 | biblios => $self->count ); | ||
| 206 | }; | ||
| 207 | |||
| 208 | |||
| 209 | no Moose; | ||
| 210 | __PACKAGE__->meta->make_immutable; | ||
| 211 | |||
| 212 | =pod | ||
| 213 | |||
| 214 | =encoding UTF-8 | ||
| 215 | |||
| 216 | =head1 NAME | ||
| 217 | |||
| 218 | Koha::Contrib::Tamil::Biblio::Dumper - Class dumping a Koha Catalog | ||
| 219 | |||
| 220 | =head1 VERSION | ||
| 221 | |||
| 222 | version 0.074 | ||
| 223 | |||
| 224 | =head1 SYNOPSIS | ||
| 225 | |||
| 226 | my $converter = sub { | ||
| 227 | # Delete some fields | ||
| 228 | $record->fields( | ||
| 229 | [ grep { $_->tag !~ /012|014|071|099/ } @{$record->fields} ] ); | ||
| 230 | return $record; | ||
| 231 | }; | ||
| 232 | my $dumper = Koha::Contrib::Tamil::Biblio::Dumper->new( | ||
| 233 | file => 'biblio.mrc', | ||
| 234 | branches => [ qw/ MAIN ANNEX / ], | ||
| 235 | query => "SELECT biblionumber FROM biblio WHERE datecreated LIKE '2014-11%'" | ||
| 236 | convert => $converter, | ||
| 237 | formater => 'iso2709', | ||
| 238 | verbose => 1, | ||
| 239 | ); | ||
| 240 | $dumper->run(); | ||
| 241 | |||
| 242 | =head1 ATTRIBUTES | ||
| 243 | |||
| 244 | =head2 file | ||
| 245 | |||
| 246 | Name of the file in which biblio records are exported. By default C<dump.mrc>. | ||
| 247 | |||
| 248 | =head2 query | ||
| 249 | |||
| 250 | Optional query to select biblio records to dump. The query must return a list | ||
| 251 | of biblio records C<biblionumber>. For example: C<SELECT biblionumber FROM | ||
| 252 | biblio WHERE biblionumber BETWEEN 1 AND 100>. | ||
| 253 | |||
| 254 | =head2 convert | ||
| 255 | |||
| 256 | A function which take in parameter a L<MARC::Moose::Record> biblio record, and | ||
| 257 | returns a converted record. | ||
| 258 | |||
| 259 | =head2 formater | ||
| 260 | |||
| 261 | Type of formater used to write in L<file> file. Default value is C<marcxml>. | ||
| 262 | Available values are: C<iso2709>, C<marcxml>, C<text>, C<json>, C<yaml>, | ||
| 263 | C<json>. | ||
| 264 | |||
| 265 | =head2 decode | ||
| 266 | |||
| 267 | Decode encoded values | ||
| 268 | |||
| 269 | =head2 verbose | ||
| 270 | |||
| 271 | Verbosity. By default 0 (false). | ||
| 272 | |||
| 273 | =head1 AUTHOR | ||
| 274 | |||
| 275 | Frédéric Demians <f.demians@tamil.fr> | ||
| 276 | |||
| 277 | =head1 COPYRIGHT AND LICENSE | ||
| 278 | |||
| 279 | This software is Copyright (c) 2025 by Fréderic Démians. | ||
| 280 | |||
| 281 | This is free software, licensed under: | ||
| 282 | |||
| 283 | The GNU General Public License, Version 3, June 2007 | ||
| 284 | |||
| 285 | =cut | ||
| 286 | |||
| 287 | __END__ |