Bug 8233 : SearchEngine: Add a Koha::SearchEngine module
[koha.git] / misc / migration_tools / create_analytical_rel.pl
blob7c970b5d2885d1ff0907876a5c95436a4852e9ae
1 #!/usr/bin/perl
3 use strict;
4 #use warnings; FIXME - Bug 2505
5 BEGIN {
6 # find Koha's Perl modules
7 # test carefully before changing this
8 use FindBin;
9 eval { require "$FindBin::Bin/../kohalib.pl" };
12 use C4::Context;
13 use C4::Biblio;
14 use C4::Items;
15 use Getopt::Long;
17 $| = 1;
19 # command-line parameters
20 my $want_help = 0;
21 my $do_update = 0;
22 my $wherestrings;
24 my $result = GetOptions(
25 'run-update' => \$do_update,
26 'where=s@' => \$wherestrings,
27 'h|help' => \$want_help,
30 if (not $result or $want_help or not $do_update) {
31 print_usage();
32 exit 0;
35 my $num_bibs_processed = 0;
36 my $num_bibs_modified = 0;
37 my $num_nobib_foritemnumber = 0;
38 my $num_noitem_forbarcode = 0;
39 my $num_nobarcode_inhostfield =0;
40 my $num_hostfields_unabletomodify =0;
41 my $num_bad_bibs = 0;
42 my $dbh = C4::Context->dbh;
43 $dbh->{AutoCommit} = 0;
45 process_bibs();
46 $dbh->commit();
48 exit 0;
50 sub process_bibs {
51 my $sql = "SELECT biblionumber FROM biblio JOIN biblioitems USING (biblionumber)";
52 $sql.="WHERE ". join(" AND ",@$wherestrings) if ($wherestrings);
53 $sql.="ORDER BY biblionumber ASC";
54 my $sth = $dbh->prepare($sql);
55 eval{$sth->execute();};
56 if ($@){ die "error $@";};
57 while (my ($biblionumber) = $sth->fetchrow_array()) {
58 $num_bibs_processed++;
59 process_bib($biblionumber);
61 if (($num_bibs_processed % 100) == 0) {
62 print_progress_and_commit($num_bibs_processed);
66 $dbh->commit;
68 print <<_SUMMARY_;
70 Create Analytical records relationships report
71 -----------------------------------------------
72 Number of bibs checked: $num_bibs_processed
73 Number of bibs modified: $num_bibs_modified
74 Number of hostfields with no barcodes: $num_nobarcode_inhostfield
75 Number of barcodes not found: $num_noitem_forbarcode
76 Number of hostfields unable to modify: $num_hostfields_unabletomodify
77 Number of bibs with errors: $num_bad_bibs
78 _SUMMARY_
81 sub process_bib {
82 my $biblionumber = shift;
84 my $bib = GetMarcBiblio($biblionumber);
85 unless (defined $bib) {
86 print "\nCould not retrieve bib $biblionumber from the database - record is corrupt.\n";
87 $num_bad_bibs++;
88 return;
90 #loop through each host field and populate subfield 0 and 9
91 my $analyticfield = '773';
92 foreach my $hostfield ( $bib->field($analyticfield) ) {
93 if(my $barcode = $hostfield->subfield('o')){
94 my $itemnumber = GetItemnumberFromBarcode($barcode);
95 if ($itemnumber ne undef){
96 my $bibnumber = GetBiblionumberFromItemnumber($itemnumber);
97 if ($bibnumber ne undef){
98 my $modif;
99 if ($hostfield->subfield('0') ne $bibnumber){
100 $hostfield->update('0', $bibnumber);
101 $modif = 1;
103 if ($hostfield->subfield('9') ne $itemnumber){
104 $hostfield->update('9', $itemnumber);
105 $modif=1;
107 if ($modif){
108 $num_bibs_modified++;
109 my $modresult = ModBiblio($bib, $biblionumber, '');
110 warn "Modifying biblio $biblionumber";
111 if (!$modresult){
112 warn "Unable to modify biblio $biblionumber with update host field";
113 $num_hostfields_unabletomodify++;
116 } else {
117 warn "No biblio record found corressponding to itemnumber $itemnumber";
118 $num_nobib_foritemnumber++;
120 } else {
121 warn "No item record found for barcode $barcode";
122 $num_noitem_forbarcode++;
124 } else{
125 warn "No barcode in host field for biblionumber $biblionumber";
126 $num_nobarcode_inhostfield++;
131 sub print_progress_and_commit {
132 my $recs = shift;
133 $dbh->commit();
134 print "... processed $recs records\n";
137 sub print_usage {
138 print <<_USAGE_;
139 $0: establish relationship to host items
141 Based on barcode in host field populates subfield 0 with host biblionumber and subfield 9 with host itemnumber.
143 Subfield 0 and 9 are used in Koha screns to display relationships between analytical records and host bibs and items.
145 NOT usable with UNIMARC data. You can use it only if you have tag 461 with also an items id (like barcode or item numbers). In UNIMARC this situation is very rare. If you have data coded in this way, send a mail to koha-dev mailing list and ask for the feature.
147 Parameters:
148 --run-update run the synchronization
149 --where condition selects the biblios on a criterium (Repeatable)
150 --help or -h show this message.
151 _USAGE_