From: Lebbeous Fogle-Weekley Date: Tue, 17 Jan 2012 22:06:58 +0000 (-0500) Subject: fix db stored proc not to return record id; basic functioning ml X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=30bd9ad5ae7431681636819dffbe9f3110f59aa8;p=evergreen%2Fequinox.git fix db stored proc not to return record id; basic functioning ml Signed-off-by: Lebbeous Fogle-Weekley --- diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/AutoSuggest.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/AutoSuggest.pm index 31110690e9..7c1636df48 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/WWW/AutoSuggest.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/WWW/AutoSuggest.pm @@ -4,13 +4,13 @@ use strict; use warnings; use Apache2::Log; -use Apache2::Const -compile => qw(OK REDIRECT DECLINED NOT_FOUND :log); -use APR::Const -compile => qw(:error SUCCESS); -use Apache2::RequestRec (); -use Apache2::RequestIO (); -use Apache2::RequestUtil; -use CGI; +use Apache2::Const -compile => qw( + OK REDIRECT DECLINED NOT_FOUND HTTP_INTERNAL_SERVER_ERROR :log +); +use XML::LibXML; +use CGI qw(:all -utf8); +use OpenILS::Utils::CStoreEditor qw/:funcs/; sub prepare_for_tsquery { my ($str) = shift; @@ -21,13 +21,101 @@ sub prepare_for_tsquery { return join(" & ", split(/\s+/, $str)); } +sub prepare_headline_opts { + my ($css_prefix, $highlight_min, $highlight_max) = @_; + + $css_prefix =~ s/[^\w]//g; + + my @parts = ( + qq{StartSel=""}, + "StopSel=" + ); + + push @parts, "MinWords=$highlight_min" if $highlight_min > 0; + push @parts, "MaxWords=$highlight_max" if $highlight_max > 0; + + return join(", ", @parts); +} + +sub get_suggestions { + my $editor = shift; + my $query = shift; + my $search_class = shift; + my $org_unit = shift; + my $css_prefix = shift || 'oils_AS'; + my $highlight_min = int(shift || 0); + my $highlight_max = int(shift || 0); + my $limit = int(shift || 10); + my $restrict = int(shift || 0); + + $limit = 10 unless $limit > 0; + + my $headline_opts = prepare_headline_opts( + $css_prefix, $highlight_min, $highlight_max + ); + + return $editor->json_query({ + "from" => [ + "metabib.suggest_browse_entries", + prepare_for_tsquery($query), + $search_class, + $headline_opts, + $org_unit, + $restrict + ] + }); +} + +sub suggestions_to_xml { + my ($suggestions) = @_; + + my $dom = new XML::LibXML::Document("1.0", "UTF-8"); + my $as = $dom->createElement("as"); + $dom->setDocumentElement($as); + + foreach (@$suggestions) { + my $val = $dom->createElement("val"); + $val->setAttribute("term", $_->{value}); + $val->setAttribute("field", $_->{field}); + $val->appendText($_->{match}); + $as->addChild($val); + } + + # XML::LibXML::Document::toString() returns an encoded byte string, which + # is why we don't need to binmode STDOUT, ':utf8'. + return $dom->toString(); +} + sub handler { my $r = shift; my $cgi = new CGI; - print "Content-type: application/xml; charset=utf-8\n\n"; + my $editor = new_editor; + $r->content_type("application/xml; charset=utf-8"); + + my $suggestions = get_suggestions( + $editor, + map { scalar($cgi->param($_)) } qw( + query + search_class + org_unit + css_prefix + highlight_min + highlight_max + limit + restrict + ) + ); + + if (not $suggestions) { + $r->log->error( + "get_suggestsions() failed: " . $editor->die_event->{textcode} + ); + return Apache2::Const::HTTP_INTERNAL_SERVER_ERROR; + } - print qq{ \n}; + $editor->disconnect; + print suggestions_to_xml($suggestions); return Apache2::Const::OK; } diff --git a/Open-ILS/src/sql/Pg/upgrade/YYYY.schema.bib_autosuggest.sql b/Open-ILS/src/sql/Pg/upgrade/YYYY.schema.bib_autosuggest.sql index 6ea56d7d06..8f35bdf740 100644 --- a/Open-ILS/src/sql/Pg/upgrade/YYYY.schema.bib_autosuggest.sql +++ b/Open-ILS/src/sql/Pg/upgrade/YYYY.schema.bib_autosuggest.sql @@ -342,14 +342,14 @@ $func$ LANGUAGE PLPGSQL; CREATE OR REPLACE - FUNCTION metabib.suggest_biblio_record_entry( + FUNCTION metabib.suggest_browse_entries( query_text TEXT, -- 'foo' or 'foo & ba:*',ready for to_tsquery() search_class TEXT, -- 'alias' or 'class' or 'class|field..', etc headline_opts TEXT, -- markup options for ts_headline() visibility_org INTEGER,-- null if you don't want opac visibility test strict_match BOOL -- actually limit to what matches search_class ) RETURNS TABLE ( - record BIGINT, + value TEXT, -- plain match TEXT, -- marked up field INTEGER, bouyant_and_class_match BOOL, @@ -396,7 +396,7 @@ BEGIN END IF; RETURN QUERY EXECUTE 'SELECT DISTINCT - mbedm.source, + mbe.value, TS_HEADLINE(mbe.value, $1, $3), cmf.id, cmc.bouyant AND _registered.field_class IS NOT NULL,