| 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; |