4 # Revision 1.52 2003/07/10 10:37:19 tipaul
5 # fix for copyrightdate problem, #514
7 # Revision 1.51 2003/07/02 14:47:17 tipaul
8 # fix for #519 : items.dateaccessioned imports incorrectly
10 # Revision 1.49 2003/06/17 11:21:13 tipaul
11 # improvments/fixes for z3950 support.
12 # * Works now even on ADD, not only on MODIFY
13 # * able to search on ISBN, author, title
15 # Revision 1.48 2003/06/16 09:22:53 rangi
16 # Just added an order clause to getitemtypes
18 # Revision 1.47 2003/05/20 16:22:44 tipaul
19 # fixing typo in Biblio.pm POD
21 # Revision 1.46 2003/05/19 13:45:18 tipaul
22 # support for subtitles, additional authors, subject.
23 # 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.
24 # Note that some OLD-DB subs are strange (dummy ?) see OLDmodsubject, OLDmodsubtitle, OLDmodaddiauthor in C4/Biblio.pm
25 # 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.
27 # Revision 1.45 2003/04/29 16:50:49 tipaul
28 # really proud of this commit :-)
29 # z3950 search and import seems to works fine.
30 # Let me explain how :
31 # * a "search z3950" button is added in the addbiblio template.
32 # * when clicked, a popup appears and z3950/search.pl is called
33 # * z3950/search.pl calls addz3950search in the DB
34 # * the z3950 daemon retrieve the records and stores them in z3950results AND in marc_breeding table.
35 # * as long as there as searches pending, the popup auto refresh every 2 seconds, and says how many searches are pending.
36 # * when the user clicks on a z3950 result => the parent popup is called with the requested biblio, and auto-filled
39 # * 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.
40 # * 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.
42 # Revision 1.44 2003/04/28 13:07:14 tipaul
43 # Those fixes solves the "internal server error" with MARC::Record 1.12.
44 # It was due to an illegal contruction in Koha : we tried to retrive subfields from <10 tags.
45 # That's not possible. MARC::Record accepted this in 0.93 version, but it was fixed after.
46 # Now, the construct/retrieving is OK !
48 # Revision 1.43 2003/04/10 13:56:02 tipaul
50 # * worked in 1.9.0, but not in 1.9.1 :
51 # - modif of a biblio didn't work
52 # - 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.
54 # * did not work before :
55 # - repeatable subfields now works correctly. Enter 2 subfields separated by | and they will be splitted during saving.
56 # - dropped the last subfield of the MARC form :-(
59 # - 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.
60 # Note the MARCdelbiblio has been rewritted to enable deletion of a biblio WITHOUT deleting items.
62 # Revision 1.42 2003/04/04 08:41:11 tipaul
63 # last commits before 1.9.1
65 # Revision 1.41 2003/04/01 12:26:43 tipaul
68 # Revision 1.40 2003/03/11 15:14:03 tipaul
71 # Revision 1.39 2003/03/07 16:35:42 tipaul
72 # * moving generic functions to Koha.pm
73 # * improvement of SearchMarc.pm
77 # Revision 1.38 2003/02/27 16:51:59 tipaul
78 # * moving prepare / execute to ? form.
81 # * road to 1.9.2 => acquisition and cataloguing merging
83 # Revision 1.37 2003/02/12 11:03:03 tipaul
84 # Support for 000 -> 010 fields.
85 # Those fields doesn't have subfields.
86 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
87 # 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.
89 # Revision 1.36 2003/02/12 11:01:01 tipaul
90 # Support for 000 -> 010 fields.
91 # Those fields doesn't have subfields.
92 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
93 # 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.
95 # Revision 1.35 2003/02/03 18:46:00 acli
96 # Minor factoring in C4/Biblio.pm, plus change to export the per-tag
97 # 'mandatory' property to a per-subfield 'tag_mandatory' template parameter,
98 # so that addbiblio.tmpl can distinguish between mandatory subfields in a
99 # mandatory tag and mandatory subfields in an optional tag
101 # Not-minor factoring in acqui.simple/addbiblio.pl to make the if-else blocks
102 # smaller, and to add some POD; need further testing for this
104 # Added function to check if a MARC subfield name is "koha-internal" (instead
105 # of checking it for 'lib' and 'tag' everywhere); temporarily added to Koha.pm
107 # Use above function in acqui.simple/additem.pl and search.marc/search.pl
109 # Revision 1.34 2003/01/28 14:50:04 tipaul
110 # fixing MARCmodbiblio API and reindenting code
112 # Revision 1.33 2003/01/23 12:22:37 tipaul
113 # adding char_decode to decode MARC21 or UNIMARC extended chars
115 # Revision 1.32 2002/12/16 15:08:50 tipaul
116 # small but important bugfix (fixes a problem in export)
118 # Revision 1.31 2002/12/13 16:22:04 tipaul
119 # 1st draft of marc export
121 # Revision 1.30 2002/12/12 21:26:35 tipaul
122 # YAB ! (Yet Another Bugfix) => related to biblio modif
123 # (some warning cleaning too)
125 # Revision 1.29 2002/12/12 16:35:00 tipaul
126 # adding authentification with Auth.pm and
127 # MAJOR BUGFIX on marc biblio modification
129 # Revision 1.28 2002/12/10 13:30:03 tipaul
130 # fugfixes from Dombes Abbey work
132 # Revision 1.27 2002/11/19 12:36:16 tipaul
134 # various bugfixes, improvments, and migration from acquisition.pm to biblio.pm
136 # Revision 1.26 2002/11/12 15:58:43 tipaul
139 # * 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)
141 # Revision 1.25 2002/10/25 10:58:26 tipaul
143 # * bugfixes and improvements
145 # Revision 1.24 2002/10/24 12:09:01 arensb
146 # Fixed "no title" warning when generating HTML documentation from POD.
148 # Revision 1.23 2002/10/16 12:43:08 arensb
149 # Added some FIXME comments.
151 # Revision 1.22 2002/10/15 13:39:17 tipaul
152 # removing Acquisition.pm
153 # deleting unused code in biblio.pm, rewriting POD and answering most FIXME comments
155 # Revision 1.21 2002/10/13 11:34:14 arensb
156 # Replaced expressions of the form "$x = $x <op> $y" with "$x <op>= $y".
157 # Thus, $x = $x+2 becomes $x += 2, and so forth.
159 # Revision 1.20 2002/10/13 08:28:32 arensb
160 # Deleted unused variables.
161 # Removed trailing whitespace.
163 # Revision 1.19 2002/10/13 05:56:10 arensb
164 # Added some FIXME comments.
166 # Revision 1.18 2002/10/11 12:34:53 arensb
167 # Replaced &requireDBI with C4::Context->dbh
169 # Revision 1.17 2002/10/10 14:48:25 tipaul
172 # Revision 1.16 2002/10/07 14:04:26 tipaul
173 # road to 1.3.1 : viewing MARC biblio
175 # Revision 1.15 2002/10/05 09:49:25 arensb
176 # Merged with arensb-context branch: use C4::Context->dbh instead of
177 # &C4Connect, and generally prefer C4::Context over C4::Database.
179 # Revision 1.14 2002/10/03 11:28:18 tipaul
180 # Extending Context.pm to add stopword management and using it in MARC-API.
181 # First benchmarks show a medium speed improvement, which is nice as this part is heavily called.
183 # Revision 1.13 2002/10/02 16:26:44 tipaul
186 # Revision 1.12.2.4 2002/10/05 07:09:31 arensb
187 # Merged in changes from main branch.
189 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
190 # Added a whole mess of FIXME comments.
192 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
193 # Added some missing semicolons.
195 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
196 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
199 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
200 # Added a whole mess of FIXME comments.
202 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
203 # Added some missing semicolons.
205 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
206 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
209 # Revision 1.12 2002/10/01 11:48:51 arensb
210 # Added some FIXME comments, mostly marking duplicate functions.
212 # Revision 1.11 2002/09/24 13:49:26 tipaul
213 # long WAS the road to 1.3.0...
214 # coming VERY SOON NOW...
215 # modifying installer and buildrelease to update the DB
217 # Revision 1.10 2002/09/22 16:50:08 arensb
218 # Added some FIXME comments.
220 # Revision 1.9 2002/09/20 12:57:46 tipaul
221 # long is the road to 1.4.0
222 # * MARCadditem and MARCmoditem now wroks
223 # * various bugfixes in MARC management
224 # !!! 1.3.0 should be released very soon now. Be careful !!!
226 # Revision 1.8 2002/09/10 13:53:52 tipaul
227 # MARC API continued...
229 # * 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)
231 # Note : it should not be hard for marcimport and marcexport to re-link fields from internal tag/subfield to "legal" tag/subfield.
233 # Revision 1.7 2002/08/14 18:12:51 tonnesen
234 # Added copyright statement to all .pl and .pm files
236 # Revision 1.6 2002/07/25 13:40:31 tipaul
237 # pod documenting the API.
239 # Revision 1.5 2002/07/24 16:11:37 tipaul
241 # Database.pm and Output.pm are almost not modified (var test...)
243 # Biblio.pm is almost completly rewritten.
245 # WHAT DOES IT ??? ==> END of Hitchcock suspens
247 # 1st, it does... nothing...
248 # 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 ...
250 # All old-API functions have been cloned. for example, the "newbiblio" sub, now has become :
251 # * a "newbiblio" sub, with the same parameters. It just call a sub named OLDnewbiblio
252 # * 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.
253 # * 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.
254 # 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 ;-)
256 # 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.
257 # Note we have decided with steve that a old-biblio <=> a MARC-Biblio.
261 # Copyright 2000-2002 Katipo Communications
263 # This file is part of Koha.
265 # Koha is free software; you can redistribute it and/or modify it under the
266 # terms of the GNU General Public License as published by the Free Software
267 # Foundation; either version 2 of the License, or (at your option) any later
270 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
271 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
272 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
274 # You should have received a copy of the GNU General Public License along with
275 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
276 # Suite 330, Boston, MA 02111-1307 USA
284 use vars qw($VERSION @ISA @EXPORT);
286 # set the version for version checking
291 # don't forget MARCxxx subs are here only for testing purposes. Should not be used
292 # as the old-style API and the NEW one are the only public functions.
295 &updateBiblio &updateBiblioItem &updateItem
296 &itemcount &newbiblio &newbiblioitem
297 &modnote &newsubject &newsubtitle
298 &modbiblio &checkitems
299 &newitems &modbibitem
300 &modsubtitle &modsubject &modaddauthor &moditem &countitems
301 &delitem &deletebiblioitem &delbiblio
302 &getitemtypes &getbiblio
303 &getbiblioitembybiblionumber
304 &getbiblioitem &getitemsbybiblioitem
306 &newcompletebiblioitem
308 &MARCfind_oldbiblionumber_from_MARCbibid
309 &MARCfind_MARCbibid_from_oldbiblionumber
310 &MARCfind_marc_from_kohafield
314 &NEWnewbiblio &NEWnewitem
315 &NEWmodbiblio &NEWmoditem
317 &MARCaddbiblio &MARCadditem
318 &MARCmodsubfield &MARCaddsubfield
319 &MARCmodbiblio &MARCmoditem
320 &MARCkoha2marcBiblio &MARCmarc2koha
321 &MARCkoha2marcItem &MARChtml2marc
322 &MARCgetbiblio &MARCgetitem
323 &MARCaddword &MARCdelword
329 # MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC
332 # all the following subs takes a MARC::Record as parameter and manage
333 # the MARC-DB. They are called by the 1.0/1.2 xxx subs, and by the
334 # NEWxxx subs (xxx deals with old-DB parameters, the NEWxxx deals with MARC-DB parameter)
338 C4::Biblio - acquisition, catalog management functions
342 move from 1.2 to 1.4 version :
343 1.2 and previous version uses a specific API to manage biblios. This API uses old-DB style parameters.
344 In the 1.4 version, we want to do 2 differents things :
345 - keep populating the old-DB, that has a LOT less datas than MARC
346 - populate the MARC-DB
347 To populate the DBs we have 2 differents sources :
348 - the standard acquisition system (through book sellers), that does'nt use MARC data
349 - the MARC acquisition system, that uses MARC data.
351 Thus, we have 2 differents cases :
352 - 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
353 - 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
355 That's why we need 4 subs :
356 all I<subs beginning by MARC> manage only MARC tables. They manage MARC-DB with MARC::Record parameters
357 all I<subs beginning by OLD> manage only OLD-DB tables. They manage old-DB with old-DB parameters
358 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
359 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.
361 - NEW and old-style API should be used in koha to manage biblio
362 - MARCsubs are divided in 2 parts :
363 * some of them manage MARC parameters. They are heavily used in koha.
364 * some of them manage MARC biblio : they are mostly used by NEW and old-style subs.
365 - OLD are used internally only
367 all subs requires/use $dbh as 1st parameter.
369 I<NEWxxx related subs>
371 all subs requires/use $dbh as 1st parameter.
372 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
374 I<OLDxxx related subs>
376 all subs requires/use $dbh as 1st parameter.
377 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
379 They all are the exact copy of 1.0/1.2 version of the sub without the OLD.
380 The OLDxxx is called by the original xxx sub.
381 the 1.4 xxx sub also builds MARC::Record an calls the MARCxxx
383 WARNING : there is 1 difference between initialxxx and OLDxxx :
384 the db header $dbh is always passed as parameter to avoid over-DB connexion
390 =item @tagslib = &MARCgettagslib($dbh,1|0);
392 last param is 1 for liblibrarian and 0 for libopac
393 returns a hash with tag/subfield meaning
394 =item ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,$kohafield);
396 finds MARC tag and subfield for a given kohafield
397 kohafield is "table.field" where table= biblio|biblioitems|items, and field a field of the previous table
399 =item $biblionumber = &MARCfind_oldbiblionumber_from_MARCbibid($dbh,$MARCbibi);
401 finds a old-db biblio number for a given MARCbibid number
403 =item $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
405 finds a MARC bibid from a old-db biblionumber
407 =item $MARCRecord = &MARCkoha2marcBiblio($dbh,$biblionumber,biblioitemnumber);
409 MARCkoha2marcBiblio is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB biblio/biblioitem
411 =item $MARCRecord = &MARCkoha2marcItem($dbh,$biblionumber,itemnumber);
413 MARCkoha2marcItem is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB item
415 =item $MARCRecord = &MARCkoha2marcSubtitle($dbh,$biblionumber,$subtitle);
417 MARCkoha2marcSubtitle is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB subtitle
419 =item $olddb = &MARCmarc2koha($dbh,$MARCRecord);
421 builds a hash with old-db datas from a MARC::Record
423 =item &MARCaddbiblio($dbh,$MARC::Record,$biblionumber);
425 creates a biblio (in the MARC tables only). $biblionumber is the old-db biblionumber of the biblio
427 =item &MARCaddsubfield($dbh,$bibid,$tagid,$indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
429 adds a subfield in a biblio (in the MARC tables only).
431 =item $MARCRecord = &MARCgetbiblio($dbh,$bibid);
433 Returns a MARC::Record for the biblio $bibid.
435 =item &MARCmodbiblio($dbh,$bibid,$record,$delete);
437 MARCmodbiblio changes a biblio for a biblio,MARC::Record passed as parameter
438 It 1st delete the biblio, then recreates it.
439 WARNING : the $delete parameter is not used anymore (too much unsolvable cases).
440 =item ($subfieldid,$subfieldvalue) = &MARCmodsubfield($dbh,$subfieldid,$subfieldvalue);
442 MARCmodsubfield changes the value of a given subfield
444 =item $subfieldid = &MARCfindsubfield($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue);
446 MARCfindsubfield returns a subfield number given a bibid/tag/subfieldvalue values.
447 Returns -1 if more than 1 answer
449 =item $subfieldid = &MARCfindsubfieldid($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
451 MARCfindsubfieldid find a subfieldid for a bibid/tag/tagorder/subfield/subfieldorder
453 =item &MARCdelsubfield($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
455 MARCdelsubfield delete a subfield for a bibid/tag/tagorder/subfield/subfieldorder
457 =item &MARCdelbiblio($dbh,$bibid);
459 MARCdelbiblio delete biblio $bibid
461 =item &MARCkoha2marcOnefield
463 used by MARCkoha2marc and should not be useful elsewhere
465 =item &MARCmarc2kohaOnefield
467 used by MARCmarc2koha and should not be useful elsewhere
471 used to manage MARC_word table and should not be useful elsewhere
475 used to manage MARC_word table and should not be useful elsewhere
480 my ($dbh,$forlibrarian)= @_;
482 my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
483 $sth=$dbh->prepare("select tagfield,$libfield as lib,mandatory from marc_tag_structure order by tagfield");
485 my ($lib,$tag,$res,$tab,$mandatory,$repeatable);
486 while ( ($tag,$lib,$mandatory) = $sth->fetchrow) {
487 $res->{$tag}->{lib}=$lib;
488 $res->{$tab}->{tab}=""; # XXX
489 $res->{$tag}->{mandatory}=$mandatory;
492 $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");
496 my $authorised_value;
497 my $thesaurus_category;
500 while ( ($tag, $subfield, $lib, $tab, $mandatory, $repeatable,$authorised_value,$thesaurus_category,$value_builder,$kohafield) = $sth->fetchrow) {
501 $res->{$tag}->{$subfield}->{lib}=$lib;
502 $res->{$tag}->{$subfield}->{tab}=$tab;
503 $res->{$tag}->{$subfield}->{mandatory}=$mandatory;
504 $res->{$tag}->{$subfield}->{repeatable}=$repeatable;
505 $res->{$tag}->{$subfield}->{authorised_value}=$authorised_value;
506 $res->{$tag}->{$subfield}->{thesaurus_category}=$thesaurus_category;
507 $res->{$tag}->{$subfield}->{value_builder}=$value_builder;
508 $res->{$tag}->{$subfield}->{kohafield}=$kohafield;
513 sub MARCfind_marc_from_kohafield {
514 my ($dbh,$kohafield) = @_;
515 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
516 $sth->execute($kohafield);
517 my ($tagfield,$tagsubfield) = $sth->fetchrow;
518 return ($tagfield,$tagsubfield);
521 sub MARCfind_oldbiblionumber_from_MARCbibid {
522 my ($dbh,$MARCbibid) = @_;
523 my $sth=$dbh->prepare("select biblionumber from marc_biblio where bibid=?");
524 $sth->execute($MARCbibid);
525 my ($biblionumber) = $sth->fetchrow;
526 return $biblionumber;
529 sub MARCfind_MARCbibid_from_oldbiblionumber {
530 my ($dbh,$oldbiblionumber) = @_;
531 my $sth=$dbh->prepare("select bibid from marc_biblio where biblionumber=?");
532 $sth->execute($oldbiblionumber);
533 my ($bibid) = $sth->fetchrow;
538 # pass the MARC::Record to this function, and it will create the records in the marc tables
539 my ($dbh,$record,$biblionumber,$bibid) = @_;
540 my @fields=$record->fields();
542 # adding main table, and retrieving bibid
543 # if bibid is sent, then it's not a true add, it's only a re-add, after a delete (ie, a mod)
544 # if bibid empty => true add, find a new bibid number
546 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
547 my $sth=$dbh->prepare("insert into marc_biblio (datecreated,biblionumber) values (now(),?)");
548 $sth->execute($biblionumber);
549 $sth=$dbh->prepare("select max(bibid) from marc_biblio");
551 ($bibid)=$sth->fetchrow;
555 # now, add subfields...
556 foreach my $field (@fields) {
558 if ($field->tag() <10) {
559 &MARCaddsubfield($dbh,$bibid,
568 my @subfields=$field->subfields();
569 foreach my $subfieldcount (0..$#subfields) {
570 &MARCaddsubfield($dbh,$bibid,
572 $field->indicator(1).$field->indicator(2),
574 $subfields[$subfieldcount][0],
576 $subfields[$subfieldcount][1]
581 $dbh->do("unlock tables");
586 # pass the MARC::Record to this function, and it will create the records in the marc tables
587 my ($dbh,$record,$biblionumber) = @_;
588 # warn "adding : ".$record->as_formatted();
589 # search for MARC biblionumber
590 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
591 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
592 my @fields=$record->fields();
593 my $sth = $dbh->prepare("select max(tagorder) from marc_subfield_table where bibid=?");
594 $sth->execute($bibid);
595 my ($fieldcount) = $sth->fetchrow;
596 # now, add subfields...
597 foreach my $field (@fields) {
598 my @subfields=$field->subfields();
600 foreach my $subfieldcount (0..$#subfields) {
601 &MARCaddsubfield($dbh,$bibid,
603 $field->indicator(1).$field->indicator(2),
605 $subfields[$subfieldcount][0],
607 $subfields[$subfieldcount][1]
611 $dbh->do("unlock tables");
615 sub MARCaddsubfield {
616 # Add a new subfield to a tag into the DB.
617 my ($dbh,$bibid,$tagid,$tag_indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalues) = @_;
618 # if not value, end of job, we do nothing
619 if (length($subfieldvalues) ==0) {
622 if (not($subfieldcode)) {
625 my @subfieldvalues = split /\|/,$subfieldvalues;
626 foreach my $subfieldvalue (@subfieldvalues) {
627 if (length($subfieldvalue)>255) {
628 # $dbh->do("lock tables marc_blob_subfield WRITE, marc_subfield_table WRITE");
629 my $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
630 $sth->execute($subfieldvalue);
631 $sth=$dbh->prepare("select max(blobidlink)from marc_blob_subfield");
633 my ($res)=$sth->fetchrow;
634 $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,valuebloblink) values (?,?,?,?,?,?,?)");
635 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$res);
637 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";
639 # $dbh->do("unlock tables");
641 my $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?,?)");
642 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue);
644 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";
647 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
652 # Returns MARC::Record of the biblio passed in parameter.
654 my $record = MARC::Record->new();
655 #---- TODO : the leader is missing
656 $record->leader(' ');
657 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
658 from marc_subfield_table
659 where bibid=? order by tag,tagorder,subfieldcode
661 my $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
662 $sth->execute($bibid);
666 my $field; # for >=10 tags
667 my $prevvalue; # for <10 tags
668 while (my $row=$sth->fetchrow_hashref) {
669 if ($row->{'valuebloblink'}) { #---- search blob if there is one
670 $sth2->execute($row->{'valuebloblink'});
671 my $row2=$sth2->fetchrow_hashref;
673 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
675 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
678 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
680 $record->add_fields($field) unless $prevtag eq "XXX";
683 $prevtagorder=$row->{tagorder};
684 $prevtag = $row->{tag};
685 $previndicator=$row->{tag_indicator};
686 if ($row->{tag}<10) {
687 $prevvalue = $row->{subfieldvalue};
689 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.' ',0,1), substr($row->{tag_indicator}.' ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
692 if ($row->{tag} <10) {
693 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
695 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
697 $prevtag= $row->{tag};
698 $previndicator=$row->{tag_indicator};
701 # the last has not been included inside the loop... do it now !
703 $record->add_fields($prevtag,$prevvalue);
705 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
706 $record->add_fields($field);
711 # Returns MARC::Record of the biblio passed in parameter.
712 my ($dbh,$bibid,$itemnumber)=@_;
713 my $record = MARC::Record->new();
714 # search MARC tagorder
715 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=?");
716 $sth2->execute($bibid,$itemnumber);
717 my ($tagorder) = $sth2->fetchrow_array();
718 #---- TODO : the leader is missing
719 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
720 from marc_subfield_table
721 where bibid=? and tagorder=? order by subfieldcode,subfieldorder
723 $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
724 $sth->execute($bibid,$tagorder);
725 while (my $row=$sth->fetchrow_hashref) {
726 if ($row->{'valuebloblink'}) { #---- search blob if there is one
727 $sth2->execute($row->{'valuebloblink'});
728 my $row2=$sth2->fetchrow_hashref;
730 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
732 if ($record->field($row->{'tag'})) {
734 #--- this test must stay as this, because of strange behaviour of mySQL/Perl DBI with char var containing a number...
735 #--- sometimes, eliminates 0 at beginning, sometimes no ;-\\\
736 if (length($row->{'tag'}) <3) {
737 $row->{'tag'} = "0".$row->{'tag'};
739 $field =$record->field($row->{'tag'});
741 my $x = $field->add_subfields($row->{'subfieldcode'},$row->{'subfieldvalue'});
742 $record->delete_field($field);
743 $record->add_fields($field);
746 if (length($row->{'tag'}) < 3) {
747 $row->{'tag'} = "0".$row->{'tag'};
749 my $temp = MARC::Field->new($row->{'tag'}," "," ", $row->{'subfieldcode'} => $row->{'subfieldvalue'});
750 $record->add_fields($temp);
758 my ($dbh,$bibid,$record,$delete)=@_;
759 my $oldrecord=&MARCgetbiblio($dbh,$bibid);
760 if ($oldrecord eq $record) {
763 # 1st delete the biblio,
765 &MARCdelbiblio($dbh,$bibid,1);
766 my $biblionumber = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
767 &MARCaddbiblio($dbh,$record,$biblionumber,$bibid);
771 my ($dbh,$bibid,$keep_items) = @_;
772 # if the keep_item is set to 1, then all items are preserved.
773 # This flag is set when the delbiblio is called by modbiblio
774 # due to a too complex structure of MARC (repeatable fields and subfields),
775 # the best solution for a modif is to delete / recreate the record.
776 if ($keep_items eq 1) {
777 #search item field code
778 my $sth = $dbh->prepare("select tagfield from marc_subfield_structure where kohafield like 'items.%'");
780 my $itemtag = $sth->fetchrow_hashref->{tagfield};
781 $dbh->do("delete from marc_subfield_table where bibid=$bibid and tag<>$itemtag");
782 $dbh->do("delete from marc_word where bibid=$bibid and tag<>$itemtag");
784 $dbh->do("delete from marc_biblio where bibid=$bibid");
785 $dbh->do("delete from marc_subfield_table where bibid=$bibid");
786 $dbh->do("delete from marc_word where bibid=$bibid");
790 my ($dbh,$record,$bibid,$itemnumber,$delete)=@_;
791 my $oldrecord=&MARCgetitem($dbh,$bibid,$itemnumber);
792 # if nothing to change, don't waste time...
793 if ($oldrecord eq $record) {
797 # otherwise, skip through each subfield...
798 my @fields = $record->fields();
799 # search old MARC item
800 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=?");
801 $sth2->execute($bibid,$itemnumber);
802 my ($tagorder) = $sth2->fetchrow_array();
803 foreach my $field (@fields) {
804 my $oldfield = $oldrecord->field($field->tag());
805 my @subfields=$field->subfields();
807 foreach my $subfield (@subfields) {
809 # warn "compare : $oldfield".$oldfield->subfield(@$subfield[0]);
810 if ($oldfield eq 0 or (length($oldfield->subfield(@$subfield[0])) ==0) ) {
811 # just adding datas...
812 # warn "addfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
813 # warn "NEW subfield : $bibid,".$field->tag().",".$tagorder.",".@$subfield[0].",".$subfieldorder.",".@$subfield[1].")";
814 &MARCaddsubfield($dbh,$bibid,$field->tag(),$field->indicator(1).$field->indicator(2),
815 $tagorder,@$subfield[0],$subfieldorder,@$subfield[1]);
817 # warn "modfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
818 # modify he subfield if it's a different string
819 if ($oldfield->subfield(@$subfield[0]) ne @$subfield[1] ) {
820 my $subfieldid=&MARCfindsubfieldid($dbh,$bibid,$field->tag(),$tagorder,@$subfield[0],$subfieldorder);
821 # warn "changing : $subfieldid, $bibid,".$field->tag(),",$tagorder,@$subfield[0],@$subfield[1],$subfieldorder";
822 &MARCmodsubfield($dbh,$subfieldid,@$subfield[1]);
830 sub MARCmodsubfield {
831 # Subroutine changes a subfield value given a subfieldid.
832 my ($dbh, $subfieldid, $subfieldvalue )=@_;
833 $dbh->do("lock tables marc_blob_subfield WRITE,marc_subfield_table WRITE");
834 my $sth1=$dbh->prepare("select valuebloblink from marc_subfield_table where subfieldid=?");
835 $sth1->execute($subfieldid);
836 my ($oldvaluebloblink)=$sth1->fetchrow;
839 # if too long, use a bloblink
840 if (length($subfieldvalue)>255 ) {
841 # if already a bloblink, update it, otherwise, insert a new one.
842 if ($oldvaluebloblink) {
843 $sth=$dbh->prepare("update marc_blob_subfield set subfieldvalue=? where blobidlink=?");
844 $sth->execute($subfieldvalue,$oldvaluebloblink);
846 $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
847 $sth->execute($subfieldvalue);
848 $sth=$dbh->prepare("select max(blobidlink) from marc_blob_subfield");
850 my ($res)=$sth->fetchrow;
851 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=null, valuebloblink=$res where subfieldid=?");
852 $sth->execute($subfieldid);
855 # note this can leave orphan bloblink. Not a big problem, but we should build somewhere a orphan deleting script...
856 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=?,valuebloblink=null where subfieldid=?");
857 $sth->execute($subfieldvalue, $subfieldid);
859 $dbh->do("unlock tables");
861 $sth=$dbh->prepare("select bibid,tag,tagorder,subfieldcode,subfieldid,subfieldorder from marc_subfield_table where subfieldid=?");
862 $sth->execute($subfieldid);
863 my ($bibid,$tagid,$tagorder,$subfieldcode,$x,$subfieldorder) = $sth->fetchrow;
865 &MARCdelword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder);
866 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
867 return($subfieldid, $subfieldvalue);
870 sub MARCfindsubfield {
871 my ($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue) = @_;
875 my $query="select subfieldid from marc_subfield_table where bibid=? and tag=? and subfieldcode=?";
876 if ($subfieldvalue) {
877 $query .= " and subfieldvalue=".$dbh->quote($subfieldvalue);
879 if ($subfieldorder<1) {
882 $query .= " and subfieldorder=$subfieldorder";
884 my $sti=$dbh->prepare($query);
885 $sti->execute($bibid,$tag, $subfieldcode);
886 while (($subfieldid) = $sti->fetchrow) {
888 $lastsubfieldid=$subfieldid;
890 if ($resultcounter>1) {
891 # Error condition. Values given did not resolve into a unique record. Don't know what to edit
892 # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
895 return $lastsubfieldid;
899 sub MARCfindsubfieldid {
900 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
901 my $sth=$dbh->prepare("select subfieldid from marc_subfield_table
902 where bibid=? and tag=? and tagorder=?
903 and subfieldcode=? and subfieldorder=?");
904 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
905 my ($res) = $sth->fetchrow;
907 $sth=$dbh->prepare("select subfieldid from marc_subfield_table
908 where bibid=? and tag=? and tagorder=?
909 and subfieldcode=?");
910 $sth->execute($bibid,$tag,$tagorder,$subfield);
911 ($res) = $sth->fetchrow;
916 sub MARCdelsubfield {
917 # delete a subfield for $bibid / tag / tagorder / subfield / subfieldorder
918 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
919 $dbh->do("delete from marc_subfield_table where bibid='$bibid' and
920 tag='$tag' and tagorder='$tagorder'
921 and subfieldcode='$subfield' and subfieldorder='$subfieldorder
925 sub MARCkoha2marcBiblio {
926 # this function builds partial MARC::Record from the old koha-DB fields
927 my ($dbh,$biblionumber,$biblioitemnumber) = @_;
928 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
929 my $record = MARC::Record->new();
930 #--- if bibid, then retrieve old-style koha data
931 if ($biblionumber>0) {
932 my $sth2=$dbh->prepare("select biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp
933 from biblio where biblionumber=?");
934 $sth2->execute($biblionumber);
935 my $row=$sth2->fetchrow_hashref;
937 foreach $code (keys %$row) {
939 &MARCkoha2marcOnefield($sth,$record,"biblio.".$code,$row->{$code});
943 #--- if biblioitem, then retrieve old-style koha data
944 if ($biblioitemnumber>0) {
945 my $sth2=$dbh->prepare(" SELECT biblioitemnumber,biblionumber,volume,number,classification,
946 itemtype,url,isbn,issn,dewey,subclass,publicationyear,publishercode,
947 volumedate,volumeddesc,timestamp,illus,pages,notes,size,place
949 WHERE biblionumber=? and biblioitemnumber=?
951 $sth2->execute($biblionumber,$biblioitemnumber);
952 my $row=$sth2->fetchrow_hashref;
954 foreach $code (keys %$row) {
956 &MARCkoha2marcOnefield($sth,$record,"biblioitems.".$code,$row->{$code});
960 # other fields => additional authors, subjects, subtitles
961 my $sth2=$dbh->prepare(" SELECT author FROM additionalauthors WHERE biblionumber=?");
962 $sth2->execute($biblionumber);
963 while (my $row=$sth2->fetchrow_hashref) {
964 &MARCkoha2marcOnefield($sth,$record,"additionalauthors.author",$row->{'author'});
966 my $sth2=$dbh->prepare(" SELECT subject FROM bibliosubject WHERE biblionumber=?");
967 $sth2->execute($biblionumber);
968 while (my $row=$sth2->fetchrow_hashref) {
969 &MARCkoha2marcOnefield($sth,$record,"bibliosubject.subject",$row->{'subject'});
971 my $sth2=$dbh->prepare(" SELECT subtitle FROM bibliosubtitle WHERE biblionumber=?");
972 $sth2->execute($biblionumber);
973 while (my $row=$sth2->fetchrow_hashref) {
974 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.title",$row->{'subtitle'});
979 sub MARCkoha2marcItem {
980 # this function builds partial MARC::Record from the old koha-DB fields
981 my ($dbh,$biblionumber,$itemnumber) = @_;
982 # my $dbh=&C4Connect;
983 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
984 my $record = MARC::Record->new();
985 #--- if item, then retrieve old-style koha data
987 # print STDERR "prepare $biblionumber,$itemnumber\n";
988 my $sth2=$dbh->prepare("SELECT itemnumber,biblionumber,multivolumepart,biblioitemnumber,barcode,dateaccessioned,
989 booksellerid,homebranch,price,replacementprice,replacementpricedate,datelastborrowed,
990 datelastseen,multivolume,stack,notforloan,itemlost,wthdrawn,bulk,issues,renewals,
991 reserves,restricted,binding,itemnotes,holdingbranch,timestamp
993 WHERE itemnumber=?");
994 $sth2->execute($itemnumber);
995 my $row=$sth2->fetchrow_hashref;
997 foreach $code (keys %$row) {
999 &MARCkoha2marcOnefield($sth,$record,"items.".$code,$row->{$code});
1006 sub MARCkoha2marcSubtitle {
1007 # this function builds partial MARC::Record from the old koha-DB fields
1008 my ($dbh,$bibnum,$subtitle) = @_;
1009 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1010 my $record = MARC::Record->new();
1011 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.subtitle",$subtitle);
1015 sub MARCkoha2marcOnefield {
1016 my ($sth,$record,$kohafieldname,$value)=@_;
1019 $sth->execute($kohafieldname);
1020 if (($tagfield,$tagsubfield)=$sth->fetchrow) {
1021 if ($record->field($tagfield)) {
1022 my $tag =$record->field($tagfield);
1024 $tag->add_subfields($tagsubfield,$value);
1025 $record->delete_field($tag);
1026 $record->add_fields($tag);
1029 $record->add_fields($tagfield," "," ",$tagsubfield => $value);
1036 my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
1038 my $record = MARC::Record->new();
1039 # my %subfieldlist=();
1040 my $prevvalue; # if tag <10
1041 my $field; # if tag >=10
1042 for (my $i=0; $i< @$rtags; $i++) {
1043 # rebuild MARC::Record
1044 if (@$rtags[$i] ne $prevtag) {
1045 if ($prevtag < 10) {
1047 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
1051 $record->add_fields($field);
1054 $indicators{@$rtags[$i]}.=' ';
1055 if (@$rtags[$i] <10) {
1056 $prevvalue= @$rvalues[$i];
1058 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
1060 $prevtag = @$rtags[$i];
1062 if (@$rtags[$i] <10) {
1063 $prevvalue=@$rvalues[$i];
1065 if (@$rvalues[$i]) {
1066 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
1069 $prevtag= @$rtags[$i];
1072 # the last has not been included inside the loop... do it now !
1073 $record->add_fields($field);
1074 # warn $record->as_formatted;
1079 my ($dbh,$record) = @_;
1080 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1082 my $sth2=$dbh->prepare("SHOW COLUMNS from biblio");
1085 # print STDERR $record->as_formatted;
1086 while (($field)=$sth2->fetchrow) {
1087 $result=&MARCmarc2kohaOneField($sth,"biblio",$field,$record,$result);
1089 $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
1091 while (($field)=$sth2->fetchrow) {
1092 $result=&MARCmarc2kohaOneField($sth,"biblioitems",$field,$record,$result);
1094 $sth2=$dbh->prepare("SHOW COLUMNS from items");
1096 while (($field)=$sth2->fetchrow) {
1097 $result = &MARCmarc2kohaOneField($sth,"items",$field,$record,$result);
1099 # additional authors : specific
1100 $result = &MARCmarc2kohaOneField($sth,"additionalauthors","additionalauthors",$record,$result);
1101 # modify copyrightdate to keep only the 1st year found
1102 my $temp = $result->{'copyrightdate'};
1103 $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
1105 $result->{'copyrightdate'} = $1;
1106 } else { # if no cYYYY, get the 1st date.
1107 $x =~ m/(\d\d\d\d)/;
1108 $result->{'copyrightdate'} = $1;
1113 sub MARCmarc2kohaOneField {
1114 # FIXME ? if a field has a repeatable subfield that is used in old-db, only the 1st will be retrieved...
1115 my ($sth,$kohatable,$kohafield,$record,$result)= @_;
1116 # warn "kohatable / $kohafield / $result / ";
1120 $sth->execute($kohatable.".".$kohafield);
1121 ($tagfield,$subfield) = $sth->fetchrow;
1122 foreach my $field ($record->field($tagfield)) {
1123 if ($field->subfield($subfield)) {
1124 if ($result->{$kohafield}) {
1125 $result->{$kohafield} .= " | ".$field->subfield($subfield);
1127 $result->{$kohafield}=$field->subfield($subfield);
1135 # split a subfield string and adds it into the word table.
1137 my ($dbh,$bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$sentence) =@_;
1138 $sentence =~ s/(\.|\?|\:|\!|\'|,|\-)/ /g;
1139 my @words = split / /,$sentence;
1140 my $stopwords= C4::Context->stopwords;
1141 my $sth=$dbh->prepare("insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word)
1142 values (?,?,?,?,?,?,soundex(?))");
1143 foreach my $word (@words) {
1144 # we record only words longer than 2 car and not in stopwords hash
1145 if (length($word)>1 and !($stopwords->{uc($word)})) {
1146 $sth->execute($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,$word);
1148 warn "ERROR ==> insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word) values ($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,soundex($word))\n";
1155 # delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
1156 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
1157 my $sth=$dbh->prepare("delete from marc_word where bibid=? and tag=? and tagorder=? and subfieldid=? and subfieldorder=?");
1158 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
1163 # NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
1166 # all the following subs are useful to manage MARC-DB with complete MARC records.
1167 # it's used with marcimport, and marc management tools
1171 =item ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbibilio($dbh,$MARCRecord,$oldbiblio,$oldbiblioitem);
1173 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
1174 are builded from the MARC::Record. If they are passed, they are used.
1176 =item NEWnewitem($dbh, $record,$bibid);
1178 adds an item in the db.
1183 my ($dbh, $record, $oldbiblio, $oldbiblioitem) = @_;
1184 # note $oldbiblio and $oldbiblioitem are not mandatory.
1185 # if not present, they will be builded from $record with MARCmarc2koha function
1186 if (($oldbiblio) and not($oldbiblioitem)) {
1187 print STDERR "NEWnewbiblio : missing parameter\n";
1188 print "NEWnewbiblio : missing parameter : contact koha development team\n";
1194 $oldbibnum = OLDnewbiblio($dbh,$oldbiblio);
1195 $oldbiblioitem->{'biblionumber'} = $oldbibnum;
1196 $oldbibitemnum = OLDnewbiblioitem($dbh,$oldbiblioitem);
1198 my $olddata = MARCmarc2koha($dbh,$record);
1199 $oldbibnum = OLDnewbiblio($dbh,$olddata);
1200 $olddata->{'biblionumber'} = $oldbibnum;
1201 $oldbibitemnum = OLDnewbiblioitem($dbh,$olddata);
1203 # search subtiles, addiauthors and subjects
1204 my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author");
1205 my @addiauthfields = $record->field($tagfield);
1206 foreach my $addiauthfield (@addiauthfields) {
1207 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1208 foreach my $subfieldcount (0..$#addiauthsubfields) {
1209 OLDmodaddauthor($dbh,$oldbibnum,$addiauthsubfields[$subfieldcount]);
1212 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle");
1213 my @subtitlefields = $record->field($tagfield);
1214 foreach my $subtitlefield (@subtitlefields) {
1215 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1216 foreach my $subfieldcount (0..$#subtitlesubfields) {
1217 OLDnewsubtitle($dbh,$oldbibnum,$subtitlesubfields[$subfieldcount]);
1220 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject");
1221 my @subj = $record->field($tagfield);
1222 foreach my $subject (@subj) {
1223 my @subjsubfield = $subject->subfield($tagsubfield);
1225 foreach my $subfieldcount (0..$#subjsubfield) {
1226 push @subjects,$subjsubfield[$subfieldcount];
1228 OLDmodsubject($dbh,$oldbibnum,1,@subjects);
1230 # we must add bibnum and bibitemnum in MARC::Record...
1231 # we build the new field with biblionumber and biblioitemnumber
1232 # we drop the original field
1233 # we add the new builded field.
1234 # NOTE : Works only if the field is ONLY for biblionumber and biblioitemnumber
1235 # (steve and paul : thinks 090 is a good choice)
1236 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1237 $sth->execute("biblio.biblionumber");
1238 (my $tagfield1, my $tagsubfield1) = $sth->fetchrow;
1239 $sth->execute("biblioitems.biblioitemnumber");
1240 (my $tagfield2, my $tagsubfield2) = $sth->fetchrow;
1241 if ($tagfield1 != $tagfield2) {
1242 warn "Error in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1243 print "Content-Type: text/html\n\nError in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1246 my $newfield = MARC::Field->new( $tagfield1,'','',
1247 "$tagsubfield1" => $oldbibnum,
1248 "$tagsubfield2" => $oldbibitemnum);
1249 # drop old field and create new one...
1250 my $old_field = $record->field($tagfield1);
1251 $record->delete_field($old_field);
1252 $record->add_fields($newfield);
1253 my $bibid = MARCaddbiblio($dbh,$record,$oldbibnum);
1254 return ($bibid,$oldbibnum,$oldbibitemnum );
1258 my ($dbh,$record,$bibid) =@_;
1259 &MARCmodbiblio($dbh,$bibid,$record,0);
1260 my $oldbiblio = MARCmarc2koha($dbh,$record);
1261 my $oldbiblionumber = OLDmodbiblio($dbh,$oldbiblio);
1262 OLDmodbibitem($dbh,$oldbiblio);
1263 # now, modify addi authors, subject, addititles.
1264 my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author");
1265 my @addiauthfields = $record->field($tagfield);
1266 foreach my $addiauthfield (@addiauthfields) {
1267 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1268 foreach my $subfieldcount (0..$#addiauthsubfields) {
1269 OLDmodaddauthor($dbh,$oldbiblionumber,$addiauthsubfields[$subfieldcount]);
1272 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle");
1273 my @subtitlefields = $record->field($tagfield);
1274 foreach my $subtitlefield (@subtitlefields) {
1275 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1276 foreach my $subfieldcount (0..$#subtitlesubfields) {
1277 OLDmodsubtitle($dbh,$oldbiblionumber,$subtitlesubfields[$subfieldcount]);
1280 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject");
1281 my @subj = $record->field($tagfield);
1282 foreach my $subject (@subj) {
1283 my @subjsubfield = $subject->subfield($tagsubfield);
1285 foreach my $subfieldcount (0..$#subjsubfield) {
1286 push @subjects,$subjsubfield[$subfieldcount];
1288 OLDmodsubject($dbh,$oldbiblionumber,1,@subjects);
1295 my ($dbh, $record,$bibid) = @_;
1296 # add item in old-DB
1297 my $item = &MARCmarc2koha($dbh,$record);
1298 # needs old biblionumber and biblioitemnumber
1299 $item->{'biblionumber'} = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
1300 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
1301 $sth->execute($item->{'biblionumber'});
1302 ($item->{'biblioitemnumber'}) = $sth->fetchrow;
1303 my ($itemnumber,$error) = &OLDnewitems($dbh,$item,$item->{barcode});
1304 # add itemnumber to MARC::Record before adding the item.
1305 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1306 &MARCkoha2marcOnefield($sth,$record,"items.itemnumber",$itemnumber);
1308 my $bib = &MARCadditem($dbh,$record,$item->{'biblionumber'});
1312 my ($dbh,$record,$bibid,$itemnumber,$delete) = @_;
1313 &MARCmoditem($dbh,$record,$bibid,$itemnumber,$delete);
1314 my $olditem = MARCmarc2koha($dbh,$record);
1315 OLDmoditem($dbh,$olditem);
1320 # OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
1324 =item $biblionumber = OLDnewbiblio($dbh,$biblio);
1326 adds a record in biblio table. Datas are in the hash $biblio.
1328 =item $biblionumber = OLDmodbiblio($dbh,$biblio);
1330 modify a record in biblio table. Datas are in the hash $biblio.
1332 =item OLDmodsubtitle($dbh,$bibnum,$subtitle);
1334 modify subtitles in bibliosubtitle table.
1336 =item OLDmodaddauthor($dbh,$bibnum,$author);
1338 adds or modify additional authors
1339 NOTE : Strange sub : seems to delete MANY and add only ONE author... maybe buggy ?
1341 =item $errors = OLDmodsubject($dbh,$bibnum, $force, @subject);
1343 modify/adds subjects
1345 =item OLDmodbibitem($dbh, $biblioitem);
1349 =item OLDmodnote($dbh,$bibitemnum,$note
1351 modify a note for a biblioitem
1353 =item OLDnewbiblioitem($dbh,$biblioitem);
1355 adds a biblioitem ($biblioitem is a hash with the values)
1357 =item OLDnewsubject($dbh,$bibnum);
1361 =item OLDnewsubtitle($dbh,$bibnum,$subtitle);
1363 create a new subtitle
1365 =item ($itemnumber,$errors)= OLDnewitems($dbh,$item,$barcode);
1367 create a item. $item is a hash and $barcode the barcode.
1369 =item OLDmoditem($dbh,$item);
1373 =item OLDdelitem($dbh,$itemnum);
1377 =item OLDdeletebiblioitem($dbh,$biblioitemnumber);
1379 deletes a biblioitem
1380 NOTE : not standard sub name. Should be OLDdelbiblioitem()
1382 =item OLDdelbiblio($dbh,$biblio);
1389 my ($dbh,$biblio) = @_;
1390 # my $dbh = &C4Connect;
1391 my $query = "Select max(biblionumber) from biblio";
1392 my $sth = $dbh->prepare($query);
1394 my $data = $sth->fetchrow_arrayref;
1395 my $bibnum = $$data[0] + 1;
1398 if ($biblio->{'seriestitle'}) { $series = 1 };
1400 $query = "insert into biblio set biblionumber = ?, title = ?, author = ?, copyrightdate = ?,
1401 serial = ?, seriestitle = ?, notes = ?, abstract = ?";
1402 $sth = $dbh->prepare($query);
1403 $sth->execute($bibnum,$biblio->{'title'},$biblio->{'author'},$biblio->{'copyright'},$series,$biblio->{'seriestitle'},$biblio->{'notes'},$biblio->{'abstract'});
1411 my ($dbh,$biblio) = @_;
1412 # my $dbh = C4Connect;
1416 $query = "Update biblio set title = ?, author = ?, abstract = ?, copyrightdate = ?,
1417 seriestitle = ?, serial = ?, unititle = ?, notes = ? where biblionumber = ?";
1418 $sth = $dbh->prepare($query);
1419 $sth->execute($biblio->{'title'},$biblio->{'author'},$biblio->{'abstract'},$biblio->{'copyrightdate'},
1420 $biblio->{'seriestitle'},$biblio->{'serial'},$biblio->{'unititle'},$biblio->{'notes'},$biblio->{'biblionumber'});
1423 return($biblio->{'biblionumber'});
1426 sub OLDmodsubtitle {
1427 my ($dbh,$bibnum, $subtitle) = @_;
1428 my $query = "update bibliosubtitle set subtitle = ? where biblionumber = ?";
1429 my $sth = $dbh->prepare($query);
1430 $sth->execute($subtitle,$bibnum);
1435 sub OLDmodaddauthor {
1436 my ($dbh,$bibnum, $author) = @_;
1437 # my $dbh = C4Connect;
1438 my $query = "Delete from additionalauthors where biblionumber = $bibnum";
1439 my $sth = $dbh->prepare($query);
1444 if ($author ne '') {
1445 $query = "Insert into additionalauthors set
1448 $sth = $dbh->prepare($query);
1450 $sth->execute($author,$bibnum);
1454 } # sub modaddauthor
1458 my ($dbh,$bibnum, $force, @subject) = @_;
1459 # my $dbh = C4Connect;
1460 my $count = @subject;
1462 for (my $i = 0; $i < $count; $i++) {
1463 $subject[$i] =~ s/^ //g;
1464 $subject[$i] =~ s/ $//g;
1465 my $query = "select * from catalogueentry where entrytype = 's' and catalogueentry = '$subject[$i]'";
1466 my $sth = $dbh->prepare($query);
1469 if (my $data = $sth->fetchrow_hashref) {
1471 if ($force eq $subject[$i] || $force eq 1) {
1472 # subject not in aut, chosen to force anway
1473 # so insert into cataloguentry so its in auth file
1474 $query = "Insert into catalogueentry (entrytype,catalogueentry) values ('s','$subject[$i]')";
1475 my $sth2 = $dbh->prepare($query);
1480 $error = "$subject[$i]\n does not exist in the subject authority file";
1481 $query = "Select * from catalogueentry where entrytype = 's' and (catalogueentry like '$subject[$i] %'
1482 or catalogueentry like '% $subject[$i] %' or catalogueentry like '% $subject[$i]')";
1483 my $sth2 = $dbh->prepare($query);
1485 while (my $data = $sth2->fetchrow_hashref) {
1486 $error .= "<br>$data->{'catalogueentry'}";
1494 my $query = "Delete from bibliosubject where biblionumber = $bibnum";
1495 my $sth = $dbh->prepare($query);
1498 for (my $i = 0; $i < $count; $i++) {
1499 $sth = $dbh->prepare("Insert into bibliosubject values ('$subject[$i]', $bibnum)");
1510 my ($dbh,$biblioitem) = @_;
1511 # my $dbh = C4Connect;
1514 $biblioitem->{'itemtype'} = $dbh->quote($biblioitem->{'itemtype'});
1515 $biblioitem->{'url'} = $dbh->quote($biblioitem->{'url'});
1516 $biblioitem->{'isbn'} = $dbh->quote($biblioitem->{'isbn'});
1517 $biblioitem->{'publishercode'} = $dbh->quote($biblioitem->{'publishercode'});
1518 $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
1519 $biblioitem->{'classification'} = $dbh->quote($biblioitem->{'classification'});
1520 $biblioitem->{'dewey'} = $dbh->quote($biblioitem->{'dewey'});
1521 $biblioitem->{'subclass'} = $dbh->quote($biblioitem->{'subclass'});
1522 $biblioitem->{'illus'} = $dbh->quote($biblioitem->{'illus'});
1523 $biblioitem->{'pages'} = $dbh->quote($biblioitem->{'pages'});
1524 $biblioitem->{'volumeddesc'} = $dbh->quote($biblioitem->{'volumeddesc'});
1525 $biblioitem->{'notes'} = $dbh->quote($biblioitem->{'notes'});
1526 $biblioitem->{'size'} = $dbh->quote($biblioitem->{'size'});
1527 $biblioitem->{'place'} = $dbh->quote($biblioitem->{'place'});
1529 $query = "Update biblioitems set
1530 itemtype = $biblioitem->{'itemtype'},
1531 url = $biblioitem->{'url'},
1532 isbn = $biblioitem->{'isbn'},
1533 publishercode = $biblioitem->{'publishercode'},
1534 publicationyear = $biblioitem->{'publicationyear'},
1535 classification = $biblioitem->{'classification'},
1536 dewey = $biblioitem->{'dewey'},
1537 subclass = $biblioitem->{'subclass'},
1538 illus = $biblioitem->{'illus'},
1539 pages = $biblioitem->{'pages'},
1540 volumeddesc = $biblioitem->{'volumeddesc'},
1541 notes = $biblioitem->{'notes'},
1542 size = $biblioitem->{'size'},
1543 place = $biblioitem->{'place'}
1544 where biblioitemnumber = $biblioitem->{'biblioitemnumber'}";
1552 my ($dbh,$bibitemnum,$note)=@_;
1553 # my $dbh=C4Connect;
1554 my $query="update biblioitems set notes='$note' where
1555 biblioitemnumber='$bibitemnum'";
1556 my $sth=$dbh->prepare($query);
1562 sub OLDnewbiblioitem {
1563 my ($dbh,$biblioitem) = @_;
1564 # my $dbh = C4Connect;
1565 my $query = "Select max(biblioitemnumber) from biblioitems";
1566 my $sth = $dbh->prepare($query);
1571 $data = $sth->fetchrow_arrayref;
1572 $bibitemnum = $$data[0] + 1;
1576 $sth = $dbh->prepare("insert into biblioitems set
1577 biblioitemnumber = ?, biblionumber = ?,
1578 volume = ?, number = ?,
1579 classification = ?, itemtype = ?,
1581 issn = ?, dewey = ?,
1582 subclass = ?, publicationyear = ?,
1583 publishercode = ?, volumedate = ?,
1584 volumeddesc = ?, illus = ?,
1585 pages = ?, notes = ?,
1587 marc = ?, place = ?");
1588 $sth->execute($bibitemnum, $biblioitem->{'biblionumber'},
1589 $biblioitem->{'volume'}, $biblioitem->{'number'},
1590 $biblioitem->{'classification'}, $biblioitem->{'itemtype'},
1591 $biblioitem->{'url'}, $biblioitem->{'isbn'},
1592 $biblioitem->{'issn'}, $biblioitem->{'dewey'},
1593 $biblioitem->{'subclass'}, $biblioitem->{'publicationyear'},
1594 $biblioitem->{'publishercode'}, $biblioitem->{'volumedate'},
1595 $biblioitem->{'volumeddesc'}, $biblioitem->{'illus'},
1596 $biblioitem->{'pages'}, $biblioitem->{'notes'},
1597 $biblioitem->{'size'}, $biblioitem->{'lccn'},
1598 $biblioitem->{'marc'}, $biblioitem->{'place'});
1601 return($bibitemnum);
1605 my ($dbh,$bibnum)=@_;
1606 my $query="insert into bibliosubject (biblionumber) values ($bibnum)";
1607 my $sth=$dbh->prepare($query);
1612 sub OLDnewsubtitle {
1613 my ($dbh,$bibnum, $subtitle) = @_;
1614 my $query = "insert into bibliosubtitle set biblionumber = ?, subtitle = ?";
1615 my $sth = $dbh->prepare($query);
1616 $sth->execute($bibnum,$subtitle);
1622 my ($dbh,$item, $barcode) = @_;
1623 # my $dbh = C4Connect;
1624 my $query = "Select max(itemnumber) from items";
1625 my $sth = $dbh->prepare($query);
1631 $data = $sth->fetchrow_hashref;
1632 $itemnumber = $data->{'max(itemnumber)'} + 1;
1634 # if dateaccessioned is provided, use it. Otherwise, set to NOW()
1635 if ($item->{'dateaccessioned'}) {
1636 $sth=$dbh->prepare("Insert into items set
1637 itemnumber = ?, biblionumber = ?,
1638 biblioitemnumber = ?, barcode = ?,
1639 booksellerid = ?, dateaccessioned = ?,
1640 homebranch = ?, holdingbranch = ?,
1641 price = ?, replacementprice = ?,
1642 replacementpricedate = NOW(), itemnotes = ?,
1645 $sth->execute($itemnumber, $item->{'biblionumber'},
1646 $item->{'biblioitemnumber'},$barcode,
1647 $item->{'booksellerid'},$item->{'dateaccessioned'},
1648 $item->{'homebranch'},$item->{'holdingbranch'},
1649 $item->{'price'},$item->{'replacementprice'},
1650 $item->{'itemnotes'},$item->{'loan'});
1652 $sth=$dbh->prepare("Insert into items set
1653 itemnumber = ?, biblionumber = ?,
1654 biblioitemnumber = ?, barcode = ?,
1655 booksellerid = ?, dateaccessioned = NOW(),
1656 homebranch = ?, holdingbranch = ?,
1657 price = ?, replacementprice = ?,
1658 replacementpricedate = NOW(), itemnotes = ?,
1661 $sth->execute($itemnumber, $item->{'biblionumber'},
1662 $item->{'biblioitemnumber'},$barcode,
1663 $item->{'booksellerid'},
1664 $item->{'homebranch'},$item->{'holdingbranch'},
1665 $item->{'price'},$item->{'replacementprice'},
1666 $item->{'itemnotes'},$item->{'loan'});
1668 if (defined $sth->errstr) {
1669 $error .= $sth->errstr;
1672 return($itemnumber,$error);
1676 my ($dbh,$item) = @_;
1677 # my ($dbh,$loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
1678 # my $dbh=C4Connect;
1679 $item->{'itemnum'}=$item->{'itemnumber'} unless $item->{'itemnum'};
1680 my $query="update items set barcode='$item->{'barcode'}',itemnotes='$item->{'notes'}'
1681 where itemnumber=$item->{'itemnum'}";
1682 if ($item->{'barcode'} eq ''){
1683 $query="update items set notforloan=$item->{'loan'} where itemnumber=$item->{'itemnum'}";
1685 if ($item->{'lost'} ne ''){
1686 $query="update items set biblioitemnumber=$item->{'bibitemnum'},
1687 barcode='$item->{'barcode'}',
1688 itemnotes='$item->{'notes'}',
1689 homebranch='$item->{'homebranch'}',
1690 itemlost='$item->{'lost'}',
1691 wthdrawn='$item->{'wthdrawn'}'
1692 where itemnumber=$item->{'itemnum'}";
1694 if ($item->{'replacement'} ne ''){
1695 $query=~ s/ where/,replacementprice='$item->{'replacement'}' where/;
1697 my $sth=$dbh->prepare($query);
1704 my ($dbh,$itemnum)=@_;
1705 # my $dbh=C4Connect;
1706 my $query="select * from items where itemnumber=$itemnum";
1707 my $sth=$dbh->prepare($query);
1709 my @data=$sth->fetchrow_array;
1711 $query="Insert into deleteditems values (";
1712 foreach my $temp (@data){
1713 $query .= "'$temp',";
1717 $sth=$dbh->prepare($query);
1720 $query = "Delete from items where itemnumber=$itemnum";
1721 $sth=$dbh->prepare($query);
1727 sub OLDdeletebiblioitem {
1728 my ($dbh,$biblioitemnumber) = @_;
1729 # my $dbh = C4Connect;
1730 my $query = "Select * from biblioitems
1731 where biblioitemnumber = $biblioitemnumber";
1732 my $sth = $dbh->prepare($query);
1737 if (@results = $sth->fetchrow_array) {
1738 $query = "Insert into deletedbiblioitems values (";
1739 foreach my $value (@results) {
1740 $value = $dbh->quote($value);
1741 $query .= "$value,";
1744 $query =~ s/\,$/\)/;
1747 $query = "Delete from biblioitems
1748 where biblioitemnumber = $biblioitemnumber";
1752 # Now delete all the items attached to the biblioitem
1753 $query = "Select * from items where biblioitemnumber = $biblioitemnumber";
1754 $sth = $dbh->prepare($query);
1756 while (@results = $sth->fetchrow_array) {
1757 $query = "Insert into deleteditems values (";
1758 foreach my $value (@results) {
1759 $value = $dbh->quote($value);
1760 $query .= "$value,";
1762 $query =~ s/\,$/\)/;
1766 $query = "Delete from items where biblioitemnumber = $biblioitemnumber";
1769 } # sub deletebiblioitem
1772 my ($dbh,$biblio)=@_;
1773 # my $dbh=C4Connect;
1774 my $query="select * from biblio where biblionumber=$biblio";
1775 my $sth=$dbh->prepare($query);
1777 if (my @data=$sth->fetchrow_array){
1779 $query="Insert into deletedbiblio values (";
1780 foreach my $temp (@data){
1781 $temp=~ s/\'/\\\'/g;
1782 $query .= "'$temp',";
1786 $sth=$dbh->prepare($query);
1789 $query = "Delete from biblio where biblionumber=$biblio";
1790 $sth=$dbh->prepare($query);
1806 my $dbh = C4::Context->dbh;
1807 my $query="Select count(*) from items where biblionumber=$biblio";
1809 my $sth=$dbh->prepare($query);
1811 my $data=$sth->fetchrow_hashref;
1813 return($data->{'count(*)'});
1818 ($order, $ordernumber) = &getorder($biblioitemnumber, $biblionumber);
1820 Looks up the order with the given biblionumber and biblioitemnumber.
1822 Returns a two-element array. C<$ordernumber> is the order number.
1823 C<$order> is a reference-to-hash describing the order; its keys are
1824 fields from the biblio, biblioitems, aqorders, and aqorderbreakdown
1825 tables of the Koha database.
1829 # FIXME - This is effectively identical to &C4::Catalogue::getorder.
1830 # Pick one and stick with it.
1833 my $dbh = C4::Context->dbh;
1834 my $query="Select ordernumber
1836 where biblionumber=? and biblioitemnumber=?";
1837 my $sth=$dbh->prepare($query);
1838 $sth->execute($bib,$bi);
1839 # FIXME - Use fetchrow_array(), since we're only interested in the one
1841 my $ordnum=$sth->fetchrow_hashref;
1843 my $order=getsingleorder($ordnum->{'ordernumber'});
1845 return ($order,$ordnum->{'ordernumber'});
1848 =item getsingleorder
1850 $order = &getsingleorder($ordernumber);
1852 Looks up an order by order number.
1854 Returns a reference-to-hash describing the order. The keys of
1855 C<$order> are fields from the biblio, biblioitems, aqorders, and
1856 aqorderbreakdown tables of the Koha database.
1860 # FIXME - This is effectively identical to
1861 # &C4::Catalogue::getsingleorder.
1862 # Pick one and stick with it.
1863 sub getsingleorder {
1865 my $dbh = C4::Context->dbh;
1866 my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown
1867 where aqorders.ordernumber=?
1868 and biblio.biblionumber=aqorders.biblionumber and
1869 biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
1870 aqorders.ordernumber=aqorderbreakdown.ordernumber";
1871 my $sth=$dbh->prepare($query);
1872 $sth->execute($ordnum);
1873 my $data=$sth->fetchrow_hashref;
1880 my $dbh = C4::Context->dbh;
1881 my $bibnum=OLDnewbiblio($dbh,$biblio);
1882 # finds new (MARC bibid
1883 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$bibnum);
1884 my $record = &MARCkoha2marcBiblio($dbh,$bibnum);
1885 MARCaddbiblio($dbh,$record,$bibnum);
1892 $biblionumber = &modbiblio($biblio);
1894 Update a biblio record.
1896 C<$biblio> is a reference-to-hash whose keys are the fields in the
1897 biblio table in the Koha database. All fields must be present, not
1898 just the ones you wish to change.
1900 C<&modbiblio> updates the record defined by
1901 C<$biblio-E<gt>{biblionumber}> with the values in C<$biblio>.
1903 C<&modbiblio> returns C<$biblio-E<gt>{biblionumber}> whether it was
1910 my $dbh = C4::Context->dbh;
1911 my $biblionumber=OLDmodbiblio($dbh,$biblio);
1912 my $record = MARCkoha2marcBiblio($dbh,$biblionumber);
1913 # finds new (MARC bibid
1914 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
1915 MARCmodbiblio($dbh,$bibid,$record,0);
1916 return($biblionumber);
1921 &modsubtitle($biblionumber, $subtitle);
1923 Sets the subtitle of a book.
1925 C<$biblionumber> is the biblionumber of the book to modify.
1927 C<$subtitle> is the new subtitle.
1932 my ($bibnum, $subtitle) = @_;
1933 my $dbh = C4::Context->dbh;
1934 &OLDmodsubtitle($dbh,$bibnum,$subtitle);
1939 &modaddauthor($biblionumber, $author);
1941 Replaces all additional authors for the book with biblio number
1942 C<$biblionumber> with C<$author>. If C<$author> is the empty string,
1943 C<&modaddauthor> deletes all additional authors.
1948 my ($bibnum, $author) = @_;
1949 my $dbh = C4::Context->dbh;
1950 &OLDmodaddauthor($dbh,$bibnum,$author);
1951 } # sub modaddauthor
1955 $error = &modsubject($biblionumber, $force, @subjects);
1957 $force - a subject to force
1959 $error - Error message, or undef if successful.
1964 my ($bibnum, $force, @subject) = @_;
1965 my $dbh = C4::Context->dbh;
1966 my $error= &OLDmodsubject($dbh,$bibnum,$force, @subject);
1971 my ($biblioitem) = @_;
1972 my $dbh = C4::Context->dbh;
1973 &OLDmodbibitem($dbh,$biblioitem);
1974 my $MARCbibitem = MARCkoha2marcBiblio($dbh,$biblioitem);
1975 &MARCmodbiblio($dbh,$biblioitem->{biblionumber},$MARCbibitem,0);
1979 my ($bibitemnum,$note)=@_;
1980 my $dbh = C4::Context->dbh;
1981 &OLDmodnote($dbh,$bibitemnum,$note);
1985 my ($biblioitem) = @_;
1986 my $dbh = C4::Context->dbh;
1987 my $bibitemnum = &OLDnewbiblioitem($dbh,$biblioitem);
1988 # print STDERR "bibitemnum : $bibitemnum\n";
1989 my $MARCbiblio= MARCkoha2marcBiblio($dbh,$biblioitem->{biblionumber},$bibitemnum);
1990 # print STDERR $MARCbiblio->as_formatted();
1991 &MARCaddbiblio($dbh,$MARCbiblio,$biblioitem->{biblionumber});
1992 return($bibitemnum);
1997 my $dbh = C4::Context->dbh;
1998 &OLDnewsubject($dbh,$bibnum);
2002 my ($bibnum, $subtitle) = @_;
2003 my $dbh = C4::Context->dbh;
2004 &OLDnewsubtitle($dbh,$bibnum,$subtitle);
2008 my ($item, @barcodes) = @_;
2009 my $dbh = C4::Context->dbh;
2013 foreach my $barcode (@barcodes) {
2014 ($itemnumber,$error)=&OLDnewitems($dbh,$item,uc($barcode));
2016 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{biblionumber},$itemnumber);
2017 &MARCadditem($dbh,$MARCitem,$item->{biblionumber});
2024 my $dbh = C4::Context->dbh;
2025 &OLDmoditem($dbh,$item);
2026 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{'biblionumber'},$item->{'itemnum'});
2027 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$item->{biblionumber});
2028 &MARCmoditem($dbh,$MARCitem,$bibid,$item->{itemnum},0);
2032 my ($count,@barcodes)=@_;
2033 my $dbh = C4::Context->dbh;
2035 for (my $i=0;$i<$count;$i++){
2036 $barcodes[$i]=uc $barcodes[$i];
2037 my $query="Select * from items where barcode='$barcodes[$i]'";
2038 my $sth=$dbh->prepare($query);
2040 if (my $data=$sth->fetchrow_hashref){
2041 $error.=" Duplicate Barcode: $barcodes[$i]";
2049 my ($bibitemnum)=@_;
2050 my $dbh = C4::Context->dbh;
2051 my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
2052 my $sth=$dbh->prepare($query);
2054 my $data=$sth->fetchrow_hashref;
2056 return($data->{'count(*)'});
2061 my $dbh = C4::Context->dbh;
2062 &OLDdelitem($dbh,$itemnum);
2065 sub deletebiblioitem {
2066 my ($biblioitemnumber) = @_;
2067 my $dbh = C4::Context->dbh;
2068 &OLDdeletebiblioitem($dbh,$biblioitemnumber);
2069 } # sub deletebiblioitem
2074 my $dbh = C4::Context->dbh;
2075 &OLDdelbiblio($dbh,$biblio);
2079 my $dbh = C4::Context->dbh;
2080 my $query = "select * from itemtypes order by description";
2081 my $sth = $dbh->prepare($query);
2082 # || die "Cannot prepare $query" . $dbh->errstr;
2087 # || die "Cannot execute $query\n" . $sth->errstr;
2088 while (my $data = $sth->fetchrow_hashref) {
2089 $results[$count] = $data;
2094 return($count, @results);
2095 } # sub getitemtypes
2098 my ($biblionumber) = @_;
2099 my $dbh = C4::Context->dbh;
2100 my $query = "Select * from biblio where biblionumber = $biblionumber";
2101 my $sth = $dbh->prepare($query);
2102 # || die "Cannot prepare $query\n" . $dbh->errstr;
2107 # || die "Cannot execute $query\n" . $sth->errstr;
2108 while (my $data = $sth->fetchrow_hashref) {
2109 $results[$count] = $data;
2114 return($count, @results);
2118 my ($biblioitemnum) = @_;
2119 my $dbh = C4::Context->dbh;
2120 my $query = "Select * from biblioitems where
2121 biblioitemnumber = $biblioitemnum";
2122 my $sth = $dbh->prepare($query);
2128 while (my $data = $sth->fetchrow_hashref) {
2129 $results[$count] = $data;
2134 return($count, @results);
2135 } # sub getbiblioitem
2137 sub getbiblioitembybiblionumber {
2138 my ($biblionumber) = @_;
2139 my $dbh = C4::Context->dbh;
2140 my $query = "Select * from biblioitems where biblionumber =
2142 my $sth = $dbh->prepare($query);
2148 while (my $data = $sth->fetchrow_hashref) {
2149 $results[$count] = $data;
2154 return($count, @results);
2157 sub getitemsbybiblioitem {
2158 my ($biblioitemnum) = @_;
2159 my $dbh = C4::Context->dbh;
2160 my $query = "Select * from items, biblio where
2161 biblio.biblionumber = items.biblionumber and biblioitemnumber
2163 my $sth = $dbh->prepare($query);
2164 # || die "Cannot prepare $query\n" . $dbh->errstr;
2169 # || die "Cannot execute $query\n" . $sth->errstr;
2170 while (my $data = $sth->fetchrow_hashref) {
2171 $results[$count] = $data;
2176 return($count, @results);
2177 } # sub getitemsbybiblioitem
2181 # Subroutine to log changes to databases
2182 # Eventually, this subroutine will be used to create a log of all changes made,
2183 # with the possibility of "undo"ing some changes
2185 if ($database eq 'kohadb') {
2191 # print STDERR "KOHA: $type $section $item $original $new\n";
2192 } elsif ($database eq 'marc') {
2194 my $Record_ID=shift;
2197 my $subfield_ID=shift;
2200 # print STDERR "MARC: $type $Record_ID $tag $mark $subfield_ID $original $new\n";
2204 #------------------------------------------------
2207 #---------------------------------------
2208 # Find a biblio entry, or create a new one if it doesn't exist.
2209 # If a "subtitle" entry is in hash, add it to subtitle table
2210 sub getoraddbiblio {
2214 # FIXME - Unused argument
2215 $biblio, # hash ref to fields
2226 $dbh = C4::Context->dbh;
2228 print "<PRE>Looking for biblio </PRE>\n" if $debug;
2229 $sth=$dbh->prepare("select biblionumber
2231 where title=? and author=?
2232 and copyrightdate=? and seriestitle=?");
2234 $biblio->{title}, $biblio->{author},
2235 $biblio->{copyright}, $biblio->{seriestitle} );
2237 ($biblionumber) = $sth->fetchrow;
2238 print "<PRE>Biblio exists with number $biblionumber</PRE>\n" if $debug;
2240 # Doesn't exist. Add new one.
2241 print "<PRE>Adding biblio</PRE>\n" if $debug;
2242 ($biblionumber,$error)=&newbiblio($biblio);
2243 if ( $biblionumber ) {
2244 print "<PRE>Added with biblio number=$biblionumber</PRE>\n" if $debug;
2245 if ( $biblio->{subtitle} ) {
2246 &newsubtitle($biblionumber,$biblio->{subtitle} );
2249 print "<PRE>Couldn't add biblio: $error</PRE>\n" if $debug;
2253 return $biblionumber,$error;
2255 } # sub getoraddbiblio
2258 # converts ISO 5426 coded string to ISO 8859-1
2259 # sloppy code : should be improved in next issue
2260 my ($string,$encoding) = @_ ;
2262 # $encoding = C4::Context->preference("marcflavour") unless $encoding;
2263 if ($encoding eq "UNIMARC") {
2325 # this handles non-sorting blocks (if implementation requires this)
2326 $string = nsb_clean($_) ;
2327 } elsif ($encoding eq "USMARC") {
2380 # this handles non-sorting blocks (if implementation requires this)
2381 $string = nsb_clean($_) ;
2388 my $NSB = '\x88' ; # NSB : begin Non Sorting Block
2389 my $NSE = '\x89' ; # NSE : Non Sorting Block end
2390 # handles non sorting blocks
2394 s/[ ]{0,1}$NSE/) /gm ;
2399 END { } # module clean-up code here (global destructor)
2405 Koha Developement team <info@koha.org>
2407 Paul POULAIN paul.poulain@free.fr