| 1 | # vim: sw=4 | ||
| 2 | package MARC::Transform; | ||
| 3 | use 5.10.0; | ||
| 4 | use warnings; | ||
| 5 | use strict; | ||
| 6 | use Carp; | ||
| 7 | use MARC::Record; | ||
| 8 | use YAML; | ||
| 9 | use Scalar::Util qw< reftype >; | ||
| 10 | our $VERSION = '0.003007'; | ||
| 11 | our $DEBUG = 0; | ||
| 12 | sub debug { $DEBUG and say STDERR @_ } | ||
| 13 | |||
| 14 | my %fields; | ||
| 15 | my $globalcondition; | ||
| 16 | my $record; | ||
| 17 | our $mth; | ||
| 18 | my $globalsubs; | ||
| 19 | my $verbose=0; | ||
| 20 | my @errors; | ||
| 21 | my $global_LUT; | ||
| 22 | our $this=""; | ||
| 23 | |||
| 24 | sub new { | ||
| 25 | my ($self,$recordsource,$yaml,$mthsource,$verb) = @_; | ||
| 26 | my @yaml; | ||
| 27 | no warnings 'redefine'; | ||
| 28 | no warnings 'newline'; | ||
| 29 | my $yamltoload; | ||
| 30 | if ( -e $yaml ) { | ||
| 31 | open my $yamls, "< $yaml" or die "can't open file: $!"; | ||
| 32 | my $yamlline; | ||
| 33 | while ($yamlline = <$yamls>){ $yamltoload.=$yamlline; } | ||
| 34 | close $yamls; | ||
| 35 | #@yaml = YAML::LoadFile($yamls); | ||
| 36 | } | ||
| 37 | else { | ||
| 38 | $yamltoload=$yaml; | ||
| 39 | #@yaml = YAML::Load($yaml); | ||
| 40 | } | ||
| 41 | $yamltoload=~s/#_dollars_#/\\#_dollars_\\#/g; | ||
| 42 | $yamltoload=~s/#_dbquote_#/\\#_dbquote_\\#/g; | ||
| 43 | @yaml = YAML::Load($yamltoload); | ||
| 44 | #warn "================". Data::Dumper::Dumper (\@yaml)."------------------"; | ||
| 45 | $record=$recordsource; | ||
| 46 | $mth=$mthsource; | ||
| 47 | $$mth{"_defaultLUT_to_mth_"}={} if $mth; | ||
| 48 | ReplaceAllInRecord("before"); | ||
| 49 | $verbose = 1 if ($verb); | ||
| 50 | foreach my $rulesub(@yaml) { | ||
| 51 | if ( ref($rulesub) eq "HASH" ) { | ||
| 52 | if ( defnonull ( $$rulesub{'global_subs'} ) ) { | ||
| 53 | $globalsubs = $$rulesub{'global_subs'}; | ||
| 54 | eval ($globalsubs); | ||
| 55 | } | ||
| 56 | if ( defnonull ( $$rulesub{'global_LUT'} ) ) { | ||
| 57 | if (ref($$rulesub{'global_LUT'}) eq "HASH") { | ||
| 58 | $global_LUT=$$rulesub{'global_LUT'}; | ||
| 59 | } | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | foreach my $rule(@yaml) { | ||
| 64 | #print Data::Dumper::Dumper ($rule); | ||
| 65 | if ( ref($rule) eq "ARRAY" ) { | ||
| 66 | my $subs=""; | ||
| 67 | foreach my $rul ( @$rule ) { | ||
| 68 | if ( defnonull ( $$rul{'subs'} ) ) { | ||
| 69 | $subs.=$$rul{'subs'}; | ||
| 70 | } | ||
| 71 | if ( defnonull ( $$rul{'LUT'} ) ) { | ||
| 72 | $$global_LUT{"lookuptableforthis"}=$$rul{'LUT'};#warn Data::Dumper::Dumper $global_LUT; | ||
| 73 | } | ||
| 74 | } | ||
| 75 | foreach my $rul ( @$rule ) { | ||
| 76 | my ($actionsin, $actionsinter, $actionsout)= parseactions($rul);#warn Data::Dumper::Dumper ($rul); | ||
| 77 | my $boolcondition = testrule($rul, $actionsin, $actionsinter, $actionsout, $subs); | ||
| 78 | #warn $boolcondition;warn "actionsin : ".$actionsin;warn "actionsout : ".$actionsout; | ||
| 79 | if ($boolcondition) { | ||
| 80 | last; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | } | ||
| 84 | elsif ( ref($rule) eq "HASH" ) { | ||
| 85 | my $subs=""; | ||
| 86 | if ( defnonull ( $$rule{'subs'} ) ) { | ||
| 87 | $subs.=$$rule{'subs'}; | ||
| 88 | } | ||
| 89 | if ( defnonull ( $$rule{'LUT'} ) ) { | ||
| 90 | $$global_LUT{"lookuptableforthis"}=$$rule{'LUT'}; | ||
| 91 | } | ||
| 92 | my ($actionsin, $actionsinter, $actionsout)= parseactions($rule); | ||
| 93 | my $boolcondition = testrule($rule, $actionsin, $actionsinter, $actionsout, $subs); | ||
| 94 | } | ||
| 95 | else { | ||
| 96 | push(@errors, 'Invalid yaml : you try to use a scalar rule.'); #error | ||
| 97 | } | ||
| 98 | } | ||
| 99 | foreach my $error (@errors) { | ||
| 100 | print "\n$error"; | ||
| 101 | } | ||
| 102 | ReplaceAllInRecord("after"); | ||
| 103 | $record; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub defnonull { my $var = shift; if (defined $var and $var ne "") { return 1; } else { return 0; } } | ||
| 107 | |||
| 108 | sub LUT { | ||
| 109 | my ( $inLUT, $type ) = @_; | ||
| 110 | if (!defined($type)) { | ||
| 111 | $type = "lookuptableforthis"; | ||
| 112 | } | ||
| 113 | my $outLUT=$inLUT; | ||
| 114 | my $boolnocor = 1; | ||
| 115 | if ( ref($global_LUT) eq "HASH") { | ||
| 116 | if (exists($$global_LUT{$type})) { | ||
| 117 | my $correspondance=$$global_LUT{$type}; | ||
| 118 | if ( ref($correspondance) eq "HASH") { | ||
| 119 | foreach my $cor (keys(%$correspondance)) { | ||
| 120 | if ($inLUT eq $cor) { | ||
| 121 | $outLUT=$$correspondance{$cor}; | ||
| 122 | $boolnocor = 0; | ||
| 123 | } | ||
| 124 | } | ||
| 125 | if ($boolnocor) { | ||
| 126 | $outLUT=$$correspondance{"_default_value_"} if (defnonull($$correspondance{"_default_value_"})); | ||
| 127 | push (@{$$mth{"_defaultLUT_to_mth_"}->{"$type"}} , $inLUT) if $mth; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | } | ||
| 131 | } | ||
| 132 | return $outLUT; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub update { | ||
| 136 | my ($field,$subfields)=@_; | ||
| 137 | transform ("update",$field,$subfields); | ||
| 138 | return 1; | ||
| 139 | } | ||
| 140 | sub forceupdate { | ||
| 141 | my ($field,$subfields)=@_; | ||
| 142 | transform ("forceupdate",$field,$subfields); | ||
| 143 | return 1; | ||
| 144 | } | ||
| 145 | sub updatefirst { | ||
| 146 | my ($field,$subfields)=@_; | ||
| 147 | transform ("updatefirst",$field,$subfields); | ||
| 148 | return 1; | ||
| 149 | } | ||
| 150 | sub forceupdatefirst { | ||
| 151 | my ($field,$subfields)=@_; | ||
| 152 | transform ("forceupdatefirst",$field,$subfields); | ||
| 153 | return 1; | ||
| 154 | } | ||
| 155 | sub create { | ||
| 156 | my ($field,$subfields)=@_; | ||
| 157 | transform ("create",$field,$subfields); | ||
| 158 | return 1; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub transform { | ||
| 162 | my ($ttype,$field,$subfields)=@_; | ||
| 163 | #print "\n------------$ttype------------ : \n".Data::Dumper::Dumper (@_); | ||
| 164 | if ($ttype eq "forceupdate" or $ttype eq "forceupdatefirst" ) { | ||
| 165 | if (ref($field) eq "" or ref($field) eq "SCALAR") { | ||
| 166 | if (!defined $record->field($field) ) {$ttype="create"} | ||
| 167 | } | ||
| 168 | } | ||
| 169 | if (ref($field) eq "MARC::Field") { | ||
| 170 | #print "\n------------$ttype------------ : \n".Data::Dumper::Dumper ($subfields); | ||
| 171 | foreach my $tag(keys(%$subfields)) { | ||
| 172 | if ( $tag eq 'i1' or $tag eq 'µ') { | ||
| 173 | #print "\n------------$ttype------------ : \n"; | ||
| 174 | $this=$field->indicator(1); | ||
| 175 | my $finalvalue=parsestringactions($$subfields{$tag}); | ||
| 176 | $field->update( ind1 => $finalvalue ) if ( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" ); | ||
| 177 | } | ||
| 178 | elsif ( $tag eq 'i2' or $tag eq '£') { | ||
| 179 | $this=$field->indicator(2); | ||
| 180 | my $finalvalue=parsestringactions($$subfields{$tag}); | ||
| 181 | $field->update( ind2 => $finalvalue ) if ( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" ); | ||
| 182 | } | ||
| 183 | else { | ||
| 184 | if( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" ) { | ||
| 185 | if($field->is_control_field()) { | ||
| 186 | $this=$field->data(); | ||
| 187 | my $finalvalue=parsestringactions($$subfields{$tag}); | ||
| 188 | $field->update($finalvalue); | ||
| 189 | } | ||
| 190 | else { | ||
| 191 | if ($ttype eq "create") { | ||
| 192 | $this=""; | ||
| 193 | my $finalvalue=parsestringactions($$subfields{$tag}); | ||
| 194 | $field->add_subfields( $tag => $finalvalue ); | ||
| 195 | } | ||
| 196 | elsif ($ttype eq "updatefirst") { | ||
| 197 | if ( defined $field->subfield( $tag ) ) { | ||
| 198 | $this=$field->subfield( $tag ); | ||
| 199 | my $finalvalue=parsestringactions($$subfields{$tag}); | ||
| 200 | $field->update( $tag => $finalvalue ); | ||
| 201 | } | ||
| 202 | #warn $tag.$$subfields{$tag}; | ||
| 203 | } | ||
| 204 | elsif ($ttype eq "forceupdatefirst") { | ||
| 205 | if ( defined $field->subfield( $tag ) ) { | ||
| 206 | $this=$field->subfield( $tag ); | ||
| 207 | my $finalvalue=parsestringactions($$subfields{$tag}); | ||
| 208 | $field->update( $tag => $finalvalue ); | ||
| 209 | } | ||
| 210 | else { | ||
| 211 | $this=""; | ||
| 212 | my $finalvalue=parsestringactions($$subfields{$tag}); | ||
| 213 | $field->add_subfields( $tag => $finalvalue ); | ||
| 214 | } | ||
| 215 | } | ||
| 216 | } | ||
| 217 | } | ||
| 218 | elsif( ref($$subfields{$tag}) eq "ARRAY" ) { | ||
| 219 | if(!$field->is_control_field()) { | ||
| 220 | foreach my $subfield(@{$$subfields{$tag}}) { | ||
| 221 | if ($ttype eq "create") { | ||
| 222 | $this=""; | ||
| 223 | my $finalvalue=parsestringactions($subfield); | ||
| 224 | $field->add_subfields( $tag => $finalvalue ); | ||
| 225 | } | ||
| 226 | elsif ($ttype eq "updatefirst") { | ||
| 227 | if ( defined $field->subfield( $tag ) ) { | ||
| 228 | $this=$field->subfield( $tag ); | ||
| 229 | my $finalvalue=parsestringactions($subfield); | ||
| 230 | $field->update( $tag => $finalvalue ); | ||
| 231 | } | ||
| 232 | } | ||
| 233 | elsif ($ttype eq "forceupdatefirst") { | ||
| 234 | if ( defined $field->subfield( $tag ) ) { | ||
| 235 | $this=$field->subfield( $tag ); | ||
| 236 | my $finalvalue=parsestringactions($subfield); | ||
| 237 | $field->update( $tag => $finalvalue ); | ||
| 238 | } | ||
| 239 | else { | ||
| 240 | $this=""; | ||
| 241 | my $finalvalue=parsestringactions($subfield); | ||
| 242 | $field->add_subfields( $tag => $finalvalue ); | ||
| 243 | } | ||
| 244 | } | ||
| 245 | } | ||
| 246 | } | ||
| 247 | else { | ||
| 248 | push(@errors, 'Invalid yaml : you try to use an array to '.$ttype.' in existing condition\'s controlfield value.'); #error | ||
| 249 | } | ||
| 250 | } | ||
| 251 | } | ||
| 252 | } | ||
| 253 | if((!$field->is_control_field()) and ($ttype eq "update" or $ttype eq "forceupdate" )) { | ||
| 254 | my @usubfields; | ||
| 255 | foreach my $subfield ( $field->subfields() ) { | ||
| 256 | if ( exists($$subfields{$$subfield[0]}) ) { | ||
| 257 | #implementation de l'eval des fonctions et de $this | ||
| 258 | $this=$$subfield[1]; | ||
| 259 | my $finalvalue=parsestringactions($$subfields{$$subfield[0]}); | ||
| 260 | push @usubfields, ( $$subfield[0],$finalvalue ); | ||
| 261 | } | ||
| 262 | else { | ||
| 263 | push @usubfields, ( $$subfield[0], $$subfield[1] ); | ||
| 264 | } | ||
| 265 | } | ||
| 266 | my $newfield = MARC::Field->new( $field->tag(), $field->indicator(1), $field->indicator(2), @usubfields ); | ||
| 267 | foreach my $tag(keys(%$subfields)) { | ||
| 268 | if($tag ne 'i1' and $tag ne 'µ' and $tag ne 'i2' and $tag ne '£' and !defined($newfield->subfield( $tag )) ) { | ||
| 269 | if( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" ) { | ||
| 270 | $this=""; | ||
| 271 | my $finalvalue=parsestringactions($$subfields{$tag}); | ||
| 272 | $newfield->add_subfields( $tag => $finalvalue ) if $ttype eq "forceupdate"; | ||
| 273 | } | ||
| 274 | else { | ||
| 275 | push(@errors, 'Invalid yaml : you try to use a non-scalar value to '.$ttype.' in existing condition\'s field value.'); #error | ||
| 276 | } | ||
| 277 | } | ||
| 278 | } | ||
| 279 | $field->replace_with($newfield); | ||
| 280 | } | ||
| 281 | } | ||
| 282 | elsif (ref($field) eq "" or ref($field) eq "SCALAR") { | ||
| 283 | #print "\n------------$ttype------------ : \n".Data::Dumper::Dumper (@_); | ||
| 284 | if ($ttype eq "update" or $ttype eq "updatefirst" or $ttype eq "forceupdate" or $ttype eq "forceupdatefirst") { | ||
| 285 | if ( defined $record->field($field) ) { | ||
| 286 | for my $updatefield ( $record->field($field) ) { | ||
| 287 | foreach my $tag(keys(%$subfields)) { | ||
| 288 | if ( $tag eq 'i1' or $tag eq 'µ') { | ||
| 289 | $this=$updatefield->indicator(1); | ||
| 290 | my $finalvalue=parsestringactions($$subfields{$tag}); | ||
| 291 | $updatefield->update( ind1 => $finalvalue ) if ( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" ); | ||
| 292 | } | ||
| 293 | elsif ( $tag eq 'i2' or $tag eq '£') { | ||
| 294 | $this=$updatefield->indicator(2); | ||
| 295 | my $finalvalue=parsestringactions($$subfields{$tag}); | ||
| 296 | $updatefield->update( ind2 => $finalvalue ) if ( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" ); | ||
| 297 | } | ||
| 298 | elsif( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" ) { | ||
| 299 | if($updatefield->is_control_field()) { | ||
| 300 | $this=$updatefield->data(); | ||
| 301 | my $finalvalue=parsestringactions($$subfields{$tag}); | ||
| 302 | $updatefield->update($finalvalue); | ||
| 303 | } | ||
| 304 | elsif ( $ttype eq "updatefirst" ) { | ||
| 305 | if ( defined $updatefield->subfield( $tag ) ) { | ||
| 306 | $this=$updatefield->subfield( $tag ); | ||
| 307 | my $finalvalue=parsestringactions($$subfields{$tag}); | ||
| 308 | $updatefield->update( $tag => $finalvalue ); | ||
| 309 | } | ||
| 310 | } | ||
| 311 | elsif ($ttype eq "forceupdatefirst") { | ||
| 312 | if ( defined $updatefield->subfield( $tag ) ) { | ||
| 313 | $this=$updatefield->subfield( $tag ); | ||
| 314 | my $finalvalue=parsestringactions($$subfields{$tag}); | ||
| 315 | $updatefield->update( $tag => $finalvalue ); | ||
| 316 | } | ||
| 317 | else { | ||
| 318 | $this=""; | ||
| 319 | my $finalvalue=parsestringactions($$subfields{$tag}); | ||
| 320 | $updatefield->add_subfields( $tag => $finalvalue ); | ||
| 321 | } | ||
| 322 | } | ||
| 323 | } | ||
| 324 | else { | ||
| 325 | push(@errors, 'Invalid yaml : you try to use a non-scalar value to '.$ttype.' field.');#error | ||
| 326 | } | ||
| 327 | } | ||
| 328 | if((!$updatefield->is_control_field()) and ($ttype eq "update" or $ttype eq "forceupdate" )) { | ||
| 329 | my @usubfields; | ||
| 330 | foreach my $subfield ( $updatefield->subfields() ) { | ||
| 331 | if ( exists($$subfields{$$subfield[0]}) ) { | ||
| 332 | $this=$$subfield[1]; | ||
| 333 | my $finalvalue=parsestringactions($$subfields{$$subfield[0]}); | ||
| 334 | push @usubfields, ( $$subfield[0],$finalvalue ); | ||
| 335 | } | ||
| 336 | else { | ||
| 337 | push @usubfields, ( $$subfield[0], $$subfield[1] ); | ||
| 338 | } | ||
| 339 | } | ||
| 340 | my $newfield = MARC::Field->new( $updatefield->tag(), $updatefield->indicator(1), $updatefield->indicator(2), @usubfields ); | ||
| 341 | foreach my $tag(keys(%$subfields)) { | ||
| 342 | if($tag ne 'i1' and $tag ne 'µ' and $tag ne 'i2' and $tag ne '£' and !defined($newfield->subfield( $tag )) ) { | ||
| 343 | if( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" ) { | ||
| 344 | $this=""; | ||
| 345 | my $finalvalue=parsestringactions($$subfields{$tag}); | ||
| 346 | $newfield->add_subfields( $tag => $finalvalue ) if $ttype eq "forceupdate"; | ||
| 347 | } | ||
| 348 | else { | ||
| 349 | push(@errors, 'Invalid yaml : you try to use a non-scalar value to '.$ttype.' field.');#error | ||
| 350 | } | ||
| 351 | } | ||
| 352 | } | ||
| 353 | $updatefield->replace_with($newfield); | ||
| 354 | } | ||
| 355 | } | ||
| 356 | } | ||
| 357 | } | ||
| 358 | elsif ($ttype eq "create") { | ||
| 359 | my $newfield; | ||
| 360 | $this=""; | ||
| 361 | if ($field < "010" ) { | ||
| 362 | $newfield = MARC::Field->new( $field, 'temp'); | ||
| 363 | } | ||
| 364 | else { | ||
| 365 | $newfield = MARC::Field->new( $field, '', '', '0'=>'temp'); | ||
| 366 | } | ||
| 367 | |||
| 368 | foreach my $tag(keys(%$subfields)) { | ||
| 369 | if ( $tag eq 'i1' or $tag eq 'µ') { | ||
| 370 | my $finalvalue=parsestringactions($$subfields{$tag}); | ||
| 371 | $newfield->update( ind1 => $finalvalue ) if ( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" ); | ||
| 372 | } | ||
| 373 | elsif ( $tag eq 'i2' or $tag eq '£') { | ||
| 374 | my $finalvalue=parsestringactions($$subfields{$tag}); | ||
| 375 | $newfield->update( ind2 => $finalvalue ) if ( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" ); | ||
| 376 | } | ||
| 377 | else { | ||
| 378 | if( ref($$subfields{$tag}) eq "" or ref($$subfields{$tag}) eq "SCALAR" ) { | ||
| 379 | if($newfield->is_control_field()) { | ||
| 380 | my $finalvalue=parsestringactions($$subfields{$tag}); | ||
| 381 | $newfield->update($finalvalue); | ||
| 382 | } | ||
| 383 | else { | ||
| 384 | my $finalvalue=parsestringactions($$subfields{$tag}); | ||
| 385 | $newfield->add_subfields( $tag => $finalvalue ); | ||
| 386 | } | ||
| 387 | } | ||
| 388 | elsif( ref($$subfields{$tag}) eq "ARRAY" ) { | ||
| 389 | if(!$newfield->is_control_field()) { | ||
| 390 | foreach my $subfield(@{$$subfields{$tag}}) { | ||
| 391 | my $finalvalue=parsestringactions($subfield); | ||
| 392 | $newfield->add_subfields( $tag => $finalvalue ); | ||
| 393 | } | ||
| 394 | } | ||
| 395 | } | ||
| 396 | } | ||
| 397 | } | ||
| 398 | if (!$newfield->is_control_field()) { | ||
| 399 | $newfield->delete_subfield(pos => '0'); | ||
| 400 | } | ||
| 401 | $record->insert_fields_ordered($newfield); | ||
| 402 | } | ||
| 403 | } | ||
| 404 | else { | ||
| 405 | push(@errors, 'Invalid yaml : you try to use an array or hash value to '.$ttype.' field.');#error | ||
| 406 | } | ||
| 407 | return 1; | ||
| 408 | } | ||
| 409 | |||
| 410 | sub parsestringactions { | ||
| 411 | my $subfieldtemp=shift; | ||
| 412 | $subfieldtemp=~s/tempnameforcurrentvalueofthissubfield/\$this/g; | ||
| 413 | $subfieldtemp=~s/temporarycallfunction/\\&/g; | ||
| 414 | my $finalvalue; | ||
| 415 | if ($subfieldtemp=~/\\&/) { | ||
| 416 | $subfieldtemp=~s/\\&/&/g; | ||
| 417 | $finalvalue = eval ($subfieldtemp); | ||
| 418 | } | ||
| 419 | else { | ||
| 420 | $finalvalue = eval '"'.$subfieldtemp.'"'; | ||
| 421 | } | ||
| 422 | return $finalvalue; | ||
| 423 | } | ||
| 424 | |||
| 425 | sub parseactions { | ||
| 426 | my $rul = shift; | ||
| 427 | my $actionsintemp=""; | ||
| 428 | my $actionsin=""; | ||
| 429 | my $actionsinter=""; | ||
| 430 | my $actionsouttemp=""; | ||
| 431 | my $actionsout=""; | ||
| 432 | #print "\n".Data::Dumper::Dumper $rul; | ||
| 433 | #create duplicatefield forceupdate forceupdatefirst update updatefirst execute delete | ||
| 434 | if ( defnonull ( $$rul{'create'} ) ) { | ||
| 435 | ($actionsintemp,$actionsouttemp)=parsesubaction ($$rul{'create'},'create'); | ||
| 436 | $actionsin.=$actionsintemp; $actionsout.=$actionsouttemp; | ||
| 437 | } | ||
| 438 | if ( defnonull ( $$rul{'duplicatefield'} ) ) { | ||
| 439 | ($actionsintemp,$actionsouttemp)=parsesubaction ($$rul{'duplicatefield'},'duplicatefield'); | ||
| 440 | $actionsinter.=$actionsintemp; $actionsout.=$actionsouttemp; | ||
| 441 | } | ||
| 442 | if ( defnonull ( $$rul{'forceupdate'} ) ) { | ||
| 443 | ($actionsintemp,$actionsouttemp)=parsesubaction ($$rul{'forceupdate'},'forceupdate'); | ||
| 444 | $actionsin.=$actionsintemp; $actionsout.=$actionsouttemp; | ||
| 445 | } | ||
| 446 | if ( defnonull ( $$rul{'forceupdatefirst'} ) ) { | ||
| 447 | ($actionsintemp,$actionsouttemp)=parsesubaction ($$rul{'forceupdatefirst'},'forceupdatefirst'); | ||
| 448 | $actionsin.=$actionsintemp; $actionsout.=$actionsouttemp; | ||
| 449 | } | ||
| 450 | if ( defnonull ( $$rul{'update'} ) ) { | ||
| 451 | ($actionsintemp,$actionsouttemp)=parsesubaction ($$rul{'update'},'update'); | ||
| 452 | $actionsin.=$actionsintemp; $actionsout.=$actionsouttemp; | ||
| 453 | } | ||
| 454 | if ( defnonull ( $$rul{'updatefirst'} ) ) { | ||
| 455 | ($actionsintemp,$actionsouttemp)=parsesubaction ($$rul{'updatefirst'},'updatefirst'); | ||
| 456 | $actionsin.=$actionsintemp; $actionsout.=$actionsouttemp; | ||
| 457 | } | ||
| 458 | if ( defnonull ( $$rul{'execute'} ) ) { | ||
| 459 | ($actionsintemp,$actionsouttemp)=parsesubaction ($$rul{'execute'},'execute'); | ||
| 460 | $actionsin.=$actionsintemp; $actionsout.=$actionsouttemp; | ||
| 461 | } | ||
| 462 | if ( defnonull ( $$rul{'delete'} ) ) { | ||
| 463 | ($actionsintemp,$actionsouttemp)=parsesubaction ($$rul{'delete'},'delete'); | ||
| 464 | $actionsin.=$actionsintemp; $actionsout.=$actionsouttemp; | ||
| 465 | } | ||
| 466 | #print "\n----------------------actionsin---------------------- : \n$actionsin\n\n----------------------actionsout---------------------- : \n$actionsout\n----------------------actionsend----------------------"; | ||
| 467 | return ($actionsin, $actionsinter, $actionsout) | ||
| 468 | } | ||
| 469 | |||
| 470 | sub parsesubaction { | ||
| 471 | my ($intaction,$type)=@_; | ||
| 472 | my $actionsin=""; | ||
| 473 | my $actionsout=""; | ||
| 474 | my $boolin=0; | ||
| 475 | my $specaction=""; | ||
| 476 | my $currentaction="";#warn ref($intaction); | ||
| 477 | $specaction=" $type"; | ||
| 478 | #print "\n".Data::Dumper::Dumper $intaction; | ||
| 479 | if ($type eq "create" or $type eq "forceupdate" or $type eq "update" or $type eq "forceupdatefirst" or $type eq "updatefirst") { | ||
| 480 | if ( ref($intaction) eq "HASH" ) { | ||
| 481 | foreach my $kint (keys(%$intaction)) { | ||
| 482 | if( ref($$intaction{$kint}) eq "HASH" ) { | ||
| 483 | my $ftag; | ||
| 484 | $currentaction=""; | ||
| 485 | $boolin=0; | ||
| 486 | if($kint=~/^\$f(\d{3})$/) { | ||
| 487 | $boolin=1; | ||
| 488 | $ftag=$kint; | ||
| 489 | } | ||
| 490 | elsif($kint=~/^f(\d{3})$/) { | ||
| 491 | $ftag='"'.$1.'"'; | ||
| 492 | } | ||
| 493 | else { | ||
| 494 | push(@errors, 'Invalid yaml : your field reference is not valid in '.$type.' action.');#error | ||
| 495 | next; | ||
| 496 | } | ||
| 497 | $currentaction.=$specaction.'('.$ftag.',{'; | ||
| 498 | my $subint=$$intaction{$kint}; | ||
| 499 | foreach my $k (keys(%$subint)) { | ||
| 500 | if( ref($$subint{$k}) eq "" or ref($$subint{$k}) eq "SCALAR" ) { | ||
| 501 | $$subint{$k}=~s/"/\\"/g; | ||
| 502 | $boolin=1 if($$subint{$k}=~/\$f/);#print $k." eq. ".$$subint{$k}."\n"; | ||
| 503 | $$subint{$k}=~s/\$this/tempnameforcurrentvalueofthissubfield/g; | ||
| 504 | $$subint{$k}=~s/\\&/temporarycallfunction/g; | ||
| 505 | $currentaction.='"'.$k.'"=> "'.$$subint{$k}.'",'; | ||
| 506 | } | ||
| 507 | elsif( ref($$subint{$k}) eq "ARRAY" ) { | ||
| 508 | $currentaction.='"'.$k.'"=>['; | ||
| 509 | foreach my $ssubint(@{$$subint{$k}}) { | ||
| 510 | $ssubint=~s/"/\\"/g; | ||
| 511 | $boolin=1 if($ssubint=~/\$f/); | ||
| 512 | $ssubint=~s/\$this/tempnameforcurrentvalueofthissubfield/g; | ||
| 513 | $ssubint=~s/\\&/temporarycallfunction/g; | ||
| 514 | $currentaction.='"'.$ssubint.'",'; | ||
| 515 | } | ||
| 516 | $currentaction.='],'; | ||
| 517 | } | ||
| 518 | else { | ||
| 519 | push(@errors, 'Invalid yaml : you try to use a hash inside another hash in '.$type.' action.');#error | ||
| 520 | } | ||
| 521 | } | ||
| 522 | $currentaction.='});'."\n"; | ||
| 523 | if ($boolin) { $actionsin.=$currentaction; } else { $actionsout.=$currentaction; } | ||
| 524 | } | ||
| 525 | elsif( ref($$intaction{$kint}) eq "" or ref($$intaction{$kint}) eq "SCALAR" ) { | ||
| 526 | $currentaction=""; | ||
| 527 | $boolin=0; | ||
| 528 | my $ftag; | ||
| 529 | my $stag; | ||
| 530 | if($kint=~/^\$f(\d{3})(\w)$/) { | ||
| 531 | $boolin=1; | ||
| 532 | $ftag='$f'.$1; | ||
| 533 | $stag=$2; | ||
| 534 | } | ||
| 535 | elsif($kint=~/^\$i(\d{3})(\w)$/) { | ||
| 536 | $boolin=1; | ||
| 537 | $ftag='$f'.$1; | ||
| 538 | $stag='µ'; | ||
| 539 | $stag='µ' if($2 eq "1"); | ||
| 540 | $stag='£' if($2 eq "2"); | ||
| 541 | } | ||
| 542 | elsif($kint=~/^f(\d{3})(\w)$/) { | ||
| 543 | $ftag='"'.$1.'"'; | ||
| 544 | $stag=$2; | ||
| 545 | } | ||
| 546 | elsif($kint=~/^i(\d{3})(\w)$/) { | ||
| 547 | $ftag='"'.$1.'"'; | ||
| 548 | $stag='µ'; | ||
| 549 | $stag='µ' if($2 eq "1"); | ||
| 550 | $stag='£' if($2 eq "2"); | ||
| 551 | } | ||
| 552 | elsif($kint=~/^i(\d)$/) { | ||
| 553 | $ftag='$$currentfield'; | ||
| 554 | $stag='µ'; | ||
| 555 | $stag='µ' if($1 eq "1"); | ||
| 556 | $stag='£' if($1 eq "2"); | ||
| 557 | $boolin=1; | ||
| 558 | } | ||
| 559 | elsif($kint=~/^(\w)$/) { | ||
| 560 | $ftag='$$currentfield'; | ||
| 561 | $boolin=1; | ||
| 562 | $stag=$kint; | ||
| 563 | } | ||
| 564 | else { | ||
| 565 | push(@errors, 'Invalid yaml : your field reference is not valid in '.$type.' action.');#error | ||
| 566 | next; | ||
| 567 | } | ||
| 568 | $$intaction{$kint}=~s/"/\\"/g; | ||
| 569 | $boolin=1 if($$intaction{$kint}=~/\$f/); | ||
| 570 | $$intaction{$kint}=~s/\$this/tempnameforcurrentvalueofthissubfield/g; | ||
| 571 | $$intaction{$kint}=~s/\\&/temporarycallfunction/g; | ||
| 572 | $currentaction.=$specaction.'('.$ftag.',{"'.$stag.'"=>"'.$$intaction{$kint}.'"});'."\n"; | ||
| 573 | if ($boolin) { $actionsin.=$currentaction; } else { $actionsout.=$currentaction; } | ||
| 574 | } | ||
| 575 | elsif( ref($$intaction{$kint}) eq "ARRAY" ) { | ||
| 576 | $currentaction=""; | ||
| 577 | $boolin=0; | ||
| 578 | my $ftag; | ||
| 579 | my $stag; | ||
| 580 | if($kint=~/^\$f(\d{3})(\w)$/) { | ||
| 581 | $boolin=1; | ||
| 582 | $ftag='$f'.$1; | ||
| 583 | $stag=$2; | ||
| 584 | } | ||
| 585 | elsif($kint=~/^\$i(\d{3})(\w)$/) { | ||
| 586 | $boolin=1; | ||
| 587 | $ftag='$f'.$1; | ||
| 588 | $stag='µ'; | ||
| 589 | $stag='µ' if($2 eq "1"); | ||
| 590 | $stag='£' if($2 eq "2"); | ||
| 591 | } | ||
| 592 | elsif($kint=~/^f(\d{3})(\w)$/) { | ||
| 593 | $ftag='"'.$1.'"'; | ||
| 594 | $stag=$2; | ||
| 595 | } | ||
| 596 | elsif($kint=~/^i(\d{3})(\w)$/) { | ||
| 597 | $ftag='"'.$1.'"'; | ||
| 598 | $stag='µ'; | ||
| 599 | $stag='µ' if($2 eq "1"); | ||
| 600 | $stag='£' if($2 eq "2"); | ||
| 601 | } | ||
| 602 | elsif($kint=~/^(\w)$/) { | ||
| 603 | $ftag='$$currentfield'; | ||
| 604 | $boolin=1; | ||
| 605 | $stag=$kint; | ||
| 606 | } | ||
| 607 | else { | ||
| 608 | push(@errors, 'Invalid yaml : your field reference is not valid in '.$type.' action.');#error | ||
| 609 | next; | ||
| 610 | } | ||
| 611 | $currentaction.=$specaction.'('.$ftag.',{"'.$stag.'"=>['; | ||
| 612 | foreach my $sintaction(@{$$intaction{$kint}}) { | ||
| 613 | $sintaction=~s/"/\\"/g; | ||
| 614 | $boolin=1 if($sintaction=~/\$f/); | ||
| 615 | $sintaction=~s/\$this/tempnameforcurrentvalueofthissubfield/g; | ||
| 616 | $sintaction=~s/\\&/temporarycallfunction/g; | ||
| 617 | $currentaction.='"'.$sintaction.'",'; | ||
| 618 | } | ||
| 619 | $currentaction.=']});'."\n"; | ||
| 620 | if ($boolin) { $actionsin.=$currentaction; } else { $actionsout.=$currentaction; } | ||
| 621 | } | ||
| 622 | } | ||
| 623 | } | ||
| 624 | else { | ||
| 625 | push(@errors, 'Invalid yaml : you try to use non hash context in '.$type.' action.');#error | ||
| 626 | } | ||
| 627 | } | ||
| 628 | elsif ($type eq "duplicatefield") { | ||
| 629 | if ( ref($intaction) eq "ARRAY" ) { | ||
| 630 | foreach my $vint (@$intaction) { | ||
| 631 | if( ref($vint) eq "" or ref($vint) eq "SCALAR" ) { | ||
| 632 | if($vint=~/^\$f(\d{3})\s?>\s?f(\d{3})$/) { | ||
| 633 | if ($1 < "010" and $2 < "010" ) { | ||
| 634 | $actionsin.='$record->insert_fields_ordered( MARC::Field->new( "'.$2.'", $f'.$1.'->data() ) );'; | ||
| 635 | } | ||
| 636 | elsif ($1 >= "010" and $2 >= "010" ) { | ||
| 637 | $actionsin.='my @dsubfields; foreach my $subfield ( $f'.$1.'->subfields() ) { push @dsubfields, ( $$subfield[0], $$subfield[1] );}'."\n"; | ||
| 638 | $actionsin.='$record->insert_fields_ordered( MARC::Field->new( "'.$2.'", $f'.$1.'->indicator(1), $f'.$1.'->indicator(2), @dsubfields ) );'; | ||
| 639 | } | ||
| 640 | else { | ||
| 641 | push(@errors, 'Invalid yaml : you want to duplicate a controlfield with a non-controlfield ');#error | ||
| 642 | } | ||
| 643 | } | ||
| 644 | elsif($vint=~/^f(\d{3})\s?>\s?f(\d{3})$/) { | ||
| 645 | if ($1 < "010" and $2 < "010" ) { | ||
| 646 | $actionsout.=' for my $fielddup($record->field("'.$1.'")){'; | ||
| 647 | $actionsout.='$record->insert_fields_ordered( MARC::Field->new( "'.$2.'", $fielddup->data() ) );'; | ||
| 648 | $actionsout.='}'."\n"; | ||
| 649 | } | ||
| 650 | elsif ($1 >= "010" and $2 >= "010" ) { | ||
| 651 | $actionsout.=' for my $fielddup($record->field("'.$1.'")){'; | ||
| 652 | $actionsout.='my @dsubfields; foreach my $subfield ( $fielddup->subfields() ) { push @dsubfields, ( $$subfield[0], $$subfield[1] );}'."\n"; | ||
| 653 | $actionsout.='$record->insert_fields_ordered( MARC::Field->new( "'.$2.'", $fielddup->indicator(1), $fielddup->indicator(2), @dsubfields ) );'; | ||
| 654 | $actionsout.='}'."\n"; | ||
| 655 | } | ||
| 656 | else { | ||
| 657 | push(@errors, 'Invalid yaml : you want to duplicate a controlfield with a non-controlfield ');#error | ||
| 658 | } | ||
| 659 | } | ||
| 660 | else { | ||
| 661 | push(@errors, 'Invalid yaml : your field reference is not valid in '.$type.' action.');#error | ||
| 662 | } | ||
| 663 | } | ||
| 664 | else { | ||
| 665 | push(@errors, 'Invalid yaml : you try to use non scalar value in '.$type.' action.');#error | ||
| 666 | } | ||
| 667 | } | ||
| 668 | } | ||
| 669 | elsif ( ref($intaction) eq "" or ref($intaction) eq "SCALAR" ) { | ||
| 670 | my $vint=$intaction; | ||
| 671 | if($vint=~/^\$f(\d{3})\s?>\s?f(\d{3})$/) { | ||
| 672 | if ($1 < "010" and $2 < "010" ) { | ||
| 673 | $actionsin.='$record->insert_fields_ordered( MARC::Field->new( "'.$2.'", $f'.$1.'->data() ) );'; | ||
| 674 | } | ||
| 675 | elsif ($1 >= "010" and $2 >= "010" ) { | ||
| 676 | $actionsin.='my @dsubfields; foreach my $subfield ( $f'.$1.'->subfields() ) { push @dsubfields, ( $$subfield[0], $$subfield[1] );}'."\n"; | ||
| 677 | $actionsin.='$record->insert_fields_ordered( MARC::Field->new( "'.$2.'", $f'.$1.'->indicator(1), $f'.$1.'->indicator(2), @dsubfields ) );'; | ||
| 678 | } | ||
| 679 | else { | ||
| 680 | push(@errors, 'Invalid yaml : you want to duplicate a controlfield with a non-controlfield ');#error | ||
| 681 | } | ||
| 682 | } | ||
| 683 | elsif($vint=~/^f(\d{3})\s?>\s?f(\d{3})$/) { | ||
| 684 | if ($1 < "010" and $2 < "010" ) { | ||
| 685 | $actionsout.=' for my $fielddup($record->field("'.$1.'")){'; | ||
| 686 | $actionsout.='$record->insert_fields_ordered( MARC::Field->new( "'.$2.'", $fielddup->data() ) );'; | ||
| 687 | $actionsout.='}'."\n"; | ||
| 688 | } | ||
| 689 | elsif ($1 >= "010" and $2 >= "010" ) { | ||
| 690 | $actionsout.=' for my $fielddup($record->field("'.$1.'")){'; | ||
| 691 | $actionsout.='my @dsubfields; foreach my $subfield ( $fielddup->subfields() ) { push @dsubfields, ( $$subfield[0], $$subfield[1] );}'."\n"; | ||
| 692 | $actionsout.='$record->insert_fields_ordered( MARC::Field->new( "'.$2.'", $fielddup->indicator(1), $fielddup->indicator(2), @dsubfields ) );'; | ||
| 693 | $actionsout.='}'."\n"; | ||
| 694 | } | ||
| 695 | else { | ||
| 696 | push(@errors, 'Invalid yaml : you want to duplicate a controlfield with a non-controlfield ');#error | ||
| 697 | } | ||
| 698 | } | ||
| 699 | else { | ||
| 700 | push(@errors, 'Invalid yaml : your field reference is not valid in '.$type.' action.');#error | ||
| 701 | } | ||
| 702 | } | ||
| 703 | else { | ||
| 704 | push(@errors, 'Invalid yaml : you try to use a hash value in '.$type.' action.');#error | ||
| 705 | } | ||
| 706 | } | ||
| 707 | elsif ($type eq "delete") { | ||
| 708 | if ( ref($intaction) eq "ARRAY" ) { | ||
| 709 | foreach my $vint (@$intaction) { | ||
| 710 | if( ref($vint) eq "" or ref($vint) eq "SCALAR" ) { | ||
| 711 | #print "$vint\n"; | ||
| 712 | if($vint=~/^\$f(\d{3})(\w)$/) { | ||
| 713 | $actionsin.=' if ( defined $f'.$1.'->subfield("'.$2.'") ) { if (scalar($f'.$1.'->subfields())==1) { $record->delete_field($f'.$1.'); } else { $f'.$1.'->delete_subfield(code => "'.$2.'"); } }'."\n"; | ||
| 714 | } | ||
| 715 | elsif($vint=~/^\$f(\d{3})$/) { | ||
| 716 | $actionsin.=' $record->delete_field('.$vint.');'."\n"; | ||
| 717 | } | ||
| 718 | elsif($vint=~/^f(\d{3})(\w)$/) { | ||
| 719 | $actionsout.=' for my $fieldel($record->field("'.$1.'")){if ( defined $fieldel->subfield("'.$2.'") ) { if (scalar($fieldel->subfields())==1) { $record->delete_field($fieldel); } else { $fieldel->delete_subfield(code => "'.$2.'"); } }}'."\n"; | ||
| 720 | } | ||
| 721 | elsif($vint=~/^f(\d{3})$/) { | ||
| 722 | $actionsout.=' $record->delete_fields($record->field("'.$1.'"));'."\n"; | ||
| 723 | } | ||
| 724 | elsif($vint=~/^(\w)$/) { | ||
| 725 | $actionsin.=' if ( defined $$currentfield->subfield("'.$vint.'") ) { if (scalar($$currentfield->subfields())==1) { $record->delete_field($$currentfield); } else { $$currentfield->delete_subfield(code => "'.$vint.'"); } }'."\n"; | ||
| 726 | } | ||
| 727 | else { | ||
| 728 | push(@errors, 'Invalid yaml : your field reference is not valid in '.$type.' action.');#error | ||
| 729 | } | ||
| 730 | } | ||
| 731 | else { | ||
| 732 | push(@errors, 'Invalid yaml : you try to use non scalar value in '.$type.' action.');#error | ||
| 733 | } | ||
| 734 | } | ||
| 735 | } | ||
| 736 | elsif ( ref($intaction) eq "" or ref($intaction) eq "SCALAR" ) { | ||
| 737 | my $vint=$intaction; | ||
| 738 | if($vint=~/^\$f(\d{3})(\w)$/) { | ||
| 739 | $actionsin.=' if ( defined $f'.$1.'->subfield("'.$2.'") ) { if (scalar($f'.$1.'->subfields())==1) { $record->delete_field($f'.$1.'); } else { $f'.$1.'->delete_subfield(code => "'.$2.'"); } }'."\n"; | ||
| 740 | } | ||
| 741 | elsif($vint=~/^\$f(\d{3})$/) { | ||
| 742 | $actionsin.=' $record->delete_field('.$vint.');'."\n"; | ||
| 743 | } | ||
| 744 | elsif($vint=~/^f(\d{3})(\w)$/) { | ||
| 745 | $actionsout.=' for my $fieldel($record->field("'.$1.'")){if ( defined $fieldel->subfield("'.$2.'") ) { if (scalar($fieldel->subfields())==1) { $record->delete_field($fieldel); } else { $fieldel->delete_subfield(code => "'.$2.'"); } }}'."\n"; | ||
| 746 | } | ||
| 747 | elsif($vint=~/^f(\d{3})$/) { | ||
| 748 | $actionsout.=' $record->delete_fields($record->field("'.$1.'"));'."\n"; | ||
| 749 | } | ||
| 750 | elsif($vint=~/^(\w)$/) { | ||
| 751 | $actionsin.=' if ( defined $$currentfield->subfield("'.$vint.'") ) { if (scalar($$currentfield->subfields())==1) { $record->delete_field($$currentfield); } else { $$currentfield->delete_subfield(code => "'.$vint.'"); } }'."\n"; | ||
| 752 | } | ||
| 753 | else { | ||
| 754 | push(@errors, 'Invalid yaml : your field reference is not valid in '.$type.' action.');#error | ||
| 755 | } | ||
| 756 | } | ||
| 757 | else { | ||
| 758 | push(@errors, 'Invalid yaml : you try to use a hash value in '.$type.' action.');#error | ||
| 759 | } | ||
| 760 | } | ||
| 761 | elsif ($type eq "execute") { | ||
| 762 | if( ref($intaction) eq "" or ref($intaction) eq "SCALAR" ) { | ||
| 763 | if($intaction=~/\$(f|i)(\d{3})/) { | ||
| 764 | $actionsin.=' eval ('.$intaction.');'; | ||
| 765 | } | ||
| 766 | else { | ||
| 767 | $actionsout.=' eval ('.$intaction.');'; | ||
| 768 | } | ||
| 769 | } | ||
| 770 | elsif( ref($intaction) eq "ARRAY" ) { | ||
| 771 | foreach my $sintaction(@$intaction) { | ||
| 772 | if($sintaction=~/\$(f|i)(\d{3})/) { | ||
| 773 | $actionsin.=' eval ('.$sintaction.');'; | ||
| 774 | } | ||
| 775 | else { | ||
| 776 | $actionsout.=' eval ('.$sintaction.');'; | ||
| 777 | } | ||
| 778 | } | ||
| 779 | } | ||
| 780 | else { | ||
| 781 | push(@errors, 'Invalid yaml : you try to use a hash value in '.$type.' action.');#error | ||
| 782 | } | ||
| 783 | } | ||
| 784 | else { | ||
| 785 | push(@errors, 'Invalid yaml : this action : '.$type.' is not valid.');#error | ||
| 786 | } | ||
| 787 | return ($actionsin,$actionsout); | ||
| 788 | } | ||
| 789 | |||
| 790 | sub testrule { | ||
| 791 | my ($rul, $actionsin, $actionsinter, $actionsout, $subs) = @_; | ||
| 792 | $globalcondition=""; | ||
| 793 | $subs="no warnings 'redefine';".$subs if $subs ne ""; | ||
| 794 | my $globalconditionstart='{ '."\n".$subs."\n".'my $boolcond=0;my $boolcondint=0;my $currentfield;no warnings \'substr\';no warnings \'uninitialized\';'."\n"; | ||
| 795 | my $globalconditionint=""; | ||
| 796 | my $globalconditionend="";#print Data::Dumper::Dumper ($rul); | ||
| 797 | if ( defnonull ( $$rul{'condition'} ) ) { | ||
| 798 | my @listconditiontags=grep( $_ , map({ $_=~/^(f|i)(\d{3})(.*)$/;$2 } (split(/\$/,$$rul{'condition'}))));#print $$rul{'condition'}; | ||
| 799 | my @listconditionsubtags=grep( $_ , map({ if($_=~/^(f)(\d{3}\w)(.*)$/){$2}elsif($_=~/^(i)(\d{3})(.*)$/){$2} } (split(/\$/,$$rul{'condition'})))); | ||
| 800 | my %tag_names = map( { $_ => 1 } @listconditiontags); | ||
| 801 | my %tag_list; | ||
| 802 | @listconditiontags = keys(%tag_names); | ||
| 803 | foreach my $tag(@listconditiontags) { | ||
| 804 | $tag_list{$tag}=[]; | ||
| 805 | foreach my $subtag(@listconditionsubtags) { | ||
| 806 | if (substr($subtag,0,3) eq $tag) { | ||
| 807 | if(length($subtag) == 3) { | ||
| 808 | push (@{$tag_list{$tag}}, "tempvalueforcurrentfield"); | ||
| 809 | } | ||
| 810 | else { | ||
| 811 | push (@{$tag_list{$tag}}, substr($subtag,3,1)); | ||
| 812 | } | ||
| 813 | } | ||
| 814 | } | ||
| 815 | } | ||
| 816 | my $condition=$$rul{'condition'}; | ||
| 817 | $condition=~s/(\$ldr(\d{1,2}))/\(substr\(\$record->leader\(\),$2,1)\)/g; | ||
| 818 | $condition=~s/(\$ldr)/(\$record->leader\(\)\)/g; | ||
| 819 | $condition=~s/(\$f\d{3})(\w)/defined($1$2) and $1$2/g; | ||
| 820 | $condition=~s/(\$f\d{3})(\w)(\d{1,2})/\(substr($1$2,$3,1\)\)/g; | ||
| 821 | $condition=~s/(\$f\d{3})(\w)/$1$2/g;#I can't remember why I did this | ||
| 822 | $condition=~s/(\$i(\d{3}))(\d)/\(\$f$2->indicator\($3\)\)/g; | ||
| 823 | my $booltagrule=0; | ||
| 824 | my $boolsubtagrule=0; | ||
| 825 | foreach my $tag (sort {$a cmp $b} keys(%tag_list)) { | ||
| 826 | my %tag_listtag = map { $_, 1 } @{$tag_list{$tag}}; | ||
| 827 | $boolsubtagrule=0; | ||
| 828 | my @tag_listtagunique = keys %tag_listtag; | ||
| 829 | $globalconditionstart.='my $f'.$tag.';'; | ||
| 830 | foreach my $subtag (@tag_listtagunique) { | ||
| 831 | my $matchdelaration='my \$f'.$tag.$subtag.';'; | ||
| 832 | $globalconditionstart.='my $f'.$tag.$subtag.';' if $globalconditionstart!~$matchdelaration; | ||
| 833 | } | ||
| 834 | if ( defined $record->field($tag) ) { | ||
| 835 | $booltagrule=1; | ||
| 836 | $globalconditionint.="\n".'for $f'.$tag.' ( $record->field("'.$tag.'") ) {'."\n".'$currentfield=\$f'.$tag.';'."\n"; | ||
| 837 | foreach my $subtag (@tag_listtagunique) { | ||
| 838 | $boolsubtagrule=1; | ||
| 839 | if ($subtag ne "tempvalueforcurrentfield" and $tag > "010") { | ||
| 840 | $globalconditionint.='for $f'.$tag.$subtag.' ( $f'.$tag.'->subfield("'.$subtag.'"), my $tmpintesta=1 ) { my $tmpintestb=0; if ($tmpintesta==1) { $tmpintesta=undef;$tmpintestb=1; }'."\n"; | ||
| 841 | $globalconditionint.='if ('.$condition.') {$boolcond=1;$boolcondint=1; eval{'.$actionsin.'}}else{$boolcondint=0 unless (!defined($tmpintesta) and $tmpintestb==0 );}'."\n"; | ||
| 842 | $globalconditionend.='}'."\n"; | ||
| 843 | } | ||
| 844 | elsif ($subtag ne "tempvalueforcurrentfield") { | ||
| 845 | $globalconditionint.='$f'.$tag.$subtag.' = $f'.$tag.'->data(); '."\n"; | ||
| 846 | $globalconditionint.='if ('.$condition.') {$boolcond=1;$boolcondint=1; eval{'.$actionsin.'}}else{$boolcondint=0;}'."\n"; | ||
| 847 | } | ||
| 848 | } | ||
| 849 | $globalconditionint.='if ('.$condition.')'."\n".'{$boolcond=1;$boolcondint=1; eval{'.$actionsin.'}}else{$boolcondint=0;}' unless $boolsubtagrule; | ||
| 850 | $globalconditionend.='if ($boolcondint){ eval{'.$actionsinter.'};}}'."\n"; | ||
| 851 | }#else { $globalconditionint.='if ('.$condition.') {$boolcond=1;$boolcondint=1; eval{'.$actionsin.'}}else{$boolcondint=0;}'."\n"; } | ||
| 852 | } | ||
| 853 | $globalconditionint.='if ('.$condition.')'."\n".'{$boolcond=1;$boolcondint=1; eval{'.$actionsin.'}}else{$boolcondint=0;}' unless $booltagrule; | ||
| 854 | $globalconditionend.="\n".' if ($boolcond){eval{'.$actionsout.'}}'."\n".' return $boolcond;}';#if ($boolcond or ('.$condition.')) | ||
| 855 | $globalcondition=$globalconditionstart.$globalconditionint.$globalconditionend; | ||
| 856 | print "\n--------globalcondition----------\n$globalcondition\n---------globalcondition---------\n" if $verbose; | ||
| 857 | return eval($globalcondition); | ||
| 858 | } | ||
| 859 | else { | ||
| 860 | print "\n--------actionsout----------\n$globalconditionstart$actionsout}\n---------actionsout---------\n" if $verbose; | ||
| 861 | eval($globalconditionstart.$actionsout.'}'); | ||
| 862 | return 1; | ||
| 863 | } | ||
| 864 | return 1; | ||
| 865 | } | ||
| 866 | |||
| 867 | sub ReplaceAllInRecord { | ||
| 868 | my ($pos) = @_; | ||
| 869 | return unless ( $record && $record->fields() ); | ||
| 870 | foreach my $field ( $record->fields() ) { | ||
| 871 | my @subfields; | ||
| 872 | if(!$field->is_control_field()) { | ||
| 873 | if (scalar($field->subfields()) > 0) { | ||
| 874 | foreach my $subfield ( $field->subfields() ) { | ||
| 875 | my $newval=$$subfield[1]; | ||
| 876 | if ($pos eq "before") { | ||
| 877 | #$newval=~s/\$/#_dollarsd_#/g;#to force warn | ||
| 878 | $newval=~s/\$/#_dollars_#/g; | ||
| 879 | $newval=~s/"/#_dbquote_#/g; | ||
| 880 | } | ||
| 881 | elsif ($pos eq "after") { | ||
| 882 | $newval=~s/#_dollars_#/\$/g; | ||
| 883 | $newval=~s/#_dbquote_#/"/g; | ||
| 884 | } | ||
| 885 | push @subfields, ( $$subfield[0], $newval ); | ||
| 886 | } | ||
| 887 | my $newfield = MARC::Field->new( $field->tag(), $field->indicator(1), $field->indicator(2), @subfields ); | ||
| 888 | $field->replace_with($newfield); | ||
| 889 | } | ||
| 890 | } | ||
| 891 | else { | ||
| 892 | my $newval=$field->data(); | ||
| 893 | if ($pos eq "before") { | ||
| 894 | $newval=~s/\$/#_dollars_#/g; | ||
| 895 | $newval=~s/"/#_dbquote_#/g; | ||
| 896 | } | ||
| 897 | elsif ($pos eq "after") { | ||
| 898 | $newval=~s/#_dollars_#/\$/g; | ||
| 899 | $newval=~s/#_dbquote_#/"/g; | ||
| 900 | } | ||
| 901 | $field->update($newval); | ||
| 902 | } | ||
| 903 | } | ||
| 904 | } | ||
| 905 | |||
| 906 | 1; | ||
| 907 | __END__ |