Usability change: Added example of 5.00 to fines/manual credit and fines/manual invoi...
[koha.git] / misc / link_bibs_to_authorities.pl
blob9954fd66bf45848903090b22835b2c8c82f9f088
1 #!/usr/bin/perl
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" };
11 use C4::Context;
12 use C4::Biblio;
13 use Getopt::Long;
15 $| = 1;
17 # command-line parameters
18 my $verbose = 0;
19 my $test_only = 0;
20 my $want_help = 0;
22 my $result = GetOptions(
23 'verbose' => \$verbose,
24 'test' => \$test_only,
25 'h|help' => \$want_help
28 if (not $result or $want_help) {
29 print_usage();
30 exit 0;
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();
41 exit 0;
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);
51 if (not $test_only and ($num_bibs_processed % 100) == 0) {
52 print_progress_and_commit($num_bibs_processed);
56 if (not $test_only) {
57 $dbh->commit;
60 print <<_SUMMARY_;
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_
70 sub process_bib {
71 my $biblionumber = shift;
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;
80 my $headings_changed = LinkBibHeadingsToAuthorities($bib);
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";
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);
93 ModBiblio($bib, $biblionumber, GetFrameworkCode($biblionumber));
94 $num_bibs_modified++;
99 sub print_progress_and_commit {
100 my $recs = shift;
101 $dbh->commit();
102 print "... processed $recs records\n";
105 sub print_usage {
106 print <<_USAGE_;
107 $0: link headings in bib records to authorities.
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.
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_