]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/version-upgrade/2.3.4-2.3.5-upgrade-db.sql
2.3.4 -> 2.3.5 DB upgrade script
[working/Evergreen.git] / Open-ILS / src / sql / Pg / version-upgrade / 2.3.4-2.3.5-upgrade-db.sql
1 --Upgrade Script for 2.3.4 to 2.3.5
2 \set eg_version '''2.3.5'''
3 BEGIN;
4 INSERT INTO config.upgrade_log (version, applied_to) VALUES ('2.3.5', :eg_version);
5 -- Evergreen DB patch XXXX.function.merge_record_assets_deleted_call_numbers.sql
6 --
7
8 -- check whether patch can be applied
9 SELECT evergreen.upgrade_deps_block_check('0761', :eg_version);
10
11 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
12 DECLARE
13     moved_objects INT := 0;
14     source_cn     asset.call_number%ROWTYPE;
15     target_cn     asset.call_number%ROWTYPE;
16     metarec       metabib.metarecord%ROWTYPE;
17     hold          action.hold_request%ROWTYPE;
18     ser_rec       serial.record_entry%ROWTYPE;
19     ser_sub       serial.subscription%ROWTYPE;
20     acq_lineitem  acq.lineitem%ROWTYPE;
21     acq_request   acq.user_request%ROWTYPE;
22     booking       booking.resource_type%ROWTYPE;
23     source_part   biblio.monograph_part%ROWTYPE;
24     target_part   biblio.monograph_part%ROWTYPE;
25     multi_home    biblio.peer_bib_copy_map%ROWTYPE;
26     uri_count     INT := 0;
27     counter       INT := 0;
28     uri_datafield TEXT;
29     uri_text      TEXT := '';
30 BEGIN
31
32     -- move any 856 entries on records that have at least one MARC-mapped URI entry
33     SELECT  INTO uri_count COUNT(*)
34       FROM  asset.uri_call_number_map m
35             JOIN asset.call_number cn ON (m.call_number = cn.id)
36       WHERE cn.record = source_record;
37
38     IF uri_count > 0 THEN
39         
40         -- This returns more nodes than you might expect:
41         -- 7 instead of 1 for an 856 with $u $y $9
42         SELECT  COUNT(*) INTO counter
43           FROM  oils_xpath_table(
44                     'id',
45                     'marc',
46                     'biblio.record_entry',
47                     '//*[@tag="856"]',
48                     'id=' || source_record
49                 ) as t(i int,c text);
50     
51         FOR i IN 1 .. counter LOOP
52             SELECT  '<datafield xmlns="http://www.loc.gov/MARC21/slim"' || 
53                         ' tag="856"' ||
54                         ' ind1="' || FIRST(ind1) || '"'  ||
55                         ' ind2="' || FIRST(ind2) || '">' ||
56                         array_to_string(
57                             array_accum(
58                                 '<subfield code="' || subfield || '">' ||
59                                 regexp_replace(
60                                     regexp_replace(
61                                         regexp_replace(data,'&','&amp;','g'),
62                                         '>', '&gt;', 'g'
63                                     ),
64                                     '<', '&lt;', 'g'
65                                 ) || '</subfield>'
66                             ), ''
67                         ) || '</datafield>' INTO uri_datafield
68               FROM  oils_xpath_table(
69                         'id',
70                         'marc',
71                         'biblio.record_entry',
72                         '//*[@tag="856"][position()=' || i || ']/@ind1|' ||
73                         '//*[@tag="856"][position()=' || i || ']/@ind2|' ||
74                         '//*[@tag="856"][position()=' || i || ']/*/@code|' ||
75                         '//*[@tag="856"][position()=' || i || ']/*[@code]',
76                         'id=' || source_record
77                     ) as t(id int,ind1 text, ind2 text,subfield text,data text);
78
79             -- As most of the results will be NULL, protect against NULLifying
80             -- the valid content that we do generate
81             uri_text := uri_text || COALESCE(uri_datafield, '');
82         END LOOP;
83
84         IF uri_text <> '' THEN
85             UPDATE  biblio.record_entry
86               SET   marc = regexp_replace(marc,'(</[^>]*record>)', uri_text || E'\\1')
87               WHERE id = target_record;
88         END IF;
89
90     END IF;
91
92         -- Find and move metarecords to the target record
93         SELECT  INTO metarec *
94           FROM  metabib.metarecord
95           WHERE master_record = source_record;
96
97         IF FOUND THEN
98                 UPDATE  metabib.metarecord
99                   SET   master_record = target_record,
100                         mods = NULL
101                   WHERE id = metarec.id;
102
103                 moved_objects := moved_objects + 1;
104         END IF;
105
106         -- Find call numbers attached to the source ...
107         FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
108
109                 SELECT  INTO target_cn *
110                   FROM  asset.call_number
111                   WHERE label = source_cn.label
112                         AND owning_lib = source_cn.owning_lib
113                         AND record = target_record
114                         AND NOT deleted;
115
116                 -- ... and if there's a conflicting one on the target ...
117                 IF FOUND THEN
118
119                         -- ... move the copies to that, and ...
120                         UPDATE  asset.copy
121                           SET   call_number = target_cn.id
122                           WHERE call_number = source_cn.id;
123
124                         -- ... move V holds to the move-target call number
125                         FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
126                 
127                                 UPDATE  action.hold_request
128                                   SET   target = target_cn.id
129                                   WHERE id = hold.id;
130                 
131                                 moved_objects := moved_objects + 1;
132                         END LOOP;
133
134                 -- ... if not ...
135                 ELSE
136                         -- ... just move the call number to the target record
137                         UPDATE  asset.call_number
138                           SET   record = target_record
139                           WHERE id = source_cn.id;
140                 END IF;
141
142                 moved_objects := moved_objects + 1;
143         END LOOP;
144
145         -- Find T holds targeting the source record ...
146         FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP
147
148                 -- ... and move them to the target record
149                 UPDATE  action.hold_request
150                   SET   target = target_record
151                   WHERE id = hold.id;
152
153                 moved_objects := moved_objects + 1;
154         END LOOP;
155
156         -- Find serial records targeting the source record ...
157         FOR ser_rec IN SELECT * FROM serial.record_entry WHERE record = source_record LOOP
158                 -- ... and move them to the target record
159                 UPDATE  serial.record_entry
160                   SET   record = target_record
161                   WHERE id = ser_rec.id;
162
163                 moved_objects := moved_objects + 1;
164         END LOOP;
165
166         -- Find serial subscriptions targeting the source record ...
167         FOR ser_sub IN SELECT * FROM serial.subscription WHERE record_entry = source_record LOOP
168                 -- ... and move them to the target record
169                 UPDATE  serial.subscription
170                   SET   record_entry = target_record
171                   WHERE id = ser_sub.id;
172
173                 moved_objects := moved_objects + 1;
174         END LOOP;
175
176         -- Find booking resource types targeting the source record ...
177         FOR booking IN SELECT * FROM booking.resource_type WHERE record = source_record LOOP
178                 -- ... and move them to the target record
179                 UPDATE  booking.resource_type
180                   SET   record = target_record
181                   WHERE id = booking.id;
182
183                 moved_objects := moved_objects + 1;
184         END LOOP;
185
186         -- Find acq lineitems targeting the source record ...
187         FOR acq_lineitem IN SELECT * FROM acq.lineitem WHERE eg_bib_id = source_record LOOP
188                 -- ... and move them to the target record
189                 UPDATE  acq.lineitem
190                   SET   eg_bib_id = target_record
191                   WHERE id = acq_lineitem.id;
192
193                 moved_objects := moved_objects + 1;
194         END LOOP;
195
196         -- Find acq user purchase requests targeting the source record ...
197         FOR acq_request IN SELECT * FROM acq.user_request WHERE eg_bib = source_record LOOP
198                 -- ... and move them to the target record
199                 UPDATE  acq.user_request
200                   SET   eg_bib = target_record
201                   WHERE id = acq_request.id;
202
203                 moved_objects := moved_objects + 1;
204         END LOOP;
205
206         -- Find parts attached to the source ...
207         FOR source_part IN SELECT * FROM biblio.monograph_part WHERE record = source_record LOOP
208
209                 SELECT  INTO target_part *
210                   FROM  biblio.monograph_part
211                   WHERE label = source_part.label
212                         AND record = target_record;
213
214                 -- ... and if there's a conflicting one on the target ...
215                 IF FOUND THEN
216
217                         -- ... move the copy-part maps to that, and ...
218                         UPDATE  asset.copy_part_map
219                           SET   part = target_part.id
220                           WHERE part = source_part.id;
221
222                         -- ... move P holds to the move-target part
223                         FOR hold IN SELECT * FROM action.hold_request WHERE target = source_part.id AND hold_type = 'P' LOOP
224                 
225                                 UPDATE  action.hold_request
226                                   SET   target = target_part.id
227                                   WHERE id = hold.id;
228                 
229                                 moved_objects := moved_objects + 1;
230                         END LOOP;
231
232                 -- ... if not ...
233                 ELSE
234                         -- ... just move the part to the target record
235                         UPDATE  biblio.monograph_part
236                           SET   record = target_record
237                           WHERE id = source_part.id;
238                 END IF;
239
240                 moved_objects := moved_objects + 1;
241         END LOOP;
242
243         -- Find multi_home items attached to the source ...
244         FOR multi_home IN SELECT * FROM biblio.peer_bib_copy_map WHERE peer_record = source_record LOOP
245                 -- ... and move them to the target record
246                 UPDATE  biblio.peer_bib_copy_map
247                   SET   peer_record = target_record
248                   WHERE id = multi_home.id;
249
250                 moved_objects := moved_objects + 1;
251         END LOOP;
252
253         -- And delete mappings where the item's home bib was merged with the peer bib
254         DELETE FROM biblio.peer_bib_copy_map WHERE peer_record = (
255                 SELECT (SELECT record FROM asset.call_number WHERE id = call_number)
256                 FROM asset.copy WHERE id = target_copy
257         );
258
259     -- Finally, "delete" the source record
260     DELETE FROM biblio.record_entry WHERE id = source_record;
261
262         -- That's all, folks!
263         RETURN moved_objects;
264 END;
265 $func$ LANGUAGE plpgsql;
266
267
268 SELECT evergreen.upgrade_deps_block_check('0764', :eg_version);
269
270 UPDATE config.z3950_source
271     SET host = 'lx2.loc.gov', port = 210, db = 'LCDB'
272     WHERE name = 'loc'
273         AND host = 'z3950.loc.gov'
274         AND port = 7090
275         AND db = 'Voyager';
276
277 UPDATE config.z3950_attr
278     SET format = 6
279     WHERE source = 'loc'
280         AND name = 'lccn'
281         AND format = 1;
282
283
284 -- Evergreen DB patch XXXX.handle_null_svf_during_import.sql
285 --
286 -- Prevent applying a normalization function to a null SVF
287 -- attribute value from breaking record import.
288 --
289
290
291 -- check whether patch can be applied
292 SELECT evergreen.upgrade_deps_block_check('0766', :eg_version);
293
294 CREATE OR REPLACE FUNCTION vandelay.extract_rec_attrs ( xml TEXT, attr_defs TEXT[]) RETURNS hstore AS $_$
295 DECLARE
296     transformed_xml TEXT;
297     prev_xfrm       TEXT;
298     normalizer      RECORD;
299     xfrm            config.xml_transform%ROWTYPE;
300     attr_value      TEXT;
301     new_attrs       HSTORE := ''::HSTORE;
302     attr_def        config.record_attr_definition%ROWTYPE;
303 BEGIN
304
305     FOR attr_def IN SELECT * FROM config.record_attr_definition WHERE name IN (SELECT * FROM UNNEST(attr_defs)) ORDER BY format LOOP
306
307         IF attr_def.tag IS NOT NULL THEN -- tag (and optional subfield list) selection
308             SELECT  ARRAY_TO_STRING(ARRAY_ACCUM(x.value), COALESCE(attr_def.joiner,' ')) INTO attr_value
309               FROM  vandelay.flatten_marc(xml) AS x
310               WHERE x.tag LIKE attr_def.tag
311                     AND CASE
312                         WHEN attr_def.sf_list IS NOT NULL
313                             THEN POSITION(x.subfield IN attr_def.sf_list) > 0
314                         ELSE TRUE
315                         END
316               GROUP BY x.tag
317               ORDER BY x.tag
318               LIMIT 1;
319
320         ELSIF attr_def.fixed_field IS NOT NULL THEN -- a named fixed field, see config.marc21_ff_pos_map.fixed_field
321             attr_value := vandelay.marc21_extract_fixed_field(xml, attr_def.fixed_field);
322
323         ELSIF attr_def.xpath IS NOT NULL THEN -- and xpath expression
324
325             SELECT INTO xfrm * FROM config.xml_transform WHERE name = attr_def.format;
326
327             -- See if we can skip the XSLT ... it's expensive
328             IF prev_xfrm IS NULL OR prev_xfrm <> xfrm.name THEN
329                 -- Can't skip the transform
330                 IF xfrm.xslt <> '---' THEN
331                     transformed_xml := oils_xslt_process(xml,xfrm.xslt);
332                 ELSE
333                     transformed_xml := xml;
334                 END IF;
335
336                 prev_xfrm := xfrm.name;
337             END IF;
338
339             IF xfrm.name IS NULL THEN
340                 -- just grab the marcxml (empty) transform
341                 SELECT INTO xfrm * FROM config.xml_transform WHERE xslt = '---' LIMIT 1;
342                 prev_xfrm := xfrm.name;
343             END IF;
344
345             attr_value := oils_xpath_string(attr_def.xpath, transformed_xml, COALESCE(attr_def.joiner,' '), ARRAY[ARRAY[xfrm.prefix, xfrm.namespace_uri]]);
346
347         ELSIF attr_def.phys_char_sf IS NOT NULL THEN -- a named Physical Characteristic, see config.marc21_physical_characteristic_*_map
348             SELECT  m.value::TEXT INTO attr_value
349               FROM  vandelay.marc21_physical_characteristics(xml) v
350                     JOIN config.marc21_physical_characteristic_value_map m ON (m.id = v.value)
351               WHERE v.subfield = attr_def.phys_char_sf
352               LIMIT 1; -- Just in case ...
353
354         END IF;
355
356         -- apply index normalizers to attr_value
357         FOR normalizer IN
358             SELECT  n.func AS func,
359                     n.param_count AS param_count,
360                     m.params AS params
361               FROM  config.index_normalizer n
362                     JOIN config.record_attr_index_norm_map m ON (m.norm = n.id)
363               WHERE attr = attr_def.name
364               ORDER BY m.pos LOOP
365                 EXECUTE 'SELECT ' || normalizer.func || '(' ||
366                     quote_nullable( attr_value ) ||
367                     CASE
368                         WHEN normalizer.param_count > 0
369                             THEN ',' || REPLACE(REPLACE(BTRIM(normalizer.params,'[]'),E'\'',E'\\\''),E'"',E'\'')
370                             ELSE ''
371                         END ||
372                     ')' INTO attr_value;
373
374         END LOOP;
375
376         -- Add the new value to the hstore
377         new_attrs := new_attrs || hstore( attr_def.name, attr_value );
378
379     END LOOP;
380
381     RETURN new_attrs;
382 END;
383 $_$ LANGUAGE PLPGSQL;
384
385
386
387 SELECT evergreen.upgrade_deps_block_check('0767', :eg_version);
388
389 CREATE OR REPLACE FUNCTION evergreen.could_be_serial_holding_code(TEXT) RETURNS BOOL AS $$
390     use JSON::XS;
391     use MARC::Field;
392
393     eval {
394         my $holding_code = (new JSON::XS)->decode(shift);
395         new MARC::Field('999', @$holding_code);
396     };
397     return 0 if $@; 
398     # verify that subfield labels are exactly one character long
399     foreach (keys %{ { @$holding_code } }) {
400         return 0 if length($_) != 1;
401     }
402     return 1;
403 $$ LANGUAGE PLPERLU;
404
405 COMMENT ON FUNCTION evergreen.could_be_serial_holding_code(TEXT) IS
406     'Return true if parameter is valid JSON representing an array that at minimu
407 m doesn''t make MARC::Field balk and only has subfield labels exactly one character long.  Otherwise false.';
408
409
410 -- This UPDATE throws away data, but only bad data that makes things break
411 -- anyway.
412 UPDATE serial.issuance
413     SET holding_code = NULL
414     WHERE NOT could_be_serial_holding_code(holding_code);
415
416 ALTER TABLE serial.issuance
417     DROP CONSTRAINT IF EXISTS issuance_holding_code_check;
418
419 ALTER TABLE serial.issuance
420     ADD CHECK (holding_code IS NULL OR could_be_serial_holding_code(holding_code));
421
422
423 SELECT evergreen.upgrade_deps_block_check('0770', :eg_version);
424
425 CREATE OR REPLACE FUNCTION evergreen.get_barcodes(select_ou INT, type TEXT, in_barcode TEXT) RETURNS SETOF evergreen.barcode_set AS $$
426 DECLARE
427     cur_barcode TEXT;
428     barcode_len INT;
429     completion_len  INT;
430     asset_barcodes  TEXT[];
431     actor_barcodes  TEXT[];
432     do_asset    BOOL = false;
433     do_serial   BOOL = false;
434     do_booking  BOOL = false;
435     do_actor    BOOL = false;
436     completion_set  config.barcode_completion%ROWTYPE;
437 BEGIN
438
439     IF position('asset' in type) > 0 THEN
440         do_asset = true;
441     END IF;
442     IF position('serial' in type) > 0 THEN
443         do_serial = true;
444     END IF;
445     IF position('booking' in type) > 0 THEN
446         do_booking = true;
447     END IF;
448     IF do_asset OR do_serial OR do_booking THEN
449         asset_barcodes = asset_barcodes || in_barcode;
450     END IF;
451     IF position('actor' in type) > 0 THEN
452         do_actor = true;
453         actor_barcodes = actor_barcodes || in_barcode;
454     END IF;
455
456     barcode_len := length(in_barcode);
457
458     FOR completion_set IN
459       SELECT * FROM config.barcode_completion
460         WHERE active
461         AND org_unit IN (SELECT aou.id FROM actor.org_unit_ancestors(select_ou) aou)
462         LOOP
463         IF completion_set.prefix IS NULL THEN
464             completion_set.prefix := '';
465         END IF;
466         IF completion_set.suffix IS NULL THEN
467             completion_set.suffix := '';
468         END IF;
469         IF completion_set.length = 0 OR completion_set.padding IS NULL OR length(completion_set.padding) = 0 THEN
470             cur_barcode = completion_set.prefix || in_barcode || completion_set.suffix;
471         ELSE
472             completion_len = completion_set.length - length(completion_set.prefix) - length(completion_set.suffix);
473             IF completion_len >= barcode_len THEN
474                 IF completion_set.padding_end THEN
475                     cur_barcode = rpad(in_barcode, completion_len, completion_set.padding);
476                 ELSE
477                     cur_barcode = lpad(in_barcode, completion_len, completion_set.padding);
478                 END IF;
479                 cur_barcode = completion_set.prefix || cur_barcode || completion_set.suffix;
480             END IF;
481         END IF;
482         IF completion_set.actor THEN
483             actor_barcodes = actor_barcodes || cur_barcode;
484         END IF;
485         IF completion_set.asset THEN
486             asset_barcodes = asset_barcodes || cur_barcode;
487         END IF;
488     END LOOP;
489
490     IF do_asset AND do_serial THEN
491         RETURN QUERY SELECT 'asset'::TEXT, id, barcode FROM ONLY asset.copy WHERE barcode = ANY(asset_barcodes) AND deleted = false;
492         RETURN QUERY SELECT 'serial'::TEXT, id, barcode FROM serial.unit WHERE barcode = ANY(asset_barcodes) AND deleted = false;
493     ELSIF do_asset THEN
494         RETURN QUERY SELECT 'asset'::TEXT, id, barcode FROM asset.copy WHERE barcode = ANY(asset_barcodes) AND deleted = false;
495     ELSIF do_serial THEN
496         RETURN QUERY SELECT 'serial'::TEXT, id, barcode FROM serial.unit WHERE barcode = ANY(asset_barcodes) AND deleted = false;
497     END IF;
498     IF do_booking THEN
499         RETURN QUERY SELECT 'booking'::TEXT, id::BIGINT, barcode FROM booking.resource WHERE barcode = ANY(asset_barcodes);
500     END IF;
501     IF do_actor THEN
502         RETURN QUERY SELECT 'actor'::TEXT, c.usr::BIGINT, c.barcode FROM actor.card c JOIN actor.usr u ON c.usr = u.id WHERE
503             ((c.barcode = ANY(actor_barcodes) AND c.active) OR c.barcode = in_barcode) AND NOT u.deleted ORDER BY usr;
504     END IF;
505     RETURN;
506 END;
507 $$ LANGUAGE plpgsql;
508
509 -- Evergreen DB patch 0783.schema.enforce_use_id_for_tcn.sql
510 --
511 -- Sets the TCN value in the biblio.record_entry row to bib ID,
512 -- if the appropriate setting is in place
513 --
514
515 -- check whether patch can be applied
516 SELECT evergreen.upgrade_deps_block_check('0783', :eg_version);
517
518 -- FIXME: add/check SQL statements to perform the upgrade
519 CREATE OR REPLACE FUNCTION evergreen.maintain_901 () RETURNS TRIGGER AS $func$
520 use strict;
521 use MARC::Record;
522 use MARC::File::XML (BinaryEncoding => 'UTF-8');
523 use MARC::Charset;
524 use Encode;
525 use Unicode::Normalize;
526
527 MARC::Charset->assume_unicode(1);
528
529 my $schema = $_TD->{table_schema};
530 my $marc = MARC::Record->new_from_xml($_TD->{new}{marc});
531
532 my @old901s = $marc->field('901');
533 $marc->delete_fields(@old901s);
534
535 if ($schema eq 'biblio') {
536     my $tcn_value = $_TD->{new}{tcn_value};
537
538     # Set TCN value to record ID?
539     my $id_as_tcn = spi_exec_query("
540         SELECT enabled
541         FROM config.global_flag
542         WHERE name = 'cat.bib.use_id_for_tcn'
543     ");
544     if (($id_as_tcn->{processed}) && $id_as_tcn->{rows}[0]->{enabled} eq 't') {
545         $tcn_value = $_TD->{new}{id}; 
546         $_TD->{new}{tcn_value} = $tcn_value;
547     }
548
549     my $new_901 = MARC::Field->new("901", " ", " ",
550         "a" => $tcn_value,
551         "b" => $_TD->{new}{tcn_source},
552         "c" => $_TD->{new}{id},
553         "t" => $schema
554     );
555
556     if ($_TD->{new}{owner}) {
557         $new_901->add_subfields("o" => $_TD->{new}{owner});
558     }
559
560     if ($_TD->{new}{share_depth}) {
561         $new_901->add_subfields("d" => $_TD->{new}{share_depth});
562     }
563
564     $marc->append_fields($new_901);
565 } elsif ($schema eq 'authority') {
566     my $new_901 = MARC::Field->new("901", " ", " ",
567         "c" => $_TD->{new}{id},
568         "t" => $schema,
569     );
570     $marc->append_fields($new_901);
571 } elsif ($schema eq 'serial') {
572     my $new_901 = MARC::Field->new("901", " ", " ",
573         "c" => $_TD->{new}{id},
574         "t" => $schema,
575         "o" => $_TD->{new}{owning_lib},
576     );
577
578     if ($_TD->{new}{record}) {
579         $new_901->add_subfields("r" => $_TD->{new}{record});
580     }
581
582     $marc->append_fields($new_901);
583 } else {
584     my $new_901 = MARC::Field->new("901", " ", " ",
585         "c" => $_TD->{new}{id},
586         "t" => $schema,
587     );
588     $marc->append_fields($new_901);
589 }
590
591 my $xml = $marc->as_xml_record();
592 $xml =~ s/\n//sgo;
593 $xml =~ s/^<\?xml.+\?\s*>//go;
594 $xml =~ s/>\s+</></go;
595 $xml =~ s/\p{Cc}//go;
596
597 # Embed a version of OpenILS::Application::AppUtils->entityize()
598 # to avoid having to set PERL5LIB for PostgreSQL as well
599
600 # If we are going to convert non-ASCII characters to XML entities,
601 # we had better be dealing with a UTF8 string to begin with
602 $xml = decode_utf8($xml);
603
604 $xml = NFC($xml);
605
606 # Convert raw ampersands to entities
607 $xml =~ s/&(?!\S+;)/&amp;/gso;
608
609 # Convert Unicode characters to entities
610 $xml =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
611
612 $xml =~ s/[\x00-\x1f]//go;
613 $_TD->{new}{marc} = $xml;
614
615 return "MODIFY";
616 $func$ LANGUAGE PLPERLU;
617
618
619 COMMIT;