bypass corrupt bibs when linking headings
[koha.git] / misc / link_bibs_to_authorities.pl
1 #!/usr/bin/perl
2
3 use strict;
4 BEGIN {
5     # find Koha's Perl modules
6     # test carefully before changing this
7     use FindBin;
8     eval { require "$FindBin::Bin/kohalib.pl" };
9 }
10
11 use C4::Context;
12 use C4::Biblio;
13 use Getopt::Long;
14
15 $| = 1;
16
17 # command-line parameters
18 my $verbose   = 0;
19 my $test_only = 0;
20 my $want_help = 0;
21
22 my $result = GetOptions(
23     'verbose'       => \$verbose,
24     'test'          => \$test_only,
25     'h|help'        => \$want_help
26 );
27
28 if (not $result or $want_help) {
29     print_usage();
30     exit 0;
31 }
32
33 my $num_bibs_processed = 0;
34 my $num_bibs_modified = 0;
35 my $num_bad_bibs = 0;
36 my $dbh = C4::Context->dbh;
37 $dbh->{AutoCommit} = 0;
38 process_bibs();
39 $dbh->commit();
40
41 exit 0;
42
43 sub process_bibs {
44     my $sql = "SELECT biblionumber FROM biblio ORDER BY biblionumber ASC";
45     my $sth = $dbh->prepare($sql);
46     $sth->execute();
47     while (my ($biblionumber) = $sth->fetchrow_array()) {
48         $num_bibs_processed++;
49         process_bib($biblionumber);
50
51         if (not $test_only and ($num_bibs_processed % 100) == 0) {
52             print_progress_and_commit($num_bibs_processed);
53         }
54     }
55
56     if (not $test_only) {
57         $dbh->commit;
58     }
59
60     print <<_SUMMARY_;
61
62 Bib authority heading linking report
63 ------------------------------------
64 Number of bibs checked:       $num_bibs_processed
65 Number of bibs modified:      $num_bibs_modified
66 Number of bibs with errors:   $num_bad_bibs
67 _SUMMARY_
68 }
69
70 sub process_bib {
71     my $biblionumber = shift;
72
73     my $bib = GetMarcBiblio($biblionumber);
74     unless (defined $bib) {
75         print "\nCould not retrieve bib $biblionumber from the database - record is corrupt.\n";
76         $num_bad_bibs++;
77         return;
78     }
79
80     my $headings_changed = LinkBibHeadingsToAuthorities($bib);
81
82     if ($headings_changed) {   
83         if ($verbose) {
84             my $title = substr($bib->title, 0, 20);
85             print "Bib $biblionumber ($title): $headings_changed headings changed\n";
86         }
87         if (not $test_only) {
88             ModBiblio($bib, $biblionumber, GetFrameworkCode($biblionumber));
89             $num_bibs_modified++;
90         }
91     }
92 }
93
94 sub print_progress_and_commit {
95     my $recs = shift;
96     $dbh->commit();
97     print "... processed $recs records\n";
98 }
99
100 sub print_usage {
101     print <<_USAGE_;
102 $0: link headings in bib records to authorities.
103
104 This batch job checks each bib record in the Koha
105 database and attempts to link each of its headings
106 to the matching authority record.
107
108 Parameters:
109     --verbose               print the number of headings changed
110                             for each bib
111     --test                  only test the authority linking
112                             and report the results; do not
113                             change the bib records.
114     --comment <comment>     optional comment to describe
115                             the record batch; if the comment
116                             has spaces in it, surround the
117                             comment with quotation marks.
118     --help or -h            show this message.
119 _USAGE_
120 }