Bug 21855: Changed "deprecated delete_unverified_opac_registrations.pl cronjob" to...
[koha.git] / misc / maintenance / auth_show_hidden_data.pl
1 #!/usr/bin/perl
2
3 # Copyright 2017 Rijksmuseum
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
21 # This script walks through your authority marc records and tells you
22 # which hidden fields in the framework still contain data.
23
24 use Modern::Perl;
25 use Getopt::Long;
26 use Pod::Usage;
27
28 use Koha::Authorities;
29 use Koha::Authority::Subfields;
30 use Koha::MetadataRecord::Authority;
31
32 my ( $max, $help, $confirm );
33 GetOptions( 'confirm' => \$confirm, 'help' => \$help, 'max' => \$max );
34 if ( !$confirm || $help ) {
35     pod2usage( -verbose => 2 );
36     exit;
37 }
38
39 our $hidden_fields = Koha::Authority::Subfields->search(
40     { hidden => { '!=' => 0 }},
41 );
42 our $results = {};
43
44 my $auths = Koha::Authorities->search( {}, { order_by => 'authid' } );
45 my $count = 0;
46 while( my $record = $auths->next ) {
47     last if $max && $count >= $max;
48     scan_record( $record );
49     $count++;
50 }
51 report_results();
52
53 sub scan_record {
54     my ( $record ) = @_;
55     my $id = $record->authid;
56     my $type = $record->authtypecode;
57     my $marc = Koha::MetadataRecord::Authority->get_from_authid($id)->record;
58     foreach my $fld ( $marc->fields ) { # does not include leader
59         my @subfields = $fld->is_control_field
60             ? '@'
61             : map { $_->[0] } $fld->subfields;
62         foreach my $sub ( @subfields ) {
63             next if $results->{ $type } && $results->{ $type }->{ $fld->tag } && $results->{ $type }->{ $fld->tag }->{ $sub };
64             if( $hidden_fields->find($type, $fld->tag, $sub) ) {
65                 $results->{ $type }->{ $fld->tag }->{ $sub } = 1;
66             }
67         }
68     }
69     # To be overcomplete, check the leader too :)
70     if( $marc->leader ) {
71         if( $hidden_fields->find($type, '000', '@') ) {
72             $results->{ $type }->{ '000' }->{ '@' } = 1;
73         }
74     }
75 }
76
77 sub report_results {
78     my $cnt = 0;
79     foreach my $fw ( sort keys %$results ) {
80         foreach my $tag ( sort keys %{$results->{$fw}} ) {
81             foreach my $sub ( sort keys %{$results->{$fw}->{$tag}} ) {
82                 print "\nFramework ".($fw||'Default').", $tag, $sub contains data but is hidden";
83                 $cnt++;
84             }
85         }
86     }
87     if( $cnt ) {
88         print "\n\nNOTE: You should consider removing the hidden attribute of these framework fields in order to not lose data in those fields when editing authority records.\n";
89     } else {
90         print "\nNo hidden (sub)fields containing data were found!\n";
91     }
92 }
93
94 =head1 NAME
95
96 auth_show_hidden_data.pl
97
98 =head1 SYNOPSIS
99
100 auth_show_hidden_data.pl -c -max 1000
101
102 =head1 DESCRIPTION
103
104 This script tells you if you have authority data in hidden (sub)fields. That
105 data will be lost when editing such authority records.
106
107 =over 8
108
109 =item B<-confirm>
110
111 Confirm flag. Required to start checking authority records.
112
113 =item B<-help>
114
115 Usage statement
116
117 =item B<-max>
118
119 This optional parameter tells the script to stop after the specified number of
120 records.
121
122 =back
123
124 =cut