4 # Revision 1.53 2003/07/10 12:24:20 tipaul
7 # Revision 1.52 2003/07/10 10:37:19 tipaul
8 # fix for copyrightdate problem, #514
10 # Revision 1.51 2003/07/02 14:47:17 tipaul
11 # fix for #519 : items.dateaccessioned imports incorrectly
13 # Revision 1.49 2003/06/17 11:21:13 tipaul
14 # improvments/fixes for z3950 support.
15 # * Works now even on ADD, not only on MODIFY
16 # * able to search on ISBN, author, title
18 # Revision 1.48 2003/06/16 09:22:53 rangi
19 # Just added an order clause to getitemtypes
21 # Revision 1.47 2003/05/20 16:22:44 tipaul
22 # fixing typo in Biblio.pm POD
24 # Revision 1.46 2003/05/19 13:45:18 tipaul
25 # support for subtitles, additional authors, subject.
26 # 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.
27 # Note that some OLD-DB subs are strange (dummy ?) see OLDmodsubject, OLDmodsubtitle, OLDmodaddiauthor in C4/Biblio.pm
28 # 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.
30 # Revision 1.45 2003/04/29 16:50:49 tipaul
31 # really proud of this commit :-)
32 # z3950 search and import seems to works fine.
33 # Let me explain how :
34 # * a "search z3950" button is added in the addbiblio template.
35 # * when clicked, a popup appears and z3950/search.pl is called
36 # * z3950/search.pl calls addz3950search in the DB
37 # * the z3950 daemon retrieve the records and stores them in z3950results AND in marc_breeding table.
38 # * as long as there as searches pending, the popup auto refresh every 2 seconds, and says how many searches are pending.
39 # * when the user clicks on a z3950 result => the parent popup is called with the requested biblio, and auto-filled
42 # * 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.
43 # * 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.
45 # Revision 1.44 2003/04/28 13:07:14 tipaul
46 # Those fixes solves the "internal server error" with MARC::Record 1.12.
47 # It was due to an illegal contruction in Koha : we tried to retrive subfields from <10 tags.
48 # That's not possible. MARC::Record accepted this in 0.93 version, but it was fixed after.
49 # Now, the construct/retrieving is OK !
51 # Revision 1.43 2003/04/10 13:56:02 tipaul
53 # * worked in 1.9.0, but not in 1.9.1 :
54 # - modif of a biblio didn't work
55 # - 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.
57 # * did not work before :
58 # - repeatable subfields now works correctly. Enter 2 subfields separated by | and they will be splitted during saving.
59 # - dropped the last subfield of the MARC form :-(
62 # - 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.
63 # Note the MARCdelbiblio has been rewritted to enable deletion of a biblio WITHOUT deleting items.
65 # Revision 1.42 2003/04/04 08:41:11 tipaul
66 # last commits before 1.9.1
68 # Revision 1.41 2003/04/01 12:26:43 tipaul
71 # Revision 1.40 2003/03/11 15:14:03 tipaul
74 # Revision 1.39 2003/03/07 16:35:42 tipaul
75 # * moving generic functions to Koha.pm
76 # * improvement of SearchMarc.pm
80 # Revision 1.38 2003/02/27 16:51:59 tipaul
81 # * moving prepare / execute to ? form.
84 # * road to 1.9.2 => acquisition and cataloguing merging
86 # Revision 1.37 2003/02/12 11:03:03 tipaul
87 # Support for 000 -> 010 fields.
88 # Those fields doesn't have subfields.
89 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
90 # 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.
92 # Revision 1.36 2003/02/12 11:01:01 tipaul
93 # Support for 000 -> 010 fields.
94 # Those fields doesn't have subfields.
95 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
96 # 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.
98 # Revision 1.35 2003/02/03 18:46:00 acli
99 # Minor factoring in C4/Biblio.pm, plus change to export the per-tag
100 # 'mandatory' property to a per-subfield 'tag_mandatory' template parameter,
101 # so that addbiblio.tmpl can distinguish between mandatory subfields in a
102 # mandatory tag and mandatory subfields in an optional tag
104 # Not-minor factoring in acqui.simple/addbiblio.pl to make the if-else blocks
105 # smaller, and to add some POD; need further testing for this
107 # Added function to check if a MARC subfield name is "koha-internal" (instead
108 # of checking it for 'lib' and 'tag' everywhere); temporarily added to Koha.pm
110 # Use above function in acqui.simple/additem.pl and search.marc/search.pl
112 # Revision 1.34 2003/01/28 14:50:04 tipaul
113 # fixing MARCmodbiblio API and reindenting code
115 # Revision 1.33 2003/01/23 12:22:37 tipaul
116 # adding char_decode to decode MARC21 or UNIMARC extended chars
118 # Revision 1.32 2002/12/16 15:08:50 tipaul
119 # small but important bugfix (fixes a problem in export)
121 # Revision 1.31 2002/12/13 16:22:04 tipaul
122 # 1st draft of marc export
124 # Revision 1.30 2002/12/12 21:26:35 tipaul
125 # YAB ! (Yet Another Bugfix) => related to biblio modif
126 # (some warning cleaning too)
128 # Revision 1.29 2002/12/12 16:35:00 tipaul
129 # adding authentification with Auth.pm and
130 # MAJOR BUGFIX on marc biblio modification
132 # Revision 1.28 2002/12/10 13:30:03 tipaul
133 # fugfixes from Dombes Abbey work
135 # Revision 1.27 2002/11/19 12:36:16 tipaul
137 # various bugfixes, improvments, and migration from acquisition.pm to biblio.pm
139 # Revision 1.26 2002/11/12 15:58:43 tipaul
142 # * 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)
144 # Revision 1.25 2002/10/25 10:58:26 tipaul
146 # * bugfixes and improvements
148 # Revision 1.24 2002/10/24 12:09:01 arensb
149 # Fixed "no title" warning when generating HTML documentation from POD.
151 # Revision 1.23 2002/10/16 12:43:08 arensb
152 # Added some FIXME comments.
154 # Revision 1.22 2002/10/15 13:39:17 tipaul
155 # removing Acquisition.pm
156 # deleting unused code in biblio.pm, rewriting POD and answering most FIXME comments
158 # Revision 1.21 2002/10/13 11:34:14 arensb
159 # Replaced expressions of the form "$x = $x <op> $y" with "$x <op>= $y".
160 # Thus, $x = $x+2 becomes $x += 2, and so forth.
162 # Revision 1.20 2002/10/13 08:28:32 arensb
163 # Deleted unused variables.
164 # Removed trailing whitespace.
166 # Revision 1.19 2002/10/13 05:56:10 arensb
167 # Added some FIXME comments.
169 # Revision 1.18 2002/10/11 12:34:53 arensb
170 # Replaced &requireDBI with C4::Context->dbh
172 # Revision 1.17 2002/10/10 14:48:25 tipaul
175 # Revision 1.16 2002/10/07 14:04:26 tipaul
176 # road to 1.3.1 : viewing MARC biblio
178 # Revision 1.15 2002/10/05 09:49:25 arensb
179 # Merged with arensb-context branch: use C4::Context->dbh instead of
180 # &C4Connect, and generally prefer C4::Context over C4::Database.
182 # Revision 1.14 2002/10/03 11:28:18 tipaul
183 # Extending Context.pm to add stopword management and using it in MARC-API.
184 # First benchmarks show a medium speed improvement, which is nice as this part is heavily called.
186 # Revision 1.13 2002/10/02 16:26:44 tipaul
189 # Revision 1.12.2.4 2002/10/05 07:09:31 arensb
190 # Merged in changes from main branch.
192 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
193 # Added a whole mess of FIXME comments.
195 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
196 # Added some missing semicolons.
198 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
199 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
202 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
203 # Added a whole mess of FIXME comments.
205 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
206 # Added some missing semicolons.
208 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
209 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
212 # Revision 1.12 2002/10/01 11:48:51 arensb
213 # Added some FIXME comments, mostly marking duplicate functions.
215 # Revision 1.11 2002/09/24 13:49:26 tipaul
216 # long WAS the road to 1.3.0...
217 # coming VERY SOON NOW...
218 # modifying installer and buildrelease to update the DB
220 # Revision 1.10 2002/09/22 16:50:08 arensb
221 # Added some FIXME comments.
223 # Revision 1.9 2002/09/20 12:57:46 tipaul
224 # long is the road to 1.4.0
225 # * MARCadditem and MARCmoditem now wroks
226 # * various bugfixes in MARC management
227 # !!! 1.3.0 should be released very soon now. Be careful !!!
229 # Revision 1.8 2002/09/10 13:53:52 tipaul
230 # MARC API continued...
232 # * 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)
234 # Note : it should not be hard for marcimport and marcexport to re-link fields from internal tag/subfield to "legal" tag/subfield.
236 # Revision 1.7 2002/08/14 18:12:51 tonnesen
237 # Added copyright statement to all .pl and .pm files
239 # Revision 1.6 2002/07/25 13:40:31 tipaul
240 # pod documenting the API.
242 # Revision 1.5 2002/07/24 16:11:37 tipaul
244 # Database.pm and Output.pm are almost not modified (var test...)
246 # Biblio.pm is almost completly rewritten.
248 # WHAT DOES IT ??? ==> END of Hitchcock suspens
250 # 1st, it does... nothing...
251 # 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 ...
253 # All old-API functions have been cloned. for example, the "newbiblio" sub, now has become :
254 # * a "newbiblio" sub, with the same parameters. It just call a sub named OLDnewbiblio
255 # * 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.
256 # * 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.
257 # 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 ;-)
259 # 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.
260 # Note we have decided with steve that a old-biblio <=> a MARC-Biblio.
264 # Copyright 2000-2002 Katipo Communications
266 # This file is part of Koha.
268 # Koha is free software; you can redistribute it and/or modify it under the
269 # terms of the GNU General Public License as published by the Free Software
270 # Foundation; either version 2 of the License, or (at your option) any later
273 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
274 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
275 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
277 # You should have received a copy of the GNU General Public License along with
278 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
279 # Suite 330, Boston, MA 02111-1307 USA
287 use vars qw($VERSION @ISA @EXPORT);
289 # set the version for version checking
294 # don't forget MARCxxx subs are here only for testing purposes. Should not be used
295 # as the old-style API and the NEW one are the only public functions.
298 &updateBiblio &updateBiblioItem &updateItem
299 &itemcount &newbiblio &newbiblioitem
300 &modnote &newsubject &newsubtitle
301 &modbiblio &checkitems
302 &newitems &modbibitem
303 &modsubtitle &modsubject &modaddauthor &moditem &countitems
304 &delitem &deletebiblioitem &delbiblio
305 &getitemtypes &getbiblio
306 &getbiblioitembybiblionumber
307 &getbiblioitem &getitemsbybiblioitem
309 &newcompletebiblioitem
311 &MARCfind_oldbiblionumber_from_MARCbibid
312 &MARCfind_MARCbibid_from_oldbiblionumber
313 &MARCfind_marc_from_kohafield
317 &NEWnewbiblio &NEWnewitem
318 &NEWmodbiblio &NEWmoditem
320 &MARCaddbiblio &MARCadditem
321 &MARCmodsubfield &MARCaddsubfield
322 &MARCmodbiblio &MARCmoditem
323 &MARCkoha2marcBiblio &MARCmarc2koha
324 &MARCkoha2marcItem &MARChtml2marc
325 &MARCgetbiblio &MARCgetitem
326 &MARCaddword &MARCdelword
332 # MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC
335 # all the following subs takes a MARC::Record as parameter and manage
336 # the MARC-DB. They are called by the 1.0/1.2 xxx subs, and by the
337 # NEWxxx subs (xxx deals with old-DB parameters, the NEWxxx deals with MARC-DB parameter)
341 C4::Biblio - acquisition, catalog management functions
345 move from 1.2 to 1.4 version :
346 1.2 and previous version uses a specific API to manage biblios. This API uses old-DB style parameters.
347 In the 1.4 version, we want to do 2 differents things :
348 - keep populating the old-DB, that has a LOT less datas than MARC
349 - populate the MARC-DB
350 To populate the DBs we have 2 differents sources :
351 - the standard acquisition system (through book sellers), that does'nt use MARC data
352 - the MARC acquisition system, that uses MARC data.
354 Thus, we have 2 differents cases :
355 - 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
356 - 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
358 That's why we need 4 subs :
359 all I<subs beginning by MARC> manage only MARC tables. They manage MARC-DB with MARC::Record parameters
360 all I<subs beginning by OLD> manage only OLD-DB tables. They manage old-DB with old-DB parameters
361 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
362 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.
364 - NEW and old-style API should be used in koha to manage biblio
365 - MARCsubs are divided in 2 parts :
366 * some of them manage MARC parameters. They are heavily used in koha.
367 * some of them manage MARC biblio : they are mostly used by NEW and old-style subs.
368 - OLD are used internally only
370 all subs requires/use $dbh as 1st parameter.
372 I<NEWxxx related subs>
374 all subs requires/use $dbh as 1st parameter.
375 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
377 I<OLDxxx related subs>
379 all subs requires/use $dbh as 1st parameter.
380 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
382 They all are the exact copy of 1.0/1.2 version of the sub without the OLD.
383 The OLDxxx is called by the original xxx sub.
384 the 1.4 xxx sub also builds MARC::Record an calls the MARCxxx
386 WARNING : there is 1 difference between initialxxx and OLDxxx :
387 the db header $dbh is always passed as parameter to avoid over-DB connexion
393 =item @tagslib = &MARCgettagslib($dbh,1|0);
395 last param is 1 for liblibrarian and 0 for libopac
396 returns a hash with tag/subfield meaning
397 =item ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,$kohafield);
399 finds MARC tag and subfield for a given kohafield
400 kohafield is "table.field" where table= biblio|biblioitems|items, and field a field of the previous table
402 =item $biblionumber = &MARCfind_oldbiblionumber_from_MARCbibid($dbh,$MARCbibi);
404 finds a old-db biblio number for a given MARCbibid number
406 =item $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
408 finds a MARC bibid from a old-db biblionumber
410 =item $MARCRecord = &MARCkoha2marcBiblio($dbh,$biblionumber,biblioitemnumber);
412 MARCkoha2marcBiblio is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB biblio/biblioitem
414 =item $MARCRecord = &MARCkoha2marcItem($dbh,$biblionumber,itemnumber);
416 MARCkoha2marcItem is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB item
418 =item $MARCRecord = &MARCkoha2marcSubtitle($dbh,$biblionumber,$subtitle);
420 MARCkoha2marcSubtitle is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB subtitle
422 =item $olddb = &MARCmarc2koha($dbh,$MARCRecord);
424 builds a hash with old-db datas from a MARC::Record
426 =item &MARCaddbiblio($dbh,$MARC::Record,$biblionumber);
428 creates a biblio (in the MARC tables only). $biblionumber is the old-db biblionumber of the biblio
430 =item &MARCaddsubfield($dbh,$bibid,$tagid,$indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
432 adds a subfield in a biblio (in the MARC tables only).
434 =item $MARCRecord = &MARCgetbiblio($dbh,$bibid);
436 Returns a MARC::Record for the biblio $bibid.
438 =item &MARCmodbiblio($dbh,$bibid,$record,$delete);
440 MARCmodbiblio changes a biblio for a biblio,MARC::Record passed as parameter
441 It 1st delete the biblio, then recreates it.
442 WARNING : the $delete parameter is not used anymore (too much unsolvable cases).
443 =item ($subfieldid,$subfieldvalue) = &MARCmodsubfield($dbh,$subfieldid,$subfieldvalue);
445 MARCmodsubfield changes the value of a given subfield
447 =item $subfieldid = &MARCfindsubfield($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue);
449 MARCfindsubfield returns a subfield number given a bibid/tag/subfieldvalue values.
450 Returns -1 if more than 1 answer
452 =item $subfieldid = &MARCfindsubfieldid($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
454 MARCfindsubfieldid find a subfieldid for a bibid/tag/tagorder/subfield/subfieldorder
456 =item &MARCdelsubfield($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
458 MARCdelsubfield delete a subfield for a bibid/tag/tagorder/subfield/subfieldorder
460 =item &MARCdelbiblio($dbh,$bibid);
462 MARCdelbiblio delete biblio $bibid
464 =item &MARCkoha2marcOnefield
466 used by MARCkoha2marc and should not be useful elsewhere
468 =item &MARCmarc2kohaOnefield
470 used by MARCmarc2koha and should not be useful elsewhere
474 used to manage MARC_word table and should not be useful elsewhere
478 used to manage MARC_word table and should not be useful elsewhere
483 my ($dbh,$forlibrarian)= @_;
485 my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
486 $sth=$dbh->prepare("select tagfield,$libfield as lib,mandatory from marc_tag_structure order by tagfield");
488 my ($lib,$tag,$res,$tab,$mandatory,$repeatable);
489 while ( ($tag,$lib,$mandatory) = $sth->fetchrow) {
490 $res->{$tag}->{lib}=$lib;
491 $res->{$tab}->{tab}=""; # XXX
492 $res->{$tag}->{mandatory}=$mandatory;
495 $sth=$dbh->prepare("select tagfield,tagsubfield,$libfield as lib,tab, mandatory, repeatable,authorised_value,thesaurus_category,value_builder,kohafield from marc_subfield_structure order by tagfield,tagsubfield");
499 my $authorised_value;
500 my $thesaurus_category;
503 while ( ($tag, $subfield, $lib, $tab, $mandatory, $repeatable,$authorised_value,$thesaurus_category,$value_builder,$kohafield) = $sth->fetchrow) {
504 $res->{$tag}->{$subfield}->{lib}=$lib;
505 $res->{$tag}->{$subfield}->{tab}=$tab;
506 $res->{$tag}->{$subfield}->{mandatory}=$mandatory;
507 $res->{$tag}->{$subfield}->{repeatable}=$repeatable;
508 $res->{$tag}->{$subfield}->{authorised_value}=$authorised_value;
509 $res->{$tag}->{$subfield}->{thesaurus_category}=$thesaurus_category;
510 $res->{$tag}->{$subfield}->{value_builder}=$value_builder;
511 $res->{$tag}->{$subfield}->{kohafield}=$kohafield;
516 sub MARCfind_marc_from_kohafield {
517 my ($dbh,$kohafield) = @_;
518 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
519 $sth->execute($kohafield);
520 my ($tagfield,$tagsubfield) = $sth->fetchrow;
521 return ($tagfield,$tagsubfield);
524 sub MARCfind_oldbiblionumber_from_MARCbibid {
525 my ($dbh,$MARCbibid) = @_;
526 my $sth=$dbh->prepare("select biblionumber from marc_biblio where bibid=?");
527 $sth->execute($MARCbibid);
528 my ($biblionumber) = $sth->fetchrow;
529 return $biblionumber;
532 sub MARCfind_MARCbibid_from_oldbiblionumber {
533 my ($dbh,$oldbiblionumber) = @_;
534 my $sth=$dbh->prepare("select bibid from marc_biblio where biblionumber=?");
535 $sth->execute($oldbiblionumber);
536 my ($bibid) = $sth->fetchrow;
541 # pass the MARC::Record to this function, and it will create the records in the marc tables
542 my ($dbh,$record,$biblionumber,$bibid) = @_;
543 my @fields=$record->fields();
545 # adding main table, and retrieving bibid
546 # if bibid is sent, then it's not a true add, it's only a re-add, after a delete (ie, a mod)
547 # if bibid empty => true add, find a new bibid number
549 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
550 my $sth=$dbh->prepare("insert into marc_biblio (datecreated,biblionumber) values (now(),?)");
551 $sth->execute($biblionumber);
552 $sth=$dbh->prepare("select max(bibid) from marc_biblio");
554 ($bibid)=$sth->fetchrow;
558 # now, add subfields...
559 foreach my $field (@fields) {
561 if ($field->tag() <10) {
562 &MARCaddsubfield($dbh,$bibid,
571 my @subfields=$field->subfields();
572 foreach my $subfieldcount (0..$#subfields) {
573 &MARCaddsubfield($dbh,$bibid,
575 $field->indicator(1).$field->indicator(2),
577 $subfields[$subfieldcount][0],
579 $subfields[$subfieldcount][1]
584 $dbh->do("unlock tables");
589 # pass the MARC::Record to this function, and it will create the records in the marc tables
590 my ($dbh,$record,$biblionumber) = @_;
591 # warn "adding : ".$record->as_formatted();
592 # search for MARC biblionumber
593 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
594 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
595 my @fields=$record->fields();
596 my $sth = $dbh->prepare("select max(tagorder) from marc_subfield_table where bibid=?");
597 $sth->execute($bibid);
598 my ($fieldcount) = $sth->fetchrow;
599 # now, add subfields...
600 foreach my $field (@fields) {
601 my @subfields=$field->subfields();
603 foreach my $subfieldcount (0..$#subfields) {
604 &MARCaddsubfield($dbh,$bibid,
606 $field->indicator(1).$field->indicator(2),
608 $subfields[$subfieldcount][0],
610 $subfields[$subfieldcount][1]
614 $dbh->do("unlock tables");
618 sub MARCaddsubfield {
619 # Add a new subfield to a tag into the DB.
620 my ($dbh,$bibid,$tagid,$tag_indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalues) = @_;
621 # if not value, end of job, we do nothing
622 if (length($subfieldvalues) ==0) {
625 if (not($subfieldcode)) {
628 my @subfieldvalues = split /\|/,$subfieldvalues;
629 foreach my $subfieldvalue (@subfieldvalues) {
630 if (length($subfieldvalue)>255) {
631 # $dbh->do("lock tables marc_blob_subfield WRITE, marc_subfield_table WRITE");
632 my $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
633 $sth->execute($subfieldvalue);
634 $sth=$dbh->prepare("select max(blobidlink)from marc_blob_subfield");
636 my ($res)=$sth->fetchrow;
637 $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,valuebloblink) values (?,?,?,?,?,?,?)");
638 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$res);
640 warn "ERROR ==> insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values ($bibid,$tagid,$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue)\n";
642 # $dbh->do("unlock tables");
644 my $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?,?)");
645 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue);
647 warn "ERROR ==> insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values ($bibid,$tagid,$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue)\n";
650 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
655 # Returns MARC::Record of the biblio passed in parameter.
657 my $record = MARC::Record->new();
658 #---- TODO : the leader is missing
659 $record->leader(' ');
660 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
661 from marc_subfield_table
662 where bibid=? order by tag,tagorder,subfieldcode
664 my $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
665 $sth->execute($bibid);
669 my $field; # for >=10 tags
670 my $prevvalue; # for <10 tags
671 while (my $row=$sth->fetchrow_hashref) {
672 if ($row->{'valuebloblink'}) { #---- search blob if there is one
673 $sth2->execute($row->{'valuebloblink'});
674 my $row2=$sth2->fetchrow_hashref;
676 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
678 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
681 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
683 $record->add_fields($field) unless $prevtag eq "XXX";
686 $prevtagorder=$row->{tagorder};
687 $prevtag = $row->{tag};
688 $previndicator=$row->{tag_indicator};
689 if ($row->{tag}<10) {
690 $prevvalue = $row->{subfieldvalue};
692 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.' ',0,1), substr($row->{tag_indicator}.' ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
695 if ($row->{tag} <10) {
696 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
698 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
700 $prevtag= $row->{tag};
701 $previndicator=$row->{tag_indicator};
704 # the last has not been included inside the loop... do it now !
706 $record->add_fields($prevtag,$prevvalue);
708 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
709 $record->add_fields($field);
714 # Returns MARC::Record of the biblio passed in parameter.
715 my ($dbh,$bibid,$itemnumber)=@_;
716 my $record = MARC::Record->new();
717 # search MARC tagorder
718 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=?");
719 $sth2->execute($bibid,$itemnumber);
720 my ($tagorder) = $sth2->fetchrow_array();
721 #---- TODO : the leader is missing
722 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
723 from marc_subfield_table
724 where bibid=? and tagorder=? order by subfieldcode,subfieldorder
726 $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
727 $sth->execute($bibid,$tagorder);
728 while (my $row=$sth->fetchrow_hashref) {
729 if ($row->{'valuebloblink'}) { #---- search blob if there is one
730 $sth2->execute($row->{'valuebloblink'});
731 my $row2=$sth2->fetchrow_hashref;
733 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
735 if ($record->field($row->{'tag'})) {
737 #--- this test must stay as this, because of strange behaviour of mySQL/Perl DBI with char var containing a number...
738 #--- sometimes, eliminates 0 at beginning, sometimes no ;-\\\
739 if (length($row->{'tag'}) <3) {
740 $row->{'tag'} = "0".$row->{'tag'};
742 $field =$record->field($row->{'tag'});
744 my $x = $field->add_subfields($row->{'subfieldcode'},$row->{'subfieldvalue'});
745 $record->delete_field($field);
746 $record->add_fields($field);
749 if (length($row->{'tag'}) < 3) {
750 $row->{'tag'} = "0".$row->{'tag'};
752 my $temp = MARC::Field->new($row->{'tag'}," "," ", $row->{'subfieldcode'} => $row->{'subfieldvalue'});
753 $record->add_fields($temp);
761 my ($dbh,$bibid,$record,$delete)=@_;
762 my $oldrecord=&MARCgetbiblio($dbh,$bibid);
763 if ($oldrecord eq $record) {
766 # 1st delete the biblio,
768 &MARCdelbiblio($dbh,$bibid,1);
769 my $biblionumber = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
770 &MARCaddbiblio($dbh,$record,$biblionumber,$bibid);
774 my ($dbh,$bibid,$keep_items) = @_;
775 # if the keep_item is set to 1, then all items are preserved.
776 # This flag is set when the delbiblio is called by modbiblio
777 # due to a too complex structure of MARC (repeatable fields and subfields),
778 # the best solution for a modif is to delete / recreate the record.
779 if ($keep_items eq 1) {
780 #search item field code
781 my $sth = $dbh->prepare("select tagfield from marc_subfield_structure where kohafield like 'items.%'");
783 my $itemtag = $sth->fetchrow_hashref->{tagfield};
784 $dbh->do("delete from marc_subfield_table where bibid=$bibid and tag<>$itemtag");
785 $dbh->do("delete from marc_word where bibid=$bibid and tag<>$itemtag");
787 $dbh->do("delete from marc_biblio where bibid=$bibid");
788 $dbh->do("delete from marc_subfield_table where bibid=$bibid");
789 $dbh->do("delete from marc_word where bibid=$bibid");
793 my ($dbh,$record,$bibid,$itemnumber,$delete)=@_;
794 my $oldrecord=&MARCgetitem($dbh,$bibid,$itemnumber);
795 # if nothing to change, don't waste time...
796 if ($oldrecord eq $record) {
800 # otherwise, skip through each subfield...
801 my @fields = $record->fields();
802 # search old MARC item
803 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=?");
804 $sth2->execute($bibid,$itemnumber);
805 my ($tagorder) = $sth2->fetchrow_array();
806 foreach my $field (@fields) {
807 my $oldfield = $oldrecord->field($field->tag());
808 my @subfields=$field->subfields();
810 foreach my $subfield (@subfields) {
812 # warn "compare : $oldfield".$oldfield->subfield(@$subfield[0]);
813 if ($oldfield eq 0 or (length($oldfield->subfield(@$subfield[0])) ==0) ) {
814 # just adding datas...
815 # warn "addfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
816 # warn "NEW subfield : $bibid,".$field->tag().",".$tagorder.",".@$subfield[0].",".$subfieldorder.",".@$subfield[1].")";
817 &MARCaddsubfield($dbh,$bibid,$field->tag(),$field->indicator(1).$field->indicator(2),
818 $tagorder,@$subfield[0],$subfieldorder,@$subfield[1]);
820 # warn "modfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
821 # modify he subfield if it's a different string
822 if ($oldfield->subfield(@$subfield[0]) ne @$subfield[1] ) {
823 my $subfieldid=&MARCfindsubfieldid($dbh,$bibid,$field->tag(),$tagorder,@$subfield[0],$subfieldorder);
824 # warn "changing : $subfieldid, $bibid,".$field->tag(),",$tagorder,@$subfield[0],@$subfield[1],$subfieldorder";
825 &MARCmodsubfield($dbh,$subfieldid,@$subfield[1]);
833 sub MARCmodsubfield {
834 # Subroutine changes a subfield value given a subfieldid.
835 my ($dbh, $subfieldid, $subfieldvalue )=@_;
836 $dbh->do("lock tables marc_blob_subfield WRITE,marc_subfield_table WRITE");
837 my $sth1=$dbh->prepare("select valuebloblink from marc_subfield_table where subfieldid=?");
838 $sth1->execute($subfieldid);
839 my ($oldvaluebloblink)=$sth1->fetchrow;
842 # if too long, use a bloblink
843 if (length($subfieldvalue)>255 ) {
844 # if already a bloblink, update it, otherwise, insert a new one.
845 if ($oldvaluebloblink) {
846 $sth=$dbh->prepare("update marc_blob_subfield set subfieldvalue=? where blobidlink=?");
847 $sth->execute($subfieldvalue,$oldvaluebloblink);
849 $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
850 $sth->execute($subfieldvalue);
851 $sth=$dbh->prepare("select max(blobidlink) from marc_blob_subfield");
853 my ($res)=$sth->fetchrow;
854 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=null, valuebloblink=$res where subfieldid=?");
855 $sth->execute($subfieldid);
858 # note this can leave orphan bloblink. Not a big problem, but we should build somewhere a orphan deleting script...
859 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=?,valuebloblink=null where subfieldid=?");
860 $sth->execute($subfieldvalue, $subfieldid);
862 $dbh->do("unlock tables");
864 $sth=$dbh->prepare("select bibid,tag,tagorder,subfieldcode,subfieldid,subfieldorder from marc_subfield_table where subfieldid=?");
865 $sth->execute($subfieldid);
866 my ($bibid,$tagid,$tagorder,$subfieldcode,$x,$subfieldorder) = $sth->fetchrow;
868 &MARCdelword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder);
869 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
870 return($subfieldid, $subfieldvalue);
873 sub MARCfindsubfield {
874 my ($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue) = @_;
878 my $query="select subfieldid from marc_subfield_table where bibid=? and tag=? and subfieldcode=?";
879 if ($subfieldvalue) {
880 $query .= " and subfieldvalue=".$dbh->quote($subfieldvalue);
882 if ($subfieldorder<1) {
885 $query .= " and subfieldorder=$subfieldorder";
887 my $sti=$dbh->prepare($query);
888 $sti->execute($bibid,$tag, $subfieldcode);
889 while (($subfieldid) = $sti->fetchrow) {
891 $lastsubfieldid=$subfieldid;
893 if ($resultcounter>1) {
894 # Error condition. Values given did not resolve into a unique record. Don't know what to edit
895 # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
898 return $lastsubfieldid;
902 sub MARCfindsubfieldid {
903 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
904 my $sth=$dbh->prepare("select subfieldid from marc_subfield_table
905 where bibid=? and tag=? and tagorder=?
906 and subfieldcode=? and subfieldorder=?");
907 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
908 my ($res) = $sth->fetchrow;
910 $sth=$dbh->prepare("select subfieldid from marc_subfield_table
911 where bibid=? and tag=? and tagorder=?
912 and subfieldcode=?");
913 $sth->execute($bibid,$tag,$tagorder,$subfield);
914 ($res) = $sth->fetchrow;
919 sub MARCdelsubfield {
920 # delete a subfield for $bibid / tag / tagorder / subfield / subfieldorder
921 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
922 $dbh->do("delete from marc_subfield_table where bibid='$bibid' and
923 tag='$tag' and tagorder='$tagorder'
924 and subfieldcode='$subfield' and subfieldorder='$subfieldorder
928 sub MARCkoha2marcBiblio {
929 # this function builds partial MARC::Record from the old koha-DB fields
930 my ($dbh,$biblionumber,$biblioitemnumber) = @_;
931 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
932 my $record = MARC::Record->new();
933 #--- if bibid, then retrieve old-style koha data
934 if ($biblionumber>0) {
935 my $sth2=$dbh->prepare("select biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp
936 from biblio where biblionumber=?");
937 $sth2->execute($biblionumber);
938 my $row=$sth2->fetchrow_hashref;
940 foreach $code (keys %$row) {
942 &MARCkoha2marcOnefield($sth,$record,"biblio.".$code,$row->{$code});
946 #--- if biblioitem, then retrieve old-style koha data
947 if ($biblioitemnumber>0) {
948 my $sth2=$dbh->prepare(" SELECT biblioitemnumber,biblionumber,volume,number,classification,
949 itemtype,url,isbn,issn,dewey,subclass,publicationyear,publishercode,
950 volumedate,volumeddesc,timestamp,illus,pages,notes,size,place
952 WHERE biblionumber=? and biblioitemnumber=?
954 $sth2->execute($biblionumber,$biblioitemnumber);
955 my $row=$sth2->fetchrow_hashref;
957 foreach $code (keys %$row) {
959 &MARCkoha2marcOnefield($sth,$record,"biblioitems.".$code,$row->{$code});
963 # other fields => additional authors, subjects, subtitles
964 my $sth2=$dbh->prepare(" SELECT author FROM additionalauthors WHERE biblionumber=?");
965 $sth2->execute($biblionumber);
966 while (my $row=$sth2->fetchrow_hashref) {
967 &MARCkoha2marcOnefield($sth,$record,"additionalauthors.author",$row->{'author'});
969 my $sth2=$dbh->prepare(" SELECT subject FROM bibliosubject WHERE biblionumber=?");
970 $sth2->execute($biblionumber);
971 while (my $row=$sth2->fetchrow_hashref) {
972 &MARCkoha2marcOnefield($sth,$record,"bibliosubject.subject",$row->{'subject'});
974 my $sth2=$dbh->prepare(" SELECT subtitle FROM bibliosubtitle WHERE biblionumber=?");
975 $sth2->execute($biblionumber);
976 while (my $row=$sth2->fetchrow_hashref) {
977 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.title",$row->{'subtitle'});
982 sub MARCkoha2marcItem {
983 # this function builds partial MARC::Record from the old koha-DB fields
984 my ($dbh,$biblionumber,$itemnumber) = @_;
985 # my $dbh=&C4Connect;
986 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
987 my $record = MARC::Record->new();
988 #--- if item, then retrieve old-style koha data
990 # print STDERR "prepare $biblionumber,$itemnumber\n";
991 my $sth2=$dbh->prepare("SELECT itemnumber,biblionumber,multivolumepart,biblioitemnumber,barcode,dateaccessioned,
992 booksellerid,homebranch,price,replacementprice,replacementpricedate,datelastborrowed,
993 datelastseen,multivolume,stack,notforloan,itemlost,wthdrawn,bulk,issues,renewals,
994 reserves,restricted,binding,itemnotes,holdingbranch,timestamp
996 WHERE itemnumber=?");
997 $sth2->execute($itemnumber);
998 my $row=$sth2->fetchrow_hashref;
1000 foreach $code (keys %$row) {
1001 if ($row->{$code}) {
1002 &MARCkoha2marcOnefield($sth,$record,"items.".$code,$row->{$code});
1009 sub MARCkoha2marcSubtitle {
1010 # this function builds partial MARC::Record from the old koha-DB fields
1011 my ($dbh,$bibnum,$subtitle) = @_;
1012 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1013 my $record = MARC::Record->new();
1014 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.subtitle",$subtitle);
1018 sub MARCkoha2marcOnefield {
1019 my ($sth,$record,$kohafieldname,$value)=@_;
1022 $sth->execute($kohafieldname);
1023 if (($tagfield,$tagsubfield)=$sth->fetchrow) {
1024 if ($record->field($tagfield)) {
1025 my $tag =$record->field($tagfield);
1027 $tag->add_subfields($tagsubfield,$value);
1028 $record->delete_field($tag);
1029 $record->add_fields($tag);
1032 $record->add_fields($tagfield," "," ",$tagsubfield => $value);
1039 my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
1041 my $record = MARC::Record->new();
1042 # my %subfieldlist=();
1043 my $prevvalue; # if tag <10
1044 my $field; # if tag >=10
1045 for (my $i=0; $i< @$rtags; $i++) {
1046 # rebuild MARC::Record
1047 if (@$rtags[$i] ne $prevtag) {
1048 if ($prevtag < 10) {
1050 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
1054 $record->add_fields($field);
1057 $indicators{@$rtags[$i]}.=' ';
1058 if (@$rtags[$i] <10) {
1059 $prevvalue= @$rvalues[$i];
1061 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
1063 $prevtag = @$rtags[$i];
1065 if (@$rtags[$i] <10) {
1066 $prevvalue=@$rvalues[$i];
1068 if (@$rvalues[$i]) {
1069 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
1072 $prevtag= @$rtags[$i];
1075 # the last has not been included inside the loop... do it now !
1076 $record->add_fields($field);
1077 # warn $record->as_formatted;
1082 my ($dbh,$record) = @_;
1083 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1085 my $sth2=$dbh->prepare("SHOW COLUMNS from biblio");
1088 # print STDERR $record->as_formatted;
1089 while (($field)=$sth2->fetchrow) {
1090 $result=&MARCmarc2kohaOneField($sth,"biblio",$field,$record,$result);
1092 $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
1094 while (($field)=$sth2->fetchrow) {
1095 $result=&MARCmarc2kohaOneField($sth,"biblioitems",$field,$record,$result);
1097 $sth2=$dbh->prepare("SHOW COLUMNS from items");
1099 while (($field)=$sth2->fetchrow) {
1100 $result = &MARCmarc2kohaOneField($sth,"items",$field,$record,$result);
1102 # additional authors : specific
1103 $result = &MARCmarc2kohaOneField($sth,"additionalauthors","additionalauthors",$record,$result);
1104 # modify copyrightdate to keep only the 1st year found
1105 my $temp = $result->{'copyrightdate'};
1106 $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
1108 $result->{'copyrightdate'} = $1;
1109 } else { # if no cYYYY, get the 1st date.
1110 $temp =~ m/(\d\d\d\d)/;
1111 $result->{'copyrightdate'} = $1;
1116 sub MARCmarc2kohaOneField {
1117 # FIXME ? if a field has a repeatable subfield that is used in old-db, only the 1st will be retrieved...
1118 my ($sth,$kohatable,$kohafield,$record,$result)= @_;
1119 # warn "kohatable / $kohafield / $result / ";
1123 $sth->execute($kohatable.".".$kohafield);
1124 ($tagfield,$subfield) = $sth->fetchrow;
1125 foreach my $field ($record->field($tagfield)) {
1126 if ($field->subfield($subfield)) {
1127 if ($result->{$kohafield}) {
1128 $result->{$kohafield} .= " | ".$field->subfield($subfield);
1130 $result->{$kohafield}=$field->subfield($subfield);
1138 # split a subfield string and adds it into the word table.
1140 my ($dbh,$bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$sentence) =@_;
1141 $sentence =~ s/(\.|\?|\:|\!|\'|,|\-)/ /g;
1142 my @words = split / /,$sentence;
1143 my $stopwords= C4::Context->stopwords;
1144 my $sth=$dbh->prepare("insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word)
1145 values (?,?,?,?,?,?,soundex(?))");
1146 foreach my $word (@words) {
1147 # we record only words longer than 2 car and not in stopwords hash
1148 if (length($word)>1 and !($stopwords->{uc($word)})) {
1149 $sth->execute($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,$word);
1151 warn "ERROR ==> insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word) values ($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,soundex($word))\n";
1158 # delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
1159 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
1160 my $sth=$dbh->prepare("delete from marc_word where bibid=? and tag=? and tagorder=? and subfieldid=? and subfieldorder=?");
1161 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
1166 # NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
1169 # all the following subs are useful to manage MARC-DB with complete MARC records.
1170 # it's used with marcimport, and marc management tools
1174 =item ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbibilio($dbh,$MARCRecord,$oldbiblio,$oldbiblioitem);
1176 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
1177 are builded from the MARC::Record. If they are passed, they are used.
1179 =item NEWnewitem($dbh, $record,$bibid);
1181 adds an item in the db.
1186 my ($dbh, $record, $oldbiblio, $oldbiblioitem) = @_;
1187 # note $oldbiblio and $oldbiblioitem are not mandatory.
1188 # if not present, they will be builded from $record with MARCmarc2koha function
1189 if (($oldbiblio) and not($oldbiblioitem)) {
1190 print STDERR "NEWnewbiblio : missing parameter\n";
1191 print "NEWnewbiblio : missing parameter : contact koha development team\n";
1197 $oldbibnum = OLDnewbiblio($dbh,$oldbiblio);
1198 $oldbiblioitem->{'biblionumber'} = $oldbibnum;
1199 $oldbibitemnum = OLDnewbiblioitem($dbh,$oldbiblioitem);
1201 my $olddata = MARCmarc2koha($dbh,$record);
1202 $oldbibnum = OLDnewbiblio($dbh,$olddata);
1203 $olddata->{'biblionumber'} = $oldbibnum;
1204 $oldbibitemnum = OLDnewbiblioitem($dbh,$olddata);
1206 # search subtiles, addiauthors and subjects
1207 my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author");
1208 my @addiauthfields = $record->field($tagfield);
1209 foreach my $addiauthfield (@addiauthfields) {
1210 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1211 foreach my $subfieldcount (0..$#addiauthsubfields) {
1212 OLDmodaddauthor($dbh,$oldbibnum,$addiauthsubfields[$subfieldcount]);
1215 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle");
1216 my @subtitlefields = $record->field($tagfield);
1217 foreach my $subtitlefield (@subtitlefields) {
1218 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1219 foreach my $subfieldcount (0..$#subtitlesubfields) {
1220 OLDnewsubtitle($dbh,$oldbibnum,$subtitlesubfields[$subfieldcount]);
1223 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject");
1224 my @subj = $record->field($tagfield);
1225 foreach my $subject (@subj) {
1226 my @subjsubfield = $subject->subfield($tagsubfield);
1228 foreach my $subfieldcount (0..$#subjsubfield) {
1229 push @subjects,$subjsubfield[$subfieldcount];
1231 OLDmodsubject($dbh,$oldbibnum,1,@subjects);
1233 # we must add bibnum and bibitemnum in MARC::Record...
1234 # we build the new field with biblionumber and biblioitemnumber
1235 # we drop the original field
1236 # we add the new builded field.
1237 # NOTE : Works only if the field is ONLY for biblionumber and biblioitemnumber
1238 # (steve and paul : thinks 090 is a good choice)
1239 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1240 $sth->execute("biblio.biblionumber");
1241 (my $tagfield1, my $tagsubfield1) = $sth->fetchrow;
1242 $sth->execute("biblioitems.biblioitemnumber");
1243 (my $tagfield2, my $tagsubfield2) = $sth->fetchrow;
1244 if ($tagfield1 != $tagfield2) {
1245 warn "Error in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1246 print "Content-Type: text/html\n\nError in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1249 my $newfield = MARC::Field->new( $tagfield1,'','',
1250 "$tagsubfield1" => $oldbibnum,
1251 "$tagsubfield2" => $oldbibitemnum);
1252 # drop old field and create new one...
1253 my $old_field = $record->field($tagfield1);
1254 $record->delete_field($old_field);
1255 $record->add_fields($newfield);
1256 my $bibid = MARCaddbiblio($dbh,$record,$oldbibnum);
1257 return ($bibid,$oldbibnum,$oldbibitemnum );
1261 my ($dbh,$record,$bibid) =@_;
1262 &MARCmodbiblio($dbh,$bibid,$record,0);
1263 my $oldbiblio = MARCmarc2koha($dbh,$record);
1264 my $oldbiblionumber = OLDmodbiblio($dbh,$oldbiblio);
1265 OLDmodbibitem($dbh,$oldbiblio);
1266 # now, modify addi authors, subject, addititles.
1267 my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author");
1268 my @addiauthfields = $record->field($tagfield);
1269 foreach my $addiauthfield (@addiauthfields) {
1270 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1271 foreach my $subfieldcount (0..$#addiauthsubfields) {
1272 OLDmodaddauthor($dbh,$oldbiblionumber,$addiauthsubfields[$subfieldcount]);
1275 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle");
1276 my @subtitlefields = $record->field($tagfield);
1277 foreach my $subtitlefield (@subtitlefields) {
1278 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1279 foreach my $subfieldcount (0..$#subtitlesubfields) {
1280 OLDmodsubtitle($dbh,$oldbiblionumber,$subtitlesubfields[$subfieldcount]);
1283 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject");
1284 my @subj = $record->field($tagfield);
1285 foreach my $subject (@subj) {
1286 my @subjsubfield = $subject->subfield($tagsubfield);
1288 foreach my $subfieldcount (0..$#subjsubfield) {
1289 push @subjects,$subjsubfield[$subfieldcount];
1291 OLDmodsubject($dbh,$oldbiblionumber,1,@subjects);
1298 my ($dbh, $record,$bibid) = @_;
1299 # add item in old-DB
1300 my $item = &MARCmarc2koha($dbh,$record);
1301 # needs old biblionumber and biblioitemnumber
1302 $item->{'biblionumber'} = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
1303 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
1304 $sth->execute($item->{'biblionumber'});
1305 ($item->{'biblioitemnumber'}) = $sth->fetchrow;
1306 my ($itemnumber,$error) = &OLDnewitems($dbh,$item,$item->{barcode});
1307 # add itemnumber to MARC::Record before adding the item.
1308 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1309 &MARCkoha2marcOnefield($sth,$record,"items.itemnumber",$itemnumber);
1311 my $bib = &MARCadditem($dbh,$record,$item->{'biblionumber'});
1315 my ($dbh,$record,$bibid,$itemnumber,$delete) = @_;
1316 &MARCmoditem($dbh,$record,$bibid,$itemnumber,$delete);
1317 my $olditem = MARCmarc2koha($dbh,$record);
1318 OLDmoditem($dbh,$olditem);
1323 # OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
1327 =item $biblionumber = OLDnewbiblio($dbh,$biblio);
1329 adds a record in biblio table. Datas are in the hash $biblio.
1331 =item $biblionumber = OLDmodbiblio($dbh,$biblio);
1333 modify a record in biblio table. Datas are in the hash $biblio.
1335 =item OLDmodsubtitle($dbh,$bibnum,$subtitle);
1337 modify subtitles in bibliosubtitle table.
1339 =item OLDmodaddauthor($dbh,$bibnum,$author);
1341 adds or modify additional authors
1342 NOTE : Strange sub : seems to delete MANY and add only ONE author... maybe buggy ?
1344 =item $errors = OLDmodsubject($dbh,$bibnum, $force, @subject);
1346 modify/adds subjects
1348 =item OLDmodbibitem($dbh, $biblioitem);
1352 =item OLDmodnote($dbh,$bibitemnum,$note
1354 modify a note for a biblioitem
1356 =item OLDnewbiblioitem($dbh,$biblioitem);
1358 adds a biblioitem ($biblioitem is a hash with the values)
1360 =item OLDnewsubject($dbh,$bibnum);
1364 =item OLDnewsubtitle($dbh,$bibnum,$subtitle);
1366 create a new subtitle
1368 =item ($itemnumber,$errors)= OLDnewitems($dbh,$item,$barcode);
1370 create a item. $item is a hash and $barcode the barcode.
1372 =item OLDmoditem($dbh,$item);
1376 =item OLDdelitem($dbh,$itemnum);
1380 =item OLDdeletebiblioitem($dbh,$biblioitemnumber);
1382 deletes a biblioitem
1383 NOTE : not standard sub name. Should be OLDdelbiblioitem()
1385 =item OLDdelbiblio($dbh,$biblio);
1392 my ($dbh,$biblio) = @_;
1393 # my $dbh = &C4Connect;
1394 my $query = "Select max(biblionumber) from biblio";
1395 my $sth = $dbh->prepare($query);
1397 my $data = $sth->fetchrow_arrayref;
1398 my $bibnum = $$data[0] + 1;
1401 if ($biblio->{'seriestitle'}) { $series = 1 };
1403 $query = "insert into biblio set biblionumber = ?, title = ?, author = ?, copyrightdate = ?,
1404 serial = ?, seriestitle = ?, notes = ?, abstract = ?";
1405 $sth = $dbh->prepare($query);
1406 $sth->execute($bibnum,$biblio->{'title'},$biblio->{'author'},$biblio->{'copyright'},$series,$biblio->{'seriestitle'},$biblio->{'notes'},$biblio->{'abstract'});
1414 my ($dbh,$biblio) = @_;
1415 # my $dbh = C4Connect;
1419 $query = "Update biblio set title = ?, author = ?, abstract = ?, copyrightdate = ?,
1420 seriestitle = ?, serial = ?, unititle = ?, notes = ? where biblionumber = ?";
1421 $sth = $dbh->prepare($query);
1422 $sth->execute($biblio->{'title'},$biblio->{'author'},$biblio->{'abstract'},$biblio->{'copyrightdate'},
1423 $biblio->{'seriestitle'},$biblio->{'serial'},$biblio->{'unititle'},$biblio->{'notes'},$biblio->{'biblionumber'});
1426 return($biblio->{'biblionumber'});
1429 sub OLDmodsubtitle {
1430 my ($dbh,$bibnum, $subtitle) = @_;
1431 my $query = "update bibliosubtitle set subtitle = ? where biblionumber = ?";
1432 my $sth = $dbh->prepare($query);
1433 $sth->execute($subtitle,$bibnum);
1438 sub OLDmodaddauthor {
1439 my ($dbh,$bibnum, $author) = @_;
1440 # my $dbh = C4Connect;
1441 my $query = "Delete from additionalauthors where biblionumber = $bibnum";
1442 my $sth = $dbh->prepare($query);
1447 if ($author ne '') {
1448 $query = "Insert into additionalauthors set
1451 $sth = $dbh->prepare($query);
1453 $sth->execute($author,$bibnum);
1457 } # sub modaddauthor
1461 my ($dbh,$bibnum, $force, @subject) = @_;
1462 # my $dbh = C4Connect;
1463 my $count = @subject;
1465 for (my $i = 0; $i < $count; $i++) {
1466 $subject[$i] =~ s/^ //g;
1467 $subject[$i] =~ s/ $//g;
1468 my $query = "select * from catalogueentry where entrytype = 's' and catalogueentry = '$subject[$i]'";
1469 my $sth = $dbh->prepare($query);
1472 if (my $data = $sth->fetchrow_hashref) {
1474 if ($force eq $subject[$i] || $force eq 1) {
1475 # subject not in aut, chosen to force anway
1476 # so insert into cataloguentry so its in auth file
1477 $query = "Insert into catalogueentry (entrytype,catalogueentry) values ('s','$subject[$i]')";
1478 my $sth2 = $dbh->prepare($query);
1483 $error = "$subject[$i]\n does not exist in the subject authority file";
1484 $query = "Select * from catalogueentry where entrytype = 's' and (catalogueentry like '$subject[$i] %'
1485 or catalogueentry like '% $subject[$i] %' or catalogueentry like '% $subject[$i]')";
1486 my $sth2 = $dbh->prepare($query);
1488 while (my $data = $sth2->fetchrow_hashref) {
1489 $error .= "<br>$data->{'catalogueentry'}";
1497 my $query = "Delete from bibliosubject where biblionumber = $bibnum";
1498 my $sth = $dbh->prepare($query);
1501 for (my $i = 0; $i < $count; $i++) {
1502 $sth = $dbh->prepare("Insert into bibliosubject values ('$subject[$i]', $bibnum)");
1513 my ($dbh,$biblioitem) = @_;
1514 # my $dbh = C4Connect;
1517 $biblioitem->{'itemtype'} = $dbh->quote($biblioitem->{'itemtype'});
1518 $biblioitem->{'url'} = $dbh->quote($biblioitem->{'url'});
1519 $biblioitem->{'isbn'} = $dbh->quote($biblioitem->{'isbn'});
1520 $biblioitem->{'publishercode'} = $dbh->quote($biblioitem->{'publishercode'});
1521 $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
1522 $biblioitem->{'classification'} = $dbh->quote($biblioitem->{'classification'});
1523 $biblioitem->{'dewey'} = $dbh->quote($biblioitem->{'dewey'});
1524 $biblioitem->{'subclass'} = $dbh->quote($biblioitem->{'subclass'});
1525 $biblioitem->{'illus'} = $dbh->quote($biblioitem->{'illus'});
1526 $biblioitem->{'pages'} = $dbh->quote($biblioitem->{'pages'});
1527 $biblioitem->{'volumeddesc'} = $dbh->quote($biblioitem->{'volumeddesc'});
1528 $biblioitem->{'notes'} = $dbh->quote($biblioitem->{'notes'});
1529 $biblioitem->{'size'} = $dbh->quote($biblioitem->{'size'});
1530 $biblioitem->{'place'} = $dbh->quote($biblioitem->{'place'});
1532 $query = "Update biblioitems set
1533 itemtype = $biblioitem->{'itemtype'},
1534 url = $biblioitem->{'url'},
1535 isbn = $biblioitem->{'isbn'},
1536 publishercode = $biblioitem->{'publishercode'},
1537 publicationyear = $biblioitem->{'publicationyear'},
1538 classification = $biblioitem->{'classification'},
1539 dewey = $biblioitem->{'dewey'},
1540 subclass = $biblioitem->{'subclass'},
1541 illus = $biblioitem->{'illus'},
1542 pages = $biblioitem->{'pages'},
1543 volumeddesc = $biblioitem->{'volumeddesc'},
1544 notes = $biblioitem->{'notes'},
1545 size = $biblioitem->{'size'},
1546 place = $biblioitem->{'place'}
1547 where biblioitemnumber = $biblioitem->{'biblioitemnumber'}";
1555 my ($dbh,$bibitemnum,$note)=@_;
1556 # my $dbh=C4Connect;
1557 my $query="update biblioitems set notes='$note' where
1558 biblioitemnumber='$bibitemnum'";
1559 my $sth=$dbh->prepare($query);
1565 sub OLDnewbiblioitem {
1566 my ($dbh,$biblioitem) = @_;
1567 # my $dbh = C4Connect;
1568 my $query = "Select max(biblioitemnumber) from biblioitems";
1569 my $sth = $dbh->prepare($query);
1574 $data = $sth->fetchrow_arrayref;
1575 $bibitemnum = $$data[0] + 1;
1579 $sth = $dbh->prepare("insert into biblioitems set
1580 biblioitemnumber = ?, biblionumber = ?,
1581 volume = ?, number = ?,
1582 classification = ?, itemtype = ?,
1584 issn = ?, dewey = ?,
1585 subclass = ?, publicationyear = ?,
1586 publishercode = ?, volumedate = ?,
1587 volumeddesc = ?, illus = ?,
1588 pages = ?, notes = ?,
1590 marc = ?, place = ?");
1591 $sth->execute($bibitemnum, $biblioitem->{'biblionumber'},
1592 $biblioitem->{'volume'}, $biblioitem->{'number'},
1593 $biblioitem->{'classification'}, $biblioitem->{'itemtype'},
1594 $biblioitem->{'url'}, $biblioitem->{'isbn'},
1595 $biblioitem->{'issn'}, $biblioitem->{'dewey'},
1596 $biblioitem->{'subclass'}, $biblioitem->{'publicationyear'},
1597 $biblioitem->{'publishercode'}, $biblioitem->{'volumedate'},
1598 $biblioitem->{'volumeddesc'}, $biblioitem->{'illus'},
1599 $biblioitem->{'pages'}, $biblioitem->{'notes'},
1600 $biblioitem->{'size'}, $biblioitem->{'lccn'},
1601 $biblioitem->{'marc'}, $biblioitem->{'place'});
1604 return($bibitemnum);
1608 my ($dbh,$bibnum)=@_;
1609 my $query="insert into bibliosubject (biblionumber) values ($bibnum)";
1610 my $sth=$dbh->prepare($query);
1615 sub OLDnewsubtitle {
1616 my ($dbh,$bibnum, $subtitle) = @_;
1617 my $query = "insert into bibliosubtitle set biblionumber = ?, subtitle = ?";
1618 my $sth = $dbh->prepare($query);
1619 $sth->execute($bibnum,$subtitle);
1625 my ($dbh,$item, $barcode) = @_;
1626 # my $dbh = C4Connect;
1627 my $query = "Select max(itemnumber) from items";
1628 my $sth = $dbh->prepare($query);
1634 $data = $sth->fetchrow_hashref;
1635 $itemnumber = $data->{'max(itemnumber)'} + 1;
1637 # if dateaccessioned is provided, use it. Otherwise, set to NOW()
1638 if ($item->{'dateaccessioned'}) {
1639 $sth=$dbh->prepare("Insert into items set
1640 itemnumber = ?, biblionumber = ?,
1641 biblioitemnumber = ?, barcode = ?,
1642 booksellerid = ?, dateaccessioned = ?,
1643 homebranch = ?, holdingbranch = ?,
1644 price = ?, replacementprice = ?,
1645 replacementpricedate = NOW(), itemnotes = ?,
1648 $sth->execute($itemnumber, $item->{'biblionumber'},
1649 $item->{'biblioitemnumber'},$barcode,
1650 $item->{'booksellerid'},$item->{'dateaccessioned'},
1651 $item->{'homebranch'},$item->{'holdingbranch'},
1652 $item->{'price'},$item->{'replacementprice'},
1653 $item->{'itemnotes'},$item->{'loan'});
1655 $sth=$dbh->prepare("Insert into items set
1656 itemnumber = ?, biblionumber = ?,
1657 biblioitemnumber = ?, barcode = ?,
1658 booksellerid = ?, dateaccessioned = NOW(),
1659 homebranch = ?, holdingbranch = ?,
1660 price = ?, replacementprice = ?,
1661 replacementpricedate = NOW(), itemnotes = ?,
1664 $sth->execute($itemnumber, $item->{'biblionumber'},
1665 $item->{'biblioitemnumber'},$barcode,
1666 $item->{'booksellerid'},
1667 $item->{'homebranch'},$item->{'holdingbranch'},
1668 $item->{'price'},$item->{'replacementprice'},
1669 $item->{'itemnotes'},$item->{'loan'});
1671 if (defined $sth->errstr) {
1672 $error .= $sth->errstr;
1675 return($itemnumber,$error);
1679 my ($dbh,$item) = @_;
1680 # my ($dbh,$loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
1681 # my $dbh=C4Connect;
1682 $item->{'itemnum'}=$item->{'itemnumber'} unless $item->{'itemnum'};
1683 my $query="update items set barcode='$item->{'barcode'}',itemnotes='$item->{'notes'}'
1684 where itemnumber=$item->{'itemnum'}";
1685 if ($item->{'barcode'} eq ''){
1686 $query="update items set notforloan=$item->{'loan'} where itemnumber=$item->{'itemnum'}";
1688 if ($item->{'lost'} ne ''){
1689 $query="update items set biblioitemnumber=$item->{'bibitemnum'},
1690 barcode='$item->{'barcode'}',
1691 itemnotes='$item->{'notes'}',
1692 homebranch='$item->{'homebranch'}',
1693 itemlost='$item->{'lost'}',
1694 wthdrawn='$item->{'wthdrawn'}'
1695 where itemnumber=$item->{'itemnum'}";
1697 if ($item->{'replacement'} ne ''){
1698 $query=~ s/ where/,replacementprice='$item->{'replacement'}' where/;
1700 my $sth=$dbh->prepare($query);
1707 my ($dbh,$itemnum)=@_;
1708 # my $dbh=C4Connect;
1709 my $query="select * from items where itemnumber=$itemnum";
1710 my $sth=$dbh->prepare($query);
1712 my @data=$sth->fetchrow_array;
1714 $query="Insert into deleteditems values (";
1715 foreach my $temp (@data){
1716 $query .= "'$temp',";
1720 $sth=$dbh->prepare($query);
1723 $query = "Delete from items where itemnumber=$itemnum";
1724 $sth=$dbh->prepare($query);
1730 sub OLDdeletebiblioitem {
1731 my ($dbh,$biblioitemnumber) = @_;
1732 # my $dbh = C4Connect;
1733 my $query = "Select * from biblioitems
1734 where biblioitemnumber = $biblioitemnumber";
1735 my $sth = $dbh->prepare($query);
1740 if (@results = $sth->fetchrow_array) {
1741 $query = "Insert into deletedbiblioitems values (";
1742 foreach my $value (@results) {
1743 $value = $dbh->quote($value);
1744 $query .= "$value,";
1747 $query =~ s/\,$/\)/;
1750 $query = "Delete from biblioitems
1751 where biblioitemnumber = $biblioitemnumber";
1755 # Now delete all the items attached to the biblioitem
1756 $query = "Select * from items where biblioitemnumber = $biblioitemnumber";
1757 $sth = $dbh->prepare($query);
1759 while (@results = $sth->fetchrow_array) {
1760 $query = "Insert into deleteditems values (";
1761 foreach my $value (@results) {
1762 $value = $dbh->quote($value);
1763 $query .= "$value,";
1765 $query =~ s/\,$/\)/;
1769 $query = "Delete from items where biblioitemnumber = $biblioitemnumber";
1772 } # sub deletebiblioitem
1775 my ($dbh,$biblio)=@_;
1776 # my $dbh=C4Connect;
1777 my $query="select * from biblio where biblionumber=$biblio";
1778 my $sth=$dbh->prepare($query);
1780 if (my @data=$sth->fetchrow_array){
1782 $query="Insert into deletedbiblio values (";
1783 foreach my $temp (@data){
1784 $temp=~ s/\'/\\\'/g;
1785 $query .= "'$temp',";
1789 $sth=$dbh->prepare($query);
1792 $query = "Delete from biblio where biblionumber=$biblio";
1793 $sth=$dbh->prepare($query);
1809 my $dbh = C4::Context->dbh;
1810 my $query="Select count(*) from items where biblionumber=$biblio";
1812 my $sth=$dbh->prepare($query);
1814 my $data=$sth->fetchrow_hashref;
1816 return($data->{'count(*)'});
1821 ($order, $ordernumber) = &getorder($biblioitemnumber, $biblionumber);
1823 Looks up the order with the given biblionumber and biblioitemnumber.
1825 Returns a two-element array. C<$ordernumber> is the order number.
1826 C<$order> is a reference-to-hash describing the order; its keys are
1827 fields from the biblio, biblioitems, aqorders, and aqorderbreakdown
1828 tables of the Koha database.
1832 # FIXME - This is effectively identical to &C4::Catalogue::getorder.
1833 # Pick one and stick with it.
1836 my $dbh = C4::Context->dbh;
1837 my $query="Select ordernumber
1839 where biblionumber=? and biblioitemnumber=?";
1840 my $sth=$dbh->prepare($query);
1841 $sth->execute($bib,$bi);
1842 # FIXME - Use fetchrow_array(), since we're only interested in the one
1844 my $ordnum=$sth->fetchrow_hashref;
1846 my $order=getsingleorder($ordnum->{'ordernumber'});
1848 return ($order,$ordnum->{'ordernumber'});
1851 =item getsingleorder
1853 $order = &getsingleorder($ordernumber);
1855 Looks up an order by order number.
1857 Returns a reference-to-hash describing the order. The keys of
1858 C<$order> are fields from the biblio, biblioitems, aqorders, and
1859 aqorderbreakdown tables of the Koha database.
1863 # FIXME - This is effectively identical to
1864 # &C4::Catalogue::getsingleorder.
1865 # Pick one and stick with it.
1866 sub getsingleorder {
1868 my $dbh = C4::Context->dbh;
1869 my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown
1870 where aqorders.ordernumber=?
1871 and biblio.biblionumber=aqorders.biblionumber and
1872 biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
1873 aqorders.ordernumber=aqorderbreakdown.ordernumber";
1874 my $sth=$dbh->prepare($query);
1875 $sth->execute($ordnum);
1876 my $data=$sth->fetchrow_hashref;
1883 my $dbh = C4::Context->dbh;
1884 my $bibnum=OLDnewbiblio($dbh,$biblio);
1885 # finds new (MARC bibid
1886 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$bibnum);
1887 my $record = &MARCkoha2marcBiblio($dbh,$bibnum);
1888 MARCaddbiblio($dbh,$record,$bibnum);
1895 $biblionumber = &modbiblio($biblio);
1897 Update a biblio record.
1899 C<$biblio> is a reference-to-hash whose keys are the fields in the
1900 biblio table in the Koha database. All fields must be present, not
1901 just the ones you wish to change.
1903 C<&modbiblio> updates the record defined by
1904 C<$biblio-E<gt>{biblionumber}> with the values in C<$biblio>.
1906 C<&modbiblio> returns C<$biblio-E<gt>{biblionumber}> whether it was
1913 my $dbh = C4::Context->dbh;
1914 my $biblionumber=OLDmodbiblio($dbh,$biblio);
1915 my $record = MARCkoha2marcBiblio($dbh,$biblionumber);
1916 # finds new (MARC bibid
1917 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
1918 MARCmodbiblio($dbh,$bibid,$record,0);
1919 return($biblionumber);
1924 &modsubtitle($biblionumber, $subtitle);
1926 Sets the subtitle of a book.
1928 C<$biblionumber> is the biblionumber of the book to modify.
1930 C<$subtitle> is the new subtitle.
1935 my ($bibnum, $subtitle) = @_;
1936 my $dbh = C4::Context->dbh;
1937 &OLDmodsubtitle($dbh,$bibnum,$subtitle);
1942 &modaddauthor($biblionumber, $author);
1944 Replaces all additional authors for the book with biblio number
1945 C<$biblionumber> with C<$author>. If C<$author> is the empty string,
1946 C<&modaddauthor> deletes all additional authors.
1951 my ($bibnum, $author) = @_;
1952 my $dbh = C4::Context->dbh;
1953 &OLDmodaddauthor($dbh,$bibnum,$author);
1954 } # sub modaddauthor
1958 $error = &modsubject($biblionumber, $force, @subjects);
1960 $force - a subject to force
1962 $error - Error message, or undef if successful.
1967 my ($bibnum, $force, @subject) = @_;
1968 my $dbh = C4::Context->dbh;
1969 my $error= &OLDmodsubject($dbh,$bibnum,$force, @subject);
1974 my ($biblioitem) = @_;
1975 my $dbh = C4::Context->dbh;
1976 &OLDmodbibitem($dbh,$biblioitem);
1977 my $MARCbibitem = MARCkoha2marcBiblio($dbh,$biblioitem);
1978 &MARCmodbiblio($dbh,$biblioitem->{biblionumber},$MARCbibitem,0);
1982 my ($bibitemnum,$note)=@_;
1983 my $dbh = C4::Context->dbh;
1984 &OLDmodnote($dbh,$bibitemnum,$note);
1988 my ($biblioitem) = @_;
1989 my $dbh = C4::Context->dbh;
1990 my $bibitemnum = &OLDnewbiblioitem($dbh,$biblioitem);
1991 # print STDERR "bibitemnum : $bibitemnum\n";
1992 my $MARCbiblio= MARCkoha2marcBiblio($dbh,$biblioitem->{biblionumber},$bibitemnum);
1993 # print STDERR $MARCbiblio->as_formatted();
1994 &MARCaddbiblio($dbh,$MARCbiblio,$biblioitem->{biblionumber});
1995 return($bibitemnum);
2000 my $dbh = C4::Context->dbh;
2001 &OLDnewsubject($dbh,$bibnum);
2005 my ($bibnum, $subtitle) = @_;
2006 my $dbh = C4::Context->dbh;
2007 &OLDnewsubtitle($dbh,$bibnum,$subtitle);
2011 my ($item, @barcodes) = @_;
2012 my $dbh = C4::Context->dbh;
2016 foreach my $barcode (@barcodes) {
2017 ($itemnumber,$error)=&OLDnewitems($dbh,$item,uc($barcode));
2019 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{biblionumber},$itemnumber);
2020 &MARCadditem($dbh,$MARCitem,$item->{biblionumber});
2027 my $dbh = C4::Context->dbh;
2028 &OLDmoditem($dbh,$item);
2029 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{'biblionumber'},$item->{'itemnum'});
2030 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$item->{biblionumber});
2031 &MARCmoditem($dbh,$MARCitem,$bibid,$item->{itemnum},0);
2035 my ($count,@barcodes)=@_;
2036 my $dbh = C4::Context->dbh;
2038 for (my $i=0;$i<$count;$i++){
2039 $barcodes[$i]=uc $barcodes[$i];
2040 my $query="Select * from items where barcode='$barcodes[$i]'";
2041 my $sth=$dbh->prepare($query);
2043 if (my $data=$sth->fetchrow_hashref){
2044 $error.=" Duplicate Barcode: $barcodes[$i]";
2052 my ($bibitemnum)=@_;
2053 my $dbh = C4::Context->dbh;
2054 my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
2055 my $sth=$dbh->prepare($query);
2057 my $data=$sth->fetchrow_hashref;
2059 return($data->{'count(*)'});
2064 my $dbh = C4::Context->dbh;
2065 &OLDdelitem($dbh,$itemnum);
2068 sub deletebiblioitem {
2069 my ($biblioitemnumber) = @_;
2070 my $dbh = C4::Context->dbh;
2071 &OLDdeletebiblioitem($dbh,$biblioitemnumber);
2072 } # sub deletebiblioitem
2077 my $dbh = C4::Context->dbh;
2078 &OLDdelbiblio($dbh,$biblio);
2082 my $dbh = C4::Context->dbh;
2083 my $query = "select * from itemtypes order by description";
2084 my $sth = $dbh->prepare($query);
2085 # || die "Cannot prepare $query" . $dbh->errstr;
2090 # || die "Cannot execute $query\n" . $sth->errstr;
2091 while (my $data = $sth->fetchrow_hashref) {
2092 $results[$count] = $data;
2097 return($count, @results);
2098 } # sub getitemtypes
2101 my ($biblionumber) = @_;
2102 my $dbh = C4::Context->dbh;
2103 my $query = "Select * from biblio where biblionumber = $biblionumber";
2104 my $sth = $dbh->prepare($query);
2105 # || die "Cannot prepare $query\n" . $dbh->errstr;
2110 # || die "Cannot execute $query\n" . $sth->errstr;
2111 while (my $data = $sth->fetchrow_hashref) {
2112 $results[$count] = $data;
2117 return($count, @results);
2121 my ($biblioitemnum) = @_;
2122 my $dbh = C4::Context->dbh;
2123 my $query = "Select * from biblioitems where
2124 biblioitemnumber = $biblioitemnum";
2125 my $sth = $dbh->prepare($query);
2131 while (my $data = $sth->fetchrow_hashref) {
2132 $results[$count] = $data;
2137 return($count, @results);
2138 } # sub getbiblioitem
2140 sub getbiblioitembybiblionumber {
2141 my ($biblionumber) = @_;
2142 my $dbh = C4::Context->dbh;
2143 my $query = "Select * from biblioitems where biblionumber =
2145 my $sth = $dbh->prepare($query);
2151 while (my $data = $sth->fetchrow_hashref) {
2152 $results[$count] = $data;
2157 return($count, @results);
2160 sub getitemsbybiblioitem {
2161 my ($biblioitemnum) = @_;
2162 my $dbh = C4::Context->dbh;
2163 my $query = "Select * from items, biblio where
2164 biblio.biblionumber = items.biblionumber and biblioitemnumber
2166 my $sth = $dbh->prepare($query);
2167 # || die "Cannot prepare $query\n" . $dbh->errstr;
2172 # || die "Cannot execute $query\n" . $sth->errstr;
2173 while (my $data = $sth->fetchrow_hashref) {
2174 $results[$count] = $data;
2179 return($count, @results);
2180 } # sub getitemsbybiblioitem
2184 # Subroutine to log changes to databases
2185 # Eventually, this subroutine will be used to create a log of all changes made,
2186 # with the possibility of "undo"ing some changes
2188 if ($database eq 'kohadb') {
2194 # print STDERR "KOHA: $type $section $item $original $new\n";
2195 } elsif ($database eq 'marc') {
2197 my $Record_ID=shift;
2200 my $subfield_ID=shift;
2203 # print STDERR "MARC: $type $Record_ID $tag $mark $subfield_ID $original $new\n";
2207 #------------------------------------------------
2210 #---------------------------------------
2211 # Find a biblio entry, or create a new one if it doesn't exist.
2212 # If a "subtitle" entry is in hash, add it to subtitle table
2213 sub getoraddbiblio {
2217 # FIXME - Unused argument
2218 $biblio, # hash ref to fields
2229 $dbh = C4::Context->dbh;
2231 print "<PRE>Looking for biblio </PRE>\n" if $debug;
2232 $sth=$dbh->prepare("select biblionumber
2234 where title=? and author=?
2235 and copyrightdate=? and seriestitle=?");
2237 $biblio->{title}, $biblio->{author},
2238 $biblio->{copyright}, $biblio->{seriestitle} );
2240 ($biblionumber) = $sth->fetchrow;
2241 print "<PRE>Biblio exists with number $biblionumber</PRE>\n" if $debug;
2243 # Doesn't exist. Add new one.
2244 print "<PRE>Adding biblio</PRE>\n" if $debug;
2245 ($biblionumber,$error)=&newbiblio($biblio);
2246 if ( $biblionumber ) {
2247 print "<PRE>Added with biblio number=$biblionumber</PRE>\n" if $debug;
2248 if ( $biblio->{subtitle} ) {
2249 &newsubtitle($biblionumber,$biblio->{subtitle} );
2252 print "<PRE>Couldn't add biblio: $error</PRE>\n" if $debug;
2256 return $biblionumber,$error;
2258 } # sub getoraddbiblio
2261 # converts ISO 5426 coded string to ISO 8859-1
2262 # sloppy code : should be improved in next issue
2263 my ($string,$encoding) = @_ ;
2265 # $encoding = C4::Context->preference("marcflavour") unless $encoding;
2266 if ($encoding eq "UNIMARC") {
2328 # this handles non-sorting blocks (if implementation requires this)
2329 $string = nsb_clean($_) ;
2330 } elsif ($encoding eq "USMARC") {
2383 # this handles non-sorting blocks (if implementation requires this)
2384 $string = nsb_clean($_) ;
2391 my $NSB = '\x88' ; # NSB : begin Non Sorting Block
2392 my $NSE = '\x89' ; # NSE : Non Sorting Block end
2393 # handles non sorting blocks
2397 s/[ ]{0,1}$NSE/) /gm ;
2402 END { } # module clean-up code here (global destructor)
2408 Koha Developement team <info@koha.org>
2410 Paul POULAIN paul.poulain@free.fr