Bug 9497 - Make it easier to add new encodings for Z39.50 servers and add ISO 8859-1
[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
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use C4::Output;
24 use C4::Auth;
25 use CGI;
26 use C4::Context;
27 use C4::Biblio;
28
29
30 my $input = new CGI;
31
32 my ($template, $borrowernumber, $cookie)
33     = get_template_and_user({template_name => "admin/checkmarc.tmpl",
34                              query => $input,
35                              type => "intranet",
36                              authnotrequired => 0,
37                  flagsrequired => {parameters => 'parameters_remaining_permissions'},
38                              debug => 1,
39                              });
40
41 my $dbh = C4::Context->dbh;
42 my $total = 0;
43 # checks itemnum field
44 my $sth = $dbh->prepare("select tab from marc_subfield_structure where kohafield=\"items.itemnumber\"");
45 $sth->execute;
46 while (my ($res) = $sth->fetchrow) {
47     if ($res==-1) {
48             $template->param(itemnum => 0);
49     } else {
50             $template->param(itemnum => 1);
51             $total++;
52         last;
53     }
54 }
55
56 # checks biblio.biblionumber and biblioitem.biblioitemnumber (same tag and tab=-1)
57 $sth = $dbh->prepare("select tagfield,tab,frameworkcode from marc_subfield_structure where kohafield=\"biblio.biblionumber\"");
58 $sth->execute;
59 my $first = 1;
60 my $bibliotag = '';
61 while (my ($res,$tab,$frameworkcode) = $sth->fetchrow) {
62     if ($first) {
63         $bibliotag = $res;
64         $first = 0;
65     } else {
66         if ($bibliotag != $res) {
67                 $template->param(biblionumber => 1);
68                 $total++;
69             last;
70         }
71     }
72     my $sth2 = $dbh->prepare("SELECT tagfield,tab 
73                               FROM marc_subfield_structure 
74                               WHERE kohafield=\"biblioitems.biblioitemnumber\"
75                               AND frameworkcode = ? ");
76     $sth2->execute($frameworkcode);
77     my ($res2,$tab2) = $sth2->fetchrow;
78     if ($res && $res2 && $tab==-1 && $tab2==-1) {
79             $template->param(biblionumber => 0);
80     } else {
81             $template->param(biblionumber => 1);
82             $total++;
83         last;
84     }
85 }
86
87 # checks all item fields are in the same tag and in tab 10
88
89 $sth = $dbh->prepare("select tagfield,tab,kohafield from marc_subfield_structure where kohafield like \"items.%\" and tab >=0");
90 $sth->execute;
91 my $field;
92 my $res;
93 my $res2;
94 my $tab;
95 ($res,$res2,$field) = $sth->fetchrow;
96 my $tagfield = $res;
97 $tab = $res2;
98 my $subtotal=0;
99 #warn "TAGF : $tagfield";
100 while (($res,$res2,$field) = $sth->fetchrow) {
101         # (ignore itemnumber, that must be in -1 tab)
102         if (($res ne $tagfield) or ($res2 ne $tab)) {
103                 $subtotal++;
104         }
105 }
106 $sth = $dbh->prepare("select kohafield from marc_subfield_structure where tagfield=?");
107 $sth->execute($tagfield);
108 while (($res2) = $sth->fetchrow) {
109         if (!$res2 || $res2 =~ /^items/) {
110         } else {
111                 $subtotal++;
112         }
113 }
114 if ($subtotal == 0) {
115         $template->param(itemfields => 0);
116 } else {
117         $template->param(itemfields => 1);
118         $total++;
119 }
120
121 $sth = $dbh->prepare("select distinct tagfield from marc_subfield_structure where tab = 10");
122 $sth->execute;
123 my $totaltags = 0;
124 my $list = "";
125 while (($res2) = $sth->fetchrow) {
126         $totaltags++;
127         $list.=$res2.",";
128 }
129 if ($totaltags > 1) {
130         $template->param(itemtags => $list);
131         $total++;
132 } else {
133         $template->param(itemtags => 0);
134 }
135
136
137 # checks biblioitems.itemtype must be mapped and use authorised_value=itemtype
138 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"biblioitems.itemtype\"");
139 $sth->execute;
140 while (($res,$res2,$field) = $sth->fetchrow) {
141     if ($res && $res2>=0 && $field eq "itemtypes") {
142             $template->param(itemtype => 0);
143     } else {
144             $template->param(itemtype => 1);
145             $total++;
146         last;
147     }
148 }
149
150 # checks items.homebranch must be mapped and use authorised_value=branches
151 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"items.homebranch\"");
152 $sth->execute;
153 while (($res,$res2,$field) = $sth->fetchrow) {
154     if ($res && $res2 eq 10 && $field eq "branches") {
155             $template->param(branch => 0);
156     } else {
157             $template->param(branch => 1);
158             $total++;
159         last;
160     }
161 }
162
163 # checks items.homebranch must be mapped and use authorised_value=branches
164 $sth = $dbh->prepare("select tagfield,tab,authorised_value from marc_subfield_structure where kohafield = \"items.holdingbranch\"");
165 $sth->execute;
166 while (($res,$res2,$field) = $sth->fetchrow) {
167     if ($res && $res2 eq 10 && $field eq "branches") {
168             $template->param(holdingbranch => 0);
169     } else {
170             $template->param(holdingbranch => 1);
171             $total++;
172             last; #MR
173     }
174 }
175
176 # checks that itemtypes & branches tables are not empty
177 $sth = $dbh->prepare("select count(*) from itemtypes");
178 $sth->execute;
179 ($res) = $sth->fetchrow;
180 if ($res) {
181         $template->param(itemtypes_empty =>0);
182 } else {
183         $template->param(itemtypes_empty =>1);
184         $total++;
185 }
186
187
188 $sth = $dbh->prepare("select count(*) from branches");
189 $sth->execute;
190 ($res) = $sth->fetchrow;
191 if ($res) {
192         $template->param(branches_empty =>0);
193 } else {
194         $template->param(branches_empty =>1);
195         $total++;
196 }
197
198 $sth = $dbh->prepare("select count(*) from marc_subfield_structure where frameworkcode is NULL");
199 $sth->execute;
200 ($res) = $sth->fetchrow;
201 if ($res) {
202         $template->param(frameworknull =>1);
203         $total++;
204 }
205 $sth = $dbh->prepare("select count(*) from marc_tag_structure where frameworkcode is NULL");
206 $sth->execute;
207 ($res) = $sth->fetchrow;
208 if ($res) {
209         $template->param(frameworknull =>1);
210         $total++;
211 }
212
213 # verify that all of a field's subfields (except the ones explicitly ignored)
214 # are in the same tab
215 $sth = $dbh->prepare("SELECT tagfield, frameworkcode, frameworktext, GROUP_CONCAT(DISTINCT tab) AS tabs
216                       FROM marc_subfield_structure
217                       LEFT JOIN biblio_framework USING (frameworkcode)
218                       WHERE tab != -1
219                       GROUP BY tagfield, frameworkcode, frameworktext
220                       HAVING COUNT(DISTINCT tab) > 1");
221 $sth->execute;
222 my $inconsistent_tabs = $sth->fetchall_arrayref({});
223 if (scalar(@$inconsistent_tabs) > 0) {
224     $total++;
225     $template->param(inconsistent_tabs => 1);
226     $template->param(tab_info => $inconsistent_tabs);
227 }
228
229 # verify that authtypecodes used in the framework 
230 # are defined in auth_types
231 $sth = $dbh->prepare("SELECT frameworkcode, frameworktext, tagfield, tagsubfield, authtypecode
232                       FROM marc_subfield_structure
233                       LEFT JOIN biblio_framework USING (frameworkcode)
234                       WHERE authtypecode IS NOT NULL
235                       AND authtypecode <> ''
236                       AND tab > '-1'
237                       AND authtypecode NOT IN (SELECT authtypecode FROM auth_types)
238                       ORDER BY frameworkcode, tagfield, tagsubfield");
239 $sth->execute;
240 my $invalid_authtypecodes = $sth->fetchall_arrayref({});
241 if (scalar(@$invalid_authtypecodes) > 0) {
242     $total++;
243     $template->param(invalid_authtypecodes => 1);
244     $template->param(authtypecode_info => $invalid_authtypecodes);
245 }
246
247 $template->param(total => $total,
248                 );
249
250 output_html_with_http_headers $input, $cookie, $template->output;