Authtypecode return an warning even if subfield is ignored
[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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use C4::Output;
23 use C4::Auth;
24 use CGI;
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.tmpl",
33                              query => $input,
34                              type => "intranet",
35                              authnotrequired => 0,
36                              flagsrequired => {parameters => 1},
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     }
172 }
173
174 # checks that itemtypes & branches tables are not empty
175 $sth = $dbh->prepare("select count(*) from itemtypes");
176 $sth->execute;
177 ($res) = $sth->fetchrow;
178 if ($res) {
179         $template->param(itemtypes_empty =>0);
180 } else {
181         $template->param(itemtypes_empty =>1);
182         $total++;
183 }
184
185
186 $sth = $dbh->prepare("select count(*) from branches");
187 $sth->execute;
188 ($res) = $sth->fetchrow;
189 if ($res) {
190         $template->param(branches_empty =>0);
191 } else {
192         $template->param(branches_empty =>1);
193         $total++;
194 }
195
196 $sth = $dbh->prepare("select count(*) from marc_subfield_structure where frameworkcode is NULL");
197 $sth->execute;
198 ($res) = $sth->fetchrow;
199 if ($res) {
200         $template->param(frameworknull =>1);
201         $total++;
202 }
203 $sth = $dbh->prepare("select count(*) from marc_tag_structure where frameworkcode is NULL");
204 $sth->execute;
205 ($res) = $sth->fetchrow;
206 if ($res) {
207         $template->param(frameworknull =>1);
208         $total++;
209 }
210
211 # verify that all of a field's subfields (except the ones explicitly ignored)
212 # are in the same tab
213 $sth = $dbh->prepare("SELECT tagfield, frameworkcode, frameworktext, GROUP_CONCAT(DISTINCT tab) AS tabs
214                       FROM marc_subfield_structure
215                       LEFT JOIN biblio_framework USING (frameworkcode)
216                       WHERE tab != -1
217                       GROUP BY tagfield, frameworkcode, frameworktext
218                       HAVING COUNT(DISTINCT tab) > 1");
219 $sth->execute;
220 my $inconsistent_tabs = $sth->fetchall_arrayref({});
221 if (scalar(@$inconsistent_tabs) > 0) {
222     $total++;
223     $template->param(inconsistent_tabs => 1);
224     $template->param(tab_info => $inconsistent_tabs);
225 }
226
227 # verify that authtypecodes used in the framework 
228 # are defined in auth_types
229 $sth = $dbh->prepare("SELECT frameworkcode, frameworktext, tagfield, tagsubfield, authtypecode
230                       FROM marc_subfield_structure
231                       LEFT JOIN biblio_framework USING (frameworkcode)
232                       WHERE authtypecode IS NOT NULL
233                       AND authtypecode <> ''
234                       AND tab > '-1'
235                       AND authtypecode NOT IN (SELECT authtypecode FROM auth_types)
236                       ORDER BY frameworkcode, tagfield, tagsubfield");
237 $sth->execute;
238 my $invalid_authtypecodes = $sth->fetchall_arrayref({});
239 if (scalar(@$invalid_authtypecodes) > 0) {
240     $total++;
241     $template->param(invalid_authtypecodes => 1);
242     $template->param(authtypecode_info => $invalid_authtypecodes);
243 }
244
245 $template->param(total => $total,
246                 );
247
248 output_html_with_http_headers $input, $cookie, $template->output;