Bug 30477: Add new UNIMARC installer translation files
[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 qw( GetOptions );
26 use Pod::Usage qw( pod2usage );
27
28 use Koha::Script;
29 use Koha::Authorities;
30 use Koha::Authority::Subfields;
31 use Koha::MetadataRecord::Authority;
32
33 my ( $max, $help, $confirm );
34 GetOptions( 'confirm' => \$confirm, 'help' => \$help, 'max' => \$max );
35 if ( !$confirm || $help ) {
36     pod2usage( -verbose => 2 );
37     exit;
38 }
39
40 our $hidden_fields = Koha::Authority::Subfields->search(
41     { hidden => { '!=' => 0 }},
42 );
43 our $results = {};
44
45 my $auths = Koha::Authorities->search( {}, { order_by => 'authid' } );
46 my $count = 0;
47 while( my $record = $auths->next ) {
48     last if $max && $count >= $max;
49     scan_record( $record );
50     $count++;
51 }
52 report_results();
53
54 sub scan_record {
55     my ( $record ) = @_;
56     my $id = $record->authid;
57     my $type = $record->authtypecode;
58     my $marc = Koha::MetadataRecord::Authority->get_from_authid($id)->record;
59     foreach my $fld ( $marc->fields ) { # does not include leader
60         my @subfields = $fld->is_control_field
61             ? '@'
62             : map { $_->[0] } $fld->subfields;
63         foreach my $sub ( @subfields ) {
64             next if $results->{ $type } && $results->{ $type }->{ $fld->tag } && $results->{ $type }->{ $fld->tag }->{ $sub };
65             if( $hidden_fields->find($type, $fld->tag, $sub) ) {
66                 $results->{ $type }->{ $fld->tag }->{ $sub } = 1;
67             }
68         }
69     }
70     # To be overcomplete, check the leader too :)
71     if( $marc->leader ) {
72         if( $hidden_fields->find($type, '000', '@') ) {
73             $results->{ $type }->{ '000' }->{ '@' } = 1;
74         }
75     }
76 }
77
78 sub report_results {
79     my $cnt = 0;
80     foreach my $fw ( sort keys %$results ) {
81         foreach my $tag ( sort keys %{$results->{$fw}} ) {
82             foreach my $sub ( sort keys %{$results->{$fw}->{$tag}} ) {
83                 print "\nFramework ".($fw||'Default').", $tag, $sub contains data but is hidden";
84                 $cnt++;
85             }
86         }
87     }
88     if( $cnt ) {
89         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";
90     } else {
91         print "\nNo hidden (sub)fields containing data were found!\n";
92     }
93 }
94
95 =head1 NAME
96
97 auth_show_hidden_data.pl
98
99 =head1 SYNOPSIS
100
101 auth_show_hidden_data.pl -c -max 1000
102
103 =head1 DESCRIPTION
104
105 This script tells you if you have authority data in hidden (sub)fields. That
106 data will be lost when editing such authority records.
107
108 =over 8
109
110 =item B<-confirm>
111
112 Confirm flag. Required to start checking authority records.
113
114 =item B<-help>
115
116 Usage statement
117
118 =item B<-max>
119
120 This optional parameter tells the script to stop after the specified number of
121 records.
122
123 =back
124
125 =cut