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;
28 use C4::Biblio qw( GetMarcFromKohaField );
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);
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 '' ) {
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
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");
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 ) {
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 ) {
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)");
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 ) {
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, @ids_not_in_marc );
122 my $biblios = Koha::Biblios->search;
123 my ( $biblio_tag, $biblio_subfield ) = C4::Biblio::GetMarcFromKohaField( "biblio.biblionumber" );
124 my ( $biblioitem_tag, $biblioitem_subfield ) = C4::Biblio::GetMarcFromKohaField( "biblioitems.biblioitemnumber" );
125 while ( my $biblio = $biblios->next ) {
126 my $record = eval{$biblio->metadata->record;};
128 push @decoding_errors, $@;
131 my ( $biblionumber, $biblioitemnumber );
132 if ( $biblio_tag < 10 ) {
133 my $biblio_control_field = $record->field($biblio_tag);
134 $biblionumber = $biblio_control_field->data if $biblio_control_field;
136 $biblionumber = $record->subfield( $biblio_tag, $biblio_subfield );
138 if ( $biblioitem_tag < 10 ) {
139 my $biblioitem_control_field = $record->field($biblioitem_tag);
140 $biblioitemnumber = $biblioitem_control_field->data if $biblioitem_control_field;
142 $biblioitemnumber = $record->subfield( $biblioitem_tag, $biblioitem_subfield );
144 if ( $biblionumber != $biblio->biblionumber ) {
145 push @ids_not_in_marc,
147 biblionumber => $biblio->biblionumber,
148 biblionumber_in_marc => $biblionumber,
151 if ( $biblioitemnumber != $biblio->biblioitem->biblioitemnumber ) {
152 push @ids_not_in_marc,
154 biblionumber => $biblio->biblionumber,
155 biblioitemnumber => $biblio->biblioitem->biblioitemnumber,
156 biblioitemnumber_in_marc => $biblionumber,
160 if ( @decoding_errors ) {
161 new_section("Bibliographic records have invalid MARCXML");
162 new_item($_) for @decoding_errors;
163 new_hint("The bibliographic records must have a valid MARCXML or you will face encoding issues or wrong displays");
165 if (@ids_not_in_marc) {
166 new_section("Bibliographic records have MARCXML without biblionumber or biblioitemnumber");
167 for my $id (@ids_not_in_marc) {
168 if ( exists $id->{biblioitemnumber} ) {
170 sprintf(q{Biblionumber %s has biblioitemnumber '%s' but should be '%s' in %s$%s},
172 $id->{biblioitemnumber},
173 $id->{biblioitemnumber_in_marc},
175 $biblioitem_subfield,
181 sprintf(q{Biblionumber %s has '%s' in %s$%s},
183 $id->{biblionumber_in_marc},
190 new_hint("The bibliographic records must have the biblionumber and biblioitemnumber in MARCXML");
195 my @framework_codes = Koha::BiblioFrameworks->search()->get_column('frameworkcode');
196 push @framework_codes,""; # The default is not stored in frameworks, we need to force it
198 my $invalid_av_per_framework = {};
199 foreach my $frameworkcode ( @framework_codes ) {
200 # We are only checking fields that are mapped to DB fields
201 my $msss = Koha::MarcSubfieldStructures->search({
202 frameworkcode => $frameworkcode,
203 authorised_value => {
204 '!=' => [ -and => ( undef, '' ) ]
207 '!=' => [ -and => ( undef, '' ) ]
210 while ( my $mss = $msss->next ) {
211 my $kohafield = $mss->kohafield;
212 my $av = $mss->authorised_value;
213 next if grep {$_ eq $av} qw( branches itemtypes cn_source ); # internal categories
215 my $avs = Koha::AuthorisedValues->search_by_koha_field(
217 frameworkcode => $frameworkcode,
218 kohafield => $kohafield,
221 my $tmp_kohafield = $kohafield;
222 if ( $tmp_kohafield =~ /^biblioitems/ ) {
223 $tmp_kohafield =~ s|biblioitems|biblioitem|;
225 $tmp_kohafield =~ s|items|me|;
227 # replace items.attr with me.attr
229 # We are only checking biblios with items
230 my $items = Koha::Items->search(
234 -not_in => [ $avs->get_column('authorised_value'), '' ],
237 'biblio.frameworkcode' => $frameworkcode
239 { join => [ 'biblioitem', 'biblio' ] }
241 if ( $items->count ) {
242 $invalid_av_per_framework->{ $frameworkcode }->{$av} =
243 { items => $items, kohafield => $kohafield };
247 if (%$invalid_av_per_framework) {
248 new_section('Wrong values linked to authorised values');
249 for my $frameworkcode ( keys %$invalid_av_per_framework ) {
250 while ( my ( $av_category, $v ) = each %{$invalid_av_per_framework->{$frameworkcode}} ) {
251 my $items = $v->{items};
252 my $kohafield = $v->{kohafield};
253 my ( $table, $column ) = split '\.', $kohafield;
255 while ( my $i = $items->next ) {
256 my $value = $table eq 'items'
259 ? $i->biblio->$column
260 : $i->biblioitem->$column;
261 $output .= " {" . $i->itemnumber . " => " . $value . "}\n";
265 "The Framework *%s* is using the authorised value's category *%s*, "
266 . "but the following %s do not have a value defined ({itemnumber => value }):\n%s",
267 $frameworkcode, $av_category, $kohafield, $output
276 my $biblios = Koha::Biblios->search({
282 if ( $biblios->count ) {
283 my ( $title_tag, $title_subtag ) = C4::Biblio::GetMarcFromKohaField( 'biblio.title' );
284 my $title_field = $title_tag // '';
285 $title_field .= '$'.$title_subtag if $title_subtag;
286 new_section("Biblio without title $title_field");
287 while ( my $biblio = $biblios->next ) {
288 new_item(sprintf "Biblio with biblionumber=%s does not have title defined", $biblio->biblionumber);
290 new_hint("Edit these bibliographic records to define a title");
310 search_for_data_inconsistencies.pl
314 perl search_for_data_inconsistencies.pl
318 Catch data inconsistencies in Koha database
320 * Items with undefined homebranch and/or holdingbranch
321 * Authorities with undefined authtypecodes/authority types
323 * if item types are defined at item level (item-level_itypes=specific item),
324 then items.itype must be set else biblioitems.itemtype must be set
325 * Item types defined in items or biblioitems must be defined in the itemtypes table
326 * Invalid MARCXML in bibliographic records