]> git.koha-community.org Git - koha.git/blob - misc/maintenance/search_for_data_inconsistencies.pl
Bug 25346: Supress warning message when install dir exists
[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;
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( { itype => undef } );
59         if ( $items_without_itype->count ) {
60             new_section("Items do not have itype defined");
61             while ( my $item = $items_without_itype->next ) {
62                 new_item(
63                     sprintf "Item with itemnumber=%s does not have a itype value, biblio's item type will be used (%s)",
64                     $item->itemnumber, $item->biblioitem->itemtype
65                 );
66             }
67             new_hint("The system preference item-level_itypes expects item types to be defined at item level");
68         }
69     }
70     else {
71         my $biblioitems_without_itemtype = Koha::Biblioitems->search( { itemtype => undef } );
72         if ( $biblioitems_without_itemtype->count ) {
73             new_section("Biblioitems do not have itemtype defined");
74             while ( my $biblioitem = $biblioitems_without_itemtype->next ) {
75                 new_item(
76                     sprintf "Biblioitem with biblioitemnumber=%s does not have a itemtype value",
77                     $biblioitem->biblioitemnumber
78                 );
79             }
80             new_hint("The system preference item-level_itypes expects item types to be defined at biblio level");
81         }
82     }
83
84     my @itemtypes = Koha::ItemTypes->search->get_column('itemtype');
85     if ( C4::Context->preference('item-level_itypes') ) {
86         my $items_with_invalid_itype = Koha::Items->search( { itype => { not_in => \@itemtypes } } );
87         if ( $items_with_invalid_itype->count ) {
88             new_section("Items have invalid itype defined");
89             while ( my $item = $items_with_invalid_itype->next ) {
90                 new_item(
91                     sprintf "Item with itemnumber=%s, biblionumber=%s does not have a valid itype value (%s)",
92                     $item->itemnumber, $item->biblionumber, $item->itype
93                 );
94             }
95             new_hint("The items must have a itype value that is defined in the item types of Koha (Home › Administration › Item types administration)");
96         }
97     }
98     else {
99         my $biblioitems_with_invalid_itemtype = Koha::Biblioitems->search(
100             { itemtype => { not_in => \@itemtypes } }
101         );
102         if ( $biblioitems_with_invalid_itemtype->count ) {
103             new_section("Biblioitems do not have itemtype defined");
104             while ( my $biblioitem = $biblioitems_with_invalid_itemtype->next ) {
105                 new_item(
106                     sprintf "Biblioitem with biblioitemnumber=%s does not have a valid itemtype value",
107                     $biblioitem->biblioitemnumber
108                 );
109             }
110             new_hint("The biblioitems must have a itemtype value that is defined in the item types of Koha (Home › Administration › Item types administration)");
111         }
112     }
113
114     my @decoding_errors;
115     my $biblios = Koha::Biblios->search;
116     while ( my $biblio = $biblios->next ) {
117         eval{$biblio->metadata->record;};
118         push @decoding_errors, $@ if $@;
119     }
120     if ( @decoding_errors ) {
121         new_section("Bibliographic records have invalid MARCXML");
122         new_item($_) for @decoding_errors;
123         new_hint("The bibliographic records must have a valid MARCXML or you will face encoding issues or wrong displays");
124     }
125 }
126
127 {
128     my $frameworks = Koha::BiblioFrameworks->search;
129     my $invalid_av_per_framework = {};
130     while ( my $framework = $frameworks->next ) {
131         my $msss = Koha::MarcSubfieldStructures->search({ frameworkcode => $framework->frameworkcode, authorised_value => { '!=' => [ -and => ( undef, '' ) ]} });
132         while ( my $mss = $msss->next ) {
133             my $kohafield = $mss->kohafield;
134             my $av = $mss->authorised_value;
135             next if grep {$_ eq $av} qw( branches itemtypes cn_source ); # internal categories
136
137             my $avs = Koha::AuthorisedValues->search_by_koha_field(
138                 {
139                     frameworkcode => $framework->frameworkcode,
140                     kohafield     => $kohafield,
141                 }
142             );
143             my $tmp_kohafield = $kohafield;
144             if ( $tmp_kohafield =~ /^biblioitems/ ) {
145                 $tmp_kohafield =~ s|biblioitems|biblioitem|;
146             } else {
147                 $tmp_kohafield =~ s|items|me|;
148             }
149             # replace items.attr with me.attr
150             my $items = Koha::Items->search(
151                 {
152                     $tmp_kohafield =>
153                       {
154                           -not_in => [ $avs->get_column('authorised_value'), '' ],
155                           '!='    => undef,
156                       },
157                     'biblio.frameworkcode' => $framework->frameworkcode
158                 },
159                 { join => [ 'biblioitem', 'biblio' ] }
160             );
161             if ( $items->count ) {
162                 $invalid_av_per_framework->{ $framework->frameworkcode }->{$av} =
163                   { items => $items, kohafield => $kohafield };
164             }
165         }
166     }
167     if (%$invalid_av_per_framework) {
168         new_section('Wrong values linked to authorised values');
169         for my $frameworkcode ( keys %$invalid_av_per_framework ) {
170             my $output;
171             while ( my ( $av_category, $v ) = each %{$invalid_av_per_framework->{$frameworkcode}} ) {
172                 my $items     = $v->{items};
173                 my $kohafield = $v->{kohafield};
174                 my ( $table, $column ) = split '\.', $kohafield;
175                 while ( my $i = $items->next ) {
176                     my $value = $table eq 'items' ? $i->$column : $i->biblioitem->$column;
177                     $output .= " {" . $i->itemnumber . " => " . $value . "}";
178                 }
179                 new_item(
180                     sprintf(
181                         "The Framework *%s* is using the authorised value's category *%s*, "
182                         . "but the following %s do not have a value defined ({itemnumber => value }):\n%s",
183                         $frameworkcode, $av_category, $kohafield, $output
184                     )
185                 );
186             }
187         }
188     }
189 }
190
191 {
192     my $biblios = Koha::Biblios->search({ -or => { title => undef, title => '' }});
193     if ( $biblios->count ) {
194         my ( $title_tag, $title_subtag ) = C4::Biblio::GetMarcFromKohaField( 'biblio.title' );
195         my $title_field = $title_tag // '';
196         $title_field .= '$'.$title_subtag if $title_subtag;
197         new_section("Biblio without title $title_field");
198         while ( my $biblio = $biblios->next ) {
199             new_item(sprintf "Biblio with biblionumber=%s does not have title defined", $biblio->biblionumber);
200         }
201         new_hint("Edit these biblio records to defined a title");
202     }
203 }
204
205 sub new_section {
206     my ( $name ) = @_;
207     say "\n== $name ==";
208 }
209
210 sub new_item {
211     my ( $name ) = @_;
212     say "\t* $name";
213 }
214 sub new_hint {
215     my ( $name ) = @_;
216     say "=> $name";
217 }
218
219 =head1 NAME
220
221 search_for_data_inconsistencies.pl
222
223 =head1 SYNOPSIS
224
225     perl search_for_data_inconsistencies.pl
226
227 =head1 DESCRIPTION
228
229 Catch data inconsistencies in Koha database
230
231 * Items with undefined homebranch and/or holdingbranch
232 * Authorities with undefined authtypecodes/authority types
233 * Item types:
234   * if item types are defined at item level (item-level_itypes=specific item),
235     then items.itype must be set else biblioitems.itemtype must be set
236   * Item types defined in items or biblioitems must be defined in the itemtypes table
237 * Invalid MARCXML in bibliographic records
238
239 =cut