Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it |
6 |
# under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 3 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
# GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
BEGIN { |
21 |
use FindBin (); |
22 |
eval { require "$FindBin::Bin/../kohalib.pl" }; |
23 |
} |
24 |
|
25 |
use Koha::Script; |
26 |
|
27 |
use Getopt::Long qw( GetOptions ); |
28 |
use List::MoreUtils qw( any ); |
29 |
use MARC::Field; |
30 |
use Pod::Usage qw( pod2usage ); |
31 |
|
32 |
use C4::Biblio qw( GetMarcBiblio ModBiblio ); |
33 |
use C4::Context; |
34 |
use Koha::Biblios; |
35 |
|
36 |
my ( $confirm, $verbose, $os_marc_input, $sqlwhere, $want_help ); |
37 |
GetOptions( |
38 |
'confirm' => \$confirm, |
39 |
'verbose' => \$verbose, |
40 |
'marc:s' => \$os_marc_input, |
41 |
'where:s' => \$sqlwhere, |
42 |
'help|h' => \$want_help, |
43 |
) || podusage(1); |
44 |
|
45 |
pod2usage(1) if $want_help; |
46 |
pod2usage("Parameter marc is mandatory") unless $os_marc_input; |
47 |
|
48 |
sub _read_marc_code { |
49 |
my $input = shift; |
50 |
my ( $field, $subfield ); |
51 |
if ( $input =~ /^(\d{3})$/ ) { |
52 |
$field = $1; |
53 |
} |
54 |
elsif ( $input =~ /^(\d{3})(\w)$/ ) { |
55 |
$field = $1; |
56 |
$subfield = $2; |
57 |
} |
58 |
return ( $field, $subfield ); |
59 |
} |
60 |
|
61 |
sub _get_new_value { |
62 |
my ( $biblionumber, $rules ) = @_; |
63 |
my $val; |
64 |
my $bibio_object = Koha::Biblios->find($biblionumber); |
65 |
my @items = $bibio_object->items->as_list; |
66 |
if (@items) { |
67 |
if ( any { !$_->hidden_in_opac( { rules => $rules } ) } @items ) { |
68 |
$val = 0; # Do not hide because there is at least one visible item |
69 |
} |
70 |
else { |
71 |
$val = 1; # Hide because all items are hidden |
72 |
} |
73 |
} |
74 |
else { |
75 |
$val = 0; # Do not hide if no items |
76 |
} |
77 |
return $val; |
78 |
} |
79 |
|
80 |
sub _get_old_value { |
81 |
my ( $marc_record, $f, $sf ) = @_; |
82 |
my $val; |
83 |
my $field = $marc_record->field($f); |
84 |
if ($field) { |
85 |
$val = |
86 |
defined $sf |
87 |
? $field->subfield($sf) |
88 |
: $field->data(); |
89 |
} |
90 |
$val = $val ? 1 : 0; |
91 |
return $val; |
92 |
} |
93 |
|
94 |
sub _set_new_value { |
95 |
my ( $marc_record, $f, $sf, $val ) = @_; |
96 |
my $field = $marc_record->field($f); |
97 |
if ($field) { |
98 |
if ( defined $sf ) { |
99 |
$field->update( $sf, $val ); |
100 |
} |
101 |
else { |
102 |
$field->update($val); |
103 |
} |
104 |
} |
105 |
else { |
106 |
if ( defined $sf ) { |
107 |
$marc_record->add_fields( MARC::Field->new( $f, ' ', ' ', $sf => $val ) ); |
108 |
} |
109 |
else { |
110 |
$marc_record->add_fields( MARC::Field->new( $f, $val ) ); |
111 |
} |
112 |
} |
113 |
} |
114 |
|
115 |
say "Updating OPAC suppression MARC data in $os_marc_input" if $verbose; |
116 |
say "Confirm flag not passed, running in dry-run mode..." unless $confirm; |
117 |
|
118 |
my ( $os_field_input, $os_subfield_input ) = _read_marc_code($os_marc_input); |
119 |
|
120 |
my $rules = C4::Context->yaml_preference('OpacHiddenItems') // {}; |
121 |
|
122 |
my $dbh = C4::Context->dbh; |
123 |
my $querysth = q{ |
124 |
SELECT items.biblionumber,biblio.frameworkcode FROM items |
125 |
LEFT JOIN biblioitems ON (items.biblioitemnumber = biblioitems.biblioitemnumber) |
126 |
LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber) |
127 |
}; |
128 |
$querysth .= qq{ WHERE $sqlwhere } if ($sqlwhere); |
129 |
$querysth .= q{ GROUP BY items.biblionumber ORDER BY items.biblionumber }; |
130 |
my $query = $dbh->prepare($querysth); |
131 |
$query->execute; |
132 |
my $count_total = 0; |
133 |
my $count_modified = 0; |
134 |
|
135 |
while ( my ( $biblionumber, $frameworkcode ) = $query->fetchrow ) { |
136 |
$count_total++; |
137 |
my $marc_record = GetMarcBiblio( { biblionumber => $biblionumber } ); |
138 |
if ($marc_record) { |
139 |
eval { |
140 |
my $os_new_value = _get_new_value( $biblionumber, $rules ); |
141 |
my $os_old_value = _get_old_value( $marc_record, $os_field_input, $os_subfield_input ); |
142 |
if ( $os_new_value != $os_old_value ) { |
143 |
if ($confirm) { |
144 |
_set_new_value( $marc_record, $os_field_input, $os_subfield_input, $os_new_value ); |
145 |
C4::Biblio::ModBiblio( $marc_record, $biblionumber, $frameworkcode ); |
146 |
$count_modified++; |
147 |
if ($verbose) { |
148 |
say "Bibliographic record $biblionumber changed to " |
149 |
. ( $os_new_value ? "hidden" : "visible" ) |
150 |
. " in OPAC"; |
151 |
} |
152 |
} |
153 |
elsif ($verbose) { |
154 |
say "Bibliographic record $biblionumber would have been changed to " |
155 |
. ( $os_new_value ? "hidden" : "visible" ) |
156 |
. " in OPAC"; |
157 |
} |
158 |
} |
159 |
}; |
160 |
if ($@) { |
161 |
warn "Problem with biblio $biblionumber : $@"; |
162 |
} |
163 |
} |
164 |
else { |
165 |
say "Error in biblio $biblionumber : can't parse record"; |
166 |
} |
167 |
} |
168 |
|
169 |
say "$count_total records processed, $count_modified modified" if $verbose; |
170 |
|
171 |
=head1 NAME |
172 |
|
173 |
update_opac_suppression.pl |
174 |
|
175 |
=head1 SYNOPSIS |
176 |
|
177 |
perl update_opac_suppression.pl --help |
178 |
|
179 |
perl update_opac_suppression.pl --confirm --marc 942n |
180 |
|
181 |
perl update_opac_suppression.pl --verbose --confirm --marc 942n --where "TO_DAYS(NOW()) - TO_DAYS(DATE(items.timestamp)) <= 1" |
182 |
|
183 |
=head1 DESCRIPTION |
184 |
|
185 |
Update MARC field for OPACSuppression depending on hidden items. |
186 |
|
187 |
=head1 OPTIONS |
188 |
|
189 |
=over 8 |
190 |
|
191 |
=item B<--help> |
192 |
|
193 |
Prints this help |
194 |
|
195 |
=item B<--confirm> |
196 |
|
197 |
Confirmation flag, the script will be running in dry-run mode if set not. |
198 |
|
199 |
=item B<--verbose> |
200 |
|
201 |
Verbose mode. |
202 |
|
203 |
=item B<--marc> |
204 |
|
205 |
MARC field (optionaly subfield) of OPACSuppression search field. |
206 |
Usually 942n. |
207 |
|
208 |
=item B<--where> |
209 |
|
210 |
Limits the search on bibliographic records with a user-specified WHERE clause. |
211 |
|
212 |
The columns from items, biblioitems and biblio tables are available. |
213 |
|
214 |
=back |
215 |
|
216 |
=cut |
217 |
|