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 |
|
29 |
use List::MoreUtils qw( any ); |
30 |
use MARC::Field; |
31 |
use Pod::Usage qw( pod2usage ); |
32 |
|
33 |
use C4::Biblio qw( ModBiblio ); |
34 |
use C4::Context; |
35 |
use Koha::Biblios; |
36 |
|
37 |
my ( $confirm, $verbose, $os_marc_input, $hide_no_items, $sqlwhere, $want_help ); |
38 |
GetOptions( |
39 |
'confirm' => \$confirm, |
40 |
'verbose' => \$verbose, |
41 |
'marc:s' => \$os_marc_input, |
42 |
'hide-no-items' => \$hide_no_items, |
43 |
'where:s' => \$sqlwhere, |
44 |
'help|h' => \$want_help, |
45 |
) || podusage(1); |
46 |
|
47 |
pod2usage(1) if $want_help; |
48 |
pod2usage("Parameter marc is mandatory") unless $os_marc_input; |
49 |
|
50 |
sub _read_marc_code { |
51 |
my $input = shift; |
52 |
my ( $field, $subfield ); |
53 |
if ( $input =~ /^(\d{3})$/ ) { |
54 |
$field = $1; |
55 |
} elsif ( $input =~ /^(\d{3})(\w)$/ ) { |
56 |
$field = $1; |
57 |
$subfield = $2; |
58 |
} |
59 |
return ( $field, $subfield ); |
60 |
} |
61 |
|
62 |
sub _get_new_value { |
63 |
my ( $biblio, $rules ) = @_; |
64 |
my $val; |
65 |
my @items = $biblio->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 |
} else { |
70 |
$val = 1; # Hide because all items are hidden |
71 |
} |
72 |
} else { |
73 |
$val = $hide_no_items ? 1 : 0; # No items |
74 |
} |
75 |
return $val; |
76 |
} |
77 |
|
78 |
sub _get_old_value { |
79 |
my ( $marc_record, $f, $sf ) = @_; |
80 |
my $val; |
81 |
my $field = $marc_record->field($f); |
82 |
if ($field) { |
83 |
$val = |
84 |
defined $sf |
85 |
? $field->subfield($sf) |
86 |
: $field->data(); |
87 |
} |
88 |
$val = $val ? 1 : 0; |
89 |
return $val; |
90 |
} |
91 |
|
92 |
sub _set_new_value { |
93 |
my ( $marc_record, $f, $sf, $val ) = @_; |
94 |
my $field = $marc_record->field($f); |
95 |
if ($field) { |
96 |
if ( defined $sf ) { |
97 |
$field->update( $sf, $val ); |
98 |
} else { |
99 |
$field->update($val); |
100 |
} |
101 |
} else { |
102 |
if ( defined $sf ) { |
103 |
$marc_record->add_fields( MARC::Field->new( $f, ' ', ' ', $sf => $val ) ); |
104 |
} else { |
105 |
$marc_record->add_fields( MARC::Field->new( $f, $val ) ); |
106 |
} |
107 |
} |
108 |
} |
109 |
|
110 |
say "Updating OPAC suppression MARC data in $os_marc_input" if $verbose; |
111 |
say "Confirm flag not passed, running in dry-run mode..." unless $confirm; |
112 |
|
113 |
my ( $os_field_input, $os_subfield_input ) = _read_marc_code($os_marc_input); |
114 |
|
115 |
my $rules = C4::Context->yaml_preference('OpacHiddenItems') // {}; |
116 |
|
117 |
my $dbh = C4::Context->dbh; |
118 |
my $querysth = q{ |
119 |
SELECT biblio.biblionumber FROM biblio |
120 |
JOIN biblio_metadata ON (biblio_metadata.biblionumber = biblio.biblionumber) |
121 |
JOIN biblioitems ON (biblioitems.biblionumber = biblio.biblionumber) |
122 |
}; |
123 |
$querysth .= qq{ WHERE $sqlwhere } if ($sqlwhere); |
124 |
$querysth .= q{ ORDER BY biblio.biblionumber }; |
125 |
my $query = $dbh->prepare($querysth); |
126 |
$query->execute; |
127 |
my $count_total = 0; |
128 |
my $count_modified = 0; |
129 |
|
130 |
while ( my ($biblionumber) = $query->fetchrow ) { |
131 |
$count_total++; |
132 |
my $biblio = Koha::Biblios->find($biblionumber); |
133 |
unless ($biblio) { |
134 |
warn "Error in biblio $biblionumber : not found"; |
135 |
next; |
136 |
} |
137 |
my $marc_record = $biblio->metadata->record; |
138 |
unless ($marc_record) { |
139 |
warn "Error in biblio $biblionumber : can't parse record"; |
140 |
next; |
141 |
} |
142 |
eval { |
143 |
my $os_new_value = _get_new_value( $biblio, $rules ); |
144 |
my $os_old_value = _get_old_value( $marc_record, $os_field_input, $os_subfield_input ); |
145 |
if ( $os_new_value != $os_old_value ) { |
146 |
if ($confirm) { |
147 |
_set_new_value( $marc_record, $os_field_input, $os_subfield_input, $os_new_value ); |
148 |
C4::Biblio::ModBiblio( $marc_record, $biblionumber, $biblio->frameworkcode, { disable_autolink => 1 } ); |
149 |
$count_modified++; |
150 |
if ($verbose) { |
151 |
say "Bibliographic record $biblionumber changed to " . ( $os_new_value ? "hidden" : "visible" ) . " in OPAC"; |
152 |
} |
153 |
} elsif ($verbose) { |
154 |
say "Bibliographic record $biblionumber would have been changed to " . ( $os_new_value ? "hidden" : "visible" ) . " in OPAC"; |
155 |
} |
156 |
} |
157 |
}; |
158 |
if ($@) { |
159 |
warn "Problem with biblio $biblionumber : $@"; |
160 |
} |
161 |
} |
162 |
|
163 |
say "$count_total records processed, $count_modified modified" if $verbose; |
164 |
|
165 |
=head1 NAME |
166 |
|
167 |
update_opac_suppression.pl |
168 |
|
169 |
=head1 SYNOPSIS |
170 |
|
171 |
perl update_opac_suppression.pl --help |
172 |
|
173 |
perl update_opac_suppression.pl --confirm --marc 942n |
174 |
|
175 |
perl update_opac_suppression.pl --verbose --confirm --marc 942n --where "TO_DAYS(NOW()) - TO_DAYS(DATE(biblio.datecreated)) <= 1" |
176 |
|
177 |
=head1 DESCRIPTION |
178 |
|
179 |
Update MARC field for OPACSuppression depending on hidden items. |
180 |
|
181 |
=head1 OPTIONS |
182 |
|
183 |
=over 8 |
184 |
|
185 |
=item B<--help> |
186 |
|
187 |
Prints this help |
188 |
|
189 |
=item B<--confirm> |
190 |
|
191 |
Confirmation flag, the script will be running in dry-run mode if set not. |
192 |
|
193 |
=item B<--verbose> |
194 |
|
195 |
Verbose mode. |
196 |
|
197 |
=item B<--marc> |
198 |
|
199 |
MARC field (optionaly subfield) of OPACSuppression search field. |
200 |
Usually 942n. |
201 |
|
202 |
=item B<--hide-no-items> |
203 |
|
204 |
Hide records with no items. By default there are not hidden. |
205 |
|
206 |
=item B<--where> |
207 |
|
208 |
Limits the search on bibliographic records with a user-specified WHERE clause. |
209 |
|
210 |
The columns from biblio, biblioitems and biblio_metadata tables are available. |
211 |
|
212 |
=back |
213 |
|
214 |
=cut |
215 |
|