<a href="http://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=9149">Bug 9149</a> filed for tracking this.<div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Nov 26, 2012 at 2:46 PM, Doug Kingston <span dir="ltr"><<a href="mailto:dpk@randomnotes.org" target="_blank">dpk@randomnotes.org</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This patch appears to break koha 3.8.7.  The routine GetAuthorizedHeading does not exist anywhere in the source.<div>This breaks <a href="http://link_bibs_to_authorities.pl" target="_blank">link_bibs_to_authorities.pl</a> and possibly other things.</div>

<span class="HOEnZb"><font color="#888888">
<div><br></div><div>-Doug-</div></font></span><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Sep 26, 2012 at 2:39 PM, Jared Camins-Esakov <span dir="ltr"><<a href="mailto:jcamins@cpbibliography.com" target="_blank">jcamins@cpbibliography.com</a>></span> wrote:<br>


<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 3.8.x, it was possible for multiple automatically generated<br>
authorities to be linked to a single heading. This patch deletes<br>
previous links from headings prior to linking them to<br>
automatically-generated headings. This patch also corrects a<br>
potential problem wherein multiple authorities might be generated if<br>
a record is edited repeatedly in quick succession. The latter problem<br>
exists on Master and 3.6.x as well, and the code that corrects the<br>
multiple linkages is equally applicable if seemingly unnecessary.<br>
---<br>
 C4/Biblio.pm |  124 +++++++++++++++++++++++++++++++++++++--------------------<br>
 1 files changed, 80 insertions(+), 44 deletions(-)<br>
