]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/WORM.pm
debuging
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / WORM.pm
1 package OpenILS::Application::Storage::WORM;
2 use base qw/OpenILS::Application::Storage/;
3 use strict; use warnings;
4
5 use OpenSRF::EX qw/:try/;
6
7 use OpenSRF::Utils::SettingsClient;
8 use OpenSRF::Utils::Logger qw/:level/;
9 my $log = 'OpenSRF::Utils::Logger';
10
11 use OpenILS::Utils::FlatXML;
12 use OpenILS::Utils::Fieldmapper;
13 use JSON;
14
15 use XML::LibXML;
16 use XML::LibXSLT;
17 use Time::HiRes qw(time);
18
19 my $xml_util    = OpenILS::Utils::FlatXML->new();
20
21 my $parser              = XML::LibXML->new();
22 my $xslt                        = XML::LibXSLT->new();
23 my $mods_sheet;
24
25 use open qw/:utf8/;
26
27 my $xpathset = {};
28
29 sub child_init {
30         my $meth = __PACKAGE__->method_lookup('open-ils.storage.direct.config.metabib_field.retrieve.all');
31         for my $f ($meth->run) {
32                 $xpathset->{ $f->field_class }->{ $f->name }->{xpath} = $f->xpath;
33                 $xpathset->{ $f->field_class }->{ $f->name }->{id} = $f->id;
34                 $log->debug("Loaded XPath from DB: ".$f->field_class." => ".$f->name." : ".$f->xpath, DEBUG);
35         }
36 }
37
38 # --------------------------------------------------------------------------------
39 # Fingerprinting
40
41 my @fp_mods_xpath = (
42         '//mods:mods/mods:typeOfResource[text()="text"]' => [
43                         title   => {
44                                         xpath   => [
45                                                         '//mods:mods/mods:titleInfo[mods:title and (@type="uniform")]',
46                                                         '//mods:mods/mods:titleInfo[mods:title and (@type="translated")]',
47                                                         '//mods:mods/mods:titleInfo[mods:title and (@type="alternative")]',
48                                                         '//mods:mods/mods:titleInfo[mods:title and not(@type)]',
49                                         ],
50                                         fixup   => '
51                                                         do {
52                                                                 $text = lc($text);
53                                                                 $text =~ s/\s+/ /sgo;
54                                                                 $text =~ s/^\s*(.+)\s*$/$1/sgo;
55                                                                 $text =~ s/\b(?:the|an?)\b//sgo;
56                                                                 $text =~ s/\[.[^\]]+\]//sgo;
57                                                                 $text =~ s/\s*[;\/\.]*$//sgo;
58                                                                 $text =~ s/\pM+//gso;
59                                                         };
60                                         ',
61                         },
62                         author  => {
63                                         xpath   => [
64                                                         '//mods:mods/mods:name[mods:role/mods:text/text()="creator" and @type="personal"]/mods:namePart',
65                                                         '//mods:mods/mods:name[mods:role/mods:text/text()="creator"]/mods:namePart',
66                                         ],
67                                         fixup   => '
68                                                         do {
69                                                                 $text = lc($text);
70                                                                 $text =~ s/\s+/ /sgo;
71                                                                 $text =~ s/^\s*(.+)\s*$/$1/sgo;
72                                                                 $text =~ s/,?\s+.*$//sgo;
73                                                                 $text =~ s/\pM+//gso;
74                                                         };
75                                         ',
76                         },
77         ],
78
79         '//mods:mods/mods:relatedItem[@type!="host" and @type!="series"]' => [
80                         title   => {
81                                         xpath   => [
82                                                         '//mods:mods/mods:relatedItem/mods:titleInfo[mods:title and (@type="uniform")]',
83                                                         '//mods:mods/mods:relatedItem/mods:titleInfo[mods:title and (@type="translated")]',
84                                                         '//mods:mods/mods:relatedItem/mods:titleInfo[mods:title and (@type="alternative")]',
85                                                         '//mods:mods/mods:relatedItem/mods:titleInfo[mods:title and not(@type)]',
86                                                         '//mods:mods/mods:titleInfo[mods:title and (@type="uniform")]',
87                                                         '//mods:mods/mods:titleInfo[mods:title and (@type="translated")]',
88                                                         '//mods:mods/mods:titleInfo[mods:title and (@type="alternative")]',
89                                                         '//mods:mods/mods:titleInfo[mods:title and not(@type)]',
90                                         ],
91                                         fixup   => '
92                                                         do {
93                                                                 $text = lc($text);
94                                                                 $text =~ s/\s+/ /sgo;
95                                                                 $text =~ s/^\s*(.+)\s*$/$1/sgo;
96                                                                 $text =~ s/\b(?:the|an?)\b//sgo;
97                                                                 $text =~ s/\[.[^\]]+\]//sgo;
98                                                                 $text =~ s/\s*[;\/\.]*$//sgo;
99                                                                 $text =~ s/\pM+//gso;
100                                                         };
101                                         ',
102                         },
103                         author  => {
104                                         xpath   => [
105                                                         '//mods:mods/mods:relatedItem/mods:name[mods:role/mods:text/text()="creator" and @type="personal"]/mods:namePart',
106                                                         '//mods:mods/mods:relatedItem/mods:name[mods:role/mods:text/text()="creator"]/mods:namePart',
107                                                         '//mods:mods/mods:name[mods:role/mods:text/text()="creator" and @type="personal"]/mods:namePart',
108                                                         '//mods:mods/mods:name[mods:role/mods:text/text()="creator"]/mods:namePart',
109                                         ],
110                                         fixup   => '
111                                                         do {
112                                                                 $text = lc($text);
113                                                                 $text =~ s/\s+/ /sgo;
114                                                                 $text =~ s/^\s*(.+)\s*$/$1/sgo;
115                                                                 $text =~ s/,?\s+.*$//sgo;
116                                                                 $text =~ s/\pM+//gso;
117                                                         };
118                                         ',
119                         },
120         ],
121
122 );
123
124 push @fp_mods_xpath, '//mods:mods/mods:titleInfo' => $fp_mods_xpath[1];
125
126 sub fingerprint_mods {
127         my $mods = shift;
128
129         my $fp_string = '';
130
131         my $match_index = 0;
132         my $block_index = 1;
133         while ( my $match_xpath = $fp_mods_xpath[$match_index] ) {
134                 if ( my @nodes = $mods->findnodes( $match_xpath ) ) {
135
136                         my $block_name_index = 0;
137                         my $block_value_index = 1;
138                         my $block = $fp_mods_xpath[$block_index];
139                         while ( my $part = $$block[$block_value_index] ) {
140                                 my $text;
141                                 for my $xpath ( @{ $part->{xpath} } ) {
142                                         $text = $mods->findvalue( $xpath );
143                                         last if ($text);
144                                 }
145
146                                 $log->debug("Found fingerprint text using $$block[$block_name_index] : [$text]", DEBUG);
147
148                                 if ($text) {
149                                         eval $$part{fixup};
150                                         $fp_string .= $text;
151                                 }
152
153                                 $block_name_index += 2;
154                                 $block_value_index += 2;
155                         }
156                 }
157                 if ($fp_string) {
158                         $fp_string =~ s/\W+//gso;
159                         $log->debug("Fingerprint is [$fp_string]", INFO);;
160                         return $fp_string;
161                 }
162
163                 $match_index += 2;
164                 $block_index += 2;
165         }
166         return undef;
167 }
168
169
170
171 # --------------------------------------------------------------------------------
172
173 my $in_xact;
174 my $begin;
175 my $commit;
176 my $rollback;
177 my $lookup;
178 my $update_entry;
179 my $mr_lookup;
180 my $mr_update;
181 my $mr_create;
182 my $create_source_map;
183 my $sm_lookup;
184 my $rm_old_rd;
185 my $rm_old_sm;
186 my $rm_old_fr;
187 my $rm_old_tr;
188 my $rm_old_ar;
189 my $rm_old_sr;
190 my $rm_old_kr;
191 my $rm_old_ser;
192
193 my $fr_create;
194 my $rd_create;
195 my $create = {};
196
197 my %descriptor_code = (
198         item_type => 'substr($ldr,6,1)',
199         item_form => '(substr($ldr,6,1) =~ /^(?:f|g|i|m|o|p|r)$/) ? substr($oo8,29,1) : substr($oo8,23,1)',
200         bib_level => 'substr($ldr,7,1)',
201         control_type => 'substr($ldr,8,1)',
202         char_encoding => 'substr($ldr,9,1)',
203         enc_level => 'substr($ldr,17,1)',
204         cat_form => 'substr($ldr,18,1)',
205         pub_status => 'substr($ldr,5,1)',
206         item_lang => 'substr($oo8,35,3)',
207         #lit_form => '(substr($ldr,6,1) =~ /^(?:f|g|i|m|o|p|r)$/) ? substr($oo8,33,1) : "0"',
208         audience => 'substr($oo8,22,1)',
209 );
210
211 sub wormize {
212
213         my $self = shift;
214         my $client = shift;
215         my @docids = @_;
216
217         my $no_map = 0;
218         if ($self->api_name =~ /no_map/o) {
219                 $no_map = 1;
220         }
221
222         $in_xact = $self->method_lookup( 'open-ils.storage.transaction.current')
223                 unless ($in_xact);
224         $begin = $self->method_lookup( 'open-ils.storage.transaction.begin')
225                 unless ($begin);
226         $commit = $self->method_lookup( 'open-ils.storage.transaction.commit')
227                 unless ($commit);
228         $rollback = $self->method_lookup( 'open-ils.storage.transaction.rollback')
229                 unless ($rollback);
230         $sm_lookup = $self->method_lookup('open-ils.storage.direct.metabib.metarecord_source_map.search.source')
231                 unless ($sm_lookup);
232         $mr_lookup = $self->method_lookup('open-ils.storage.direct.metabib.metarecord.search.fingerprint')
233                 unless ($mr_lookup);
234         $mr_update = $self->method_lookup('open-ils.storage.direct.metabib.metarecord.batch.update')
235                 unless ($mr_update);
236         $mr_create = $self->method_lookup('open-ils.storage.direct.metabib.metarecord.create')
237                 unless ($mr_create);
238         $create_source_map = $self->method_lookup('open-ils.storage.direct.metabib.metarecord_source_map.batch.create')
239                 unless ($create_source_map);
240         $lookup = $self->method_lookup('open-ils.storage.direct.biblio.record_entry.batch.retrieve')
241                 unless ($lookup);
242         $update_entry = $self->method_lookup('open-ils.storage.direct.biblio.record_entry.batch.update')
243                 unless ($update_entry);
244         $rm_old_sm = $self->method_lookup( 'open-ils.storage.direct.metabib.metarecord_source_map.mass_delete')
245                 unless ($rm_old_sm);
246         $rm_old_rd = $self->method_lookup( 'open-ils.storage.direct.metabib.record_descriptor.mass_delete')
247                 unless ($rm_old_rd);
248         $rm_old_fr = $self->method_lookup( 'open-ils.storage.direct.metabib.full_rec.mass_delete')
249                 unless ($rm_old_fr);
250         $rm_old_tr = $self->method_lookup( 'open-ils.storage.direct.metabib.title_field_entry.mass_delete')
251                 unless ($rm_old_tr);
252         $rm_old_ar = $self->method_lookup( 'open-ils.storage.direct.metabib.author_field_entry.mass_delete')
253                 unless ($rm_old_ar);
254         $rm_old_sr = $self->method_lookup( 'open-ils.storage.direct.metabib.subject_field_entry.mass_delete')
255                 unless ($rm_old_sr);
256         $rm_old_kr = $self->method_lookup( 'open-ils.storage.direct.metabib.keyword_field_entry.mass_delete')
257                 unless ($rm_old_kr);
258         $rm_old_ser = $self->method_lookup( 'open-ils.storage.direct.metabib.series_field_entry.mass_delete')
259                 unless ($rm_old_ser);
260         $rd_create = $self->method_lookup( 'open-ils.storage.direct.metabib.record_descriptor.batch.create')
261                 unless ($rd_create);
262         $fr_create = $self->method_lookup( 'open-ils.storage.direct.metabib.full_rec.batch.create')
263                 unless ($fr_create);
264         $$create{title} = $self->method_lookup( 'open-ils.storage.direct.metabib.title_field_entry.batch.create')
265                 unless ($$create{title});
266         $$create{author} = $self->method_lookup( 'open-ils.storage.direct.metabib.author_field_entry.batch.create')
267                 unless ($$create{author});
268         $$create{subject} = $self->method_lookup( 'open-ils.storage.direct.metabib.subject_field_entry.batch.create')
269                 unless ($$create{subject});
270         $$create{keyword} = $self->method_lookup( 'open-ils.storage.direct.metabib.keyword_field_entry.batch.create')
271                 unless ($$create{keyword});
272         $$create{series} = $self->method_lookup( 'open-ils.storage.direct.metabib.series_field_entry.batch.create')
273                 unless ($$create{series});
274
275
276         my ($outer_xact) = $in_xact->run;
277         try {
278                 unless ($outer_xact) {
279                         $log->debug("WoRM isn't inside a transaction, starting one now.", INFO);
280                         my ($r) = $begin->run($client);
281                         unless (defined $r and $r) {
282                                 $rollback->run;
283                                 throw OpenSRF::EX::PANIC ("Couldn't BEGIN transaction!")
284                         }
285                 }
286         } catch Error with {
287                 throw OpenSRF::EX::PANIC ("WoRM Couldn't BEGIN transaction!")
288         };
289
290         my @source_maps;
291         my @entry_list;
292         my @mr_list;
293         my @rd_list;
294         my @ns_list;
295         my @mods_data;
296         my $ret = 0;
297         for my $entry ( $lookup->run(@docids) ) {
298                 # step -1: grab the doc from storage
299                 next unless ($entry);
300
301                 if(!$mods_sheet) {
302                         my $xslt_doc = $parser->parse_file(
303                                 OpenSRF::Utils::SettingsClient->new->config_value(dirs => 'xsl') .  "/MARC21slim2MODS.xsl");
304                         $mods_sheet = $xslt->parse_stylesheet( $xslt_doc );
305                 }
306
307                 my $xml = $entry->marc;
308                 my $docid = $entry->id;
309                 my $marcdoc = $parser->parse_string($xml);
310                 my $modsdoc = $mods_sheet->transform($marcdoc);
311
312                 my $mods = $modsdoc->documentElement;
313                 $mods->setNamespace( "http://www.loc.gov/mods/", "mods", 1 );
314
315                 $entry->fingerprint( fingerprint_mods( $mods ) );
316                 push @entry_list, $entry;
317
318                 $log->debug("Fingerprint for Record Entry ".$docid." is [".$entry->fingerprint."]", INFO);
319
320                 unless ($no_map) {
321                         my ($mr) = $mr_lookup->run( $entry->fingerprint );
322                         if (!$mr || !@$mr) {
323                                 $log->debug("No metarecord found for fingerprint [".$entry->fingerprint."]; Creating a new one", INFO);
324                                 $mr = new Fieldmapper::metabib::metarecord;
325                                 $mr->fingerprint( $entry->fingerprint );
326                                 $mr->master_record( $entry->id );
327                                 my ($new_mr) = $mr_create->run($mr);
328                                 $mr->id($new_mr);
329                                 unless (defined $mr) {
330                                         throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.metarecord.create!")
331                                 }
332                         } else {
333                                 $log->debug("Retrieved metarecord, id is ".$mr->id, INFO);
334                                 $mr->mods('');
335                                 push @mr_list, $mr;
336                         }
337
338                         my $sm = new Fieldmapper::metabib::metarecord_source_map;
339                         $sm->metarecord( $mr->id );
340                         $sm->source( $entry->id );
341                         push @source_maps, $sm;
342                 }
343
344                 my $ldr = $marcdoc->documentElement->getChildrenByTagName('leader')->pop->textContent;
345                 my $oo8 = $marcdoc->documentElement->findvalue('//*[local-name()="controlfield" and @tag="008"]');
346
347                 my $rd_obj = Fieldmapper::metabib::record_descriptor->new;
348                 for my $rd_field ( keys %descriptor_code ) {
349                         $rd_obj->$rd_field( eval "$descriptor_code{$rd_field};" );
350                 }
351                 $rd_obj->record( $docid );
352                 push @rd_list, $rd_obj;
353
354                 push @mods_data, { $docid => $self->modsdoc_to_values( $mods ) };
355
356                 # step 2: build the KOHA rows
357                 my @tmp_list = _marcxml_to_full_rows( $marcdoc );
358                 $_->record( $docid ) for (@tmp_list);
359                 push @ns_list, @tmp_list;
360
361                 $ret++;
362
363                 last unless ($self->api_name =~ /batch$/o);
364         }
365
366         $rm_old_rd->run( { record => \@docids } );
367         $rm_old_fr->run( { record => \@docids } );
368         $rm_old_sm->run( { source => \@docids } ) unless ($no_map);
369         $rm_old_tr->run( { source => \@docids } );
370         $rm_old_ar->run( { source => \@docids } );
371         $rm_old_sr->run( { source => \@docids } );
372         $rm_old_kr->run( { source => \@docids } );
373         $rm_old_ser->run( { source => \@docids } );
374
375         unless ($no_map) {
376                 my ($sm) = $create_source_map->run(@source_maps);
377                 unless (defined $sm) {
378                         throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.metarecord_source_map.batch.create!")
379                 }
380                 my ($mr) = $mr_update->run(@mr_list);
381                 unless (defined $mr) {
382                         throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.metarecord.batch.update!")
383                 }
384         }
385
386         my ($re) = $update_entry->run(@entry_list);
387         unless (defined $re) {
388                 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.biblio.record_entry.batch.update!")
389         }
390
391         my ($rd) = $rd_create->run(@rd_list);
392         unless (defined $rd) {
393                 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.record_descriptor.batch.create!")
394         }
395
396         my ($fr) = $fr_create->run(@ns_list);
397         unless (defined $fr) {
398                 throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.full_rec.batch.create!")
399         }
400
401         # step 5: insert the new metadata
402         for my $class ( qw/title author subject keyword series/ ) {
403                 my @md_list = ();
404                 for my $doc ( @mods_data ) {
405                         my ($did) = keys %$doc;
406                         my ($data) = values %$doc;
407
408                         my $fm_constructor = "Fieldmapper::metabib::${class}_field_entry";
409                         for my $row ( keys %{ $$data{$class} } ) {
410                                 next unless (exists $$data{$class}{$row});
411                                 next unless ($$data{$class}{$row}{value});
412                                 my $fm_obj = $fm_constructor->new;
413                                 $fm_obj->value( $$data{$class}{$row}{value} );
414                                 $fm_obj->field( $$data{$class}{$row}{field_id} );
415                                 $fm_obj->source( $did );
416                                 $log->debug("$class entry: ".$fm_obj->source." => ".$fm_obj->field." : ".$fm_obj->value, DEBUG);
417
418                                 push @md_list, $fm_obj;
419                         }
420                 }
421                         
422                 my ($cr) = $$create{$class}->run(@md_list);
423                 unless (defined $cr) {
424                         throw OpenSRF::EX::PANIC ("Couldn't run open-ils.storage.direct.metabib.${class}_field_entry.batch.create!")
425                 }
426         }
427
428         unless ($outer_xact) {
429                 $log->debug("Commiting transaction started by the WoRM.", INFO);
430                 my ($c) = $commit->run;
431                 unless (defined $c and $c) {
432                         $rollback->run;
433                         throw OpenSRF::EX::PANIC ("Couldn't COMMIT changes!")
434                 }
435         }
436
437         return $ret;
438 }
439 __PACKAGE__->register_method( 
440         api_name        => "open-ils.worm.wormize",
441         method          => "wormize",
442         api_level       => 1,
443         argc            => 1,
444 );
445 __PACKAGE__->register_method( 
446         api_name        => "open-ils.worm.wormize.no_map",
447         method          => "wormize",
448         api_level       => 1,
449         argc            => 1,
450 );
451 __PACKAGE__->register_method( 
452         api_name        => "open-ils.worm.wormize.batch",
453         method          => "wormize",
454         api_level       => 1,
455         argc            => 1,
456 );
457 __PACKAGE__->register_method( 
458         api_name        => "open-ils.worm.wormize.no_map.batch",
459         method          => "wormize",
460         api_level       => 1,
461         argc            => 1,
462 );
463
464
465 # --------------------------------------------------------------------------------
466
467
468 sub _marcxml_to_full_rows {
469
470         my $marcxml = shift;
471
472         my @ns_list;
473         
474         my $root = $marcxml->documentElement;
475
476         for my $tagline ( @{$root->getChildrenByTagName("leader")} ) {
477                 next unless $tagline;
478
479                 my $ns = new Fieldmapper::metabib::full_rec;
480
481                 $ns->tag( 'LDR' );
482                 my $val = $tagline->textContent;
483                 $val =~ s/(\pM)//gso;
484                 $ns->value( $val );
485
486                 push @ns_list, $ns;
487         }
488
489         for my $tagline ( @{$root->getChildrenByTagName("controlfield")} ) {
490                 next unless $tagline;
491
492                 my $ns = new Fieldmapper::metabib::full_rec;
493
494                 $ns->tag( $tagline->getAttribute( "tag" ) );
495                 my $val = $tagline->textContent;
496                 $val =~ s/(\pM)//gso;
497                 $ns->value( $val );
498
499                 push @ns_list, $ns;
500         }
501
502         for my $tagline ( @{$root->getChildrenByTagName("datafield")} ) {
503                 next unless $tagline;
504
505                 my $tag = $tagline->getAttribute( "tag" );
506                 my $ind1 = $tagline->getAttribute( "ind1" );
507                 my $ind2 = $tagline->getAttribute( "ind2" );
508
509                 for my $data ( $tagline->childNodes ) {
510                         next unless $data;
511
512                         my $ns = new Fieldmapper::metabib::full_rec;
513
514                         $ns->tag( $tag );
515                         $ns->ind1( $ind1 );
516                         $ns->ind2( $ind2 );
517                         $ns->subfield( $data->getAttribute( "code" ) );
518                         my $val = $data->textContent;
519                         $val =~ s/(\pM)//gso;
520                         $ns->value( lc($val) );
521
522                         push @ns_list, $ns;
523                 }
524         }
525         return @ns_list;
526 }
527
528 sub _get_field_value {
529
530         my( $root, $xpath ) = @_;
531
532         my $string = "";
533
534         # grab the set of matching nodes
535         my @nodes = $root->findnodes( $xpath );
536         for my $value (@nodes) {
537
538                 # grab all children of the node
539                 my @children = $value->childNodes();
540                 for my $child (@children) {
541
542                         # add the childs content to the growing buffer
543                         my $content = quotemeta($child->textContent);
544                         next if ($string =~ /$content/);  # uniquify the values
545                         $string .= $child->textContent . " ";
546                 }
547                 if( ! @children ) {
548                         $string .= $value->textContent . " ";
549                 }
550         }
551         $string =~ s/(\pM)//gso;
552         return lc($string);
553 }
554
555
556 sub modsdoc_to_values {
557         my( $self, $mods ) = @_;
558         my $data = {};
559         for my $class (keys %$xpathset) {
560                 $data->{$class} = {};
561                 for my $type (keys %{$xpathset->{$class}}) {
562                         $data->{$class}->{$type} = {};
563                         $data->{$class}->{$type}->{value} = _get_field_value( $mods, $xpathset->{$class}->{$type}->{xpath} );
564                         $data->{$class}->{$type}->{field_id} = $xpathset->{$class}->{$type}->{id};
565                 }
566         }
567         return $data;
568 }
569
570
571 1;
572
573