Lines 28-33
use MARC::File::USMARC;
Link Here
|
28 |
use MARC::File::XML; |
28 |
use MARC::File::XML; |
29 |
use POSIX qw(strftime); |
29 |
use POSIX qw(strftime); |
30 |
use Module::Load::Conditional qw(can_load); |
30 |
use Module::Load::Conditional qw(can_load); |
|
|
31 |
use String::Similarity; |
31 |
|
32 |
|
32 |
use C4::Koha; |
33 |
use C4::Koha; |
33 |
use C4::Log; # logaction |
34 |
use C4::Log; # logaction |
Lines 106-111
BEGIN {
Link Here
|
106 |
&CountItemsIssued |
107 |
&CountItemsIssued |
107 |
&CountBiblioInOrders |
108 |
&CountBiblioInOrders |
108 |
&GetSubscriptionsId |
109 |
&GetSubscriptionsId |
|
|
110 |
|
111 |
&GetMarcPermissionsRules |
112 |
&GetMarcPermissionsModules |
113 |
&ModMarcPermissionsRule |
114 |
&AddMarcPermissionsRule |
115 |
&DelMarcPermissionsRule |
109 |
); |
116 |
); |
110 |
|
117 |
|
111 |
# To modify something |
118 |
# To modify something |
Lines 133-138
BEGIN {
Link Here
|
133 |
# they are useful in a few circumstances, so they are exported, |
140 |
# they are useful in a few circumstances, so they are exported, |
134 |
# but don't use them unless you are a core developer ;-) |
141 |
# but don't use them unless you are a core developer ;-) |
135 |
push @EXPORT, qw( |
142 |
push @EXPORT, qw( |
|
|
143 |
&ApplyMarcPermissions |
136 |
&ModBiblioMarc |
144 |
&ModBiblioMarc |
137 |
); |
145 |
); |
138 |
|
146 |
|
Lines 270-276
sub AddBiblio {
Link Here
|
270 |
|
278 |
|
271 |
=head2 ModBiblio |
279 |
=head2 ModBiblio |
272 |
|
280 |
|
273 |
ModBiblio( $record,$biblionumber,$frameworkcode); |
281 |
ModBiblio($record, $biblionumber, $frameworkcode, $options); |
274 |
|
282 |
|
275 |
Replace an existing bib record identified by C<$biblionumber> |
283 |
Replace an existing bib record identified by C<$biblionumber> |
276 |
with one supplied by the MARC::Record object C<$record>. The embedded |
284 |
with one supplied by the MARC::Record object C<$record>. The embedded |
Lines 291-297
Returns 1 on success 0 on failure
Link Here
|
291 |
=cut |
299 |
=cut |
292 |
|
300 |
|
293 |
sub ModBiblio { |
301 |
sub ModBiblio { |
294 |
my ( $record, $biblionumber, $frameworkcode ) = @_; |
302 |
my ( $record, $biblionumber, $frameworkcode, $filter ) = @_; |
|
|
303 |
|
295 |
if (!$record) { |
304 |
if (!$record) { |
296 |
carp 'No record passed to ModBiblio'; |
305 |
carp 'No record passed to ModBiblio'; |
297 |
return 0; |
306 |
return 0; |
Lines 329-341
sub ModBiblio {
Link Here
|
329 |
_koha_marc_update_bib_ids( $record, $frameworkcode, $biblionumber, $biblioitemnumber ); |
338 |
_koha_marc_update_bib_ids( $record, $frameworkcode, $biblionumber, $biblioitemnumber ); |
330 |
|
339 |
|
331 |
# load the koha-table data object |
340 |
# load the koha-table data object |
332 |
my $oldbiblio = TransformMarcToKoha( $record, $frameworkcode ); |
341 |
my $oldbiblio = TransformMarcToKoha( $record, $frameworkcode, undef, $biblionumber, $filter ); |
333 |
|
342 |
|
334 |
# update MARC subfield that stores biblioitems.cn_sort |
343 |
# update MARC subfield that stores biblioitems.cn_sort |
335 |
_koha_marc_update_biblioitem_cn_sort( $record, $oldbiblio, $frameworkcode ); |
344 |
_koha_marc_update_biblioitem_cn_sort( $record, $oldbiblio, $frameworkcode ); |
336 |
|
345 |
|
337 |
# update the MARC record (that now contains biblio and items) with the new record data |
346 |
# update the MARC record (that now contains biblio and items) with the new record data |
338 |
&ModBiblioMarc( $record, $biblionumber, $frameworkcode ); |
347 |
ModBiblioMarc( $record, $biblionumber, $frameworkcode, $filter ); |
339 |
|
348 |
|
340 |
# modify the other koha tables |
349 |
# modify the other koha tables |
341 |
_koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode ); |
350 |
_koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode ); |
Lines 2656-2665
sub TransformHtmlToMarc {
Link Here
|
2656 |
|
2665 |
|
2657 |
=head2 TransformMarcToKoha |
2666 |
=head2 TransformMarcToKoha |
2658 |
|
2667 |
|
2659 |
$result = TransformMarcToKoha( $record, $frameworkcode ) |
2668 |
$result = TransformMarcToKoha( $record, $frameworkcode, $biblionumber, $filter ) |
2660 |
|
2669 |
|
2661 |
Extract data from a MARC bib record into a hashref representing |
2670 |
Extract data from a MARC bib record into a hashref representing |
2662 |
Koha biblio, biblioitems, and items fields. |
2671 |
Koha biblio, biblioitems, and items fields. |
2663 |
|
2672 |
|
2664 |
If passed an undefined record will log the error and return an empty |
2673 |
If passed an undefined record will log the error and return an empty |
2665 |
hash_ref |
2674 |
hash_ref |
Lines 2667-2673
hash_ref
Link Here
|
2667 |
=cut |
2676 |
=cut |
2668 |
|
2677 |
|
2669 |
sub TransformMarcToKoha { |
2678 |
sub TransformMarcToKoha { |
2670 |
my ( $record, $frameworkcode, $limit_table ) = @_; |
2679 |
my ( $record, $frameworkcode, $limit_table, $biblionumber, $filter ) = @_; |
2671 |
|
2680 |
|
2672 |
my $result = {}; |
2681 |
my $result = {}; |
2673 |
if (!defined $record) { |
2682 |
if (!defined $record) { |
Lines 2688-2693
sub TransformMarcToKoha {
Link Here
|
2688 |
$tables{'biblioitems'} = 1; |
2697 |
$tables{'biblioitems'} = 1; |
2689 |
} |
2698 |
} |
2690 |
|
2699 |
|
|
|
2700 |
# apply permissions |
2701 |
if ( C4::Context->preference('MARCPermissions') && $biblionumber && $filter) { |
2702 |
$record = ApplyMarcPermissions({ |
2703 |
biblionumber => $biblionumber, |
2704 |
record => $record, |
2705 |
frameworkcode => $frameworkcode, |
2706 |
filter => $filter, |
2707 |
nolog => 1 |
2708 |
}); |
2709 |
} |
2710 |
|
2691 |
# traverse through record |
2711 |
# traverse through record |
2692 |
MARCFIELD: foreach my $field ( $record->fields() ) { |
2712 |
MARCFIELD: foreach my $field ( $record->fields() ) { |
2693 |
my $tag = $field->tag(); |
2713 |
my $tag = $field->tag(); |
Lines 3442-3448
sub _koha_delete_biblioitems {
Link Here
|
3442 |
|
3462 |
|
3443 |
=head2 ModBiblioMarc |
3463 |
=head2 ModBiblioMarc |
3444 |
|
3464 |
|
3445 |
&ModBiblioMarc($newrec,$biblionumber,$frameworkcode); |
3465 |
&ModBiblioMarc($newrec,$biblionumber,$frameworkcode,$filter); |
3446 |
|
3466 |
|
3447 |
Add MARC XML data for a biblio to koha |
3467 |
Add MARC XML data for a biblio to koha |
3448 |
|
3468 |
|
Lines 3453-3459
Function exported, but should NOT be used, unless you really know what you're do
Link Here
|
3453 |
sub ModBiblioMarc { |
3473 |
sub ModBiblioMarc { |
3454 |
# pass the MARC::Record to this function, and it will create the records in |
3474 |
# pass the MARC::Record to this function, and it will create the records in |
3455 |
# the marcxml field |
3475 |
# the marcxml field |
3456 |
my ( $record, $biblionumber, $frameworkcode ) = @_; |
3476 |
my ( $record, $biblionumber, $frameworkcode, $options ) = @_; |
|
|
3477 |
|
3457 |
if ( !$record ) { |
3478 |
if ( !$record ) { |
3458 |
carp 'ModBiblioMarc passed an undefined record'; |
3479 |
carp 'ModBiblioMarc passed an undefined record'; |
3459 |
return; |
3480 |
return; |
Lines 3466-3471
sub ModBiblioMarc {
Link Here
|
3466 |
if ( !$frameworkcode ) { |
3487 |
if ( !$frameworkcode ) { |
3467 |
$frameworkcode = ""; |
3488 |
$frameworkcode = ""; |
3468 |
} |
3489 |
} |
|
|
3490 |
|
3491 |
# apply permissions |
3492 |
if ( |
3493 |
C4::Context->preference('MARCPermissions') && |
3494 |
defined $options && |
3495 |
exists $options->{'filter'} |
3496 |
) { |
3497 |
$record = ApplyMarcPermissions( |
3498 |
{ |
3499 |
biblionumber => $biblionumber, |
3500 |
record => $record, |
3501 |
frameworkcode => $frameworkcode, |
3502 |
filter => $options->{'filter'} |
3503 |
} |
3504 |
); |
3505 |
} |
3506 |
|
3469 |
my $sth = $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"); |
3507 |
my $sth = $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"); |
3470 |
$sth->execute( $frameworkcode, $biblionumber ); |
3508 |
$sth->execute( $frameworkcode, $biblionumber ); |
3471 |
$sth->finish; |
3509 |
$sth->finish; |
Lines 3786-3791
sub RemoveAllNsb {
Link Here
|
3786 |
return $record; |
3824 |
return $record; |
3787 |
} |
3825 |
} |
3788 |
|
3826 |
|
|
|
3827 |
=head2 ApplyMarcPermissions |
3828 |
|
3829 |
my $record = ApplyMarcPermissions($arguments) |
3830 |
|
3831 |
Applies marc permission rules to a record. |
3832 |
|
3833 |
C<$arguments> is expected to be a hashref with below keys defined. |
3834 |
|
3835 |
=over 4 |
3836 |
|
3837 |
=item C<biblionumber> |
3838 |
biblionumber of old record |
3839 |
|
3840 |
=item C<record> |
3841 |
record that will modify old record |
3842 |
|
3843 |
=item C<frameworkcode> |
3844 |
only tags included in framework will be processed |
3845 |
|
3846 |
=item C<filter> |
3847 |
hashref containing at least one filter module from the marc_permissions_modules |
3848 |
table in form {module => filter}. Three predefined filter modules exists: |
3849 |
|
3850 |
* source |
3851 |
* category |
3852 |
* borrower |
3853 |
|
3854 |
=item C<log> |
3855 |
optional reference to array that will be filled with rule evaluation log |
3856 |
entries. |
3857 |
|
3858 |
=item C<nolog> |
3859 |
optional boolean which when true disables logging to action log. |
3860 |
|
3861 |
=back |
3862 |
|
3863 |
Returns: |
3864 |
|
3865 |
=over 4 |
3866 |
|
3867 |
=item C<$record> |
3868 |
|
3869 |
new MARC record based on C<record> with C<filter> applied. If no old |
3870 |
record for C<biblionumber> can be found, C<record> is returned unchanged. |
3871 |
Default action when no matching filter found is to leave old record unchanged. |
3872 |
|
3873 |
=back |
3874 |
|
3875 |
=cut |
3876 |
|
3877 |
sub ApplyMarcPermissions { |
3878 |
my $arguments = shift; |
3879 |
my $biblionumber = $arguments->{biblionumber}; |
3880 |
my $new_record = $arguments->{record}; |
3881 |
my $frameworkcode = $arguments->{frameworkcode} || ''; |
3882 |
|
3883 |
if ( !$biblionumber ) { |
3884 |
carp 'ApplyMarcPermissions called on undefined biblionumber'; |
3885 |
return; |
3886 |
} |
3887 |
if ( !$new_record ) { |
3888 |
carp 'ApplyMarcPermissions called on undefined record'; |
3889 |
return; |
3890 |
} |
3891 |
my $filter = $arguments->{filter} |
3892 |
or return $new_record; |
3893 |
|
3894 |
my $log = $arguments->{log} || []; |
3895 |
my $nolog = $arguments->{nolog}; |
3896 |
|
3897 |
my $dbh = C4::Context->dbh; |
3898 |
|
3899 |
my $is_regex = sub { |
3900 |
my ( $tag, $m ) = @_; |
3901 |
# tag is not exactly same as possible regex |
3902 |
$tag ne $m && |
3903 |
|
3904 |
# wildcard |
3905 |
$m ne '*' && |
3906 |
|
3907 |
# valid tagDataType |
3908 |
$m !~ /^(0[1-9A-z][\dA-Z]) | |
3909 |
([1-9A-z][\dA-z]{2})$/x && |
3910 |
|
3911 |
# nor valid controltagDataType |
3912 |
$m !~ /00[1-9A-Za-z]{1}/ && |
3913 |
|
3914 |
# so we try it as a regex |
3915 |
$tag =~ /^$m$/ |
3916 |
}; |
3917 |
|
3918 |
my $old_record = GetMarcBiblio($biblionumber); |
3919 |
if ( $old_record ) { |
3920 |
my $ret_record = MARC::Record->new(); |
3921 |
my $perm = GetMarcPermissions() or return $new_record; |
3922 |
my $marc_struct = GetMarcStructure(1, $frameworkcode); |
3923 |
my $similarity_pref = C4::Context->preference("MARCPermissionsCorrectionSimilarity"); |
3924 |
$similarity_pref = defined $similarity_pref && |
3925 |
$similarity_pref >= 0 && |
3926 |
$similarity_pref <= 100 ? |
3927 |
$similarity_pref/100.0 : undef; |
3928 |
|
3929 |
# First came the leader ... |
3930 |
$ret_record->leader($old_record->leader()); |
3931 |
|
3932 |
# ... and then came all the tags in the framework |
3933 |
for my $tag (sort keys %{$marc_struct}) { |
3934 |
my @old_fields = $old_record->field($tag); |
3935 |
my @new_fields = $new_record->field($tag); |
3936 |
next unless @old_fields or @new_fields; |
3937 |
my @perms = ( |
3938 |
$perm->{'*'}, |
3939 |
( # regex tags |
3940 |
map { $perm->{$_} } |
3941 |
grep { $is_regex->( $tag, $_ ) } sort keys %{$perm} |
3942 |
), |
3943 |
$perm->{$tag} |
3944 |
); |
3945 |
|
3946 |
# There can be more than one field for each tag, so we have to |
3947 |
# iterate all fields and distinguish which are new, removed or |
3948 |
# modified |
3949 |
my $max_fields = ($#old_fields, $#new_fields) |
3950 |
[$#old_fields< $#new_fields]; |
3951 |
for ( my $i_field = 0; $i_field <= $max_fields; $i_field++ ) { |
3952 |
my $old_field = $old_fields[$i_field]; |
3953 |
my $new_field = $new_fields[$i_field]; |
3954 |
|
3955 |
# Existing field |
3956 |
if ( $old_field and $new_field ) { |
3957 |
# Control fields |
3958 |
if ( $old_field->is_control_field() ) { |
3959 |
# Existing control field |
3960 |
if ( $old_field and $new_field ) { |
3961 |
my $on_existing = GetMarcPermissionsAction('on_existing', $filter, @perms); |
3962 |
if ( defined $on_existing->{action} ) { |
3963 |
push @{$log}, |
3964 |
{ |
3965 |
rule => $on_existing->{rule}, |
3966 |
event => "existing", |
3967 |
action => $on_existing->{action}, |
3968 |
tag => $tag, |
3969 |
subfieldcode => undef, |
3970 |
filter => $filter |
3971 |
}; |
3972 |
if ( $on_existing->{action} eq 'skip' ) { |
3973 |
$ret_record->insert_fields_ordered($old_field); |
3974 |
next; |
3975 |
} elsif ( $on_existing->{action} eq 'overwrite' ) { |
3976 |
$ret_record->insert_fields_ordered($new_field); |
3977 |
next; |
3978 |
} elsif ( $on_existing->{action} eq 'add' ) { |
3979 |
$ret_record->insert_fields_ordered($old_field); |
3980 |
$ret_record->insert_fields_ordered($new_field); |
3981 |
next; |
3982 |
} elsif ( $on_existing->{action} eq 'add_or_correct' ) { |
3983 |
if ( $similarity_pref ) { |
3984 |
my $similarity = similarity $old_field->data(), $new_field->data(); |
3985 |
if ( $similarity >= $similarity_pref ) { |
3986 |
$ret_record->insert_fields_ordered($new_field); |
3987 |
} else { |
3988 |
$ret_record->insert_fields_ordered($old_field); |
3989 |
$ret_record->insert_fields_ordered($new_field); |
3990 |
} |
3991 |
} else { |
3992 |
$ret_record->insert_fields_ordered($old_field); |
3993 |
} |
3994 |
} |
3995 |
} else { # default to skip |
3996 |
$ret_record->insert_fields_ordered($old_field); |
3997 |
next; |
3998 |
} |
3999 |
# New control field |
4000 |
} elsif ( not $old_field and $new_field ) { |
4001 |
my $on_new = GetMarcPermissionsAction('on_new', $filter, @perms); |
4002 |
if ( defined $on_new->{action} ) { |
4003 |
push @{$log}, |
4004 |
{ |
4005 |
rule => $on_new->{rule}, |
4006 |
event => "new", |
4007 |
action => $on_new->{action}, |
4008 |
tag => $tag, |
4009 |
subfieldcode => undef, |
4010 |
filter => $filter |
4011 |
}; |
4012 |
if ( $on_new->{action} eq 'skip' ) { # Redundant |
4013 |
next; |
4014 |
} elsif ( $on_new->{action} eq 'add' ) { |
4015 |
$ret_record->insert_fields_ordered($new_field); |
4016 |
next; |
4017 |
} |
4018 |
} |
4019 |
# Removed control field |
4020 |
} elsif ( $old_field and not $new_field ) { |
4021 |
my $on_removed = GetMarcPermissionsAction('on_removed', $filter, @perms); |
4022 |
if ( defined $on_removed->{action} ) { |
4023 |
push @{$log}, |
4024 |
{ |
4025 |
rule => $on_removed->{rule}, |
4026 |
event => "removed", |
4027 |
action => $on_removed->{action}, |
4028 |
tag => $tag, |
4029 |
subfieldcode => undef |
4030 |
}; |
4031 |
if ( $on_removed->{action} eq 'skip' ) { |
4032 |
$ret_record->insert_fields_ordered($old_field); |
4033 |
next; |
4034 |
} elsif ( $on_removed->{action} eq 'remove' ) { |
4035 |
next; |
4036 |
} |
4037 |
} else { # default to skip |
4038 |
$ret_record->insert_fields_ordered($old_field); |
4039 |
next; |
4040 |
} |
4041 |
} |
4042 |
# Indicators and subfields |
4043 |
} else { |
4044 |
# Indicators |
4045 |
my @old_ind = map { $old_field->indicator($_) } (1,2); |
4046 |
my @new_ind = map { $new_field->indicator($_) } (1,2); |
4047 |
my @ind = ('',''); |
4048 |
|
4049 |
for my $i ((0,1)) { |
4050 |
my @ind_perms = ( |
4051 |
@perms, |
4052 |
$perm->{$tag}->{subfields}->{ 'i' . ( $i + 1 ) } |
4053 |
); |
4054 |
|
4055 |
# Existing indicator |
4056 |
if ( defined $old_ind[$i] and defined $new_ind[$i] ) { |
4057 |
my $on_existing = GetMarcPermissionsAction('on_existing', $filter, @ind_perms); |
4058 |
if ( defined $on_existing->{action} ) { |
4059 |
push @{$log}, |
4060 |
{ |
4061 |
rule => $on_existing->{rule}, |
4062 |
event => "existing", |
4063 |
action => $on_existing->{action}, |
4064 |
tag => $tag, |
4065 |
subfieldcode => 'i' . ( $i + 1 ), |
4066 |
filter => $filter |
4067 |
}; |
4068 |
if ( $on_existing->{action} eq 'skip' ) { |
4069 |
$ind[$i] = $old_ind[$i]; |
4070 |
} elsif ( $on_existing->{action} eq 'overwrite' ) { |
4071 |
$ind[$i] = $new_ind[$i]; |
4072 |
} |
4073 |
} else { # default to skip |
4074 |
$ind[$i] = $old_ind[$i]; |
4075 |
} |
4076 |
# New indicator |
4077 |
} elsif ( not defined $old_ind[$i] and defined $new_ind[$i] ) { |
4078 |
my $on_new = GetMarcPermissionsAction('on_new', $filter, @ind_perms); |
4079 |
if ( defined $on_new->{action} ) { |
4080 |
push @{$log}, |
4081 |
{ |
4082 |
rule => $on_new->{rule}, |
4083 |
event => "new", |
4084 |
action => $on_new->{action}, |
4085 |
tag => $tag, |
4086 |
subfieldcode => 'i' . ( $i + 1 ), |
4087 |
filter => $filter |
4088 |
}; |
4089 |
if ( $on_new->{action} eq 'skip' ) { |
4090 |
$ind[$i] = $old_ind[$i]; |
4091 |
} elsif ( $on_new->{action} eq 'add' ) { |
4092 |
$ind[$i] = $new_ind[$i]; |
4093 |
} |
4094 |
} |
4095 |
# Removed indicator |
4096 |
} elsif ( defined $old_ind[$i] and not defined $new_ind[$i] ) { |
4097 |
my $on_removed = GetMarcPermissionsAction('on_removed', $filter, @ind_perms); |
4098 |
if ( defined $on_removed->{action} ) { |
4099 |
push @{$log}, |
4100 |
{ |
4101 |
rule => $on_removed->{rule}, |
4102 |
event => "removed", |
4103 |
action => $on_removed->{action}, |
4104 |
tag => $tag, |
4105 |
subfieldcode => 'i' . ( $i + 1 ), |
4106 |
filter => $filter |
4107 |
}; |
4108 |
if ( $on_removed->{action} eq 'skip' ) { |
4109 |
$ind[$i] = $old_ind[$i]; |
4110 |
} elsif ( $on_removed->{action} eq 'remove' ) { |
4111 |
$ind[$i] = ''; |
4112 |
} |
4113 |
} else { # default to skip |
4114 |
$ind[$i] = $old_ind[$i]; |
4115 |
} |
4116 |
} |
4117 |
} |
4118 |
|
4119 |
# Subfields |
4120 |
my @subfields = (); |
4121 |
my %proccessed_subcodes = (); |
4122 |
|
4123 |
# Try to preserve subfield order by first processing |
4124 |
# subfields from the old record, and then subfields from |
4125 |
# the framework structure unless include in old record. |
4126 |
my @sorted_subfields = ( |
4127 |
map { shift @{$_} } $old_field->subfields(), |
4128 |
() = sort keys %{ $marc_struct->{$tag} } |
4129 |
); |
4130 |
for my $subcode ( @sorted_subfields ) { |
4131 |
next if $proccessed_subcodes{$subcode}; |
4132 |
$proccessed_subcodes{$subcode} = 1; |
4133 |
next unless ref $marc_struct->{$tag}{$subcode} eq 'HASH'; |
4134 |
my @old_subfields = $old_field->subfield($subcode); |
4135 |
my @new_subfields = $new_field->subfield($subcode); |
4136 |
next unless @old_subfields or @new_subfields; |
4137 |
|
4138 |
my @subfield_perms = ( |
4139 |
@perms, |
4140 |
$perm->{'*'}->{subfields}->{'*'}, |
4141 |
$perm->{$tag}->{subfields}->{'*'}, |
4142 |
( # tag->regex |
4143 |
map { $perm->{$tag}->{subfields}->{$_} } |
4144 |
grep { |
4145 |
# subcode is not exactly same as |
4146 |
# possible regex |
4147 |
$subcode ne $_ && |
4148 |
|
4149 |
# wildcard, not checking for valid |
4150 |
# subfieldcodeDataType since it |
4151 |
# contains too many regex characters |
4152 |
$_ !~ /^(\*)$/ && |
4153 |
|
4154 |
# so we try it as a regex |
4155 |
$subcode =~ /$_/ |
4156 |
} sort |
4157 |
keys %{ $perm->{$tag}->{subfields} } |
4158 |
), |
4159 |
( # regex->* |
4160 |
map { $perm->{$_}->{subfields}->{'*'} } |
4161 |
grep { $is_regex->( $tag, $_ ) } |
4162 |
sort keys %{$perm} |
4163 |
), |
4164 |
$perm->{'*'}->{subfields}->{$subcode}, |
4165 |
( # regex->subcode |
4166 |
map { $perm->{$_}->{subfields}->{$subcode} } |
4167 |
grep { $is_regex->( $tag, $_ ) } |
4168 |
sort keys %{$perm} |
4169 |
), |
4170 |
$perm->{$tag}->{subfields}->{$subcode} |
4171 |
); |
4172 |
|
4173 |
# Existing subfield |
4174 |
if ( @old_subfields and @new_subfields ) { |
4175 |
my $on_existing = GetMarcPermissionsAction('on_existing', $filter, @subfield_perms); |
4176 |
if ( defined $on_existing->{action} ) { |
4177 |
push @{$log}, |
4178 |
{ |
4179 |
rule => $on_existing->{rule}, |
4180 |
event => "existing", |
4181 |
action => $on_existing->{action}, |
4182 |
tag => $tag, |
4183 |
subfieldcode => $subcode, |
4184 |
filter => $filter |
4185 |
}; |
4186 |
if ( $on_existing->{action} eq 'skip' ) { |
4187 |
push(@subfields, map {($subcode,$_)} @old_subfields); |
4188 |
next; |
4189 |
} elsif ( $on_existing->{action} eq 'overwrite' ) { |
4190 |
push(@subfields, map {($subcode,$_)} @new_subfields); |
4191 |
next; |
4192 |
} elsif ( $on_existing->{action} eq 'add' ) { |
4193 |
push(@subfields, map {($subcode,$_)} @old_subfields); |
4194 |
push(@subfields, map {($subcode,$_)} @new_subfields); |
4195 |
next; |
4196 |
} elsif ( $on_existing->{action} eq 'add_or_correct' ) { |
4197 |
if ( $similarity_pref ) { |
4198 |
my @corrected_vals = (); |
4199 |
|
4200 |
# correct all old subfields |
4201 |
for ( my $i_new = 0; $i_new <= $#new_subfields; $i_new++ ) { |
4202 |
my $new_val = $new_subfields[$i_new]; |
4203 |
for ( my $i_old = 0; $i_old <= $#old_subfields; $i_old++ ) { |
4204 |
my $old_val = $old_subfields[$i_old]; |
4205 |
next if not defined $old_val or not defined $new_val; |
4206 |
my $similarity = similarity ($old_val, $new_val); |
4207 |
if ( $similarity >= $similarity_pref ) { |
4208 |
$corrected_vals[$i_old] = $new_val; |
4209 |
$new_subfields[$i_new] = undef; |
4210 |
$old_subfields[$i_old] = undef; |
4211 |
last; |
4212 |
} |
4213 |
} |
4214 |
} |
4215 |
|
4216 |
# insert all unchanged old subfields |
4217 |
map { |
4218 |
$corrected_vals[$_] = $old_subfields[$_] |
4219 |
if defined $old_subfields[$_] |
4220 |
} 0 .. $#old_subfields; |
4221 |
|
4222 |
|
4223 |
# append all remaning new subfields |
4224 |
map {push @corrected_vals, $_ if defined} @new_subfields; |
4225 |
|
4226 |
push(@subfields, map {($subcode,$_)} @corrected_vals); |
4227 |
} else { |
4228 |
push(@subfields, map {($subcode,$_)} @old_subfields); |
4229 |
} |
4230 |
} |
4231 |
} else { # default to skip |
4232 |
push(@subfields, map {($subcode,$_)} @old_subfields); |
4233 |
next; |
4234 |
} |
4235 |
# New subfield |
4236 |
} elsif ( not @old_subfields and @new_subfields ) { |
4237 |
my $on_new = GetMarcPermissionsAction('on_new', $filter, @subfield_perms); |
4238 |
if ( defined $on_new->{action} ) { |
4239 |
push @{$log}, |
4240 |
{ |
4241 |
rule => $on_new->{rule}, |
4242 |
event => "new", |
4243 |
action => $on_new->{action}, |
4244 |
tag => $tag, |
4245 |
subfieldcode => $subcode, |
4246 |
filter => $filter |
4247 |
}; |
4248 |
if ( $on_new->{action} eq 'skip' ) { |
4249 |
next; |
4250 |
} elsif ( $on_new->{action} eq 'add' ) { |
4251 |
push(@subfields, map {($subcode,$_)} @new_subfields); |
4252 |
next; |
4253 |
} |
4254 |
} |
4255 |
# Removed subfield |
4256 |
} elsif ( @old_subfields and not @new_subfields ) { |
4257 |
my $on_removed = GetMarcPermissionsAction('on_removed', $filter, @subfield_perms); |
4258 |
if ( defined $on_removed->{action} ) { |
4259 |
push @{$log}, |
4260 |
{ |
4261 |
rule => $on_removed->{rule}, |
4262 |
event => "removed", |
4263 |
action => $on_removed->{action}, |
4264 |
tag => $tag, |
4265 |
subfieldcode => $subcode, |
4266 |
filter => $filter |
4267 |
}; |
4268 |
if ( $on_removed->{action} eq 'skip' ) { |
4269 |
push(@subfields, ($subcode => @old_subfields)); |
4270 |
next; |
4271 |
} elsif ( $on_removed->{action} eq 'remove' ) { |
4272 |
next; |
4273 |
} |
4274 |
} else { # default to skip |
4275 |
push(@subfields, ($subcode => @old_subfields)); |
4276 |
next; |
4277 |
} |
4278 |
} |
4279 |
} # / for each subfield |
4280 |
$ret_record->insert_grouped_field(MARC::Field->new($tag, $ind[0], $ind[1], @subfields)); |
4281 |
} |
4282 |
# New field |
4283 |
} elsif ( not $old_field and $new_field ) { |
4284 |
my $on_new = GetMarcPermissionsAction('on_new', $filter, @perms); |
4285 |
|
4286 |
if ( defined $on_new->{action} ) { |
4287 |
push @{$log}, |
4288 |
{ |
4289 |
rule => $on_new->{rule}, |
4290 |
event => "new", |
4291 |
action => $on_new->{action}, |
4292 |
tag => $tag, |
4293 |
subfieldcode => undef, |
4294 |
filter => $filter |
4295 |
}; |
4296 |
if ( $on_new->{action} eq 'skip' ) { |
4297 |
next; |
4298 |
} elsif ( $on_new->{action} eq 'add' ) { |
4299 |
$ret_record->insert_fields_ordered($new_field); |
4300 |
next; |
4301 |
} |
4302 |
} |
4303 |
# Removed field |
4304 |
} elsif ( $old_field and not $new_field ) { |
4305 |
my $on_removed = GetMarcPermissionsAction('on_removed', $filter, @perms); |
4306 |
if ( defined $on_removed->{action} ) { |
4307 |
push @{$log}, |
4308 |
{ |
4309 |
rule => $on_removed->{rule}, |
4310 |
event => "removed", |
4311 |
action => $on_removed->{action}, |
4312 |
tag => $tag, |
4313 |
subfieldcode => undef, |
4314 |
filter => $filter |
4315 |
}; |
4316 |
if ( $on_removed->{action} eq 'skip' ) { |
4317 |
$ret_record->insert_grouped_field($old_field); |
4318 |
next; |
4319 |
} elsif ( $on_removed->{action} eq 'remove' ) { |
4320 |
next; |
4321 |
} |
4322 |
} else { # default to skip |
4323 |
$ret_record->insert_grouped_field($old_field); |
4324 |
next; |
4325 |
} |
4326 |
} |
4327 |
} # / for each field |
4328 |
} # / for each tag |
4329 |
if ( !$nolog && C4::Context->preference("MARCPermissionsLog") ) { |
4330 |
my $log_str = ''; |
4331 |
my $n = $#{$log}; |
4332 |
for my $l ( @{$log} ) { |
4333 |
$log_str .= "{\n"; |
4334 |
my $kn = keys %{$l}; |
4335 |
for my $k ( sort keys %{$l} ) { |
4336 |
if ( $k eq 'filter' ) { |
4337 |
$log_str .= " filter => {\n"; |
4338 |
my $fkn = keys %{ $l->{$k} }; |
4339 |
for my $fk ( sort keys %{ $l->{$k} } ) { |
4340 |
$log_str .= ' ' |
4341 |
. $fk . ' => ' |
4342 |
. $l->{$k}->{$fk} |
4343 |
. ( --$fkn ? ',' : '' ) . "\n"; |
4344 |
} |
4345 |
$log_str .= " }" . ( --$kn ? ',' : '' ) . "\n"; |
4346 |
} |
4347 |
else { |
4348 |
$log_str .= ' ' |
4349 |
. $k . ' => ' |
4350 |
. $l->{$k} |
4351 |
. ( --$kn ? ',' : '' ) . "\n" |
4352 |
if defined $l->{$k}; |
4353 |
} |
4354 |
} |
4355 |
$log_str .= '}' . ( $n-- ? ",\n" : '' ); |
4356 |
} |
4357 |
logaction( "CATALOGUING", "MODIFY", $biblionumber, $log_str ); |
4358 |
} |
4359 |
return $ret_record; |
4360 |
} |
4361 |
return $new_record; |
4362 |
} |
4363 |
|
4364 |
|
4365 |
=head2 GetMarcPermissions |
4366 |
|
4367 |
my $marc_permissions = GetMarcPermissions() |
4368 |
|
4369 |
Loads MARC field permissions from the marc_permissions table. |
4370 |
|
4371 |
Returns: |
4372 |
|
4373 |
=over 4 |
4374 |
|
4375 |
=item C<$marc_permissions> |
4376 |
|
4377 |
hashref with permissions structure for use with GetMarcPermissionsAction. |
4378 |
|
4379 |
=back |
4380 |
|
4381 |
=cut |
4382 |
|
4383 |
sub GetMarcPermissions { |
4384 |
my $dbh = C4::Context->dbh; |
4385 |
my $rule_count = 0; |
4386 |
my %perms = (); |
4387 |
|
4388 |
my $query = ' |
4389 |
SELECT `marc_permissions`.*, |
4390 |
`marc_permissions_modules`.`name`, |
4391 |
`marc_permissions_modules`.`description`, |
4392 |
`marc_permissions_modules`.`specificity` |
4393 |
FROM `marc_permissions` |
4394 |
LEFT JOIN `marc_permissions_modules` ON `module` = `marc_permissions_modules`.`id` |
4395 |
ORDER BY `marc_permissions_modules`.`specificity`, `id` |
4396 |
'; |
4397 |
my $sth = $dbh->prepare($query); |
4398 |
$sth->execute(); |
4399 |
while ( my $row = $sth->fetchrow_hashref ) { |
4400 |
$rule_count++; |
4401 |
if ( defined $row->{tagsubfield} and $row->{tagsubfield} ) { |
4402 |
$perms{ $row->{tagfield} }->{subfields}->{ $row->{tagsubfield} } |
4403 |
->{on_existing}->{ $row->{name} }->{ $row->{filter} } = |
4404 |
{ action => $row->{on_existing}, rule => $row->{'id'} }; |
4405 |
$perms{ $row->{tagfield} }->{subfields}->{ $row->{tagsubfield} } |
4406 |
->{on_new}->{ $row->{name} }->{ $row->{filter} } = |
4407 |
{ action => $row->{on_new}, rule => $row->{'id'} }; |
4408 |
$perms{ $row->{tagfield} }->{subfields}->{ $row->{tagsubfield} } |
4409 |
->{on_removed}->{ $row->{name} }->{ $row->{filter} } = |
4410 |
{ action => $row->{on_removed}, rule => $row->{'id'} }; |
4411 |
} |
4412 |
else { |
4413 |
$perms{ $row->{tagfield} }->{on_existing}->{ $row->{name} } |
4414 |
->{ $row->{filter} } = |
4415 |
{ action => $row->{on_existing}, rule => $row->{'id'} }; |
4416 |
$perms{ $row->{tagfield} }->{on_new}->{ $row->{name} } |
4417 |
->{ $row->{filter} } = |
4418 |
{ action => $row->{on_new}, rule => $row->{'id'} }; |
4419 |
$perms{ $row->{tagfield} }->{on_removed}->{ $row->{name} } |
4420 |
->{ $row->{filter} } = |
4421 |
{ action => $row->{on_removed}, rule => $row->{'id'} }; |
4422 |
} |
4423 |
} |
4424 |
|
4425 |
return unless $rule_count; |
4426 |
return \%perms; |
4427 |
} |
4428 |
|
4429 |
=head2 GetMarcPermissionsAction |
4430 |
|
4431 |
my $action = GetMarcPermissionsAction($event, $filter, @permissions) |
4432 |
|
4433 |
Gets action based on C<$event>, C<$filter> and C<@permissions>. |
4434 |
|
4435 |
=over 4 |
4436 |
|
4437 |
=item C<$event> |
4438 |
|
4439 |
which event: 'on_existing', 'on_new' or 'on_removed' |
4440 |
|
4441 |
=item C<$filter> |
4442 |
|
4443 |
hashref containing at least one filter module from the marc_permissions_modules |
4444 |
table in form {module => filter}. Three predefined filter modules exists: |
4445 |
|
4446 |
* source |
4447 |
* category |
4448 |
* borrower |
4449 |
|
4450 |
=item C<@permissions> |
4451 |
|
4452 |
list of permission structures in order of specificity from least to most |
4453 |
specific. |
4454 |
|
4455 |
=back |
4456 |
|
4457 |
Returns: |
4458 |
|
4459 |
=over 4 |
4460 |
|
4461 |
=item C<$action> |
4462 |
|
4463 |
hashref defining matching action. |
4464 |
|
4465 |
=back |
4466 |
|
4467 |
=cut |
4468 |
|
4469 |
sub GetMarcPermissionsAction { |
4470 |
my $what = shift or return; |
4471 |
my $filter = shift or return; |
4472 |
my @perms = @_; |
4473 |
my $modules = GetMarcPermissionsModules(); |
4474 |
my $action = undef; |
4475 |
|
4476 |
for my $perm (@perms) { |
4477 |
next if not defined $perm; |
4478 |
next if not defined $perm->{$what}; |
4479 |
my $tmp_action = undef; |
4480 |
for my $module ( @{$modules} ) { |
4481 |
my $action_candidate = defined $perm->{$what}->{ $module->{'name'} } ? $perm->{$what}->{ $module->{'name'} } : undef; |
4482 |
if ($action_candidate) { |
4483 |
if ( |
4484 |
defined $filter->{ $module->{'name'} } and |
4485 |
defined $action_candidate->{ $filter->{ $module->{'name'} } }->{action} |
4486 |
) { |
4487 |
$tmp_action = $perm->{$what}->{ $module->{'name'} }->{ $filter->{ $module->{'name'} } }; |
4488 |
last; |
4489 |
} |
4490 |
elsif (defined $action_candidate->{'*'}->{action} and !$tmp_action) { |
4491 |
$tmp_action = $perm->{$what}->{ $module->{'name'} }->{'*'}; |
4492 |
} |
4493 |
} |
4494 |
} |
4495 |
$action = $tmp_action if defined $tmp_action; |
4496 |
} |
4497 |
return $action; |
4498 |
} |
4499 |
|
4500 |
=head2 GetMarcPermissionsRules |
4501 |
|
4502 |
my $rules = GetMarcPermissionsRules() |
4503 |
|
4504 |
Returns: |
4505 |
|
4506 |
=over 4 |
4507 |
|
4508 |
=item C<$rules> |
4509 |
|
4510 |
array (in list context, arrayref otherwise) of hashrefs from marc_permissions |
4511 |
table in order of module specificity and rule id. |
4512 |
|
4513 |
=back |
4514 |
|
4515 |
=cut |
4516 |
|
4517 |
sub GetMarcPermissionsRules { |
4518 |
my $dbh = C4::Context->dbh; |
4519 |
my @rules = (); |
4520 |
|
4521 |
my $query = ' |
4522 |
SELECT `marc_permissions`.`id`, |
4523 |
`marc_permissions`.`tagfield`, |
4524 |
`marc_permissions`.`tagsubfield`, |
4525 |
`marc_permissions`.`filter`, |
4526 |
`marc_permissions`.`on_existing`, |
4527 |
`marc_permissions`.`on_new`, |
4528 |
`marc_permissions`.`on_removed`, |
4529 |
`marc_permissions_modules`.`name` as `module`, |
4530 |
`marc_permissions_modules`.`description`, |
4531 |
`marc_permissions_modules`.`specificity` |
4532 |
FROM `marc_permissions` |
4533 |
LEFT JOIN `marc_permissions_modules` ON `module` = `marc_permissions_modules`.`id` |
4534 |
ORDER BY `marc_permissions_modules`.`specificity`, `id` |
4535 |
'; |
4536 |
my $sth = $dbh->prepare($query); |
4537 |
$sth->execute(); |
4538 |
while ( my $row = $sth->fetchrow_hashref ) { |
4539 |
push(@rules, $row); |
4540 |
} |
4541 |
|
4542 |
return wantarray ? @rules : \@rules; |
4543 |
} |
4544 |
|
4545 |
=head2 GetMarcPermissionsModules |
4546 |
|
4547 |
my $modules = GetMarcPermissionsModules() |
4548 |
|
4549 |
Returns: |
4550 |
|
4551 |
=over 4 |
4552 |
|
4553 |
=item C<$modules> |
4554 |
|
4555 |
array (in list context, arrayref otherwise) of hashrefs from |
4556 |
marc_permissions_modules table in order of specificity. |
4557 |
|
4558 |
=back |
4559 |
|
4560 |
=cut |
4561 |
|
4562 |
sub GetMarcPermissionsModules { |
4563 |
my $dbh = C4::Context->dbh; |
4564 |
my @modules = (); |
4565 |
|
4566 |
my $query = ' |
4567 |
SELECT * |
4568 |
FROM `marc_permissions_modules` |
4569 |
ORDER BY `specificity` DESC |
4570 |
'; |
4571 |
my $sth = $dbh->prepare($query); |
4572 |
$sth->execute(); |
4573 |
while ( my $row = $sth->fetchrow_hashref ) { |
4574 |
push(@modules, $row); |
4575 |
} |
4576 |
|
4577 |
return wantarray ? @modules : \@modules; |
4578 |
} |
4579 |
|
4580 |
=head2 ModMarcPermissionsRule |
4581 |
|
4582 |
my $success = ModMarcPermissionsRule($id, $fields) |
4583 |
|
4584 |
Modifies rule in the marc_permissions table. |
4585 |
|
4586 |
=over 4 |
4587 |
|
4588 |
=item C<$id> |
4589 |
|
4590 |
rule id to modify |
4591 |
|
4592 |
=item C<$fields> |
4593 |
|
4594 |
hashref defining the table fields |
4595 |
|
4596 |
* tagfield - required |
4597 |
* tagsubfield |
4598 |
* module - required |
4599 |
* filter - required |
4600 |
* on_existing - required |
4601 |
* on_new - required |
4602 |
* on_removed - required |
4603 |
|
4604 |
=back |
4605 |
|
4606 |
Returns: |
4607 |
|
4608 |
=over 4 |
4609 |
|
4610 |
=item C<$success> |
4611 |
|
4612 |
undef if an error occurs, otherwise true. |
4613 |
|
4614 |
=back |
4615 |
|
4616 |
=cut |
4617 |
|
4618 |
sub ModMarcPermissionsRule { |
4619 |
my ($id, $f) = @_; |
4620 |
my $dbh = C4::Context->dbh; |
4621 |
my $query = ' |
4622 |
UPDATE `marc_permissions` |
4623 |
SET |
4624 |
tagfield = ?, |
4625 |
tagsubfield = ?, |
4626 |
module = ?, |
4627 |
filter = ?, |
4628 |
on_existing = ?, |
4629 |
on_new = ?, |
4630 |
on_removed = ? |
4631 |
WHERE |
4632 |
id = ? |
4633 |
'; |
4634 |
my $sth = $dbh->prepare($query); |
4635 |
return $sth->execute ( |
4636 |
$f->{tagfield}, |
4637 |
$f->{tagsubfield}, |
4638 |
$f->{module}, |
4639 |
$f->{filter}, |
4640 |
$f->{on_existing}, |
4641 |
$f->{on_new}, |
4642 |
$f->{on_removed}, |
4643 |
$id |
4644 |
); |
4645 |
} |
4646 |
|
4647 |
=head2 AddMarcPermissionsRule |
4648 |
|
4649 |
my $success = AddMarcPermissionsRule($fields) |
4650 |
|
4651 |
Add rule to the marc_permissions table. |
4652 |
|
4653 |
=over 4 |
4654 |
|
4655 |
=item C<$fields> |
4656 |
|
4657 |
hashref defining the table fields |
4658 |
|
4659 |
tagfield - required |
4660 |
tagsubfield |
4661 |
module - required |
4662 |
filter - required |
4663 |
on_existing - required |
4664 |
on_new - required |
4665 |
on_removed - required |
4666 |
|
4667 |
=back |
4668 |
|
4669 |
Returns: |
4670 |
|
4671 |
=over 4 |
4672 |
|
4673 |
=item C<$success> |
4674 |
|
4675 |
undef if an error occurs, otherwise true. |
4676 |
|
4677 |
=back |
4678 |
|
4679 |
=cut |
4680 |
|
4681 |
sub AddMarcPermissionsRule { |
4682 |
my $f = shift; |
4683 |
my $dbh = C4::Context->dbh; |
4684 |
my $query = ' |
4685 |
INSERT INTO `marc_permissions` |
4686 |
( |
4687 |
tagfield, |
4688 |
tagsubfield, |
4689 |
module, |
4690 |
filter, |
4691 |
on_existing, |
4692 |
on_new, |
4693 |
on_removed |
4694 |
) |
4695 |
VALUES (?, ?, ?, ?, ?, ?, ?) |
4696 |
'; |
4697 |
my $sth = $dbh->prepare($query); |
4698 |
return $sth->execute ( |
4699 |
$f->{tagfield}, |
4700 |
$f->{tagsubfield}, |
4701 |
$f->{module}, |
4702 |
$f->{filter}, |
4703 |
$f->{on_existing}, |
4704 |
$f->{on_new}, |
4705 |
$f->{on_removed} |
4706 |
); |
4707 |
} |
4708 |
|
4709 |
=head2 DelMarcPermissionsRule |
4710 |
|
4711 |
my $success = DelMarcPermissionsRule($id) |
4712 |
|
4713 |
Deletes rule from the marc_permissions table. |
4714 |
|
4715 |
=over 4 |
4716 |
|
4717 |
=item C<$id> |
4718 |
|
4719 |
rule id to delete |
4720 |
|
4721 |
=back |
4722 |
|
4723 |
Returns: |
4724 |
|
4725 |
=over 4 |
4726 |
|
4727 |
=item C<$success> |
4728 |
|
4729 |
undef if an error occurs, otherwise true. |
4730 |
|
4731 |
=back |
4732 |
|
4733 |
=cut |
4734 |
|
4735 |
sub DelMarcPermissionsRule { |
4736 |
my $id = shift; |
4737 |
my $dbh = C4::Context->dbh; |
4738 |
my $query = ' |
4739 |
DELETE FROM `marc_permissions` |
4740 |
WHERE |
4741 |
id = ? |
4742 |
'; |
4743 |
my $sth = $dbh->prepare($query); |
4744 |
return $sth->execute($id); |
4745 |
} |
3789 |
1; |
4746 |
1; |
3790 |
|
4747 |
|
3791 |
|
4748 |
|