Bug 23846: Add a check to the data inconsistencies script
[koha.git] / admin / checkmarc.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2000-2002 Katipo Communications
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use Modern::Perl;
22 use C4::Output;
23 use C4::Auth;
24 use CGI qw ( -utf8 );
25 use C4::Context;
26 use C4::Biblio;
27
28
29 my $input = new CGI;
30
31 my ($template, $borrowernumber, $cookie)
32     = get_template_and_user({template_name => "admin/checkmarc.tt",
33                              query => $input,
34                              type => "intranet",
35                              authnotrequired => 0,
36                  flagsrequired => { parameters => 'manage_marc_frameworks' },
37                              debug => 1,
38                              });
39
40 my $dbh = C4::Context->dbh;
41 my $total = 0;
42 # checks itemnum field
43 my $sth = $dbh->prepare("select tab from marc_subfield_structure where kohafield=\"items.itemnumber\"");
44 $sth->execute;
45 while (my ($res) = $sth->fetchrow) {
46     if ($res==-1) {
47             $template->param(itemnum => 0);
48     } else {
49             $template->param(itemnum => 1);
50             $total++;
51         last;
52     }
53 }
54
55 # checks biblio.biblionumber and biblioitem.biblioitemnumber (same tag and tab=-1)
56 $sth = $dbh->prepare("select tagfield,tab,frameworkcode from marc_subfield_structure where kohafield=\"biblio.biblionumber\"");
57 $sth->execute;
58 my $first = 1;
59 my $bibliotag = '';
60 while (my ($res,$tab,$frameworkcode) = $sth->fetchrow) {
61     if ($first) {
62         $bibliotag = $res;
63         $first = 0;
64     } else {
65         if ($bibliotag != $res) {
66                 $template->param(biblionumber => 1);
67                 $total++;
68             last;
69         }
70     }
71     my $sth2 = $dbh->prepare("SELECT tagfield,tab 
72                               FROM marc_subfield_structure 
73                               WHERE kohafield=\"biblioitems.biblioitemnumber\"
74                               AND frameworkcode = ? ");
75     $sth2->execute($frameworkcode);
76     my ($res2,$tab2) = $sth2->fetchrow;
77     if ($res && $res2 && $tab==-1 && $tab2==-1) {
78             $template->param(biblionumber => 0);
79     } else {
80             $template->param(biblionumber => 1);
81             $total++;
82         last;
83     }
84 }
85
86 # checks all item fields are in the same tag and in tab 10
87
88 $sth = $dbh->prepare("select tagfield,tab,kohafield from marc_subfield_structure where kohafield like \"items.%\" and tab >=0");
89 $sth->execute;
90 my $field;
91 my $res;
92 my $res2;
93 my $tab;
94 ($res,$res2,$field) = $sth->fetchrow;
95 my $tagfield = $res;
96 $tab = $res2;
97 my $subtotal=0;
98 #warn "TAGF : $tagfield";
99 while (($res,$res2,$field) = $sth->fetchrow) {
100         # (ignore itemnumber, that must be in -1 tab)
101         if (($res ne $tagfield) or ($res2 ne $tab)) {
102                 $subtotal++;
103         }
104 }
105 $sth = $dbh->prepare("select kohafield from marc_subfield_structure where tagfield=?");
106 $sth->execute($tagfield);
107 while (($res2) = $sth->fetchrow) {
108         if (!$res2 || $res2 =~ /^items/) {
109         } else {
110                 $subtotal++;
111         }
112 }
113 if ($subtotal == 0) {
114         $template->param(itemfields => 0);
115 } else {
116         $template->param(itemfields => 1);
117         $total++;
118 }
119
120 $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where tab = 10");
121 $sth->execute;
122 my $totaltags = 0;
123 my $list = "";
124 while (($res2) = $sth->fetchrow) {
125         $totaltags++;
126         $list.=$res2.",";
127 }
128 if ($totaltags > 1) {
129         $template->param(itemtags => $list);
130         $total++;
131 } else {
132         $template->param(itemtags => 0);
133 }
134
135
136 # checks biblioitems.itemtype must be mapped and use authorised_value=itemtype
137 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"biblioitems.itemtype\"");
138 $sth->execute;
139 while (($res,$res2,$field) = $sth->fetchrow) {
140     if ($res && $res2>=0 && $field eq "itemtypes") {
141             $template->param(itemtype => 0);
142     } else {
143             $template->param(itemtype => 1);
144             $total++;
145         last;
146     }
147 }
148
149 # checks items.homebranch must be mapped and use authorised_value=branches
150 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"items.homebranch\"");
151 $sth->execute;
152 while (($res,$res2,$field) = $sth->fetchrow) {
153     if ($res && $res2 eq 10 && $field eq "branches") {
154             $template->param(branch => 0);
155     } else {
156             $template->param(branch => 1);
157             $total++;
158         last;
159     }
160 }
161
162 # checks items.homebranch must be mapped and use authorised_value=branches
163 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"items.holdingbranch\"");
164 $sth->execute;
165 while (($res,$res2,$field) = $sth->fetchrow) {
166     if ($res && $res2 eq 10 && $field eq "branches") {
167             $template->param(holdingbranch => 0);
168     } else {
169             $template->param(holdingbranch => 1);
170             $total++;
171             last; #MR
172     }
173 }
174
175 # checks that itemtypes & branches tables are not empty
176 $sth = $dbh->prepare("select count(*) from itemtypes");
177 $sth->execute;
178 ($res) = $sth->fetchrow;
179 if ($res) {
180         $template->param(itemtypes_empty =>0);
181 } else {
182         $template->param(itemtypes_empty =>1);
183         $total++;
184 }
185
186
187 $sth = $dbh->prepare("select count(*) from branches");
188 $sth->execute;
189 ($res) = $sth->fetchrow;
190 if ($res) {
191         $template->param(branches_empty =>0);
192 } else {
193         $template->param(branches_empty =>1);
194         $total++;
195 }
196
197 $sth = $dbh->prepare("select count(*) from marc_subfield_structure where frameworkcode is NULL");
198 $sth->execute;
199 ($res) = $sth->fetchrow;
200 if ($res) {
201         $template->param(frameworknull =>1);
202         $total++;
203 }
204 $sth = $dbh->prepare("select count(*) from marc_tag_structure where frameworkcode is NULL");
205 $sth->execute;
206 ($res) = $sth->fetchrow;
207 if ($res) {
208         $template->param(frameworknull =>1);
209         $total++;
210 }
211
212 # verify that all of a field's subfields (except the ones explicitly ignored)
213 # are in the same tab
214 $sth = $dbh->prepare("SELECT tagfield, frameworkcode, frameworktext, GROUP_CONCAT(DISTINCT tab) AS tabs
215                       FROM marc_subfield_structure
216                       LEFT JOIN biblio_framework USING (frameworkcode)
217                       WHERE tab != -1
218                       GROUP BY tagfield, frameworkcode, frameworktext
219                       HAVING COUNT(DISTINCT tab) > 1");
220 $sth->execute;
221 my $inconsistent_tabs = $sth->fetchall_arrayref({});
222 if (scalar(@$inconsistent_tabs) > 0) {
223     $total++;
224     $template->param(inconsistent_tabs => 1);
225     $template->param(tab_info => $inconsistent_tabs);
226 }
227
228 # verify that authtypecodes used in the framework 
229 # are defined in auth_types
230 $sth = $dbh->prepare("SELECT frameworkcode, frameworktext, tagfield, tagsubfield, authtypecode
231                       FROM marc_subfield_structure
232                       LEFT JOIN biblio_framework USING (frameworkcode)
233                       WHERE authtypecode IS NOT NULL
234                       AND authtypecode <> ''
235                       AND tab > '-1'
236                       AND authtypecode NOT IN (SELECT authtypecode FROM auth_types)
237                       ORDER BY frameworkcode, tagfield, tagsubfield");
238 $sth->execute;
239 my $invalid_authtypecodes = $sth->fetchall_arrayref({});
240 if (scalar(@$invalid_authtypecodes) > 0) {
241     $total++;
242     $template->param(invalid_authtypecodes => 1);
243     $template->param(authtypecode_info => $invalid_authtypecodes);
244 }
245
246 # checks items.permanent_location is not mapped
247 $sth = $dbh->prepare("SELECT frameworkcode, frameworktext, tagfield, tagsubfield
248                       FROM marc_subfield_structure
249                       LEFT JOIN biblio_framework USING (frameworkcode)
250                       WHERE kohafield='permanent_location' OR
251                             kohafield='items.permanent_location'");
252 $sth->execute;
253 my $permanent_location_mapped = $sth->fetchall_arrayref({});
254 if (scalar(@$permanent_location_mapped) > 0) {
255     $total++;
256     $template->param(permanent_location_mapped => 1);
257     $template->param(mapped_permanent_location => $permanent_location_mapped);
258 }
259
260
261 $template->param(total => $total,
262                 );
263
264 output_html_with_http_headers $input, $cookie, $template->output;