patch to sysprefs and minor fix to Search.pm that
[koha.git] / C4 / ImportBatch.pm
1 package C4::ImportBatch;
2
3 # Copyright (C) 2007 LibLime
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use C4::Context;
22 use C4::Koha;
23 use C4::Biblio;
24 use C4::Matcher;
25 require Exporter;
26
27
28 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
29
30 # set the version for version checking
31 $VERSION = 3.00;
32
33 =head1 NAME
34
35 C4::ImportBatch - manage batches of imported MARC records
36
37 =head1 SYNOPSIS
38
39 =over 4
40
41 use C4::ImportBatch;
42
43 =back
44
45 =head1 FUNCTIONS
46
47 =cut
48
49 @ISA    = qw(Exporter);
50 @EXPORT = qw(
51     GetZ3950BatchId
52     GetImportRecordMarc
53     AddImportBatch
54     GetImportBatch
55     AddBiblioToBatch
56     ModBiblioInBatch
57
58     BatchStageMarcRecords
59     BatchFindBibDuplicates
60     BatchCommitBibRecords
61     BatchRevertBibRecords
62
63     GetImportBatchRangeDesc
64     GetNumberOfNonZ3950ImportBatches
65     GetImportBibliosRange
66     
67     GetImportBatchStatus
68     SetImportBatchStatus
69     GetImportBatchOverlayAction
70     SetImportBatchOverlayAction
71     GetImportRecordOverlayStatus
72     SetImportRecordOverlayStatus
73     GetImportRecordStatus
74     SetImportRecordStatus
75     GetImportRecordMatches
76     SetImportRecordMatches
77 );
78
79 =head2 GetZ3950BatchId
80
81 =over 4
82
83 my $batchid = GetZ3950BatchId($z3950server);
84
85 =back
86
87 Retrieves the ID of the import batch for the Z39.50
88 reservoir for the given target.  If necessary,
89 creates the import batch.
90
91 =cut
92
93 sub GetZ3950BatchId {
94     my ($z3950server) = @_;
95
96     my $dbh = C4::Context->dbh;
97     my $sth = $dbh->prepare("SELECT import_batch_id FROM import_batches
98                              WHERE  batch_type = 'z3950'
99                              AND    file_name = ?");
100     $sth->execute($z3950server);
101     my $rowref = $sth->fetchrow_arrayref();
102     $sth->finish();
103     if (defined $rowref) {
104         return $rowref->[0];
105     } else {
106         my $batch_id = AddImportBatch('create_new', 'staged', 'z3950', $z3950server, '');
107         return $batch_id;
108     }
109     
110 }
111
112 =head2 GetImportRecordMarc
113
114 =over4
115
116 my ($marcblob, $encoding) = GetImportRecordMarc($import_record_id);
117
118 =back
119
120 =cut
121
122 sub GetImportRecordMarc {
123     my ($import_record_id) = @_;
124
125     my $dbh = C4::Context->dbh;
126     my $sth = $dbh->prepare("SELECT marc, encoding FROM import_records WHERE import_record_id = ?");
127     $sth->execute($import_record_id);
128     my ($marc, $encoding) = $sth->fetchrow();
129     $sth->finish();
130     return $marc;
131
132 }
133
134 =head2 AddImportBatch
135
136 =over 4
137
138 my $batch_id = AddImportBatch($overlay_action, $import_status, $type, $file_name, $comments);
139
140 =back
141
142 =cut
143
144 sub AddImportBatch {
145     my ($overlay_action, $import_status, $type, $file_name, $comments) = @_;
146
147     my $dbh = C4::Context->dbh;
148     my $sth = $dbh->prepare("INSERT INTO import_batches (overlay_action, import_status, batch_type,
149                                                          file_name, comments)
150                                     VALUES (?, ?, ?, ?, ?)");
151     $sth->execute($overlay_action, $import_status, $type, $file_name, $comments);
152     my $batch_id = $dbh->{'mysql_insertid'};
153     $sth->finish();
154
155     return $batch_id;
156
157 }
158
159 =head2 GetImportBatch 
160
161 =over 4
162
163 my $row = GetImportBatch($batch_id);
164
165 =back
166
167 Retrieve a hashref of an import_batches row.
168
169 =cut
170
171 sub GetImportBatch {
172     my ($batch_id) = @_;
173
174     my $dbh = C4::Context->dbh;
175     my $sth = $dbh->prepare_cached("SELECT * FROM import_batches WHERE import_batch_id = ?");
176     $sth->bind_param(1, $batch_id);
177     $sth->execute();
178     my $result = $sth->fetchrow_hashref;
179     $sth->finish();
180     return $result;
181
182 }
183
184 =head2 AddBiblioToBatch 
185
186 =over 4
187
188 my $import_record_id = AddBiblioToBatch($batch_id, $record_sequence, $marc_record, $encoding, $z3950random, $update_counts);
189
190 =back
191
192 =cut
193
194 sub AddBiblioToBatch {
195     my $batch_id = shift;
196     my $record_sequence = shift;
197     my $marc_record = shift;
198     my $encoding = shift;
199     my $z3950random = shift;
200     my $update_counts = @_ ? shift : 1;
201
202     my $import_record_id = _create_import_record($batch_id, $record_sequence, $marc_record, 'biblio', $encoding, $z3950random);
203     _add_biblio_fields($import_record_id, $marc_record);
204     _update_batch_record_counts($batch_id) if $update_counts;
205     return $import_record_id;
206 }
207
208 =head2 ModBiblioInBatch
209
210 =over 4
211
212 ModBiblioInBatch($import_record_id, $marc_record);
213
214 =back
215
216 =cut
217
218 sub ModBiblioInBatch {
219     my ($import_record_id, $marc_record) = @_;
220
221     _update_import_record_marc($import_record_id, $marc_record);
222     _update_biblio_fields($import_record_id, $marc_record);
223
224 }
225
226 =head2 BatchStageMarcRecords
227
228 =over 4
229
230 ($batch_id, $num_records, @invalid_records) = BatchStageMarcRecords($marc_flavor, $marc_records, $file_name, 
231                                                                     $comments, $branch_code, $leave_as_staging);
232
233 =back
234
235 =cut
236
237 sub  BatchStageMarcRecords {
238     my ($marc_flavor, $marc_records, $file_name, $comments, $branch_code, $leave_as_staging) = @_;
239
240     my $batch_id = AddImportBatch('create_new', 'staging', 'batch', $file_name, $comments);
241     my @invalid_records = ();
242     my $num_valid = 0;
243     # FIXME - for now, we're dealing only with bibs
244     my $rec_num = 0;
245     foreach my $marc_blob (split(/\x1D/, $marc_records)) {
246         $rec_num++;
247         my $marc_record = FixEncoding($marc_blob, "\x1D");
248         my $import_record_id;
249         if (scalar($marc_record->fields()) == 0) {
250             push @invalid_records, $marc_blob;
251         } else {
252             $num_valid++;
253             $import_record_id = AddBiblioToBatch($batch_id, $rec_num, $marc_record, $marc_flavor, int(rand(99999)), 0);
254         }
255     }
256     unless ($leave_as_staging) {
257         SetImportBatchStatus($batch_id, 'staged');
258     }
259     # FIXME branch_code, number of bibs, number of items
260     _update_batch_record_counts($batch_id);
261     return ($batch_id, $num_valid, @invalid_records);
262 }
263
264 =head2 BatchFindBibDuplicates
265
266 =over4
267
268 my $num_with_matches = BatchFindBibDuplicates($batch_id, $matcher, $max_matches);
269
270 =back
271
272 Goes through the records loaded in the batch and attempts to 
273 find duplicates for each one.  Sets the overlay action to
274 'replace' if it was 'create_new', and sets the overlay status
275 of each record to 'no_match' or 'auto_match' as appropriate.
276
277 The $max_matches parameter is optional; if it is not supplied,
278 it defaults to 10.
279
280 =cut
281
282 sub BatchFindBibDuplicates {
283     my $batch_id = shift;
284     my $matcher = shift;
285     my $max_matches = @_ ? shift : 10;
286
287     my $dbh = C4::Context->dbh;
288     my $old_overlay_action = GetImportBatchOverlayAction($batch_id);
289     if ($old_overlay_action eq "create_new") {
290         SetImportBatchOverlayAction($batch_id, 'replace');
291     }
292
293     my $sth = $dbh->prepare("SELECT import_record_id, marc
294                              FROM import_records
295                              JOIN import_biblios USING (import_record_id)
296                              WHERE import_batch_id = ?");
297     $sth->execute($batch_id);
298     my $num_with_matches = 0;
299     while (my $rowref = $sth->fetchrow_hashref) {
300         my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
301         my @matches = $matcher->get_matches($marc_record, $max_matches);
302         if (scalar(@matches) > 0) {
303             $num_with_matches++;
304             SetImportRecordMatches($rowref->{'import_record_id'}, @matches);
305             SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'auto_match');
306         } else {
307             SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'no_match');
308         }
309     }
310     $sth->finish();
311     return $num_with_matches;
312 }
313
314 =head2 BatchCommitBibRecords
315
316 =over 4
317
318 my ($num_added, $num_updated, $num_ignored) = BatchCommitBibRecords($batch_id);
319
320 =back
321
322 =cut
323
324 sub BatchCommitBibRecords {
325     my $batch_id = shift;
326
327     my $num_added = 0;
328     my $num_updated = 0;
329     my $num_ignored = 0;
330     # commit (i.e., save, all records in the batch)
331     # FIXME biblio only at the moment
332     SetImportBatchStatus('importing');
333     my $overlay_action = GetImportBatchOverlayAction($batch_id);
334     my $dbh = C4::Context->dbh;
335     my $sth = $dbh->prepare("SELECT import_record_id, status, overlay_status, marc
336                              FROM import_records
337                              JOIN import_biblios USING (import_record_id)
338                              WHERE import_batch_id = ?");
339     $sth->execute($batch_id);
340     while (my $rowref = $sth->fetchrow_hashref) {
341         if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'imported') {
342             $num_ignored++;
343         }
344         my $marc_record = MARC::Record->new_from_usmarc($rowref->{'marc'});
345         if ($overlay_action eq 'create_new' or
346             ($overlay_action eq 'replace' and $rowref->{'overlay_status'} eq 'no_match')) {
347             $num_added++;
348             my ($biblionumber, $biblioitemnumber) = AddBiblio($marc_record, '');
349             my $sth = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
350             $sth->execute($biblionumber, $rowref->{'import_record_id'});
351             $sth->finish();
352             SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
353         } else {
354             $num_updated++;
355             my $biblionumber = GetBestRecordMatch($rowref->{'import_record_id'});
356             my ($count, $oldbiblio) = GetBiblio($biblionumber);
357             my $oldxml = GetXmlBiblio($biblionumber);
358             ModBiblio($marc_record, $biblionumber, $oldbiblio->{'frameworkcode'});
359             my $sth = $dbh->prepare_cached("UPDATE import_records SET marcxml_old = ? WHERE import_record_id = ?");
360             $sth->execute($oldxml, $rowref->{'import_record_id'});
361             $sth->finish();
362             my $sth2 = $dbh->prepare_cached("UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?");
363             $sth2->execute($biblionumber, $rowref->{'import_record_id'});
364             $sth2->finish();
365             SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied');
366             SetImportRecordStatus($rowref->{'import_record_id'}, 'imported');
367         }
368     }
369     $sth->finish();
370     SetImportBatchStatus($batch_id, 'imported');
371     return ($num_added, $num_updated, $num_ignored);
372 }
373
374 =head2 BatchRevertBibRecords
375
376 =over 4
377
378 my ($num_deleted, $num_errors, $num_reverted, $num_ignored) = BatchRevertBibRecords($batch_id);
379
380 =back
381
382 =cut
383
384 sub BatchRevertBibRecords {
385     my $batch_id = shift;
386
387     my $num_deleted = 0;
388     my $num_errors = 0;
389     my $num_reverted = 0;
390     my $num_ignored = 0;
391     # commit (i.e., save, all records in the batch)
392     # FIXME biblio only at the moment
393     SetImportBatchStatus('reverting');
394     my $overlay_action = GetImportBatchOverlayAction($batch_id);
395     my $dbh = C4::Context->dbh;
396     my $sth = $dbh->prepare("SELECT import_record_id, status, overlay_status, marcxml_old, encoding, matched_biblionumber
397                              FROM import_records
398                              JOIN import_biblios USING (import_record_id)
399                              WHERE import_batch_id = ?");
400     $sth->execute($batch_id);
401     while (my $rowref = $sth->fetchrow_hashref) {
402         if ($rowref->{'status'} eq 'error' or $rowref->{'status'} eq 'reverted') {
403             $num_ignored++;
404         }
405         if ($overlay_action eq 'create_new' or
406             ($overlay_action eq 'replace' and $rowref->{'overlay_status'} eq 'no_match')) {
407             my $error = DelBiblio($rowref->{'matched_biblionumber'});
408             if (defined $error) {
409                 $num_errors++;
410             } else {
411                 $num_deleted++;
412                 SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
413             }
414         } else {
415             $num_reverted++;
416             my $old_record = MARC::Record->new_from_xml($rowref->{'marcxml_old'}, 'UTF-8', $rowref->{'encoding'});
417             my $biblionumber = $rowref->{'matched_biblionumber'};
418             my ($count, $oldbiblio) = GetBiblio($biblionumber);
419             ModBiblio($old_record, $biblionumber, $oldbiblio->{'frameworkcode'});
420             SetImportRecordStatus($rowref->{'import_record_id'}, 'reverted');
421         }
422     }
423     $sth->finish();
424     SetImportBatchStatus($batch_id, 'reverted');
425     return ($num_deleted, $num_errors, $num_reverted, $num_ignored);
426 }
427
428 =head2 GetImportBatchRangeDesc
429
430 =over 4
431
432 my $results = GetImportBatchRangeDesc($offset, $results_per_group);
433
434 =back
435
436 Returns a reference to an array of hash references corresponding to
437 import_batches rows (sorted in descending order by import_batch_id)
438 start at the given offset.
439
440 =cut
441
442 sub GetImportBatchRangeDesc {
443     my ($offset, $results_per_group) = @_;
444
445     my $dbh = C4::Context->dbh;
446     my $sth = $dbh->prepare_cached("SELECT * FROM import_batches
447                                     WHERE batch_type = 'batch'
448                                     ORDER BY import_batch_id DESC
449                                     LIMIT ? OFFSET ?");
450     $sth->bind_param(1, $results_per_group);
451     $sth->bind_param(2, $offset);
452
453     my $results = [];
454     $sth->execute();
455     while (my $row = $sth->fetchrow_hashref) {
456         push @$results, $row;
457     }
458     $sth->finish();
459     return $results;
460 }
461
462 =head2 GetNumberOfImportBatches 
463
464 =over 4
465
466 my $count = GetNumberOfImportBatches();
467
468 =back
469
470 =cut
471
472 sub GetNumberOfNonZ3950ImportBatches {
473     my $dbh = C4::Context->dbh;
474     my $sth = $dbh->prepare("SELECT COUNT(*) FROM import_batches WHERE batch_type='batch'");
475     $sth->execute();
476     my ($count) = $sth->fetchrow_array();
477     $sth->finish();
478     return $count;
479 }
480
481 =head2 GetImportBibliosRange
482
483 =over 4
484
485 my $results = GetImportBibliosRange($batch_id, $offset, $results_per_group);
486
487 =back
488
489 Returns a reference to an array of hash references corresponding to
490 import_biblios/import_records rows for a given batch
491 starting at the given offset.
492
493 =cut
494
495 sub GetImportBibliosRange {
496     my ($batch_id, $offset, $results_per_group) = @_;
497
498     my $dbh = C4::Context->dbh;
499     my $sth = $dbh->prepare_cached("SELECT title, author, isbn, issn, import_record_id, record_sequence,
500                                            status, overlay_status
501                                     FROM   import_records
502                                     JOIN   import_biblios USING (import_record_id)
503                                     WHERE  import_batch_id = ?
504                                     ORDER BY import_record_id LIMIT ? OFFSET ?");
505     $sth->bind_param(1, $batch_id);
506     $sth->bind_param(2, $results_per_group);
507     $sth->bind_param(3, $offset);
508     my $results = [];
509     $sth->execute();
510     while (my $row = $sth->fetchrow_hashref) {
511         push @$results, $row;
512     }
513     $sth->finish();
514     return $results;
515
516 }
517
518 =head2 GetBestRecordMatch
519
520 =over 4
521
522 my $record_id = GetBestRecordMatch($import_record_id);
523
524 =back
525
526 =cut
527
528 sub GetBestRecordMatch {
529     my ($import_record_id) = @_;
530
531     my $dbh = C4::Context->dbh;
532     my $sth = $dbh->prepare("SELECT candidate_match_id
533                              FROM   import_record_matches
534                              WHERE  import_record_id = ?
535                              ORDER BY score DESC, candidate_match_id DESC");
536     $sth->execute($import_record_id);
537     my ($record_id) = $sth->fetchrow_array();
538     $sth->finish();
539     return $record_id;
540 }
541
542 =head2 GetImportBatchStatus
543
544 =over 4
545
546 my $status = GetImportBatchStatus($batch_id);
547
548 =back
549
550 =cut
551
552 sub GetImportBatchStatus {
553     my ($batch_id) = @_;
554
555     my $dbh = C4::Context->dbh;
556     my $sth = $dbh->prepare("SELECT import_status FROM import_batches WHERE batch_id = ?");
557     $sth->execute($batch_id);
558     my ($status) = $sth->fetchrow_array();
559     $sth->finish();
560     return;
561
562 }
563
564
565 =head2 SetImportBatchStatus
566
567 =over 4
568
569 SetImportBatchStatus($batch_id, $new_status);
570
571 =back
572
573 =cut
574
575 sub SetImportBatchStatus {
576     my ($batch_id, $new_status) = @_;
577
578     my $dbh = C4::Context->dbh;
579     my $sth = $dbh->prepare("UPDATE import_batches SET import_status = ? WHERE import_batch_id = ?");
580     $sth->execute($new_status, $batch_id);
581     $sth->finish();
582
583 }
584
585 =head2 GetImportBatchOverlayAction
586
587 =over 4
588
589 my $overlay_action = GetImportBatchOverlayAction($batch_id);
590
591 =back
592
593 =cut
594
595 sub GetImportBatchOverlayAction {
596     my ($batch_id) = @_;
597
598     my $dbh = C4::Context->dbh;
599     my $sth = $dbh->prepare("SELECT overlay_action FROM import_batches WHERE import_batch_id = ?");
600     $sth->execute($batch_id);
601     my ($overlay_action) = $sth->fetchrow_array();
602     $sth->finish();
603     return $overlay_action;
604
605 }
606
607
608 =head2 SetImportBatchOverlayAction
609
610 =over 4
611
612 SetImportBatchOverlayAction($batch_id, $new_overlay_action);
613
614 =back
615
616 =cut
617
618 sub SetImportBatchOverlayAction {
619     my ($batch_id, $new_overlay_action) = @_;
620
621     my $dbh = C4::Context->dbh;
622     my $sth = $dbh->prepare("UPDATE import_batches SET overlay_action = ? WHERE import_batch_id = ?");
623     $sth->execute($new_overlay_action, $batch_id);
624     $sth->finish();
625
626 }
627
628 =head2 GetImportRecordOverlayStatus
629
630 =over 4
631
632 my $overlay_status = GetImportRecordOverlayStatus($import_record_id);
633
634 =back
635
636 =cut
637
638 sub GetImportRecordOverlayStatus {
639     my ($import_record_id) = @_;
640
641     my $dbh = C4::Context->dbh;
642     my $sth = $dbh->prepare("SELECT overlay_status FROM import_records WHERE import_record_id = ?");
643     $sth->execute($import_record_id);
644     my ($overlay_status) = $sth->fetchrow_array();
645     $sth->finish();
646     return $overlay_status;
647
648 }
649
650
651 =head2 SetImportRecordOverlayStatus
652
653 =over 4
654
655 SetImportRecordOverlayStatus($import_record_id, $new_overlay_status);
656
657 =back
658
659 =cut
660
661 sub SetImportRecordOverlayStatus {
662     my ($import_record_id, $new_overlay_status) = @_;
663
664     my $dbh = C4::Context->dbh;
665     my $sth = $dbh->prepare("UPDATE import_records SET overlay_status = ? WHERE import_record_id = ?");
666     $sth->execute($new_overlay_status, $import_record_id);
667     $sth->finish();
668
669 }
670
671 =head2 GetImportRecordStatus
672
673 =over 4
674
675 my $overlay_status = GetImportRecordStatus($import_record_id);
676
677 =back
678
679 =cut
680
681 sub GetImportRecordStatus {
682     my ($import_record_id) = @_;
683
684     my $dbh = C4::Context->dbh;
685     my $sth = $dbh->prepare("SELECT status FROM import_records WHERE import_record_id = ?");
686     $sth->execute($import_record_id);
687     my ($overlay_status) = $sth->fetchrow_array();
688     $sth->finish();
689     return $overlay_status;
690
691 }
692
693
694 =head2 SetImportRecordStatus
695
696 =over 4
697
698 SetImportRecordStatus($import_record_id, $new_overlay_status);
699
700 =back
701
702 =cut
703
704 sub SetImportRecordStatus {
705     my ($import_record_id, $new_overlay_status) = @_;
706
707     my $dbh = C4::Context->dbh;
708     my $sth = $dbh->prepare("UPDATE import_records SET status = ? WHERE import_record_id = ?");
709     $sth->execute($new_overlay_status, $import_record_id);
710     $sth->finish();
711
712 }
713
714 =head2 GetImportRecordMatches
715
716 =over 4
717
718 my $results = GetImportRecordMatches($import_record_id, $best_only);
719
720 =back
721
722 =cut
723
724 sub GetImportRecordMatches {
725     my $import_record_id = shift;
726     my $best_only = @_ ? shift : 0;
727
728     my $dbh = C4::Context->dbh;
729     # FIXME currently biblio only
730     my $sth = $dbh->prepare_cached("SELECT title, author, biblionumber, score
731                                     FROM import_records
732                                     JOIN import_record_matches USING (import_record_id)
733                                     JOIN biblio ON (biblionumber = candidate_match_id)
734                                     WHERE import_record_id = ?
735                                     ORDER BY score DESC, biblionumber DESC");
736     $sth->bind_param(1, $import_record_id);
737     my $results = [];
738     $sth->execute();
739     while (my $row = $sth->fetchrow_hashref) {
740         push @$results, $row;
741         last if $best_only;
742     }
743     $sth->finish();
744
745     return $results;
746     
747 }
748
749
750 =head2 SetImportRecordMatches
751
752 =over 4
753
754 SetImportRecordMatches($import_record_id, @matches);
755
756 =back
757
758 =cut
759
760 sub SetImportRecordMatches {
761     my $import_record_id = shift;
762     my @matches = @_;
763
764     my $dbh = C4::Context->dbh;
765     my $delsth = $dbh->prepare("DELETE FROM import_record_matches WHERE import_record_id = ?");
766     $delsth->execute($import_record_id);
767     $delsth->finish();
768
769     my $sth = $dbh->prepare("INSERT INTO import_record_matches (import_record_id, candidate_match_id, score)
770                                     VALUES (?, ?, ?)");
771     foreach my $match (@matches) {
772         $sth->execute($import_record_id, $match->{'record_id'}, $match->{'score'});
773     }
774 }
775
776
777 # internal functions
778
779 sub _create_import_record {
780     my ($batch_id, $record_sequence, $marc_record, $record_type, $encoding, $z3950random) = @_;
781     
782     my $dbh = C4::Context->dbh;
783     my $sth = $dbh->prepare("INSERT INTO import_records (import_batch_id, record_sequence, marc, marcxml, 
784                                                          record_type, encoding, z3950random)
785                                     VALUES (?, ?, ?, ?, ?, ?, ?)");
786     $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml(),
787                   $record_type, $encoding, $z3950random);
788     my $import_record_id = $dbh->{'mysql_insertid'};
789     $sth->finish();
790     return $import_record_id;
791 }
792
793 sub _update_import_record_marc {
794     my ($import_record_id, $marc_record) = @_;
795
796     my $dbh = C4::Context->dbh;
797     my $sth = $dbh->prepare("UPDATE import_records SET marc = ?, marcxml = ?
798                              WHERE  import_record_id = ?");
799     $sth->execute($marc_record->as_usmarc(), $marc_record->as_xml(), $import_record_id);
800     $sth->finish();
801 }
802
803 sub _add_biblio_fields {
804     my ($import_record_id, $marc_record) = @_;
805
806     my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
807     my $dbh = C4::Context->dbh;
808     # FIXME no controlnumber, originalsource
809     # FIXME 2 - should regularize normalization of ISBN wherever it is done
810     $isbn =~ s/\(.*$//;
811     $isbn =~ tr/ -_//;  
812     $isbn = uc $isbn;
813     my $sth = $dbh->prepare("INSERT INTO import_biblios (import_record_id, title, author, isbn, issn) VALUES (?, ?, ?, ?, ?)");
814     $sth->execute($import_record_id, $title, $author, $isbn, $issn);
815     $sth->finish();
816                 
817 }
818
819 sub _update_biblio_fields {
820     my ($import_record_id, $marc_record) = @_;
821
822     my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
823     my $dbh = C4::Context->dbh;
824     # FIXME no controlnumber, originalsource
825     # FIXME 2 - should regularize normalization of ISBN wherever it is done
826     $isbn =~ s/\(.*$//;
827     $isbn =~ tr/ -_//;
828     $isbn = uc $isbn;
829     my $sth = $dbh->prepare("UPDATE import_biblios SET title = ?, author = ?, isbn = ?, issn = ?
830                              WHERE  import_record_id = ?");
831     $sth->execute($title, $author, $isbn, $issn, $import_record_id);
832     $sth->finish();
833 }
834
835 sub _parse_biblio_fields {
836     my ($marc_record) = @_;
837
838     my $dbh = C4::Context->dbh;
839     my $bibliofields = TransformMarcToKoha($dbh, $marc_record, '');
840     return ($bibliofields->{'title'}, $bibliofields->{'author'}, $bibliofields->{'isbn'}, $bibliofields->{'issn'});
841
842 }
843
844 sub _update_batch_record_counts {
845     my ($batch_id) = @_;
846
847     my $dbh = C4::Context->dbh;
848     my $sth = $dbh->prepare_cached("UPDATE import_batches SET num_biblios = (
849                                     SELECT COUNT(*)
850                                     FROM import_records
851                                     WHERE import_batch_id = import_batches.import_batch_id
852                                     AND record_type = 'biblio')
853                                     WHERE import_batch_id = ?");
854     $sth->bind_param(1, $batch_id);
855     $sth->execute();
856     $sth->finish();
857
858 }
859
860 1;
861
862 =head1 AUTHOR
863
864 Koha Development Team <info@koha.org>
865
866 Galen Charlton <galen.charlton@liblime.com>
867
868 =cut