Bug 26922: Regression tests
[koha.git] / misc / maintenance / search_for_data_inconsistencies.pl
blob433f668040bdd3535268150d7c6d84b47f0409b9
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
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;
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);
42 if ( $items->count ) { new_hint("Edit these items and set valid homebranch and/or holdingbranch")}
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);
53 if ( $authorities->count ) {new_hint("Go to 'Home › Administration › Authority types' to define them")}
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
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
74 new_hint("The system preference item-level_itypes expects item types to be defined at item level");
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
87 new_hint("The system preference item-level_itypes expects item types to be defined at biblio level");
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
102 new_hint("The items must have a itype value that is defined in the item types of Koha (Home › Administration › Item types administration)");
105 else {
106 my $biblioitems_with_invalid_itemtype = Koha::Biblioitems->search(
107 { itemtype => { not_in => \@itemtypes } }
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
117 new_hint("The biblioitems must have a itemtype value that is defined in the item types of Koha (Home › Administration › Item types administration)");
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 $@;
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");
135 my $frameworks = Koha::BiblioFrameworks->search;
136 my $invalid_av_per_framework = {};
137 while ( my $framework = $frameworks->next ) {
138 my $msss = Koha::MarcSubfieldStructures->search({ frameworkcode => $framework->frameworkcode, authorised_value => { '!=' => [ -and => ( undef, '' ) ]} });
139 while ( my $mss = $msss->next ) {
140 my $kohafield = $mss->kohafield;
141 my $av = $mss->authorised_value;
142 next if grep {$_ eq $av} qw( branches itemtypes cn_source ); # internal categories
144 my $avs = Koha::AuthorisedValues->search_by_koha_field(
146 frameworkcode => $framework->frameworkcode,
147 kohafield => $kohafield,
150 my $tmp_kohafield = $kohafield;
151 if ( $tmp_kohafield =~ /^biblioitems/ ) {
152 $tmp_kohafield =~ s|biblioitems|biblioitem|;
153 } else {
154 $tmp_kohafield =~ s|items|me|;
156 # replace items.attr with me.attr
157 my $items = Koha::Items->search(
159 $tmp_kohafield =>
161 -not_in => [ $avs->get_column('authorised_value'), '' ],
162 '!=' => undef,
164 'biblio.frameworkcode' => $framework->frameworkcode
166 { join => [ 'biblioitem', 'biblio' ] }
168 if ( $items->count ) {
169 $invalid_av_per_framework->{ $framework->frameworkcode }->{$av} =
170 { items => $items, kohafield => $kohafield };
174 if (%$invalid_av_per_framework) {
175 new_section('Wrong values linked to authorised values');
176 for my $frameworkcode ( keys %$invalid_av_per_framework ) {
177 my $output;
178 while ( my ( $av_category, $v ) = each %{$invalid_av_per_framework->{$frameworkcode}} ) {
179 my $items = $v->{items};
180 my $kohafield = $v->{kohafield};
181 my ( $table, $column ) = split '\.', $kohafield;
182 while ( my $i = $items->next ) {
183 my $value = $table eq 'items' ? $i->$column : $i->biblioitem->$column;
184 $output .= " {" . $i->itemnumber . " => " . $value . "}";
186 new_item(
187 sprintf(
188 "The Framework *%s* is using the authorised value's category *%s*, "
189 . "but the following %s do not have a value defined ({itemnumber => value }):\n%s",
190 $frameworkcode, $av_category, $kohafield, $output
199 my $biblios = Koha::Biblios->search({
200 -or => [
201 title => '',
202 title => undef,
205 if ( $biblios->count ) {
206 my ( $title_tag, $title_subtag ) = C4::Biblio::GetMarcFromKohaField( 'biblio.title' );
207 my $title_field = $title_tag // '';
208 $title_field .= '$'.$title_subtag if $title_subtag;
209 new_section("Biblio without title $title_field");
210 while ( my $biblio = $biblios->next ) {
211 new_item(sprintf "Biblio with biblionumber=%s does not have title defined", $biblio->biblionumber);
213 new_hint("Edit these biblio records to defined a title");
217 sub new_section {
218 my ( $name ) = @_;
219 say "\n== $name ==";
222 sub new_item {
223 my ( $name ) = @_;
224 say "\t* $name";
226 sub new_hint {
227 my ( $name ) = @_;
228 say "=> $name";
231 =head1 NAME
233 search_for_data_inconsistencies.pl
235 =head1 SYNOPSIS
237 perl search_for_data_inconsistencies.pl
239 =head1 DESCRIPTION
241 Catch data inconsistencies in Koha database
243 * Items with undefined homebranch and/or holdingbranch
244 * Authorities with undefined authtypecodes/authority types
245 * Item types:
246 * if item types are defined at item level (item-level_itypes=specific item),
247 then items.itype must be set else biblioitems.itemtype must be set
248 * Item types defined in items or biblioitems must be defined in the itemtypes table
249 * Invalid MARCXML in bibliographic records
251 =cut