Bug 9282: improve auto-completion for authority record finder
authorFridolyn SOMERS <fridolyn.somers@biblibre.com>
Thu, 13 Dec 2012 13:38:11 +0000 (14:38 +0100)
committerGalen Charlton <gmc@esilibrary.com>
Thu, 10 Oct 2013 22:23:09 +0000 (22:23 +0000)
This patch adjusts the auto-completion on the authority record
finder (accessed from the bib editor) so that if you do
start typing in the "Main entry ($a only)" input field, it will
return only the $a of the main heading for matching authority
records.

This fixes a problem where typing "shakes", choosing
"Shakespeare, William, 1564-1616" from the auto-completion
result list, then hitting the search button fails to bring
up results, as the dates come from the $d of the 100 field
(in MARC21).

Signed-off-by: Mathieu Saby <mathieu.saby@univ-rennes2.fr>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Works as advertised.
Tested with an authority where I added my search term in $b.
The modified authority came up in main entry, not in mainmainentry.
That was the desired result.

Signed-off-by: Galen Charlton <gmc@esilibrary.com>
C4/AuthoritiesMarc.pm
authorities/ysearch.pl

index 010fad7..f1b4935 100644 (file)
@@ -953,7 +953,11 @@ sub BuildSummary {
 # construct UNIMARC summary, that is quite different from MARC21 one
 # accepted form
         foreach my $field ($record->field('2..')) {
-            push @authorized, { heading => $field->as_string('abcdefghijlmnopqrstuvwxyz'), field => $field->tag() };
+            push @authorized, {
+                heading => $field->as_string('abcdefghijlmnopqrstuvwxyz'),
+                hemain  => $field->subfield('a'),
+                field   => $field->tag(),
+            };
         }
 # rejected form(s)
         foreach my $field ($record->field('3..')) {
@@ -961,7 +965,12 @@ sub BuildSummary {
         }
         foreach my $field ($record->field('4..')) {
             my $thesaurus = $field->subfield('2') ? "thes. : ".$thesaurus{"$field->subfield('2')"}." : " : '';
-            push @seefrom, { heading => $thesaurus . $field->as_string('abcdefghijlmnopqrstuvwxyz'), type => 'seefrom', field => $field->tag() };
+            push @seefrom, {
+                heading => $thesaurus . $field->as_string('abcdefghijlmnopqrstuvwxyz'),
+                hemain  => $field->subfield('a'),
+                type    => 'seefrom',
+                field   => $field->tag(),
+            };
         }
 
         # see :
@@ -972,6 +981,7 @@ sub BuildSummary {
                 field   => $_->tag,
                 type    => $type,
                 heading => $heading,
+                hemain  => $_->subfield('a'),
                 search  => $heading,
                 authid  => $_->subfield('9'),
             }
@@ -1022,9 +1032,17 @@ sub BuildSummary {
                 $subfields_to_report = 'vxyz';
             }
             if ($subfields_to_report) {
-                push @authorized, { heading => $field->as_string($subfields_to_report), field => $tag };
+                push @authorized, {
+                    heading => $field->as_string($subfields_to_report),
+                    hemain  => $field->subfield( substr($subfields_to_report, 0, 1) ),
+                    field   => $tag,
+                };
             } else {
-                push @authorized, { heading => $field->as_string(), field => $tag };
+                push @authorized, {
+                    heading => $field->as_string(),
+                    hemain  => $field->subfield('a'),
+                    field   => $tag,
+                };
             }
         }
         foreach my $field ($record->field('4..')) { #See From
@@ -1035,9 +1053,19 @@ sub BuildSummary {
                 $type = 'earlier' if $type && $type ne 'n';
             }
             if ($type eq 'subfi') {
-                push @seefrom, { heading => $field->as_string($marc21subfields), type => ($field->subfield('i') || ''), field => $field->tag() };
+                push @seefrom, {
+                    heading => $field->as_string($marc21subfields),
+                    hemain  => $field->subfield( substr($marc21subfields, 0, 1) ),
+                    type    => ($field->subfield('i') || ''),
+                    field   => $field->tag(),
+                };
             } else {
-                push @seefrom, { heading => $field->as_string($marc21subfields), type => $type, field => $field->tag() };
+                push @seefrom, {
+                    heading => $field->as_string($marc21subfields),
+                    hemain  => $field->subfield( substr($marc21subfields, 0, 1) ),
+                    type    => $type,
+                    field   => $field->tag(),
+                };
             }
         }
         foreach my $field ($record->field('5..')) { #See Also
@@ -1050,18 +1078,20 @@ sub BuildSummary {
             if ($type eq 'subfi') {
                 push @seealso, {
                     heading => $field->as_string($marc21subfields),
-                    type => $field->subfield('i'),
-                    field => $field->tag(),
-                    search => $field->as_string($marc21subfields) || '',
-                    authid => $field->subfield('9') || ''
+                    hemain  => $field->subfield( substr($marc21subfields, 0, 1) ),
+                    type    => $field->subfield('i'),
+                    field   => $field->tag(),
+                    search  => $field->as_string($marc21subfields) || '',
+                    authid  => $field->subfield('9') || ''
                 };
             } else {
                 push @seealso, {
                     heading => $field->as_string($marc21subfields),
-                    type => $type,
-                    field => $field->tag(),
-                    search => $field->as_string($marc21subfields) || '',
-                    authid => $field->subfield('9') || ''
+                    hemain  => $field->subfield( substr($marc21subfields, 0, 1) ),
+                    type    => $type,
+                    field   => $field->tag(),
+                    search  => $field->as_string($marc21subfields) || '',
+                    authid  => $field->subfield('9') || ''
                 };
             }
         }
@@ -1089,6 +1119,7 @@ sub BuildSummary {
         }
     }
     $summary{mainentry} = $authorized[0]->{heading};
+    $summary{mainmainentry} = $authorized[0]->{hemain};
     $summary{authorized} = \@authorized;
     $summary{notes} = \@notes;
     $summary{seefrom} = \@seefrom;
index 6584123..7996c6d 100755 (executable)
@@ -29,18 +29,22 @@ This script allows ajax call for dynamic authorities search
 
 use CGI;
 use Modern::Perl;
+use JSON;
+
 use C4::Context;
 use C4::Charset;
 use C4::AuthoritiesMarc;
 use C4::Auth qw/check_cookie_auth/;
+use C4::Output;
 
 my $query = new CGI;
 
-binmode STDOUT, ':encoding(UTF-8)';
-print $query->header( -type => 'text/plain', -charset => 'UTF-8' );
+my ( $auth_status, $sessionID ) = check_cookie_auth( $query->cookie('CGISESSID'), { catalogue => 1 } );
 
-my ( $auth_status, $sessionID ) = check_cookie_auth( $query->cookie('CGISESSID'), { } );
 if ( $auth_status ne "ok" ) {
+    # send empty response
+    my $reply = CGI->new("");
+    print $reply->header(-type => 'text/html');
     exit 0;
 }
 
@@ -64,17 +68,26 @@ if ( $auth_status ne "ok" ) {
 
     my ( $results, $total ) = SearchAuthorities( \@marclist, \@and_or, \@excluding, \@operator, \@value, $startfrom * $resultsperpage, $resultsperpage, $authtypecode, $orderby );
 
-print "[";
-my $i = 0;
+    my %used_summaries; # hash to avoid duplicates
+    my @summaries;
     foreach my $result (@$results) {
-        if($i > 0){ print ","; }
-        my $value = '';
         my $authorized = $result->{'summary'}->{'authorized'};
-        foreach my $heading (@$authorized) {
-            $value .= $heading->{'heading'} . ' ';
+        my $summary    = join(
+            ' ',
+            map {
+                ( $searchtype eq 'mainmainentry' )
+                  ? $_->{'hemain'}
+                  : $_->{'heading'}
+              } @$authorized
+        );
+        $summary =~ s/^\s+//;
+        $summary =~ s/\s+$//;
+        $summary = nsb_clean($summary);
+        # test if already added ignoring case
+        unless ( exists $used_summaries{ lc($summary) } ) {
+            push @summaries, { 'summary' => $summary };
+            $used_summaries{ lc($summary) } = 1;
         }
-        $value = "{\"summary\":\"" . $value . "\"" . "}";
-        print nsb_clean($value) . "\n";
-        $i++;
     }
-print "]";
+
+output_with_http_headers $query, undef, to_json(\@summaries, { utf8 => 1 }), 'json';