82d729481be6fc4cc6f48412f6369c24082ac51a
[koha.git] / misc / maintenance / remove_items_from_biblioitems.pl
1 #!/usr/bin/perl
2
3 # Copyright 2010 BibLibre
4 # Copyright 2011 Equinox Software, Inc.
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use strict;
22 use warnings;
23 $|=1;
24
25 use Koha::Script;
26 use C4::Context;
27 use C4::Biblio qw( GetFrameworkCode ModBiblio );
28 use Koha::Biblios;
29 use Getopt::Long qw( GetOptions );
30
31 my ($wherestring, $run, $silent, $want_help);
32 my $result = GetOptions(
33     'where:s'      => \$wherestring,
34     '--run'        => \$run,
35     '--silent'     => \$silent,
36     'help|h'       => \$want_help,
37 );
38
39 if ( not $result or not $run or $want_help ) {
40     print_usage();
41     exit 0;
42 }
43
44 my $dbh = C4::Context->dbh;
45 my $count = 0;
46 my $querysth =  qq{SELECT biblionumber from biblioitems };
47 $querysth    .= " WHERE $wherestring " if ($wherestring);
48 my $query = $dbh->prepare($querysth);
49 $query->execute;
50 while (my $biblionumber = $query->fetchrow){
51     $count++;
52     print "." unless $silent;
53     print "\r$count" unless ($silent or ($count % 100)); 
54     my $biblio = Koha::Biblios->find($biblionumber);
55     my $record = $biblio->metadata->record;
56
57     if ($record) {
58         ModBiblio($record, $biblionumber, GetFrameworkCode($biblionumber)) ;
59     }
60     else {
61         print "error in $biblionumber : can't parse biblio";
62     }
63 }
64
65 print "\n\n$count records processed.\n" unless $silent;
66
67 sub print_usage {
68     print <<_USAGE_;
69 $0: removes items from selected biblios
70
71 This utility is meant to be run as part of the upgrade to
72 Koha 3.4.  It removes the 9XX fields in the bib records that
73 store a (now) duplicate copy of the item record information.  After
74 running this utility, a full reindexing of the bibliographic records
75 should be run using rebuild_zebra.pl -b -r.
76
77 Parameters:
78     -where                  use this to limit modifications to selected biblios
79     --run                   perform the update
80     --silent                run silently
81     --help or -h            show this message
82 _USAGE_
83 }