First step of independacy branch management.
[koha.git] / import / breeding.pl
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Script for handling import of MARC data into Koha db
6 #   and Z39.50 lookups
7
8 # Koha library project  www.koha.org
9
10 # Licensed under the GPL
11
12
13 # Copyright 2000-2002 Katipo Communications
14 #
15 # This file is part of Koha.
16 #
17 # Koha is free software; you can redistribute it and/or modify it under the
18 # terms of the GNU General Public License as published by the Free Software
19 # Foundation; either version 2 of the License, or (at your option) any later
20 # version.
21 #
22 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
23 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
24 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License along with
27 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
28 # Suite 330, Boston, MA  02111-1307 USA
29
30 use strict;
31
32 # standard or CPAN modules used
33 use CGI;
34 use DBI;
35
36 # Koha modules used
37 use C4::Context;
38 use C4::Output;
39 use C4::Interface::CGI::Output;
40 use C4::Input;
41 use C4::Biblio;
42 use MARC::File::USMARC;
43 use HTML::Template;
44 use C4::Output;
45 use C4::Auth;
46 use C4::Breeding;
47
48 #------------------
49 # Constants
50
51 my $includes = C4::Context->config('includes') ||
52         "/usr/local/www/hdl/htdocs/includes";
53
54 # HTML colors for alternating lines
55 my $lc1='#dddddd';
56 my $lc2='#ddaaaa';
57
58 #-------------
59 #-------------
60 # Initialize
61
62 my $userid=$ENV{'REMOTE_USER'};
63
64 my $input = new CGI;
65 my $dbh = C4::Context->dbh;
66
67 my $uploadmarc=$input->param('uploadmarc');
68 my $overwrite_biblio = $input->param('overwrite_biblio');
69 my $filename = $input->param('filename');
70 my $syntax = $input->param('syntax');
71 my ($template, $loggedinuser, $cookie)
72         = get_template_and_user({template_name => "import/breeding.tmpl",
73                                         query => $input,
74                                         type => "intranet",
75                                         authnotrequired => 0,
76                                         flagsrequired => {parameters => 1, management => 1, tools => 1},
77                                         debug => 1,
78                                         });
79
80 $template->param(SCRIPT_NAME => $ENV{'SCRIPT_NAME'},
81                                                 uploadmarc => $uploadmarc);
82 if ($uploadmarc && length($uploadmarc)>0) {
83         my $marcrecord='';
84         while (<$uploadmarc>) {
85                 $marcrecord.=$_;
86         }
87         my ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported) = ImportBreeding($marcrecord,$overwrite_biblio,$filename,$syntax,int(rand(99999)));
88
89         $template->param(imported => $imported,
90                                                         alreadyindb => $alreadyindb,
91                                                         alreadyinfarm => $alreadyinfarm,
92                                                         notmarcrecord => $notmarcrecord,
93                                                         total => $imported+$alreadyindb+$alreadyinfarm+$notmarcrecord,
94                                                         );
95
96 }
97
98 output_html_with_http_headers $input, $cookie, $template->output;
99 my $menu;
100 my $file;
101
102
103 #---------------
104 # log cleared, as marcimport is (almost) rewritten from scratch.
105 # $Log$
106 # Revision 1.4  2005/05/04 08:52:13  tipaul
107 # synch'ing 2.2 and head
108 #
109 # Revision 1.3  2005/03/23 09:57:47  doxulting
110 # Adding a parameter to allow acces to people with management/tools flags
111 #
112 # Revision 1.2  2003/10/06 09:10:38  slef
113 # Removing config info from z3950*sh and using C4::Context in processz3950queue (Fixed bug 39)
114 #
115 # Revision 1.1  2003/06/04 13:46:25  tipaul
116 # moving breeding farm import to parameters page (GUI) and to new import/ directory (code structure)
117 #
118 # Revision 1.33  2003/04/29 16:48:36  tipaul
119 # really proud of this commit :-)
120 # z3950 search and import seems to works fine.
121 # Let me explain how :
122 # * a "search z3950" button is added in the addbiblio template.
123 # * when clicked, a popup appears and z3950/search.pl is called
124 # * z3950/search.pl calls addz3950search in the DB
125 # * the z3950 daemon retrieve the records and stores them in z3950results AND in marc_breeding table.
126 # * as long as there as searches pending, the popup auto refresh every 2 seconds, and says how many searches are pending.
127 # * when the user clicks on a z3950 result => the parent popup is called with the requested biblio, and auto-filled
128 #
129 # Note :
130 # * character encoding support : (It's a nightmare...) In the z3950servers table, a "encoding" column has been added. You can put "UNIMARC" or "USMARC" in this column. Depending on this, the char_decode in C4::Biblio.pm replaces marc-char-encode by an iso 8859-1 encoding. Note that in the breeding import this value has been added too, for a better support.
131 # * the marc_breeding and z3950* tables have been modified : they have an encoding column and the random z3950 number is stored too for convenience => it's the key I use to list only requested biblios in the popup.
132 #
133 # Revision 1.32  2003/04/22 12:22:54  tipaul
134 # 1st draft for z3950 client import.
135 # moving Breeding farm script to a perl package C4/Breeding.pm
136 #
137 # Revision 1.31  2003/02/19 01:01:07  wolfpac444
138 # Removed the unecessary $dbh argument from being passed.
139 # Resolved a few minor FIXMEs.
140 #
141 # Revision 1.30  2003/02/02 07:18:38  acli
142 # Moved C4/Charset.pm to C4/Interface/CGI/Output.pm
143 #
144 # Create output_html_with_http_headers function to contain the "print $query
145 # ->header(-type => guesstype...),..." call. This is in preparation for
146 # non-HTML output (e.g., text/xml) and charset conversion before output in
147 # the future.
148 #
149 # Created C4/Interface/CGI/Template.pm to hold convenience functions specific
150 # to the CGI interface using HTML::Template
151 #
152 # Modified moremembers.pl to make the "sex" field localizable for languages
153 # where M and F doesn't make sense
154 #
155 # Revision 1.29  2003/01/28 15:28:31  tipaul
156 # removing use MARC::Charset
157 # Was a buggy test
158 #
159 # Revision 1.28  2003/01/28 15:00:31  tipaul
160 # user can now search in breeding farm with isbn/issn or title. Title/name are stored in breeding farm and showed when a search is done
161 #
162 # Revision 1.27  2003/01/26 23:21:49  acli
163 # Handle non-latin1 charsets
164 #
165 # Revision 1.26  2003/01/23 12:26:41  tipaul
166 # upgrading import in breeding farm (you can now search on ISBN or on title) AND character encoding.
167 #
168 # Revision 1.25  2003/01/21 08:13:50  tipaul
169 # character encoding ISO646 => 8859-1, first draft
170 #
171 # Revision 1.24  2003/01/14 16:41:17  tipaul
172 # bugfix : use gettemplate_and_user instead of gettemplate.
173 # fix a blank screen in 1.3.3 in "import in breeding farm"
174 #
175 # Revision 1.23  2003/01/06 13:06:28  tipaul
176 # removing trailing #
177 #
178 # Revision 1.22  2002/11/12 15:58:43  tipaul
179 # road to 1.3.2 :
180 # * many bugfixes
181 # * adding value_builder : you can map a subfield in the marc_subfield_structure to a sub stored in "value_builder" directory. In this directory you can create screen used to build values with any method. In this commit is a 1st draft of the builder for 100$a unimarc french subfield, which is composed of 35 digits, with 12 differents values (only the 4th first are provided for instance)
182 #
183 # Revision 1.21  2002/10/22 15:50:23  tipaul
184 # road to 1.3.2 : adding a biblio in MARC format.
185 # seems to work a few.
186 # still to do :
187 # * manage html checks (mandatory subfields...)
188 # * add list of acceptable values (authorities)
189 # * manage ## in MARC format
190 # * manage correctly repeatable fields
191 # and probably a LOT of bugfixes
192 #
193 # Revision 1.20  2002/10/16 12:46:19  arensb
194 # Added a FIXME comment.
195 #
196 # Revision 1.19  2002/10/15 10:14:44  tipaul
197 # road to 1.3.2. Full rewrite of marcimport.pl.
198 # The acquisition system in MARC version will work like this :
199 # * marcimport will put marc records into a "breeding farm" table.
200 # * when the user want to add a biblio, he enters first the ISBN/ISSN of the biblio. koha searches into breeding farm and if the record exists, it is shown to the user to help him adding the biblio. When the biblio is added, it's deleted from the breeding farm.
201 #
202 # This commit :
203 # * modify acqui.simple home page  (addbooks.pl)
204 # * adds import into breeding farm
205 #
206 # Please note that :
207 # * z3950 functionnality is dropped from "marcimport" will be added somewhere else.
208 # * templates are in a new acqui.simple sub directory, and the marcimport template directory will become obsolete soon.I think this is more logic
209 #
210 #!/usr/bin/perl
211
212 # $Id$
213
214 # Script for handling import of MARC data into Koha db
215 #   and Z39.50 lookups
216
217 # Koha library project  www.koha.org
218
219 # Licensed under the GPL
220
221
222 # Copyright 2000-2002 Katipo Communications
223 #
224 # This file is part of Koha.
225 #
226 # Koha is free software; you can redistribute it and/or modify it under the
227 # terms of the GNU General Public License as published by the Free Software
228 # Foundation; either version 2 of the License, or (at your option) any later
229 # version.
230 #
231 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
232 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
233 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
234 #
235 # You should have received a copy of the GNU General Public License along with
236 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
237 # Suite 330, Boston, MA  02111-1307 USA
238
239 use strict;
240
241 # standard or CPAN modules used
242 use CGI;
243 use DBI;
244
245 # Koha modules used
246 use C4::Context;
247 use C4::Output;
248 use C4::Interface::CGI::Output;
249 use C4::Input;
250 use C4::Biblio;
251 use MARC::File::USMARC;
252 use HTML::Template;
253 use C4::Output;
254 use C4::Auth;
255 use C4::Breeding;
256
257 #------------------
258 # Constants
259
260 my $includes = C4::Context->config('includes') ||
261         "/usr/local/www/hdl/htdocs/includes";
262
263 # HTML colors for alternating lines
264 my $lc1='#dddddd';
265 my $lc2='#ddaaaa';
266
267 #-------------
268 #-------------
269 # Initialize
270
271 my $userid=$ENV{'REMOTE_USER'};
272
273 my $input = new CGI;
274 my $dbh = C4::Context->dbh;
275
276 my $uploadmarc=$input->param('uploadmarc');
277 my $overwrite_biblio = $input->param('overwrite_biblio');
278 my $filename = $input->param('filename');
279 my $syntax = $input->param('syntax');
280 my ($template, $loggedinuser, $cookie)
281         = get_template_and_user({template_name => "import/breeding.tmpl",
282                                         query => $input,
283                                         type => "intranet",
284                                         authnotrequired => 0,
285                                         flagsrequired => {parameters => 1, management => 1, tools => 1},
286                                         debug => 1,
287                                         });
288
289 $template->param(SCRIPT_NAME => $ENV{'SCRIPT_NAME'},
290                                                 uploadmarc => $uploadmarc);
291 if ($uploadmarc && length($uploadmarc)>0) {
292         my $marcrecord='';
293         while (<$uploadmarc>) {
294                 $marcrecord.=$_;
295         }
296         my ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported) = ImportBreeding($marcrecord,$overwrite_biblio,$filename,$syntax,int(rand(99999)));
297
298         $template->param(imported => $imported,
299                                                         alreadyindb => $alreadyindb,
300                                                         alreadyinfarm => $alreadyinfarm,
301                                                         notmarcrecord => $notmarcrecord,
302                                                         total => $imported+$alreadyindb+$alreadyinfarm+$notmarcrecord,
303                                                         );
304
305 }
306
307 output_html_with_http_headers $input, $cookie, $template->output;
308 my $menu;
309 my $file;
310
311
312 #---------------
313 # log cleared, as marcimport is (almost) rewritten from scratch.
314 # $Log$
315 # Revision 1.4  2005/05/04 08:52:13  tipaul
316 # synch'ing 2.2 and head
317 #
318 # Revision 1.2.4.1  2005/04/07 10:10:52  tipaul
319 # copying processz3950queue from 2.0 branch. The 2.2 version misses an important fix
320 #
321 # Revision 1.2  2003/10/06 09:10:38  slef
322 # Removing config info from z3950*sh and using C4::Context in processz3950queue (Fixed bug 39)
323 #
324 # Revision 1.1  2003/06/04 13:46:25  tipaul
325 # moving breeding farm import to parameters page (GUI) and to new import/ directory (code structure)
326 #
327 # Revision 1.33  2003/04/29 16:48:36  tipaul
328 # really proud of this commit :-)
329 # z3950 search and import seems to works fine.
330 # Let me explain how :
331 # * a "search z3950" button is added in the addbiblio template.
332 # * when clicked, a popup appears and z3950/search.pl is called
333 # * z3950/search.pl calls addz3950search in the DB
334 # * the z3950 daemon retrieve the records and stores them in z3950results AND in marc_breeding table.
335 # * as long as there as searches pending, the popup auto refresh every 2 seconds, and says how many searches are pending.
336 # * when the user clicks on a z3950 result => the parent popup is called with the requested biblio, and auto-filled
337 #
338 # Note :
339 # * character encoding support : (It's a nightmare...) In the z3950servers table, a "encoding" column has been added. You can put "UNIMARC" or "USMARC" in this column. Depending on this, the char_decode in C4::Biblio.pm replaces marc-char-encode by an iso 8859-1 encoding. Note that in the breeding import this value has been added too, for a better support.
340 # * the marc_breeding and z3950* tables have been modified : they have an encoding column and the random z3950 number is stored too for convenience => it's the key I use to list only requested biblios in the popup.
341 #
342 # Revision 1.32  2003/04/22 12:22:54  tipaul
343 # 1st draft for z3950 client import.
344 # moving Breeding farm script to a perl package C4/Breeding.pm
345 #
346 # Revision 1.31  2003/02/19 01:01:07  wolfpac444
347 # Removed the unecessary $dbh argument from being passed.
348 # Resolved a few minor FIXMEs.
349 #
350 # Revision 1.30  2003/02/02 07:18:38  acli
351 # Moved C4/Charset.pm to C4/Interface/CGI/Output.pm
352 #
353 # Create output_html_with_http_headers function to contain the "print $query
354 # ->header(-type => guesstype...),..." call. This is in preparation for
355 # non-HTML output (e.g., text/xml) and charset conversion before output in
356 # the future.
357 #
358 # Created C4/Interface/CGI/Template.pm to hold convenience functions specific
359 # to the CGI interface using HTML::Template
360 #
361 # Modified moremembers.pl to make the "sex" field localizable for languages
362 # where M and F doesn't make sense
363 #
364 # Revision 1.29  2003/01/28 15:28:31  tipaul
365 # removing use MARC::Charset
366 # Was a buggy test
367 #
368 # Revision 1.28  2003/01/28 15:00:31  tipaul
369 # user can now search in breeding farm with isbn/issn or title. Title/name are stored in breeding farm and showed when a search is done
370 #
371 # Revision 1.27  2003/01/26 23:21:49  acli
372 # Handle non-latin1 charsets
373 #
374 # Revision 1.26  2003/01/23 12:26:41  tipaul
375 # upgrading import in breeding farm (you can now search on ISBN or on title) AND character encoding.
376 #
377 # Revision 1.25  2003/01/21 08:13:50  tipaul
378 # character encoding ISO646 => 8859-1, first draft
379 #
380 # Revision 1.24  2003/01/14 16:41:17  tipaul
381 # bugfix : use gettemplate_and_user instead of gettemplate.
382 # fix a blank screen in 1.3.3 in "import in breeding farm"
383 #
384 # Revision 1.23  2003/01/06 13:06:28  tipaul
385 # removing trailing #
386 #
387 # Revision 1.22  2002/11/12 15:58:43  tipaul
388 # road to 1.3.2 :
389 # * many bugfixes
390 # * adding value_builder : you can map a subfield in the marc_subfield_structure to a sub stored in "value_builder" directory. In this directory you can create screen used to build values with any method. In this commit is a 1st draft of the builder for 100$a unimarc french subfield, which is composed of 35 digits, with 12 differents values (only the 4th first are provided for instance)
391 #
392 # Revision 1.21  2002/10/22 15:50:23  tipaul
393 # road to 1.3.2 : adding a biblio in MARC format.
394 # seems to work a few.
395 # still to do :
396 # * manage html checks (mandatory subfields...)
397 # * add list of acceptable values (authorities)
398 # * manage ## in MARC format
399 # * manage correctly repeatable fields
400 # and probably a LOT of bugfixes
401 #
402 # Revision 1.20  2002/10/16 12:46:19  arensb
403 # Added a FIXME comment.
404 #
405 # Revision 1.19  2002/10/15 10:14:44  tipaul
406 # road to 1.3.2. Full rewrite of marcimport.pl.
407 # The acquisition system in MARC version will work like this :
408 # * marcimport will put marc records into a "breeding farm" table.
409 # * when the user want to add a biblio, he enters first the ISBN/ISSN of the biblio. koha searches into breeding farm and if the record exists, it is shown to the user to help him adding the biblio. When the biblio is added, it's deleted from the breeding farm.
410 #
411 # This commit :
412 # * modify acqui.simple home page  (addbooks.pl)
413 # * adds import into breeding farm
414 #
415 # Please note that :
416 # * z3950 functionnality is dropped from "marcimport" will be added somewhere else.
417 # * templates are in a new acqui.simple sub directory, and the marcimport template directory will become obsolete soon.I think this is more logic
418 #