View | Details | Raw Unified | Return to bug 31035
Collapse All | Expand All

(-)a/misc/maintenance/update_opac_suppression.pl (-1 / +181 lines)
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 Pod::Usage qw( pod2usage );
30
31
use C4::Biblio qw( GetMarcBiblio ModBiblio );
32
use C4::Context;
33
use Koha::Bilblios;
34
35
my ( $sqlwhere, $confirm, $verbose, $want_help );
36
GetOptions(
37
    'confirm' => \$confirm,
38
    'verbose' => \$verbose,
39
    'marc:s'  => \$os_marc_input,
40
    'where:s' => \$sqlwhere,
41
    'help|h'  => \$want_help,
42
) || podusage(1);
43
44
pod2usage(1) if $help;
45
pod2usage("Parameter marc is mandatory") unless $os_marc_input;
46
47
sub _read_marc_code {
48
    my $input = shift;
49
    my ( $field, $subfield );
50
    if ( $input =~ /^(\d{3})$/ ) {
51
        $field = $1;
52
    }
53
    elsif ( $input =~ /^(\d{3})(\w)$/ ) {
54
        $field    = $1;
55
        $subfield = $2;
56
    }
57
    return ( $field, $subfield );
58
}
59
60
say "Updating OPAC suppression MARC data in $os_marc_input" if $verbose;
61
say "Confirm flag not passed, running in dry-run mode..." unless $confirm;
62
63
my ( $os_field, $os_subfield ) = _read_marc_code($os_marc_input);
64
65
my $os_rules = C4::Context->yaml_preference('OpacHiddenItems') // {};
66
67
my $dbh      = C4::Context->dbh;
68
my $querysth = q{
69
    SELECT items.biblionumber,biblio.frameworkcode FROM items
70
    LEFT JOIN biblioitems ON (items.biblioitemnumber = biblioitems.biblioitemnumber)
71
    LEFT JOIN biblio ON (biblioitems.biblionumber = biblio.biblionumber)
72
};
73
$querysth .= qq{ WHERE $sqlwhere } if ($sqlwhere);
74
$querysth .= q{ GROUP BY items.biblionumber ORDER BY items.biblionumber };
75
my $query = $dbh->prepare($querysth);
76
$query->execute;
77
while ( my ( $biblionumber, $frameworkcode ) = $query->fetchrow ) {
78
    $count++;
79
    say "$count processed" if ( $verbose && $count % 100 );
80
    my $marc_record = GetMarcBiblio( { biblionumber => $biblionumber } );
81
    if ($marc_record) {
82
        eval {
83
84
            my $bibio_object = Koha::Bilios->find($biblionumber);
85
            my @items        = $bibio_object->items->as_list;
86
            my $os_new_value;
87
88
            if (@items) {
89
                if ( any { !$_->hidden_in_opac( { rules => $rules } ) } @items ) {
90
                    $os_new_value = 0;    # Do not hide because there is at least one visible item
91
                }
92
                else {
93
                    $os_new_value = 1;    # Hide because all items are hidden
94
                }
95
            }
96
            else {
97
                $os_new_value = 0;        # Do not hide if no items
98
            }
99
100
            my $os_old_value;
101
            my $os_marc_field = $record->field($os_marc_field);
102
            if ($os_marc_field) {
103
                $os_old_value =
104
                  defined $os_subfield
105
                  ? $os_marc_field->subfield($os_subfield)
106
                  : $os_marc_field->data();
107
            }
108
            $os_old_value = $os_old_value ? 1 : 0;
109
110
            if ( $confirm && $os_new_value != $os_old_value ) {
111
                my $modified = C4::Biblio::ModBiblio( $marc_record, $biblionumber, $frameworkcode );
112
                if ( $verbose and $modified ) {
113
                    say "Bibliographic record $biblionumber changed to "
114
                      . ( $os_new_value ? "hidden" : "visible" )
115
                      . " in OPAC";
116
                }
117
            }
118
            elsif ($verbose) {
119
                say "Bibliographic record $biblionumber would have been changed to "
120
                  . ( $os_new_value ? "hidden" : "visible" )
121
                  . " in OPAC";
122
            }
123
124
        } if ($@) {
125
            warn "Problem with biblio $biblionumber : $@";
126
        }
127
    }
128
    else {
129
        print "Error in biblio $biblionumber : can't parse record";
130
    }
131
}
132
133
print "\n\n$count records processed.\n" if $verbose;
134
135
=head1 NAME
136
137
update_opac_suppression.pl
138
139
=head1 SYNOPSIS
140
141
  perl update_opac_suppression.pl --help
142
143
  perl update_opac_suppression.pl --confirm --marc 942n
144
145
  perl update_opac_suppression.pl --verbose --confirm --marc 942n --where "TO_DAYS(NOW()) - TO_DAYS(DATE(items.timestamp)) <= 1" 
146
147
=head1 DESCRIPTION
148
149
Update MARC field for OPACSuppression depending on hidden items.
150
151
=head1 OPTIONS
152
153
=over 8
154
155
=item B<--help>
156
157
Prints this help
158
159
=item B<--confirm>
160
161
Confirmation flag, the script will be running in dry-run mode if set not.
162
163
=item B<--verbose>
164
165
Verbose mode.
166
167
=item B<--marc>
168
169
MARC field (optionaly subfield) of OPACSuppression search field.
170
Usually 942n.
171
172
=item B<--where>
173
174
Limits the search on bibliographic records with a user-specified WHERE clause.
175
176
The columns from items, biblioitems and biblio tables are available.
177
178
=back
179
180
=cut
181

Return to bug 31035