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