| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
|
| 6 |
our $VERSION = '0.10'; |
| 7 |
|
| 8 |
use Getopt::Long 'HelpMessage'; |
| 9 |
use MARC::Parser::RAW; |
| 10 |
use MARC::Parser::XML; |
| 11 |
use MARC::Schema; |
| 12 |
|
| 13 |
GetOptions( |
| 14 |
'file|f=s' => \my $file, |
| 15 |
'type|t=s' => \( my $type = 'RAW' ), |
| 16 |
'schema|s=s' => \my $schema_file, |
| 17 |
'help|h' => sub { HelpMessage() }, |
| 18 |
) or HelpMessage(); |
| 19 |
|
| 20 |
$file = shift unless defined($file); |
| 21 |
|
| 22 |
HelpMessage() unless defined $file and -e $file; |
| 23 |
|
| 24 |
my $schema = MARC::Schema->new({file => $schema_file}); |
| 25 |
|
| 26 |
my $parser; |
| 27 |
if ( $type eq 'RAW' ) { |
| 28 |
$parser = MARC::Parser::RAW->new($file); |
| 29 |
} |
| 30 |
elsif ( $type eq 'XML' ) { |
| 31 |
$parser = MARC::Parser::XML->new($file); |
| 32 |
} |
| 33 |
else { |
| 34 |
print q{type '$type' not supported. Use 'RAW' or 'XML'}; |
| 35 |
} |
| 36 |
|
| 37 |
my $record_count = 0; |
| 38 |
while ( my $record = $parser->next() ) { |
| 39 |
$record_count++; |
| 40 |
my $id = _id($record); |
| 41 |
my @error = $schema->check($record); |
| 42 |
if (@error > 0) { |
| 43 |
foreach my $error (@error) { |
| 44 |
if (exists $error->{value}) { |
| 45 |
print qq{$id\t$error->{tag}\t$error->{error}\t$error->{value}\n}; |
| 46 |
} else { |
| 47 |
print qq{$id\t$error->{tag}\t$error->{error}\t\n}; |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
sub _id { |
| 54 |
my ($record) = @_; |
| 55 |
my ($id) = map { $_->[-1] } grep { $_->[0] eq '001' } @$record; |
| 56 |
$id = defined $id ? $id : $record_count; |
| 57 |
return $id; |
| 58 |
} |
| 59 |
|
| 60 |
__END__ |
| 61 |
|
| 62 |
=encoding utf-8 |
| 63 |
|
| 64 |
=head1 NAME |
| 65 |
|
| 66 |
marcvalidate - Validate a file with MARC21 records |
| 67 |
|
| 68 |
=head1 SYNOPSIS |
| 69 |
|
| 70 |
$ marcvalidate [options] FILE |
| 71 |
|
| 72 |
options: |
| 73 |
--type,-t Type of MARC21 serialization (RAW|XML, default: RAW) |
| 74 |
--schema,-s Location MARC JSON schema |
| 75 |
--help,-h Print this help |
| 76 |
|
| 77 |
=head1 DESCRIPTION |
| 78 |
|
| 79 |
C<marcvalidate> validates a file with MARC21 records against L<MARC::Schema> |
| 80 |
and prints out errors as tab separated list (id, field tag, error, value). |
| 81 |
The ID is extracted from MARC21 field L<001|https://www.loc.gov/marc/bibliographic/bd001.html>. |
| 82 |
For a detailed description of the (default) schema see L<"MARC21 structure in JSON"|https://pkiraly.github.io/2018/01/28/marc21-in-json/>. |
| 83 |
|
| 84 |
|
| 85 |
=head1 AUTHOR |
| 86 |
|
| 87 |
Johann Rolschewski E<lt>[email protected]<gt> |
| 88 |
|
| 89 |
=head1 COPYRIGHT |
| 90 |
|
| 91 |
Copyright 2018- Johann Rolschewski |
| 92 |
|
| 93 |
=head1 LICENSE |
| 94 |
|
| 95 |
This library is free software; you can redistribute it and/or modify |
| 96 |
it under the same terms as Perl itself. |
| 97 |
|
| 98 |
=head1 SEE ALSO |
| 99 |
|
| 100 |
L<Catmandu::Validator> |
| 101 |
|
| 102 |
L<JSON::Schema> |
| 103 |
|
| 104 |
L<PICA::Schema> |
| 105 |
|
| 106 |
L<MARC::Lint> |
| 107 |
|
| 108 |
=cut |