Tamil/Biblio/Dumper.pm untested

filter: Statementstmt 0.0% 0 / 102 Branchbran 0.0% 0 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod n/a totaltotal 0.0% 0 / 133 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Koha::Contrib::Tamil::Biblio::Dumper;
2 # ABSTRACT: Class dumping a Koha Catalog
3 $Koha::Contrib::Tamil::Biblio::Dumper::VERSION = '0.070';
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 $self->koha->_old_marc_biblio_sub
83 ? "SELECT marcxml FROM biblioitems WHERE biblionumber=?"
84 : "SELECT metadata FROM biblio_metadata WHERE biblionumber=?"
85 ) );
86  
87 $query = "SELECT * FROM items WHERE biblionumber=?";
88 $query .= " AND $where" if $where;
89 #say $query;
90 $self->sth_item( $self->koha->dbh->prepare($query));
91  
92 my $fh = new IO::File '> ' . $self->file;
93 binmode($fh, ':encoding(utf8)');
94 $self->writer( MARC::Moose::Writer->new(
95 formater => $self->formater =~ /marcxml/i
96 ? MARC::Moose::Formater::Marcxml->new()
97 : MARC::Moose::Formater::Iso2709->new(),
98 fh => $fh ) );
99 };
100  
101  
102 before 'start_process' => sub {
103 shift->writer->begin();
104 };
105  
106  
107 override 'process' => sub {
108 my $self = shift;
109  
110 my ($biblionumber) = $self->sth->fetchrow;
111 return unless $biblionumber;
112  
113 # Get the biblio record
114 $self->sth_marcxml->execute($biblionumber);
115 my ($marcxml) = $self->sth_marcxml->fetchrow;
116 my $record = MARC::Moose::Record::new_from($marcxml, 'marcxml');
117  
118 # Construct item fields
119 $self->sth_item->execute($biblionumber);
120 while ( my $item = $self->sth_item->fetchrow_hashref ) {
121 my $imarc = Item2Marc($item, $biblionumber);
122 my $field = $imarc->field('952|995');
123 my $f = MARC::Moose::Field::Std->new(
124 tag => $field->tag,
125 subf => [ $field->subfields ]);
126 $record->append($f);
127 }
128  
129 # Specific conversion
130 $record = $self->convert->($record);
131  
132 $self->writer->write($record) if $record;
133 return super();
134 };
135  
136  
137 before 'end_process' => sub {
138 shift->writer->end();
139 };
140  
141  
142 override 'start_message' => sub {
143 my $self = shift;
144 say __x("Dump of Koha Catalog into file: {file}",
145 file => $self->file);
146 };
147  
148  
149 override 'process_message' => sub {
150 my $self = shift;
151 say $self->count;
152 };
153  
154  
155 override 'end_message' => sub {
156 my $self = shift;
157 say __x("Number of biblio records exported: {biblios}",
158 biblios => $self->count );
159 };
160  
161  
162 no Moose;
163 __PACKAGE__->meta->make_immutable;
164  
165 =pod
166  
167 =encoding UTF-8
168  
169 =head1 NAME
170  
171 Koha::Contrib::Tamil::Biblio::Dumper - Class dumping a Koha Catalog
172  
173 =head1 VERSION
174  
175 version 0.070
176  
177 =head1 SYNOPSIS
178  
179 my $converter = sub {
180 # Delete some fields
181 $record->fields(
182 [ grep { $_->tag !~ /012|014|071|099/ } @{$record->fields} ] );
183 return $record;
184 };
185 my $dumper = Koha::Contrib::Tamil::Biblio::Dumper->new(
186 file => 'biblio.mrc',
187 branches => [ qw/ MAIN ANNEX / ],
188 query => "SELECT biblionumber FROM biblio WHERE datecreated LIKE '2014-11%'"
189 convert => $converter,
190 formater => 'iso2709',
191 verbose => 1,
192 );
193 $dumper->run();
194  
195 =head1 ATTRIBUTES
196  
197 =head2 file
198  
199 Name of the file in which biblio records are exported. By default C<dump.mrc>.
200  
201 =head2 query
202  
203 Optional query to select biblio records to dump. The query must return a list
204 of biblio records C<biblionumber>. For example: C<SELECT biblionumber FROM
205 biblio WHERE biblionumber BETWEEN 1 AND 100>.
206  
207 =head2 convert
208  
209 A function which take in parameter a L<MARC::Moose::Record> biblio record, and
210 returns a converted record.
211  
212 =head2 formater
213  
214 Type of formater used to write in L<file> file. Default value is C<marcxml>.
215 Available values are: C<iso2709>, C<marcxml>, C<text>, C<json>, C<yaml>,
216 C<json>.
217  
218 =head2 verbose
219  
220 Verbosity. By default 0 (false).
221  
222 =head1 AUTHOR
223  
224 Frédéric Demians <f.demians@tamil.fr>
225  
226 =head1 COPYRIGHT AND LICENSE
227  
228 This software is Copyright (c) 2022 by Fréderic Démians.
229  
230 This is free software, licensed under:
231  
232 The GNU General Public License, Version 3, June 2007
233  
234 =cut
235  
236 __END__
237  
238 1;
239  
240