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>.
21 use Koha::AuthorisedValues;
22 use Koha::Authorities;
24 use Koha::BiblioFrameworks;
25 use Koha::Biblioitems;
29 use C4::Biblio qw( GetMarcFromKohaField );
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);
40 new_item(sprintf "Item with itemnumber=%s does not have holdingbranch defined", $item->itemnumber);
43 if ( $items->count ) { new_hint("Edit these items and set valid homebranch and/or holdingbranch")}
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);
54 if ( $authorities->count ) {new_hint("Go to 'Home › Administration › Authority types' to define them")}
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 '' ) {
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
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
75 new_hint("The system preference item-level_itypes expects item types to be defined at item level");
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 ) {
84 sprintf "Biblioitem with biblioitemnumber=%s does not have a itemtype value",
85 $biblioitem->biblioitemnumber
88 new_hint("The system preference item-level_itypes expects item types to be defined at biblio level");
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 ) {
99 sprintf "Item with itemnumber=%s, biblionumber=%s does not have a valid itype value (%s)",
100 $item->itemnumber, $item->biblionumber, $item->itype
103 new_hint("The items must have a itype value that is defined in the item types of Koha (Home › Administration › Item types administration)");
107 my $biblioitems_with_invalid_itemtype = Koha::Biblioitems->search(
108 { itemtype => { not_in => \@itemtypes } }
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 ) {
114 sprintf "Biblioitem with biblioitemnumber=%s does not have a valid itemtype value",
115 $biblioitem->biblioitemnumber
118 new_hint("The biblioitems must have a itemtype value that is defined in the item types of Koha (Home › Administration › Item types administration)");
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;};
129 push @decoding_errors, $@;
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,
137 biblionumber => $biblio->biblionumber,
138 biblionumber_in_marc => $biblionumber,
141 if ( $biblioitemnumber != $biblio->biblioitem->biblioitemnumber ) {
142 push @ids_not_in_marc,
144 biblionumber => $biblio->biblionumber,
145 biblioitemnumber => $biblio->biblioitem->biblioitemnumber,
146 biblioitemnumber_in_marc => $biblionumber,
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");
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} ) {
160 sprintf(q{Biblionumber %s has biblioitemnumber '%s' but should be '%s' in %s$%s},
162 $id->{biblioitemnumber},
163 $id->{biblioitemnumber_in_marc},
165 $biblioitem_subfield,
171 sprintf(q{Biblionumber %s has '%s' in %s$%s},
173 $id->{biblionumber_in_marc},
180 new_hint("The bibliographic records must have the biblionumber and biblioitemnumber in MARCXML");
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
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, '' ) ]
197 '!=' => [ -and => ( undef, '' ) ]
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
205 my $avs = Koha::AuthorisedValues->search_by_koha_field(
207 frameworkcode => $frameworkcode,
208 kohafield => $kohafield,
211 my $tmp_kohafield = $kohafield;
212 if ( $tmp_kohafield =~ /^biblioitems/ ) {
213 $tmp_kohafield =~ s|biblioitems|biblioitem|;
215 $tmp_kohafield =~ s|items|me|;
217 # replace items.attr with me.attr
219 # We are only checking biblios with items
220 my $items = Koha::Items->search(
224 -not_in => [ $avs->get_column('authorised_value'), '' ],
227 'biblio.frameworkcode' => $frameworkcode
229 { join => [ 'biblioitem', 'biblio' ] }
231 if ( $items->count ) {
232 $invalid_av_per_framework->{ $frameworkcode }->{$av} =
233 { items => $items, kohafield => $kohafield };
237 if (%$invalid_av_per_framework) {
238 new_section('Wrong values linked to authorised values');
239 for my $frameworkcode ( keys %$invalid_av_per_framework ) {
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'
249 ? $i->biblio->$column
250 : $i->biblioitem->$column;
251 $output .= " {" . $i->itemnumber . " => " . $value . "}";
255 "The Framework *%s* is using the authorised value's category *%s*, "
256 . "but the following %s do not have a value defined ({itemnumber => value }):\n%s",
257 $frameworkcode, $av_category, $kohafield, $output
266 my $biblios = Koha::Biblios->search({
272 if ( $biblios->count ) {
273 my ( $title_tag, $title_subtag ) = C4::Biblio::GetMarcFromKohaField( 'biblio.title' );
274 my $title_field = $title_tag // '';
275 $title_field .= '$'.$title_subtag if $title_subtag;
276 new_section("Biblio without title $title_field");
277 while ( my $biblio = $biblios->next ) {
278 new_item(sprintf "Biblio with biblionumber=%s does not have title defined", $biblio->biblionumber);
280 new_hint("Edit these biblio records to defined a title");
285 my $aging_patrons = Koha::Patrons->search(
289 'me.dateofbirth' => undef,
291 'categorycode.dateofbirthrequired' => undef,
292 'categorycode.upperagelimit' => undef,
297 { prefetch => ['categorycode'] },
298 { order_by => [ 'me.categorycode', 'me.borrowernumber' ] },
301 while ( my $aging_patron = $aging_patrons->next ) {
302 push @invalid_patrons, $aging_patron unless $aging_patron->is_valid_age;
304 if (@invalid_patrons) {
305 new_section("Patrons with invalid age for category");
306 foreach my $invalid_patron (@invalid_patrons) {
307 my $category = $invalid_patron->category;
309 sprintf "Patron borrowernumber=%s has an invalid age of %s for their category '%s' (%s to %s)",
310 $invalid_patron->borrowernumber, $invalid_patron->get_age, $category->categorycode,
311 $category->dateofbirthrequired // '0', $category->upperagelimit // 'unlimited'
314 new_hint("You may change the patron's category automatically with misc/cronjobs/update_patrons_category.pl");
334 search_for_data_inconsistencies.pl
338 perl search_for_data_inconsistencies.pl
342 Catch data inconsistencies in Koha database
344 * Items with undefined homebranch and/or holdingbranch
345 * Authorities with undefined authtypecodes/authority types
347 * if item types are defined at item level (item-level_itypes=specific item),
348 then items.itype must be set else biblioitems.itemtype must be set
349 * Item types defined in items or biblioitems must be defined in the itemtypes table
350 * Invalid MARCXML in bibliographic records
351 * Patrons with invalid category types due to lower and upper age limits