4 # Revision 1.57 2003/07/15 23:09:18 slef
5 # change show columns to use biblioitems bnotes too
7 # Revision 1.56 2003/07/15 11:34:52 slef
8 # fixes from paul email
10 # Revision 1.55 2003/07/15 00:02:49 slef
11 # Work on bug 515... can we do a single-side rename of notes to bnotes?
13 # Revision 1.54 2003/07/11 11:51:32 tipaul
14 # *** empty log message ***
16 # Revision 1.52 2003/07/10 10:37:19 tipaul
17 # fix for copyrightdate problem, #514
19 # Revision 1.51 2003/07/02 14:47:17 tipaul
20 # fix for #519 : items.dateaccessioned imports incorrectly
22 # Revision 1.49 2003/06/17 11:21:13 tipaul
23 # improvments/fixes for z3950 support.
24 # * Works now even on ADD, not only on MODIFY
25 # * able to search on ISBN, author, title
27 # Revision 1.48 2003/06/16 09:22:53 rangi
28 # Just added an order clause to getitemtypes
30 # Revision 1.47 2003/05/20 16:22:44 tipaul
31 # fixing typo in Biblio.pm POD
33 # Revision 1.46 2003/05/19 13:45:18 tipaul
34 # support for subtitles, additional authors, subject.
35 # 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.
36 # Note that some OLD-DB subs are strange (dummy ?) see OLDmodsubject, OLDmodsubtitle, OLDmodaddiauthor in C4/Biblio.pm
37 # 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.
39 # Revision 1.45 2003/04/29 16:50:49 tipaul
40 # really proud of this commit :-)
41 # z3950 search and import seems to works fine.
42 # Let me explain how :
43 # * a "search z3950" button is added in the addbiblio template.
44 # * when clicked, a popup appears and z3950/search.pl is called
45 # * z3950/search.pl calls addz3950search in the DB
46 # * the z3950 daemon retrieve the records and stores them in z3950results AND in marc_breeding table.
47 # * as long as there as searches pending, the popup auto refresh every 2 seconds, and says how many searches are pending.
48 # * when the user clicks on a z3950 result => the parent popup is called with the requested biblio, and auto-filled
51 # * 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.
52 # * 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.
54 # Revision 1.44 2003/04/28 13:07:14 tipaul
55 # Those fixes solves the "internal server error" with MARC::Record 1.12.
56 # It was due to an illegal contruction in Koha : we tried to retrive subfields from <10 tags.
57 # That's not possible. MARC::Record accepted this in 0.93 version, but it was fixed after.
58 # Now, the construct/retrieving is OK !
60 # Revision 1.43 2003/04/10 13:56:02 tipaul
62 # * worked in 1.9.0, but not in 1.9.1 :
63 # - modif of a biblio didn't work
64 # - 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.
66 # * did not work before :
67 # - repeatable subfields now works correctly. Enter 2 subfields separated by | and they will be splitted during saving.
68 # - dropped the last subfield of the MARC form :-(
71 # - 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.
72 # Note the MARCdelbiblio has been rewritted to enable deletion of a biblio WITHOUT deleting items.
74 # Revision 1.42 2003/04/04 08:41:11 tipaul
75 # last commits before 1.9.1
77 # Revision 1.41 2003/04/01 12:26:43 tipaul
80 # Revision 1.40 2003/03/11 15:14:03 tipaul
83 # Revision 1.39 2003/03/07 16:35:42 tipaul
84 # * moving generic functions to Koha.pm
85 # * improvement of SearchMarc.pm
89 # Revision 1.38 2003/02/27 16:51:59 tipaul
90 # * moving prepare / execute to ? form.
93 # * road to 1.9.2 => acquisition and cataloguing merging
95 # Revision 1.37 2003/02/12 11:03:03 tipaul
96 # Support for 000 -> 010 fields.
97 # Those fields doesn't have subfields.
98 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
99 # 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.
101 # Revision 1.36 2003/02/12 11:01:01 tipaul
102 # Support for 000 -> 010 fields.
103 # Those fields doesn't have subfields.
104 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
105 # 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.
107 # Revision 1.35 2003/02/03 18:46:00 acli
108 # Minor factoring in C4/Biblio.pm, plus change to export the per-tag
109 # 'mandatory' property to a per-subfield 'tag_mandatory' template parameter,
110 # so that addbiblio.tmpl can distinguish between mandatory subfields in a
111 # mandatory tag and mandatory subfields in an optional tag
113 # Not-minor factoring in acqui.simple/addbiblio.pl to make the if-else blocks
114 # smaller, and to add some POD; need further testing for this
116 # Added function to check if a MARC subfield name is "koha-internal" (instead
117 # of checking it for 'lib' and 'tag' everywhere); temporarily added to Koha.pm
119 # Use above function in acqui.simple/additem.pl and search.marc/search.pl
121 # Revision 1.34 2003/01/28 14:50:04 tipaul
122 # fixing MARCmodbiblio API and reindenting code
124 # Revision 1.33 2003/01/23 12:22:37 tipaul
125 # adding char_decode to decode MARC21 or UNIMARC extended chars
127 # Revision 1.32 2002/12/16 15:08:50 tipaul
128 # small but important bugfix (fixes a problem in export)
130 # Revision 1.31 2002/12/13 16:22:04 tipaul
131 # 1st draft of marc export
133 # Revision 1.30 2002/12/12 21:26:35 tipaul
134 # YAB ! (Yet Another Bugfix) => related to biblio modif
135 # (some warning cleaning too)
137 # Revision 1.29 2002/12/12 16:35:00 tipaul
138 # adding authentification with Auth.pm and
139 # MAJOR BUGFIX on marc biblio modification
141 # Revision 1.28 2002/12/10 13:30:03 tipaul
142 # fugfixes from Dombes Abbey work
144 # Revision 1.27 2002/11/19 12:36:16 tipaul
146 # various bugfixes, improvments, and migration from acquisition.pm to biblio.pm
148 # Revision 1.26 2002/11/12 15:58:43 tipaul
151 # * 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)
153 # Revision 1.25 2002/10/25 10:58:26 tipaul
155 # * bugfixes and improvements
157 # Revision 1.24 2002/10/24 12:09:01 arensb
158 # Fixed "no title" warning when generating HTML documentation from POD.
160 # Revision 1.23 2002/10/16 12:43:08 arensb
161 # Added some FIXME comments.
163 # Revision 1.22 2002/10/15 13:39:17 tipaul
164 # removing Acquisition.pm
165 # deleting unused code in biblio.pm, rewriting POD and answering most FIXME comments
167 # Revision 1.21 2002/10/13 11:34:14 arensb
168 # Replaced expressions of the form "$x = $x <op> $y" with "$x <op>= $y".
169 # Thus, $x = $x+2 becomes $x += 2, and so forth.
171 # Revision 1.20 2002/10/13 08:28:32 arensb
172 # Deleted unused variables.
173 # Removed trailing whitespace.
175 # Revision 1.19 2002/10/13 05:56:10 arensb
176 # Added some FIXME comments.
178 # Revision 1.18 2002/10/11 12:34:53 arensb
179 # Replaced &requireDBI with C4::Context->dbh
181 # Revision 1.17 2002/10/10 14:48:25 tipaul
184 # Revision 1.16 2002/10/07 14:04:26 tipaul
185 # road to 1.3.1 : viewing MARC biblio
187 # Revision 1.15 2002/10/05 09:49:25 arensb
188 # Merged with arensb-context branch: use C4::Context->dbh instead of
189 # &C4Connect, and generally prefer C4::Context over C4::Database.
191 # Revision 1.14 2002/10/03 11:28:18 tipaul
192 # Extending Context.pm to add stopword management and using it in MARC-API.
193 # First benchmarks show a medium speed improvement, which is nice as this part is heavily called.
195 # Revision 1.13 2002/10/02 16:26:44 tipaul
198 # Revision 1.12.2.4 2002/10/05 07:09:31 arensb
199 # Merged in changes from main branch.
201 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
202 # Added a whole mess of FIXME comments.
204 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
205 # Added some missing semicolons.
207 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
208 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
211 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
212 # Added a whole mess of FIXME comments.
214 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
215 # Added some missing semicolons.
217 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
218 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
221 # Revision 1.12 2002/10/01 11:48:51 arensb
222 # Added some FIXME comments, mostly marking duplicate functions.
224 # Revision 1.11 2002/09/24 13:49:26 tipaul
225 # long WAS the road to 1.3.0...
226 # coming VERY SOON NOW...
227 # modifying installer and buildrelease to update the DB
229 # Revision 1.10 2002/09/22 16:50:08 arensb
230 # Added some FIXME comments.
232 # Revision 1.9 2002/09/20 12:57:46 tipaul
233 # long is the road to 1.4.0
234 # * MARCadditem and MARCmoditem now wroks
235 # * various bugfixes in MARC management
236 # !!! 1.3.0 should be released very soon now. Be careful !!!
238 # Revision 1.8 2002/09/10 13:53:52 tipaul
239 # MARC API continued...
241 # * 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)
243 # Note : it should not be hard for marcimport and marcexport to re-link fields from internal tag/subfield to "legal" tag/subfield.
245 # Revision 1.7 2002/08/14 18:12:51 tonnesen
246 # Added copyright statement to all .pl and .pm files
248 # Revision 1.6 2002/07/25 13:40:31 tipaul
249 # pod documenting the API.
251 # Revision 1.5 2002/07/24 16:11:37 tipaul
253 # Database.pm and Output.pm are almost not modified (var test...)
255 # Biblio.pm is almost completly rewritten.
257 # WHAT DOES IT ??? ==> END of Hitchcock suspens
259 # 1st, it does... nothing...
260 # 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 ...
262 # All old-API functions have been cloned. for example, the "newbiblio" sub, now has become :
263 # * a "newbiblio" sub, with the same parameters. It just call a sub named OLDnewbiblio
264 # * 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.
265 # * 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.
266 # 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 ;-)
268 # 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.
269 # Note we have decided with steve that a old-biblio <=> a MARC-Biblio.
273 # Copyright 2000-2002 Katipo Communications
275 # This file is part of Koha.
277 # Koha is free software; you can redistribute it and/or modify it under the
278 # terms of the GNU General Public License as published by the Free Software
279 # Foundation; either version 2 of the License, or (at your option) any later
282 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
283 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
284 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
286 # You should have received a copy of the GNU General Public License along with
287 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
288 # Suite 330, Boston, MA 02111-1307 USA
296 use vars qw($VERSION @ISA @EXPORT);
298 # set the version for version checking
303 # don't forget MARCxxx subs are here only for testing purposes. Should not be used
304 # as the old-style API and the NEW one are the only public functions.
307 &updateBiblio &updateBiblioItem &updateItem
308 &itemcount &newbiblio &newbiblioitem
309 &modnote &newsubject &newsubtitle
310 &modbiblio &checkitems
311 &newitems &modbibitem
312 &modsubtitle &modsubject &modaddauthor &moditem &countitems
313 &delitem &deletebiblioitem &delbiblio
314 &getitemtypes &getbiblio
315 &getbiblioitembybiblionumber
316 &getbiblioitem &getitemsbybiblioitem
318 &newcompletebiblioitem
320 &MARCfind_oldbiblionumber_from_MARCbibid
321 &MARCfind_MARCbibid_from_oldbiblionumber
322 &MARCfind_marc_from_kohafield
326 &NEWnewbiblio &NEWnewitem
327 &NEWmodbiblio &NEWmoditem
329 &MARCaddbiblio &MARCadditem
330 &MARCmodsubfield &MARCaddsubfield
331 &MARCmodbiblio &MARCmoditem
332 &MARCkoha2marcBiblio &MARCmarc2koha
333 &MARCkoha2marcItem &MARChtml2marc
334 &MARCgetbiblio &MARCgetitem
335 &MARCaddword &MARCdelword
341 # MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC
344 # all the following subs takes a MARC::Record as parameter and manage
345 # the MARC-DB. They are called by the 1.0/1.2 xxx subs, and by the
346 # NEWxxx subs (xxx deals with old-DB parameters, the NEWxxx deals with MARC-DB parameter)
350 C4::Biblio - acquisition, catalog management functions
354 move from 1.2 to 1.4 version :
355 1.2 and previous version uses a specific API to manage biblios. This API uses old-DB style parameters.
356 In the 1.4 version, we want to do 2 differents things :
357 - keep populating the old-DB, that has a LOT less datas than MARC
358 - populate the MARC-DB
359 To populate the DBs we have 2 differents sources :
360 - the standard acquisition system (through book sellers), that does'nt use MARC data
361 - the MARC acquisition system, that uses MARC data.
363 Thus, we have 2 differents cases :
364 - 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
365 - 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
367 That's why we need 4 subs :
368 all I<subs beginning by MARC> manage only MARC tables. They manage MARC-DB with MARC::Record parameters
369 all I<subs beginning by OLD> manage only OLD-DB tables. They manage old-DB with old-DB parameters
370 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
371 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.
373 - NEW and old-style API should be used in koha to manage biblio
374 - MARCsubs are divided in 2 parts :
375 * some of them manage MARC parameters. They are heavily used in koha.
376 * some of them manage MARC biblio : they are mostly used by NEW and old-style subs.
377 - OLD are used internally only
379 all subs requires/use $dbh as 1st parameter.
381 I<NEWxxx related subs>
383 all subs requires/use $dbh as 1st parameter.
384 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
386 I<OLDxxx related subs>
388 all subs requires/use $dbh as 1st parameter.
389 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
391 They all are the exact copy of 1.0/1.2 version of the sub without the OLD.
392 The OLDxxx is called by the original xxx sub.
393 the 1.4 xxx sub also builds MARC::Record an calls the MARCxxx
395 WARNING : there is 1 difference between initialxxx and OLDxxx :
396 the db header $dbh is always passed as parameter to avoid over-DB connexion
402 =item @tagslib = &MARCgettagslib($dbh,1|0);
404 last param is 1 for liblibrarian and 0 for libopac
405 returns a hash with tag/subfield meaning
406 =item ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,$kohafield);
408 finds MARC tag and subfield for a given kohafield
409 kohafield is "table.field" where table= biblio|biblioitems|items, and field a field of the previous table
411 =item $biblionumber = &MARCfind_oldbiblionumber_from_MARCbibid($dbh,$MARCbibi);
413 finds a old-db biblio number for a given MARCbibid number
415 =item $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
417 finds a MARC bibid from a old-db biblionumber
419 =item $MARCRecord = &MARCkoha2marcBiblio($dbh,$biblionumber,biblioitemnumber);
421 MARCkoha2marcBiblio is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB biblio/biblioitem
423 =item $MARCRecord = &MARCkoha2marcItem($dbh,$biblionumber,itemnumber);
425 MARCkoha2marcItem is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB item
427 =item $MARCRecord = &MARCkoha2marcSubtitle($dbh,$biblionumber,$subtitle);
429 MARCkoha2marcSubtitle is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB subtitle
431 =item $olddb = &MARCmarc2koha($dbh,$MARCRecord);
433 builds a hash with old-db datas from a MARC::Record
435 =item &MARCaddbiblio($dbh,$MARC::Record,$biblionumber);
437 creates a biblio (in the MARC tables only). $biblionumber is the old-db biblionumber of the biblio
439 =item &MARCaddsubfield($dbh,$bibid,$tagid,$indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
441 adds a subfield in a biblio (in the MARC tables only).
443 =item $MARCRecord = &MARCgetbiblio($dbh,$bibid);
445 Returns a MARC::Record for the biblio $bibid.
447 =item &MARCmodbiblio($dbh,$bibid,$record,$delete);
449 MARCmodbiblio changes a biblio for a biblio,MARC::Record passed as parameter
450 It 1st delete the biblio, then recreates it.
451 WARNING : the $delete parameter is not used anymore (too much unsolvable cases).
452 =item ($subfieldid,$subfieldvalue) = &MARCmodsubfield($dbh,$subfieldid,$subfieldvalue);
454 MARCmodsubfield changes the value of a given subfield
456 =item $subfieldid = &MARCfindsubfield($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue);
458 MARCfindsubfield returns a subfield number given a bibid/tag/subfieldvalue values.
459 Returns -1 if more than 1 answer
461 =item $subfieldid = &MARCfindsubfieldid($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
463 MARCfindsubfieldid find a subfieldid for a bibid/tag/tagorder/subfield/subfieldorder
465 =item &MARCdelsubfield($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
467 MARCdelsubfield delete a subfield for a bibid/tag/tagorder/subfield/subfieldorder
469 =item &MARCdelbiblio($dbh,$bibid);
471 MARCdelbiblio delete biblio $bibid
473 =item &MARCkoha2marcOnefield
475 used by MARCkoha2marc and should not be useful elsewhere
477 =item &MARCmarc2kohaOnefield
479 used by MARCmarc2koha and should not be useful elsewhere
483 used to manage MARC_word table and should not be useful elsewhere
487 used to manage MARC_word table and should not be useful elsewhere
492 my ($dbh,$forlibrarian)= @_;
494 my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
495 $sth=$dbh->prepare("select tagfield,$libfield as lib,mandatory from marc_tag_structure order by tagfield");
497 my ($lib,$tag,$res,$tab,$mandatory,$repeatable);
498 while ( ($tag,$lib,$mandatory) = $sth->fetchrow) {
499 $res->{$tag}->{lib}=$lib;
500 $res->{$tab}->{tab}=""; # XXX
501 $res->{$tag}->{mandatory}=$mandatory;
504 $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");
508 my $authorised_value;
509 my $thesaurus_category;
512 while ( ($tag, $subfield, $lib, $tab, $mandatory, $repeatable,$authorised_value,$thesaurus_category,$value_builder,$kohafield) = $sth->fetchrow) {
513 $res->{$tag}->{$subfield}->{lib}=$lib;
514 $res->{$tag}->{$subfield}->{tab}=$tab;
515 $res->{$tag}->{$subfield}->{mandatory}=$mandatory;
516 $res->{$tag}->{$subfield}->{repeatable}=$repeatable;
517 $res->{$tag}->{$subfield}->{authorised_value}=$authorised_value;
518 $res->{$tag}->{$subfield}->{thesaurus_category}=$thesaurus_category;
519 $res->{$tag}->{$subfield}->{value_builder}=$value_builder;
520 $res->{$tag}->{$subfield}->{kohafield}=$kohafield;
525 sub MARCfind_marc_from_kohafield {
526 my ($dbh,$kohafield) = @_;
527 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
528 $sth->execute($kohafield);
529 my ($tagfield,$tagsubfield) = $sth->fetchrow;
530 return ($tagfield,$tagsubfield);
533 sub MARCfind_oldbiblionumber_from_MARCbibid {
534 my ($dbh,$MARCbibid) = @_;
535 my $sth=$dbh->prepare("select biblionumber from marc_biblio where bibid=?");
536 $sth->execute($MARCbibid);
537 my ($biblionumber) = $sth->fetchrow;
538 return $biblionumber;
541 sub MARCfind_MARCbibid_from_oldbiblionumber {
542 my ($dbh,$oldbiblionumber) = @_;
543 my $sth=$dbh->prepare("select bibid from marc_biblio where biblionumber=?");
544 $sth->execute($oldbiblionumber);
545 my ($bibid) = $sth->fetchrow;
550 # pass the MARC::Record to this function, and it will create the records in the marc tables
551 my ($dbh,$record,$biblionumber,$bibid) = @_;
552 my @fields=$record->fields();
554 # adding main table, and retrieving bibid
555 # if bibid is sent, then it's not a true add, it's only a re-add, after a delete (ie, a mod)
556 # if bibid empty => true add, find a new bibid number
558 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
559 my $sth=$dbh->prepare("insert into marc_biblio (datecreated,biblionumber) values (now(),?)");
560 $sth->execute($biblionumber);
561 $sth=$dbh->prepare("select max(bibid) from marc_biblio");
563 ($bibid)=$sth->fetchrow;
567 # now, add subfields...
568 foreach my $field (@fields) {
570 if ($field->tag() <10) {
571 &MARCaddsubfield($dbh,$bibid,
580 my @subfields=$field->subfields();
581 foreach my $subfieldcount (0..$#subfields) {
582 &MARCaddsubfield($dbh,$bibid,
584 $field->indicator(1).$field->indicator(2),
586 $subfields[$subfieldcount][0],
588 $subfields[$subfieldcount][1]
593 $dbh->do("unlock tables");
598 # pass the MARC::Record to this function, and it will create the records in the marc tables
599 my ($dbh,$record,$biblionumber) = @_;
600 # warn "adding : ".$record->as_formatted();
601 # search for MARC biblionumber
602 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
603 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
604 my @fields=$record->fields();
605 my $sth = $dbh->prepare("select max(tagorder) from marc_subfield_table where bibid=?");
606 $sth->execute($bibid);
607 my ($fieldcount) = $sth->fetchrow;
608 # now, add subfields...
609 foreach my $field (@fields) {
610 my @subfields=$field->subfields();
612 foreach my $subfieldcount (0..$#subfields) {
613 &MARCaddsubfield($dbh,$bibid,
615 $field->indicator(1).$field->indicator(2),
617 $subfields[$subfieldcount][0],
619 $subfields[$subfieldcount][1]
623 $dbh->do("unlock tables");
627 sub MARCaddsubfield {
628 # Add a new subfield to a tag into the DB.
629 my ($dbh,$bibid,$tagid,$tag_indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalues) = @_;
630 # if not value, end of job, we do nothing
631 if (length($subfieldvalues) ==0) {
634 if (not($subfieldcode)) {
637 my @subfieldvalues = split /\|/,$subfieldvalues;
638 foreach my $subfieldvalue (@subfieldvalues) {
639 if (length($subfieldvalue)>255) {
640 # $dbh->do("lock tables marc_blob_subfield WRITE, marc_subfield_table WRITE");
641 my $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
642 $sth->execute($subfieldvalue);
643 $sth=$dbh->prepare("select max(blobidlink)from marc_blob_subfield");
645 my ($res)=$sth->fetchrow;
646 $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,valuebloblink) values (?,?,?,?,?,?,?)");
647 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$res);
649 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";
651 # $dbh->do("unlock tables");
653 my $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?,?)");
654 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue);
656 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";
659 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
664 # Returns MARC::Record of the biblio passed in parameter.
666 my $record = MARC::Record->new();
667 #---- TODO : the leader is missing
668 $record->leader(' ');
669 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
670 from marc_subfield_table
671 where bibid=? order by tag,tagorder,subfieldcode
673 my $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
674 $sth->execute($bibid);
678 my $field; # for >=10 tags
679 my $prevvalue; # for <10 tags
680 while (my $row=$sth->fetchrow_hashref) {
681 if ($row->{'valuebloblink'}) { #---- search blob if there is one
682 $sth2->execute($row->{'valuebloblink'});
683 my $row2=$sth2->fetchrow_hashref;
685 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
687 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
690 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
692 $record->add_fields($field) unless $prevtag eq "XXX";
695 $prevtagorder=$row->{tagorder};
696 $prevtag = $row->{tag};
697 $previndicator=$row->{tag_indicator};
698 if ($row->{tag}<10) {
699 $prevvalue = $row->{subfieldvalue};
701 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.' ',0,1), substr($row->{tag_indicator}.' ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
704 if ($row->{tag} <10) {
705 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
707 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
709 $prevtag= $row->{tag};
710 $previndicator=$row->{tag_indicator};
713 # the last has not been included inside the loop... do it now !
715 $record->add_fields($prevtag,$prevvalue);
717 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
718 $record->add_fields($field);
723 # Returns MARC::Record of the biblio passed in parameter.
724 my ($dbh,$bibid,$itemnumber)=@_;
725 my $record = MARC::Record->new();
726 # search MARC tagorder
727 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=?");
728 $sth2->execute($bibid,$itemnumber);
729 my ($tagorder) = $sth2->fetchrow_array();
730 #---- TODO : the leader is missing
731 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
732 from marc_subfield_table
733 where bibid=? and tagorder=? order by subfieldcode,subfieldorder
735 $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
736 $sth->execute($bibid,$tagorder);
737 while (my $row=$sth->fetchrow_hashref) {
738 if ($row->{'valuebloblink'}) { #---- search blob if there is one
739 $sth2->execute($row->{'valuebloblink'});
740 my $row2=$sth2->fetchrow_hashref;
742 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
744 if ($record->field($row->{'tag'})) {
746 #--- this test must stay as this, because of strange behaviour of mySQL/Perl DBI with char var containing a number...
747 #--- sometimes, eliminates 0 at beginning, sometimes no ;-\\\
748 if (length($row->{'tag'}) <3) {
749 $row->{'tag'} = "0".$row->{'tag'};
751 $field =$record->field($row->{'tag'});
753 my $x = $field->add_subfields($row->{'subfieldcode'},$row->{'subfieldvalue'});
754 $record->delete_field($field);
755 $record->add_fields($field);
758 if (length($row->{'tag'}) < 3) {
759 $row->{'tag'} = "0".$row->{'tag'};
761 my $temp = MARC::Field->new($row->{'tag'}," "," ", $row->{'subfieldcode'} => $row->{'subfieldvalue'});
762 $record->add_fields($temp);
770 my ($dbh,$bibid,$record,$delete)=@_;
771 my $oldrecord=&MARCgetbiblio($dbh,$bibid);
772 if ($oldrecord eq $record) {
775 # 1st delete the biblio,
777 &MARCdelbiblio($dbh,$bibid,1);
778 my $biblionumber = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
779 &MARCaddbiblio($dbh,$record,$biblionumber,$bibid);
783 my ($dbh,$bibid,$keep_items) = @_;
784 # if the keep_item is set to 1, then all items are preserved.
785 # This flag is set when the delbiblio is called by modbiblio
786 # due to a too complex structure of MARC (repeatable fields and subfields),
787 # the best solution for a modif is to delete / recreate the record.
788 if ($keep_items eq 1) {
789 #search item field code
790 my $sth = $dbh->prepare("select tagfield from marc_subfield_structure where kohafield like 'items.%'");
792 my $itemtag = $sth->fetchrow_hashref->{tagfield};
793 $dbh->do("delete from marc_subfield_table where bibid=$bibid and tag<>$itemtag");
794 $dbh->do("delete from marc_word where bibid=$bibid and tag<>$itemtag");
796 $dbh->do("delete from marc_biblio where bibid=$bibid");
797 $dbh->do("delete from marc_subfield_table where bibid=$bibid");
798 $dbh->do("delete from marc_word where bibid=$bibid");
802 my ($dbh,$record,$bibid,$itemnumber,$delete)=@_;
803 my $oldrecord=&MARCgetitem($dbh,$bibid,$itemnumber);
804 # if nothing to change, don't waste time...
805 if ($oldrecord eq $record) {
809 # otherwise, skip through each subfield...
810 my @fields = $record->fields();
811 # search old MARC item
812 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=?");
813 $sth2->execute($bibid,$itemnumber);
814 my ($tagorder) = $sth2->fetchrow_array();
815 foreach my $field (@fields) {
816 my $oldfield = $oldrecord->field($field->tag());
817 my @subfields=$field->subfields();
819 foreach my $subfield (@subfields) {
821 # warn "compare : $oldfield".$oldfield->subfield(@$subfield[0]);
822 if ($oldfield eq 0 or (length($oldfield->subfield(@$subfield[0])) ==0) ) {
823 # just adding datas...
824 # warn "addfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
825 # warn "NEW subfield : $bibid,".$field->tag().",".$tagorder.",".@$subfield[0].",".$subfieldorder.",".@$subfield[1].")";
826 &MARCaddsubfield($dbh,$bibid,$field->tag(),$field->indicator(1).$field->indicator(2),
827 $tagorder,@$subfield[0],$subfieldorder,@$subfield[1]);
829 # warn "modfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
830 # modify he subfield if it's a different string
831 if ($oldfield->subfield(@$subfield[0]) ne @$subfield[1] ) {
832 my $subfieldid=&MARCfindsubfieldid($dbh,$bibid,$field->tag(),$tagorder,@$subfield[0],$subfieldorder);
833 # warn "changing : $subfieldid, $bibid,".$field->tag(),",$tagorder,@$subfield[0],@$subfield[1],$subfieldorder";
834 &MARCmodsubfield($dbh,$subfieldid,@$subfield[1]);
842 sub MARCmodsubfield {
843 # Subroutine changes a subfield value given a subfieldid.
844 my ($dbh, $subfieldid, $subfieldvalue )=@_;
845 $dbh->do("lock tables marc_blob_subfield WRITE,marc_subfield_table WRITE");
846 my $sth1=$dbh->prepare("select valuebloblink from marc_subfield_table where subfieldid=?");
847 $sth1->execute($subfieldid);
848 my ($oldvaluebloblink)=$sth1->fetchrow;
851 # if too long, use a bloblink
852 if (length($subfieldvalue)>255 ) {
853 # if already a bloblink, update it, otherwise, insert a new one.
854 if ($oldvaluebloblink) {
855 $sth=$dbh->prepare("update marc_blob_subfield set subfieldvalue=? where blobidlink=?");
856 $sth->execute($subfieldvalue,$oldvaluebloblink);
858 $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
859 $sth->execute($subfieldvalue);
860 $sth=$dbh->prepare("select max(blobidlink) from marc_blob_subfield");
862 my ($res)=$sth->fetchrow;
863 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=null, valuebloblink=$res where subfieldid=?");
864 $sth->execute($subfieldid);
867 # note this can leave orphan bloblink. Not a big problem, but we should build somewhere a orphan deleting script...
868 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=?,valuebloblink=null where subfieldid=?");
869 $sth->execute($subfieldvalue, $subfieldid);
871 $dbh->do("unlock tables");
873 $sth=$dbh->prepare("select bibid,tag,tagorder,subfieldcode,subfieldid,subfieldorder from marc_subfield_table where subfieldid=?");
874 $sth->execute($subfieldid);
875 my ($bibid,$tagid,$tagorder,$subfieldcode,$x,$subfieldorder) = $sth->fetchrow;
877 &MARCdelword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder);
878 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
879 return($subfieldid, $subfieldvalue);
882 sub MARCfindsubfield {
883 my ($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue) = @_;
887 my $query="select subfieldid from marc_subfield_table where bibid=? and tag=? and subfieldcode=?";
888 if ($subfieldvalue) {
889 $query .= " and subfieldvalue=".$dbh->quote($subfieldvalue);
891 if ($subfieldorder<1) {
894 $query .= " and subfieldorder=$subfieldorder";
896 my $sti=$dbh->prepare($query);
897 $sti->execute($bibid,$tag, $subfieldcode);
898 while (($subfieldid) = $sti->fetchrow) {
900 $lastsubfieldid=$subfieldid;
902 if ($resultcounter>1) {
903 # Error condition. Values given did not resolve into a unique record. Don't know what to edit
904 # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
907 return $lastsubfieldid;
911 sub MARCfindsubfieldid {
912 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
913 my $sth=$dbh->prepare("select subfieldid from marc_subfield_table
914 where bibid=? and tag=? and tagorder=?
915 and subfieldcode=? and subfieldorder=?");
916 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
917 my ($res) = $sth->fetchrow;
919 $sth=$dbh->prepare("select subfieldid from marc_subfield_table
920 where bibid=? and tag=? and tagorder=?
921 and subfieldcode=?");
922 $sth->execute($bibid,$tag,$tagorder,$subfield);
923 ($res) = $sth->fetchrow;
928 sub MARCdelsubfield {
929 # delete a subfield for $bibid / tag / tagorder / subfield / subfieldorder
930 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
931 $dbh->do("delete from marc_subfield_table where bibid='$bibid' and
932 tag='$tag' and tagorder='$tagorder'
933 and subfieldcode='$subfield' and subfieldorder='$subfieldorder
937 sub MARCkoha2marcBiblio {
938 # this function builds partial MARC::Record from the old koha-DB fields
939 my ($dbh,$biblionumber,$biblioitemnumber) = @_;
940 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
941 my $record = MARC::Record->new();
942 #--- if bibid, then retrieve old-style koha data
943 if ($biblionumber>0) {
944 my $sth2=$dbh->prepare("select biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp
945 from biblio where biblionumber=?");
946 $sth2->execute($biblionumber);
947 my $row=$sth2->fetchrow_hashref;
949 foreach $code (keys %$row) {
951 &MARCkoha2marcOnefield($sth,$record,"biblio.".$code,$row->{$code});
955 #--- if biblioitem, then retrieve old-style koha data
956 if ($biblioitemnumber>0) {
957 my $sth2=$dbh->prepare(" SELECT biblioitemnumber,biblionumber,volume,number,classification,
958 itemtype,url,isbn,issn,dewey,subclass,publicationyear,publishercode,
959 volumedate,volumeddesc,timestamp,illus,pages,notes AS bnotes,size,place
961 WHERE biblionumber=? and biblioitemnumber=?
963 $sth2->execute($biblionumber,$biblioitemnumber);
964 my $row=$sth2->fetchrow_hashref;
966 foreach $code (keys %$row) {
968 &MARCkoha2marcOnefield($sth,$record,"biblioitems.".$code,$row->{$code});
972 # other fields => additional authors, subjects, subtitles
973 my $sth2=$dbh->prepare(" SELECT author FROM additionalauthors WHERE biblionumber=?");
974 $sth2->execute($biblionumber);
975 while (my $row=$sth2->fetchrow_hashref) {
976 &MARCkoha2marcOnefield($sth,$record,"additionalauthors.author",$row->{'author'});
978 my $sth2=$dbh->prepare(" SELECT subject FROM bibliosubject WHERE biblionumber=?");
979 $sth2->execute($biblionumber);
980 while (my $row=$sth2->fetchrow_hashref) {
981 &MARCkoha2marcOnefield($sth,$record,"bibliosubject.subject",$row->{'subject'});
983 my $sth2=$dbh->prepare(" SELECT subtitle FROM bibliosubtitle WHERE biblionumber=?");
984 $sth2->execute($biblionumber);
985 while (my $row=$sth2->fetchrow_hashref) {
986 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.title",$row->{'subtitle'});
991 sub MARCkoha2marcItem {
992 # this function builds partial MARC::Record from the old koha-DB fields
993 my ($dbh,$biblionumber,$itemnumber) = @_;
994 # my $dbh=&C4Connect;
995 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
996 my $record = MARC::Record->new();
997 #--- if item, then retrieve old-style koha data
999 # print STDERR "prepare $biblionumber,$itemnumber\n";
1000 my $sth2=$dbh->prepare("SELECT itemnumber,biblionumber,multivolumepart,biblioitemnumber,barcode,dateaccessioned,
1001 booksellerid,homebranch,price,replacementprice,replacementpricedate,datelastborrowed,
1002 datelastseen,multivolume,stack,notforloan,itemlost,wthdrawn,bulk,issues,renewals,
1003 reserves,restricted,binding,itemnotes,holdingbranch,timestamp
1005 WHERE itemnumber=?");
1006 $sth2->execute($itemnumber);
1007 my $row=$sth2->fetchrow_hashref;
1009 foreach $code (keys %$row) {
1010 if ($row->{$code}) {
1011 &MARCkoha2marcOnefield($sth,$record,"items.".$code,$row->{$code});
1018 sub MARCkoha2marcSubtitle {
1019 # this function builds partial MARC::Record from the old koha-DB fields
1020 my ($dbh,$bibnum,$subtitle) = @_;
1021 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1022 my $record = MARC::Record->new();
1023 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.subtitle",$subtitle);
1027 sub MARCkoha2marcOnefield {
1028 my ($sth,$record,$kohafieldname,$value)=@_;
1031 $sth->execute($kohafieldname);
1032 if (($tagfield,$tagsubfield)=$sth->fetchrow) {
1033 if ($record->field($tagfield)) {
1034 my $tag =$record->field($tagfield);
1036 $tag->add_subfields($tagsubfield,$value);
1037 $record->delete_field($tag);
1038 $record->add_fields($tag);
1041 $record->add_fields($tagfield," "," ",$tagsubfield => $value);
1048 my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
1050 my $record = MARC::Record->new();
1051 # my %subfieldlist=();
1052 my $prevvalue; # if tag <10
1053 my $field; # if tag >=10
1054 for (my $i=0; $i< @$rtags; $i++) {
1055 # rebuild MARC::Record
1056 if (@$rtags[$i] ne $prevtag) {
1057 if ($prevtag < 10) {
1059 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
1063 $record->add_fields($field);
1066 $indicators{@$rtags[$i]}.=' ';
1067 if (@$rtags[$i] <10) {
1068 $prevvalue= @$rvalues[$i];
1070 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
1072 $prevtag = @$rtags[$i];
1074 if (@$rtags[$i] <10) {
1075 $prevvalue=@$rvalues[$i];
1077 if (@$rvalues[$i]) {
1078 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
1081 $prevtag= @$rtags[$i];
1084 # the last has not been included inside the loop... do it now !
1085 $record->add_fields($field);
1086 # warn $record->as_formatted;
1091 my ($dbh,$record) = @_;
1092 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1094 my $sth2=$dbh->prepare("SHOW COLUMNS from biblio");
1097 # print STDERR $record->as_formatted;
1098 while (($field)=$sth2->fetchrow) {
1099 $result=&MARCmarc2kohaOneField($sth,"biblio",$field,$record,$result);
1101 $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
1103 while (($field)=$sth2->fetchrow) {
1104 if ($field eq 'notes') { $field = 'bnotes'; }
1105 $result=&MARCmarc2kohaOneField($sth,"biblioitems",$field,$record,$result);
1107 $sth2=$dbh->prepare("SHOW COLUMNS from items");
1109 while (($field)=$sth2->fetchrow) {
1110 $result = &MARCmarc2kohaOneField($sth,"items",$field,$record,$result);
1112 # additional authors : specific
1113 $result = &MARCmarc2kohaOneField($sth,"additionalauthors","additionalauthors",$record,$result);
1114 # modify copyrightdate to keep only the 1st year found
1115 my $temp = $result->{'copyrightdate'};
1116 $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
1118 $result->{'copyrightdate'} = $1;
1119 } else { # if no cYYYY, get the 1st date.
1120 $temp =~ m/(\d\d\d\d)/;
1121 $result->{'copyrightdate'} = $1;
1126 sub MARCmarc2kohaOneField {
1127 # FIXME ? if a field has a repeatable subfield that is used in old-db, only the 1st will be retrieved...
1128 my ($sth,$kohatable,$kohafield,$record,$result)= @_;
1129 # warn "kohatable / $kohafield / $result / ";
1133 $sth->execute($kohatable.".".$kohafield);
1134 ($tagfield,$subfield) = $sth->fetchrow;
1135 foreach my $field ($record->field($tagfield)) {
1136 if ($field->subfield($subfield)) {
1137 if ($result->{$kohafield}) {
1138 $result->{$kohafield} .= " | ".$field->subfield($subfield);
1140 $result->{$kohafield}=$field->subfield($subfield);
1148 # split a subfield string and adds it into the word table.
1150 my ($dbh,$bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$sentence) =@_;
1151 $sentence =~ s/(\.|\?|\:|\!|\'|,|\-)/ /g;
1152 my @words = split / /,$sentence;
1153 my $stopwords= C4::Context->stopwords;
1154 my $sth=$dbh->prepare("insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word)
1155 values (?,?,?,?,?,?,soundex(?))");
1156 foreach my $word (@words) {
1157 # we record only words longer than 2 car and not in stopwords hash
1158 if (length($word)>1 and !($stopwords->{uc($word)})) {
1159 $sth->execute($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,$word);
1161 warn "ERROR ==> insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word) values ($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,soundex($word))\n";
1168 # delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
1169 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
1170 my $sth=$dbh->prepare("delete from marc_word where bibid=? and tag=? and tagorder=? and subfieldid=? and subfieldorder=?");
1171 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
1176 # NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
1179 # all the following subs are useful to manage MARC-DB with complete MARC records.
1180 # it's used with marcimport, and marc management tools
1184 =item ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbibilio($dbh,$MARCRecord,$oldbiblio,$oldbiblioitem);
1186 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
1187 are builded from the MARC::Record. If they are passed, they are used.
1189 =item NEWnewitem($dbh, $record,$bibid);
1191 adds an item in the db.
1196 my ($dbh, $record, $oldbiblio, $oldbiblioitem) = @_;
1197 # note $oldbiblio and $oldbiblioitem are not mandatory.
1198 # if not present, they will be builded from $record with MARCmarc2koha function
1199 if (($oldbiblio) and not($oldbiblioitem)) {
1200 print STDERR "NEWnewbiblio : missing parameter\n";
1201 print "NEWnewbiblio : missing parameter : contact koha development team\n";
1207 $oldbibnum = OLDnewbiblio($dbh,$oldbiblio);
1208 $oldbiblioitem->{'biblionumber'} = $oldbibnum;
1209 $oldbibitemnum = OLDnewbiblioitem($dbh,$oldbiblioitem);
1211 my $olddata = MARCmarc2koha($dbh,$record);
1212 $oldbibnum = OLDnewbiblio($dbh,$olddata);
1213 $olddata->{'biblionumber'} = $oldbibnum;
1214 $oldbibitemnum = OLDnewbiblioitem($dbh,$olddata);
1216 # search subtiles, addiauthors and subjects
1217 my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author");
1218 my @addiauthfields = $record->field($tagfield);
1219 foreach my $addiauthfield (@addiauthfields) {
1220 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1221 foreach my $subfieldcount (0..$#addiauthsubfields) {
1222 OLDmodaddauthor($dbh,$oldbibnum,$addiauthsubfields[$subfieldcount]);
1225 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle");
1226 my @subtitlefields = $record->field($tagfield);
1227 foreach my $subtitlefield (@subtitlefields) {
1228 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1229 foreach my $subfieldcount (0..$#subtitlesubfields) {
1230 OLDnewsubtitle($dbh,$oldbibnum,$subtitlesubfields[$subfieldcount]);
1233 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject");
1234 my @subj = $record->field($tagfield);
1235 foreach my $subject (@subj) {
1236 my @subjsubfield = $subject->subfield($tagsubfield);
1238 foreach my $subfieldcount (0..$#subjsubfield) {
1239 push @subjects,$subjsubfield[$subfieldcount];
1241 OLDmodsubject($dbh,$oldbibnum,1,@subjects);
1243 # we must add bibnum and bibitemnum in MARC::Record...
1244 # we build the new field with biblionumber and biblioitemnumber
1245 # we drop the original field
1246 # we add the new builded field.
1247 # NOTE : Works only if the field is ONLY for biblionumber and biblioitemnumber
1248 # (steve and paul : thinks 090 is a good choice)
1249 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1250 $sth->execute("biblio.biblionumber");
1251 (my $tagfield1, my $tagsubfield1) = $sth->fetchrow;
1252 $sth->execute("biblioitems.biblioitemnumber");
1253 (my $tagfield2, my $tagsubfield2) = $sth->fetchrow;
1254 if ($tagfield1 != $tagfield2) {
1255 warn "Error in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1256 print "Content-Type: text/html\n\nError in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1259 my $newfield = MARC::Field->new( $tagfield1,'','',
1260 "$tagsubfield1" => $oldbibnum,
1261 "$tagsubfield2" => $oldbibitemnum);
1262 # drop old field and create new one...
1263 my $old_field = $record->field($tagfield1);
1264 $record->delete_field($old_field);
1265 $record->add_fields($newfield);
1266 my $bibid = MARCaddbiblio($dbh,$record,$oldbibnum);
1267 return ($bibid,$oldbibnum,$oldbibitemnum );
1271 my ($dbh,$record,$bibid) =@_;
1272 &MARCmodbiblio($dbh,$bibid,$record,0);
1273 my $oldbiblio = MARCmarc2koha($dbh,$record);
1274 my $oldbiblionumber = OLDmodbiblio($dbh,$oldbiblio);
1275 OLDmodbibitem($dbh,$oldbiblio);
1276 # now, modify addi authors, subject, addititles.
1277 my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author");
1278 my @addiauthfields = $record->field($tagfield);
1279 foreach my $addiauthfield (@addiauthfields) {
1280 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1281 foreach my $subfieldcount (0..$#addiauthsubfields) {
1282 OLDmodaddauthor($dbh,$oldbiblionumber,$addiauthsubfields[$subfieldcount]);
1285 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle");
1286 my @subtitlefields = $record->field($tagfield);
1287 foreach my $subtitlefield (@subtitlefields) {
1288 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1289 foreach my $subfieldcount (0..$#subtitlesubfields) {
1290 OLDmodsubtitle($dbh,$oldbiblionumber,$subtitlesubfields[$subfieldcount]);
1293 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject");
1294 my @subj = $record->field($tagfield);
1295 foreach my $subject (@subj) {
1296 my @subjsubfield = $subject->subfield($tagsubfield);
1298 foreach my $subfieldcount (0..$#subjsubfield) {
1299 push @subjects,$subjsubfield[$subfieldcount];
1301 OLDmodsubject($dbh,$oldbiblionumber,1,@subjects);
1308 my ($dbh, $record,$bibid) = @_;
1309 # add item in old-DB
1310 my $item = &MARCmarc2koha($dbh,$record);
1311 # needs old biblionumber and biblioitemnumber
1312 $item->{'biblionumber'} = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
1313 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
1314 $sth->execute($item->{'biblionumber'});
1315 ($item->{'biblioitemnumber'}) = $sth->fetchrow;
1316 my ($itemnumber,$error) = &OLDnewitems($dbh,$item,$item->{barcode});
1317 # add itemnumber to MARC::Record before adding the item.
1318 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1319 &MARCkoha2marcOnefield($sth,$record,"items.itemnumber",$itemnumber);
1321 my $bib = &MARCadditem($dbh,$record,$item->{'biblionumber'});
1325 my ($dbh,$record,$bibid,$itemnumber,$delete) = @_;
1326 &MARCmoditem($dbh,$record,$bibid,$itemnumber,$delete);
1327 my $olditem = MARCmarc2koha($dbh,$record);
1328 OLDmoditem($dbh,$olditem);
1333 # OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
1337 =item $biblionumber = OLDnewbiblio($dbh,$biblio);
1339 adds a record in biblio table. Datas are in the hash $biblio.
1341 =item $biblionumber = OLDmodbiblio($dbh,$biblio);
1343 modify a record in biblio table. Datas are in the hash $biblio.
1345 =item OLDmodsubtitle($dbh,$bibnum,$subtitle);
1347 modify subtitles in bibliosubtitle table.
1349 =item OLDmodaddauthor($dbh,$bibnum,$author);
1351 adds or modify additional authors
1352 NOTE : Strange sub : seems to delete MANY and add only ONE author... maybe buggy ?
1354 =item $errors = OLDmodsubject($dbh,$bibnum, $force, @subject);
1356 modify/adds subjects
1358 =item OLDmodbibitem($dbh, $biblioitem);
1362 =item OLDmodnote($dbh,$bibitemnum,$note
1364 modify a note for a biblioitem
1366 =item OLDnewbiblioitem($dbh,$biblioitem);
1368 adds a biblioitem ($biblioitem is a hash with the values)
1370 =item OLDnewsubject($dbh,$bibnum);
1374 =item OLDnewsubtitle($dbh,$bibnum,$subtitle);
1376 create a new subtitle
1378 =item ($itemnumber,$errors)= OLDnewitems($dbh,$item,$barcode);
1380 create a item. $item is a hash and $barcode the barcode.
1382 =item OLDmoditem($dbh,$item);
1386 =item OLDdelitem($dbh,$itemnum);
1390 =item OLDdeletebiblioitem($dbh,$biblioitemnumber);
1392 deletes a biblioitem
1393 NOTE : not standard sub name. Should be OLDdelbiblioitem()
1395 =item OLDdelbiblio($dbh,$biblio);
1402 my ($dbh,$biblio) = @_;
1403 # my $dbh = &C4Connect;
1404 my $query = "Select max(biblionumber) from biblio";
1405 my $sth = $dbh->prepare($query);
1407 my $data = $sth->fetchrow_arrayref;
1408 my $bibnum = $$data[0] + 1;
1411 if ($biblio->{'seriestitle'}) { $series = 1 };
1413 $query = "insert into biblio set biblionumber = ?, title = ?, author = ?, copyrightdate = ?,
1414 serial = ?, seriestitle = ?, notes = ?, abstract = ?";
1415 $sth = $dbh->prepare($query);
1416 $sth->execute($bibnum,$biblio->{'title'},$biblio->{'author'},$biblio->{'copyright'},$series,$biblio->{'seriestitle'},$biblio->{'notes'},$biblio->{'abstract'});
1424 my ($dbh,$biblio) = @_;
1425 # my $dbh = C4Connect;
1429 $query = "Update biblio set title = ?, author = ?, abstract = ?, copyrightdate = ?,
1430 seriestitle = ?, serial = ?, unititle = ?, notes = ? where biblionumber = ?";
1431 $sth = $dbh->prepare($query);
1432 $sth->execute($biblio->{'title'},$biblio->{'author'},$biblio->{'abstract'},$biblio->{'copyrightdate'},
1433 $biblio->{'seriestitle'},$biblio->{'serial'},$biblio->{'unititle'},$biblio->{'notes'},$biblio->{'biblionumber'});
1436 return($biblio->{'biblionumber'});
1439 sub OLDmodsubtitle {
1440 my ($dbh,$bibnum, $subtitle) = @_;
1441 my $query = "update bibliosubtitle set subtitle = ? where biblionumber = ?";
1442 my $sth = $dbh->prepare($query);
1443 $sth->execute($subtitle,$bibnum);
1448 sub OLDmodaddauthor {
1449 my ($dbh,$bibnum, $author) = @_;
1450 # my $dbh = C4Connect;
1451 my $query = "Delete from additionalauthors where biblionumber = $bibnum";
1452 my $sth = $dbh->prepare($query);
1457 if ($author ne '') {
1458 $query = "Insert into additionalauthors set
1461 $sth = $dbh->prepare($query);
1463 $sth->execute($author,$bibnum);
1467 } # sub modaddauthor
1471 my ($dbh,$bibnum, $force, @subject) = @_;
1472 # my $dbh = C4Connect;
1473 my $count = @subject;
1475 for (my $i = 0; $i < $count; $i++) {
1476 $subject[$i] =~ s/^ //g;
1477 $subject[$i] =~ s/ $//g;
1478 my $query = "select * from catalogueentry where entrytype = 's' and catalogueentry = '$subject[$i]'";
1479 my $sth = $dbh->prepare($query);
1482 if (my $data = $sth->fetchrow_hashref) {
1484 if ($force eq $subject[$i] || $force eq 1) {
1485 # subject not in aut, chosen to force anway
1486 # so insert into cataloguentry so its in auth file
1487 $query = "Insert into catalogueentry (entrytype,catalogueentry) values ('s','$subject[$i]')";
1488 my $sth2 = $dbh->prepare($query);
1493 $error = "$subject[$i]\n does not exist in the subject authority file";
1494 $query = "Select * from catalogueentry where entrytype = 's' and (catalogueentry like '$subject[$i] %'
1495 or catalogueentry like '% $subject[$i] %' or catalogueentry like '% $subject[$i]')";
1496 my $sth2 = $dbh->prepare($query);
1498 while (my $data = $sth2->fetchrow_hashref) {
1499 $error .= "<br>$data->{'catalogueentry'}";
1507 my $query = "Delete from bibliosubject where biblionumber = $bibnum";
1508 my $sth = $dbh->prepare($query);
1511 for (my $i = 0; $i < $count; $i++) {
1512 $sth = $dbh->prepare("Insert into bibliosubject values ('$subject[$i]', $bibnum)");
1523 my ($dbh,$biblioitem) = @_;
1524 # my $dbh = C4Connect;
1527 $biblioitem->{'itemtype'} = $dbh->quote($biblioitem->{'itemtype'});
1528 $biblioitem->{'url'} = $dbh->quote($biblioitem->{'url'});
1529 $biblioitem->{'isbn'} = $dbh->quote($biblioitem->{'isbn'});
1530 $biblioitem->{'publishercode'} = $dbh->quote($biblioitem->{'publishercode'});
1531 $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
1532 $biblioitem->{'classification'} = $dbh->quote($biblioitem->{'classification'});
1533 $biblioitem->{'dewey'} = $dbh->quote($biblioitem->{'dewey'});
1534 $biblioitem->{'subclass'} = $dbh->quote($biblioitem->{'subclass'});
1535 $biblioitem->{'illus'} = $dbh->quote($biblioitem->{'illus'});
1536 $biblioitem->{'pages'} = $dbh->quote($biblioitem->{'pages'});
1537 $biblioitem->{'volumeddesc'} = $dbh->quote($biblioitem->{'volumeddesc'});
1538 $biblioitem->{'bnotes'} = $dbh->quote($biblioitem->{'bnotes'});
1539 $biblioitem->{'size'} = $dbh->quote($biblioitem->{'size'});
1540 $biblioitem->{'place'} = $dbh->quote($biblioitem->{'place'});
1542 $query = "Update biblioitems set
1543 itemtype = $biblioitem->{'itemtype'},
1544 url = $biblioitem->{'url'},
1545 isbn = $biblioitem->{'isbn'},
1546 publishercode = $biblioitem->{'publishercode'},
1547 publicationyear = $biblioitem->{'publicationyear'},
1548 classification = $biblioitem->{'classification'},
1549 dewey = $biblioitem->{'dewey'},
1550 subclass = $biblioitem->{'subclass'},
1551 illus = $biblioitem->{'illus'},
1552 pages = $biblioitem->{'pages'},
1553 volumeddesc = $biblioitem->{'volumeddesc'},
1554 notes = $biblioitem->{'bnotes'},
1555 size = $biblioitem->{'size'},
1556 place = $biblioitem->{'place'}
1557 where biblioitemnumber = $biblioitem->{'biblioitemnumber'}";
1565 my ($dbh,$bibitemnum,$note)=@_;
1566 # my $dbh=C4Connect;
1567 my $query="update biblioitems set notes='$note' where
1568 biblioitemnumber='$bibitemnum'";
1569 my $sth=$dbh->prepare($query);
1575 sub OLDnewbiblioitem {
1576 my ($dbh,$biblioitem) = @_;
1577 # my $dbh = C4Connect;
1578 my $query = "Select max(biblioitemnumber) from biblioitems";
1579 my $sth = $dbh->prepare($query);
1584 $data = $sth->fetchrow_arrayref;
1585 $bibitemnum = $$data[0] + 1;
1589 $sth = $dbh->prepare("insert into biblioitems set
1590 biblioitemnumber = ?, biblionumber = ?,
1591 volume = ?, number = ?,
1592 classification = ?, itemtype = ?,
1594 issn = ?, dewey = ?,
1595 subclass = ?, publicationyear = ?,
1596 publishercode = ?, volumedate = ?,
1597 volumeddesc = ?, illus = ?,
1598 pages = ?, notes = ?,
1600 marc = ?, place = ?");
1601 $sth->execute($bibitemnum, $biblioitem->{'biblionumber'},
1602 $biblioitem->{'volume'}, $biblioitem->{'number'},
1603 $biblioitem->{'classification'}, $biblioitem->{'itemtype'},
1604 $biblioitem->{'url'}, $biblioitem->{'isbn'},
1605 $biblioitem->{'issn'}, $biblioitem->{'dewey'},
1606 $biblioitem->{'subclass'}, $biblioitem->{'publicationyear'},
1607 $biblioitem->{'publishercode'}, $biblioitem->{'volumedate'},
1608 $biblioitem->{'volumeddesc'}, $biblioitem->{'illus'},
1609 $biblioitem->{'pages'}, $biblioitem->{'bnotes'},
1610 $biblioitem->{'size'}, $biblioitem->{'lccn'},
1611 $biblioitem->{'marc'}, $biblioitem->{'place'});
1614 return($bibitemnum);
1618 my ($dbh,$bibnum)=@_;
1619 my $query="insert into bibliosubject (biblionumber) values ($bibnum)";
1620 my $sth=$dbh->prepare($query);
1625 sub OLDnewsubtitle {
1626 my ($dbh,$bibnum, $subtitle) = @_;
1627 my $query = "insert into bibliosubtitle set biblionumber = ?, subtitle = ?";
1628 my $sth = $dbh->prepare($query);
1629 $sth->execute($bibnum,$subtitle);
1635 my ($dbh,$item, $barcode) = @_;
1636 # my $dbh = C4Connect;
1637 my $query = "Select max(itemnumber) from items";
1638 my $sth = $dbh->prepare($query);
1644 $data = $sth->fetchrow_hashref;
1645 $itemnumber = $data->{'max(itemnumber)'} + 1;
1647 # if dateaccessioned is provided, use it. Otherwise, set to NOW()
1648 if ($item->{'dateaccessioned'}) {
1649 $sth=$dbh->prepare("Insert into items set
1650 itemnumber = ?, biblionumber = ?,
1651 biblioitemnumber = ?, barcode = ?,
1652 booksellerid = ?, dateaccessioned = ?,
1653 homebranch = ?, holdingbranch = ?,
1654 price = ?, replacementprice = ?,
1655 replacementpricedate = NOW(), itemnotes = ?,
1658 $sth->execute($itemnumber, $item->{'biblionumber'},
1659 $item->{'biblioitemnumber'},$barcode,
1660 $item->{'booksellerid'},$item->{'dateaccessioned'},
1661 $item->{'homebranch'},$item->{'holdingbranch'},
1662 $item->{'price'},$item->{'replacementprice'},
1663 $item->{'itemnotes'},$item->{'loan'});
1665 $sth=$dbh->prepare("Insert into items set
1666 itemnumber = ?, biblionumber = ?,
1667 biblioitemnumber = ?, barcode = ?,
1668 booksellerid = ?, dateaccessioned = NOW(),
1669 homebranch = ?, holdingbranch = ?,
1670 price = ?, replacementprice = ?,
1671 replacementpricedate = NOW(), itemnotes = ?,
1674 $sth->execute($itemnumber, $item->{'biblionumber'},
1675 $item->{'biblioitemnumber'},$barcode,
1676 $item->{'booksellerid'},
1677 $item->{'homebranch'},$item->{'holdingbranch'},
1678 $item->{'price'},$item->{'replacementprice'},
1679 $item->{'itemnotes'},$item->{'loan'});
1681 if (defined $sth->errstr) {
1682 $error .= $sth->errstr;
1685 return($itemnumber,$error);
1689 my ($dbh,$item) = @_;
1690 # my ($dbh,$loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
1691 # my $dbh=C4Connect;
1692 $item->{'itemnum'}=$item->{'itemnumber'} unless $item->{'itemnum'};
1693 my $query="update items set barcode='$item->{'barcode'}',itemnotes='$item->{'notes'}'
1694 where itemnumber=$item->{'itemnum'}";
1695 if ($item->{'barcode'} eq ''){
1696 $query="update items set notforloan=$item->{'loan'} where itemnumber=$item->{'itemnum'}";
1698 if ($item->{'lost'} ne ''){
1699 $query="update items set biblioitemnumber=$item->{'bibitemnum'},
1700 barcode='$item->{'barcode'}',
1701 itemnotes='$item->{'notes'}',
1702 homebranch='$item->{'homebranch'}',
1703 itemlost='$item->{'lost'}',
1704 wthdrawn='$item->{'wthdrawn'}'
1705 where itemnumber=$item->{'itemnum'}";
1707 if ($item->{'replacement'} ne ''){
1708 $query=~ s/ where/,replacementprice='$item->{'replacement'}' where/;
1710 my $sth=$dbh->prepare($query);
1717 my ($dbh,$itemnum)=@_;
1718 # my $dbh=C4Connect;
1719 my $query="select * from items where itemnumber=$itemnum";
1720 my $sth=$dbh->prepare($query);
1722 my @data=$sth->fetchrow_array;
1724 $query="Insert into deleteditems values (";
1725 foreach my $temp (@data){
1726 $query .= "'$temp',";
1730 $sth=$dbh->prepare($query);
1733 $query = "Delete from items where itemnumber=$itemnum";
1734 $sth=$dbh->prepare($query);
1740 sub OLDdeletebiblioitem {
1741 my ($dbh,$biblioitemnumber) = @_;
1742 # my $dbh = C4Connect;
1743 my $query = "Select * from biblioitems
1744 where biblioitemnumber = $biblioitemnumber";
1745 my $sth = $dbh->prepare($query);
1750 if (@results = $sth->fetchrow_array) {
1751 $query = "Insert into deletedbiblioitems values (";
1752 foreach my $value (@results) {
1753 $value = $dbh->quote($value);
1754 $query .= "$value,";
1757 $query =~ s/\,$/\)/;
1760 $query = "Delete from biblioitems
1761 where biblioitemnumber = $biblioitemnumber";
1765 # Now delete all the items attached to the biblioitem
1766 $query = "Select * from items where biblioitemnumber = $biblioitemnumber";
1767 $sth = $dbh->prepare($query);
1769 while (@results = $sth->fetchrow_array) {
1770 $query = "Insert into deleteditems values (";
1771 foreach my $value (@results) {
1772 $value = $dbh->quote($value);
1773 $query .= "$value,";
1775 $query =~ s/\,$/\)/;
1779 $query = "Delete from items where biblioitemnumber = $biblioitemnumber";
1782 } # sub deletebiblioitem
1785 my ($dbh,$biblio)=@_;
1786 # my $dbh=C4Connect;
1787 my $query="select * from biblio where biblionumber=$biblio";
1788 my $sth=$dbh->prepare($query);
1790 if (my @data=$sth->fetchrow_array){
1792 $query="Insert into deletedbiblio values (";
1793 foreach my $temp (@data){
1794 $temp=~ s/\'/\\\'/g;
1795 $query .= "'$temp',";
1799 $sth=$dbh->prepare($query);
1802 $query = "Delete from biblio where biblionumber=$biblio";
1803 $sth=$dbh->prepare($query);
1819 my $dbh = C4::Context->dbh;
1820 my $query="Select count(*) from items where biblionumber=$biblio";
1822 my $sth=$dbh->prepare($query);
1824 my $data=$sth->fetchrow_hashref;
1826 return($data->{'count(*)'});
1831 ($order, $ordernumber) = &getorder($biblioitemnumber, $biblionumber);
1833 Looks up the order with the given biblionumber and biblioitemnumber.
1835 Returns a two-element array. C<$ordernumber> is the order number.
1836 C<$order> is a reference-to-hash describing the order; its keys are
1837 fields from the biblio, biblioitems, aqorders, and aqorderbreakdown
1838 tables of the Koha database.
1842 # FIXME - This is effectively identical to &C4::Catalogue::getorder.
1843 # Pick one and stick with it.
1846 my $dbh = C4::Context->dbh;
1847 my $query="Select ordernumber
1849 where biblionumber=? and biblioitemnumber=?";
1850 my $sth=$dbh->prepare($query);
1851 $sth->execute($bib,$bi);
1852 # FIXME - Use fetchrow_array(), since we're only interested in the one
1854 my $ordnum=$sth->fetchrow_hashref;
1856 my $order=getsingleorder($ordnum->{'ordernumber'});
1858 return ($order,$ordnum->{'ordernumber'});
1861 =item getsingleorder
1863 $order = &getsingleorder($ordernumber);
1865 Looks up an order by order number.
1867 Returns a reference-to-hash describing the order. The keys of
1868 C<$order> are fields from the biblio, biblioitems, aqorders, and
1869 aqorderbreakdown tables of the Koha database.
1873 # FIXME - This is effectively identical to
1874 # &C4::Catalogue::getsingleorder.
1875 # Pick one and stick with it.
1876 sub getsingleorder {
1878 my $dbh = C4::Context->dbh;
1879 my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown
1880 where aqorders.ordernumber=?
1881 and biblio.biblionumber=aqorders.biblionumber and
1882 biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
1883 aqorders.ordernumber=aqorderbreakdown.ordernumber";
1884 my $sth=$dbh->prepare($query);
1885 $sth->execute($ordnum);
1886 my $data=$sth->fetchrow_hashref;
1893 my $dbh = C4::Context->dbh;
1894 my $bibnum=OLDnewbiblio($dbh,$biblio);
1895 # finds new (MARC bibid
1896 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$bibnum);
1897 my $record = &MARCkoha2marcBiblio($dbh,$bibnum);
1898 MARCaddbiblio($dbh,$record,$bibnum);
1905 $biblionumber = &modbiblio($biblio);
1907 Update a biblio record.
1909 C<$biblio> is a reference-to-hash whose keys are the fields in the
1910 biblio table in the Koha database. All fields must be present, not
1911 just the ones you wish to change.
1913 C<&modbiblio> updates the record defined by
1914 C<$biblio-E<gt>{biblionumber}> with the values in C<$biblio>.
1916 C<&modbiblio> returns C<$biblio-E<gt>{biblionumber}> whether it was
1923 my $dbh = C4::Context->dbh;
1924 my $biblionumber=OLDmodbiblio($dbh,$biblio);
1925 my $record = MARCkoha2marcBiblio($dbh,$biblionumber);
1926 # finds new (MARC bibid
1927 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
1928 MARCmodbiblio($dbh,$bibid,$record,0);
1929 return($biblionumber);
1934 &modsubtitle($biblionumber, $subtitle);
1936 Sets the subtitle of a book.
1938 C<$biblionumber> is the biblionumber of the book to modify.
1940 C<$subtitle> is the new subtitle.
1945 my ($bibnum, $subtitle) = @_;
1946 my $dbh = C4::Context->dbh;
1947 &OLDmodsubtitle($dbh,$bibnum,$subtitle);
1952 &modaddauthor($biblionumber, $author);
1954 Replaces all additional authors for the book with biblio number
1955 C<$biblionumber> with C<$author>. If C<$author> is the empty string,
1956 C<&modaddauthor> deletes all additional authors.
1961 my ($bibnum, $author) = @_;
1962 my $dbh = C4::Context->dbh;
1963 &OLDmodaddauthor($dbh,$bibnum,$author);
1964 } # sub modaddauthor
1968 $error = &modsubject($biblionumber, $force, @subjects);
1970 $force - a subject to force
1972 $error - Error message, or undef if successful.
1977 my ($bibnum, $force, @subject) = @_;
1978 my $dbh = C4::Context->dbh;
1979 my $error= &OLDmodsubject($dbh,$bibnum,$force, @subject);
1984 my ($biblioitem) = @_;
1985 my $dbh = C4::Context->dbh;
1986 &OLDmodbibitem($dbh,$biblioitem);
1987 my $MARCbibitem = MARCkoha2marcBiblio($dbh,$biblioitem);
1988 &MARCmodbiblio($dbh,$biblioitem->{biblionumber},$MARCbibitem,0);
1992 my ($bibitemnum,$note)=@_;
1993 my $dbh = C4::Context->dbh;
1994 &OLDmodnote($dbh,$bibitemnum,$note);
1998 my ($biblioitem) = @_;
1999 my $dbh = C4::Context->dbh;
2000 my $bibitemnum = &OLDnewbiblioitem($dbh,$biblioitem);
2001 # print STDERR "bibitemnum : $bibitemnum\n";
2002 my $MARCbiblio= MARCkoha2marcBiblio($dbh,$biblioitem->{biblionumber},$bibitemnum);
2003 # print STDERR $MARCbiblio->as_formatted();
2004 &MARCaddbiblio($dbh,$MARCbiblio,$biblioitem->{biblionumber});
2005 return($bibitemnum);
2010 my $dbh = C4::Context->dbh;
2011 &OLDnewsubject($dbh,$bibnum);
2015 my ($bibnum, $subtitle) = @_;
2016 my $dbh = C4::Context->dbh;
2017 &OLDnewsubtitle($dbh,$bibnum,$subtitle);
2021 my ($item, @barcodes) = @_;
2022 my $dbh = C4::Context->dbh;
2026 foreach my $barcode (@barcodes) {
2027 ($itemnumber,$error)=&OLDnewitems($dbh,$item,uc($barcode));
2029 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{biblionumber},$itemnumber);
2030 &MARCadditem($dbh,$MARCitem,$item->{biblionumber});
2037 my $dbh = C4::Context->dbh;
2038 &OLDmoditem($dbh,$item);
2039 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{'biblionumber'},$item->{'itemnum'});
2040 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$item->{biblionumber});
2041 &MARCmoditem($dbh,$MARCitem,$bibid,$item->{itemnum},0);
2045 my ($count,@barcodes)=@_;
2046 my $dbh = C4::Context->dbh;
2048 for (my $i=0;$i<$count;$i++){
2049 $barcodes[$i]=uc $barcodes[$i];
2050 my $query="Select * from items where barcode='$barcodes[$i]'";
2051 my $sth=$dbh->prepare($query);
2053 if (my $data=$sth->fetchrow_hashref){
2054 $error.=" Duplicate Barcode: $barcodes[$i]";
2062 my ($bibitemnum)=@_;
2063 my $dbh = C4::Context->dbh;
2064 my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
2065 my $sth=$dbh->prepare($query);
2067 my $data=$sth->fetchrow_hashref;
2069 return($data->{'count(*)'});
2074 my $dbh = C4::Context->dbh;
2075 &OLDdelitem($dbh,$itemnum);
2078 sub deletebiblioitem {
2079 my ($biblioitemnumber) = @_;
2080 my $dbh = C4::Context->dbh;
2081 &OLDdeletebiblioitem($dbh,$biblioitemnumber);
2082 } # sub deletebiblioitem
2087 my $dbh = C4::Context->dbh;
2088 &OLDdelbiblio($dbh,$biblio);
2092 my $dbh = C4::Context->dbh;
2093 my $query = "select * from itemtypes order by description";
2094 my $sth = $dbh->prepare($query);
2095 # || die "Cannot prepare $query" . $dbh->errstr;
2100 # || die "Cannot execute $query\n" . $sth->errstr;
2101 while (my $data = $sth->fetchrow_hashref) {
2102 $results[$count] = $data;
2107 return($count, @results);
2108 } # sub getitemtypes
2111 my ($biblionumber) = @_;
2112 my $dbh = C4::Context->dbh;
2113 my $query = "Select * from biblio where biblionumber = $biblionumber";
2114 my $sth = $dbh->prepare($query);
2115 # || die "Cannot prepare $query\n" . $dbh->errstr;
2120 # || die "Cannot execute $query\n" . $sth->errstr;
2121 while (my $data = $sth->fetchrow_hashref) {
2122 $results[$count] = $data;
2127 return($count, @results);
2131 my ($biblioitemnum) = @_;
2132 my $dbh = C4::Context->dbh;
2133 my $query = "Select * from biblioitems where
2134 biblioitemnumber = $biblioitemnum";
2135 my $sth = $dbh->prepare($query);
2141 while (my $data = $sth->fetchrow_hashref) {
2142 $results[$count] = $data;
2147 return($count, @results);
2148 } # sub getbiblioitem
2150 sub getbiblioitembybiblionumber {
2151 my ($biblionumber) = @_;
2152 my $dbh = C4::Context->dbh;
2153 my $query = "Select * from biblioitems where biblionumber =
2155 my $sth = $dbh->prepare($query);
2161 while (my $data = $sth->fetchrow_hashref) {
2162 $results[$count] = $data;
2167 return($count, @results);
2170 sub getitemsbybiblioitem {
2171 my ($biblioitemnum) = @_;
2172 my $dbh = C4::Context->dbh;
2173 my $query = "Select * from items, biblio where
2174 biblio.biblionumber = items.biblionumber and biblioitemnumber
2176 my $sth = $dbh->prepare($query);
2177 # || die "Cannot prepare $query\n" . $dbh->errstr;
2182 # || die "Cannot execute $query\n" . $sth->errstr;
2183 while (my $data = $sth->fetchrow_hashref) {
2184 $results[$count] = $data;
2189 return($count, @results);
2190 } # sub getitemsbybiblioitem
2194 # Subroutine to log changes to databases
2195 # Eventually, this subroutine will be used to create a log of all changes made,
2196 # with the possibility of "undo"ing some changes
2198 if ($database eq 'kohadb') {
2204 # print STDERR "KOHA: $type $section $item $original $new\n";
2205 } elsif ($database eq 'marc') {
2207 my $Record_ID=shift;
2210 my $subfield_ID=shift;
2213 # print STDERR "MARC: $type $Record_ID $tag $mark $subfield_ID $original $new\n";
2217 #------------------------------------------------
2220 #---------------------------------------
2221 # Find a biblio entry, or create a new one if it doesn't exist.
2222 # If a "subtitle" entry is in hash, add it to subtitle table
2223 sub getoraddbiblio {
2227 # FIXME - Unused argument
2228 $biblio, # hash ref to fields
2239 $dbh = C4::Context->dbh;
2241 print "<PRE>Looking for biblio </PRE>\n" if $debug;
2242 $sth=$dbh->prepare("select biblionumber
2244 where title=? and author=?
2245 and copyrightdate=? and seriestitle=?");
2247 $biblio->{title}, $biblio->{author},
2248 $biblio->{copyright}, $biblio->{seriestitle} );
2250 ($biblionumber) = $sth->fetchrow;
2251 print "<PRE>Biblio exists with number $biblionumber</PRE>\n" if $debug;
2253 # Doesn't exist. Add new one.
2254 print "<PRE>Adding biblio</PRE>\n" if $debug;
2255 ($biblionumber,$error)=&newbiblio($biblio);
2256 if ( $biblionumber ) {
2257 print "<PRE>Added with biblio number=$biblionumber</PRE>\n" if $debug;
2258 if ( $biblio->{subtitle} ) {
2259 &newsubtitle($biblionumber,$biblio->{subtitle} );
2262 print "<PRE>Couldn't add biblio: $error</PRE>\n" if $debug;
2266 return $biblionumber,$error;
2268 } # sub getoraddbiblio
2271 # converts ISO 5426 coded string to ISO 8859-1
2272 # sloppy code : should be improved in next issue
2273 my ($string,$encoding) = @_ ;
2275 # $encoding = C4::Context->preference("marcflavour") unless $encoding;
2276 if ($encoding eq "UNIMARC") {
2338 # this handles non-sorting blocks (if implementation requires this)
2339 $string = nsb_clean($_) ;
2340 } elsif ($encoding eq "USMARC") {
2393 # this handles non-sorting blocks (if implementation requires this)
2394 $string = nsb_clean($_) ;
2401 my $NSB = '\x88' ; # NSB : begin Non Sorting Block
2402 my $NSE = '\x89' ; # NSE : Non Sorting Block end
2403 # handles non sorting blocks
2407 s/[ ]{0,1}$NSE/) /gm ;
2412 END { } # module clean-up code here (global destructor)
2418 Koha Developement team <info@koha.org>
2420 Paul POULAIN paul.poulain@free.fr