adding frameworkcode to API in some subs
[koha.git] / C4 / Biblio.pm
1 package C4::Biblio;
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 require Exporter;
22 use C4::Context;
23 use C4::Database;
24 use MARC::Record;
25
26 use vars qw($VERSION @ISA @EXPORT);
27
28 # set the version for version checking
29 $VERSION = 0.01;
30
31 @ISA = qw(Exporter);
32
33 #
34 # don't forget MARCxxx subs are exported only for testing purposes. Should not be used
35 # as the old-style API and the NEW one are the only public functions.
36 #
37 @EXPORT = qw(
38   &updateBiblio &updateBiblioItem &updateItem
39   &itemcount &newbiblio &newbiblioitem
40   &modnote &newsubject &newsubtitle
41   &modbiblio &checkitems
42   &newitems &modbibitem
43   &modsubtitle &modsubject &modaddauthor &moditem &countitems
44   &delitem &deletebiblioitem &delbiblio
45   &getbiblio
46   &getbiblioitembybiblionumber
47   &getbiblioitem &getitemsbybiblioitem
48   &skip &getitemtypes
49   &newcompletebiblioitem
50
51   &MARCfind_oldbiblionumber_from_MARCbibid
52   &MARCfind_MARCbibid_from_oldbiblionumber
53   &MARCfind_marc_from_kohafield
54   &MARCfindsubfield
55   &MARCfind_frameworkcode
56   &MARCgettagslib
57
58   &NEWnewbiblio &NEWnewitem
59   &NEWmodbiblio &NEWmoditem
60   &NEWdelbiblio &NEWdelitem
61
62   &MARCaddbiblio &MARCadditem
63   &MARCmodsubfield &MARCaddsubfield
64   &MARCmodbiblio &MARCmoditem
65   &MARCkoha2marcBiblio &MARCmarc2koha
66   &MARCkoha2marcItem &MARChtml2marc
67   &MARCgetbiblio &MARCgetitem
68   &MARCaddword &MARCdelword
69   &char_decode
70 );
71
72 #
73 #
74 # MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC
75 #
76 #
77 # all the following subs takes a MARC::Record as parameter and manage
78 # the MARC-DB. They are called by the 1.0/1.2 xxx subs, and by the
79 # NEWxxx subs (xxx deals with old-DB parameters, the NEWxxx deals with MARC-DB parameter)
80
81 =head1 NAME
82
83 C4::Biblio - acquisition, catalog  management functions
84
85 =head1 SYNOPSIS
86
87 move from 1.2 to 1.4 version :
88 1.2 and previous version uses a specific API to manage biblios. This API uses old-DB style parameters.
89 In the 1.4 version, we want to do 2 differents things :
90  - keep populating the old-DB, that has a LOT less datas than MARC
91  - populate the MARC-DB
92 To populate the DBs we have 2 differents sources :
93  - the standard acquisition system (through book sellers), that does'nt use MARC data
94  - the MARC acquisition system, that uses MARC data.
95
96 Thus, we have 2 differents cases :
97 - with the standard acquisition system, we have non MARC data and want to populate old-DB and MARC-DB, knowing it's an incomplete MARC-record
98 - with the MARC acquisition system, we have MARC datas, and want to loose nothing in MARC-DB. So, we can't store datas in old-DB, then copy in MARC-DB. we MUST have an API for true MARC data, that populate MARC-DB then old-DB
99
100 That's why we need 4 subs :
101 all I<subs beginning by MARC> manage only MARC tables. They manage MARC-DB with MARC::Record parameters
102 all I<subs beginning by OLD> manage only OLD-DB tables. They manage old-DB with old-DB parameters
103 all I<subs beginning by NEW> manage both OLD-DB and MARC tables. They use MARC::Record as parameters. it's the API that MUST be used in MARC acquisition system
104 all I<subs beginning by seomething else> are the old-style API. They use old-DB as parameter, then call internally the OLD and MARC subs.
105
106 - NEW and old-style API should be used in koha to manage biblio
107 - MARCsubs are divided in 2 parts :
108 * some of them manage MARC parameters. They are heavily used in koha.
109 * some of them manage MARC biblio : they are mostly used by NEW and old-style subs.
110 - OLD are used internally only
111
112 all subs requires/use $dbh as 1st parameter.
113
114 I<NEWxxx related subs>
115
116 all subs requires/use $dbh as 1st parameter.
117 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
118
119 I<OLDxxx related subs>
120
121 all subs requires/use $dbh as 1st parameter.
122 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
123
124 They all are the exact copy of 1.0/1.2 version of the sub without the OLD.
125 The OLDxxx is called by the original xxx sub.
126 the 1.4 xxx sub also builds MARC::Record an calls the MARCxxx
127
128 WARNING : there is 1 difference between initialxxx and OLDxxx :
129 the db header $dbh is always passed as parameter to avoid over-DB connexion
130
131 =head1 DESCRIPTION
132
133 =over 4
134
135 =item @tagslib = &MARCgettagslib($dbh,1|0,$itemtype);
136
137 last param is 1 for liblibrarian and 0 for libopac
138 $itemtype contains the itemtype framework reference. If empty or does not exist, the default one is used
139 returns a hash with tag/subfield meaning
140 =item ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,$kohafield);
141
142 finds MARC tag and subfield for a given kohafield
143 kohafield is "table.field" where table= biblio|biblioitems|items, and field a field of the previous table
144
145 =item $biblionumber = &MARCfind_oldbiblionumber_from_MARCbibid($dbh,$MARCbibi);
146
147 finds a old-db biblio number for a given MARCbibid number
148
149 =item $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
150
151 finds a MARC bibid from a old-db biblionumber
152
153 =item $MARCRecord = &MARCkoha2marcBiblio($dbh,$biblionumber,biblioitemnumber);
154
155 MARCkoha2marcBiblio is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB biblio/biblioitem
156
157 =item $MARCRecord = &MARCkoha2marcItem($dbh,$biblionumber,itemnumber);
158
159 MARCkoha2marcItem is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB item
160
161 =item $MARCRecord = &MARCkoha2marcSubtitle($dbh,$biblionumber,$subtitle);
162
163 MARCkoha2marcSubtitle is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB subtitle
164
165 =item $olddb = &MARCmarc2koha($dbh,$MARCRecord);
166
167 builds a hash with old-db datas from a MARC::Record
168
169 =item &MARCaddbiblio($dbh,$MARC::Record,$biblionumber);
170
171 creates a biblio (in the MARC tables only). $biblionumber is the old-db biblionumber of the biblio
172
173 =item &MARCaddsubfield($dbh,$bibid,$tagid,$indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
174
175 adds a subfield in a biblio (in the MARC tables only).
176
177 =item $MARCRecord = &MARCgetbiblio($dbh,$bibid);
178
179 Returns a MARC::Record for the biblio $bibid.
180
181 =item &MARCmodbiblio($dbh,$bibid,$record,$frameworkcode,$delete);
182
183 MARCmodbiblio changes a biblio for a biblio,MARC::Record passed as parameter
184 It 1st delete the biblio, then recreates it.
185 WARNING : the $delete parameter is not used anymore (too much unsolvable cases).
186 =item ($subfieldid,$subfieldvalue) = &MARCmodsubfield($dbh,$subfieldid,$subfieldvalue);
187
188 MARCmodsubfield changes the value of a given subfield
189
190 =item $subfieldid = &MARCfindsubfield($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue);
191
192 MARCfindsubfield returns a subfield number given a bibid/tag/subfieldvalue values.
193 Returns -1 if more than 1 answer
194
195 =item $subfieldid = &MARCfindsubfieldid($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
196
197 MARCfindsubfieldid find a subfieldid for a bibid/tag/tagorder/subfield/subfieldorder
198
199 =item &MARCdelsubfield($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
200
201 MARCdelsubfield delete a subfield for a bibid/tag/tagorder/subfield/subfieldorder
202
203 =item &MARCdelbiblio($dbh,$bibid);
204
205 MARCdelbiblio delete biblio $bibid
206
207 =item &MARCkoha2marcOnefield
208
209 used by MARCkoha2marc and should not be useful elsewhere
210
211 =item &MARCmarc2kohaOnefield
212
213 used by MARCmarc2koha and should not be useful elsewhere
214
215 =item MARCaddword
216
217 used to manage MARC_word table and should not be useful elsewhere
218
219 =item MARCdelword
220
221 used to manage MARC_word table and should not be useful elsewhere
222
223 =cut
224
225 sub MARCgettagslib {
226     my ( $dbh, $forlibrarian, $frameworkcode ) = @_;
227     $frameworkcode = "" unless $frameworkcode;
228     my $sth;
229     my $libfield = ( $forlibrarian eq 1 ) ? 'liblibrarian' : 'libopac';
230
231     # check that framework exists
232     $sth =
233       $dbh->prepare(
234         "select count(*) from marc_tag_structure where frameworkcode=?");
235     $sth->execute($frameworkcode);
236     my ($total) = $sth->fetchrow;
237     $frameworkcode = "" unless ( $total > 0 );
238     $sth =
239       $dbh->prepare(
240 "select tagfield,$libfield as lib,mandatory,repeatable from marc_tag_structure where frameworkcode=? order by tagfield"
241     );
242     $sth->execute($frameworkcode);
243     my ( $lib, $tag, $res, $tab, $mandatory, $repeatable );
244
245     while ( ( $tag, $lib, $mandatory, $repeatable ) = $sth->fetchrow ) {
246         $res->{$tag}->{lib}        = $lib;
247         $res->{$tab}->{tab}        = "";            # XXX
248         $res->{$tag}->{mandatory}  = $mandatory;
249         $res->{$tag}->{repeatable} = $repeatable;
250     }
251
252     $sth =
253       $dbh->prepare(
254 "select tagfield,tagsubfield,$libfield as lib,tab, mandatory, repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl from marc_subfield_structure where frameworkcode=? order by tagfield,tagsubfield"
255     );
256     $sth->execute($frameworkcode);
257
258     my $subfield;
259     my $authorised_value;
260     my $authtypecode;
261     my $value_builder;
262     my $kohafield;
263     my $seealso;
264     my $hidden;
265     my $isurl;
266
267     while (
268         ( $tag,         $subfield,   $lib,              $tab,
269         $mandatory,     $repeatable, $authorised_value, $authtypecode,
270         $value_builder, $kohafield,  $seealso,          $hidden,
271         $isurl )
272         = $sth->fetchrow
273       )
274     {
275         $res->{$tag}->{$subfield}->{lib}              = $lib;
276         $res->{$tag}->{$subfield}->{tab}              = $tab;
277         $res->{$tag}->{$subfield}->{mandatory}        = $mandatory;
278         $res->{$tag}->{$subfield}->{repeatable}       = $repeatable;
279         $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
280         $res->{$tag}->{$subfield}->{authtypecode}     = $authtypecode;
281         $res->{$tag}->{$subfield}->{value_builder}    = $value_builder;
282         $res->{$tag}->{$subfield}->{kohafield}        = $kohafield;
283         $res->{$tag}->{$subfield}->{seealso}          = $seealso;
284         $res->{$tag}->{$subfield}->{hidden}           = $hidden;
285         $res->{$tag}->{$subfield}->{isurl}            = $isurl;
286     }
287     return $res;
288 }
289
290 sub MARCfind_marc_from_kohafield {
291     my ( $dbh, $kohafield,$frameworkcode ) = @_;
292     return 0, 0 unless $kohafield;
293     my $sth =
294       $dbh->prepare(
295 "select tagfield,tagsubfield from marc_subfield_structure where frameworkcode=? and kohafield=?"
296     );
297     $sth->execute($frameworkcode,$kohafield);
298     my ( $tagfield, $tagsubfield ) = $sth->fetchrow;
299     return ( $tagfield, $tagsubfield );
300 }
301
302 sub MARCfind_oldbiblionumber_from_MARCbibid {
303     my ( $dbh, $MARCbibid ) = @_;
304     my $sth =
305       $dbh->prepare("select biblionumber from marc_biblio where bibid=?");
306     $sth->execute($MARCbibid);
307     my ($biblionumber) = $sth->fetchrow;
308     return $biblionumber;
309 }
310
311 sub MARCfind_MARCbibid_from_oldbiblionumber {
312     my ( $dbh, $oldbiblionumber ) = @_;
313     my $sth =
314       $dbh->prepare("select bibid from marc_biblio where biblionumber=?");
315     $sth->execute($oldbiblionumber);
316     my ($bibid) = $sth->fetchrow;
317     return $bibid;
318 }
319
320 sub MARCaddbiblio {
321
322 # pass the MARC::Record to this function, and it will create the records in the marc tables
323         my ($dbh,$record,$biblionumber,$frameworkcode,$bibid) = @_;
324         my @fields=$record->fields();
325 # my $bibid;
326 # adding main table, and retrieving bibid
327 # if bibid is sent, then it's not a true add, it's only a re-add, after a delete (ie, a mod)
328     # if bibid empty => true add, find a new bibid number
329     unless ($bibid) {
330         $dbh->do(
331 "lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ"
332         );
333         my $sth =
334           $dbh->prepare(
335 "insert into marc_biblio (datecreated,biblionumber,frameworkcode) values (now(),?,?)"
336         );
337         $sth->execute( $biblionumber, $frameworkcode );
338         $sth = $dbh->prepare("select max(bibid) from marc_biblio");
339         $sth->execute;
340         ($bibid) = $sth->fetchrow;
341         $sth->finish;
342     }
343     my $fieldcount = 0;
344
345     # now, add subfields...
346     foreach my $field (@fields) {
347         $fieldcount++;
348         if ( $field->tag() < 10 ) {
349             &MARCaddsubfield( $dbh, $bibid, $field->tag(), '', $fieldcount, '',
350                 1, $field->data() );
351         }
352         else {
353             my @subfields = $field->subfields();
354             foreach my $subfieldcount ( 0 .. $#subfields ) {
355                 &MARCaddsubfield(
356                     $dbh,
357                     $bibid,
358                     $field->tag(),
359                     $field->indicator(1) . $field->indicator(2),
360                     $fieldcount,
361                     $subfields[$subfieldcount][0],
362                     $subfieldcount + 1,
363                     $subfields[$subfieldcount][1]
364                 );
365             }
366         }
367     }
368     $dbh->do("unlock tables");
369     return $bibid;
370 }
371
372 sub MARCadditem {
373
374 # pass the MARC::Record to this function, and it will create the records in the marc tables
375     my ($dbh,$record,$biblionumber) = @_;
376 # search for MARC biblionumber
377     $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
378     my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
379     my @fields=$record->fields();
380     my $sth = $dbh->prepare("select max(tagorder) from marc_subfield_table where bibid=?");
381     $sth->execute($bibid);
382     my ($fieldcount) = $sth->fetchrow;
383
384     # now, add subfields...
385     foreach my $field (@fields) {
386         my @subfields = $field->subfields();
387         $fieldcount++;
388         foreach my $subfieldcount ( 0 .. $#subfields ) {
389             &MARCaddsubfield(
390                 $dbh,
391                 $bibid,
392                 $field->tag(),
393                 $field->indicator(1) . $field->indicator(2),
394                 $fieldcount,
395                 $subfields[$subfieldcount][0],
396                 $subfieldcount + 1,
397                 $subfields[$subfieldcount][1]
398             );
399         }
400     }
401     $dbh->do("unlock tables");
402     return $bibid;
403 }
404
405 sub MARCaddsubfield {
406
407     # Add a new subfield to a tag into the DB.
408     my (
409         $dbh,      $bibid,        $tagid,         $tag_indicator,
410         $tagorder, $subfieldcode, $subfieldorder, $subfieldvalues
411       )
412       = @_;
413
414     # if not value, end of job, we do nothing
415     if ( length($subfieldvalues) == 0 ) {
416         return;
417     }
418     if ( not($subfieldcode) ) {
419         $subfieldcode = ' ';
420     }
421     my @subfieldvalues = split /\|/, $subfieldvalues;
422     foreach my $subfieldvalue (@subfieldvalues) {
423         if ( length($subfieldvalue) > 255 ) {
424             $dbh->do(
425 "lock tables marc_blob_subfield WRITE, marc_subfield_table WRITE"
426             );
427             my $sth =
428               $dbh->prepare(
429                 "insert into marc_blob_subfield (subfieldvalue) values (?)");
430             $sth->execute($subfieldvalue);
431             $sth =
432               $dbh->prepare("select max(blobidlink)from marc_blob_subfield");
433             $sth->execute;
434             my ($res) = $sth->fetchrow;
435             $sth =
436               $dbh->prepare(
437 "insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,valuebloblink) values (?,?,?,?,?,?,?)"
438             );
439             $sth->execute( $bibid, ( sprintf "%03s", $tagid ), $tagorder,
440                 $tag_indicator, $subfieldcode, $subfieldorder, $res );
441
442             if ( $sth->errstr ) {
443                 warn
444 "ERROR ==> insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values ($bibid,$tagid,$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue)\n";
445             }
446             $dbh->do("unlock tables");
447         }
448         else {
449             my $sth =
450               $dbh->prepare(
451 "insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?,?)"
452             );
453             $sth->execute(
454                 $bibid,        ( sprintf "%03s", $tagid ),
455                 $tagorder,     $tag_indicator,
456                 $subfieldcode, $subfieldorder,
457                 $subfieldvalue
458             );
459             if ( $sth->errstr ) {
460                 warn
461 "ERROR ==> insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values ($bibid,$tagid,$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue)\n";
462             }
463         }
464         &MARCaddword(
465             $dbh,          $bibid,         $tagid,       $tagorder,
466             $subfieldcode, $subfieldorder, $subfieldvalue
467         );
468     }
469 }
470
471 sub MARCgetbiblio {
472
473     # Returns MARC::Record of the biblio passed in parameter.
474     my ( $dbh, $bibid ) = @_;
475     my $record = MARC::Record->new();
476
477     #---- TODO : the leader is missing
478     $record->leader('                        ');
479     my $sth =
480       $dbh->prepare(
481 "select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
482                                  from marc_subfield_table
483                                  where bibid=? order by tag,tagorder,subfieldcode
484                          "
485     );
486     my $sth2 =
487       $dbh->prepare(
488         "select subfieldvalue from marc_blob_subfield where blobidlink=?");
489     $sth->execute($bibid);
490     my $prevtagorder = 1;
491     my $prevtag      = 'XXX';
492     my $previndicator;
493     my $field;        # for >=10 tags
494     my $prevvalue;    # for <10 tags
495     while ( my $row = $sth->fetchrow_hashref ) {
496
497         if ( $row->{'valuebloblink'} ) {    #---- search blob if there is one
498             $sth2->execute( $row->{'valuebloblink'} );
499             my $row2 = $sth2->fetchrow_hashref;
500             $sth2->finish;
501             $row->{'subfieldvalue'} = $row2->{'subfieldvalue'};
502         }
503         if ( $row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag ) {
504             $previndicator .= "  ";
505             if ( $prevtag < 10 ) {
506                 $record->add_fields( ( sprintf "%03s", $prevtag ), $prevvalue )
507                   unless $prevtag eq "XXX";    # ignore the 1st loop
508             }
509             else {
510                 $record->add_fields($field) unless $prevtag eq "XXX";
511             }
512             undef $field;
513             $prevtagorder  = $row->{tagorder};
514             $prevtag       = $row->{tag};
515             $previndicator = $row->{tag_indicator};
516             if ( $row->{tag} < 10 ) {
517                 $prevvalue = $row->{subfieldvalue};
518             }
519             else {
520                 $field = MARC::Field->new(
521                     ( sprintf "%03s", $prevtag ),
522                     substr( $row->{tag_indicator} . '  ', 0, 1 ),
523                     substr( $row->{tag_indicator} . '  ', 1, 1 ),
524                     $row->{'subfieldcode'},
525                     $row->{'subfieldvalue'}
526                 );
527             }
528         }
529         else {
530             if ( $row->{tag} < 10 ) {
531                 $record->add_fields( ( sprintf "%03s", $row->{tag} ),
532                     $row->{'subfieldvalue'} );
533             }
534             else {
535                 $field->add_subfields( $row->{'subfieldcode'},
536                     $row->{'subfieldvalue'} );
537             }
538             $prevtag       = $row->{tag};
539             $previndicator = $row->{tag_indicator};
540         }
541     }
542
543     # the last has not been included inside the loop... do it now !
544     if ( $prevtag ne "XXX" )
545     { # check that we have found something. Otherwise, prevtag is still XXX and we
546          # must return an empty record, not make MARC::Record fail because we try to
547          # create a record with XXX as field :-(
548         if ( $prevtag < 10 ) {
549             $record->add_fields( $prevtag, $prevvalue );
550         }
551         else {
552
553             #           my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
554             $record->add_fields($field);
555         }
556     }
557     return $record;
558 }
559
560 sub MARCgetitem {
561
562     # Returns MARC::Record of the biblio passed in parameter.
563     my ( $dbh, $bibid, $itemnumber ) = @_;
564     my $record = MARC::Record->new();
565
566     # search MARC tagorder
567     my $sth2 =
568       $dbh->prepare(
569 "select tagorder from marc_subfield_table,marc_subfield_structure where marc_subfield_table.tag=marc_subfield_structure.tagfield and marc_subfield_table.subfieldcode=marc_subfield_structure.tagsubfield and bibid=? and kohafield='items.itemnumber' and subfieldvalue=?"
570     );
571     $sth2->execute( $bibid, $itemnumber );
572     my ($tagorder) = $sth2->fetchrow_array();
573
574     #---- TODO : the leader is missing
575     my $sth =
576       $dbh->prepare(
577 "select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
578                                  from marc_subfield_table
579                                  where bibid=? and tagorder=? order by subfieldcode,subfieldorder
580                          "
581     );
582     $sth2 =
583       $dbh->prepare(
584         "select subfieldvalue from marc_blob_subfield where blobidlink=?");
585     $sth->execute( $bibid, $tagorder );
586     while ( my $row = $sth->fetchrow_hashref ) {
587         if ( $row->{'valuebloblink'} ) {    #---- search blob if there is one
588             $sth2->execute( $row->{'valuebloblink'} );
589             my $row2 = $sth2->fetchrow_hashref;
590             $sth2->finish;
591             $row->{'subfieldvalue'} = $row2->{'subfieldvalue'};
592         }
593         if ( $record->field( $row->{'tag'} ) ) {
594             my $field;
595
596 #--- this test must stay as this, because of strange behaviour of mySQL/Perl DBI with char var containing a number...
597             #--- sometimes, eliminates 0 at beginning, sometimes no ;-\\\
598             if ( length( $row->{'tag'} ) < 3 ) {
599                 $row->{'tag'} = "0" . $row->{'tag'};
600             }
601             $field = $record->field( $row->{'tag'} );
602             if ($field) {
603                 my $x =
604                   $field->add_subfields( $row->{'subfieldcode'},
605                     $row->{'subfieldvalue'} );
606                 $record->delete_field($field);
607                 $record->add_fields($field);
608             }
609         }
610         else {
611             if ( length( $row->{'tag'} ) < 3 ) {
612                 $row->{'tag'} = "0" . $row->{'tag'};
613             }
614             my $temp =
615               MARC::Field->new( $row->{'tag'}, " ", " ",
616                 $row->{'subfieldcode'} => $row->{'subfieldvalue'} );
617             $record->add_fields($temp);
618         }
619
620     }
621     return $record;
622 }
623
624 sub MARCmodbiblio {
625         my ($dbh,$bibid,$record,$frameworkcode,$delete)=@_;
626         my $oldrecord=&MARCgetbiblio($dbh,$bibid);
627         if ($oldrecord eq $record) {
628                 return;
629         }
630 # 1st delete the biblio,
631 # 2nd recreate it
632         my $biblionumber = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
633         &MARCdelbiblio($dbh,$bibid,1);
634         &MARCaddbiblio($dbh,$record,$biblionumber,$frameworkcode,$bibid);
635 }
636
637 sub MARCdelbiblio {
638     my ( $dbh, $bibid, $keep_items ) = @_;
639
640     # if the keep_item is set to 1, then all items are preserved.
641     # This flag is set when the delbiblio is called by modbiblio
642     # due to a too complex structure of MARC (repeatable fields and subfields),
643     # the best solution for a modif is to delete / recreate the record.
644
645 # 1st of all, copy the MARC::Record to deletedbiblio table => if a true deletion, MARC data will be kept.
646 # if deletion called before MARCmodbiblio => won't do anything, as the oldbiblionumber doesn't
647     # exist in deletedbiblio table
648     my $record = MARCgetbiblio( $dbh, $bibid );
649     my $oldbiblionumber =
650       MARCfind_oldbiblionumber_from_MARCbibid( $dbh, $bibid );
651     my $copy2deleted =
652       $dbh->prepare("update deletedbiblio set marc=? where biblionumber=?");
653     $copy2deleted->execute( $record->as_usmarc(), $oldbiblionumber );
654
655     # now, delete in MARC tables.
656     if ( $keep_items eq 1 ) {
657
658         #search item field code
659         my $sth =
660           $dbh->prepare(
661 "select tagfield from marc_subfield_structure where kohafield like 'items.%'"
662         );
663         $sth->execute;
664         my $itemtag = $sth->fetchrow_hashref->{tagfield};
665         $dbh->do(
666 "delete from marc_subfield_table where bibid=$bibid and tag<>$itemtag"
667         );
668         $dbh->do(
669 "delete from marc_word where bibid=$bibid and not (tagsubfield like \"$itemtag%\")"
670         );
671     }
672     else {
673         $dbh->do("delete from marc_biblio where bibid=$bibid");
674         $dbh->do("delete from marc_subfield_table where bibid=$bibid");
675         $dbh->do("delete from marc_word where bibid=$bibid");
676     }
677 }
678
679 sub MARCdelitem {
680
681     # delete the item passed in parameter in MARC tables.
682     my ( $dbh, $bibid, $itemnumber ) = @_;
683
684     #    my $record = MARC::Record->new();
685     # search MARC tagorder
686     my $record = MARCgetitem( $dbh, $bibid, $itemnumber );
687     my $copy2deleted =
688       $dbh->prepare("update deleteditems set marc=? where itemnumber=?");
689     $copy2deleted->execute( $record->as_usmarc(), $itemnumber );
690
691     my $sth2 =
692       $dbh->prepare(
693 "select tagorder from marc_subfield_table,marc_subfield_structure where marc_subfield_table.tag=marc_subfield_structure.tagfield and marc_subfield_table.subfieldcode=marc_subfield_structure.tagsubfield and bibid=? and kohafield='items.itemnumber' and subfieldvalue=?"
694     );
695     $sth2->execute( $bibid, $itemnumber );
696     my ($tagorder) = $sth2->fetchrow_array();
697     my $sth =
698       $dbh->prepare(
699         "delete from marc_subfield_table where bibid=? and tagorder=?");
700     $sth->execute( $bibid, $tagorder );
701 }
702
703 sub MARCmoditem {
704         my ($dbh,$record,$bibid,$itemnumber,$delete)=@_;
705         my $oldrecord=&MARCgetitem($dbh,$bibid,$itemnumber);
706         # if nothing to change, don't waste time...
707         if ($oldrecord eq $record) {
708                 return;
709         }
710         # otherwise, skip through each subfield...
711         my @fields = $record->fields();
712         # search old MARC item
713         my $sth2 = $dbh->prepare("select tagorder from marc_subfield_table,marc_subfield_structure where marc_subfield_table.tag=marc_subfield_structure.tagfield and marc_subfield_table.subfieldcode=marc_subfield_structure.tagsubfield and bibid=? and kohafield='items.itemnumber' and subfieldvalue=?");
714         $sth2->execute($bibid,$itemnumber);
715         my ($tagorder) = $sth2->fetchrow_array();
716         foreach my $field (@fields) {
717                 my $oldfield = $oldrecord->field($field->tag());
718                 my @subfields=$field->subfields();
719                 my $subfieldorder=0;
720                 foreach my $subfield (@subfields) {
721                         $subfieldorder++;
722                         if ($oldfield eq 0 or (length($oldfield->subfield(@$subfield[0])) ==0) ) {
723                 # just adding datas...
724                                 &MARCaddsubfield($dbh,$bibid,$field->tag(),$field->indicator(1).$field->indicator(2),
725                                                 $tagorder,@$subfield[0],$subfieldorder,@$subfield[1]);
726                         } else {
727                 # modify he subfield if it's a different string
728                                 if ($oldfield->subfield(@$subfield[0]) ne @$subfield[1] ) {
729                                         my $subfieldid=&MARCfindsubfieldid($dbh,$bibid,$field->tag(),$tagorder,@$subfield[0],$subfieldorder);
730                                         &MARCmodsubfield($dbh,$subfieldid,@$subfield[1]);
731                                 }
732                         }
733                 }
734         }
735 }
736
737 sub MARCmodsubfield {
738
739     # Subroutine changes a subfield value given a subfieldid.
740     my ( $dbh, $subfieldid, $subfieldvalue ) = @_;
741     $dbh->do("lock tables marc_blob_subfield WRITE,marc_subfield_table WRITE");
742     my $sth1 =
743       $dbh->prepare(
744         "select valuebloblink from marc_subfield_table where subfieldid=?");
745     $sth1->execute($subfieldid);
746     my ($oldvaluebloblink) = $sth1->fetchrow;
747     $sth1->finish;
748     my $sth;
749
750     # if too long, use a bloblink
751     if ( length($subfieldvalue) > 255 ) {
752
753         # if already a bloblink, update it, otherwise, insert a new one.
754         if ($oldvaluebloblink) {
755             $sth =
756               $dbh->prepare(
757 "update marc_blob_subfield set subfieldvalue=? where blobidlink=?"
758             );
759             $sth->execute( $subfieldvalue, $oldvaluebloblink );
760         }
761         else {
762             $sth =
763               $dbh->prepare(
764                 "insert into marc_blob_subfield (subfieldvalue) values (?)");
765             $sth->execute($subfieldvalue);
766             $sth =
767               $dbh->prepare("select max(blobidlink) from marc_blob_subfield");
768             $sth->execute;
769             my ($res) = $sth->fetchrow;
770             $sth =
771               $dbh->prepare(
772 "update marc_subfield_table set subfieldvalue=null, valuebloblink=? where subfieldid=?"
773             );
774             $sth->execute( $res, $subfieldid );
775         }
776     }
777     else {
778
779 # note this can leave orphan bloblink. Not a big problem, but we should build somewhere a orphan deleting script...
780         $sth =
781           $dbh->prepare(
782 "update marc_subfield_table set subfieldvalue=?,valuebloblink=null where subfieldid=?"
783         );
784         $sth->execute( $subfieldvalue, $subfieldid );
785     }
786     $dbh->do("unlock tables");
787     $sth->finish;
788     $sth =
789       $dbh->prepare(
790 "select bibid,tag,tagorder,subfieldcode,subfieldid,subfieldorder from marc_subfield_table where subfieldid=?"
791     );
792     $sth->execute($subfieldid);
793     my ( $bibid, $tagid, $tagorder, $subfieldcode, $x, $subfieldorder ) =
794       $sth->fetchrow;
795     $subfieldid = $x;
796     &MARCdelword( $dbh, $bibid, $tagid, $tagorder, $subfieldcode,
797         $subfieldorder );
798     &MARCaddword(
799         $dbh,          $bibid,         $tagid,       $tagorder,
800         $subfieldcode, $subfieldorder, $subfieldvalue
801     );
802     return ( $subfieldid, $subfieldvalue );
803 }
804
805 sub MARCfindsubfield {
806     my ( $dbh, $bibid, $tag, $subfieldcode, $subfieldorder, $subfieldvalue ) =
807       @_;
808     my $resultcounter = 0;
809     my $subfieldid;
810     my $lastsubfieldid;
811     my $query =
812 "select subfieldid from marc_subfield_table where bibid=? and tag=? and subfieldcode=?";
813     my @bind_values = ( $bibid, $tag, $subfieldcode );
814     if ($subfieldvalue) {
815         $query .= " and subfieldvalue=?";
816         push ( @bind_values, $subfieldvalue );
817     }
818     else {
819         if ( $subfieldorder < 1 ) {
820             $subfieldorder = 1;
821         }
822         $query .= " and subfieldorder=?";
823         push ( @bind_values, $subfieldorder );
824     }
825     my $sti = $dbh->prepare($query);
826     $sti->execute(@bind_values);
827     while ( ($subfieldid) = $sti->fetchrow ) {
828         $resultcounter++;
829         $lastsubfieldid = $subfieldid;
830     }
831     if ( $resultcounter > 1 ) {
832
833 # Error condition.  Values given did not resolve into a unique record.  Don't know what to edit
834 # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
835         return -1;
836     }
837     else {
838         return $lastsubfieldid;
839     }
840 }
841
842 sub MARCfindsubfieldid {
843     my ( $dbh, $bibid, $tag, $tagorder, $subfield, $subfieldorder ) = @_;
844     my $sth = $dbh->prepare( "select subfieldid from marc_subfield_table
845                                 where bibid=? and tag=? and tagorder=?
846                                         and subfieldcode=? and subfieldorder=?"
847     );
848     $sth->execute( $bibid, $tag, $tagorder, $subfield, $subfieldorder );
849     my ($res) = $sth->fetchrow;
850     unless ($res) {
851         $sth = $dbh->prepare( "select subfieldid from marc_subfield_table
852                                 where bibid=? and tag=? and tagorder=?
853                                         and subfieldcode=?"
854         );
855         $sth->execute( $bibid, $tag, $tagorder, $subfield );
856         ($res) = $sth->fetchrow;
857     }
858     return $res;
859 }
860
861 sub MARCfind_frameworkcode {
862     my ( $dbh, $bibid ) = @_;
863     my $sth =
864       $dbh->prepare("select frameworkcode from marc_biblio where bibid=?");
865     $sth->execute($bibid);
866     my ($frameworkcode) = $sth->fetchrow;
867     return $frameworkcode;
868 }
869
870 sub MARCdelsubfield {
871
872     # delete a subfield for $bibid / tag / tagorder / subfield / subfieldorder
873     my ( $dbh, $bibid, $tag, $tagorder, $subfield, $subfieldorder ) = @_;
874     $dbh->do( "delete from marc_subfield_table where bibid='$bibid' and
875                         tag='$tag' and tagorder='$tagorder'
876                         and subfieldcode='$subfield' and subfieldorder='$subfieldorder'
877                         "
878     );
879 }
880
881 sub MARCkoha2marcBiblio {
882
883     # this function builds partial MARC::Record from the old koha-DB fields
884     my ( $dbh, $biblionumber, $biblioitemnumber ) = @_;
885     my $sth =
886       $dbh->prepare(
887 "select tagfield,tagsubfield from marc_subfield_structure where kohafield=?"
888     );
889     my $record = MARC::Record->new();
890
891     #--- if bibid, then retrieve old-style koha data
892     if ( $biblionumber > 0 ) {
893         my $sth2 =
894           $dbh->prepare(
895 "select biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp
896                 from biblio where biblionumber=?"
897         );
898         $sth2->execute($biblionumber);
899         my $row = $sth2->fetchrow_hashref;
900         my $code;
901         foreach $code ( keys %$row ) {
902             if ( $row->{$code} ) {
903                 &MARCkoha2marcOnefield( $sth, $record, "biblio." . $code,
904                     $row->{$code} );
905             }
906         }
907     }
908
909     #--- if biblioitem, then retrieve old-style koha data
910     if ( $biblioitemnumber > 0 ) {
911         my $sth2 =
912           $dbh->prepare(
913             " SELECT biblioitemnumber,biblionumber,volume,number,classification,
914                                                 itemtype,url,isbn,issn,dewey,subclass,publicationyear,publishercode,
915                                                 volumedate,volumeddesc,timestamp,illus,pages,notes AS bnotes,size,place
916                                         FROM biblioitems
917                                         WHERE biblioitemnumber=?
918                                         "
919         );
920         $sth2->execute($biblioitemnumber);
921         my $row = $sth2->fetchrow_hashref;
922         my $code;
923         foreach $code ( keys %$row ) {
924             if ( $row->{$code} ) {
925                 &MARCkoha2marcOnefield( $sth, $record, "biblioitems." . $code,
926                     $row->{$code} );
927             }
928         }
929     }
930
931     # other fields => additional authors, subjects, subtitles
932     my $sth2 =
933       $dbh->prepare(
934         " SELECT author FROM additionalauthors WHERE biblionumber=?");
935     $sth2->execute($biblionumber);
936     while ( my $row = $sth2->fetchrow_hashref ) {
937         &MARCkoha2marcOnefield( $sth, $record, "additionalauthors.author",
938             $row->{'author'} );
939     }
940     my $sth2 =
941       $dbh->prepare(" SELECT subject FROM bibliosubject WHERE biblionumber=?");
942     $sth2->execute($biblionumber);
943     while ( my $row = $sth2->fetchrow_hashref ) {
944         &MARCkoha2marcOnefield( $sth, $record, "bibliosubject.subject",
945             $row->{'subject'} );
946     }
947     my $sth2 =
948       $dbh->prepare(
949         " SELECT subtitle FROM bibliosubtitle WHERE biblionumber=?");
950     $sth2->execute($biblionumber);
951     while ( my $row = $sth2->fetchrow_hashref ) {
952         &MARCkoha2marcOnefield( $sth, $record, "bibliosubtitle.title",
953             $row->{'subtitle'} );
954     }
955     return $record;
956 }
957
958 sub MARCkoha2marcItem {
959
960     # this function builds partial MARC::Record from the old koha-DB fields
961     my ( $dbh, $biblionumber, $itemnumber ) = @_;
962
963     #    my $dbh=&C4Connect;
964     my $sth =
965       $dbh->prepare(
966 "select tagfield,tagsubfield from marc_subfield_structure where kohafield=?"
967     );
968     my $record = MARC::Record->new();
969
970     #--- if item, then retrieve old-style koha data
971     if ( $itemnumber > 0 ) {
972
973         #       print STDERR "prepare $biblionumber,$itemnumber\n";
974         my $sth2 =
975           $dbh->prepare(
976 "SELECT itemnumber,biblionumber,multivolumepart,biblioitemnumber,barcode,dateaccessioned,
977                                                 booksellerid,homebranch,price,replacementprice,replacementpricedate,datelastborrowed,
978                                                 datelastseen,multivolume,stack,notforloan,itemlost,wthdrawn,itemcallnumber,issues,renewals,
979                                         reserves,restricted,binding,itemnotes,holdingbranch,timestamp
980                                         FROM items
981                                         WHERE itemnumber=?"
982         );
983         $sth2->execute($itemnumber);
984         my $row = $sth2->fetchrow_hashref;
985         my $code;
986         foreach $code ( keys %$row ) {
987             if ( $row->{$code} ) {
988                 &MARCkoha2marcOnefield( $sth, $record, "items." . $code,
989                     $row->{$code} );
990             }
991         }
992     }
993     return $record;
994 }
995
996 sub MARCkoha2marcSubtitle {
997
998     # this function builds partial MARC::Record from the old koha-DB fields
999     my ( $dbh, $bibnum, $subtitle ) = @_;
1000     my $sth =
1001       $dbh->prepare(
1002 "select tagfield,tagsubfield from marc_subfield_structure where kohafield=?"
1003     );
1004     my $record = MARC::Record->new();
1005     &MARCkoha2marcOnefield( $sth, $record, "bibliosubtitle.subtitle",
1006         $subtitle );
1007     return $record;
1008 }
1009
1010 sub MARCkoha2marcOnefield {
1011     my ( $sth, $record, $kohafieldname, $value ) = @_;
1012     my $tagfield;
1013     my $tagsubfield;
1014     $sth->execute($kohafieldname);
1015     if ( ( $tagfield, $tagsubfield ) = $sth->fetchrow ) {
1016         if ( $record->field($tagfield) ) {
1017             my $tag = $record->field($tagfield);
1018             if ($tag) {
1019                 $tag->add_subfields( $tagsubfield, $value );
1020                 $record->delete_field($tag);
1021                 $record->add_fields($tag);
1022             }
1023         }
1024         else {
1025             $record->add_fields( $tagfield, " ", " ", $tagsubfield => $value );
1026         }
1027     }
1028     return $record;
1029 }
1030
1031 sub MARChtml2marc {
1032         my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
1033         my $prevtag = -1;
1034         my $record = MARC::Record->new();
1035 #       my %subfieldlist=();
1036         my $prevvalue; # if tag <10
1037         my $field; # if tag >=10
1038         for (my $i=0; $i< @$rtags; $i++) {
1039                 # rebuild MARC::Record
1040                 if (@$rtags[$i] ne $prevtag) {
1041                         if ($prevtag < 10) {
1042                                 if ($prevvalue) {
1043                                         $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
1044                                 }
1045                         } else {
1046                                 if ($field) {
1047                                         $record->add_fields($field);
1048                                 }
1049                         }
1050                         $indicators{@$rtags[$i]}.='  ';
1051                         if (@$rtags[$i] <10) {
1052                                 $prevvalue= @$rvalues[$i];
1053                         } else {
1054                                 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
1055                         }
1056                         $prevtag = @$rtags[$i];
1057                 } else {
1058                         if (@$rtags[$i] <10) {
1059                                 $prevvalue=@$rvalues[$i];
1060                         } else {
1061                                 if (@$rvalues[$i]) {
1062                                         $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
1063                                 }
1064                         }
1065                         $prevtag= @$rtags[$i];
1066                 }
1067         }
1068         # the last has not been included inside the loop... do it now !
1069         $record->add_fields($field);
1070         return $record;
1071 }
1072
1073 sub MARCmarc2koha {
1074         my ($dbh,$record,$frameworkcode) = @_;
1075         my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where frameworkcode=? and kohafield=?");
1076         my $result;
1077         my $sth2=$dbh->prepare("SHOW COLUMNS from biblio");
1078         $sth2->execute;
1079         my $field;
1080         while (($field)=$sth2->fetchrow) {
1081                 $result=&MARCmarc2kohaOneField($sth,"biblio",$field,$record,$result,$frameworkcode);
1082         }
1083         $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
1084         $sth2->execute;
1085         while (($field)=$sth2->fetchrow) {
1086                 if ($field eq 'notes') { $field = 'bnotes'; }
1087                 $result=&MARCmarc2kohaOneField($sth,"biblioitems",$field,$record,$result,$frameworkcode);
1088         }
1089         $sth2=$dbh->prepare("SHOW COLUMNS from items");
1090         $sth2->execute;
1091         while (($field)=$sth2->fetchrow) {
1092                 $result = &MARCmarc2kohaOneField($sth,"items",$field,$record,$result,$frameworkcode);
1093         }
1094         # additional authors : specific
1095         $result = &MARCmarc2kohaOneField($sth,"bibliosubtitle","subtitle",$record,$result,$frameworkcode);
1096         $result = &MARCmarc2kohaOneField($sth,"additionalauthors","additionalauthors",$record,$result,$frameworkcode);
1097 # modify copyrightdate to keep only the 1st year found
1098         my $temp = $result->{'copyrightdate'};
1099         $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
1100         if ($1>0) {
1101                 $result->{'copyrightdate'} = $1;
1102         } else { # if no cYYYY, get the 1st date.
1103                 $temp =~ m/(\d\d\d\d)/;
1104                 $result->{'copyrightdate'} = $1;
1105         }
1106 # modify publicationyear to keep only the 1st year found
1107         my $temp = $result->{'publicationyear'};
1108         $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
1109         if ($1>0) {
1110                 $result->{'publicationyear'} = $1;
1111         } else { # if no cYYYY, get the 1st date.
1112                 $temp =~ m/(\d\d\d\d)/;
1113                 $result->{'publicationyear'} = $1;
1114         }
1115         return $result;
1116 }
1117
1118 sub MARCmarc2kohaOneField {
1119
1120 # FIXME ? if a field has a repeatable subfield that is used in old-db, only the 1st will be retrieved...
1121     my ( $sth, $kohatable, $kohafield, $record, $result,$frameworkcode ) = @_;
1122
1123     #    warn "kohatable / $kohafield / $result / ";
1124     my $res = "";
1125     my $tagfield;
1126     my $subfield;
1127     $sth->execute( $frameworkcode,$kohatable . "." . $kohafield );
1128     ( $tagfield, $subfield ) = $sth->fetchrow;
1129     foreach my $field ( $record->field($tagfield) ) {
1130         if ( $field->subfield($subfield) ) {
1131             if ( $result->{$kohafield} ) {
1132                 $result->{$kohafield} .= " | " . $field->subfield($subfield);
1133             }
1134             else {
1135                 $result->{$kohafield} = $field->subfield($subfield);
1136             }
1137         }
1138     }
1139     return $result;
1140 }
1141
1142 sub MARCaddword {
1143
1144     # split a subfield string and adds it into the word table.
1145     # removes stopwords
1146     my (
1147         $dbh,        $bibid,         $tag,    $tagorder,
1148         $subfieldid, $subfieldorder, $sentence
1149       )
1150       = @_;
1151     $sentence =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\})/ /g;
1152     my @words = split / /, $sentence;
1153     my $stopwords = C4::Context->stopwords;
1154     my $sth       =
1155       $dbh->prepare(
1156 "insert into marc_word (bibid, tagsubfield, tagorder, subfieldorder, word, sndx_word)
1157                         values (?,concat(?,?),?,?,?,soundex(?))"
1158     );
1159     foreach my $word (@words) {
1160 # we record only words one char long and not in stopwords hash
1161         if (length($word)>=1 and !($stopwords->{uc($word)})) {
1162             $sth->execute($bibid,$tag,$subfieldid,$tagorder,$subfieldorder,$word,$word);
1163             if ($sth->err()) {
1164                 warn "ERROR ==> insert into marc_word (bibid, tagsubfield, tagorder, subfieldorder, word, sndx_word) values ($bibid,concat($tag,$subfieldid),$tagorder,$subfieldorder,$word,soundex($word))\n";
1165             }
1166         }
1167     }
1168 }
1169
1170 sub MARCdelword {
1171
1172 # delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
1173     my ( $dbh, $bibid, $tag, $tagorder, $subfield, $subfieldorder ) = @_;
1174     my $sth =
1175       $dbh->prepare(
1176 "delete from marc_word where bibid=? and tagsubfield=concat(?,?) and tagorder=? and subfieldorder=?"
1177     );
1178     $sth->execute( $bibid, $tag, $subfield, $tagorder, $subfieldorder );
1179 }
1180
1181 #
1182 #
1183 # NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
1184 #
1185 #
1186 # all the following subs are useful to manage MARC-DB with complete MARC records.
1187 # it's used with marcimport, and marc management tools
1188 #
1189
1190 =item ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbibilio($dbh,$MARCRecord,$oldbiblio,$oldbiblioitem);
1191
1192 creates a new biblio from a MARC::Record. The 3rd and 4th parameter are hashes and may be ignored. If only 2 params are passed to the sub, the old-db hashes
1193 are builded from the MARC::Record. If they are passed, they are used.
1194
1195 =item NEWnewitem($dbh, $record,$bibid);
1196
1197 adds an item in the db.
1198
1199 =cut
1200
1201 sub NEWnewbiblio {
1202     my ( $dbh, $record, $frameworkcode ) = @_;
1203     my $oldbibnum;
1204     my $oldbibitemnum;
1205     my $olddata = MARCmarc2koha( $dbh, $record,$frameworkcode );
1206     $oldbibnum = OLDnewbiblio( $dbh, $olddata );
1207     $olddata->{'biblionumber'} = $oldbibnum;
1208     $oldbibitemnum = OLDnewbiblioitem( $dbh, $olddata );
1209
1210     # search subtiles, addiauthors and subjects
1211     my ( $tagfield, $tagsubfield ) =
1212       MARCfind_marc_from_kohafield( $dbh, "additionalauthors.author",$frameworkcode );
1213     my @addiauthfields = $record->field($tagfield);
1214     foreach my $addiauthfield (@addiauthfields) {
1215         my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1216         foreach my $subfieldcount ( 0 .. $#addiauthsubfields ) {
1217             OLDmodaddauthor( $dbh, $oldbibnum,
1218                 $addiauthsubfields[$subfieldcount] );
1219         }
1220     }
1221     ( $tagfield, $tagsubfield ) =
1222       MARCfind_marc_from_kohafield( $dbh, "bibliosubtitle.title",$frameworkcode );
1223     my @subtitlefields = $record->field($tagfield);
1224     foreach my $subtitlefield (@subtitlefields) {
1225         my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1226         foreach my $subfieldcount ( 0 .. $#subtitlesubfields ) {
1227             OLDnewsubtitle( $dbh, $oldbibnum,
1228                 $subtitlesubfields[$subfieldcount] );
1229         }
1230     }
1231     ( $tagfield, $tagsubfield ) =
1232       MARCfind_marc_from_kohafield( $dbh, "bibliosubject.subject",$frameworkcode );
1233     my @subj = $record->field($tagfield);
1234     my @subjects;
1235     foreach my $subject (@subj) {
1236         my @subjsubfield = $subject->subfield($tagsubfield);
1237         foreach my $subfieldcount ( 0 .. $#subjsubfield ) {
1238             push @subjects, $subjsubfield[$subfieldcount];
1239         }
1240     }
1241     OLDmodsubject( $dbh, $oldbibnum, 1, @subjects );
1242
1243     # we must add bibnum and bibitemnum in MARC::Record...
1244     # we build the new field with biblionumber and biblioitemnumber
1245     # we drop the original field
1246     # we add the new builded field.
1247 # NOTE : Works only if the field is ONLY for biblionumber and biblioitemnumber
1248     # (steve and paul : thinks 090 is a good choice)
1249     my $sth =
1250       $dbh->prepare(
1251 "select tagfield,tagsubfield from marc_subfield_structure where kohafield=?"
1252     );
1253     $sth->execute("biblio.biblionumber");
1254     ( my $tagfield1, my $tagsubfield1 ) = $sth->fetchrow;
1255     $sth->execute("biblioitems.biblioitemnumber");
1256     ( my $tagfield2, my $tagsubfield2 ) = $sth->fetchrow;
1257     if ( $tagfield1 != $tagfield2 ) {
1258         warn
1259 "Error in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1260         print
1261 "Content-Type: text/html\n\nError in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1262         die;
1263     }
1264     my $newfield = MARC::Field->new(
1265         $tagfield1, '', '', "$tagsubfield1" => $oldbibnum,
1266         "$tagsubfield2" => $oldbibitemnum
1267     );
1268
1269     # drop old field and create new one...
1270     my $old_field = $record->field($tagfield1);
1271     $record->delete_field($old_field);
1272     $record->add_fields($newfield);
1273     my $bibid = MARCaddbiblio( $dbh, $record, $oldbibnum, $frameworkcode );
1274     return ( $bibid, $oldbibnum, $oldbibitemnum );
1275 }
1276
1277 sub NEWmodbiblio {
1278         my ($dbh,$record,$bibid,$frameworkcode) =@_;
1279         $frameworkcode="" unless $frameworkcode;
1280         &MARCmodbiblio($dbh,$bibid,$record,$frameworkcode,0);
1281         my $oldbiblio = MARCmarc2koha($dbh,$record,$frameworkcode);
1282         my $oldbiblionumber = OLDmodbiblio($dbh,$oldbiblio);
1283         OLDmodbibitem($dbh,$oldbiblio);
1284         # now, modify addi authors, subject, addititles.
1285         my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author",$frameworkcode);
1286         my @addiauthfields = $record->field($tagfield);
1287         foreach my $addiauthfield (@addiauthfields) {
1288                 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1289                 foreach my $subfieldcount (0..$#addiauthsubfields) {
1290                         OLDmodaddauthor($dbh,$oldbiblionumber,$addiauthsubfields[$subfieldcount]);
1291                 }
1292         }
1293         ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle",$frameworkcode);
1294         my @subtitlefields = $record->field($tagfield);
1295         foreach my $subtitlefield (@subtitlefields) {
1296                 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1297                 foreach my $subfieldcount (0..$#subtitlesubfields) {
1298                         OLDmodsubtitle($dbh,$oldbiblionumber,$subtitlesubfields[$subfieldcount]);
1299                 }
1300         }
1301         ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject",$frameworkcode);
1302         my @subj = $record->field($tagfield);
1303         my @subjects;
1304         foreach my $subject (@subj) {
1305                 my @subjsubfield = $subject->subfield($tagsubfield);
1306                 foreach my $subfieldcount (0..$#subjsubfield) {
1307                         push @subjects,$subjsubfield[$subfieldcount];
1308                 }
1309         }
1310         OLDmodsubject($dbh,$oldbiblionumber,1,@subjects);
1311         return 1;
1312 }
1313
1314 sub NEWdelbiblio {
1315     my ( $dbh, $bibid ) = @_;
1316     my $biblio = &MARCfind_oldbiblionumber_from_MARCbibid( $dbh, $bibid );
1317     &OLDdelbiblio( $dbh, $biblio );
1318     my $sth =
1319       $dbh->prepare(
1320         "select biblioitemnumber from biblioitems where biblionumber=?");
1321     $sth->execute($biblio);
1322     while ( my ($biblioitemnumber) = $sth->fetchrow ) {
1323         OLDdeletebiblioitem( $dbh, $biblioitemnumber );
1324     }
1325     &MARCdelbiblio( $dbh, $bibid, 0 );
1326 }
1327
1328 sub NEWnewitem {
1329     my ( $dbh, $record, $bibid ) = @_;
1330
1331     # add item in old-DB
1332         my $frameworkcode=MARCfind_frameworkcode($dbh,$bibid);
1333         
1334     my $item = &MARCmarc2koha( $dbh, $record,$frameworkcode );
1335
1336     # needs old biblionumber and biblioitemnumber
1337     $item->{'biblionumber'} =
1338       MARCfind_oldbiblionumber_from_MARCbibid( $dbh, $bibid );
1339     my $sth =
1340       $dbh->prepare(
1341         "select biblioitemnumber from biblioitems where biblionumber=?");
1342     $sth->execute( $item->{'biblionumber'} );
1343     ( $item->{'biblioitemnumber'} ) = $sth->fetchrow;
1344     my ( $itemnumber, $error ) = &OLDnewitems( $dbh, $item, $item->{barcode} );
1345
1346     # add itemnumber to MARC::Record before adding the item.
1347     my $sth =
1348       $dbh->prepare(
1349 "select tagfield,tagsubfield from marc_subfield_structure where kohafield=?"
1350     );
1351     &MARCkoha2marcOnefield( $sth, $record, "items.itemnumber", $itemnumber );
1352
1353     # add the item
1354     my $bib = &MARCadditem( $dbh, $record, $item->{'biblionumber'} );
1355 }
1356
1357 sub NEWmoditem {
1358     my ( $dbh, $record, $bibid, $itemnumber, $delete ) = @_;
1359     &MARCmoditem( $dbh, $record, $bibid, $itemnumber, $delete );
1360         my $frameworkcode=MARCfind_frameworkcode($dbh,$bibid);
1361     my $olditem = MARCmarc2koha( $dbh, $record,$frameworkcode );
1362     OLDmoditem( $dbh, $olditem );
1363 }
1364
1365 sub NEWdelitem {
1366     my ( $dbh, $bibid, $itemnumber ) = @_;
1367     my $biblio = &MARCfind_oldbiblionumber_from_MARCbibid( $dbh, $bibid );
1368     &OLDdelitem( $dbh, $itemnumber );
1369     &MARCdelitem( $dbh, $bibid, $itemnumber );
1370 }
1371
1372 #
1373 #
1374 # OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
1375 #
1376 #
1377
1378 =item $biblionumber = OLDnewbiblio($dbh,$biblio);
1379
1380 adds a record in biblio table. Datas are in the hash $biblio.
1381
1382 =item $biblionumber = OLDmodbiblio($dbh,$biblio);
1383
1384 modify a record in biblio table. Datas are in the hash $biblio.
1385
1386 =item OLDmodsubtitle($dbh,$bibnum,$subtitle);
1387
1388 modify subtitles in bibliosubtitle table.
1389
1390 =item OLDmodaddauthor($dbh,$bibnum,$author);
1391
1392 adds or modify additional authors
1393 NOTE :  Strange sub : seems to delete MANY and add only ONE author... maybe buggy ?
1394
1395 =item $errors = OLDmodsubject($dbh,$bibnum, $force, @subject);
1396
1397 modify/adds subjects
1398
1399 =item OLDmodbibitem($dbh, $biblioitem);
1400
1401 modify a biblioitem
1402
1403 =item OLDmodnote($dbh,$bibitemnum,$note
1404
1405 modify a note for a biblioitem
1406
1407 =item OLDnewbiblioitem($dbh,$biblioitem);
1408
1409 adds a biblioitem ($biblioitem is a hash with the values)
1410
1411 =item OLDnewsubject($dbh,$bibnum);
1412
1413 adds a subject
1414
1415 =item OLDnewsubtitle($dbh,$bibnum,$subtitle);
1416
1417 create a new subtitle
1418
1419 =item ($itemnumber,$errors)= OLDnewitems($dbh,$item,$barcode);
1420
1421 create a item. $item is a hash and $barcode the barcode.
1422
1423 =item OLDmoditem($dbh,$item);
1424
1425 modify item
1426
1427 =item OLDdelitem($dbh,$itemnum);
1428
1429 delete item
1430
1431 =item OLDdeletebiblioitem($dbh,$biblioitemnumber);
1432
1433 deletes a biblioitem
1434 NOTE : not standard sub name. Should be OLDdelbiblioitem()
1435
1436 =item OLDdelbiblio($dbh,$biblio);
1437
1438 delete a biblio
1439
1440 =cut
1441
1442 sub OLDnewbiblio {
1443     my ( $dbh, $biblio ) = @_;
1444
1445     #  my $dbh    = &C4Connect;
1446     my $sth = $dbh->prepare("Select max(biblionumber) from biblio");
1447     $sth->execute;
1448     my $data   = $sth->fetchrow_arrayref;
1449     my $bibnum = $$data[0] + 1;
1450     my $series = 0;
1451
1452     if ( $biblio->{'seriestitle'} ) { $series = 1 }
1453     $sth->finish;
1454     $sth =
1455       $dbh->prepare(
1456 "insert into biblio set biblionumber  = ?, title = ?, author = ?, copyrightdate = ?, serial = ?, seriestitle = ?, notes = ?, abstract = ?"
1457     );
1458     $sth->execute(
1459         $bibnum,             $biblio->{'title'},
1460         $biblio->{'author'}, $biblio->{'copyrightdate'},
1461         $biblio->{'serial'},             $biblio->{'seriestitle'},
1462         $biblio->{'notes'},  $biblio->{'abstract'}
1463     );
1464
1465     $sth->finish;
1466
1467     #  $dbh->disconnect;
1468     return ($bibnum);
1469 }
1470
1471 sub OLDmodbiblio {
1472     my ( $dbh, $biblio ) = @_;
1473
1474     #  my $dbh   = C4Connect;
1475     my $query;
1476     my $sth;
1477
1478     $query = "";
1479     $sth   =
1480       $dbh->prepare(
1481 "Update biblio set title = ?, author = ?, abstract = ?, copyrightdate = ?, seriestitle = ?, serial = ?, unititle = ?, notes = ? where biblionumber = ?"
1482     );
1483     $sth->execute(
1484         $biblio->{'title'},       $biblio->{'author'},
1485         $biblio->{'abstract'},    $biblio->{'copyrightdate'},
1486         $biblio->{'seriestitle'}, $biblio->{'serial'},
1487         $biblio->{'unititle'},    $biblio->{'notes'},
1488         $biblio->{'biblionumber'}
1489     );
1490
1491     $sth->finish;
1492     return ( $biblio->{'biblionumber'} );
1493 }    # sub modbiblio
1494
1495 sub OLDmodsubtitle {
1496     my ( $dbh, $bibnum, $subtitle ) = @_;
1497     my $sth =
1498       $dbh->prepare(
1499         "update bibliosubtitle set subtitle = ? where biblionumber = ?");
1500     $sth->execute( $subtitle, $bibnum );
1501     $sth->finish;
1502 }    # sub modsubtitle
1503
1504 sub OLDmodaddauthor {
1505     my ( $dbh, $bibnum, @authors ) = @_;
1506
1507     #    my $dbh   = C4Connect;
1508     my $sth =
1509       $dbh->prepare("Delete from additionalauthors where biblionumber = ?");
1510
1511     $sth->execute($bibnum);
1512     $sth->finish;
1513     foreach my $author (@authors) {
1514         if ( $author ne '' ) {
1515             $sth =
1516               $dbh->prepare(
1517                 "Insert into additionalauthors set author = ?, biblionumber = ?"
1518             );
1519
1520             $sth->execute( $author, $bibnum );
1521
1522             $sth->finish;
1523         }    # if
1524     }
1525 }    # sub modaddauthor
1526
1527 sub OLDmodsubject {
1528     my ( $dbh, $bibnum, $force, @subject ) = @_;
1529
1530     #  my $dbh   = C4Connect;
1531     my $count = @subject;
1532     my $error;
1533     for ( my $i = 0 ; $i < $count ; $i++ ) {
1534         $subject[$i] =~ s/^ //g;
1535         $subject[$i] =~ s/ $//g;
1536         my $sth =
1537           $dbh->prepare(
1538 "select * from catalogueentry where entrytype = 's' and catalogueentry = ?"
1539         );
1540         $sth->execute( $subject[$i] );
1541
1542         if ( my $data = $sth->fetchrow_hashref ) {
1543         }
1544         else {
1545             if ( $force eq $subject[$i] || $force == 1 ) {
1546
1547                 # subject not in aut, chosen to force anway
1548                 # so insert into cataloguentry so its in auth file
1549                 my $sth2 =
1550                   $dbh->prepare(
1551 "Insert into catalogueentry (entrytype,catalogueentry) values ('s',?)"
1552                 );
1553
1554                 $sth2->execute( $subject[$i] ) if ( $subject[$i] );
1555                 $sth2->finish;
1556             }
1557             else {
1558                 $error =
1559                   "$subject[$i]\n does not exist in the subject authority file";
1560                 my $sth2 =
1561                   $dbh->prepare(
1562 "Select * from catalogueentry where entrytype = 's' and (catalogueentry like ? or catalogueentry like ? or catalogueentry like ?)"
1563                 );
1564                 $sth2->execute( "$subject[$i] %", "% $subject[$i] %",
1565                     "% $subject[$i]" );
1566                 while ( my $data = $sth2->fetchrow_hashref ) {
1567                     $error .= "<br>$data->{'catalogueentry'}";
1568                 }    # while
1569                 $sth2->finish;
1570             }    # else
1571         }    # else
1572         $sth->finish;
1573     }    # else
1574     if ( $error eq '' ) {
1575         my $sth =
1576           $dbh->prepare("Delete from bibliosubject where biblionumber = ?");
1577         $sth->execute($bibnum);
1578         $sth->finish;
1579         $sth =
1580           $dbh->prepare(
1581             "Insert into bibliosubject (subject,biblionumber) values (?,?)");
1582         my $query;
1583         foreach $query (@subject) {
1584             $sth->execute( $query, $bibnum ) if ( $query && $bibnum );
1585         }    # foreach
1586         $sth->finish;
1587     }    # if
1588
1589     #  $dbh->disconnect;
1590     return ($error);
1591 }    # sub modsubject
1592
1593 sub OLDmodbibitem {
1594     my ( $dbh, $biblioitem ) = @_;
1595
1596     #    my $dbh   = C4Connect;
1597     my $query;
1598
1599     $biblioitem->{'itemtype'}      = $dbh->quote( $biblioitem->{'itemtype'} );
1600     $biblioitem->{'url'}           = $dbh->quote( $biblioitem->{'url'} );
1601     $biblioitem->{'isbn'}          = $dbh->quote( $biblioitem->{'isbn'} );
1602     $biblioitem->{'publishercode'} =
1603       $dbh->quote( $biblioitem->{'publishercode'} );
1604     $biblioitem->{'publicationyear'} =
1605       $dbh->quote( $biblioitem->{'publicationyear'} );
1606     $biblioitem->{'classification'} =
1607       $dbh->quote( $biblioitem->{'classification'} );
1608     $biblioitem->{'dewey'}       = $dbh->quote( $biblioitem->{'dewey'} );
1609     $biblioitem->{'subclass'}    = $dbh->quote( $biblioitem->{'subclass'} );
1610     $biblioitem->{'illus'}       = $dbh->quote( $biblioitem->{'illus'} );
1611     $biblioitem->{'pages'}       = $dbh->quote( $biblioitem->{'pages'} );
1612     $biblioitem->{'volumeddesc'} = $dbh->quote( $biblioitem->{'volumeddesc'} );
1613     $biblioitem->{'bnotes'}      = $dbh->quote( $biblioitem->{'bnotes'} );
1614     $biblioitem->{'size'}        = $dbh->quote( $biblioitem->{'size'} );
1615     $biblioitem->{'place'}       = $dbh->quote( $biblioitem->{'place'} );
1616
1617     $query = "Update biblioitems set
1618 itemtype        = $biblioitem->{'itemtype'},
1619 url             = $biblioitem->{'url'},
1620 isbn            = $biblioitem->{'isbn'},
1621 publishercode   = $biblioitem->{'publishercode'},
1622 publicationyear = $biblioitem->{'publicationyear'},
1623 classification  = $biblioitem->{'classification'},
1624 dewey           = $biblioitem->{'dewey'},
1625 subclass        = $biblioitem->{'subclass'},
1626 illus           = $biblioitem->{'illus'},
1627 pages           = $biblioitem->{'pages'},
1628 volumeddesc     = $biblioitem->{'volumeddesc'},
1629 notes           = $biblioitem->{'bnotes'},
1630 size            = $biblioitem->{'size'},
1631 place           = $biblioitem->{'place'}
1632 where biblioitemnumber = $biblioitem->{'biblioitemnumber'}";
1633
1634     $dbh->do($query);
1635     if ( $dbh->errstr ) {
1636         warn "$query";
1637     }
1638
1639     #    $dbh->disconnect;
1640 }    # sub modbibitem
1641
1642 sub OLDmodnote {
1643     my ( $dbh, $bibitemnum, $note ) = @_;
1644
1645     #  my $dbh=C4Connect;
1646     my $query = "update biblioitems set notes='$note' where
1647   biblioitemnumber='$bibitemnum'";
1648     my $sth = $dbh->prepare($query);
1649     $sth->execute;
1650     $sth->finish;
1651
1652     #  $dbh->disconnect;
1653 }
1654
1655 sub OLDnewbiblioitem {
1656     my ( $dbh, $biblioitem ) = @_;
1657
1658     #  my $dbh   = C4Connect;
1659     my $sth = $dbh->prepare("Select max(biblioitemnumber) from biblioitems");
1660     my $data;
1661     my $bibitemnum;
1662
1663     $sth->execute;
1664     $data       = $sth->fetchrow_arrayref;
1665     $bibitemnum = $$data[0] + 1;
1666
1667     $sth->finish;
1668
1669     $sth = $dbh->prepare( "insert into biblioitems set
1670                                                                         biblioitemnumber = ?,           biblionumber     = ?,
1671                                                                         volume           = ?,                   number           = ?,
1672                                                                         classification  = ?,                    itemtype         = ?,
1673                                                                         url              = ?,                           isbn             = ?,
1674                                                                         issn             = ?,                           dewey            = ?,
1675                                                                         subclass         = ?,                           publicationyear  = ?,
1676                                                                         publishercode    = ?,           volumedate       = ?,
1677                                                                         volumeddesc      = ?,           illus            = ?,
1678                                                                         pages            = ?,                           notes            = ?,
1679                                                                         size             = ?,                           lccn             = ?,
1680                                                                         marc             = ?,                           place            = ?"
1681     );
1682     $sth->execute(
1683         $bibitemnum,                     $biblioitem->{'biblionumber'},
1684         $biblioitem->{'volume'},         $biblioitem->{'number'},
1685         $biblioitem->{'classification'}, $biblioitem->{'itemtype'},
1686         $biblioitem->{'url'},            $biblioitem->{'isbn'},
1687         $biblioitem->{'issn'},           $biblioitem->{'dewey'},
1688         $biblioitem->{'subclass'},       $biblioitem->{'publicationyear'},
1689         $biblioitem->{'publishercode'},  $biblioitem->{'volumedate'},
1690         $biblioitem->{'volumeddesc'},    $biblioitem->{'illus'},
1691         $biblioitem->{'pages'},          $biblioitem->{'bnotes'},
1692         $biblioitem->{'size'},           $biblioitem->{'lccn'},
1693         $biblioitem->{'marc'},           $biblioitem->{'place'}
1694     );
1695     $sth->finish;
1696
1697     #    $dbh->disconnect;
1698     return ($bibitemnum);
1699 }
1700
1701 sub OLDnewsubject {
1702     my ( $dbh, $bibnum ) = @_;
1703     my $sth =
1704       $dbh->prepare("insert into bibliosubject (biblionumber) values (?)");
1705     $sth->execute($bibnum);
1706     $sth->finish;
1707 }
1708
1709 sub OLDnewsubtitle {
1710     my ( $dbh, $bibnum, $subtitle ) = @_;
1711     my $sth =
1712       $dbh->prepare(
1713         "insert into bibliosubtitle set biblionumber = ?, subtitle = ?");
1714     $sth->execute( $bibnum, $subtitle );
1715     $sth->finish;
1716 }
1717
1718 sub OLDnewitems {
1719     my ( $dbh, $item, $barcode ) = @_;
1720
1721     #  my $dbh   = C4Connect;
1722     my $sth = $dbh->prepare("Select max(itemnumber) from items");
1723     my $data;
1724     my $itemnumber;
1725     my $error = "";
1726
1727     $sth->execute;
1728     $data       = $sth->fetchrow_hashref;
1729     $itemnumber = $data->{'max(itemnumber)'} + 1;
1730     $sth->finish;
1731
1732 # FIXME the "notforloan" field seems to be named "loan" in some places. workaround bugfix.
1733     if ( $item->{'loan'} ) {
1734         $item->{'notforloan'} = $item->{'loan'};
1735     }
1736
1737     # if dateaccessioned is provided, use it. Otherwise, set to NOW()
1738     if ( $item->{'dateaccessioned'} ) {
1739         $sth = $dbh->prepare( "Insert into items set
1740                                                         itemnumber           = ?,                               biblionumber         = ?,
1741                                                         biblioitemnumber     = ?,                               barcode              = ?,
1742                                                         booksellerid         = ?,                                       dateaccessioned      = ?,
1743                                                         homebranch           = ?,                               holdingbranch        = ?,
1744                                                         price                = ?,                                               replacementprice     = ?,
1745                                                         replacementpricedate = NOW(),   itemnotes            = ?,
1746                                                         itemcallnumber  =?,                                                     notforloan = ?
1747                                                         "
1748         );
1749         $sth->execute(
1750             $itemnumber,                 $item->{'biblionumber'},
1751             $item->{'biblioitemnumber'}, $barcode,
1752             $item->{'booksellerid'},     $item->{'dateaccessioned'},
1753             $item->{'homebranch'},       $item->{'holdingbranch'},
1754             $item->{'price'},            $item->{'replacementprice'},
1755             $item->{'itemnotes'},        $item->{'itemcallnumber'},
1756             $item->{'notforloan'}
1757         );
1758     }
1759     else {
1760         $sth = $dbh->prepare( "Insert into items set
1761                                                         itemnumber           = ?,                               biblionumber         = ?,
1762                                                         biblioitemnumber     = ?,                               barcode              = ?,
1763                                                         booksellerid         = ?,                                       dateaccessioned      = NOW(),
1764                                                         homebranch           = ?,                               holdingbranch        = ?,
1765                                                         price                = ?,                                               replacementprice     = ?,
1766                                                         replacementpricedate = NOW(),   itemnotes            = ?,
1767                                                         itemcallnumber = ? , notforloan = ?
1768                                                         "
1769         );
1770         $sth->execute(
1771             $itemnumber,                 $item->{'biblionumber'},
1772             $item->{'biblioitemnumber'}, $barcode,
1773             $item->{'booksellerid'},     $item->{'homebranch'},
1774             $item->{'holdingbranch'},    $item->{'price'},
1775             $item->{'replacementprice'}, $item->{'itemnotes'},
1776             $item->{'itemcallnumber'},   $item->{'notforloan'}
1777         );
1778     }
1779     if ( defined $sth->errstr ) {
1780         $error .= $sth->errstr;
1781     }
1782     $sth->finish;
1783     return ( $itemnumber, $error );
1784 }
1785
1786 sub OLDmoditem {
1787     my ( $dbh, $item ) = @_;
1788
1789 #  my ($dbh,$loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
1790     #  my $dbh=C4Connect;
1791     $item->{'itemnum'} = $item->{'itemnumber'} unless $item->{'itemnum'};
1792     my $query =
1793 "update items set  barcode=?,itemnotes=?,itemcallnumber=?,notforloan=? where itemnumber=?";
1794     my @bind = (
1795         $item->{'barcode'},        $item->{'notes'},
1796         $item->{'itemcallnumber'}, $item->{'notforloan'},
1797         $item->{'itemnum'}
1798     );
1799     if ( $item->{'barcode'} eq '' ) {
1800         $item->{'notforloan'} = 0 unless $item->{'notforloan'};
1801         $query = "update items set notforloan=? where itemnumber=?";
1802         @bind = ( $item->{'notforloan'}, $item->{'itemnum'} );
1803     }
1804     if ( $item->{'lost'} ne '' ) {
1805         $query = "update items set biblioitemnumber=?,
1806                              barcode=?,
1807                              itemnotes=?,
1808                              homebranch=?,
1809                              itemlost=?,
1810                              wthdrawn=?,
1811                              itemcallnumber=?,
1812                              notforloan=?";
1813         @bind = (
1814             $item->{'bibitemnum'},     $item->{'barcode'},
1815             $item->{'notes'},          $item->{'homebranch'},
1816             $item->{'lost'},           $item->{'wthdrawn'},
1817             $item->{'itemcallnumber'}, $item->{'notforloan'},
1818             $item->{'itemnum'}
1819         );
1820                 if ($item->{homebranch}) {
1821                         $query.=",homebranch=?";
1822                         push @bind, $item->{homebranch};
1823                 }
1824                 if ($item->{holdingbranch}) {
1825                         $query.=",holdingbranch=?";
1826                         push @bind, $item->{holdingbranch};
1827                 }
1828                 $query.=" where itemnumber=?";
1829     }
1830     if ( $item->{'replacement'} ne '' ) {
1831         $query =~ s/ where/,replacementprice='$item->{'replacement'}' where/;
1832     }
1833     my $sth = $dbh->prepare($query);
1834     $sth->execute(@bind);
1835     $sth->finish;
1836
1837     #  $dbh->disconnect;
1838 }
1839
1840 sub OLDdelitem {
1841     my ( $dbh, $itemnum ) = @_;
1842
1843     #  my $dbh=C4Connect;
1844     my $sth = $dbh->prepare("select * from items where itemnumber=?");
1845     $sth->execute($itemnum);
1846     my $data = $sth->fetchrow_hashref;
1847     $sth->finish;
1848     my $query = "Insert into deleteditems set ";
1849     my @bind  = ();
1850     foreach my $temp ( keys %$data ) {
1851         $query .= "$temp = ?,";
1852         push ( @bind, $data->{$temp} );
1853     }
1854     $query =~ s/\,$//;
1855
1856     #  print $query;
1857     $sth = $dbh->prepare($query);
1858     $sth->execute(@bind);
1859     $sth->finish;
1860     $sth = $dbh->prepare("Delete from items where itemnumber=?");
1861     $sth->execute($itemnum);
1862     $sth->finish;
1863
1864     #  $dbh->disconnect;
1865 }
1866
1867 sub OLDdeletebiblioitem {
1868     my ( $dbh, $biblioitemnumber ) = @_;
1869
1870     #    my $dbh   = C4Connect;
1871     my $sth = $dbh->prepare( "Select * from biblioitems
1872 where biblioitemnumber = ?"
1873     );
1874     my $results;
1875
1876     $sth->execute($biblioitemnumber);
1877
1878     if ( $results = $sth->fetchrow_hashref ) {
1879         $sth->finish;
1880         $sth =
1881           $dbh->prepare(
1882 "Insert into deletedbiblioitems (biblioitemnumber, biblionumber, volume, number, classification, itemtype,
1883                                         isbn, issn ,dewey ,subclass ,publicationyear ,publishercode ,volumedate ,volumeddesc ,timestamp ,illus ,
1884                                         pages ,notes ,size ,url ,lccn ) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
1885         );
1886
1887         $sth->execute(
1888             $results->{biblioitemnumber}, $results->{biblionumber},
1889             $results->{volume},           $results->{number},
1890             $results->{classification},   $results->{itemtype},
1891             $results->{isbn},             $results->{issn},
1892             $results->{dewey},            $results->{subclass},
1893             $results->{publicationyear},  $results->{publishercode},
1894             $results->{volumedate},       $results->{volumeddesc},
1895             $results->{timestamp},        $results->{illus},
1896             $results->{pages},            $results->{notes},
1897             $results->{size},             $results->{url},
1898             $results->{lccn}
1899         );
1900         my $sth2 =
1901           $dbh->prepare("Delete from biblioitems where biblioitemnumber = ?");
1902         $sth2->execute($biblioitemnumber);
1903         $sth2->finish();
1904     }    # if
1905     $sth->finish;
1906
1907     # Now delete all the items attached to the biblioitem
1908     $sth = $dbh->prepare("Select * from items where biblioitemnumber = ?");
1909     $sth->execute($biblioitemnumber);
1910     my @results;
1911     while ( my $data = $sth->fetchrow_hashref ) {
1912         my $query = "Insert into deleteditems set ";
1913         my @bind  = ();
1914         foreach my $temp ( keys %$data ) {
1915             $query .= "$temp = ?,";
1916             push ( @bind, $data->{$temp} );
1917         }
1918         $query =~ s/\,$//;
1919         my $sth2 = $dbh->prepare($query);
1920         $sth2->execute(@bind);
1921     }    # while
1922     $sth->finish;
1923     $sth = $dbh->prepare("Delete from items where biblioitemnumber = ?");
1924     $sth->execute($biblioitemnumber);
1925     $sth->finish();
1926
1927     #    $dbh->disconnect;
1928 }    # sub deletebiblioitem
1929
1930 sub OLDdelbiblio {
1931     my ( $dbh, $biblio ) = @_;
1932     my $sth = $dbh->prepare("select * from biblio where biblionumber=?");
1933     $sth->execute($biblio);
1934     if ( my $data = $sth->fetchrow_hashref ) {
1935         $sth->finish;
1936         my $query = "Insert into deletedbiblio set ";
1937         my @bind  = ();
1938         foreach my $temp ( keys %$data ) {
1939             $query .= "$temp = ?,";
1940             push ( @bind, $data->{$temp} );
1941         }
1942
1943         #replacing the last , by ",?)"
1944         $query =~ s/\,$//;
1945         $sth = $dbh->prepare($query);
1946         $sth->execute(@bind);
1947         $sth->finish;
1948         $sth = $dbh->prepare("Delete from biblio where biblionumber=?");
1949         $sth->execute($biblio);
1950         $sth->finish;
1951     }
1952     $sth->finish;
1953 }
1954
1955 #
1956 #
1957 # old functions
1958 #
1959 #
1960
1961 sub itemcount {
1962     my ($biblio) = @_;
1963     my $dbh = C4::Context->dbh;
1964
1965     #  print $query;
1966     my $sth = $dbh->prepare("Select count(*) from items where biblionumber=?");
1967     $sth->execute($biblio);
1968     my $data = $sth->fetchrow_hashref;
1969     $sth->finish;
1970     return ( $data->{'count(*)'} );
1971 }
1972
1973 sub newbiblio {
1974     my ($biblio) = @_;
1975     my $dbh    = C4::Context->dbh;
1976     my $bibnum = OLDnewbiblio( $dbh, $biblio );
1977
1978     # finds new (MARC bibid
1979     #   my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$bibnum);
1980     my $record = &MARCkoha2marcBiblio( $dbh, $bibnum );
1981     MARCaddbiblio( $dbh, $record, $bibnum,'' );
1982     return ($bibnum);
1983 }
1984
1985 =item modbiblio
1986
1987   $biblionumber = &modbiblio($biblio);
1988
1989 Update a biblio record.
1990
1991 C<$biblio> is a reference-to-hash whose keys are the fields in the
1992 biblio table in the Koha database. All fields must be present, not
1993 just the ones you wish to change.
1994
1995 C<&modbiblio> updates the record defined by
1996 C<$biblio-E<gt>{biblionumber}> with the values in C<$biblio>.
1997
1998 C<&modbiblio> returns C<$biblio-E<gt>{biblionumber}> whether it was
1999 successful or not.
2000
2001 =cut
2002
2003 sub modbiblio {
2004         my ($biblio) = @_;
2005         my $dbh  = C4::Context->dbh;
2006         my $biblionumber=OLDmodbiblio($dbh,$biblio);
2007         my $record = MARCkoha2marcBiblio($dbh,$biblionumber,$biblionumber);
2008         # finds new (MARC bibid
2009         my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
2010         MARCmodbiblio($dbh,$bibid,$record,"",0);
2011         return($biblionumber);
2012 } # sub modbiblio
2013
2014 =item modsubtitle
2015
2016   &modsubtitle($biblionumber, $subtitle);
2017
2018 Sets the subtitle of a book.
2019
2020 C<$biblionumber> is the biblionumber of the book to modify.
2021
2022 C<$subtitle> is the new subtitle.
2023
2024 =cut
2025
2026 sub modsubtitle {
2027     my ( $bibnum, $subtitle ) = @_;
2028     my $dbh = C4::Context->dbh;
2029     &OLDmodsubtitle( $dbh, $bibnum, $subtitle );
2030 }    # sub modsubtitle
2031
2032 =item modaddauthor
2033
2034   &modaddauthor($biblionumber, $author);
2035
2036 Replaces all additional authors for the book with biblio number
2037 C<$biblionumber> with C<$author>. If C<$author> is the empty string,
2038 C<&modaddauthor> deletes all additional authors.
2039
2040 =cut
2041
2042 sub modaddauthor {
2043     my ( $bibnum, @authors ) = @_;
2044     my $dbh = C4::Context->dbh;
2045     &OLDmodaddauthor( $dbh, $bibnum, @authors );
2046 }    # sub modaddauthor
2047
2048 =item modsubject
2049
2050   $error = &modsubject($biblionumber, $force, @subjects);
2051
2052 $force - a subject to force
2053
2054 $error - Error message, or undef if successful.
2055
2056 =cut
2057
2058 sub modsubject {
2059     my ( $bibnum, $force, @subject ) = @_;
2060     my $dbh = C4::Context->dbh;
2061     my $error = &OLDmodsubject( $dbh, $bibnum, $force, @subject );
2062     return ($error);
2063 }    # sub modsubject
2064
2065 sub modbibitem {
2066     my ($biblioitem) = @_;
2067     my $dbh = C4::Context->dbh;
2068     &OLDmodbibitem( $dbh, $biblioitem );
2069 }    # sub modbibitem
2070
2071 sub modnote {
2072     my ( $bibitemnum, $note ) = @_;
2073     my $dbh = C4::Context->dbh;
2074     &OLDmodnote( $dbh, $bibitemnum, $note );
2075 }
2076
2077 sub newbiblioitem {
2078     my ($biblioitem) = @_;
2079     my $dbh        = C4::Context->dbh;
2080     my $bibitemnum = &OLDnewbiblioitem( $dbh, $biblioitem );
2081
2082     ################################################################
2083     ## Fix template and shift this to newbiblio
2084     my @subjects = split ( /\n/, $biblioitem->{'subjectheadings'} );
2085     modsubject( $biblioitem->{'biblionumber'}, 1, @subjects );
2086
2087     ################################################################
2088     my $MARCbiblio =
2089       MARCkoha2marcBiblio( $dbh, 0, $bibitemnum )
2090       ; # the 0 means "do NOT retrieve biblio, only biblioitem, in the MARC record
2091     my $bibid =
2092       &MARCfind_MARCbibid_from_oldbiblionumber( $dbh,
2093         $biblioitem->{biblionumber} );
2094     &MARCaddbiblio( $dbh, $MARCbiblio, $biblioitem->{biblionumber}, $bibid );
2095     return ($bibitemnum);
2096 }
2097
2098 sub newsubject {
2099     my ($bibnum) = @_;
2100     my $dbh = C4::Context->dbh;
2101     &OLDnewsubject( $dbh, $bibnum );
2102 }
2103
2104 sub newsubtitle {
2105     my ( $bibnum, $subtitle ) = @_;
2106     my $dbh = C4::Context->dbh;
2107     &OLDnewsubtitle( $dbh, $bibnum, $subtitle );
2108 }
2109
2110 sub newitems {
2111     my ( $item, @barcodes ) = @_;
2112     my $dbh = C4::Context->dbh;
2113     my $errors;
2114     my $itemnumber;
2115     my $error;
2116     foreach my $barcode (@barcodes) {
2117         ( $itemnumber, $error ) = &OLDnewitems( $dbh, $item, uc($barcode) );
2118         $errors .= $error;
2119         my $MARCitem =
2120           &MARCkoha2marcItem( $dbh, $item->{biblionumber}, $itemnumber );
2121         &MARCadditem( $dbh, $MARCitem, $item->{biblionumber} );
2122     }
2123     return ($errors);
2124 }
2125
2126 sub moditem {
2127     my ($item) = @_;
2128     my $dbh = C4::Context->dbh;
2129     &OLDmoditem( $dbh, $item );
2130     my $MARCitem =
2131       &MARCkoha2marcItem( $dbh, $item->{'biblionumber'}, $item->{'itemnum'} );
2132     my $bibid =
2133       &MARCfind_MARCbibid_from_oldbiblionumber( $dbh, $item->{biblionumber} );
2134     &MARCmoditem( $dbh, $MARCitem, $bibid, $item->{itemnum}, 0 );
2135 }
2136
2137 sub checkitems {
2138     my ( $count, @barcodes ) = @_;
2139     my $dbh = C4::Context->dbh;
2140     my $error;
2141     my $sth = $dbh->prepare("Select * from items where barcode=?");
2142     for ( my $i = 0 ; $i < $count ; $i++ ) {
2143         $barcodes[$i] = uc $barcodes[$i];
2144         $sth->execute( $barcodes[$i] );
2145         if ( my $data = $sth->fetchrow_hashref ) {
2146             $error .= " Duplicate Barcode: $barcodes[$i]";
2147         }
2148     }
2149     $sth->finish;
2150     return ($error);
2151 }
2152
2153 sub countitems {
2154     my ($bibitemnum) = @_;
2155     my $dbh   = C4::Context->dbh;
2156     my $query = "";
2157     my $sth   =
2158       $dbh->prepare("Select count(*) from items where biblioitemnumber=?");
2159     $sth->execute($bibitemnum);
2160     my $data = $sth->fetchrow_hashref;
2161     $sth->finish;
2162     return ( $data->{'count(*)'} );
2163 }
2164
2165 sub delitem {
2166     my ($itemnum) = @_;
2167     my $dbh = C4::Context->dbh;
2168     &OLDdelitem( $dbh, $itemnum );
2169 }
2170
2171 sub deletebiblioitem {
2172     my ($biblioitemnumber) = @_;
2173     my $dbh = C4::Context->dbh;
2174     &OLDdeletebiblioitem( $dbh, $biblioitemnumber );
2175 }    # sub deletebiblioitem
2176
2177 sub delbiblio {
2178     my ($biblio) = @_;
2179     my $dbh = C4::Context->dbh;
2180     &OLDdelbiblio( $dbh, $biblio );
2181     my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber( $dbh, $biblio );
2182     &MARCdelbiblio( $dbh, $bibid, 0 );
2183 }
2184
2185 sub getbiblio {
2186     my ($biblionumber) = @_;
2187     my $dbh = C4::Context->dbh;
2188     my $sth = $dbh->prepare("Select * from biblio where biblionumber = ?");
2189
2190     # || die "Cannot prepare $query\n" . $dbh->errstr;
2191     my $count = 0;
2192     my @results;
2193
2194     $sth->execute($biblionumber);
2195
2196     # || die "Cannot execute $query\n" . $sth->errstr;
2197     while ( my $data = $sth->fetchrow_hashref ) {
2198         $results[$count] = $data;
2199         $count++;
2200     }    # while
2201
2202     $sth->finish;
2203     return ( $count, @results );
2204 }    # sub getbiblio
2205
2206 sub getbiblioitem {
2207     my ($biblioitemnum) = @_;
2208     my $dbh = C4::Context->dbh;
2209     my $sth = $dbh->prepare( "Select * from biblioitems where
2210 biblioitemnumber = ?"
2211     );
2212     my $count = 0;
2213     my @results;
2214
2215     $sth->execute($biblioitemnum);
2216
2217     while ( my $data = $sth->fetchrow_hashref ) {
2218         $results[$count] = $data;
2219         $count++;
2220     }    # while
2221
2222     $sth->finish;
2223     return ( $count, @results );
2224 }    # sub getbiblioitem
2225
2226 sub getbiblioitembybiblionumber {
2227     my ($biblionumber) = @_;
2228     my $dbh = C4::Context->dbh;
2229     my $sth = $dbh->prepare("Select * from biblioitems where biblionumber = ?");
2230     my $count = 0;
2231     my @results;
2232
2233     $sth->execute($biblionumber);
2234
2235     while ( my $data = $sth->fetchrow_hashref ) {
2236         $results[$count] = $data;
2237         $count++;
2238     }    # while
2239
2240     $sth->finish;
2241     return ( $count, @results );
2242 }    # sub
2243
2244 sub getitemtypes {
2245     my $dbh   = C4::Context->dbh;
2246     my $query = "select * from itemtypes order by description";
2247     my $sth   = $dbh->prepare($query);
2248
2249     # || die "Cannot prepare $query" . $dbh->errstr;      
2250     my $count = 0;
2251     my @results;
2252
2253     $sth->execute;
2254
2255     # || die "Cannot execute $query\n" . $sth->errstr;
2256     while ( my $data = $sth->fetchrow_hashref ) {
2257         $results[$count] = $data;
2258         $count++;
2259     }    # while
2260
2261     $sth->finish;
2262     return ( $count, @results );
2263 }    # sub getitemtypes
2264
2265 sub getitemsbybiblioitem {
2266     my ($biblioitemnum) = @_;
2267     my $dbh = C4::Context->dbh;
2268     my $sth = $dbh->prepare( "Select * from items, biblio where
2269 biblio.biblionumber = items.biblionumber and biblioitemnumber
2270 = ?"
2271     );
2272
2273     # || die "Cannot prepare $query\n" . $dbh->errstr;
2274     my $count = 0;
2275     my @results;
2276
2277     $sth->execute($biblioitemnum);
2278
2279     # || die "Cannot execute $query\n" . $sth->errstr;
2280     while ( my $data = $sth->fetchrow_hashref ) {
2281         $results[$count] = $data;
2282         $count++;
2283     }    # while
2284
2285     $sth->finish;
2286     return ( $count, @results );
2287 }    # sub getitemsbybiblioitem
2288
2289 sub logchange {
2290
2291     # Subroutine to log changes to databases
2292 # Eventually, this subroutine will be used to create a log of all changes made,
2293     # with the possibility of "undo"ing some changes
2294     my $database = shift;
2295     if ( $database eq 'kohadb' ) {
2296         my $type     = shift;
2297         my $section  = shift;
2298         my $item     = shift;
2299         my $original = shift;
2300         my $new      = shift;
2301
2302         #       print STDERR "KOHA: $type $section $item $original $new\n";
2303     }
2304     elsif ( $database eq 'marc' ) {
2305         my $type        = shift;
2306         my $Record_ID   = shift;
2307         my $tag         = shift;
2308         my $mark        = shift;
2309         my $subfield_ID = shift;
2310         my $original    = shift;
2311         my $new         = shift;
2312
2313 #       print STDERR "MARC: $type $Record_ID $tag $mark $subfield_ID $original $new\n";
2314     }
2315 }
2316
2317 #------------------------------------------------
2318
2319 #---------------------------------------
2320 # Find a biblio entry, or create a new one if it doesn't exist.
2321 #  If a "subtitle" entry is in hash, add it to subtitle table
2322 sub getoraddbiblio {
2323
2324     # input params
2325     my (
2326         $dbh,       # db handle
2327                     # FIXME - Unused argument
2328         $biblio,    # hash ref to fields
2329     ) = @_;
2330
2331     # return
2332     my $biblionumber;
2333
2334     my $debug = 0;
2335     my $sth;
2336     my $error;
2337
2338     #-----
2339     $dbh = C4::Context->dbh;
2340
2341     print "<PRE>Looking for biblio </PRE>\n" if $debug;
2342     $sth = $dbh->prepare( "select biblionumber
2343                 from biblio
2344                 where title=? and author=?
2345                   and copyrightdate=? and seriestitle=?"
2346     );
2347     $sth->execute(
2348         $biblio->{title},     $biblio->{author},
2349         $biblio->{copyright}, $biblio->{seriestitle}
2350     );
2351     if ( $sth->rows ) {
2352         ($biblionumber) = $sth->fetchrow;
2353         print "<PRE>Biblio exists with number $biblionumber</PRE>\n" if $debug;
2354     }
2355     else {
2356
2357         # Doesn't exist.  Add new one.
2358         print "<PRE>Adding biblio</PRE>\n" if $debug;
2359         ( $biblionumber, $error ) = &newbiblio($biblio);
2360         if ($biblionumber) {
2361             print "<PRE>Added with biblio number=$biblionumber</PRE>\n"
2362               if $debug;
2363             if ( $biblio->{subtitle} ) {
2364                 &newsubtitle( $biblionumber, $biblio->{subtitle} );
2365             }    # if subtitle
2366         }
2367         else {
2368             print "<PRE>Couldn't add biblio: $error</PRE>\n" if $debug;
2369         }    # if added
2370     }
2371
2372     return $biblionumber, $error;
2373
2374 }    # sub getoraddbiblio
2375
2376 sub char_decode {
2377
2378     # converts ISO 5426 coded string to ISO 8859-1
2379     # sloppy code : should be improved in next issue
2380     my ( $string, $encoding ) = @_;
2381     $_ = $string;
2382
2383     #   $encoding = C4::Context->preference("marcflavour") unless $encoding;
2384     if ( $encoding eq "UNIMARC" ) {
2385         s/\xe1/Æ/gm;
2386         s/\xe2/Ð/gm;
2387         s/\xe9/Ø/gm;
2388         s/\xec/þ/gm;
2389         s/\xf1/æ/gm;
2390         s/\xf3/ð/gm;
2391         s/\xf9/ø/gm;
2392         s/\xfb/ß/gm;
2393         s/\xc1\x61/à/gm;
2394         s/\xc1\x65/è/gm;
2395         s/\xc1\x69/ì/gm;
2396         s/\xc1\x6f/ò/gm;
2397         s/\xc1\x75/ù/gm;
2398         s/\xc1\x41/À/gm;
2399         s/\xc1\x45/È/gm;
2400         s/\xc1\x49/Ì/gm;
2401         s/\xc1\x4f/Ò/gm;
2402         s/\xc1\x55/Ù/gm;
2403         s/\xc2\x41/Á/gm;
2404         s/\xc2\x45/É/gm;
2405         s/\xc2\x49/Í/gm;
2406         s/\xc2\x4f/Ó/gm;
2407         s/\xc2\x55/Ú/gm;
2408         s/\xc2\x59/Ý/gm;
2409         s/\xc2\x61/á/gm;
2410         s/\xc2\x65/é/gm;
2411         s/\xc2\x69/í/gm;
2412         s/\xc2\x6f/ó/gm;
2413         s/\xc2\x75/ú/gm;
2414         s/\xc2\x79/ý/gm;
2415         s/\xc3\x41/Â/gm;
2416         s/\xc3\x45/Ê/gm;
2417         s/\xc3\x49/Î/gm;
2418         s/\xc3\x4f/Ô/gm;
2419         s/\xc3\x55/Û/gm;
2420         s/\xc3\x61/â/gm;
2421         s/\xc3\x65/ê/gm;
2422         s/\xc3\x69/î/gm;
2423         s/\xc3\x6f/ô/gm;
2424         s/\xc3\x75/û/gm;
2425         s/\xc4\x41/Ã/gm;
2426         s/\xc4\x4e/Ñ/gm;
2427         s/\xc4\x4f/Õ/gm;
2428         s/\xc4\x61/ã/gm;
2429         s/\xc4\x6e/ñ/gm;
2430         s/\xc4\x6f/õ/gm;
2431         s/\xc8\x45/Ë/gm;
2432         s/\xc8\x49/Ï/gm;
2433         s/\xc8\x65/ë/gm;
2434         s/\xc8\x69/ï/gm;
2435         s/\xc8\x76/ÿ/gm;
2436         s/\xc9\x41/Ä/gm;
2437         s/\xc9\x4f/Ö/gm;
2438         s/\xc9\x55/Ü/gm;
2439         s/\xc9\x61/ä/gm;
2440         s/\xc9\x6f/ö/gm;
2441         s/\xc9\x75/ü/gm;
2442         s/\xca\x41/Å/gm;
2443         s/\xca\x61/å/gm;
2444         s/\xd0\x43/Ç/gm;
2445         s/\xd0\x63/ç/gm;
2446
2447         # this handles non-sorting blocks (if implementation requires this)
2448         $string = nsb_clean($_);
2449     }
2450     elsif ( $encoding eq "USMARC" || $encoding eq "MARC21" ) {
2451         if (/[\xc1-\xff]/) {
2452             s/\xe1\x61/à/gm;
2453             s/\xe1\x65/è/gm;
2454             s/\xe1\x69/ì/gm;
2455             s/\xe1\x6f/ò/gm;
2456             s/\xe1\x75/ù/gm;
2457             s/\xe1\x41/À/gm;
2458             s/\xe1\x45/È/gm;
2459             s/\xe1\x49/Ì/gm;
2460             s/\xe1\x4f/Ò/gm;
2461             s/\xe1\x55/Ù/gm;
2462             s/\xe2\x41/Á/gm;
2463             s/\xe2\x45/É/gm;
2464             s/\xe2\x49/Í/gm;
2465             s/\xe2\x4f/Ó/gm;
2466             s/\xe2\x55/Ú/gm;
2467             s/\xe2\x59/Ý/gm;
2468             s/\xe2\x61/á/gm;
2469             s/\xe2\x65/é/gm;
2470             s/\xe2\x69/í/gm;
2471             s/\xe2\x6f/ó/gm;
2472             s/\xe2\x75/ú/gm;
2473             s/\xe2\x79/ý/gm;
2474             s/\xe3\x41/Â/gm;
2475             s/\xe3\x45/Ê/gm;
2476             s/\xe3\x49/Î/gm;
2477             s/\xe3\x4f/Ô/gm;
2478             s/\xe3\x55/Û/gm;
2479             s/\xe3\x61/â/gm;
2480             s/\xe3\x65/ê/gm;
2481             s/\xe3\x69/î/gm;
2482             s/\xe3\x6f/ô/gm;
2483             s/\xe3\x75/û/gm;
2484             s/\xe4\x41/Ã/gm;
2485             s/\xe4\x4e/Ñ/gm;
2486             s/\xe4\x4f/Õ/gm;
2487             s/\xe4\x61/ã/gm;
2488             s/\xe4\x6e/ñ/gm;
2489             s/\xe4\x6f/õ/gm;
2490             s/\xe8\x45/Ë/gm;
2491             s/\xe8\x49/Ï/gm;
2492             s/\xe8\x65/ë/gm;
2493             s/\xe8\x69/ï/gm;
2494             s/\xe8\x76/ÿ/gm;
2495             s/\xe9\x41/Ä/gm;
2496             s/\xe9\x4f/Ö/gm;
2497             s/\xe9\x55/Ü/gm;
2498             s/\xe9\x61/ä/gm;
2499             s/\xe9\x6f/ö/gm;
2500             s/\xe9\x75/ü/gm;
2501             s/\xea\x41/Å/gm;
2502             s/\xea\x61/å/gm;
2503
2504             # this handles non-sorting blocks (if implementation requires this)
2505             $string = nsb_clean($_);
2506         }
2507     }
2508     return ($string);
2509 }
2510
2511 sub nsb_clean {
2512     my $NSB = '\x88';    # NSB : begin Non Sorting Block
2513     my $NSE = '\x89';    # NSE : Non Sorting Block end
2514                          # handles non sorting blocks
2515     my ($string) = @_;
2516     $_ = $string;
2517     s/$NSB/(/gm;
2518     s/[ ]{0,1}$NSE/) /gm;
2519     $string = $_;
2520     return ($string);
2521 }
2522
2523 END { }    # module clean-up code here (global destructor)
2524
2525 =back
2526
2527 =head1 AUTHOR
2528
2529 Koha Developement team <info@koha.org>
2530
2531 Paul POULAIN paul.poulain@free.fr
2532
2533 =cut
2534
2535 # $Id$
2536 # $Log$
2537 # Revision 1.100  2004/08/13 16:37:25  tipaul
2538 # adding frameworkcode to API in some subs
2539 #
2540 # Revision 1.99  2004/07/30 13:54:50  doxulting
2541 # Beginning of serial commit
2542 #
2543 # Revision 1.98  2004/07/15 09:48:10  tipaul
2544 # * removing useless sub
2545 # * minor bugfix in moditem (managing homebranch & holdingbranch)
2546 #
2547 # Revision 1.97  2004/07/02 15:53:53  tipaul
2548 # bugfix (due to frameworkcode field)
2549 #
2550 # Revision 1.96  2004/06/29 16:07:10  tipaul
2551 # last sync for 2.1.0 release
2552 #
2553 # Revision 1.95  2004/06/26 23:19:59  rangi
2554 # Fixing modaddauthor, and adding getitemtypes.
2555 # Also tidying up formatting of code
2556 #
2557 # Revision 1.94  2004/06/17 08:16:32  tipaul
2558 # merging tag & subfield in marc_word for better perfs
2559 #
2560 # Revision 1.93  2004/06/11 15:38:06  joshferraro
2561 # Changes MARCaddword to index words >= 1 char ... needed for more accurate
2562 # searches using SearchMarc routines.
2563 #
2564 # Revision 1.92  2004/06/10 08:29:01  tipaul
2565 # MARC authority management (continued)
2566 #
2567 # Revision 1.91  2004/06/03 10:03:01  tipaul
2568 # * frameworks and itemtypes are independant
2569 # * in the MARC editor, showing the + to duplicate a tag only if the tag is repeatable
2570 #
2571 # Revision 1.90  2004/05/28 08:25:53  tipaul
2572 # hidding hidden & isurl constraints into MARC subfield structure
2573 #
2574 # Revision 1.89  2004/05/27 21:47:21  rangi
2575 # Fix for bug 787
2576 #
2577 # Revision 1.88  2004/05/18 15:23:49  tipaul
2578 # framework management : 1 MARC framework for each itemtype
2579 #
2580 # Revision 1.87  2004/05/18 11:54:07  tipaul
2581 # getitemtypes moved in Koha.pm
2582 #
2583 # Revision 1.86  2004/05/03 09:19:22  tipaul
2584 # some fixes for mysql prepare & execute
2585 #
2586 # Revision 1.85  2004/04/02 14:55:48  tipaul
2587 # renaming items.bulk field to items.itemcallnumber.
2588 # Will be used to store call number for libraries that don't use dewey classification.
2589 # Note it's related to ITEMS, not biblio.
2590 #
2591 # Revision 1.84  2004/03/24 17:18:30  joshferraro
2592 # Fixes bug 749 by removing the comma on line 1488.
2593 #
2594 # Revision 1.83  2004/03/15 14:31:50  tipaul
2595 # adding a minor check
2596 #
2597 # Revision 1.82  2004/03/07 05:47:31  acli
2598 # Various updates/fixes from rel_2_0
2599 # Fixes for bugs 721 (templating), 727, and 734
2600 #
2601 # Revision 1.81  2004/03/06 20:26:13  tipaul
2602 # adding seealso feature in MARC searches
2603 #
2604 # Revision 1.80  2004/02/12 13:40:56  tipaul
2605 # deleting subs duplicated by error
2606 #
2607 # Revision 1.79  2004/02/11 08:40:09  tipaul
2608 # synch'ing 2.0.0 branch and head
2609 #
2610 # Revision 1.78.2.3  2004/02/10 13:15:46  tipaul
2611 # removing 2 warnings
2612 #
2613 # Revision 1.78.2.2  2004/01/26 10:38:06  tipaul
2614 # dealing correctly "bulk" field
2615 #
2616 # Revision 1.78.2.1  2004/01/13 17:29:53  tipaul
2617 # * minor html fixes
2618 # * adding publisher in acquisition process (& ordering basket by publisher)
2619 #
2620 # Revision 1.78  2003/12/09 15:57:28  tipaul
2621 # rolling back to working char_decode sub
2622 #
2623 # Revision 1.77  2003/12/03 17:47:14  tipaul
2624 # bugfixes for biblio deletion
2625 #
2626 # Revision 1.76  2003/12/03 01:43:41  slef
2627 # conflict markers?
2628 #
2629 # Revision 1.75  2003/12/03 01:42:03  slef
2630 # bug 662 fixes securing DBI
2631 #
2632 # Revision 1.74  2003/11/28 09:48:33  tipaul
2633 # bugfix : misusing prepare & execute => now using prepare(?) and execute($var)
2634 #
2635 # Revision 1.73  2003/11/28 09:45:25  tipaul
2636 # bugfix for iso2709 file import in the "notforloan" field.
2637 #
2638 # But notforloan field called "loan" somewhere, so in case "loan" is used, copied to "notforloan" to avoid a bug.
2639 #
2640 # Revision 1.72  2003/11/24 17:40:14  tipaul
2641 # fix for #385
2642 #
2643 # Revision 1.71  2003/11/24 16:28:49  tipaul
2644 # biblio & item deletion now works fine in MARC editor.
2645 # Stores deleted biblio/item in the marc field of the deletedbiblio/deleteditem table.
2646 #
2647 # Revision 1.70  2003/11/24 13:29:55  tipaul
2648 # moving $id from beginning to end of file (70 commits... huge comments...)
2649 #
2650 # Revision 1.69  2003/11/24 13:27:17  tipaul
2651 # fix for #380 (bibliosubject)
2652 #
2653 # Revision 1.68  2003/11/06 17:18:30  tipaul
2654 # bugfix for #384
2655 #
2656 # 1st draft for MARC biblio deletion.
2657 # Still does not work well, but at least, Biblio.pm compiles & it should'nt break too many things
2658 # (Note the trash in the MARCdetail, but don't use it, please :-) )
2659 #
2660 # Revision 1.67  2003/10/25 08:46:27  tipaul
2661 # minor fixes for bilbio deletion (still buggy)
2662 #
2663 # Revision 1.66  2003/10/17 10:02:56  tipaul
2664 # Indexing only words longer than 2 letters. Was >=2 before, & 2 letters words usually means nothing.
2665 #
2666 # Revision 1.65  2003/10/14 09:45:29  tipaul
2667 # adding rebuildnonmarc.pl script : run this script when you change a link between marc and non MARC DB. It rebuilds the non-MARC DB (long operation)
2668 #
2669 # Revision 1.64  2003/10/06 15:20:51  tipaul
2670 # fix for 536 (subtitle error)
2671 #
2672 # Revision 1.63  2003/10/01 13:25:49  tipaul
2673 # seems a char encoding problem modified something in char_decode sub... changing back to something that works...
2674 #
2675 # Revision 1.62  2003/09/17 14:21:13  tipaul
2676 # fixing bug that makes a MARC biblio disappear when using full acquisition (order => recieve ==> MARC editor).
2677 # Before this 2 lines fix, the MARC biblio was deleted during recieve, and had to be entirely recreated :-(
2678 #
2679 # Revision 1.61  2003/09/17 10:24:39  tipaul
2680 # notforloan value in itemtype was overwritting notforloan value in a given item.
2681 # I changed this behaviour :
2682 # if notforloan is set for a given item, and NOT for all items from this itemtype, the notforloan is kept.
2683 # If notforloan is set for itemtype, it's used (and impossible to loan a specific item from this itemtype)
2684 #
2685 # Revision 1.60  2003/09/04 14:11:23  tipaul
2686 # fix for 593 (data duplication in MARC-DB)
2687 #
2688 # Revision 1.58  2003/08/06 12:54:52  tipaul
2689 # fix for publicationyear : extracting numeric value from MARC string, like for copyrightdate.
2690 # (note that copyrightdate still extracted to get numeric format)
2691 #
2692 # Revision 1.57  2003/07/15 23:09:18  slef
2693 # change show columns to use biblioitems bnotes too
2694 #
2695 # Revision 1.56  2003/07/15 11:34:52  slef
2696 # fixes from paul email
2697 #
2698 # Revision 1.55  2003/07/15 00:02:49  slef
2699 # Work on bug 515... can we do a single-side rename of notes to bnotes?
2700 #
2701 # Revision 1.54  2003/07/11 11:51:32  tipaul
2702 # *** empty log message ***
2703 #
2704 # Revision 1.52  2003/07/10 10:37:19  tipaul
2705 # fix for copyrightdate problem, #514
2706 #
2707 # Revision 1.51  2003/07/02 14:47:17  tipaul
2708 # fix for #519 : items.dateaccessioned imports incorrectly
2709 #
2710 # Revision 1.49  2003/06/17 11:21:13  tipaul
2711 # improvments/fixes for z3950 support.
2712 # * Works now even on ADD, not only on MODIFY
2713 # * able to search on ISBN, author, title
2714 #
2715 # Revision 1.48  2003/06/16 09:22:53  rangi
2716 # Just added an order clause to getitemtypes
2717 #
2718 # Revision 1.47  2003/05/20 16:22:44  tipaul
2719 # fixing typo in Biblio.pm POD
2720 #
2721 # Revision 1.46  2003/05/19 13:45:18  tipaul
2722 # support for subtitles, additional authors, subject.
2723 # This supports is only for MARC <-> OLD-DB link. It worked previously, but values entered as MARC were not reported to OLD-DB, neither values entered as OLD-DB were reported to MARC.
2724 # Note that some OLD-DB subs are strange (dummy ?) see OLDmodsubject, OLDmodsubtitle, OLDmodaddiauthor in C4/Biblio.pm
2725 # For example it seems impossible to have more that 1 addi author and 1 subtitle. In MARC it's not the case. So, if you enter more than one, I'm afraid only the LAST will be stored.
2726 #
2727 # Revision 1.45  2003/04/29 16:50:49  tipaul
2728 # really proud of this commit :-)
2729 # z3950 search and import seems to works fine.
2730 # Let me explain how :
2731 # * a "search z3950" button is added in the addbiblio template.
2732 # * when clicked, a popup appears and z3950/search.pl is called
2733 # * z3950/search.pl calls addz3950search in the DB
2734 # * the z3950 daemon retrieve the records and stores them in z3950results AND in marc_breeding table.
2735 # * as long as there as searches pending, the popup auto refresh every 2 seconds, and says how many searches are pending.
2736 # * when the user clicks on a z3950 result => the parent popup is called with the requested biblio, and auto-filled
2737 #
2738 # Note :
2739 # * 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.
2740 # * 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.
2741 #
2742 # Revision 1.44  2003/04/28 13:07:14  tipaul
2743 # Those fixes solves the "internal server error" with MARC::Record 1.12.
2744 # It was due to an illegal contruction in Koha : we tried to retrive subfields from <10 tags.
2745 # That's not possible. MARC::Record accepted this in 0.93 version, but it was fixed after.
2746 # Now, the construct/retrieving is OK !
2747 #
2748 # Revision 1.43  2003/04/10 13:56:02  tipaul
2749 # Fix some bugs :
2750 # * worked in 1.9.0, but not in 1.9.1 :
2751 # - modif of a biblio didn't work
2752 # - empty fields where not shown when modifying a biblio. empty fields managed by the library (ie in tab 0->9 in MARC parameter table) MUST be entered, even if not presented.
2753 #
2754 # * did not work before :
2755 # - repeatable subfields now works correctly. Enter 2 subfields separated by | and they will be splitted during saving.
2756 # - dropped the last subfield of the MARC form :-(
2757 #
2758 # Internal changes :
2759 # - MARCmodbiblio now works by deleting and recreating the biblio. It's not perf optimized, but MARC is a "do_something_impossible_to_trace" standard, so, it's the best solution. not a problem for me, as biblio are rarely modified.
2760 # Note the MARCdelbiblio has been rewritted to enable deletion of a biblio WITHOUT deleting items.
2761 #
2762 # Revision 1.42  2003/04/04 08:41:11  tipaul
2763 # last commits before 1.9.1
2764 #
2765 # Revision 1.41  2003/04/01 12:26:43  tipaul
2766 # fixes
2767 #
2768 # Revision 1.40  2003/03/11 15:14:03  tipaul
2769 # pod updating
2770 #
2771 # Revision 1.39  2003/03/07 16:35:42  tipaul
2772 # * moving generic functions to Koha.pm
2773 # * improvement of SearchMarc.pm
2774 # * bugfixes
2775 # * code cleaning
2776 #
2777 # Revision 1.38  2003/02/27 16:51:59  tipaul
2778 # * moving prepare / execute to ? form.
2779 # * some # cleaning
2780 # * little bugfix.
2781 # * road to 1.9.2 => acquisition and cataloguing merging
2782 #
2783 # Revision 1.37  2003/02/12 11:03:03  tipaul
2784 # Support for 000 -> 010 fields.
2785 # Those fields doesn't have subfields.
2786 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
2787 # Note it's only virtual : when rebuilding the MARC::Record, the koha API handle correctly "@" subfields => the resulting MARC record has a 00x field without subfield.
2788 #
2789 # Revision 1.36  2003/02/12 11:01:01  tipaul
2790 # Support for 000 -> 010 fields.
2791 # Those fields doesn't have subfields.
2792 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
2793 # Note it's only virtual : when rebuilding the MARC::Record, the koha API handle correctly "@" subfields => the resulting MARC record has a 00x field without subfield.
2794 #
2795 # Revision 1.35  2003/02/03 18:46:00  acli
2796 # Minor factoring in C4/Biblio.pm, plus change to export the per-tag
2797 # 'mandatory' property to a per-subfield 'tag_mandatory' template parameter,
2798 # so that addbiblio.tmpl can distinguish between mandatory subfields in a
2799 # mandatory tag and mandatory subfields in an optional tag
2800 #
2801 # Not-minor factoring in acqui.simple/addbiblio.pl to make the if-else blocks
2802 # smaller, and to add some POD; need further testing for this
2803 #
2804 # Added function to check if a MARC subfield name is "koha-internal" (instead
2805 # of checking it for 'lib' and 'tag' everywhere); temporarily added to Koha.pm
2806 #
2807 # Use above function in acqui.simple/additem.pl and search.marc/search.pl
2808 #
2809 # Revision 1.34  2003/01/28 14:50:04  tipaul
2810 # fixing MARCmodbiblio API and reindenting code
2811 #
2812 # Revision 1.33  2003/01/23 12:22:37  tipaul
2813 # adding char_decode to decode MARC21 or UNIMARC extended chars
2814 #
2815 # Revision 1.32  2002/12/16 15:08:50  tipaul
2816 # small but important bugfix (fixes a problem in export)
2817 #
2818 # Revision 1.31  2002/12/13 16:22:04  tipaul
2819 # 1st draft of marc export
2820 #
2821 # Revision 1.30  2002/12/12 21:26:35  tipaul
2822 # YAB ! (Yet Another Bugfix) => related to biblio modif
2823 # (some warning cleaning too)
2824 #
2825 # Revision 1.29  2002/12/12 16:35:00  tipaul
2826 # adding authentification with Auth.pm and
2827 # MAJOR BUGFIX on marc biblio modification
2828 #
2829 # Revision 1.28  2002/12/10 13:30:03  tipaul
2830 # fugfixes from Dombes Abbey work
2831 #
2832 # Revision 1.27  2002/11/19 12:36:16  tipaul
2833 # road to 1.3.2
2834 # various bugfixes, improvments, and migration from acquisition.pm to biblio.pm
2835 #
2836 # Revision 1.26  2002/11/12 15:58:43  tipaul
2837 # road to 1.3.2 :
2838 # * many bugfixes
2839 # * 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)
2840 #
2841 # Revision 1.25  2002/10/25 10:58:26  tipaul
2842 # Road to 1.3.2
2843 # * bugfixes and improvements
2844 #
2845 # Revision 1.24  2002/10/24 12:09:01  arensb
2846 # Fixed "no title" warning when generating HTML documentation from POD.
2847 #
2848 # Revision 1.23  2002/10/16 12:43:08  arensb
2849 # Added some FIXME comments.
2850 #
2851 # Revision 1.22  2002/10/15 13:39:17  tipaul
2852 # removing Acquisition.pm
2853 # deleting unused code in biblio.pm, rewriting POD and answering most FIXME comments
2854 #
2855 # Revision 1.21  2002/10/13 11:34:14  arensb
2856 # Replaced expressions of the form "$x = $x <op> $y" with "$x <op>= $y".
2857 # Thus, $x = $x+2 becomes $x += 2, and so forth.
2858 #
2859 # Revision 1.20  2002/10/13 08:28:32  arensb
2860 # Deleted unused variables.
2861 # Removed trailing whitespace.
2862 #
2863 # Revision 1.19  2002/10/13 05:56:10  arensb
2864 # Added some FIXME comments.
2865 #
2866 # Revision 1.18  2002/10/11 12:34:53  arensb
2867 # Replaced &requireDBI with C4::Context->dbh
2868 #
2869 # Revision 1.17  2002/10/10 14:48:25  tipaul
2870 # bugfixes
2871 #
2872 # Revision 1.16  2002/10/07 14:04:26  tipaul
2873 # road to 1.3.1 : viewing MARC biblio
2874 #
2875 # Revision 1.15  2002/10/05 09:49:25  arensb
2876 # Merged with arensb-context branch: use C4::Context->dbh instead of
2877 # &C4Connect, and generally prefer C4::Context over C4::Database.
2878 #
2879 # Revision 1.14  2002/10/03 11:28:18  tipaul
2880 # Extending Context.pm to add stopword management and using it in MARC-API.
2881 # First benchmarks show a medium speed improvement, which  is nice as this part is heavily called.
2882 #
2883 # Revision 1.13  2002/10/02 16:26:44  tipaul
2884 # road to 1.3.1
2885 #
2886 # Revision 1.12.2.4  2002/10/05 07:09:31  arensb
2887 # Merged in changes from main branch.
2888 #
2889 # Revision 1.12.2.3  2002/10/05 06:12:10  arensb
2890 # Added a whole mess of FIXME comments.
2891 #
2892 # Revision 1.12.2.2  2002/10/05 04:03:14  arensb
2893 # Added some missing semicolons.
2894 #
2895 # Revision 1.12.2.1  2002/10/04 02:24:01  arensb
2896 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
2897 # C4Connect.
2898 #
2899 # Revision 1.12.2.3  2002/10/05 06:12:10  arensb
2900 # Added a whole mess of FIXME comments.
2901 #
2902 # Revision 1.12.2.2  2002/10/05 04:03:14  arensb
2903 # Added some missing semicolons.
2904 #
2905 # Revision 1.12.2.1  2002/10/04 02:24:01  arensb
2906 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
2907 # C4Connect.
2908 #
2909 # Revision 1.12  2002/10/01 11:48:51  arensb
2910 # Added some FIXME comments, mostly marking duplicate functions.
2911 #
2912 # Revision 1.11  2002/09/24 13:49:26  tipaul
2913 # long WAS the road to 1.3.0...
2914 # coming VERY SOON NOW...
2915 # modifying installer and buildrelease to update the DB
2916 #
2917 # Revision 1.10  2002/09/22 16:50:08  arensb
2918 # Added some FIXME comments.
2919 #
2920 # Revision 1.9  2002/09/20 12:57:46  tipaul
2921 # long is the road to 1.4.0
2922 # * MARCadditem and MARCmoditem now wroks
2923 # * various bugfixes in MARC management
2924 # !!! 1.3.0 should be released very soon now. Be careful !!!
2925 #
2926 # Revision 1.8  2002/09/10 13:53:52  tipaul
2927 # MARC API continued...
2928 # * some bugfixes
2929 # * multiple item management : MARCadditem and MARCmoditem have been added. They suppose that ALL the MARC field linked to koha-item are in the same MARC tag (on the same line of MARC file)
2930 #
2931 # Note : it should not be hard for marcimport and marcexport to re-link fields from internal tag/subfield to "legal" tag/subfield.
2932 #
2933 # Revision 1.7  2002/08/14 18:12:51  tonnesen
2934 # Added copyright statement to all .pl and .pm files
2935 #
2936 # Revision 1.6  2002/07/25 13:40:31  tipaul
2937 # pod documenting the API.
2938 #
2939 # Revision 1.5  2002/07/24 16:11:37  tipaul
2940 # Now, the API...
2941 # Database.pm and Output.pm are almost not modified (var test...)
2942 #
2943 # Biblio.pm is almost completly rewritten.
2944 #
2945 # WHAT DOES IT ??? ==> END of Hitchcock suspens
2946 #
2947 # 1st, it does... nothing...
2948 # Every old API should be there. So if MARC-stuff is not done, the behaviour is EXACTLY the same (if there is no added bug, of course). So, if you use normal acquisition, you won't find anything new neither on screen or old-DB tables ...
2949 #
2950 # All old-API functions have been cloned. for example, the "newbiblio" sub, now has become :
2951 # * a "newbiblio" sub, with the same parameters. It just call a sub named OLDnewbiblio
2952 # * a "OLDnewbiblio" sub, which is a copy/paste of the previous newbiblio sub. Then, when you want to add the MARC-DB stuff, you can modify the newbiblio sub without modifying the OLDnewbiblio one. If we correct a bug in 1.2 in newbiblio, we can do the same in main branch by correcting OLDnewbiblio.
2953 # * The MARC stuff is usually done through a sub named MARCxxx where xxx is the same as OLDxxx. For example, newbiblio calls MARCnewbiblio. the MARCxxx subs use a MARC::Record as parameter.
2954 # The last thing to solve was to manage biblios through real MARC import : they must populate the old-db, but must populate the MARC-DB too, without loosing information (if we go from MARC::Record to old-data then back to MARC::Record, we loose A LOT OF ROWS). To do this, there are subs beginning by "NEWxxx" : they manage datas with MARC::Record datas. they call OLDxxx sub too (to populate old-DB), but MARCxxx subs too, with a complete MARC::Record ;-)
2955 #
2956 # In Biblio.pm, there are some subs that permits to build a old-style record from a MARC::Record, and the opposite. There is also a sub finding a MARC-bibid from a old-biblionumber and the opposite too.
2957 # Note we have decided with steve that a old-biblio <=> a MARC-Biblio.
2958 #