+ $department_in_sum->{ $row->{department} }++;
+ foreach my $name ( keys %$department_groups ) {
+ my $regex = $department_groups->{$name};
+ if ( $row->{department} =~ $regex ) {
+ $department_in_group->{ $row->{department} } = $name;
+ last;
+ }
+ }
+}
+
+debug 'department_in_group' => $department_in_group;
+
+foreach my $department ( keys %$department_in_sum ) {
+ $department_in_sum->{$department} = 0 unless $department =~ m/(centar|croaticum|katedra|odsjek)/i;
+}
+
+debug 'auth_department' => $auth_department;
+debug 'auth_group' => $auth_group;
+debug 'department_in_sum' => $department_in_sum;
+
+
+my $authors;
+my $marcxml;
+
+my $sth_select_authors = $dbh->prepare(q{
+select
+ biblionumber,
+ itemtype,
+ marcxml
+from biblioitems
+where
+ agerestriction > 0
+});
+
+=for sql
+-- ExtractValue(marcxml,'//datafield[@tag="100"]/subfield[@code="9"]') as first_author,
+-- ExtractValue(marcxml,'//datafield[@tag="700"]/subfield[@code="9"]') as other_authors,
+-- ExtractValue(marcxml,'//datafield[@tag="942"]/subfield[@code="t"]') as category,
+
+-- and SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) between 2008 and 2013
+-- order by SUBSTR(ExtractValue(marcxml,'//controlfield[@tag="008"]'),8,4) desc
+=cut
+
+my $biblio_year;
+my $type_stats;
+
+my $parser = XML::LibXML->new();
+$parser->recover_silently(0); # don't die when you find &, >, etc
+my $style_doc = $parser->parse_file($xslfilename);
+my $xslt = XML::LibXSLT->new();
+my $parsed = $xslt->parse_stylesheet($style_doc);
+
+my $biblio_html;
+my $biblio_parsed;
+my $biblio_data;
+my $biblio_author_external;
+
+open(my $xml_fh, '>', '/tmp/bibliografija.xml') if $ENV{XML};
+
+sub biblioitem_html {
+ my ($biblionumber, $parse_only) = @_;
+
+ return $biblio_html->{$biblionumber} if exists $biblio_html->{$biblionumber} && ! $parse_only;
+
+ my $xmlrecord = $marcxml->{$biblionumber} || confess "missing $biblionumber marcxml";
+
+ print $xml_fh $xmlrecord if $ENV{XML};
+
+ my $source = eval { $parser->parse_string($xmlrecord) };
+ if ( $@ ) {
+# warn "SKIP $biblionumber corrupt XML";
+ push @{ $skip->{XML_corrupt} }, $biblionumber;
+ return;
+ }
+
+ if ( $parse_only ) {
+ $biblio_parsed->{$biblionumber} = $source;
+ return $source;
+ }
+
+ my $transformed = $parsed->transform($source);
+ $biblio_html->{$biblionumber} = $parsed->output_string( $transformed );
+
+ delete $biblio_parsed->{$biblionumber};
+
+ return $biblio_html->{$biblionumber};
+}
+
+$sth_select_authors->execute();
+while( my $row = $sth_select_authors->fetchrow_hashref ) {
+# warn dump($row),$/;
+
+ my $biblio;
+
+ $marcxml->{ $row->{biblionumber} } = $row->{marcxml};
+
+ my $doc = biblioitem_html( $row->{biblionumber}, 1 );
+ if ( ! $doc ) {
+# warn "ERROR can't parse MARCXML ", $row->{biblionumber}, " ", $row->{marcxml}, "\n";
+ next;
+ }
+
+ my $root = $doc->documentElement;
+=for leader
+ my @leaders = $root->getElementsByLocalName('leader');
+ if (@leaders) {
+ my $leader = $leaders[0]->textContent;
+ warn "leader $leader\n";
+ }
+=cut
+
+ my $extract = {
+ '008' => undef,
+ '100' => '(9|a)',
+ '680' => 'i',
+ '700' => '(9|4|a)',
+ '942' => '(t|r|v)'
+ };
+
+ my $data;
+
+ foreach my $elt ($root->getChildrenByLocalName('*')) {
+ my $tag = $elt->getAttribute('tag');
+ next if ! $tag;
+ next unless exists $extract->{ $tag };
+
+ if ($elt->localname eq 'controlfield') {
+ if ( $tag eq '008' ) {
+ my $content = $elt->textContent;
+ my $year = substr($content, 7, 4 );
+ if ( $year !~ m/^\d+$/ ) {
+ $year = 0;
+ push @{ $skip->{invalid_year} }, $row->{biblionumber};
+ }
+ $biblio_year->{ $row->{biblionumber} } = $data->{year} = $year;
+ $data->{'008'} = $content;
+ }
+ next;
+ } elsif ($elt->localname eq 'datafield') {
+ my $sf_data;
+ foreach my $sfelt ($elt->getChildrenByLocalName('subfield')) {
+ my $sf = $sfelt->getAttribute('code');
+ next unless $sf =~ m/$extract->{$tag}/;
+ if ( exists $sf_data->{$sf} ) {
+ $sf_data->{$sf} .= " " . $sfelt->textContent();
+ } else {
+ $sf_data->{$sf} = $sfelt->textContent();
+ }
+ }
+ push @{ $data->{$tag} }, $sf_data if $sf_data;
+ }
+ }
+
+ if ( ! defined $data->{year} ) {
+ warn "MISSING year in ", $row->{biblionumber};
+ } elsif ( $data->{year} < 2008 ) {
+ push @{ $skip->{year_lt_2008} }, $row->{biblionumber};
+ next;
+ } elsif ( $data->{year} > 2013 ) {
+ push @{ $skip->{year_gt_2013} }, $row->{biblionumber};
+ next;
+ }
+
+# warn "# ", $row->{biblionumber}, " data ",dump($data);
+
+ my $category = $data->{942}->[0]->{'t'};
+ if ( ! $category ) {
+# warn "# SKIP ", $row->{biblionumber}, " no category in ", dump($data);
+ push @{ $skip->{no_category} }, $row->{biblionumber};
+ next;
+ }
+
+ my $have_100 = 1;
+
+ if ( exists $data->{100} ) {
+ my @first_author =
+ map { $_->{'9'} }
+ grep {
+ if ( ! exists $_->{9} ) {
+ $biblio_author_external->{ $row->{biblionumber} }++;
+ 0;
+ } elsif ( exists $auth_header->{ $_->{9} } ) {
+ 1; # from FFZXG
+ } else {
+ 0;
+ }
+ }
+ @{ $data->{100} };
+ foreach my $authid ( @first_author ) {
+ push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
+ }
+ } else {
+ $have_100 = 0;
+ }
+
+ my $have_edt;
+
+ if ( exists $data->{700} ) {
+ my @other_authors =
+ grep {
+ if ( ! exists $_->{9} ) {
+ $biblio_author_external->{ $row->{biblionumber} }++;
+ 0;
+ } elsif ( exists $auth_header->{ $_->{9} } ) {
+ 1; # from FFZXG
+ } else {
+ 0;
+ }
+ }
+ @{ $data->{700} };
+ foreach my $auth ( @other_authors ) {
+ my $authid = $auth->{9} || next;
+ my $type = $auth->{4} || next; #die "no 4 in ",dump($data);
+
+ $type_stats->{$type}++;
+
+ if ( $type =~ m/(edt|trl|com|ctb)/ ) {
+ push @{ $authors->{$authid}->{sec}->{ $category } }, $row->{biblionumber};
+ push @{ $authors->{$authid}->{$1}->{ $category } }, $row->{biblionumber};
+ } elsif ( $type =~ m/aut/ ) {
+ if ( ! $have_100 ) {
+ $have_edt = grep { exists $_->{4} && $_->{4} =~ m/edt/ } @{ $data->{700} } if ! defined $have_edt;
+ if ( $have_edt ) {
+ $skip->{ have_700_edt }->{ $row->{biblionumber} }++;
+ } else {
+ push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
+ }
+ } else {
+ push @{ $authors->{$authid}->{aut}->{ $category } }, $row->{biblionumber};
+ }
+ } else {
+# warn "# SKIP ", $row->{biblionumber}, ' no 700$4 in ', dump($data);
+ $skip->{ 'no_700$4' }->{ $row->{biblionumber} }++;
+ }
+ }
+ delete $data->{700};
+ }
+
+ $biblio_data->{ $row->{biblionumber} } = $data;
+
+}
+
+debug 'authors' => $authors;
+debug 'type_stats' => $type_stats;
+debug 'skip' => $skip;
+debug 'biblio_year' => $biblio_year;
+debug 'biblio_data' => $biblio_data;
+debug 'biblio_author_external' => $biblio_author_external;
+
+my $category_label;
+my $sth_categories = $dbh->prepare(q{
+select authorised_value, lib from authorised_values where category = 'BIBCAT'
+});
+$sth_categories->execute();
+while( my $row = $sth_categories->fetchrow_hashref ) {
+ $category_label->{ $row->{authorised_value} } = $row->{lib};