|
Lines 29-34
use MARC::File::USMARC;
Link Here
|
| 29 |
use MARC::File::XML; |
29 |
use MARC::File::XML; |
| 30 |
use POSIX qw(strftime); |
30 |
use POSIX qw(strftime); |
| 31 |
use Module::Load::Conditional qw(can_load); |
31 |
use Module::Load::Conditional qw(can_load); |
|
|
32 |
use YAML qw(Load); |
| 32 |
|
33 |
|
| 33 |
use C4::Koha; |
34 |
use C4::Koha; |
| 34 |
use C4::Log; # logaction |
35 |
use C4::Log; # logaction |
|
Lines 94-99
BEGIN {
Link Here
|
| 94 |
&GetMarcFromKohaField |
95 |
&GetMarcFromKohaField |
| 95 |
&GetMarcSubfieldStructureFromKohaField |
96 |
&GetMarcSubfieldStructureFromKohaField |
| 96 |
&GetFrameworkCode |
97 |
&GetFrameworkCode |
|
|
98 |
&GetAutoFrameworkCode |
| 97 |
&TransformKohaToMarc |
99 |
&TransformKohaToMarc |
| 98 |
&PrepHostMarcField |
100 |
&PrepHostMarcField |
| 99 |
|
101 |
|
|
Lines 239-244
sub AddBiblio {
Link Here
|
| 239 |
|
241 |
|
| 240 |
# transform the data into koha-table style data |
242 |
# transform the data into koha-table style data |
| 241 |
SetUTF8Flag($record); |
243 |
SetUTF8Flag($record); |
|
|
244 |
|
| 245 |
$frameworkcode = GetAutoFrameworkCode($record) if (!$frameworkcode || $frameworkcode eq ''); |
| 246 |
|
| 242 |
my $olddata = TransformMarcToKoha( $record, $frameworkcode ); |
247 |
my $olddata = TransformMarcToKoha( $record, $frameworkcode ); |
| 243 |
( $biblionumber, $error ) = _koha_add_biblio( $dbh, $olddata, $frameworkcode ); |
248 |
( $biblionumber, $error ) = _koha_add_biblio( $dbh, $olddata, $frameworkcode ); |
| 244 |
$olddata->{'biblionumber'} = $biblionumber; |
249 |
$olddata->{'biblionumber'} = $biblionumber; |
|
Lines 309-314
sub ModBiblio {
Link Here
|
| 309 |
my $dbh = C4::Context->dbh; |
314 |
my $dbh = C4::Context->dbh; |
| 310 |
|
315 |
|
| 311 |
$frameworkcode = "" if !$frameworkcode || $frameworkcode eq "Default"; # XXX |
316 |
$frameworkcode = "" if !$frameworkcode || $frameworkcode eq "Default"; # XXX |
|
|
317 |
$frameworkcode = GetAutoFrameworkCode($record) if ($frameworkcode eq ""); |
| 312 |
|
318 |
|
| 313 |
_strip_item_fields($record, $frameworkcode); |
319 |
_strip_item_fields($record, $frameworkcode); |
| 314 |
|
320 |
|
|
Lines 2133-2138
sub GetFrameworkCode {
Link Here
|
| 2133 |
return $frameworkcode; |
2139 |
return $frameworkcode; |
| 2134 |
} |
2140 |
} |
| 2135 |
|
2141 |
|
|
|
2142 |
=head2 _matchRecordFieldspec |
| 2143 |
|
| 2144 |
$language = _matchRecordFieldspec($record, '008/35-37'); |
| 2145 |
|
| 2146 |
Returns field value from record. Fieldspec is a string of the following type: |
| 2147 |
'003', '100$a', '000/07', '008/35-37', or any of those types joined with plus sign. |
| 2148 |
If a matching field has been repeated in the record, the value from the first one is returned. |
| 2149 |
|
| 2150 |
=cut |
| 2151 |
|
| 2152 |
sub _matchRecordFieldspec { |
| 2153 |
my ($record, $fieldstr) = @_; |
| 2154 |
|
| 2155 |
$fieldstr =~ s/^\s+//; |
| 2156 |
$fieldstr =~ s/\s+$//; |
| 2157 |
|
| 2158 |
if ($fieldstr =~ /^(\d\d\d)$/) { |
| 2159 |
my $fld = $1; |
| 2160 |
my $data = ''; |
| 2161 |
if ($fld eq '000') { |
| 2162 |
$data = $record->leader(); |
| 2163 |
} else { |
| 2164 |
my $field = $record->field($fld); |
| 2165 |
$data = $field->data() if ($field && $field->is_control_field()); |
| 2166 |
} |
| 2167 |
return $data; |
| 2168 |
} elsif ($fieldstr =~ /^(\d\d\d)\$(\S)$/) { |
| 2169 |
my ($fld, $subfld) = ($1, $2); |
| 2170 |
my $data = ''; |
| 2171 |
my @fields = $record->field($fld); |
| 2172 |
foreach my $field (@fields) { |
| 2173 |
if ($field && !$field->is_control_field() && $field->subfield($subfld)) { |
| 2174 |
return $field->subfield($subfld); |
| 2175 |
} |
| 2176 |
} |
| 2177 |
return $data; |
| 2178 |
} elsif ($fieldstr =~ /^(\d\d\d)\/(\d+)$/) { |
| 2179 |
my ($fld, $pos) = ($1, int($2)); |
| 2180 |
my $data = ''; |
| 2181 |
if ($fld eq '000') { |
| 2182 |
$data = $record->leader(); |
| 2183 |
} else { |
| 2184 |
my $field = $record->field($fld); |
| 2185 |
$data = $field->data() if ($field && $field->is_control_field()); |
| 2186 |
} |
| 2187 |
return substr($data, $pos, 1); |
| 2188 |
} elsif ($fieldstr =~ /^(\d\d\d)\/(\d+)-(\d+)$/) { |
| 2189 |
my ($fld, $spos, $epos) = ($1, int($2), int($3)); |
| 2190 |
my $data = ''; |
| 2191 |
if ($fld eq '000') { |
| 2192 |
$data = $record->leader(); |
| 2193 |
} else { |
| 2194 |
my $field = $record->field($fld); |
| 2195 |
$data = $field->data() if ($field && $field->is_control_field()); |
| 2196 |
} |
| 2197 |
return substr($data, $spos, ($epos-$spos)+1); |
| 2198 |
} elsif ($fieldstr =~ /^(.+)\+(.+)$/) { |
| 2199 |
my ($fld1, $fld2) = ($1, $2); |
| 2200 |
return _matchRecordFieldspec($record, $fld1) . '+' . _matchRecordFieldspec($record, $fld2); |
| 2201 |
} else { |
| 2202 |
warn "_matchRecordFieldspec: unknown fieldspec '$fieldstr'"; |
| 2203 |
} |
| 2204 |
return ''; |
| 2205 |
} |
| 2206 |
|
| 2207 |
=head2 GetAutoFrameworkCode |
| 2208 |
|
| 2209 |
$frameworkcode = GetAutoFrameworkCode( $marcRecord ); |
| 2210 |
|
| 2211 |
Uses the MarcToFrameworkcodeAutoconvert system preference to determine what |
| 2212 |
framework code the MARC record should have, based on the record field values. |
| 2213 |
|
| 2214 |
=cut |
| 2215 |
|
| 2216 |
sub GetAutoFrameworkCode { |
| 2217 |
my ($record) = @_; |
| 2218 |
|
| 2219 |
my $prefname = 'MarcToFrameworkcodeAutoconvert'; |
| 2220 |
|
| 2221 |
my $cache = Koha::Caches->get_instance(); |
| 2222 |
my $cache_key = "parsed-pref-$prefname"; |
| 2223 |
my $fwcoderules = $cache->get_from_cache($cache_key); |
| 2224 |
|
| 2225 |
if (!$fwcoderules) { |
| 2226 |
my $yaml = C4::Context->preference($prefname) || ''; |
| 2227 |
return '' if ($yaml !~ /\S/); |
| 2228 |
|
| 2229 |
$yaml = "$yaml\n\n"; |
| 2230 |
eval { |
| 2231 |
$fwcoderules = YAML::Load($yaml); |
| 2232 |
}; |
| 2233 |
if ($@) { |
| 2234 |
warn "Unable to parse $prefname syspref: $@"; |
| 2235 |
return ''; |
| 2236 |
} |
| 2237 |
|
| 2238 |
if (ref($fwcoderules) ne 'ARRAY') { |
| 2239 |
warn "$prefname YAML root element is not array"; |
| 2240 |
return ''; |
| 2241 |
} |
| 2242 |
|
| 2243 |
$cache->set_in_cache($cache_key, $fwcoderules); |
| 2244 |
} |
| 2245 |
|
| 2246 |
foreach my $elem (@$fwcoderules) { |
| 2247 |
if (ref($elem) ne 'HASH') { |
| 2248 |
warn "$prefname 2nd level YAML element not a hash"; |
| 2249 |
$cache->clear_from_cache($cache_key); |
| 2250 |
return ''; |
| 2251 |
} |
| 2252 |
foreach my $ekey (keys(%{$elem})) { |
| 2253 |
my $matchvalue = _matchRecordFieldspec($record, $ekey) || ''; |
| 2254 |
if (defined($elem->{$ekey})) { |
| 2255 |
my $matches = $elem->{$ekey}; |
| 2256 |
if (ref($elem->{$ekey}) ne 'HASH') { |
| 2257 |
warn "$prefname 3rd level YAML element not a hash"; |
| 2258 |
$cache->clear_from_cache($cache_key); |
| 2259 |
return ''; |
| 2260 |
} |
| 2261 |
my %hmatches = %{$matches}; |
| 2262 |
foreach my $elm (keys(%hmatches)) { |
| 2263 |
return $hmatches{$elm} if ($elm eq $matchvalue); |
| 2264 |
} |
| 2265 |
} |
| 2266 |
} |
| 2267 |
} |
| 2268 |
return ''; |
| 2269 |
} |
| 2270 |
|
| 2136 |
=head2 TransformKohaToMarc |
2271 |
=head2 TransformKohaToMarc |
| 2137 |
|
2272 |
|
| 2138 |
$record = TransformKohaToMarc( $hash [, $params ] ) |
2273 |
$record = TransformKohaToMarc( $hash [, $params ] ) |
|
Lines 3266-3271
sub ModBiblioMarc {
Link Here
|
| 3266 |
if ( !$frameworkcode ) { |
3401 |
if ( !$frameworkcode ) { |
| 3267 |
$frameworkcode = ""; |
3402 |
$frameworkcode = ""; |
| 3268 |
} |
3403 |
} |
|
|
3404 |
$frameworkcode = GetAutoFrameworkCode($record) if ($frameworkcode eq ""); |
| 3269 |
my $sth = $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"); |
3405 |
my $sth = $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"); |
| 3270 |
$sth->execute( $frameworkcode, $biblionumber ); |
3406 |
$sth->execute( $frameworkcode, $biblionumber ); |
| 3271 |
$sth->finish; |
3407 |
$sth->finish; |