| 1 | package MARC::Moose::Lint::Processor; | ||
| 2 | # ABSTRACT: Processor to lint a biblio records file | ||
| 3 | $MARC::Moose::Lint::Processor::VERSION = '1.0.46'; | ||
| 4 | use Moose; | ||
| 5 | use Modern::Perl; | ||
| 6 | use MARC::Moose::Reader::File::Iso2709; | ||
| 7 | use MARC::Moose::Writer; | ||
| 8 | use MARC::Moose::Lint::Checker; | ||
| 9 | |||
| 10 | extends 'AnyEvent::Processor'; | ||
| 11 | |||
| 12 | has lint => ( | ||
| 13 | is => 'rw', | ||
| 14 | ); | ||
| 15 | |||
| 16 | |||
| 17 | has file => ( | ||
| 18 | is => 'ro', | ||
| 19 | isa => 'Str', | ||
| 20 | trigger => sub { | ||
| 21 | my ($self, $file) = @_; | ||
| 22 | $self->reader( MARC::Moose::Reader::File::Iso2709->new( | ||
| 23 | file => $file, | ||
| 24 | parser => MARC::Moose::Parser::Iso2709->new( lint => $self->lint ) ) ); | ||
| 25 | $self->writer_ok( MARC::Moose::Writer->new( | ||
| 26 | formater => MARC::Moose::Formater::Iso2709->new(), | ||
| 27 | fh => IO::File->new("$file.ok", ">:encoding(utf8)") | ||
| 28 | ) ); | ||
| 29 | $self->writer_bad( MARC::Moose::Writer->new( | ||
| 30 | formater => MARC::Moose::Formater::Iso2709->new(), | ||
| 31 | fh => IO::File->new("$file.bad", ">:encoding(utf8)") | ||
| 32 | ) ); | ||
| 33 | $self->fh_log( IO::File->new("$file.log", ">:encoding(utf8)") ); | ||
| 34 | }, | ||
| 35 | ); | ||
| 36 | |||
| 37 | |||
| 38 | has cleaner => (is => 'rw', isa => 'MARC::Moose::Formater'); | ||
| 39 | |||
| 40 | |||
| 41 | has reader => (is => 'rw', isa => 'MARC::Moose::Reader' ); | ||
| 42 | |||
| 43 | |||
| 44 | has writer_ok => (is => 'rw', isa => 'MARC::Moose::Writer' ); | ||
| 45 | |||
| 46 | |||
| 47 | has writer_bad => (is => 'rw', isa => 'MARC::Moose::Writer' ); | ||
| 48 | |||
| 49 | |||
| 50 | has fh_log => (is => 'rw', isa => 'IO::File'); | ||
| 51 | |||
| 52 | has count_ok => (is => 'rw', isa => 'Int', default => 0); | ||
| 53 | has count_bad => (is => 'rw', isa => 'Int', default => 0); | ||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | sub process { | ||
| 58 | my $self = shift; | ||
| 59 | |||
| 60 | my $record = $self->reader->read(); | ||
| 61 | return unless $record; | ||
| 62 | |||
| 63 | $record = $self->cleaner->format($record) if $self->cleaner; | ||
| 64 | |||
| 65 | if ( my @result = $record->check() ) { | ||
| 66 | $self->writer_bad->write($record); | ||
| 67 | my $rectext = $record->as('Text'); | ||
| 68 | chop $rectext; | ||
| 69 | my $fh = $self->fh_log; | ||
| 70 | print $fh $rectext, '-' x 80, "\n", join("\n", @result), "\n\n"; | ||
| 71 | $self->count_bad( $self->count_bad + 1 ); | ||
| 72 | } | ||
| 73 | else { | ||
| 74 | $self->writer_ok->write($record); | ||
| 75 | $self->count_ok( $self->count_ok + 1 ); | ||
| 76 | } | ||
| 77 | $self->SUPER::process(); | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 81 | sub start_message { | ||
| 82 | say "Start linting biblio records"; | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | sub process_message { | ||
| 87 | my $self = shift; | ||
| 88 | say $self->count, " - OK ", $self->count_ok, " - BAD ", $self->count_bad; | ||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | sub end_message { | ||
| 93 | my $self = shift; | ||
| 94 | my $file = $self->file; | ||
| 95 | say "Processed records: ", $self->count, "\n", | ||
| 96 | "Valid records: ", $self->count_ok, "\n", | ||
| 97 | "Invalid records: ", $self->count_bad, "\n", | ||
| 98 | "Lint result files: $file.ok, $file.bad, $file.log", ; | ||
| 99 | } | ||
| 100 | |||
| 101 | __PACKAGE__->meta->make_immutable; | ||
| 102 | |||
| 103 | 1; | ||
| 104 | |||
| 105 | __END__ | ||
| 106 | |||
| 107 | =pod | ||
| 108 | |||
| 109 | =encoding UTF-8 | ||
| 110 | |||
| 111 | =head1 NAME | ||
| 112 | |||
| 113 | MARC::Moose::Lint::Processor - Processor to lint a biblio records file | ||
| 114 | |||
| 115 | =head1 VERSION | ||
| 116 | |||
| 117 | version 1.0.46 | ||
| 118 | |||
| 119 | =head1 ATTRIBUTES | ||
| 120 | |||
| 121 | =head2 lint | ||
| 122 | |||
| 123 | A L<MARC::Moose::Lint::Checker> to be used to validate biblio record. | ||
| 124 | |||
| 125 | =head2 file | ||
| 126 | |||
| 127 | The name of an ISO2709 file containing biblio records to control. When the | ||
| 128 | processor object is created with this attribute, other attributes are | ||
| 129 | automatically constructed: C<reader> as L<MARC::Moose::Reader::File::Iso2709> | ||
| 130 | object reading the file, C<writer_ok> and C<writer_bad> as | ||
| 131 | L<MARC::Moose::Writer> object with an ISO2709 formater writing to files named | ||
| 132 | F<file.ok> and F<file.bad>, and C<fh_log> as a L<IO::File>, writing a text file | ||
| 133 | named F<file.log>. | ||
| 134 | |||
| 135 | =head2 cleaner | ||
| 136 | |||
| 137 | A L<MARC::Moose::Formater> which transform a L<MARC::Moose::Record> into | ||
| 138 | another L<MARC::Moose::Record>. Using a cleaner, it's possible to clean biblio | ||
| 139 | records before validating them. | ||
| 140 | |||
| 141 | =head2 reader | ||
| 142 | |||
| 143 | A L<MARC::Moose::Reader> object from which biblio records are read. | ||
| 144 | |||
| 145 | =head2 writer_ok | ||
| 146 | |||
| 147 | A L<MARC::Moose::Writer> object in which valid biblio records are written. | ||
| 148 | |||
| 149 | =head2 writer_bad | ||
| 150 | |||
| 151 | A L<MARC::Moose::Writer> object in which invalid biblio records are written. | ||
| 152 | |||
| 153 | =head2 writer_bad | ||
| 154 | |||
| 155 | A L<IO::File> file handle which is used to write invalid biblio records with | ||
| 156 | generated warnings. | ||
| 157 | |||
| 158 | =head1 SYNOPSYS | ||
| 159 | |||
| 160 | package PeterCleaner; | ||
| 161 | |||
| 162 | use Moose; | ||
| 163 | |||
| 164 | extends 'MARC::Moose::Formater'; | ||
| 165 | |||
| 166 | override 'format' => sub { | ||
| 167 | my ($self, $record) = @_; | ||
| 168 | |||
| 169 | for my $field (@{$record->fields}) { | ||
| 170 | # clean content | ||
| 171 | } | ||
| 172 | return $record; | ||
| 173 | }; | ||
| 174 | |||
| 175 | package Main; | ||
| 176 | |||
| 177 | my $processor = MARC::Moose::Lint::Processor->new( | ||
| 178 | lint => MARC::Moose::Lint::Checker::RulesFile->new( file => 'unimarc.rules', | ||
| 179 | file => 'biblio.mrc', | ||
| 180 | cleaner => PeterCleaner->new(), | ||
| 181 | verbose => 1, | ||
| 182 | }; | ||
| 183 | $processor->run(); | ||
| 184 | |||
| 185 | The above script validates an ISO2709 file named F<biblio.mrc> on a rules file | ||
| 186 | named F<unimarc.rules>. As a result, 3 files are created: (1) | ||
| 187 | F<biblio.mrc.ok>, an ISO2709 containing biblio records complying to the rules, | ||
| 188 | (2) F<biblio.mrc.bad> containing biblios violating the rules, and (3) | ||
| 189 | F<biblio.mrc.log> containing a textual representation of biblio records | ||
| 190 | violating the rules + a description of violated rules. | ||
| 191 | |||
| 192 | A more specific construction is also possible: | ||
| 193 | |||
| 194 | my $lint => MARC::Moose::Lint::Checker::RulesFile->new( file => 'marc21.rules' ); | ||
| 195 | my $processor = MARC::Moose::Lint::Processor->new( | ||
| 196 | reader => MARC::Moose::Reader::File::Marcxml->new( | ||
| 197 | file => 'biblio.xml', | ||
| 198 | parser => MARC::Moose::Parser::Marcxml->new( lint => $lint ), | ||
| 199 | writer_ok => MARC::Moose::Writer->new( | ||
| 200 | formater => MARC::Moose::Formater::Marcxml->new(), | ||
| 201 | fh => IO::File->new('ok.xml', '>:encoding(utf8') | ||
| 202 | ), | ||
| 203 | writer_bad => MARC::Moose::Writer->new( | ||
| 204 | formater => MARC::Moose::Formater::Marcxml->new(), | ||
| 205 | fh => IO::File->new('bad.xml', '>:encoding(utf8')) | ||
| 206 | ), | ||
| 207 | fh_log => IO::File->new('warnings.log', '>:encoding(utf8')), | ||
| 208 | verbose => 1, | ||
| 209 | ); | ||
| 210 | $processor->run(); | ||
| 211 | |||
| 212 | =head1 SEE ALSO | ||
| 213 | |||
| 214 | =over 4 | ||
| 215 | |||
| 216 | =item * | ||
| 217 | |||
| 218 | L<MARC::Moose> | ||
| 219 | |||
| 220 | =item * | ||
| 221 | |||
| 222 | L<MARC::Moose::Lint::Checker> | ||
| 223 | |||
| 224 | =item * | ||
| 225 | |||
| 226 | L<MARC::Moose::Lint::Checker::RulesFile> | ||
| 227 | |||
| 228 | =back | ||
| 229 | |||
| 230 | =head1 AUTHOR | ||
| 231 | |||
| 232 | Frédéric Demians <f.demians@tamil.fr> | ||
| 233 | |||
| 234 | =head1 COPYRIGHT AND LICENSE | ||
| 235 | |||
| 236 | This software is copyright (c) 2022 by Frédéric Demians. | ||
| 237 | |||
| 238 | This is free software; you can redistribute it and/or modify it under | ||
| 239 | the same terms as the Perl 5 programming language system itself. | ||
| 240 | |||
| 241 | =cut |