<br>
diff --git a/C4/Biblio.pm b/C4/Biblio.pm<br>
index ed8a169..9de9b85 100644<br>
--- a/C4/Biblio.pm<br>
+++ b/C4/Biblio.pm<br>
@@ -568,21 +568,27 @@ sub LinkBibHeadingsToAuthorities {<br>
                 $results{'fuzzy'}->{ $heading->display_form() }++;<br>
             }<br>
             elsif ( C4::Context->preference('AutoCreateAuthorities') ) {<br>
-                my $authtypedata =<br>
-                  C4::AuthoritiesMarc::GetAuthType( $heading->auth_type() );<br>
-                my $marcrecordauth = MARC::Record->new();<br>
-                if ( C4::Context->preference('marcflavour') eq 'MARC21' ) {<br>
-                    $marcrecordauth->leader('     nz  a22     o  4500');<br>
-                    SetMarcUnicodeFlag( $marcrecordauth, 'MARC21' );<br>
+                if ( _check_valid_auth_link( $current_link, $field ) ) {<br>
+                    $results{'linked'}->{ $heading->display_form() }++;<br>
                 }<br>
-                my $authfield =<br>
-                  MARC::Field->new( $authtypedata->{auth_tag_to_report},<br>
-                    '', '', "a" => "" . $field->subfield('a') );<br>
-                map {<br>
-                    $authfield->add_subfields( $_->[0] => $_->[1] )<br>
-                      if ( $_->[0] =~ /[A-z]/ && $_->[0] ne "a" )<br>
-                } $field->subfields();<br>
-                $marcrecordauth->insert_fields_ordered($authfield);<br>
+                else {<br>
+                    my $authtypedata =<br>
+                      C4::AuthoritiesMarc::GetAuthType( $heading->auth_type() );<br>
+                    my $marcrecordauth = MARC::Record->new();<br>
+                    if ( C4::Context->preference('marcflavour') eq 'MARC21' ) {<br>
+                        $marcrecordauth->leader('     nz  a22     o  4500');<br>
+                        SetMarcUnicodeFlag( $marcrecordauth, 'MARC21' );<br>
+                    }<br>
+                    $field->delete_subfield( code => '9' )<br>
+                      if defined $current_link;<br>
+                    my $authfield =<br>
+                      MARC::Field->new( $authtypedata->{auth_tag_to_report},<br>
+                        '', '', "a" => "" . $field->subfield('a') );<br>
+                    map {<br>
+                        $authfield->add_subfields( $_->[0] => $_->[1] )<br>
+                          if ( $_->[0] =~ /[A-z]/ && $_->[0] ne "a" )<br>
+                    } $field->subfields();<br>
+                    $marcrecordauth->insert_fields_ordered($authfield);<br>
<br>
 # bug 2317: ensure new authority knows it's using UTF-8; currently<br>
 # only need to do this for MARC21, as MARC::Record->as_xml_record() handles<br>
@@ -591,41 +597,47 @@ sub LinkBibHeadingsToAuthorities {<br>
 # use UTF-8, but as of 2008-08-05, did not want to introduce that kind<br>
 # of change to a core API just before the 3.0 release.<br>
<br>
-                if ( C4::Context->preference('marcflavour') eq 'MARC21' ) {<br>
-                    $marcrecordauth->insert_fields_ordered(<br>
-                        MARC::Field->new(<br>
-                            '667', '', '',<br>
-                            'a' => "Machine generated authority record."<br>
-                        )<br>
-                    );<br>
-                    my $cite =<br>
-                        $bib->author() . ", "<br>
-                      . $bib->title_proper() . ", "<br>
-                      . $bib->publication_date() . " ";<br>
-                    $cite =~ s/^[\s\,]*//;<br>
-                    $cite =~ s/[\s\,]*$//;<br>
-                    $cite =<br>
-                        "Work cat.: ("<br>
-                      . C4::Context->preference('MARCOrgCode') . ")"<br>
-                      . $bib->subfield( '999', 'c' ) . ": "<br>
-                      . $cite;<br>
-                    $marcrecordauth->insert_fields_ordered(<br>
-                        MARC::Field->new( '670', '', '', 'a' => $cite ) );<br>
-                }<br>
+                    if ( C4::Context->preference('marcflavour') eq 'MARC21' ) {<br>
+                        $marcrecordauth->insert_fields_ordered(<br>
+                            MARC::Field->new(<br>
+                                '667', '', '',<br>
+                                'a' => "Machine generated authority record."<br>
+                            )<br>
+                        );<br>
+                        my $cite =<br>
+                            $bib->author() . ", "<br>
+                          . $bib->title_proper() . ", "<br>
+                          . $bib->publication_date() . " ";<br>
+                        $cite =~ s/^[\s\,]*//;<br>
+                        $cite =~ s/[\s\,]*$//;<br>
+                        $cite =<br>
+                            "Work cat.: ("<br>
+                          . C4::Context->preference('MARCOrgCode') . ")"<br>
+                          . $bib->subfield( '999', 'c' ) . ": "<br>
+                          . $cite;<br>
+                        $marcrecordauth->insert_fields_ordered(<br>
+                            MARC::Field->new( '670', '', '', 'a' => $cite ) );<br>
+                    }<br>
<br>
            #          warn "AUTH RECORD ADDED : ".$marcrecordauth->as_formatted;<br>
<br>
-                $authid =<br>
-                  C4::AuthoritiesMarc::AddAuthority( $marcrecordauth, '',<br>
-                    $heading->auth_type() );<br>
-                $field->add_subfields( '9', $authid );<br>
-                $num_headings_changed++;<br>
-                $results{'added'}->{ $heading->display_form() }++;<br>
+                    $authid =<br>
+                      C4::AuthoritiesMarc::AddAuthority( $marcrecordauth, '',<br>
+                        $heading->auth_type() );<br>
+                    $field->add_subfields( '9', $authid );<br>
+                    $num_headings_changed++;<br>
+                    $results{'added'}->{ $heading->display_form() }++;<br>
+                }<br>
             }<br>
             elsif ( defined $current_link ) {<br>
-                $field->delete_subfield( code => '9' );<br>
-                $num_headings_changed++;<br>
-                $results{'unlinked'}->{ $heading->display_form() }++;<br>
+                if ( _check_valid_auth_link( $current_link, $field ) ) {<br>
+                    $results{'linked'}->{ $heading->display_form() }++;<br>
+                }<br>
+                else {<br>
+                    $field->delete_subfield( code => '9' );<br>
+                    $num_headings_changed++;<br>
+                    $results{'unlinked'}->{ $heading->display_form() }++;<br>
+                }<br>
             }<br>
             else {<br>
                 $results{'unlinked'}->{ $heading->display_form() }++;<br>
@@ -636,6 +648,30 @@ sub LinkBibHeadingsToAuthorities {<br>
     return $num_headings_changed, \%results;<br>
 }<br>
<br>
+=head2 _check_valid_auth_link<br>
+<br>
+    if ( _check_valid_auth_link($authid, $field) ) {<br>
+        ...<br>
+    }<br>
+<br>
+Check whether the specified heading-auth link is valid without reference<br>
+to Zebra/Solr. Ideally this code would be in C4::Heading, but that won't be<br>
+possible until we have de-cycled C4::AuthoritiesMarc, so this is the<br>
+safest place.<br>
+<br>
+=cut<br>
+<br>
+sub _check_valid_auth_link {<br>
+    my ( $authid, $field ) = @_;<br>
+<br>
+    require C4::AuthoritiesMarc;<br>
+<br>
+    my $authorized_heading =<br>
+      C4::AuthoritiesMarc::GetAuthorizedHeading( { 'authid' => $authid } );<br>
+<br>
+   return ($field->as_string('abcdefghijklmnopqrstuvwxyz') eq $authorized_heading);<br>
+}<br>
+<br>
 =head2 GetRecordValue<br>
<br>
   my $values = GetRecordValue($field, $record, $frameworkcode);<br>
<span><font color="#888888">--<br>
1.7.2.5<br>
_______________________________________________<br>
Koha-patches mailing list<br>
<a href="mailto:Koha-patches@lists.koha-community.org" target="_blank">Koha-patches@lists.koha-community.org</a><br>
<a href="http://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-patches" target="_blank">http://lists.koha-community.org/cgi-bin/mailman/listinfo/koha-patches</a><br>
website : <a href="http://www.koha-community.org/" target="_blank">http://www.koha-community.org/</a><br>
git : <a href="http://git.koha-community.org/" target="_blank">http://git.koha-community.org/</a><br>
bugs : <a href="http://bugs.koha-community.org/" target="_blank">http://bugs.koha-community.org/</a><br>
</font></span></blockquote></div><br></div>
</div></div></blockquote></div><br></div>