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>.
27 use Getopt::Long qw( GetOptions );
28 use Pod::Usage qw( pod2usage );
30 my ( $help, $verbose, $confirm, $biblionumbers, $reindex, $filename,
31 $auto_search, $fix_ampersand );
32 my $result = GetOptions(
34 'v|verbose' => \$verbose,
35 'c|confirm' => \$confirm,
36 'biblionumbers:s' => \$biblionumbers,
37 'reindex' => \$reindex,
38 'f|filename:s' => \$filename,
39 'auto-search' => \$auto_search,
40 'fix-ampersand' => \$fix_ampersand,
43 # This script only fix ampersand at the moment.
44 # It is enabled by default.
51 unless ( $filename or $biblionumbers or $auto_search ) {
55 qq{\n\tAt least one record number source should be provided.\n}
59 if ( $filename and $biblionumbers
60 or $filename and $auto_search
61 or $biblionumbers and $auto_search )
65 -message => qq{\n\tOnly one record number source should be provided.\n}
71 # We first detect if we have a file or biblos directly entered by command line
72 #or if we want to use findAmp() sub
74 @biblionumbers = biblios_to_sanitize();
78 open( my $fh, '<', $filename ) || die("Can't open $filename ($!)");
82 push @biblionumbers, split( " |,", $line );
90 qq{\n\tThis filename does not exist. Please verify the path is correct.\n}
95 @biblionumbers = split m|,|, $biblionumbers if $biblionumbers;
99 s/(^\s*|\s*$)//g for @biblionumbers;
102 @biblionumbers = grep { !/^$/ } @biblionumbers;
104 say @biblionumbers . " records to process" if $verbose;
107 for my $biblionumber (@biblionumbers) {
108 print "processing record $biblionumber..." if $verbose;
109 unless ( $biblionumber =~ m|^\d+$| ) {
110 say " skipping. ERROR: Invalid biblionumber." if $verbose;
113 my $biblio = Koha::Biblios->find($biblionumber);
115 say " skipping. ERROR: biblionumber not found." if $verbose;
118 my $record = $biblio->metadata->record;
120 say " skipping. ERROR: Invalid record." if $verbose;
124 my ( $cleaned_record, $has_been_modified ) =
125 C4::Charset::SanitizeRecord( $record, $biblionumber );
126 if ($has_been_modified) {
127 my $frameworkcode = C4::Biblio::GetFrameworkCode($record);
129 C4::Biblio::ModBiblio( $cleaned_record, $biblionumber, $frameworkcode )
131 push @changes, $biblionumber;
132 say " Done!" if $verbose;
135 say " Nothing to do." if $verbose;
143 . ( $confirm ? "cleaned!" : "to clean." );
146 if ( $reindex and $confirm and @changes ) {
147 say "Now, reindexing using -b -v" if $verbose;
148 my $kohapath = C4::Context->config('intranetdir');
150 $kohapath/misc/migration_tools/rebuild_zebra.pl -b -v -where "biblionumber IN ( |
151 . join( ',', @changes ) . q| )"
156 sub biblios_to_sanitize {
157 my $dbh = C4::Context->dbh;
161 WHERE format = 'marcxml'
163 AND metadata LIKE "%&amp;%"
165 return @{ $dbh->selectcol_arrayref( $query, { Slice => {} }, C4::Context->preference('marcflavour') ) };
170 sanitize_records - This script sanitizes a record.
174 sanitize_records.pl [-h|--help] [-v|--verbose] [-c|--confirm] [--biblionumbers=BIBLIONUMBER_LIST] [-f|--filename=FILENAME] [--auto-search] [--reindex] [--fix-ampersand]
176 You can either give some biblionumbers or a file with biblionumbers or ask for an auto-search.
184 Print a brief help message
186 =item B<-v|--verbose>
190 =item B<-c|--confirm>
192 This flag must be provided in order for the script to actually
193 sanitize records. If it is not supplied, the script will
194 only report on the record list to process.
196 =item B<--biblionumbers=BIBLIONUMBER_LIST>
198 Give a biblionumber list using this parameter. They must be separated by
201 =item B<-f|--filename=FILENAME>
203 Give a biblionumber list using a filename. One biblionumber by line or separate them with a whitespace character.
205 =item B<--auto-search>
207 Automatically search records containing "&" in biblio_metadata.metadata or in the specified fields.
209 =item B<--fix-ampersand>
211 Replace '&' by '&' in the records.
212 Replace '&amp;amp;etc.' with '&' in the records.
216 Reindex the modified records.
222 Alex Arnaud <alex.arnaud@biblibre.com>
223 Christophe Croullebois <christophe.croullebois@biblibre.com>
224 Jonathan Druart <jonathan.druart@biblibre.com>
228 Copyright 2014 BibLibre
232 This file is part of Koha.
234 # Koha is free software; you can redistribute it and/or modify it
235 # under the terms of the GNU General Public License as published by
236 # the Free Software Foundation; either version 3 of the License, or
237 # (at your option) any later version.
239 # Koha is distributed in the hope that it will be useful, but
240 # WITHOUT ANY WARRANTY; without even the implied warranty of
241 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
242 # GNU General Public License for more details.
244 # You should have received a copy of the GNU General Public License
245 # along with Koha; if not, see <http://www.gnu.org/licenses>.