View | Details | Raw Unified | Return to bug 12872
Collapse All | Expand All

(-)a/misc/migration_tools/ZebraExclude.pm (+156 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2014 Rijksmuseum
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20
use Modern::Perl;
21
22
sub load_zebra_exclude {
23
    my $pref = C4::Context->preference('ZebraExclude');
24
    #this pref consists of lines /condition/:field list
25
    #the condition is a regex applied to the marcxml (and is optional)
26
    #field list contains XXX (whole record), 9XX (tab), 700 (field) or
27
    #650$ac (one or more subfields)
28
    return if !$pref;
29
30
    my $ret; #return hashref
31
    #add the biblionumber and biblioitemnumber tag for Koha to returned hash
32
    #we are asking for the default framework just as GetMarcBiblio does
33
    #the returned subfield is ignored; we will not exclude these tags
34
    ( $ret->{biblio} ) = GetMarcFromKohaField( "biblio.biblionumber", '');
35
    ( $ret->{biblioitem} ) =
36
        GetMarcFromKohaField( "biblioitems.biblioitemnumber", '');
37
38
    my @lines= split "\n", $pref;
39
    $ret->{list} = [];
40
    foreach my $li ( @lines ) {
41
        my ( $regex, $list );
42
        if( $li =~ /\/(.*)(?<!\\)\/\s*:\s*/ ) {
43
            $regex= $1;
44
            $list= $';
45
        } else {
46
            $list= $li;
47
        }
48
        $regex = _test_condition( $regex );
49
        my $fh = _parse_fieldlist( $list , $ret );
50
        push @{$ret->{list}}, [ $fh, $regex ] if $fh;
51
    }
52
    return $ret;
53
}
54
55
sub exclude_fields {
56
    my ($marc, $exclude) = @_;
57
58
    foreach my $rf ( @{$exclude->{list}} ) {
59
        my $rx= $rf->[1];
60
        #check the condition if present; if not, continue
61
        #if the condition fails, skip this exclude step
62
        if( $rx && $marc->as_xml !~ /$rx/ ) {
63
            next;
64
        }
65
66
        #check wildcards like XXX or 9XX
67
        my $wildcards = $rf->[0]->{wildcard};
68
        foreach my $w ( @$wildcards ) {
69
            if( $w eq 'XXX' ) {
70
                #skip the whole record
71
                return;
72
            } else {
73
                $w =~ s/XX/\.\./;
74
                $marc->delete_fields( _skip999($marc->field($w), $exclude) );
75
            }
76
        }
77
78
        #check whole fields
79
        my $wholefields= $rf->[0]->{field};
80
        foreach( @$wholefields ) {
81
            $marc->delete_fields( $marc->field($_) );
82
        }
83
84
        #check subfields
85
        my $subs= $rf->[0]->{subfield};
86
        foreach my $ref ( @$subs ) {
87
            my ($tag, $sub)= @$ref;
88
            foreach my $fld ( $marc->field($tag) ) {
89
                $fld->delete_subfield( code => qr/$sub/ );
90
                if( !$fld->subfields() ) {
91
                    #if it was the last subfield, delete whole field
92
                    $marc->delete_fields( $fld );
93
                }
94
            }
95
        }
96
    }
97
    return 1;
98
}
99
100
sub _test_condition {
101
    my $regex= shift;
102
    if( $regex ) {
103
        #test the regex
104
        eval { 'whatever string' =~ /$regex/ };
105
        if( $@ ) {
106
            warn "Zebra exclude regex $regex fails!\n";
107
            return;
108
        }
109
    }
110
    return $regex;
111
}
112
113
sub _parse_fieldlist {
114
    my ( $list, $check999 ) = @_;
115
116
    my $ret={};
117
    my @list = split ",", $list;
118
    foreach( @list ) {
119
        next if !/^\s*(\d{3}|\dXX|XXX)\s*[\$:-]?\s*([A-Za-z0-9]*)\s*$/;
120
        my $tag= $1;
121
        my $sub= $2;
122
        if ( $tag =~ /XX/ ) {
123
            #we ignore sub here
124
            #the test for 999 tags in this case is done later on
125
            push @{$ret->{wildcard}}, $tag;
126
        } elsif( defined $sub && $sub ne '' ) { #could be e.g. '0'
127
            next if !_comp999( $tag, $check999 );
128
            push @{$ret->{subfield}}, [ $tag, $sub ];
129
        } else {
130
            next if !_comp999( $tag, $check999 );
131
            push @{$ret->{field}}, $tag;
132
        }
133
    }
134
    if( keys %$ret ) { # add possible missing hash keys
135
       $ret->{wildcard} = [] if !exists $ret->{wildcard};
136
       $ret->{subfield} = [] if !exists $ret->{subfield};
137
       $ret->{field} = [] if !exists $ret->{field};
138
       return $ret;
139
    }
140
    return;
141
}
142
143
sub _skip999 {
144
    my $excl= pop @_;
145
    #pass list of fields, removes the Koha biblio and biblioitem field
146
    #in order to not delete them
147
    #default tag is 999, but may have changed..
148
    grep { _comp999($_->tag, $excl) } @_;
149
}
150
151
sub _comp999 {
152
    my ($tag, $excl) = @_;
153
    return $tag ne $excl->{biblio} && $tag ne $excl->{biblioitem};
154
}
155
156
1;
(-)a/misc/migration_tools/rebuild_zebra.pl (-8 / +12 lines)
Lines 13-18 use C4::AuthoritiesMarc; Link Here
13
use C4::Items;
13
use C4::Items;
14
use Koha::RecordProcessor;
14
use Koha::RecordProcessor;
15
use XML::LibXML;
15
use XML::LibXML;
16
use misc::migration_tools::ZebraExclude;
16
17
17
use constant LOCK_FILENAME => 'rebuild..LCK';
18
use constant LOCK_FILENAME => 'rebuild..LCK';
18
19
Lines 155-160 my $kohadir = C4::Context->config('intranetdir'); Link Here
155
my $bib_index_mode  = C4::Context->config('zebra_bib_index_mode')  // 'dom';
156
my $bib_index_mode  = C4::Context->config('zebra_bib_index_mode')  // 'dom';
156
my $auth_index_mode = C4::Context->config('zebra_auth_index_mode') // 'dom';
157
my $auth_index_mode = C4::Context->config('zebra_auth_index_mode') // 'dom';
157
158
159
my $zebra_excl= load_zebra_exclude();
160
158
my $dbh = C4::Context->dbh;
161
my $dbh = C4::Context->dbh;
159
my ($biblionumbertagfield,$biblionumbertagsubfield) = &GetMarcFromKohaField("biblio.biblionumber","");
162
my ($biblionumbertagfield,$biblionumbertagsubfield) = &GetMarcFromKohaField("biblio.biblionumber","");
160
my ($biblioitemnumbertagfield,$biblioitemnumbertagsubfield) = &GetMarcFromKohaField("biblioitems.biblioitemnumber","");
163
my ($biblioitemnumbertagfield,$biblioitemnumbertagsubfield) = &GetMarcFromKohaField("biblioitems.biblioitemnumber","");
Lines 635-652 sub get_corrected_marc_record { Link Here
635
    my $marc = get_raw_marc_record($record_type, $record_number, $noxml);
638
    my $marc = get_raw_marc_record($record_type, $record_number, $noxml);
636
639
637
    if (defined $marc) {
640
    if (defined $marc) {
641
        return if $zebra_excl && !defined( exclude_fields($marc,$zebra_excl) );
638
        fix_leader($marc);
642
        fix_leader($marc);
639
        if ($record_type eq 'authority') {
640
            fix_authority_id($marc, $record_number);
641
        } elsif ($record_type eq 'biblio' && C4::Context->preference('IncludeSeeFromInSearches')) {
642
            my $normalizer = Koha::RecordProcessor->new( { filters => 'EmbedSeeFromHeadings' } );
643
            $marc = $normalizer->process($marc);
644
        }
645
        if (C4::Context->preference("marcflavour") eq "UNIMARC") {
643
        if (C4::Context->preference("marcflavour") eq "UNIMARC") {
646
            fix_unimarc_100($marc);
644
            fix_unimarc_100($marc);
647
        }
645
        }
646
        if ($record_type eq 'authority') {
647
            fix_authority_id($marc, $record_number);
648
        } elsif ($record_type eq 'biblio') {
649
            if (C4::Context->preference('IncludeSeeFromInSearches')) {
650
                my $normalizer = Koha::RecordProcessor->new( { filters => 'EmbedSeeFromHeadings' } );
651
                $marc = $normalizer->process($marc);
652
            }
653
        }
648
    }
654
    }
649
650
    return $marc;
655
    return $marc;
651
}
656
}
652
657
653
- 

Return to bug 12872