Bug 14224: Check if the columns do not exist before creating them
[koha.git] / admin / checkmarc.pl
blob456f22bea92fc366cd75aca9bd761d9aa9405e1d
1 #!/usr/bin/perl
4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
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.
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.
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>.
21 use strict;
22 #use warnings; FIXME - Bug 2505
23 use C4::Output;
24 use C4::Auth;
25 use CGI qw ( -utf8 );
26 use C4::Context;
27 use C4::Biblio;
30 my $input = new CGI;
32 my ($template, $borrowernumber, $cookie)
33 = get_template_and_user({template_name => "admin/checkmarc.tt",
34 query => $input,
35 type => "intranet",
36 authnotrequired => 0,
37 flagsrequired => {parameters => 'parameters_remaining_permissions'},
38 debug => 1,
39 });
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;
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;
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;
87 # checks all item fields are in the same tag and in tab 10
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++;
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++;
114 if ($subtotal == 0) {
115 $template->param(itemfields => 0);
116 } else {
117 $template->param(itemfields => 1);
118 $total++;
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.",";
129 if ($totaltags > 1) {
130 $template->param(itemtags => $list);
131 $total++;
132 } else {
133 $template->param(itemtags => 0);
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;
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;
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
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++;
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++;
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++;
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++;
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);
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);
247 # checks items.permanent_location is not mapped
248 $sth = $dbh->prepare("SELECT frameworkcode, frameworktext, tagfield, tagsubfield
249 FROM marc_subfield_structure
250 LEFT JOIN biblio_framework USING (frameworkcode)
251 WHERE kohafield='permanent_location' OR
252 kohafield='items.permanent_location'");
253 $sth->execute;
254 my $permanent_location_mapped = $sth->fetchall_arrayref({});
255 if (scalar(@$permanent_location_mapped) > 0) {
256 $total++;
257 $template->param(permanent_location_mapped => 1);
258 $template->param(mapped_permanent_location => $permanent_location_mapped);
262 $template->param(total => $total,
265 output_html_with_http_headers $input, $cookie, $template->output;