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