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