+=head2 save_into_lookup
+
+Save value into lookup. It associates current database, input
+and specific keys with one or more values which will be
+associated over MFN.
+
+MFN will be extracted from first occurence current of field 000
+in current record, or if it doesn't exist from L<_set_config> C<_mfn>.
+
+ my $nr = save_into_lookup($database,$input,$key,sub {
+ # code which produce one or more values
+ });
+
+It returns number of items saved.
+
+This function shouldn't be called directly, it's called from code created by
+L<WebPAC::Parser>.
+
+=cut
+
+sub save_into_lookup {
+ my ($database,$input,$key,$coderef) = @_;
+ die "save_into_lookup needs database" unless defined($database);
+ die "save_into_lookup needs input" unless defined($input);
+ die "save_into_lookup needs key" unless defined($key);
+ die "save_into_lookup needs CODE" unless ( defined($coderef) && ref($coderef) eq 'CODE' );
+
+ warn "## save_into_lookup rec = ", dump($rec), " config = ", dump($config), $/ if ($debug > 2);
+
+ my $mfn =
+ defined($rec->{'000'}->[0]) ? $rec->{'000'}->[0] :
+ defined($config->{_mfn}) ? $config->{_mfn} :
+ die "mfn not defined or zero";
+
+ my $nr = 0;
+
+ foreach my $v ( $coderef->() ) {
+ $lookup->{$database}->{$input}->{$key}->{$v}->{$mfn}++;
+ warn "# saved lookup $database/$input/$key [$v] $mfn\n" if ($debug > 1);
+ $nr++;
+ }
+
+ return $nr;
+}
+
+=head2 config
+
+Consult config values stored in C<config.yml>
+
+ # return database code (key under databases in yaml)
+ $database_code = config(); # use _ from hash
+ $database_name = config('name');
+ $database_input_name = config('input name');
+
+Up to three levels are supported.
+
+=cut
+
+sub config {
+ return unless ($config);
+
+ my $p = shift;
+
+ $p ||= '';
+
+ my $v;
+
+ warn "### getting config($p)\n" if ($debug > 1);
+
+ my @p = split(/\s+/,$p);
+ if ($#p < 0) {
+ $v = $config->{ '_' }; # special, database code
+ } else {
+
+ my $c = dclone( $config );
+
+ foreach my $k (@p) {
+ warn "### k: $k c = ",dump($c),$/ if ($debug > 1);
+ if (ref($c) eq 'ARRAY') {
+ $c = shift @$c;
+ warn "config($p) taking first occurence of '$k', probably not what you wanted!\n";
+ last;
+ }
+
+ if (! defined($c->{$k}) ) {
+ $c = undef;
+ last;
+ } else {
+ $c = $c->{$k};
+ }
+ }
+ $v = $c if ($c);
+
+ }
+
+ warn "## config( '$p' ) = ",dump( $v ),$/ if ($v && $debug);
+ warn "config( '$p' ) is empty\n" if (! $v);
+
+ return $v;
+}
+
+=head2 id
+
+Returns unique id of this record
+
+ $id = id();
+
+Returns C<42/2> for 2nd occurence of MFN 42.
+
+=cut
+
+sub id {
+ my $mfn = $config->{_mfn} || die "no _mfn in config data";
+ return $mfn . ( WebPAC::Normalize::MARC::_created_marc_records() || '' );
+}
+