1 |
package WebPAC::Normalise::XML; |
package WebPAC::Normalize::XML; |
2 |
|
|
3 |
use warnings; |
use warnings; |
4 |
use strict; |
use strict; |
5 |
|
|
6 |
use base qw/WebPAC::Common/; |
use base qw/WebPAC::Common WebPAC::Normalize/; |
7 |
|
use XML::Simple; |
8 |
|
use Data::Dumper; |
9 |
|
use Text::Iconv; |
10 |
|
use YAML qw/Dump LoadFile/; |
11 |
|
|
12 |
=head1 NAME |
=head1 NAME |
13 |
|
|
14 |
WebPAC::Normalise::XML - apply XML normalisaton rules |
WebPAC::Normalize::XML - apply XML or YAML normalisaton rules |
15 |
|
|
16 |
=head1 VERSION |
=head1 VERSION |
17 |
|
|
18 |
Version 0.01 |
Version 0.03 |
19 |
|
|
20 |
=cut |
=cut |
21 |
|
|
22 |
our $VERSION = '0.01'; |
our $VERSION = '0.03'; |
23 |
|
|
24 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
25 |
|
|
26 |
This module uses C<conf/normalize/*.xml> files to perform normalisation |
This module uses C<conf/normalize/*.xml> files to perform normalisation |
27 |
from input records |
from input records |
28 |
|
|
|
use WebPAC::Normalise::XML; |
|
|
|
|
|
my $foo = WebPAC::Normalise::XML->new(); |
|
|
... |
|
|
|
|
29 |
=cut |
=cut |
30 |
|
|
|
# mapping between data type and tag which specify |
|
|
# format in XML file |
|
|
my %type2tag = ( |
|
|
'isis' => 'isis', |
|
|
# 'excel' => 'column', |
|
|
# 'marc' => 'marc', |
|
|
# 'feed' => 'feed' |
|
|
); |
|
|
|
|
|
|
|
|
=head1 EXPORT |
|
|
|
|
|
A list of functions that can be exported. You can delete this section |
|
|
if you don't export anything, such as for a purely object-oriented module. |
|
|
|
|
31 |
=head1 FUNCTIONS |
=head1 FUNCTIONS |
32 |
|
|
33 |
=head2 new |
=head2 open |
34 |
|
|
35 |
Create new instance of WebPAC using configuration specified by C<config_file>. |
Read normalisation rules defined using XML from C<conf/normalize/*.xml> and |
36 |
|
parse it. |
37 |
|
|
38 |
my $n = new WebPAC::Normalize::XML( |
my $n = new WebPAC::Normalize::XML; |
39 |
cache_data_structure => './cache/ds/', |
$n->open( |
40 |
} |
tag => 'isis', |
41 |
|
xml_file => '/path/to/conf/normalize/isis.xml', |
42 |
|
); |
43 |
|
|
44 |
Optional parameter C<cache_data_structure> defines path to directory |
C<tag> defines tag to use within C<xml_file> |
|
in which cache file for C<data_structure> call will be created. |
|
|
|
|
|
=cut |
|
45 |
|
|
46 |
sub new { |
C<xml_file> defines path to normalize XML |
|
my $class = shift; |
|
|
my $self = {@_}; |
|
|
bless($self, $class); |
|
47 |
|
|
48 |
$self->setup_cache_dir( $self->{'cache_data_structure'} ); |
C<tags> define additional tags that can be forced (and an be array). |
|
|
|
|
return $self; |
|
|
} |
|
|
|
|
|
=head2 open_import_xml |
|
|
|
|
|
Read file from C<import_xml/> directory and parse it. |
|
|
|
|
|
$webpac->open_import_xml(type => 'isis'); |
|
49 |
|
|
50 |
=cut |
=cut |
51 |
|
|
52 |
sub open_import_xml { |
sub open { |
53 |
my $self = shift; |
my $self = shift; |
54 |
|
|
|
my $log = $self->_get_logger(); |
|
|
|
|
55 |
my $arg = {@_}; |
my $arg = {@_}; |
|
$log->logconfess("need type to load file from import_xml/") if (! $arg->{'type'}); |
|
56 |
|
|
57 |
$self->{'type'} = $arg->{'type'}; |
my $log = $self->_get_logger(); |
58 |
|
|
59 |
my $type_base = $arg->{'type'}; |
foreach my $req (qw/tag xml_file/) { |
60 |
$type_base =~ s/_.*$//g; |
$log->logconfess("need argument $req") unless $arg->{$req}; |
61 |
|
} |
62 |
|
|
63 |
$self->{'tag'} = $type2tag{$type_base}; |
$self->{'tag'} = $arg->{'tag'}; |
64 |
|
my $xml_file = $arg->{'xml_file'}; |
65 |
|
|
66 |
$log->info("using type '",$self->{'type'},"' tag <",$self->{'tag'},">"); |
$log->info("using $xml_file tag <",$self->{'tag'},">"); |
67 |
|
|
68 |
my $f = "./import_xml/".$self->{'type'}.".xml"; |
$log->logdie("normalisation xml file '$xml_file' doesn't exist!") if (! -e $xml_file); |
|
$log->logconfess("import_xml file '$f' doesn't exist!") if (! -e "$f"); |
|
69 |
|
|
70 |
$log->info("reading '$f'"); |
$self->{'import_xml_file'} = $xml_file; |
71 |
|
|
72 |
$self->{'import_xml_file'} = $f; |
my @force_array = [ $self->{'tag'}, 'config', 'format' ]; |
73 |
|
push @force_array, $self->{'tags'} if ($self->{'tags'}); |
74 |
|
|
75 |
$self->{'import_xml'} = XMLin($f, |
$self->{'import_xml'} = XMLin($xml_file, |
76 |
ForceArray => [ $self->{'tag'}, 'config', 'format' ], |
ForceArray => @force_array, |
77 |
|
ForceContent => 1, |
78 |
); |
); |
79 |
|
|
80 |
$log->debug("import xml is ",sub { Dumper($self->{'import_xml'}) }); |
$log->debug("import xml is ",sub { Dumper($self->{'import_xml'}) }, $self->{lookup} ? " using lookups" : "lookups disabled"); |
|
|
|
|
} |
|
|
|
|
|
=head2 setup_cache_dir |
|
|
|
|
|
Check if specified cache directory exist, and if not, disable caching. |
|
|
|
|
|
$setup_cache_dir('./cache/ds/'); |
|
81 |
|
|
82 |
If you pass false or zero value to this function, it will disable |
#print STDERR Dump($self->{import_xml}); |
|
cacheing. |
|
|
|
|
|
=cut |
|
|
|
|
|
sub setup_cache_dir { |
|
|
my $self = shift; |
|
|
|
|
|
my $dir = shift; |
|
|
|
|
|
my $log = $self->_get_logger(); |
|
83 |
|
|
84 |
if ($dir) { |
return $self; |
|
my $msg; |
|
|
if (! -e $dir) { |
|
|
$msg = "doesn't exist"; |
|
|
} elsif (! -d $dir) { |
|
|
$msg = "is not directory"; |
|
|
} elsif (! -w $dir) { |
|
|
$msg = "not writable"; |
|
|
} |
|
|
|
|
|
if ($msg) { |
|
|
undef $self->{'cache_data_structure'}; |
|
|
$log->warn("cache_data_structure $dir $msg, disabling..."); |
|
|
} else { |
|
|
$log->debug("using cache dir $dir"); |
|
|
} |
|
|
} else { |
|
|
$log->debug("disabling cache"); |
|
|
undef $self->{'cache_data_structure'}; |
|
|
} |
|
85 |
} |
} |
86 |
|
|
87 |
|
=head2 open_yaml |
88 |
|
|
89 |
=head2 data_structure |
Read normalisation rules defined in YAML file located usually at |
90 |
|
C<conf/normalize/*.yml> and parse it. |
|
Create in-memory data structure which represents layout from C<import_xml>. |
|
|
It is used later to produce output. |
|
|
|
|
|
my @ds = $webpac->data_structure($rec); |
|
91 |
|
|
92 |
This method will also set C<$webpac->{'currnet_filename'}> if there is |
my $n = new WebPAC::Normalize::XML; |
93 |
<filename> tag in C<import_xml> and C<$webpac->{'headline'}> if there is |
$n->open_yaml( |
94 |
<headline> tag. |
tag => 'isis', |
95 |
|
path => '/path/to/conf/normalize/isis.yml', |
96 |
|
); |
97 |
|
|
98 |
=cut |
=cut |
99 |
|
|
100 |
sub data_structure { |
sub open_yaml { |
101 |
my $self = shift; |
my $self = shift; |
102 |
|
|
103 |
my $log = $self->_get_logger(); |
my $arg = {@_}; |
|
|
|
|
my $rec = shift; |
|
|
$log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o); |
|
|
|
|
|
my $cache_file; |
|
|
|
|
|
if (my $cache_path = $self->{'cache_data_structure'}) { |
|
|
my $id = $rec->{'000'}; |
|
|
$id = $rec->{'000'}->[0] if ($id =~ m/^ARRAY/o); |
|
|
unless (defined($id)) { |
|
|
$log->warn("Can't use cache_data_structure on records without unique identifier in field 000"); |
|
|
undef $self->{'cache_data_structure'}; |
|
|
} else { |
|
|
$cache_file = "$cache_path/$id"; |
|
|
if (-r $cache_file) { |
|
|
my $ds_ref = retrieve($cache_file); |
|
|
if ($ds_ref) { |
|
|
$log->debug("cache hit: $cache_file"); |
|
|
my $ok = 1; |
|
|
foreach my $f (qw(current_filename headline)) { |
|
|
if ($ds_ref->{$f}) { |
|
|
$self->{$f} = $ds_ref->{$f}; |
|
|
} else { |
|
|
$ok = 0; |
|
|
} |
|
|
}; |
|
|
if ($ok && $ds_ref->{'ds'}) { |
|
|
return @{ $ds_ref->{'ds'} }; |
|
|
} else { |
|
|
$log->warn("cache_data_structure $cache_path corrupt. Use rm $cache_path/* to re-create it on next run!"); |
|
|
undef $self->{'cache_data_structure'}; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
104 |
|
|
105 |
undef $self->{'currnet_filename'}; |
my $log = $self->_get_logger(); |
|
undef $self->{'headline'}; |
|
106 |
|
|
107 |
my @sorted_tags; |
foreach my $req (qw/tag path/) { |
108 |
if ($self->{tags_by_order}) { |
$log->logconfess("need argument $req") unless $arg->{$req}; |
|
@sorted_tags = @{$self->{tags_by_order}}; |
|
|
} else { |
|
|
@sorted_tags = sort { $self->_sort_by_order } keys %{$self->{'import_xml'}->{'indexer'}}; |
|
|
$self->{tags_by_order} = \@sorted_tags; |
|
109 |
} |
} |
110 |
|
|
111 |
my @ds; |
my $path = $arg->{path}; |
112 |
|
$self->{tag} = $arg->{tag}; |
|
$log->debug("tags: ",sub { join(", ",@sorted_tags) }); |
|
|
|
|
|
foreach my $field (@sorted_tags) { |
|
|
|
|
|
my $row; |
|
|
|
|
|
#print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}); |
|
113 |
|
|
114 |
foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) { |
$log->logdie("normalisation yaml file '$path' doesn't exist!") if (! -e $path); |
|
my $format = $tag->{'value'} || $tag->{'content'}; |
|
115 |
|
|
116 |
$log->debug("format: $format"); |
$log->info("using $path normalization YAML"); |
117 |
|
|
118 |
my @v; |
$self->{'import_xml'} = LoadFile( $path ) || $log->die("can't load $path: $!"); |
|
if ($format =~ /$LOOKUP_REGEX/o) { |
|
|
@v = $self->fill_in_to_arr($rec,$format); |
|
|
} else { |
|
|
@v = $self->parse_to_arr($rec,$format); |
|
|
} |
|
|
next if (! @v); |
|
119 |
|
|
120 |
if ($tag->{'sort'}) { |
$log->debug("import yaml is ",sub { Dumper($self->{'import_xml'}) }, $self->{lookup} ? " using lookups" : "lookups disabled"); |
|
@v = $self->sort_arr(@v); |
|
|
} |
|
121 |
|
|
122 |
# use format? |
$self->{_skip_x} = 1; |
|
if ($tag->{'format_name'}) { |
|
|
@v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v; |
|
|
} |
|
123 |
|
|
124 |
if ($field eq 'filename') { |
return $self; |
125 |
$self->{'current_filename'} = join('',@v); |
} |
|
$log->debug("filename: ",$self->{'current_filename'}); |
|
|
} elsif ($field eq 'headline') { |
|
|
$self->{'headline'} .= join('',@v); |
|
|
$log->debug("headline: ",$self->{'headline'}); |
|
|
next; # don't return headline in data_structure! |
|
|
} |
|
|
|
|
|
# delimiter will join repeatable fields |
|
|
if ($tag->{'delimiter'}) { |
|
|
@v = ( join($tag->{'delimiter'}, @v) ); |
|
|
} |
|
|
|
|
|
# default types |
|
|
my @types = qw(display swish); |
|
|
# override by type attribute |
|
|
@types = ( $tag->{'type'} ) if ($tag->{'type'}); |
|
|
|
|
|
foreach my $type (@types) { |
|
|
# append to previous line? |
|
|
$log->debug("type: $type ",sub { join(" ",@v) }, $row->{'append'} || 'no append'); |
|
|
if ($tag->{'append'}) { |
|
|
|
|
|
# I will delimit appended part with |
|
|
# delimiter (or ,) |
|
|
my $d = $tag->{'delimiter'}; |
|
|
# default delimiter |
|
|
$d ||= " "; |
|
|
|
|
|
my $last = pop @{$row->{$type}}; |
|
|
$d = "" if (! $last); |
|
|
$last .= $d . join($d, @v); |
|
|
push @{$row->{$type}}, $last; |
|
|
|
|
|
} else { |
|
|
push @{$row->{$type}}, @v; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
if ($row) { |
|
|
$row->{'tag'} = $field; |
|
126 |
|
|
127 |
# TODO: name_sigular, name_plural |
=head2 _x |
|
my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'}; |
|
|
$row->{'name'} = $name ? $self->_x($name) : $field; |
|
128 |
|
|
129 |
# post-sort all values in field |
Convert string from XML UTF-8 encoding to code page defined in C<xml_file>. |
|
if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) { |
|
|
$log->warn("sort at field tag not implemented"); |
|
|
} |
|
130 |
|
|
131 |
push @ds, $row; |
my $text = $n->_x('utf8 text'); |
132 |
|
|
133 |
$log->debug("row $field: ",sub { Dumper($row) }); |
Default application code page is C<ISO-8859-2>. You will probably want to |
134 |
} |
change that when creating new instance of object based on this one. |
135 |
|
|
136 |
} |
=cut |
137 |
|
|
138 |
if ($cache_file) { |
sub _x { |
139 |
store { |
my $self = shift; |
140 |
ds => \@ds, |
my $utf8 = shift || return; |
141 |
current_filename => $self->{'current_filename'}, |
return $utf8 if ($self->{_skip_x}); |
|
headline => $self->{'headline'}, |
|
|
}, $cache_file; |
|
|
$log->debug("created storable cache file $cache_file"); |
|
|
} |
|
142 |
|
|
143 |
return @ds; |
# create UTF-8 convertor for import_xml files |
144 |
|
$self->{'utf2cp'} ||= Text::Iconv->new('UTF-8' ,$self->{'code_page'} || 'ISO-8859-2'); |
145 |
|
|
146 |
|
return $self->{'utf2cp'}->convert($utf8) || |
147 |
|
$self->_get_logger()->logwarn("can't convert '$utf8'"); |
148 |
} |
} |
149 |
|
|
150 |
|
|
161 |
|
|
162 |
=cut |
=cut |
163 |
|
|
164 |
1; # End of WebPAC::Normalise::XML |
1; # End of WebPAC::Normalize::XML |