3 # This file is part of Koha.
5 # Copyright 2014 BibLibre
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.
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.
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>.
26 use Getopt::Long qw( GetOptions );
27 use Pod::Usage qw( pod2usage );
29 my ( $help, $verbose, $confirm, $biblionumbers, $reindex, $filename,
30 $auto_search, $fix_ampersand );
31 my $result = GetOptions(
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,
42 # This script only fix ampersand at the moment.
43 # It is enabled by default.
50 unless ( $filename or $biblionumbers or $auto_search ) {
54 qq{\n\tAt least one record number source should be provided.\n}
58 if ( $filename and $biblionumbers
59 or $filename and $auto_search
60 or $biblionumbers and $auto_search )
64 -message => qq{\n\tOnly one record number source should be provided.\n}
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
73 @biblionumbers = biblios_to_sanitize();
77 open( my $fh, '<', $filename ) || die("Can't open $filename ($!)");
81 push @biblionumbers, split( " |,", $line );
89 qq{\n\tThis filename does not exist. Please verify the path is correct.\n}
94 @biblionumbers = split m|,|, $biblionumbers if $biblionumbers;
98 s/(^\s*|\s*$)//g for @biblionumbers;
101 @biblionumbers = grep { !/^$/ } @biblionumbers;
103 say @biblionumbers . " records to process" if $verbose;
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;
112 my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblionumber });
114 say " skipping. ERROR: Invalid record." if $verbose;
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);
123 C4::Biblio::ModBiblio( $cleaned_record, $biblionumber, $frameworkcode )
125 push @changes, $biblionumber;
126 say " Done!" if $verbose;
129 say " Nothing to do." if $verbose;
137 . ( $confirm ? "cleaned!" : "to clean." );
140 if ( $reindex and $confirm and @changes ) {
141 say "Now, reindexing using -b -v" if $verbose;
142 my $kohapath = C4::Context->config('intranetdir');
144 $kohapath/misc/migration_tools/rebuild_zebra.pl -b -v -where "biblionumber IN ( |
145 . join( ',', @changes ) . q| )"
150 sub biblios_to_sanitize {
151 my $dbh = C4::Context->dbh;
155 WHERE format = 'marcxml'
157 AND metadata LIKE "%&amp;%"
159 return @{ $dbh->selectcol_arrayref( $query, { Slice => {} }, C4::Context->preference('marcflavour') ) };
164 sanitize_records - This script sanitizes a record.
168 sanitize_records.pl [-h|--help] [-v|--verbose] [-c|--confirm] [--biblionumbers=BIBLIONUMBER_LIST] [-f|--filename=FILENAME] [--auto-search] [--reindex] [--fix-ampersand]
170 You can either give some biblionumbers or a file with biblionumbers or ask for an auto-search.
178 Print a brief help message
180 =item B<-v|--verbose>
184 =item B<-c|--confirm>
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.
190 =item B<--biblionumbers=BIBLIONUMBER_LIST>
192 Give a biblionumber list using this parameter. They must be separated by
195 =item B<-f|--filename=FILENAME>
197 Give a biblionumber list using a filename. One biblionumber by line or separate them with a whitespace character.
199 =item B<--auto-search>
201 Automatically search records containing "&" in biblio_metadata.metadata or in the specified fields.
203 =item B<--fix-ampersand>
205 Replace '&' by '&' in the records.
206 Replace '&amp;amp;etc.' with '&' in the records.
210 Reindex the modified records.
216 Alex Arnaud <alex.arnaud@biblibre.com>
217 Christophe Croullebois <christophe.croullebois@biblibre.com>
218 Jonathan Druart <jonathan.druart@biblibre.com>
222 Copyright 2014 BibLibre
226 This file is part of Koha.
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.
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.
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>.