Update release notes for 22.05.21 release
[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, @ids_not_in_marc );
122     my $biblios = Koha::Biblios->search;
123     my ( $biblio_tag,     $biblio_subfield )     = C4::Biblio::GetMarcFromKohaField( "biblio.biblionumber" );
124     my ( $biblioitem_tag, $biblioitem_subfield ) = C4::Biblio::GetMarcFromKohaField( "biblioitems.biblioitemnumber" );
125     while ( my $biblio = $biblios->next ) {
126         my $record = eval{$biblio->metadata->record;};
127         if ($@) {
128             push @decoding_errors, $@;
129             next;
130         }
131         my ( $biblionumber, $biblioitemnumber );
132         if ( $biblio_tag < 10 ) {
133             my $biblio_control_field = $record->field($biblio_tag);
134             $biblionumber = $biblio_control_field->data if $biblio_control_field;
135         } else {
136             $biblionumber = $record->subfield( $biblio_tag, $biblio_subfield );
137         }
138         if ( $biblioitem_tag < 10 ) {
139             my $biblioitem_control_field = $record->field($biblioitem_tag);
140             $biblioitemnumber = $biblioitem_control_field->data if $biblioitem_control_field;
141         } else {
142             $biblioitemnumber = $record->subfield( $biblioitem_tag, $biblioitem_subfield );
143         }
144         if ( $biblionumber != $biblio->biblionumber ) {
145             push @ids_not_in_marc,
146               {
147                 biblionumber         => $biblio->biblionumber,
148                 biblionumber_in_marc => $biblionumber,
149               };
150         }
151         if ( $biblioitemnumber != $biblio->biblioitem->biblioitemnumber ) {
152             push @ids_not_in_marc,
153             {
154                 biblionumber     => $biblio->biblionumber,
155                 biblioitemnumber => $biblio->biblioitem->biblioitemnumber,
156                 biblioitemnumber_in_marc => $biblionumber,
157             };
158         }
159     }
160     if ( @decoding_errors ) {
161         new_section("Bibliographic records have invalid MARCXML");
162         new_item($_) for @decoding_errors;
163         new_hint("The bibliographic records must have a valid MARCXML or you will face encoding issues or wrong displays");
164     }
165     if (@ids_not_in_marc) {
166         new_section("Bibliographic records have MARCXML without biblionumber or biblioitemnumber");
167         for my $id (@ids_not_in_marc) {
168             if ( exists $id->{biblioitemnumber} ) {
169                 new_item(
170                     sprintf(q{Biblionumber %s has biblioitemnumber '%s' but should be '%s' in %s$%s},
171                         $id->{biblionumber},
172                         $id->{biblioitemnumber},
173                         $id->{biblioitemnumber_in_marc},
174                         $biblioitem_tag,
175                         $biblioitem_subfield,
176                     )
177                 );
178             }
179             else {
180                 new_item(
181                     sprintf(q{Biblionumber %s has '%s' in %s$%s},
182                         $id->{biblionumber},
183                         $id->{biblionumber_in_marc},
184                         $biblio_tag,
185                         $biblio_subfield,
186                     )
187                 );
188             }
189         }
190         new_hint("The bibliographic records must have the biblionumber and biblioitemnumber in MARCXML");
191     }
192 }
193
194 {
195     my @framework_codes = Koha::BiblioFrameworks->search()->get_column('frameworkcode');
196     push @framework_codes,""; # The default is not stored in frameworks, we need to force it
197
198     my $invalid_av_per_framework = {};
199     foreach my $frameworkcode ( @framework_codes ) {
200         # We are only checking fields that are mapped to DB fields
201         my $msss = Koha::MarcSubfieldStructures->search({
202             frameworkcode => $frameworkcode,
203             authorised_value => {
204                 '!=' => [ -and => ( undef, '' ) ]
205             },
206             kohafield => {
207                 '!=' => [ -and => ( undef, '' ) ]
208             }
209         });
210         while ( my $mss = $msss->next ) {
211             my $kohafield = $mss->kohafield;
212             my $av = $mss->authorised_value;
213             next if grep {$_ eq $av} qw( branches itemtypes cn_source ); # internal categories
214
215             my $avs = Koha::AuthorisedValues->search_by_koha_field(
216                 {
217                     frameworkcode => $frameworkcode,
218                     kohafield     => $kohafield,
219                 }
220             );
221             my $tmp_kohafield = $kohafield;
222             if ( $tmp_kohafield =~ /^biblioitems/ ) {
223                 $tmp_kohafield =~ s|biblioitems|biblioitem|;
224             } else {
225                 $tmp_kohafield =~ s|items|me|;
226             }
227             # replace items.attr with me.attr
228
229             # We are only checking biblios with items
230             my $items = Koha::Items->search(
231                 {
232                     $tmp_kohafield =>
233                       {
234                           -not_in => [ $avs->get_column('authorised_value'), '' ],
235                           '!='    => undef,
236                       },
237                     'biblio.frameworkcode' => $frameworkcode
238                 },
239                 { join => [ 'biblioitem', 'biblio' ] }
240             );
241             if ( $items->count ) {
242                 $invalid_av_per_framework->{ $frameworkcode }->{$av} =
243                   { items => $items, kohafield => $kohafield };
244             }
245         }
246     }
247     if (%$invalid_av_per_framework) {
248         new_section('Wrong values linked to authorised values');
249         for my $frameworkcode ( keys %$invalid_av_per_framework ) {
250             while ( my ( $av_category, $v ) = each %{$invalid_av_per_framework->{$frameworkcode}} ) {
251                 my $items     = $v->{items};
252                 my $kohafield = $v->{kohafield};
253                 my ( $table, $column ) = split '\.', $kohafield;
254                 my $output;
255                 while ( my $i = $items->next ) {
256                     my $value = $table eq 'items'
257                         ? $i->$column
258                         : $table eq 'biblio'
259                         ? $i->biblio->$column
260                         : $i->biblioitem->$column;
261                     $output .= " {" . $i->itemnumber . " => " . $value . "}\n";
262                 }
263                 new_item(
264                     sprintf(
265                         "The Framework *%s* is using the authorised value's category *%s*, "
266                         . "but the following %s do not have a value defined ({itemnumber => value }):\n%s",
267                         $frameworkcode, $av_category, $kohafield, $output
268                     )
269                 );
270             }
271         }
272     }
273 }
274
275 {
276     my $biblios = Koha::Biblios->search({
277         -or => [
278             title => '',
279             title => undef,
280         ]
281     });
282     if ( $biblios->count ) {
283         my ( $title_tag, $title_subtag ) = C4::Biblio::GetMarcFromKohaField( 'biblio.title' );
284         my $title_field = $title_tag // '';
285         $title_field .= '$'.$title_subtag if $title_subtag;
286         new_section("Biblio without title $title_field");
287         while ( my $biblio = $biblios->next ) {
288             new_item(sprintf "Biblio with biblionumber=%s does not have title defined", $biblio->biblionumber);
289         }
290         new_hint("Edit these bibliographic records to define a title");
291     }
292 }
293
294 sub new_section {
295     my ( $name ) = @_;
296     say "\n== $name ==";
297 }
298
299 sub new_item {
300     my ( $name ) = @_;
301     say "\t* $name";
302 }
303 sub new_hint {
304     my ( $name ) = @_;
305     say "=> $name";
306 }
307
308 =head1 NAME
309
310 search_for_data_inconsistencies.pl
311
312 =head1 SYNOPSIS
313
314     perl search_for_data_inconsistencies.pl
315
316 =head1 DESCRIPTION
317
318 Catch data inconsistencies in Koha database
319
320 * Items with undefined homebranch and/or holdingbranch
321 * Authorities with undefined authtypecodes/authority types
322 * Item types:
323   * if item types are defined at item level (item-level_itypes=specific item),
324     then items.itype must be set else biblioitems.itemtype must be set
325   * Item types defined in items or biblioitems must be defined in the itemtypes table
326 * Invalid MARCXML in bibliographic records
327
328 =cut