Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
# small script that replaces '&etc.' by '&' in a record |
3 |
|
4 |
# Copyright 2012 Biblibre SARL |
5 |
# |
6 |
# This file is part of Koha. |
7 |
# |
8 |
# Koha is free software; you can redistribute it and/or modify it under the |
9 |
# terms of the GNU General Public License as published by the Free Software |
10 |
# Foundation; either version 2 of the License, or (at your option) any later |
11 |
# version. |
12 |
# |
13 |
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY |
14 |
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
15 |
# A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License along |
18 |
# with Koha; if not, write to the Free Software Foundation, Inc., |
19 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
20 |
|
21 |
use Modern::Perl; |
22 |
use C4::Charset; |
23 |
use C4::Context; |
24 |
use DBI; |
25 |
use C4::Biblio; |
26 |
use Getopt::Long; |
27 |
use Pod::Usage; |
28 |
|
29 |
my ( $biblios,$run,$want_help,$batch,$cron ); |
30 |
my $result = GetOptions( |
31 |
'where=s' => \$biblios, |
32 |
'--run' => \$run, |
33 |
'help|h' => \$want_help, |
34 |
'batch|b' => \$batch, |
35 |
'cron|c' => \$cron, |
36 |
); |
37 |
|
38 |
# We check if required entries are given : |
39 |
if ( not $result or $want_help or not $biblios or not $run ) { |
40 |
print_usage(); |
41 |
exit 0; |
42 |
} |
43 |
|
44 |
# We initialise some tools : |
45 |
my $count; |
46 |
my $bibliocount; |
47 |
my @bibliofile; |
48 |
my @biblionum; |
49 |
my @biblios; |
50 |
my $record; |
51 |
my $cleanrecord; |
52 |
|
53 |
# We first detect if we have a file or biblos directly entered by command line |
54 |
#or if we want to use findAmp() sub |
55 |
if ( $biblios eq "search" ) { |
56 |
@biblios = findAmp(); |
57 |
} |
58 |
else { |
59 |
if ( $biblios =~ m|/| ) { |
60 |
open( FILE, "$biblios" ) || die("Can't open $biblios"); |
61 |
@bibliofile = <FILE>; |
62 |
close(FILE); |
63 |
} |
64 |
else { |
65 |
@biblionum = split ',', $biblios; |
66 |
} |
67 |
|
68 |
# We take the biblios |
69 |
@biblios = @bibliofile ? @bibliofile : @biblionum; |
70 |
} |
71 |
|
72 |
# We remove spaces |
73 |
s/(^\s*|\s*$)//g for @biblios; |
74 |
# Remove empty lines |
75 |
@biblios = grep {!/^$/} @biblios; |
76 |
|
77 |
# We valid the input |
78 |
foreach my $biblio (@biblios) { |
79 |
if ( $biblio !~ /^\d+$/ ) { |
80 |
print |
81 |
"=============== \"$biblio\" is not a biblionumber !!! ===============\n"; |
82 |
print "=============== please verify \"$biblio\" !!! ===============\n"; |
83 |
$count++; |
84 |
} |
85 |
exit(1) if $count; |
86 |
} |
87 |
|
88 |
$bibliocount = scalar @biblios; |
89 |
|
90 |
print |
91 |
"=============== $bibliocount Biblio(s) ready to be cleaned ===============\n"; |
92 |
if ( !$batch or !$cron ) { |
93 |
my $confirm = 0; |
94 |
print "=============== You are ok ? Types yes/no :\n"; |
95 |
while ( $confirm == 0 ) { |
96 |
my $prout = <STDIN>; |
97 |
if ($prout eq "yes\n") { |
98 |
$confirm = 1; |
99 |
} |
100 |
elsif ($prout eq "no\n") { |
101 |
print "=============== Ok, bye ===============\n"; |
102 |
exit(0); |
103 |
} |
104 |
else { |
105 |
print "======= Bad answer please types 'yes' or 'no' !!!!!!!!!!! ===============\n"; |
106 |
} |
107 |
} |
108 |
} |
109 |
|
110 |
foreach my $biblio ( @biblios ) { |
111 |
print "=============== N° $biblio selected ===============\n"; |
112 |
$record = GetMarcBiblio($biblio); |
113 |
$cleanrecord = SanitizeEntity($record); |
114 |
my $frameworkcode = GetFrameworkCode($biblio); |
115 |
ModBiblio( $cleanrecord, $biblio, $frameworkcode ); |
116 |
print "=============== Biblio n° $biblio cleaning done ===============\n"; |
117 |
$count++; |
118 |
} |
119 |
|
120 |
print "==============================================================\n"; |
121 |
print "=============== $count Biblios cleaned ===============\n"; |
122 |
|
123 |
if ( $cron ) { |
124 |
print "==============================================================\n"; |
125 |
print |
126 |
"========= Now we are reindexing -b -v -where \"biblionumber=xxx\" =========\n"; |
127 |
print "==============================================================\n"; |
128 |
$count = 0; |
129 |
my $kohapath = C4::Context->config('intranetdir'); |
130 |
foreach my $biblio ( @biblios ) { |
131 |
system("$kohapath/misc/migration_tools/rebuild_zebra.pl |
132 |
-b -v -where \"biblionumber=$biblio\""); |
133 |
print "========= Biblio n° $biblio re-indexing done =========\n"; |
134 |
$count ++; |
135 |
} |
136 |
print "=============== $count Biblios re-indexed ===============\n"; |
137 |
} |
138 |
|
139 |
sub print_usage { |
140 |
print <<_USAGE_; |
141 |
$0: replaces '&' by '&' in a record, you can either give some biblionumbers or a file with biblionumbers or |
142 |
|
143 |
Parameters: |
144 |
-where use this to give biblionumbers in a string with "" (separated by coma) |
145 |
or an absolute path to a file containing biblionumbers (1 by row) |
146 |
or the command 'search' that creates an array with biblionumbers with "&amp;..." |
147 |
--run run the command |
148 |
--batch or -b run in batch mode |
149 |
--cron or -c run in cron mode, it reindexes the changed records (you can redirect the output to a file) |
150 |
--help or -h show this message. |
151 |
_USAGE_ |
152 |
} |
153 |
|
154 |
sub findAmp { |
155 |
my @bibliosearch; |
156 |
my $dbh = C4::Context->dbh; |
157 |
my $strsth = |
158 |
qq{SELECT biblionumber FROM biblioitems WHERE marcxml LIKE "%amp;amp;%"}; |
159 |
my $sth = $dbh->prepare($strsth); |
160 |
$sth->execute(); |
161 |
while ( my $bib = $sth-> fetchrow_array() ) { |
162 |
push @bibliosearch, $bib; |
163 |
} |
164 |
return @bibliosearch; |
165 |
} |