2 * Copyright (C) 2004-2008 Georgia Public Library Service
3 * Copyright (C) 2008 Equinox Software, Inc.
4 * Mike Rylander <miker@esilibrary.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
22 CREATE OR REPLACE FUNCTION oils_xml_transform ( TEXT, TEXT ) RETURNS TEXT AS $_$
23 SELECT CASE WHEN (SELECT COUNT(*) FROM config.xml_transform WHERE name = $2 AND xslt = '---') > 0 THEN $1
24 ELSE xslt_process($1, (SELECT xslt FROM config.xml_transform WHERE name = $2))
26 $_$ LANGUAGE SQL STRICT IMMUTABLE;
28 CREATE OR REPLACE FUNCTION public.extract_marc_field ( TEXT, BIGINT, TEXT, TEXT ) RETURNS TEXT AS $$
29 SELECT regexp_replace(array_to_string( array_accum( output ),' ' ),$4,'','g') FROM oils_xpath_table('id', 'marc', $1, $3, 'id='||$2)x(id INT, output TEXT);
32 CREATE OR REPLACE FUNCTION oils_xml_uncache (xml TEXT) RETURNS BOOL AS $func$
33 delete $_SHARED{'_xslt_process'}{docs}{shift()};
35 $func$ LANGUAGE PLPERLU;
37 CREATE OR REPLACE FUNCTION oils_xml_cache (xml TEXT) RETURNS BOOL AS $func$
43 # The following approach uses the older XML::LibXML 1.69 / XML::LibXSLT 1.68
44 # methods of parsing XML documents and stylesheets, in the hopes of broader
45 # compatibility with distributions
46 my $parser = $_SHARED{'_xslt_process'}{parsers}{xml} || XML::LibXML->new();
48 # Cache the XML parser, if we do not already have one
49 $_SHARED{'_xslt_process'}{parsers}{xml} = $parser
50 unless ($_SHARED{'_xslt_process'}{parsers}{xml});
52 # Parse and cache the doc
53 eval { $_SHARED{'_xslt_process'}{docs}{$doc} = $parser->parse_string($doc) };
57 $func$ LANGUAGE PLPERLU;
59 -- if we use these, we need to ...
60 drop function oils_xpath(text, text, anyarray);
62 CREATE OR REPLACE FUNCTION oils_xpath (xpath TEXT, xml TEXT, ns TEXT[][]) RETURNS TEXT[] AS $func$
68 my $ns_string = shift || '';
69 #elog(NOTICE,"ns_string: $ns_string");
71 my %ns_list = $ns_string =~ m/\{([^{,]+),([^}]+)\}/g;
72 #elog(NOTICE,"NS Prefix $_: $ns_list{$_}") for (keys %ns_list);
74 # The following approach uses the older XML::LibXML 1.69 / XML::LibXSLT 1.68
75 # methods of parsing XML documents and stylesheets, in the hopes of broader
76 # compatibility with distributions
77 my $parser = eval { $_SHARED{'_xslt_process'}{parsers}{xml} || XML::LibXML->new() };
81 # Cache the XML parser, if we do not already have one
82 $_SHARED{'_xslt_process'}{parsers}{xml} = $parser
83 unless ($_SHARED{'_xslt_process'}{parsers}{xml});
85 # Look for a cached version of the doc, or parse it if none
86 my $dom = eval { $_SHARED{'_xslt_process'}{docs}{$doc} || $parser->parse_string($doc) };
90 # Cache the parsed XML doc, if already there
91 $_SHARED{'_xslt_process'}{docs}{$doc} = $dom
92 unless ($_SHARED{'_xslt_process'}{docs}{$doc});
94 # Register the requested namespaces
95 $dom->documentElement->setNamespace( $ns_list{$_} => $_ ) for ( keys %ns_list );
97 # Gather and return nodes
98 my @nodes = $dom->findnodes($xpath);
99 #elog(NOTICE,"nodes found by $xpath: ". scalar(@nodes));
101 return [ map { $_->toString } @nodes ];
102 $func$ LANGUAGE PLPERLU;
104 CREATE OR REPLACE FUNCTION oils_xpath ( TEXT, TEXT ) RETURNS TEXT[] AS $$SELECT oils_xpath( $1, $2, '{}'::TEXT[] );$$ LANGUAGE SQL IMMUTABLE;
108 CREATE FUNCTION version_specific_xpath () RETURNS TEXT AS $wrapper_function$
113 IF REGEXP_REPLACE(VERSION(),E'^.+?(\\d+\\.\\d+).*?$',E'\\1')::FLOAT < 8.3 THEN
114 out_text := 'Creating XPath functions that work like the native XPATH function in 8.3+';
116 EXECUTE $create_82_funcs$
118 CREATE OR REPLACE FUNCTION oils_xpath ( xpath TEXT, xml TEXT, ns ANYARRAY ) RETURNS TEXT[] AS $func$
125 munged_xpath := xpath;
127 IF ns IS NOT NULL AND array_upper(ns, 1) IS NOT NULL THEN
128 FOR namespace IN 1 .. array_upper(ns, 1) LOOP
129 munged_xpath := REGEXP_REPLACE(
131 E'(' || ns[namespace][1] || E'):(\\w+)',
132 E'*[local-name() = "\\2" and namespace-uri() = "' || ns[namespace][2] || E'"]',
137 munged_xpath := REGEXP_REPLACE( munged_xpath, E'\\]\\[(\\D)',E' and \\1', 'g');
140 -- RAISE NOTICE 'munged xpath: %', munged_xpath;
142 node_text := xpath_nodeset(xml, munged_xpath, 'XXX_OILS_NODESET');
143 -- RAISE NOTICE 'node_text: %', node_text;
145 IF munged_xpath ~ $re$/[^/[]*@[^/]+$$re$ THEN
146 node_text := REGEXP_REPLACE(node_text,'<XXX_OILS_NODESET>[^"]+"', '<XXX_OILS_NODESET>', 'g');
147 node_text := REGEXP_REPLACE(node_text,'"</XXX_OILS_NODESET>', '</XXX_OILS_NODESET>', 'g');
150 node_text := REGEXP_REPLACE(node_text,'^<XXX_OILS_NODESET>', '');
151 node_text := REGEXP_REPLACE(node_text,'</XXX_OILS_NODESET>$', '');
153 RETURN STRING_TO_ARRAY(node_text, '</XXX_OILS_NODESET><XXX_OILS_NODESET>');
155 $func$ LANGUAGE PLPGSQL IMMUTABLE;
157 CREATE OR REPLACE FUNCTION oils_xpath ( TEXT, TEXT ) RETURNS TEXT[] AS $$SELECT oils_xpath( $1, $2, '{}'::TEXT[] );$$ LANGUAGE SQL IMMUTABLE;
159 CREATE OR REPLACE FUNCTION oils_xslt_process(TEXT, TEXT) RETURNS TEXT AS $$
160 SELECT xslt_process( $1, $2 );
161 $$ LANGUAGE SQL IMMUTABLE;
164 ELSIF REGEXP_REPLACE(VERSION(),E'^.+?(\\d+\\.\\d+).*?$',E'\\1')::FLOAT = 8.3 THEN
165 out_text := 'Creating XPath wrapper functions around the native XPATH function in 8.3. contrib/xml2 still required!';
167 EXECUTE $create_83_funcs$
169 CREATE OR REPLACE FUNCTION oils_xpath ( TEXT, TEXT, ANYARRAY ) RETURNS TEXT[] AS 'SELECT XPATH( $1, $2::XML, $3 )::TEXT[];' LANGUAGE SQL IMMUTABLE;
170 CREATE OR REPLACE FUNCTION oils_xpath ( TEXT, TEXT ) RETURNS TEXT[] AS 'SELECT XPATH( $1, $2::XML )::TEXT[];' LANGUAGE SQL IMMUTABLE;
172 CREATE OR REPLACE FUNCTION oils_xslt_process(TEXT, TEXT) RETURNS TEXT AS $$
173 SELECT xslt_process( $1, $2 );
174 $$ LANGUAGE SQL IMMUTABLE;
179 out_text := 'Creating XPath wrapper functions around the native XPATH function in 8.4+, and plperlu-based xslt processor. No contrib/xml2 needed!';
181 EXECUTE $create_84_funcs$
183 CREATE OR REPLACE FUNCTION oils_xpath ( TEXT, TEXT, ANYARRAY ) RETURNS TEXT[] AS 'SELECT XPATH( $1, $2::XML, $3 )::TEXT[];' LANGUAGE SQL IMMUTABLE;
184 CREATE OR REPLACE FUNCTION oils_xpath ( TEXT, TEXT ) RETURNS TEXT[] AS 'SELECT XPATH( $1, $2::XML )::TEXT[];' LANGUAGE SQL IMMUTABLE;
186 CREATE OR REPLACE FUNCTION oils_xslt_process(TEXT, TEXT) RETURNS TEXT AS $func$
195 # The following approach uses the older XML::LibXML 1.69 / XML::LibXSLT 1.68
196 # methods of parsing XML documents and stylesheets, in the hopes of broader
197 # compatibility with distributions
198 my $parser = $_SHARED{'_xslt_process'}{parsers}{xml} || XML::LibXML->new();
200 # Cache the XML parser, if we do not already have one
201 $_SHARED{'_xslt_process'}{parsers}{xml} = $parser
202 unless ($_SHARED{'_xslt_process'}{parsers}{xml});
204 my $xslt_parser = $_SHARED{'_xslt_process'}{parsers}{xslt} || XML::LibXSLT->new();
206 # Cache the XSLT processor, if we do not already have one
207 $_SHARED{'_xslt_process'}{parsers}{xslt} = $xslt_parser
208 unless ($_SHARED{'_xslt_process'}{parsers}{xslt});
210 my $stylesheet = $_SHARED{'_xslt_process'}{stylesheets}{$xslt} ||
211 $xslt_parser->parse_stylesheet( $parser->parse_string($xslt) );
213 $_SHARED{'_xslt_process'}{stylesheets}{$xslt} = $stylesheet
214 unless ($_SHARED{'_xslt_process'}{stylesheets}{$xslt});
216 return $stylesheet->output_string(
217 $stylesheet->transform(
218 $parser->parse_string($doc)
222 $func$ LANGUAGE 'plperlu' STRICT IMMUTABLE;
230 $wrapper_function$ LANGUAGE PLPGSQL;
232 SELECT version_specific_xpath();
233 DROP FUNCTION version_specific_xpath();
236 CREATE OR REPLACE FUNCTION oils_xpath_string ( TEXT, TEXT, TEXT, ANYARRAY ) RETURNS TEXT AS $func$
237 SELECT ARRAY_TO_STRING(
240 CASE WHEN $1 ~ $re$/[^/[]*@[^]]+$$re$ OR $1 ~ $re$text\(\)$$re$ THEN '' ELSE '//text()' END,
246 $func$ LANGUAGE SQL IMMUTABLE;
248 CREATE OR REPLACE FUNCTION oils_xpath_string ( TEXT, TEXT, TEXT ) RETURNS TEXT AS $func$
249 SELECT oils_xpath_string( $1, $2, $3, '{}'::TEXT[] );
250 $func$ LANGUAGE SQL IMMUTABLE;
252 CREATE OR REPLACE FUNCTION oils_xpath_string ( TEXT, TEXT, ANYARRAY ) RETURNS TEXT AS $func$
253 SELECT oils_xpath_string( $1, $2, '', $3 );
254 $func$ LANGUAGE SQL IMMUTABLE;
256 CREATE OR REPLACE FUNCTION oils_xpath_string ( TEXT, TEXT ) RETURNS TEXT AS $func$
257 SELECT oils_xpath_string( $1, $2, '{}'::TEXT[] );
258 $func$ LANGUAGE SQL IMMUTABLE;
261 CREATE OR REPLACE FUNCTION oils_xpath_table ( key TEXT, document_field TEXT, relation_name TEXT, xpaths TEXT, criteria TEXT ) RETURNS SETOF RECORD AS $func$
270 xpath_list := STRING_TO_ARRAY( xpaths, '|' );
272 select_list := ARRAY_APPEND( select_list, key || '::INT AS key' );
274 FOR i IN 1 .. ARRAY_UPPER(xpath_list,1) LOOP
275 select_list := ARRAY_APPEND(
285 WHEN xpath_list[i] ~ $re$/[^/[]*@[^/]+$$re$ OR xpath_list[i] ~ $re$text\(\)$$re$ THEN xpath_list[i]
286 ELSE xpath_list[i] || '//text()'
290 $sel$ || document_field || $sel$
298 where_list := ARRAY_APPEND(
300 'c_' || i || ' IS NOT NULL'
306 SELECT $q$ || ARRAY_TO_STRING( select_list, ', ' ) || $q$ FROM $q$ || relation_name || $q$ WHERE ($q$ || criteria || $q$)
307 )x WHERE $q$ || ARRAY_TO_STRING( where_list, ' AND ' );
308 -- RAISE NOTICE 'query: %', q;
310 FOR out_record IN EXECUTE q LOOP
311 RETURN NEXT out_record;
316 $func$ LANGUAGE PLPGSQL IMMUTABLE;
319 CREATE OR REPLACE FUNCTION extract_marc_field ( TEXT, BIGINT, TEXT, TEXT ) RETURNS TEXT AS $$
325 SELECT regexp_replace(
327 $q$ || quote_literal($3) || $q$,
331 $q$ || quote_literal($4) || $q$,
334 FROM $q$ || $1 || $q$
335 WHERE id = $q$ || $2;
337 EXECUTE query INTO output;
339 -- RAISE NOTICE 'query: %, output; %', query, output;
343 $$ LANGUAGE PLPGSQL IMMUTABLE;
345 CREATE OR REPLACE FUNCTION extract_marc_field ( TEXT, BIGINT, TEXT ) RETURNS TEXT AS $$
346 SELECT extract_marc_field($1,$2,$3,'');
347 $$ LANGUAGE SQL IMMUTABLE;
351 CREATE OR REPLACE FUNCTION oils_i18n_xlate ( keytable TEXT, keyclass TEXT, keycol TEXT, identcol TEXT, keyvalue TEXT, raw_locale TEXT ) RETURNS TEXT AS $func$
353 locale TEXT := REGEXP_REPLACE( REGEXP_REPLACE( raw_locale, E'[;, ].+$', '' ), E'_', '-', 'g' );
354 language TEXT := REGEXP_REPLACE( locale, E'-.+$', '' );
355 result config.i18n_core%ROWTYPE;
357 keyfield TEXT := keyclass || '.' || keycol;
360 -- Try the full locale
362 FROM config.i18n_core
363 WHERE fq_field = keyfield
364 AND identity_value = keyvalue
365 AND translation = locale;
367 -- Try just the language
370 FROM config.i18n_core
371 WHERE fq_field = keyfield
372 AND identity_value = keyvalue
373 AND translation = language;
376 -- Fall back to the string we passed in in the first place
381 ' FROM ' || keytable ||
382 ' WHERE ' || identcol || ' = ' || quote_literal(keyvalue)
387 RETURN result.string;
389 $func$ LANGUAGE PLPGSQL STABLE;
391 -- Functions for marking translatable strings in SQL statements
392 -- Parameters are: primary key, string, class hint, property
393 CREATE OR REPLACE FUNCTION oils_i18n_gettext( INT, TEXT, TEXT, TEXT ) RETURNS TEXT AS $$
397 CREATE OR REPLACE FUNCTION oils_i18n_gettext( TEXT, TEXT, TEXT, TEXT ) RETURNS TEXT AS $$
401 CREATE OR REPLACE FUNCTION is_json( TEXT ) RETURNS BOOL AS $f$
404 eval { JSON::XS->new->allow_nonref->decode( $json ) };
406 $f$ LANGUAGE PLPERLU;
408 -- turn a JSON scalar into an SQL TEXT value
409 CREATE OR REPLACE FUNCTION oils_json_to_text( TEXT ) RETURNS TEXT AS $f$
413 eval { $txt = JSON::XS->new->allow_nonref->decode( $json ) };
414 return undef if ($@);
416 $f$ LANGUAGE PLPERLU;