Bug 3216: UNIMARC author facets
authorFrédéric Demians <f.demians@tamil.fr>
Fri, 2 Dec 2011 06:56:27 +0000 (07:56 +0100)
committerPaul Poulain <paul.poulain@biblibre.com>
Tue, 13 Mar 2012 15:35:02 +0000 (16:35 +0100)
Add 700$b to UNIMARC author facets.

Other facets subfields could be added now. For example, other subjects
subfields.

Following patches are required to handle better MARC21 subfields and choose
other subfields to deal with UNIMARC format.

Signed-off-by: Jared Camins-Esakov <jcamins@cpbibliography.com>
Tested under both MARC21 and UNIMARC. Does not cause any regressions with
MARC21, and offers the possibility for better faceting there in the future.
Works as advertised with UNIMARC.

Signed-off-by: Jared Camins-Esakov <jcamins@cpbibliography.com>
Signed-off-by: Paul Poulain <paul.poulain@biblibre.com>

C4/Koha.pm
C4/Search.pm

index 43bfdf5..0192989 100644 (file)
@@ -678,95 +678,89 @@ sub getFacets {
     if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) {
         $facets = [
             {
-                link_value  => 'su-to',
-                label_value => 'Topics',
-                tags        =>
-                  [ '600', '601', '602', '603', '604', '605', '606', '610' ],
-                subfield => 'a',
+                idx   => 'su-to',
+                label => 'Topics',
+                tags  => [ qw/ 600a 601a 602a 603a 604a 605a 606ax 610a/ ],
+                sep   => ' - ',
             },
             {
-                link_value  => 'su-geo',
-                label_value => 'Places',
-                tags        => ['651'],
-                subfield    => 'a',
+                idx   => 'su-geo',
+                label => 'Places',
+                tags  => [ qw/ 651a / ],
+                sep   => ' - ',
             },
             {
-                link_value  => 'su-ut',
-                label_value => 'Titles',
-                tags        => [ '500', '501', '502', '503', '504', ],
-                subfield    => 'a',
+                idx   => 'su-ut',
+                label => 'Titles',
+                tags  => [ qw/ 500a 501a 502a 503a 504a / ],
+                sep   => ', ',
             },
             {
-                link_value  => 'au',
-                label_value => 'Authors',
-                tags        => [ '700', '701', '702', ],
-                subfield    => 'a',
+                idx   => 'au',
+                label => 'Authors',
+                tags  => [ qw/ 700ab 701ab 702ab / ],
+                sep   => ', ',
             },
             {
-                link_value  => 'se',
-                label_value => 'Series',
-                tags        => ['225'],
-                subfield    => 'a',
+                idx   => 'se',
+                label => 'Series',
+                tags  => [ qw/ 225a / ],
+                sep   => ', ',
             },
-            ];
-
-            my $library_facet;
-
-            $library_facet = {
-                link_value  => 'branch',
-                label_value => 'Libraries',
-                tags        => [ '995', ],
-                subfield    => 'b',
-                expanded    => '1',
-            };
-            push @$facets, $library_facet unless C4::Context->preference("singleBranchMode");
+        ];
+        my $library_facet = {
+            idx   => 'branch',
+            label => 'Libraries',
+            tags  => [ qw/ 995b / ],
+            expanded => '1',
+        };
+        push @$facets, $library_facet unless C4::Context->preference("singleBranchMode");
     }
     else {
         $facets = [
             {
-                link_value  => 'su-to',
-                label_value => 'Topics',
-                tags        => ['650'],
-                subfield    => 'a',
+                idx   => 'su-to',
+                label => 'Topics',
+                tags  => [ qw/ 650a / ],
+                sep   => '--',
             },
-
             #        {
-            #        link_value => 'su-na',
-            #        label_value => 'People and Organizations',
-            #        tags => ['600', '610', '611'],
-            #        subfield => 'a',
+            #        idx   => 'su-na',
+            #        label => 'People and Organizations',
+            #        tags  => [ qw/ 600a 610a 611a / ],
+            #        sep   => 'a',
             #        },
             {
-                link_value  => 'su-geo',
-                label_value => 'Places',
-                tags        => ['651'],
-                subfield    => 'a',
+                idx   => 'su-geo',
+                label => 'Places',
+                tags  => [ qw/ 651a / ],
+                sep   => '--',
             },
             {
-                link_value  => 'su-ut',
-                label_value => 'Titles',
-                tags        => ['630'],
-                subfield    => 'a',
+                idx   => 'su-ut',
+                label => 'Titles',
+                tags  => [ qw/ 630a / ],
+                sep   => '--',
             },
             {
-                link_value  => 'au',
-                label_value => 'Authors',
-                tags        => [ '100', '110', '700', ],
-                subfield    => 'a',
+                idx   => 'au',
+                label => 'Authors',
+                tags  => [ qw/ 100a 110a 700a / ],
+                sep   => ', ',
             },
             {
-                link_value  => 'se',
-                label_value => 'Series',
-                tags        => [ '440', '490', ],
-                subfield    => 'a',
+                idx   => 'se',
+                label => 'Series',
+                tags  => [ qw/ 440a 490a / ],
+                sep   => ', ',
             },
             ];
             my $library_facet;
             $library_facet = {
-                link_value  => 'branch',
-                label_value => 'Libraries',
-                tags        => [ '952', ],
-                subfield    => 'b',
+                idx   => 'branch',
+                label => 'Libraries',
+                tags  => [ qw/ 952b / ],
+                sep   => ', ',
                 expanded    => '1',
             };
             push @$facets, $library_facet unless C4::Context->preference("singleBranchMode");
index 04ce112..05a860c 100644 (file)
@@ -469,46 +469,39 @@ sub getRecords {
                 if ( !$scan && $servers[ $i - 1 ] =~ /biblioserver/ ) {
 
                     my $jmax = $size>$facets_maxrecs? $facets_maxrecs: $size;
-
-                    for ( my $k = 0 ; $k <= @$facets ; $k++ ) {
-                        ($facets->[$k]) or next;
-                        my @fcodes = @{$facets->[$k]->{'tags'}};
-                        my $sfcode = $facets->[$k]->{'subfield'};
-
+                    for my $facet ( @$facets ) {
                                for ( my $j = 0 ; $j < $jmax ; $j++ ) {
                                    my $render_record = $results[ $i - 1 ]->record($j)->render();
                             my @used_datas = ();
-
-                            foreach my $fcode (@fcodes) {
-
+                            foreach my $tag ( @{$facet->{tags}} ) {
                                 # avoid first line
-                                my $field_pattern = '\n'.$fcode.' ([^\n]+)';
+                                my $tag_num = substr($tag, 0, 3);
+                                my $letters = substr($tag, 3);
+                                my $field_pattern = '\n' . $tag_num . ' ([^\n]+)';
                                 my @field_tokens = ( $render_record =~ /$field_pattern/g ) ;
-
                                 foreach my $field_token (@field_tokens) {
-                                    my $subfield_pattern = '\$'.$sfcode.' ([^\$]+)';
-                                    my @subfield_values = ( $field_token =~ /$subfield_pattern/g );
-
-                                    foreach my $subfield_value (@subfield_values) {
-
-                                        my $data = $subfield_value;
-                                        $data =~ s/^\s+//; # trim left
-                                        $data =~ s/\s+$//; # trim right
-
-                                        unless ( $data ~~ @used_datas ) {
-                                            $facets_counter->{ $facets->[$k]->{'link_value'} }->{$data}++;
-                                            push @used_datas, $data;
+                                    my @subf = ( $field_token =~ /\$([a-zA-Z0-9]) ([^\$]+)/g );
+                                    my @values;
+                                    for (my $i = 0; $i < @subf; $i += 2) {
+                                        if ( $letters =~ $subf[$i] ) {
+                                             my $value = $subf[$i+1];
+                                             $value =~ s/^ *//;
+                                             $value =~ s/ *$//;
+                                             push @values, $value;
                                         }
-                                    } # subfields
+                                    }
+                                    my $data = join($facet->{sep}, @values);
+                                    unless ( $data ~~ @used_datas ) {
+                                        $facets_counter->{ $facet->{idx} }->{$data}++;
+                                        push @used_datas, $data;
+                                    }
                                 } # fields
                             } # field codes
                         } # records
-
-                        $facets_info->{ $facets->[$k]->{'link_value'} }->{'label_value'} = $facets->[$k]->{'label_value'};
-                        $facets_info->{ $facets->[$k]->{'link_value'} }->{'expanded'} = $facets->[$k]->{'expanded'};
+                        $facets_info->{ $facet->{idx} }->{label_value} = $facet->{label};
+                        $facets_info->{ $facet->{idx} }->{expanded} = $facet->{expanded};
                     } # facets
                 }
-                # End PROGILONE
             }
 
             # warn "connection ", $i-1, ": $size hits";