Adding a few new languages and more updates
[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             # delete any item tags
89             my ($itemtag, $itemsubfield) = GetMarcFromKohaField("items.itemnumber", '');
90             foreach my $field ($bib->field($itemtag)) {
91                 $bib->delete_field($field);
92             }
93             ModBiblio($bib, $biblionumber, GetFrameworkCode($biblionumber));
94             $num_bibs_modified++;
95         }
96     }
97 }
98
99 sub print_progress_and_commit {
100     my $recs = shift;
101     $dbh->commit();
102     print "... processed $recs records\n";
103 }
104
105 sub print_usage {
106     print <<_USAGE_;
107 $0: link headings in bib records to authorities.
108
109 This batch job checks each bib record in the Koha
110 database and attempts to link each of its headings
111 to the matching authority record.
112
113 Parameters:
114     --verbose               print the number of headings changed
115                             for each bib
116     --test                  only test the authority linking
117                             and report the results; do not
118                             change the bib records.
119     --help or -h            show this message.
120 _USAGE_
121 }