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