authorities: new batch job to link bibs to authorities
[koha-equinox.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 $dbh = C4::Context->dbh;
36 $dbh->{AutoCommit} = 0;
37 process_bibs();
38 $dbh->commit();
39
40 exit 0;
41
42 sub process_bibs {
43     my $sql = "SELECT biblionumber FROM biblio ORDER BY biblionumber ASC";
44     my $sth = $dbh->prepare($sql);
45     $sth->execute();
46     while (my ($biblionumber) = $sth->fetchrow_array()) {
47         $num_bibs_processed++;
48         process_bib($biblionumber);
49
50         if (not $test_only and ($num_bibs_processed % 100) == 0) {
51             print_progress_and_commit($num_bibs_processed);
52         }
53     }
54
55     if (not $test_only) {
56         $dbh->commit;
57     }
58
59     print <<_SUMMARY_;
60
61 Bib authority heading linking report
62 ------------------------------------
63 Number of bibs checked:       $num_bibs_processed
64 Number of bibs modified:      $num_bibs_modified
65 _SUMMARY_
66 }
67
68 sub process_bib {
69     my $biblionumber = shift;
70
71     my $bib = GetMarcBiblio($biblionumber);
72     my $headings_changed = LinkBibHeadingsToAuthorities($bib);
73
74     if ($headings_changed) {   
75         if ($verbose) {
76             my $title = substr($bib->title, 0, 20);
77             print "Bib $biblionumber ($title): $headings_changed headings changed\n";
78         }
79         if (not $test_only) {
80             ModBiblio($bib, $biblionumber, GetFrameworkCode($biblionumber));
81             $num_bibs_modified++;
82         }
83     }
84 }
85
86 sub print_progress_and_commit {
87     my $recs = shift;
88     $dbh->commit();
89     print "... processed $recs records\n";
90 }
91
92 sub print_usage {
93     print <<_USAGE_;
94 $0: link headings in bib records to authorities.
95
96 This batch job checks each bib record in the Koha
97 database and attempts to link each of its headings
98 to the matching authority record.
99
100 Parameters:
101     --verbose               print the number of headings changed
102                             for each bib
103     --test                  only test the authority linking
104                             and report the results; do not
105                             change the bib records.
106     --comment <comment>     optional comment to describe
107                             the record batch; if the comment
108                             has spaces in it, surround the
109                             comment with quotation marks.
110     --help or -h            show this message.
111 _USAGE_
112 }