From d203a871d7594dce3ea2183b78965fa70eba72c4 Mon Sep 17 00:00:00 2001
From: Jared Camins-Esakov <jcamins@cpbibliography.com>
Date: Sun, 11 Mar 2012 13:48:43 -0400
Subject: [PATCH] [SIGNED-OFF] Bug 7617: Sort authority results by authid

Add the option of sorting authority search results by authid, and instruct the
FirstMatch and LastMatch linkers to use that sort order rather than the default
search order.

To test:
1. Install new Zebra authorities config
    etc/zebradb/marc_defs/marc21/authorities/authority-koha-indexdefs.xml,
    etc/zebradb/marc_defs/marc21/authorities/authority-zebra-indexdefs.xsl,
    etc/zebradb/marc_defs/marc21/authorities/record.abs, and
    etc/zebradb/marc_defs/unimarc/authorities/record.abs
2. Reindex authorities in Zebra
3. Set LinkerModule to FirstMatch or LastMatch
4. Add two identical authority records, and a bib record with a heading that
   matches them
5. Run misc/link_bibs_to_authorities.pl on that record
6. Confirm that the authid that's been inserted into subfield $9 of that
   heading is the first, if you selected FirstMatch, or last if you selected
   LastMatch

Signed-off-by: Katrin Fischer <Katrin.Fischer.83@web.de>
I followed the test plan and checked that for "Last match" and "First match"
the correct authority was selected and linked to the record.
---
 C4/AuthoritiesMarc.pm                              |   17 ++++++++++-------
 C4/Heading.pm                                      |    2 +-
 .../authorities/authority-koha-indexdefs.xml       |    1 +
 .../authorities/authority-zebra-indexdefs.xsl      |    2 +-
 .../marc_defs/marc21/authorities/record.abs        |    2 +-
 .../marc_defs/unimarc/authorities/record.abs       |    2 +-
 6 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/C4/AuthoritiesMarc.pm b/C4/AuthoritiesMarc.pm
index f35b885..703ea75 100644
--- a/C4/AuthoritiesMarc.pm
+++ b/C4/AuthoritiesMarc.pm
@@ -279,13 +279,16 @@ sub SearchAuthorities {
         }
         ## Adding order
         #$query=' @or  @attr 7=2 @attr 1=Heading 0 @or  @attr 7=1 @attr 1=Heading 1'.$query if ($sortby eq "HeadingDsc");
-        my $orderstring= ($sortby eq "HeadingAsc"?
-                           '@attr 7=1 @attr 1=Heading 0'
-                         :
-                           $sortby eq "HeadingDsc"?      
-                            '@attr 7=2 @attr 1=Heading 0'
-                           :''
-                        );            
+        my $orderstring;
+        if ($sortby eq 'HeadingAsc') {
+            $orderstring = '@attr 7=1 @attr 1=Heading 0';
+        } elsif ($sortby eq 'HeadingDsc') {
+            $orderstring = '@attr 7=2 @attr 1=Heading 0';
+        } elsif ($sortby eq 'AuthidAsc') {
+            $orderstring = '@attr 7=1 @attr 1=Local-Number 0';
+        } elsif ($sortby eq 'AuthidDsc') {
+            $orderstring = '@attr 7=2 @attr 1=Local-Number 0';
+        }
         $query=($query?$query:"\@attr 1=_ALLRECORDS \@attr 2=103 ''");
         $query="\@or $orderstring $query" if $orderstring;
 
diff --git a/C4/Heading.pm b/C4/Heading.pm
index 98bb4a8..102cac6 100644
--- a/C4/Heading.pm
+++ b/C4/Heading.pm
@@ -205,7 +205,7 @@ sub _search {
     return C4::AuthoritiesMarc::SearchAuthorities(
         \@marclist, \@and_or, \@excluding, \@operator,
         \@value,    0,        20,          $self->{'auth_type'},
-        '',         $skipmetadata
+        'AuthidAsc',         $skipmetadata
     );
 }
 
diff --git a/etc/zebradb/marc_defs/marc21/authorities/authority-koha-indexdefs.xml b/etc/zebradb/marc_defs/marc21/authorities/authority-koha-indexdefs.xml
index 67b234f..f1013e1 100644
--- a/etc/zebradb/marc_defs/marc21/authorities/authority-koha-indexdefs.xml
+++ b/etc/zebradb/marc_defs/marc21/authorities/authority-koha-indexdefs.xml
@@ -23,6 +23,7 @@ authority-zebra-indexdefs.xsl`
   <!-- Original record ID -->
   <kohaidx:index_control_field tag="001">
     <kohaidx:target_index>Local-Number:w</kohaidx:target_index>
+    <kohaidx:target_index>Local-Number:s</kohaidx:target_index>
   </kohaidx:index_control_field>
   
   <!-- 008 -->
diff --git a/etc/zebradb/marc_defs/marc21/authorities/authority-zebra-indexdefs.xsl b/etc/zebradb/marc_defs/marc21/authorities/authority-zebra-indexdefs.xsl
index 3ee5d6c..aa02e70 100644
--- a/etc/zebradb/marc_defs/marc21/authorities/authority-zebra-indexdefs.xsl
+++ b/etc/zebradb/marc_defs/marc21/authorities/authority-zebra-indexdefs.xsl
@@ -46,7 +46,7 @@ authority-zebra-indexdefs.xsl` (substituting the appropriate file names).
     </z:index>
   </xslo:template>
   <xslo:template match="marc:controlfield[@tag='001']">
-    <z:index name="Local-Number:w">
+    <z:index name="Local-Number:w Local-Number:s">
       <xslo:value-of select="."/>
     </z:index>
   </xslo:template>
diff --git a/etc/zebradb/marc_defs/marc21/authorities/record.abs b/etc/zebradb/marc_defs/marc21/authorities/record.abs
index 5baa5b2..23d75a2 100644
--- a/etc/zebradb/marc_defs/marc21/authorities/record.abs
+++ b/etc/zebradb/marc_defs/marc21/authorities/record.abs
@@ -11,7 +11,7 @@ systag sysno rank
 xpath enable
 
 all any
-melm 001 Local-Number
+melm 001 Local-Number,Local-Number:s
 melm 942$a authtype:w,authtype:p
 
 # Personal Name
diff --git a/etc/zebradb/marc_defs/unimarc/authorities/record.abs b/etc/zebradb/marc_defs/unimarc/authorities/record.abs
index 92f742b..18245c0 100644
--- a/etc/zebradb/marc_defs/unimarc/authorities/record.abs
+++ b/etc/zebradb/marc_defs/unimarc/authorities/record.abs
@@ -11,7 +11,7 @@ systag sysno rank
 xpath enable
 
 all any
-melm 001 Local-Number
+melm 001 Local-Number,Local-Number:s
 melm 942$a authtype
 melm 152$b authtype:w,authtype:p
 
-- 
1.7.5.4