Bug 25006: Regression tests
[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::Items;
22 use Koha::Biblios;
23 use Koha::Biblioitems;
24 use Koha::ItemTypes;
25 use Koha::Authorities;
26
27 {
28     my $items = Koha::Items->search({ -or => { homebranch => undef, holdingbranch => undef }});
29     if ( $items->count ) { new_section("Not defined items.homebranch and/or items.holdingbranch")}
30     while ( my $item = $items->next ) {
31         if ( not $item->homebranch and not $item->holdingbranch ) {
32             new_item(sprintf "Item with itemnumber=%s does not have homebranch and holdingbranch defined", $item->itemnumber);
33         } elsif ( not $item->homebranch ) {
34             new_item(sprintf "Item with itemnumber=%s does not have homebranch defined", $item->itemnumber);
35         } else {
36             new_item(sprintf "Item with itemnumber=%s does not have holdingbranch defined", $item->itemnumber);
37         }
38     }
39     if ( $items->count ) { new_hint("Edit these items and set valid homebranch and/or holdingbranch")}
40 }
41
42 {
43     # No join possible, FK is missing at DB level
44     my @auth_types = Koha::Authority::Types->search->get_column('authtypecode');
45     my $authorities = Koha::Authorities->search({authtypecode => { 'not in' => \@auth_types } });
46     if ( $authorities->count ) {new_section("Invalid auth_header.authtypecode")}
47     while ( my $authority = $authorities->next ) {
48         new_item(sprintf "Authority with authid=%s does not have a code defined (%s)", $authority->authid, $authority->authtypecode);
49     }
50     if ( $authorities->count ) {new_hint("Go to 'Home › Administration › Authority types' to define them")}
51 }
52
53 {
54     if ( C4::Context->preference('item-level_itypes') ) {
55         my $items_without_itype = Koha::Items->search( { itype => undef } );
56         if ( $items_without_itype->count ) {
57             new_section("Items do not have itype defined");
58             while ( my $item = $items_without_itype->next ) {
59                 new_item(
60                     sprintf "Item with itemnumber=%s does not have a itype value, biblio's item type will be used (%s)",
61                     $item->itemnumber, $item->biblioitem->itemtype
62                 );
63             }
64             new_hint("The system preference item-level_itypes expects item types to be defined at item level");
65         }
66     }
67     else {
68         my $biblioitems_without_itemtype = Koha::Biblioitems->search( { itemtype => undef } );
69         if ( $biblioitems_without_itemtype->count ) {
70             new_section("Biblioitems do not have itemtype defined");
71             while ( my $biblioitem = $biblioitems_without_itemtype->next ) {
72                 new_item(
73                     sprintf "Biblioitem with biblioitemnumber=%s does not have a itemtype value",
74                     $biblioitem->biblioitemnumber
75                 );
76             }
77             new_hint("The system preference item-level_itypes expects item types to be defined at biblio level");
78         }
79     }
80
81     my @itemtypes = Koha::ItemTypes->search->get_column('itemtype');
82     if ( C4::Context->preference('item-level_itypes') ) {
83         my $items_with_invalid_itype = Koha::Items->search( { itype => { not_in => \@itemtypes } } );
84         if ( $items_with_invalid_itype->count ) {
85             new_section("Items have invalid itype defined");
86             while ( my $item = $items_with_invalid_itype->next ) {
87                 new_item(
88                     sprintf "Item with itemnumber=%s, biblionumber=%s does not have a valid itype value (%s)",
89                     $item->itemnumber, $item->biblionumber, $item->itype
90                 );
91             }
92             new_hint("The items must have a itype value that is defined in the item types of Koha (Home › Administration › Item types administration)");
93         }
94     }
95     else {
96         my $biblioitems_with_invalid_itemtype = Koha::Biblioitems->search(
97             { itemtype => { not_in => \@itemtypes } }
98         );
99         if ( $biblioitems_with_invalid_itemtype->count ) {
100             new_section("Biblioitems do not have itemtype defined");
101             while ( my $biblioitem = $biblioitems_with_invalid_itemtype->next ) {
102                 new_item(
103                     sprintf "Biblioitem with biblioitemnumber=%s does not have a valid itemtype value",
104                     $biblioitem->biblioitemnumber
105                 );
106             }
107             new_hint("The biblioitems must have a itemtype value that is defined in the item types of Koha (Home › Administration › Item types administration)");
108         }
109     }
110
111     my @decoding_errors;
112     my $biblios = Koha::Biblios->search;
113     while ( my $biblio = $biblios->next ) {
114         eval{$biblio->metadata->record;};
115         push @decoding_errors, $@ if $@;
116     }
117     if ( @decoding_errors ) {
118         new_section("Bibliographic records have invalid MARCXML");
119         new_item($_) for @decoding_errors;
120         new_hint("The bibliographic records must have a valid MARCXML or you will face encoding issues or wrong displays");
121     }
122 }
123
124 sub new_section {
125     my ( $name ) = @_;
126     say "\n== $name ==";
127 }
128
129 sub new_item {
130     my ( $name ) = @_;
131     say "\t* $name";
132 }
133 sub new_hint {
134     my ( $name ) = @_;
135     say "=> $name";
136 }
137
138 =head1 NAME
139
140 search_for_data_inconsistencies.pl
141
142 =head1 SYNOPSIS
143
144     perl search_for_data_inconsistencies.pl
145
146 =head1 DESCRIPTION
147
148 Catch data inconsistencies in Koha database
149
150 * Items with undefined homebranch and/or holdingbranch
151 * Authorities with undefined authtypecodes/authority types
152 * Item types:
153   * if item types are defined at item level (item-level_itypes=specific item),
154     then items.itype must be set else biblioitems.itemtype must be set
155   * Item types defined in items or biblioitems must be defined in the itemtypes table
156 * Invalid MARCXML in bibliographic records
157
158 =cut