Bug 30477: Add new UNIMARC installer translation files
[koha.git] / misc / maintenance / sanitize_records.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2014 BibLibre
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use Koha::Script;
23 use C4::Charset;
24 use C4::Context;
25 use C4::Biblio;
26 use Getopt::Long qw( GetOptions );
27 use Pod::Usage qw( pod2usage );
28
29 my ( $help, $verbose, $confirm, $biblionumbers, $reindex, $filename,
30     $auto_search, $fix_ampersand );
31 my $result = GetOptions(
32     'h|help'          => \$help,
33     'v|verbose'       => \$verbose,
34     'c|confirm'       => \$confirm,
35     'biblionumbers:s' => \$biblionumbers,
36     'reindex'         => \$reindex,
37     'f|filename:s'    => \$filename,
38     'auto-search'     => \$auto_search,
39     'fix-ampersand'   => \$fix_ampersand,
40 ) || pod2usage(1);
41
42 # This script only fix ampersand at the moment.
43 # It is enabled by default.
44 $fix_ampersand = 1;
45
46 if ($help) {
47     pod2usage(0);
48 }
49
50 unless ( $filename or $biblionumbers or $auto_search ) {
51     pod2usage(
52         -exitval => 1,
53         -message =>
54           qq{\n\tAt least one record number source should be provided.\n}
55     );
56 }
57
58 if (   $filename and $biblionumbers
59     or $filename and $auto_search
60     or $biblionumbers and $auto_search )
61 {
62     pod2usage(
63         -exitval => 1,
64         -message => qq{\n\tOnly one record number source should be provided.\n}
65     );
66 }
67
68 my @biblionumbers;
69
70 # We first detect if we have a file or biblos directly entered by command line
71 #or if we want to use findAmp() sub
72 if ($auto_search) {
73     @biblionumbers = biblios_to_sanitize();
74 }
75 elsif ($filename) {
76     if ( -e $filename ) {
77         open( my $fh, '<', $filename ) || die("Can't open $filename ($!)");
78         while (<$fh>) {
79             chomp;
80             my $line = $_;
81             push @biblionumbers, split( " |,", $line );
82         }
83         close $fh;
84     }
85     else {
86         pod2usage(
87             -exitval => 1,
88             -message =>
89 qq{\n\tThis filename does not exist. Please verify the path is correct.\n}
90         );
91     }
92 }
93 else {
94     @biblionumbers = split m|,|, $biblionumbers if $biblionumbers;
95 }
96
97 # We remove spaces
98 s/(^\s*|\s*$)//g for @biblionumbers;
99
100 # Remove empty lines
101 @biblionumbers = grep { !/^$/ } @biblionumbers;
102
103 say @biblionumbers . " records to process" if $verbose;
104
105 my @changes;
106 for my $biblionumber (@biblionumbers) {
107     print "processing record $biblionumber..." if $verbose;
108     unless ( $biblionumber =~ m|^\d+$| ) {
109         say " skipping. ERROR: Invalid biblionumber." if $verbose;
110         next;
111     }
112     my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
113     unless ($record) {
114         say " skipping. ERROR: Invalid record." if $verbose;
115         next;
116     }
117
118     my ( $cleaned_record, $has_been_modified ) =
119       C4::Charset::SanitizeRecord( $record, $biblionumber );
120     if ($has_been_modified) {
121         my $frameworkcode = C4::Biblio::GetFrameworkCode($record);
122
123         C4::Biblio::ModBiblio( $cleaned_record, $biblionumber, $frameworkcode )
124             if $confirm;
125         push @changes, $biblionumber;
126         say " Done!" if $verbose;
127     }
128     else {
129         say " Nothing to do." if $verbose;
130     }
131 }
132
133 if ($verbose) {
134     say "Total: "
135       . @changes
136       . " records "
137       . ( $confirm ? "cleaned!" : "to clean." );
138 }
139
140 if ( $reindex and $confirm and @changes ) {
141     say "Now, reindexing using -b -v" if $verbose;
142     my $kohapath = C4::Context->config('intranetdir');
143     my $cmd      = qq|
144         $kohapath/misc/migration_tools/rebuild_zebra.pl -b -v -where "biblionumber IN ( |
145       . join( ',', @changes ) . q| )"
146     |;
147     system($cmd);
148 }
149
150 sub biblios_to_sanitize {
151     my $dbh   = C4::Context->dbh;
152     my $query = q{
153         SELECT biblionumber
154         FROM biblio_metadata
155         WHERE format = 'marcxml'
156             AND `schema` = ?
157             AND metadata LIKE "%&amp;amp;%"
158         };
159     return @{ $dbh->selectcol_arrayref( $query, { Slice => {} }, C4::Context->preference('marcflavour') ) };
160 }
161
162 =head1 NAME
163
164 sanitize_records - This script sanitizes a record.
165
166 =head1 SYNOPSIS
167
168 sanitize_records.pl [-h|--help] [-v|--verbose] [-c|--confirm] [--biblionumbers=BIBLIONUMBER_LIST] [-f|--filename=FILENAME] [--auto-search] [--reindex] [--fix-ampersand]
169
170 You can either give some biblionumbers or a file with biblionumbers or ask for an auto-search.
171
172 =head1 OPTIONS
173
174 =over
175
176 =item B<-h|--help>
177
178 Print a brief help message
179
180 =item B<-v|--verbose>
181
182 Verbose mode.
183
184 =item B<-c|--confirm>
185
186 This flag must be provided in order for the script to actually
187 sanitize records. If it is not supplied, the script will
188 only report on the record list to process.
189
190 =item B<--biblionumbers=BIBLIONUMBER_LIST>
191
192 Give a biblionumber list using this parameter. They must be separated by
193 commas.
194
195 =item B<-f|--filename=FILENAME>
196
197 Give a biblionumber list using a filename. One biblionumber by line or separate them with a whitespace character.
198
199 =item B<--auto-search>
200
201 Automatically search records containing "&amp;" in biblio_metadata.metadata or in the specified fields.
202
203 =item B<--fix-ampersand>
204
205 Replace '&amp;' by '&' in the records.
206 Replace '&amp;amp;amp;etc.' with '&amp;' in the records.
207
208 =item B<--reindex>
209
210 Reindex the modified records.
211
212 =back
213
214 =head1 AUTHOR
215
216 Alex Arnaud <alex.arnaud@biblibre.com>
217 Christophe Croullebois <christophe.croullebois@biblibre.com>
218 Jonathan Druart <jonathan.druart@biblibre.com>
219
220 =head1 COPYRIGHT
221
222 Copyright 2014 BibLibre
223
224 =head1 LICENSE
225
226 This file is part of Koha.
227
228 # Koha is free software; you can redistribute it and/or modify it
229 # under the terms of the GNU General Public License as published by
230 # the Free Software Foundation; either version 3 of the License, or
231 # (at your option) any later version.
232 #
233 # Koha is distributed in the hope that it will be useful, but
234 # WITHOUT ANY WARRANTY; without even the implied warranty of
235 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
236 # GNU General Public License for more details.
237 #
238 # You should have received a copy of the GNU General Public License
239 # along with Koha; if not, see <http://www.gnu.org/licenses>.
240
241 =cut