Bug 20443: Remove C4::Members::AttributeTypes
[koha.git] / misc / maintenance / search_for_data_inconsistencies.pl
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 use Koha::Script;
21 use Koha::AuthorisedValues;
22 use Koha::Authorities;
23 use Koha::Biblios;
24 use Koha::BiblioFrameworks;
25 use Koha::Biblioitems;
26 use Koha::Items;
27 use Koha::ItemTypes;
28
29 {
30     my $items = Koha::Items->search({ -or => { homebranch => undef, holdingbranch => undef }});
31     if ( $items->count ) { new_section("Not defined items.homebranch and/or items.holdingbranch")}
32     while ( my $item = $items->next ) {
33         if ( not $item->homebranch and not $item->holdingbranch ) {
34             new_item(sprintf "Item with itemnumber=%s does not have homebranch and holdingbranch defined", $item->itemnumber);
35         } elsif ( not $item->homebranch ) {
36             new_item(sprintf "Item with itemnumber=%s does not have homebranch defined", $item->itemnumber);
37         } else {
38             new_item(sprintf "Item with itemnumber=%s does not have holdingbranch defined", $item->itemnumber);
39         }
40     }
41     if ( $items->count ) { new_hint("Edit these items and set valid homebranch and/or holdingbranch")}
42 }
43
44 {
45     # No join possible, FK is missing at DB level
46     my @auth_types = Koha::Authority::Types->search->get_column('authtypecode');
47     my $authorities = Koha::Authorities->search({authtypecode => { 'not in' => \@auth_types } });
48     if ( $authorities->count ) {new_section("Invalid auth_header.authtypecode")}
49     while ( my $authority = $authorities->next ) {
50         new_item(sprintf "Authority with authid=%s does not have a code defined (%s)", $authority->authid, $authority->authtypecode);
51     }
52     if ( $authorities->count ) {new_hint("Go to 'Home › Administration › Authority types' to define them")}
53 }
54
55 {
56     if ( C4::Context->preference('item-level_itypes') ) {
57         my $items_without_itype = Koha::Items->search( { itype => undef } );
58         if ( $items_without_itype->count ) {
59             new_section("Items do not have itype defined");
60             while ( my $item = $items_without_itype->next ) {
61                 new_item(
62                     sprintf "Item with itemnumber=%s does not have a itype value, biblio's item type will be used (%s)",
63                     $item->itemnumber, $item->biblioitem->itemtype
64                 );
65             }
66             new_hint("The system preference item-level_itypes expects item types to be defined at item level");
67         }
68     }
69     else {
70         my $biblioitems_without_itemtype = Koha::Biblioitems->search( { itemtype => undef } );
71         if ( $biblioitems_without_itemtype->count ) {
72             new_section("Biblioitems do not have itemtype defined");
73             while ( my $biblioitem = $biblioitems_without_itemtype->next ) {
74                 new_item(
75                     sprintf "Biblioitem with biblioitemnumber=%s does not have a itemtype value",
76                     $biblioitem->biblioitemnumber
77                 );
78             }
79             new_hint("The system preference item-level_itypes expects item types to be defined at biblio level");
80         }
81     }
82
83     my @itemtypes = Koha::ItemTypes->search->get_column('itemtype');
84     if ( C4::Context->preference('item-level_itypes') ) {
85         my $items_with_invalid_itype = Koha::Items->search( { itype => { not_in => \@itemtypes } } );
86         if ( $items_with_invalid_itype->count ) {
87             new_section("Items have invalid itype defined");
88             while ( my $item = $items_with_invalid_itype->next ) {
89                 new_item(
90                     sprintf "Item with itemnumber=%s, biblionumber=%s does not have a valid itype value (%s)",
91                     $item->itemnumber, $item->biblionumber, $item->itype
92                 );
93             }
94             new_hint("The items must have a itype value that is defined in the item types of Koha (Home › Administration › Item types administration)");
95         }
96     }
97     else {
98         my $biblioitems_with_invalid_itemtype = Koha::Biblioitems->search(
99             { itemtype => { not_in => \@itemtypes } }
100         );
101         if ( $biblioitems_with_invalid_itemtype->count ) {
102             new_section("Biblioitems do not have itemtype defined");
103             while ( my $biblioitem = $biblioitems_with_invalid_itemtype->next ) {
104                 new_item(
105                     sprintf "Biblioitem with biblioitemnumber=%s does not have a valid itemtype value",
106                     $biblioitem->biblioitemnumber
107                 );
108             }
109             new_hint("The biblioitems must have a itemtype value that is defined in the item types of Koha (Home › Administration › Item types administration)");
110         }
111     }
112
113     my @decoding_errors;
114     my $biblios = Koha::Biblios->search;
115     while ( my $biblio = $biblios->next ) {
116         eval{$biblio->metadata->record;};
117         push @decoding_errors, $@ if $@;
118     }
119     if ( @decoding_errors ) {
120         new_section("Bibliographic records have invalid MARCXML");
121         new_item($_) for @decoding_errors;
122         new_hint("The bibliographic records must have a valid MARCXML or you will face encoding issues or wrong displays");
123     }
124 }
125
126 {
127     my $frameworks = Koha::BiblioFrameworks->search;
128     my $invalid_av_per_framework = {};
129     while ( my $framework = $frameworks->next ) {
130         my $msss = Koha::MarcSubfieldStructures->search({ frameworkcode => $framework->frameworkcode, authorised_value => { '!=' => [ -and => ( undef, '' ) ]} });
131         while ( my $mss = $msss->next ) {
132             my $kohafield = $mss->kohafield;
133             my $av = $mss->authorised_value;
134             next if grep {$_ eq $av} qw( branches itemtypes cn_source ); # internal categories
135
136             my $avs = Koha::AuthorisedValues->search_by_koha_field(
137                 {
138                     frameworkcode => $framework->frameworkcode,
139                     kohafield     => $kohafield,
140                 }
141             );
142             my $tmp_kohafield = $kohafield;
143             if ( $tmp_kohafield =~ /^biblioitems/ ) {
144                 $tmp_kohafield =~ s|biblioitems|biblioitem|;
145             } else {
146                 $tmp_kohafield =~ s|items|me|;
147             }
148             # replace items.attr with me.attr
149             my $items = Koha::Items->search(
150                 {
151                     $tmp_kohafield =>
152                       {
153                           -not_in => [ $avs->get_column('authorised_value'), '' ],
154                           '!='    => undef,
155                       },
156                     'biblio.frameworkcode' => $framework->frameworkcode
157                 },
158                 { join => [ 'biblioitem', 'biblio' ] }
159             );
160             if ( $items->count ) {
161                 $invalid_av_per_framework->{ $framework->frameworkcode }->{$av} =
162                   { items => $items, kohafield => $kohafield };
163             }
164         }
165     }
166     if (%$invalid_av_per_framework) {
167         new_section('Wrong values linked to authorised values');
168         for my $frameworkcode ( keys %$invalid_av_per_framework ) {
169             my $output;
170             while ( my ( $av_category, $v ) = each %{$invalid_av_per_framework->{$frameworkcode}} ) {
171                 my $items     = $v->{items};
172                 my $kohafield = $v->{kohafield};
173                 my ( $table, $column ) = split '\.', $kohafield;
174                 while ( my $i = $items->next ) {
175                     my $value = $table eq 'items' ? $i->$column : $i->biblioitem->$column;
176                     $output .= " {" . $i->itemnumber . " => " . $value . "}";
177                 }
178                 new_item(
179                     sprintf(
180                         "The Framework *%s* is using the authorised value's category *%s*, "
181                         . "but the following %s do not have a value defined ({itemnumber => value }):\n%s",
182                         $frameworkcode, $av_category, $kohafield, $output
183                     )
184                 );
185             }
186         }
187     }
188 }
189
190 sub new_section {
191     my ( $name ) = @_;
192     say "\n== $name ==";
193 }
194
195 sub new_item {
196     my ( $name ) = @_;
197     say "\t* $name";
198 }
199 sub new_hint {
200     my ( $name ) = @_;
201     say "=> $name";
202 }
203
204 =head1 NAME
205
206 search_for_data_inconsistencies.pl
207
208 =head1 SYNOPSIS
209
210     perl search_for_data_inconsistencies.pl
211
212 =head1 DESCRIPTION
213
214 Catch data inconsistencies in Koha database
215
216 * Items with undefined homebranch and/or holdingbranch
217 * Authorities with undefined authtypecodes/authority types
218 * Item types:
219   * if item types are defined at item level (item-level_itypes=specific item),
220     then items.itype must be set else biblioitems.itemtype must be set
221   * Item types defined in items or biblioitems must be defined in the itemtypes table
222 * Invalid MARCXML in bibliographic records
223
224 =cut