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