linked authority records working; leading article warning working
authorLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Fri, 3 May 2013 19:49:56 +0000 (15:49 -0400)
committerLebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Fri, 3 May 2013 19:49:56 +0000 (15:49 -0400)
Signed-off-by: Lebbeous Fogle-Weekley <lebbeous@esilibrary.com>
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Browse.pm
Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/Util.pm
Open-ILS/src/sql/Pg/999.functions.global.sql
Open-ILS/src/sql/Pg/upgrade/XXXX.schema.config-metabib-interauthority.sql
Open-ILS/src/sql/Pg/upgrade/YYYY.schema.bib-auth-browse.sql
Open-ILS/src/templates/opac/browse.tt2
Open-ILS/src/templates/opac/css/style.css.tt2

index 423903c..33db369 100644 (file)
@@ -82,6 +82,7 @@ sub flesh_browse_results {
     open FH, ">/tmp/lfw-auth-" . time() or die "LFW XXX $!";
     print FH Dumper(\@auth_ids), "\n";
     print FH Dumper($results), "\n";
+
     if (@auth_ids) {
         # Get all linked authority records themselves
         my $linked_authorities = $self->editor->json_query({
@@ -94,22 +95,23 @@ sub flesh_browse_results {
             where => {"+aalink" => {target => \@auth_ids}}
         }) or return;
 
+        my %linked_headings_by_auth_id;
+
         # Then use the linked authority records' control sets to find and
         # pick out non-main-entry headings.  Build the headings and make a
         # combined data structure for the template's use.
 
-        my %linked_headings_by_target;
-
         foreach my $row (@$linked_authorities) {
-            my $acsaf_list =
+            my $acsaf_table =
                 $self->ctx->{get_authority_fields}->($row->{control_set});
 
             $row->{headings} = [];
-            # BEGIN isolate in eval?
+            # XXX BEGIN isolate in eval? in case of bad marc causing a die()?
             my $record = new_from_xml MARC::Record($row->{marc});
-            foreach my $acsaf (@$acsaf_list) {
+            foreach my $acsaf (values(%$acsaf_table)) {
                 my @fields = $record->field($acsaf->tag);
                 my @headings;
+
                 foreach (@fields) {
                     my $heading = "";
                     foreach my $sf (split "", $acsaf->sf_list) {
@@ -118,15 +120,28 @@ sub flesh_browse_results {
                     push @headings, $heading;
                 }
 
-                push @{$row->{headings}}, {$acsaf->id => \@headings};
+                # Remember: main_entry is a link field, so for it to evaluate
+                # to true means that we *have* (and therefore *aren't*) a main
+                # entry.  The rest of the time when main_entry is undef we
+                # *are* a main entry.
+                #
+                # For this, we only want non-main entries.
+                push @{$row->{headings}}, {$acsaf->id => \@headings}
+                    if @headings and $acsaf->main_entry;
             }
-            # END isolate in eval?
+            # XXX END isolate in eval?
 
-            $linked_headings_by_target{$row->{target}} = $row;
+            $linked_headings_by_auth_id{$row->{id}} = $row;
         }
 
         # XXX TODO count bibs linked to each linked auth record?
-        print FH Dumper(\%linked_headings_by_target), "\n";
+#        print FH Dumper(\%linked_headings_by_auth_id), "\n";
+
+        foreach my $row (@$results) {
+            $row->{authorities} = [
+                map { $linked_headings_by_auth_id{$_} } @{$row->{authorities}}
+            ];
+        }
     }
     close FH;
 
@@ -187,6 +202,35 @@ sub deduce_possible_paging {
     }
 }
 
+sub leading_article_test {
+    my ($self, $qtype, $bterm) = @_;
+
+    my $flag_name = "opac.browse.warnable_regexp_per_class";
+    my $flag = $self->ctx->{get_cgf}->($flag_name);
+
+    return unless $flag->enabled;
+
+    my $map;
+
+    eval { $map = OpenSRF::Utils::JSON->JSON2perl($flag->value); };
+    if ($@) {
+        $logger->warn("cgf '$flag_name' enabled but value is invalid JSON? $@");
+        return;
+    }
+
+    # Don't crash over any of the things that could go wrong in here:
+    eval {
+        if ($map->{$qtype}) {
+            if ($bterm =~ qr/$map->{$qtype}/i) {
+                $self->ctx->{browse_leading_article_warning} = 1;
+            }
+        }
+    };
+    if ($@) {
+        $logger->warn("cgf '$flag_name' has valid JSON in value, but: $@");
+    }
+}
+
 sub load_browse {
     my ($self) = @_;
 
@@ -196,8 +240,13 @@ sub load_browse {
     $self->ctx->{more_back} = 0;
 
     if ($self->cgi->param('qtype') and $self->cgi->param('bterm')) {
-        my ($cache_key, $limit, $offset, @params) =
-            $self->prepare_browse_parameters;
+
+        $self->leading_article_test(
+            $self->cgi->param('qtype'),
+            $self->cgi->param('bterm')
+        );
+
+        my ($cache_key, $limit, $offset, @params) = $self->prepare_browse_parameters;
 
         my $results = $browse_cache->get_cache($cache_key) ||
             $self->load_browse_impl($limit, $offset, @params);
index f2abacd..8e7a1ec 100644 (file)
@@ -232,9 +232,13 @@ sub init_ro_object_cache {
     $ro_object_subs->{get_authority_fields} = sub {
         my ($control_set) = @_;
 
-        $cache{authority_fields}{$ctx->{locale}}{$control_set} =
-            $e->search_authority_control_set_authority_field({control_set => $control_set})
-                unless exists $cache{authority_fields}{$ctx->{locale}}{$control_set};
+        if (not exists $cache{authority_fields}{$ctx->{locale}}{$control_set}) {
+            my $acs = $e->search_authority_control_set_authority_field(
+                {control_set => $control_set}
+            ) or return;
+            $cache{authority_fields}{$ctx->{locale}}{$control_set} =
+                +{ map { $_->id => $_ } @$acs };
+        }
 
         return $cache{authority_fields}{$ctx->{locale}}{$control_set};
     };
index 404480f..e419dae 100644 (file)
@@ -1537,7 +1537,7 @@ BEGIN
             FROM authority.control_set_authority_field
             WHERE tag IN (
                 SELECT UNNEST(
-                    XPATH('//*[starts-with(@tag,"1")]/@tag',rec_marc::XML)::TEXT[]
+                    XPATH('//*[starts-with(@tag,"1")]/@tag',rec_marc_xml)::TEXT[]
                 )
             ) LIMIT 1;
 
index 0424a77..7901876 100644 (file)
@@ -33,7 +33,7 @@ BEGIN
             FROM authority.control_set_authority_field
             WHERE tag IN (
                 SELECT UNNEST(
-                    XPATH('//*[starts-with(@tag,"1")]/@tag',rec_marc::XML)::TEXT[]
+                    XPATH('//*[starts-with(@tag,"1")]/@tag',rec_marc_xml::XML)::TEXT[]
                 )
             ) LIMIT 1;
 
index a93d6ba..460983e 100644 (file)
@@ -2,9 +2,7 @@ BEGIN;
 
 -- check whether patch can be applied
 -- SELECT evergreen.upgrade_deps_block_check('YYYY', :eg_version);
-
-ALTER TABLE metabib.browse_entry_def_map
-    ADD COLUMN authority BIGINT REFERENCES authority.record_entry (id)
+ALTER TABLE metabib.browse_entry_def_map ADD COLUMN authority BIGINT REFERENCES authority.record_entry (id)
         ON DELETE SET NULL;
 
 ALTER TABLE config.metabib_field ADD COLUMN authority_xpath TEXT;
@@ -6885,6 +6883,19 @@ Revision 1.2 - Added Log Comment  2003/03/24 19:37:42  ckeith
 -- ----------------------------------------------------
 -- XXX TODO From here down, add this to stock SQL files
 
+INSERT INTO config.global_flag (name, value, enabled, label) 
+VALUES (
+    'opac.browse.warnable_regexp_per_class',
+    '{"title": "^(a|the|an)\\s"}',
+    FALSE,
+    oils_i18n_gettext(
+        'opac.browse.warnable_regexp_per_class',
+        'Map of search classes to regular expressions to warn user about leading articles.',
+        'cgf',
+        'label'
+    )
+);
+
 ALTER TABLE metabib.browse_entry ADD COLUMN sort_value TEXT;
 
 CREATE OR REPLACE FUNCTION metabib.browse_entry_sort_value()
index 3c4b6b4..c6ae787 100644 (file)
@@ -9,14 +9,16 @@
     ctx.page_title = l("Browse the Catalog");
     blimit = CGI.param('blimit') || 10;
     boffset = CGI.param('boffset') || 0;
-    %]
+
+    depart_list = ['blimit', 'bterm', 'boffset'];
+%]
 
     <div id="search-wrapper">
         [%# XXX TODO Give searchbar.tt2 more smarts so we can just do:
           # INCLUDE "opac/parts/searchbar.tt2" %]
         <div id="search-tools">
-            <span class="search_catalog_lbl">[% l('Search the Catalog') %]</span>
-            <span><a href="[% mkurl(ctx.opac_root _ '/advanced') %]"
+            <span class="search_catalog_lbl"><a href="[% mkurl(ctx.opac_root _ '/home', {}, depart_list) %]">[% l('Search the Catalog') %]</a></span>
+            <span><a href="[% mkurl(ctx.opac_root _ '/advanced', {}, depart_list) %]"
                     id="home_adv_search_link">[%l('Advanced Search')%]</a></span>
             <span>[% l('Browse the Catalog') %]</span>
         </div>
                         "to library staff.") %]
                     </span>
                 [% ELSE %]
+                    [% IF ctx.browse_leading_article_warning %]
+                    <div class="browse-leading-article-warning">
+                            [% l("Your browse term seems to begin with an article. You might better results by omitting the article.") %]
+                    </div>
+                    [% END %]
                     <ul class="browse-result-list">
                     [% FOR result IN ctx.browse_results %]
                         <li class="browse-result">
                                     }) %]">[% result.value | html %]</a>
                             </span>
                             <span class="browse-result-sources">([% result.sources %])</span>
+                            [% IF result.authorities %]
+                            <ul class="browse-result-authority-headings">
+                                [% FOR a IN result.authorities;
+                                    NEXT UNLESS a.control_set;  # XXX can't deal
+
+                                    # get_authority_fields is fast and cache-y.
+                                    acs = ctx.get_authority_fields(a.control_set);
+                                    FOR h IN a.headings;
+                                        field_id = h.keys.0;
+                                        field = acs.$field_id;
+                                        headings_themselves = h.values.0 %]
+                                        <li>[% field.name %] <a href="[% mkurl(ctx.opac_root _ '/results', {query => 'identifier|authority_id[' _ a.target _ ']'}) %]">[% headings_themselves.join(";") %]</a></li>
+                                    [% END %]
+                                [% END %]
+                            </ul>
+                            [% END %]
                         </li>
                     [% END %]
                     </ul>
index b3e497b..ba4eec7 100644 (file)
@@ -1531,3 +1531,7 @@ a.preflib_change {
 .browse-shortcuts {
     font-size: 120%;
 }
+.browse-leading-article-warning {
+    font-style: italic;
+    font-size: 110%;
+}