4 # Revision 1.62 2003/09/17 14:21:13 tipaul
5 # fixing bug that makes a MARC biblio disappear when using full acquisition (order => recieve ==> MARC editor).
6 # Before this 2 lines fix, the MARC biblio was deleted during recieve, and had to be entirely recreated :-(
8 # Revision 1.61 2003/09/17 10:24:39 tipaul
9 # notforloan value in itemtype was overwritting notforloan value in a given item.
10 # I changed this behaviour :
11 # if notforloan is set for a given item, and NOT for all items from this itemtype, the notforloan is kept.
12 # If notforloan is set for itemtype, it's used (and impossible to loan a specific item from this itemtype)
14 # Revision 1.60 2003/09/04 14:11:23 tipaul
15 # fix for 593 (data duplication in MARC-DB)
17 # Revision 1.58 2003/08/06 12:54:52 tipaul
18 # fix for publicationyear : extracting numeric value from MARC string, like for copyrightdate.
19 # (note that copyrightdate still extracted to get numeric format)
21 # Revision 1.57 2003/07/15 23:09:18 slef
22 # change show columns to use biblioitems bnotes too
24 # Revision 1.56 2003/07/15 11:34:52 slef
25 # fixes from paul email
27 # Revision 1.55 2003/07/15 00:02:49 slef
28 # Work on bug 515... can we do a single-side rename of notes to bnotes?
30 # Revision 1.54 2003/07/11 11:51:32 tipaul
31 # *** empty log message ***
33 # Revision 1.52 2003/07/10 10:37:19 tipaul
34 # fix for copyrightdate problem, #514
36 # Revision 1.51 2003/07/02 14:47:17 tipaul
37 # fix for #519 : items.dateaccessioned imports incorrectly
39 # Revision 1.49 2003/06/17 11:21:13 tipaul
40 # improvments/fixes for z3950 support.
41 # * Works now even on ADD, not only on MODIFY
42 # * able to search on ISBN, author, title
44 # Revision 1.48 2003/06/16 09:22:53 rangi
45 # Just added an order clause to getitemtypes
47 # Revision 1.47 2003/05/20 16:22:44 tipaul
48 # fixing typo in Biblio.pm POD
50 # Revision 1.46 2003/05/19 13:45:18 tipaul
51 # support for subtitles, additional authors, subject.
52 # 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.
53 # Note that some OLD-DB subs are strange (dummy ?) see OLDmodsubject, OLDmodsubtitle, OLDmodaddiauthor in C4/Biblio.pm
54 # 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.
56 # Revision 1.45 2003/04/29 16:50:49 tipaul
57 # really proud of this commit :-)
58 # z3950 search and import seems to works fine.
59 # Let me explain how :
60 # * a "search z3950" button is added in the addbiblio template.
61 # * when clicked, a popup appears and z3950/search.pl is called
62 # * z3950/search.pl calls addz3950search in the DB
63 # * the z3950 daemon retrieve the records and stores them in z3950results AND in marc_breeding table.
64 # * as long as there as searches pending, the popup auto refresh every 2 seconds, and says how many searches are pending.
65 # * when the user clicks on a z3950 result => the parent popup is called with the requested biblio, and auto-filled
68 # * 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.
69 # * 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.
71 # Revision 1.44 2003/04/28 13:07:14 tipaul
72 # Those fixes solves the "internal server error" with MARC::Record 1.12.
73 # It was due to an illegal contruction in Koha : we tried to retrive subfields from <10 tags.
74 # That's not possible. MARC::Record accepted this in 0.93 version, but it was fixed after.
75 # Now, the construct/retrieving is OK !
77 # Revision 1.43 2003/04/10 13:56:02 tipaul
79 # * worked in 1.9.0, but not in 1.9.1 :
80 # - modif of a biblio didn't work
81 # - 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.
83 # * did not work before :
84 # - repeatable subfields now works correctly. Enter 2 subfields separated by | and they will be splitted during saving.
85 # - dropped the last subfield of the MARC form :-(
88 # - 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.
89 # Note the MARCdelbiblio has been rewritted to enable deletion of a biblio WITHOUT deleting items.
91 # Revision 1.42 2003/04/04 08:41:11 tipaul
92 # last commits before 1.9.1
94 # Revision 1.41 2003/04/01 12:26:43 tipaul
97 # Revision 1.40 2003/03/11 15:14:03 tipaul
100 # Revision 1.39 2003/03/07 16:35:42 tipaul
101 # * moving generic functions to Koha.pm
102 # * improvement of SearchMarc.pm
106 # Revision 1.38 2003/02/27 16:51:59 tipaul
107 # * moving prepare / execute to ? form.
110 # * road to 1.9.2 => acquisition and cataloguing merging
112 # Revision 1.37 2003/02/12 11:03:03 tipaul
113 # Support for 000 -> 010 fields.
114 # Those fields doesn't have subfields.
115 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
116 # 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.
118 # Revision 1.36 2003/02/12 11:01:01 tipaul
119 # Support for 000 -> 010 fields.
120 # Those fields doesn't have subfields.
121 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
122 # 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.
124 # Revision 1.35 2003/02/03 18:46:00 acli
125 # Minor factoring in C4/Biblio.pm, plus change to export the per-tag
126 # 'mandatory' property to a per-subfield 'tag_mandatory' template parameter,
127 # so that addbiblio.tmpl can distinguish between mandatory subfields in a
128 # mandatory tag and mandatory subfields in an optional tag
130 # Not-minor factoring in acqui.simple/addbiblio.pl to make the if-else blocks
131 # smaller, and to add some POD; need further testing for this
133 # Added function to check if a MARC subfield name is "koha-internal" (instead
134 # of checking it for 'lib' and 'tag' everywhere); temporarily added to Koha.pm
136 # Use above function in acqui.simple/additem.pl and search.marc/search.pl
138 # Revision 1.34 2003/01/28 14:50:04 tipaul
139 # fixing MARCmodbiblio API and reindenting code
141 # Revision 1.33 2003/01/23 12:22:37 tipaul
142 # adding char_decode to decode MARC21 or UNIMARC extended chars
144 # Revision 1.32 2002/12/16 15:08:50 tipaul
145 # small but important bugfix (fixes a problem in export)
147 # Revision 1.31 2002/12/13 16:22:04 tipaul
148 # 1st draft of marc export
150 # Revision 1.30 2002/12/12 21:26:35 tipaul
151 # YAB ! (Yet Another Bugfix) => related to biblio modif
152 # (some warning cleaning too)
154 # Revision 1.29 2002/12/12 16:35:00 tipaul
155 # adding authentification with Auth.pm and
156 # MAJOR BUGFIX on marc biblio modification
158 # Revision 1.28 2002/12/10 13:30:03 tipaul
159 # fugfixes from Dombes Abbey work
161 # Revision 1.27 2002/11/19 12:36:16 tipaul
163 # various bugfixes, improvments, and migration from acquisition.pm to biblio.pm
165 # Revision 1.26 2002/11/12 15:58:43 tipaul
168 # * 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)
170 # Revision 1.25 2002/10/25 10:58:26 tipaul
172 # * bugfixes and improvements
174 # Revision 1.24 2002/10/24 12:09:01 arensb
175 # Fixed "no title" warning when generating HTML documentation from POD.
177 # Revision 1.23 2002/10/16 12:43:08 arensb
178 # Added some FIXME comments.
180 # Revision 1.22 2002/10/15 13:39:17 tipaul
181 # removing Acquisition.pm
182 # deleting unused code in biblio.pm, rewriting POD and answering most FIXME comments
184 # Revision 1.21 2002/10/13 11:34:14 arensb
185 # Replaced expressions of the form "$x = $x <op> $y" with "$x <op>= $y".
186 # Thus, $x = $x+2 becomes $x += 2, and so forth.
188 # Revision 1.20 2002/10/13 08:28:32 arensb
189 # Deleted unused variables.
190 # Removed trailing whitespace.
192 # Revision 1.19 2002/10/13 05:56:10 arensb
193 # Added some FIXME comments.
195 # Revision 1.18 2002/10/11 12:34:53 arensb
196 # Replaced &requireDBI with C4::Context->dbh
198 # Revision 1.17 2002/10/10 14:48:25 tipaul
201 # Revision 1.16 2002/10/07 14:04:26 tipaul
202 # road to 1.3.1 : viewing MARC biblio
204 # Revision 1.15 2002/10/05 09:49:25 arensb
205 # Merged with arensb-context branch: use C4::Context->dbh instead of
206 # &C4Connect, and generally prefer C4::Context over C4::Database.
208 # Revision 1.14 2002/10/03 11:28:18 tipaul
209 # Extending Context.pm to add stopword management and using it in MARC-API.
210 # First benchmarks show a medium speed improvement, which is nice as this part is heavily called.
212 # Revision 1.13 2002/10/02 16:26:44 tipaul
215 # Revision 1.12.2.4 2002/10/05 07:09:31 arensb
216 # Merged in changes from main branch.
218 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
219 # Added a whole mess of FIXME comments.
221 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
222 # Added some missing semicolons.
224 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
225 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
228 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
229 # Added a whole mess of FIXME comments.
231 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
232 # Added some missing semicolons.
234 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
235 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
238 # Revision 1.12 2002/10/01 11:48:51 arensb
239 # Added some FIXME comments, mostly marking duplicate functions.
241 # Revision 1.11 2002/09/24 13:49:26 tipaul
242 # long WAS the road to 1.3.0...
243 # coming VERY SOON NOW...
244 # modifying installer and buildrelease to update the DB
246 # Revision 1.10 2002/09/22 16:50:08 arensb
247 # Added some FIXME comments.
249 # Revision 1.9 2002/09/20 12:57:46 tipaul
250 # long is the road to 1.4.0
251 # * MARCadditem and MARCmoditem now wroks
252 # * various bugfixes in MARC management
253 # !!! 1.3.0 should be released very soon now. Be careful !!!
255 # Revision 1.8 2002/09/10 13:53:52 tipaul
256 # MARC API continued...
258 # * 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)
260 # Note : it should not be hard for marcimport and marcexport to re-link fields from internal tag/subfield to "legal" tag/subfield.
262 # Revision 1.7 2002/08/14 18:12:51 tonnesen
263 # Added copyright statement to all .pl and .pm files
265 # Revision 1.6 2002/07/25 13:40:31 tipaul
266 # pod documenting the API.
268 # Revision 1.5 2002/07/24 16:11:37 tipaul
270 # Database.pm and Output.pm are almost not modified (var test...)
272 # Biblio.pm is almost completly rewritten.
274 # WHAT DOES IT ??? ==> END of Hitchcock suspens
276 # 1st, it does... nothing...
277 # 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 ...
279 # All old-API functions have been cloned. for example, the "newbiblio" sub, now has become :
280 # * a "newbiblio" sub, with the same parameters. It just call a sub named OLDnewbiblio
281 # * 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.
282 # * 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.
283 # 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 ;-)
285 # 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.
286 # Note we have decided with steve that a old-biblio <=> a MARC-Biblio.
290 # Copyright 2000-2002 Katipo Communications
292 # This file is part of Koha.
294 # Koha is free software; you can redistribute it and/or modify it under the
295 # terms of the GNU General Public License as published by the Free Software
296 # Foundation; either version 2 of the License, or (at your option) any later
299 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
300 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
301 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
303 # You should have received a copy of the GNU General Public License along with
304 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
305 # Suite 330, Boston, MA 02111-1307 USA
313 use vars qw($VERSION @ISA @EXPORT);
315 # set the version for version checking
320 # don't forget MARCxxx subs are here only for testing purposes. Should not be used
321 # as the old-style API and the NEW one are the only public functions.
324 &updateBiblio &updateBiblioItem &updateItem
325 &itemcount &newbiblio &newbiblioitem
326 &modnote &newsubject &newsubtitle
327 &modbiblio &checkitems
328 &newitems &modbibitem
329 &modsubtitle &modsubject &modaddauthor &moditem &countitems
330 &delitem &deletebiblioitem &delbiblio
331 &getitemtypes &getbiblio
332 &getbiblioitembybiblionumber
333 &getbiblioitem &getitemsbybiblioitem
335 &newcompletebiblioitem
337 &MARCfind_oldbiblionumber_from_MARCbibid
338 &MARCfind_MARCbibid_from_oldbiblionumber
339 &MARCfind_marc_from_kohafield
343 &NEWnewbiblio &NEWnewitem
344 &NEWmodbiblio &NEWmoditem
346 &MARCaddbiblio &MARCadditem
347 &MARCmodsubfield &MARCaddsubfield
348 &MARCmodbiblio &MARCmoditem
349 &MARCkoha2marcBiblio &MARCmarc2koha
350 &MARCkoha2marcItem &MARChtml2marc
351 &MARCgetbiblio &MARCgetitem
352 &MARCaddword &MARCdelword
358 # MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC
361 # all the following subs takes a MARC::Record as parameter and manage
362 # the MARC-DB. They are called by the 1.0/1.2 xxx subs, and by the
363 # NEWxxx subs (xxx deals with old-DB parameters, the NEWxxx deals with MARC-DB parameter)
367 C4::Biblio - acquisition, catalog management functions
371 move from 1.2 to 1.4 version :
372 1.2 and previous version uses a specific API to manage biblios. This API uses old-DB style parameters.
373 In the 1.4 version, we want to do 2 differents things :
374 - keep populating the old-DB, that has a LOT less datas than MARC
375 - populate the MARC-DB
376 To populate the DBs we have 2 differents sources :
377 - the standard acquisition system (through book sellers), that does'nt use MARC data
378 - the MARC acquisition system, that uses MARC data.
380 Thus, we have 2 differents cases :
381 - 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
382 - 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
384 That's why we need 4 subs :
385 all I<subs beginning by MARC> manage only MARC tables. They manage MARC-DB with MARC::Record parameters
386 all I<subs beginning by OLD> manage only OLD-DB tables. They manage old-DB with old-DB parameters
387 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
388 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.
390 - NEW and old-style API should be used in koha to manage biblio
391 - MARCsubs are divided in 2 parts :
392 * some of them manage MARC parameters. They are heavily used in koha.
393 * some of them manage MARC biblio : they are mostly used by NEW and old-style subs.
394 - OLD are used internally only
396 all subs requires/use $dbh as 1st parameter.
398 I<NEWxxx related subs>
400 all subs requires/use $dbh as 1st parameter.
401 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
403 I<OLDxxx related subs>
405 all subs requires/use $dbh as 1st parameter.
406 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
408 They all are the exact copy of 1.0/1.2 version of the sub without the OLD.
409 The OLDxxx is called by the original xxx sub.
410 the 1.4 xxx sub also builds MARC::Record an calls the MARCxxx
412 WARNING : there is 1 difference between initialxxx and OLDxxx :
413 the db header $dbh is always passed as parameter to avoid over-DB connexion
419 =item @tagslib = &MARCgettagslib($dbh,1|0);
421 last param is 1 for liblibrarian and 0 for libopac
422 returns a hash with tag/subfield meaning
423 =item ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,$kohafield);
425 finds MARC tag and subfield for a given kohafield
426 kohafield is "table.field" where table= biblio|biblioitems|items, and field a field of the previous table
428 =item $biblionumber = &MARCfind_oldbiblionumber_from_MARCbibid($dbh,$MARCbibi);
430 finds a old-db biblio number for a given MARCbibid number
432 =item $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
434 finds a MARC bibid from a old-db biblionumber
436 =item $MARCRecord = &MARCkoha2marcBiblio($dbh,$biblionumber,biblioitemnumber);
438 MARCkoha2marcBiblio is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB biblio/biblioitem
440 =item $MARCRecord = &MARCkoha2marcItem($dbh,$biblionumber,itemnumber);
442 MARCkoha2marcItem is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB item
444 =item $MARCRecord = &MARCkoha2marcSubtitle($dbh,$biblionumber,$subtitle);
446 MARCkoha2marcSubtitle is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB subtitle
448 =item $olddb = &MARCmarc2koha($dbh,$MARCRecord);
450 builds a hash with old-db datas from a MARC::Record
452 =item &MARCaddbiblio($dbh,$MARC::Record,$biblionumber);
454 creates a biblio (in the MARC tables only). $biblionumber is the old-db biblionumber of the biblio
456 =item &MARCaddsubfield($dbh,$bibid,$tagid,$indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
458 adds a subfield in a biblio (in the MARC tables only).
460 =item $MARCRecord = &MARCgetbiblio($dbh,$bibid);
462 Returns a MARC::Record for the biblio $bibid.
464 =item &MARCmodbiblio($dbh,$bibid,$record,$delete);
466 MARCmodbiblio changes a biblio for a biblio,MARC::Record passed as parameter
467 It 1st delete the biblio, then recreates it.
468 WARNING : the $delete parameter is not used anymore (too much unsolvable cases).
469 =item ($subfieldid,$subfieldvalue) = &MARCmodsubfield($dbh,$subfieldid,$subfieldvalue);
471 MARCmodsubfield changes the value of a given subfield
473 =item $subfieldid = &MARCfindsubfield($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue);
475 MARCfindsubfield returns a subfield number given a bibid/tag/subfieldvalue values.
476 Returns -1 if more than 1 answer
478 =item $subfieldid = &MARCfindsubfieldid($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
480 MARCfindsubfieldid find a subfieldid for a bibid/tag/tagorder/subfield/subfieldorder
482 =item &MARCdelsubfield($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
484 MARCdelsubfield delete a subfield for a bibid/tag/tagorder/subfield/subfieldorder
486 =item &MARCdelbiblio($dbh,$bibid);
488 MARCdelbiblio delete biblio $bibid
490 =item &MARCkoha2marcOnefield
492 used by MARCkoha2marc and should not be useful elsewhere
494 =item &MARCmarc2kohaOnefield
496 used by MARCmarc2koha and should not be useful elsewhere
500 used to manage MARC_word table and should not be useful elsewhere
504 used to manage MARC_word table and should not be useful elsewhere
509 my ($dbh,$forlibrarian)= @_;
511 my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
512 $sth=$dbh->prepare("select tagfield,$libfield as lib,mandatory from marc_tag_structure order by tagfield");
514 my ($lib,$tag,$res,$tab,$mandatory,$repeatable);
515 while ( ($tag,$lib,$mandatory) = $sth->fetchrow) {
516 $res->{$tag}->{lib}=$lib;
517 $res->{$tab}->{tab}=""; # XXX
518 $res->{$tag}->{mandatory}=$mandatory;
521 $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");
525 my $authorised_value;
526 my $thesaurus_category;
529 while ( ($tag, $subfield, $lib, $tab, $mandatory, $repeatable,$authorised_value,$thesaurus_category,$value_builder,$kohafield) = $sth->fetchrow) {
530 $res->{$tag}->{$subfield}->{lib}=$lib;
531 $res->{$tag}->{$subfield}->{tab}=$tab;
532 $res->{$tag}->{$subfield}->{mandatory}=$mandatory;
533 $res->{$tag}->{$subfield}->{repeatable}=$repeatable;
534 $res->{$tag}->{$subfield}->{authorised_value}=$authorised_value;
535 $res->{$tag}->{$subfield}->{thesaurus_category}=$thesaurus_category;
536 $res->{$tag}->{$subfield}->{value_builder}=$value_builder;
537 $res->{$tag}->{$subfield}->{kohafield}=$kohafield;
542 sub MARCfind_marc_from_kohafield {
543 my ($dbh,$kohafield) = @_;
544 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
545 $sth->execute($kohafield);
546 my ($tagfield,$tagsubfield) = $sth->fetchrow;
547 return ($tagfield,$tagsubfield);
550 sub MARCfind_oldbiblionumber_from_MARCbibid {
551 my ($dbh,$MARCbibid) = @_;
552 my $sth=$dbh->prepare("select biblionumber from marc_biblio where bibid=?");
553 $sth->execute($MARCbibid);
554 my ($biblionumber) = $sth->fetchrow;
555 return $biblionumber;
558 sub MARCfind_MARCbibid_from_oldbiblionumber {
559 my ($dbh,$oldbiblionumber) = @_;
560 my $sth=$dbh->prepare("select bibid from marc_biblio where biblionumber=?");
561 $sth->execute($oldbiblionumber);
562 my ($bibid) = $sth->fetchrow;
567 # pass the MARC::Record to this function, and it will create the records in the marc tables
568 my ($dbh,$record,$biblionumber,$bibid) = @_;
569 my @fields=$record->fields();
570 # warn "IN MARCaddbiblio $bibid => ".$record->as_formatted;
572 # adding main table, and retrieving bibid
573 # if bibid is sent, then it's not a true add, it's only a re-add, after a delete (ie, a mod)
574 # if bibid empty => true add, find a new bibid number
576 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
577 my $sth=$dbh->prepare("insert into marc_biblio (datecreated,biblionumber) values (now(),?)");
578 $sth->execute($biblionumber);
579 $sth=$dbh->prepare("select max(bibid) from marc_biblio");
581 ($bibid)=$sth->fetchrow;
585 # now, add subfields...
586 foreach my $field (@fields) {
588 if ($field->tag() <10) {
589 &MARCaddsubfield($dbh,$bibid,
598 my @subfields=$field->subfields();
599 foreach my $subfieldcount (0..$#subfields) {
600 &MARCaddsubfield($dbh,$bibid,
602 $field->indicator(1).$field->indicator(2),
604 $subfields[$subfieldcount][0],
606 $subfields[$subfieldcount][1]
611 $dbh->do("unlock tables");
616 # pass the MARC::Record to this function, and it will create the records in the marc tables
617 my ($dbh,$record,$biblionumber) = @_;
618 # warn "adding : ".$record->as_formatted();
619 # search for MARC biblionumber
620 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
621 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
622 my @fields=$record->fields();
623 my $sth = $dbh->prepare("select max(tagorder) from marc_subfield_table where bibid=?");
624 $sth->execute($bibid);
625 my ($fieldcount) = $sth->fetchrow;
626 # now, add subfields...
627 foreach my $field (@fields) {
628 my @subfields=$field->subfields();
630 foreach my $subfieldcount (0..$#subfields) {
631 &MARCaddsubfield($dbh,$bibid,
633 $field->indicator(1).$field->indicator(2),
635 $subfields[$subfieldcount][0],
637 $subfields[$subfieldcount][1]
641 $dbh->do("unlock tables");
645 sub MARCaddsubfield {
646 # Add a new subfield to a tag into the DB.
647 my ($dbh,$bibid,$tagid,$tag_indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalues) = @_;
648 # if not value, end of job, we do nothing
649 if (length($subfieldvalues) ==0) {
652 if (not($subfieldcode)) {
655 my @subfieldvalues = split /\|/,$subfieldvalues;
656 foreach my $subfieldvalue (@subfieldvalues) {
657 if (length($subfieldvalue)>255) {
658 $dbh->do("lock tables marc_blob_subfield WRITE, marc_subfield_table WRITE");
659 my $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
660 $sth->execute($subfieldvalue);
661 $sth=$dbh->prepare("select max(blobidlink)from marc_blob_subfield");
663 my ($res)=$sth->fetchrow;
664 $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,valuebloblink) values (?,?,?,?,?,?,?)");
665 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$res);
667 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";
669 $dbh->do("unlock tables");
671 my $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?,?)");
672 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue);
674 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";
677 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
682 # Returns MARC::Record of the biblio passed in parameter.
684 my $record = MARC::Record->new();
685 #---- TODO : the leader is missing
686 $record->leader(' ');
687 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
688 from marc_subfield_table
689 where bibid=? order by tag,tagorder,subfieldcode
691 my $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
692 $sth->execute($bibid);
696 my $field; # for >=10 tags
697 my $prevvalue; # for <10 tags
698 while (my $row=$sth->fetchrow_hashref) {
699 if ($row->{'valuebloblink'}) { #---- search blob if there is one
700 $sth2->execute($row->{'valuebloblink'});
701 my $row2=$sth2->fetchrow_hashref;
703 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
705 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
708 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
710 $record->add_fields($field) unless $prevtag eq "XXX";
713 $prevtagorder=$row->{tagorder};
714 $prevtag = $row->{tag};
715 $previndicator=$row->{tag_indicator};
716 if ($row->{tag}<10) {
717 $prevvalue = $row->{subfieldvalue};
719 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.' ',0,1), substr($row->{tag_indicator}.' ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
722 if ($row->{tag} <10) {
723 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
725 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
727 $prevtag= $row->{tag};
728 $previndicator=$row->{tag_indicator};
731 # the last has not been included inside the loop... do it now !
733 $record->add_fields($prevtag,$prevvalue);
735 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
736 $record->add_fields($field);
741 # Returns MARC::Record of the biblio passed in parameter.
742 my ($dbh,$bibid,$itemnumber)=@_;
743 my $record = MARC::Record->new();
744 # search MARC tagorder
745 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=?");
746 $sth2->execute($bibid,$itemnumber);
747 my ($tagorder) = $sth2->fetchrow_array();
748 #---- TODO : the leader is missing
749 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
750 from marc_subfield_table
751 where bibid=? and tagorder=? order by subfieldcode,subfieldorder
753 $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
754 $sth->execute($bibid,$tagorder);
755 while (my $row=$sth->fetchrow_hashref) {
756 if ($row->{'valuebloblink'}) { #---- search blob if there is one
757 $sth2->execute($row->{'valuebloblink'});
758 my $row2=$sth2->fetchrow_hashref;
760 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
762 if ($record->field($row->{'tag'})) {
764 #--- this test must stay as this, because of strange behaviour of mySQL/Perl DBI with char var containing a number...
765 #--- sometimes, eliminates 0 at beginning, sometimes no ;-\\\
766 if (length($row->{'tag'}) <3) {
767 $row->{'tag'} = "0".$row->{'tag'};
769 $field =$record->field($row->{'tag'});
771 my $x = $field->add_subfields($row->{'subfieldcode'},$row->{'subfieldvalue'});
772 $record->delete_field($field);
773 $record->add_fields($field);
776 if (length($row->{'tag'}) < 3) {
777 $row->{'tag'} = "0".$row->{'tag'};
779 my $temp = MARC::Field->new($row->{'tag'}," "," ", $row->{'subfieldcode'} => $row->{'subfieldvalue'});
780 $record->add_fields($temp);
788 my ($dbh,$bibid,$record,$delete)=@_;
789 my $oldrecord=&MARCgetbiblio($dbh,$bibid);
790 if ($oldrecord eq $record) {
793 # 1st delete the biblio,
795 my $biblionumber = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
796 &MARCdelbiblio($dbh,$bibid,1);
797 &MARCaddbiblio($dbh,$record,$biblionumber,$bibid);
801 my ($dbh,$bibid,$keep_items) = @_;
802 # if the keep_item is set to 1, then all items are preserved.
803 # This flag is set when the delbiblio is called by modbiblio
804 # due to a too complex structure of MARC (repeatable fields and subfields),
805 # the best solution for a modif is to delete / recreate the record.
806 if ($keep_items eq 1) {
807 #search item field code
808 my $sth = $dbh->prepare("select tagfield from marc_subfield_structure where kohafield like 'items.%'");
810 my $itemtag = $sth->fetchrow_hashref->{tagfield};
811 $dbh->do("delete from marc_subfield_table where bibid=$bibid and tag<>$itemtag");
812 $dbh->do("delete from marc_word where bibid=$bibid and tag<>$itemtag");
814 $dbh->do("delete from marc_biblio where bibid=$bibid");
815 $dbh->do("delete from marc_subfield_table where bibid=$bibid");
816 $dbh->do("delete from marc_word where bibid=$bibid");
820 my ($dbh,$record,$bibid,$itemnumber,$delete)=@_;
821 my $oldrecord=&MARCgetitem($dbh,$bibid,$itemnumber);
822 # if nothing to change, don't waste time...
823 if ($oldrecord eq $record) {
827 # otherwise, skip through each subfield...
828 my @fields = $record->fields();
829 # search old MARC item
830 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=?");
831 $sth2->execute($bibid,$itemnumber);
832 my ($tagorder) = $sth2->fetchrow_array();
833 foreach my $field (@fields) {
834 my $oldfield = $oldrecord->field($field->tag());
835 my @subfields=$field->subfields();
837 foreach my $subfield (@subfields) {
839 # warn "compare : $oldfield".$oldfield->subfield(@$subfield[0]);
840 if ($oldfield eq 0 or (length($oldfield->subfield(@$subfield[0])) ==0) ) {
841 # just adding datas...
842 # warn "addfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
843 # warn "NEW subfield : $bibid,".$field->tag().",".$tagorder.",".@$subfield[0].",".$subfieldorder.",".@$subfield[1].")";
844 &MARCaddsubfield($dbh,$bibid,$field->tag(),$field->indicator(1).$field->indicator(2),
845 $tagorder,@$subfield[0],$subfieldorder,@$subfield[1]);
847 # warn "modfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
848 # modify he subfield if it's a different string
849 if ($oldfield->subfield(@$subfield[0]) ne @$subfield[1] ) {
850 my $subfieldid=&MARCfindsubfieldid($dbh,$bibid,$field->tag(),$tagorder,@$subfield[0],$subfieldorder);
851 # warn "changing : $subfieldid, $bibid,".$field->tag(),",$tagorder,@$subfield[0],@$subfield[1],$subfieldorder";
852 &MARCmodsubfield($dbh,$subfieldid,@$subfield[1]);
860 sub MARCmodsubfield {
861 # Subroutine changes a subfield value given a subfieldid.
862 my ($dbh, $subfieldid, $subfieldvalue )=@_;
863 $dbh->do("lock tables marc_blob_subfield WRITE,marc_subfield_table WRITE");
864 my $sth1=$dbh->prepare("select valuebloblink from marc_subfield_table where subfieldid=?");
865 $sth1->execute($subfieldid);
866 my ($oldvaluebloblink)=$sth1->fetchrow;
869 # if too long, use a bloblink
870 if (length($subfieldvalue)>255 ) {
871 # if already a bloblink, update it, otherwise, insert a new one.
872 if ($oldvaluebloblink) {
873 $sth=$dbh->prepare("update marc_blob_subfield set subfieldvalue=? where blobidlink=?");
874 $sth->execute($subfieldvalue,$oldvaluebloblink);
876 $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
877 $sth->execute($subfieldvalue);
878 $sth=$dbh->prepare("select max(blobidlink) from marc_blob_subfield");
880 my ($res)=$sth->fetchrow;
881 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=null, valuebloblink=$res where subfieldid=?");
882 $sth->execute($subfieldid);
885 # note this can leave orphan bloblink. Not a big problem, but we should build somewhere a orphan deleting script...
886 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=?,valuebloblink=null where subfieldid=?");
887 $sth->execute($subfieldvalue, $subfieldid);
889 $dbh->do("unlock tables");
891 $sth=$dbh->prepare("select bibid,tag,tagorder,subfieldcode,subfieldid,subfieldorder from marc_subfield_table where subfieldid=?");
892 $sth->execute($subfieldid);
893 my ($bibid,$tagid,$tagorder,$subfieldcode,$x,$subfieldorder) = $sth->fetchrow;
895 &MARCdelword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder);
896 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
897 return($subfieldid, $subfieldvalue);
900 sub MARCfindsubfield {
901 my ($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue) = @_;
905 my $query="select subfieldid from marc_subfield_table where bibid=? and tag=? and subfieldcode=?";
906 if ($subfieldvalue) {
907 $query .= " and subfieldvalue=".$dbh->quote($subfieldvalue);
909 if ($subfieldorder<1) {
912 $query .= " and subfieldorder=$subfieldorder";
914 my $sti=$dbh->prepare($query);
915 $sti->execute($bibid,$tag, $subfieldcode);
916 while (($subfieldid) = $sti->fetchrow) {
918 $lastsubfieldid=$subfieldid;
920 if ($resultcounter>1) {
921 # Error condition. Values given did not resolve into a unique record. Don't know what to edit
922 # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
925 return $lastsubfieldid;
929 sub MARCfindsubfieldid {
930 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
931 my $sth=$dbh->prepare("select subfieldid from marc_subfield_table
932 where bibid=? and tag=? and tagorder=?
933 and subfieldcode=? and subfieldorder=?");
934 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
935 my ($res) = $sth->fetchrow;
937 $sth=$dbh->prepare("select subfieldid from marc_subfield_table
938 where bibid=? and tag=? and tagorder=?
939 and subfieldcode=?");
940 $sth->execute($bibid,$tag,$tagorder,$subfield);
941 ($res) = $sth->fetchrow;
946 sub MARCdelsubfield {
947 # delete a subfield for $bibid / tag / tagorder / subfield / subfieldorder
948 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
949 $dbh->do("delete from marc_subfield_table where bibid='$bibid' and
950 tag='$tag' and tagorder='$tagorder'
951 and subfieldcode='$subfield' and subfieldorder='$subfieldorder
955 sub MARCkoha2marcBiblio {
956 # this function builds partial MARC::Record from the old koha-DB fields
957 my ($dbh,$biblionumber,$biblioitemnumber) = @_;
958 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
959 my $record = MARC::Record->new();
960 #--- if bibid, then retrieve old-style koha data
961 if ($biblionumber>0) {
962 my $sth2=$dbh->prepare("select biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp
963 from biblio where biblionumber=?");
964 $sth2->execute($biblionumber);
965 my $row=$sth2->fetchrow_hashref;
967 foreach $code (keys %$row) {
969 &MARCkoha2marcOnefield($sth,$record,"biblio.".$code,$row->{$code});
973 #--- if biblioitem, then retrieve old-style koha data
974 if ($biblioitemnumber>0) {
975 my $sth2=$dbh->prepare(" SELECT biblioitemnumber,biblionumber,volume,number,classification,
976 itemtype,url,isbn,issn,dewey,subclass,publicationyear,publishercode,
977 volumedate,volumeddesc,timestamp,illus,pages,notes AS bnotes,size,place
979 WHERE biblioitemnumber=?
981 $sth2->execute($biblioitemnumber);
982 my $row=$sth2->fetchrow_hashref;
984 foreach $code (keys %$row) {
986 &MARCkoha2marcOnefield($sth,$record,"biblioitems.".$code,$row->{$code});
990 # other fields => additional authors, subjects, subtitles
991 my $sth2=$dbh->prepare(" SELECT author FROM additionalauthors WHERE biblionumber=?");
992 $sth2->execute($biblionumber);
993 while (my $row=$sth2->fetchrow_hashref) {
994 &MARCkoha2marcOnefield($sth,$record,"additionalauthors.author",$row->{'author'});
996 my $sth2=$dbh->prepare(" SELECT subject FROM bibliosubject WHERE biblionumber=?");
997 $sth2->execute($biblionumber);
998 while (my $row=$sth2->fetchrow_hashref) {
999 &MARCkoha2marcOnefield($sth,$record,"bibliosubject.subject",$row->{'subject'});
1001 my $sth2=$dbh->prepare(" SELECT subtitle FROM bibliosubtitle WHERE biblionumber=?");
1002 $sth2->execute($biblionumber);
1003 while (my $row=$sth2->fetchrow_hashref) {
1004 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.title",$row->{'subtitle'});
1009 sub MARCkoha2marcItem {
1010 # this function builds partial MARC::Record from the old koha-DB fields
1011 my ($dbh,$biblionumber,$itemnumber) = @_;
1012 # my $dbh=&C4Connect;
1013 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1014 my $record = MARC::Record->new();
1015 #--- if item, then retrieve old-style koha data
1016 if ($itemnumber>0) {
1017 # print STDERR "prepare $biblionumber,$itemnumber\n";
1018 my $sth2=$dbh->prepare("SELECT itemnumber,biblionumber,multivolumepart,biblioitemnumber,barcode,dateaccessioned,
1019 booksellerid,homebranch,price,replacementprice,replacementpricedate,datelastborrowed,
1020 datelastseen,multivolume,stack,notforloan,itemlost,wthdrawn,bulk,issues,renewals,
1021 reserves,restricted,binding,itemnotes,holdingbranch,timestamp
1023 WHERE itemnumber=?");
1024 $sth2->execute($itemnumber);
1025 my $row=$sth2->fetchrow_hashref;
1027 foreach $code (keys %$row) {
1028 if ($row->{$code}) {
1029 &MARCkoha2marcOnefield($sth,$record,"items.".$code,$row->{$code});
1036 sub MARCkoha2marcSubtitle {
1037 # this function builds partial MARC::Record from the old koha-DB fields
1038 my ($dbh,$bibnum,$subtitle) = @_;
1039 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1040 my $record = MARC::Record->new();
1041 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.subtitle",$subtitle);
1045 sub MARCkoha2marcOnefield {
1046 my ($sth,$record,$kohafieldname,$value)=@_;
1049 $sth->execute($kohafieldname);
1050 if (($tagfield,$tagsubfield)=$sth->fetchrow) {
1051 if ($record->field($tagfield)) {
1052 my $tag =$record->field($tagfield);
1054 $tag->add_subfields($tagsubfield,$value);
1055 $record->delete_field($tag);
1056 $record->add_fields($tag);
1059 $record->add_fields($tagfield," "," ",$tagsubfield => $value);
1066 my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
1068 my $record = MARC::Record->new();
1069 # my %subfieldlist=();
1070 my $prevvalue; # if tag <10
1071 my $field; # if tag >=10
1072 for (my $i=0; $i< @$rtags; $i++) {
1073 # rebuild MARC::Record
1074 if (@$rtags[$i] ne $prevtag) {
1075 if ($prevtag < 10) {
1077 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
1081 $record->add_fields($field);
1084 $indicators{@$rtags[$i]}.=' ';
1085 if (@$rtags[$i] <10) {
1086 $prevvalue= @$rvalues[$i];
1088 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
1090 $prevtag = @$rtags[$i];
1092 if (@$rtags[$i] <10) {
1093 $prevvalue=@$rvalues[$i];
1095 if (@$rvalues[$i]) {
1096 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
1099 $prevtag= @$rtags[$i];
1102 # the last has not been included inside the loop... do it now !
1103 $record->add_fields($field);
1104 # warn $record->as_formatted;
1109 my ($dbh,$record) = @_;
1110 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1112 my $sth2=$dbh->prepare("SHOW COLUMNS from biblio");
1115 # print STDERR $record->as_formatted;
1116 while (($field)=$sth2->fetchrow) {
1117 $result=&MARCmarc2kohaOneField($sth,"biblio",$field,$record,$result);
1119 $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
1121 while (($field)=$sth2->fetchrow) {
1122 if ($field eq 'notes') { $field = 'bnotes'; }
1123 $result=&MARCmarc2kohaOneField($sth,"biblioitems",$field,$record,$result);
1125 $sth2=$dbh->prepare("SHOW COLUMNS from items");
1127 while (($field)=$sth2->fetchrow) {
1128 $result = &MARCmarc2kohaOneField($sth,"items",$field,$record,$result);
1130 # additional authors : specific
1131 $result = &MARCmarc2kohaOneField($sth,"additionalauthors","additionalauthors",$record,$result);
1132 # modify copyrightdate to keep only the 1st year found
1133 my $temp = $result->{'copyrightdate'};
1134 $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
1136 $result->{'copyrightdate'} = $1;
1137 } else { # if no cYYYY, get the 1st date.
1138 $temp =~ m/(\d\d\d\d)/;
1139 $result->{'copyrightdate'} = $1;
1141 # modify publicationyear to keep only the 1st year found
1142 my $temp = $result->{'publicationyear'};
1143 $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
1145 $result->{'publicationyear'} = $1;
1146 } else { # if no cYYYY, get the 1st date.
1147 $temp =~ m/(\d\d\d\d)/;
1148 $result->{'publicationyear'} = $1;
1153 sub MARCmarc2kohaOneField {
1154 # FIXME ? if a field has a repeatable subfield that is used in old-db, only the 1st will be retrieved...
1155 my ($sth,$kohatable,$kohafield,$record,$result)= @_;
1156 # warn "kohatable / $kohafield / $result / ";
1160 $sth->execute($kohatable.".".$kohafield);
1161 ($tagfield,$subfield) = $sth->fetchrow;
1162 foreach my $field ($record->field($tagfield)) {
1163 if ($field->subfield($subfield)) {
1164 if ($result->{$kohafield}) {
1165 $result->{$kohafield} .= " | ".$field->subfield($subfield);
1167 $result->{$kohafield}=$field->subfield($subfield);
1175 # split a subfield string and adds it into the word table.
1177 my ($dbh,$bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$sentence) =@_;
1178 $sentence =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\})/ /g;
1179 my @words = split / /,$sentence;
1180 my $stopwords= C4::Context->stopwords;
1181 my $sth=$dbh->prepare("insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word)
1182 values (?,?,?,?,?,?,soundex(?))");
1183 foreach my $word (@words) {
1184 # we record only words longer than 2 car and not in stopwords hash
1185 if (length($word)>1 and !($stopwords->{uc($word)})) {
1186 $sth->execute($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,$word);
1188 warn "ERROR ==> insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word) values ($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,soundex($word))\n";
1195 # delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
1196 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
1197 my $sth=$dbh->prepare("delete from marc_word where bibid=? and tag=? and tagorder=? and subfieldid=? and subfieldorder=?");
1198 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
1203 # NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
1206 # all the following subs are useful to manage MARC-DB with complete MARC records.
1207 # it's used with marcimport, and marc management tools
1211 =item ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbibilio($dbh,$MARCRecord,$oldbiblio,$oldbiblioitem);
1213 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
1214 are builded from the MARC::Record. If they are passed, they are used.
1216 =item NEWnewitem($dbh, $record,$bibid);
1218 adds an item in the db.
1223 my ($dbh, $record, $oldbiblio, $oldbiblioitem) = @_;
1224 # note $oldbiblio and $oldbiblioitem are not mandatory.
1225 # if not present, they will be builded from $record with MARCmarc2koha function
1226 if (($oldbiblio) and not($oldbiblioitem)) {
1227 print STDERR "NEWnewbiblio : missing parameter\n";
1228 print "NEWnewbiblio : missing parameter : contact koha development team\n";
1234 $oldbibnum = OLDnewbiblio($dbh,$oldbiblio);
1235 $oldbiblioitem->{'biblionumber'} = $oldbibnum;
1236 $oldbibitemnum = OLDnewbiblioitem($dbh,$oldbiblioitem);
1238 my $olddata = MARCmarc2koha($dbh,$record);
1239 $oldbibnum = OLDnewbiblio($dbh,$olddata);
1240 $olddata->{'biblionumber'} = $oldbibnum;
1241 $oldbibitemnum = OLDnewbiblioitem($dbh,$olddata);
1243 # search subtiles, addiauthors and subjects
1244 my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author");
1245 my @addiauthfields = $record->field($tagfield);
1246 foreach my $addiauthfield (@addiauthfields) {
1247 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1248 foreach my $subfieldcount (0..$#addiauthsubfields) {
1249 OLDmodaddauthor($dbh,$oldbibnum,$addiauthsubfields[$subfieldcount]);
1252 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle");
1253 my @subtitlefields = $record->field($tagfield);
1254 foreach my $subtitlefield (@subtitlefields) {
1255 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1256 foreach my $subfieldcount (0..$#subtitlesubfields) {
1257 OLDnewsubtitle($dbh,$oldbibnum,$subtitlesubfields[$subfieldcount]);
1260 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject");
1261 my @subj = $record->field($tagfield);
1262 foreach my $subject (@subj) {
1263 my @subjsubfield = $subject->subfield($tagsubfield);
1265 foreach my $subfieldcount (0..$#subjsubfield) {
1266 push @subjects,$subjsubfield[$subfieldcount];
1268 OLDmodsubject($dbh,$oldbibnum,1,@subjects);
1270 # we must add bibnum and bibitemnum in MARC::Record...
1271 # we build the new field with biblionumber and biblioitemnumber
1272 # we drop the original field
1273 # we add the new builded field.
1274 # NOTE : Works only if the field is ONLY for biblionumber and biblioitemnumber
1275 # (steve and paul : thinks 090 is a good choice)
1276 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1277 $sth->execute("biblio.biblionumber");
1278 (my $tagfield1, my $tagsubfield1) = $sth->fetchrow;
1279 $sth->execute("biblioitems.biblioitemnumber");
1280 (my $tagfield2, my $tagsubfield2) = $sth->fetchrow;
1281 if ($tagfield1 != $tagfield2) {
1282 warn "Error in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1283 print "Content-Type: text/html\n\nError in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1286 my $newfield = MARC::Field->new( $tagfield1,'','',
1287 "$tagsubfield1" => $oldbibnum,
1288 "$tagsubfield2" => $oldbibitemnum);
1289 # drop old field and create new one...
1290 my $old_field = $record->field($tagfield1);
1291 $record->delete_field($old_field);
1292 $record->add_fields($newfield);
1293 my $bibid = MARCaddbiblio($dbh,$record,$oldbibnum);
1294 return ($bibid,$oldbibnum,$oldbibitemnum );
1298 my ($dbh,$record,$bibid) =@_;
1299 &MARCmodbiblio($dbh,$bibid,$record,0);
1300 my $oldbiblio = MARCmarc2koha($dbh,$record);
1301 my $oldbiblionumber = OLDmodbiblio($dbh,$oldbiblio);
1302 OLDmodbibitem($dbh,$oldbiblio);
1303 # now, modify addi authors, subject, addititles.
1304 my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author");
1305 my @addiauthfields = $record->field($tagfield);
1306 foreach my $addiauthfield (@addiauthfields) {
1307 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1308 foreach my $subfieldcount (0..$#addiauthsubfields) {
1309 OLDmodaddauthor($dbh,$oldbiblionumber,$addiauthsubfields[$subfieldcount]);
1312 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle");
1313 my @subtitlefields = $record->field($tagfield);
1314 foreach my $subtitlefield (@subtitlefields) {
1315 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1316 foreach my $subfieldcount (0..$#subtitlesubfields) {
1317 OLDmodsubtitle($dbh,$oldbiblionumber,$subtitlesubfields[$subfieldcount]);
1320 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject");
1321 my @subj = $record->field($tagfield);
1322 foreach my $subject (@subj) {
1323 my @subjsubfield = $subject->subfield($tagsubfield);
1325 foreach my $subfieldcount (0..$#subjsubfield) {
1326 push @subjects,$subjsubfield[$subfieldcount];
1328 OLDmodsubject($dbh,$oldbiblionumber,1,@subjects);
1335 my ($dbh, $record,$bibid) = @_;
1336 # add item in old-DB
1337 my $item = &MARCmarc2koha($dbh,$record);
1338 # needs old biblionumber and biblioitemnumber
1339 $item->{'biblionumber'} = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
1340 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
1341 $sth->execute($item->{'biblionumber'});
1342 ($item->{'biblioitemnumber'}) = $sth->fetchrow;
1343 my ($itemnumber,$error) = &OLDnewitems($dbh,$item,$item->{barcode});
1344 # add itemnumber to MARC::Record before adding the item.
1345 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1346 &MARCkoha2marcOnefield($sth,$record,"items.itemnumber",$itemnumber);
1348 my $bib = &MARCadditem($dbh,$record,$item->{'biblionumber'});
1352 my ($dbh,$record,$bibid,$itemnumber,$delete) = @_;
1353 &MARCmoditem($dbh,$record,$bibid,$itemnumber,$delete);
1354 my $olditem = MARCmarc2koha($dbh,$record);
1355 OLDmoditem($dbh,$olditem);
1360 # OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
1364 =item $biblionumber = OLDnewbiblio($dbh,$biblio);
1366 adds a record in biblio table. Datas are in the hash $biblio.
1368 =item $biblionumber = OLDmodbiblio($dbh,$biblio);
1370 modify a record in biblio table. Datas are in the hash $biblio.
1372 =item OLDmodsubtitle($dbh,$bibnum,$subtitle);
1374 modify subtitles in bibliosubtitle table.
1376 =item OLDmodaddauthor($dbh,$bibnum,$author);
1378 adds or modify additional authors
1379 NOTE : Strange sub : seems to delete MANY and add only ONE author... maybe buggy ?
1381 =item $errors = OLDmodsubject($dbh,$bibnum, $force, @subject);
1383 modify/adds subjects
1385 =item OLDmodbibitem($dbh, $biblioitem);
1389 =item OLDmodnote($dbh,$bibitemnum,$note
1391 modify a note for a biblioitem
1393 =item OLDnewbiblioitem($dbh,$biblioitem);
1395 adds a biblioitem ($biblioitem is a hash with the values)
1397 =item OLDnewsubject($dbh,$bibnum);
1401 =item OLDnewsubtitle($dbh,$bibnum,$subtitle);
1403 create a new subtitle
1405 =item ($itemnumber,$errors)= OLDnewitems($dbh,$item,$barcode);
1407 create a item. $item is a hash and $barcode the barcode.
1409 =item OLDmoditem($dbh,$item);
1413 =item OLDdelitem($dbh,$itemnum);
1417 =item OLDdeletebiblioitem($dbh,$biblioitemnumber);
1419 deletes a biblioitem
1420 NOTE : not standard sub name. Should be OLDdelbiblioitem()
1422 =item OLDdelbiblio($dbh,$biblio);
1429 my ($dbh,$biblio) = @_;
1430 # my $dbh = &C4Connect;
1431 my $query = "Select max(biblionumber) from biblio";
1432 my $sth = $dbh->prepare($query);
1434 my $data = $sth->fetchrow_arrayref;
1435 my $bibnum = $$data[0] + 1;
1438 if ($biblio->{'seriestitle'}) { $series = 1 };
1440 $query = "insert into biblio set biblionumber = ?, title = ?, author = ?, copyrightdate = ?,
1441 serial = ?, seriestitle = ?, notes = ?, abstract = ?";
1442 $sth = $dbh->prepare($query);
1443 $sth->execute($bibnum,$biblio->{'title'},$biblio->{'author'},$biblio->{'copyright'},$series,$biblio->{'seriestitle'},$biblio->{'notes'},$biblio->{'abstract'});
1451 my ($dbh,$biblio) = @_;
1452 # my $dbh = C4Connect;
1456 $query = "Update biblio set title = ?, author = ?, abstract = ?, copyrightdate = ?,
1457 seriestitle = ?, serial = ?, unititle = ?, notes = ? where biblionumber = ?";
1458 $sth = $dbh->prepare($query);
1459 $sth->execute($biblio->{'title'},$biblio->{'author'},$biblio->{'abstract'},$biblio->{'copyrightdate'},
1460 $biblio->{'seriestitle'},$biblio->{'serial'},$biblio->{'unititle'},$biblio->{'notes'},$biblio->{'biblionumber'});
1463 return($biblio->{'biblionumber'});
1466 sub OLDmodsubtitle {
1467 my ($dbh,$bibnum, $subtitle) = @_;
1468 my $query = "update bibliosubtitle set subtitle = ? where biblionumber = ?";
1469 my $sth = $dbh->prepare($query);
1470 $sth->execute($subtitle,$bibnum);
1475 sub OLDmodaddauthor {
1476 my ($dbh,$bibnum, $author) = @_;
1477 # my $dbh = C4Connect;
1478 my $query = "Delete from additionalauthors where biblionumber = $bibnum";
1479 my $sth = $dbh->prepare($query);
1484 if ($author ne '') {
1485 $query = "Insert into additionalauthors set
1488 $sth = $dbh->prepare($query);
1490 $sth->execute($author,$bibnum);
1494 } # sub modaddauthor
1498 my ($dbh,$bibnum, $force, @subject) = @_;
1499 # my $dbh = C4Connect;
1500 my $count = @subject;
1502 for (my $i = 0; $i < $count; $i++) {
1503 $subject[$i] =~ s/^ //g;
1504 $subject[$i] =~ s/ $//g;
1505 my $query = "select * from catalogueentry where entrytype = 's' and catalogueentry = '$subject[$i]'";
1506 my $sth = $dbh->prepare($query);
1509 if (my $data = $sth->fetchrow_hashref) {
1511 if ($force eq $subject[$i] || $force eq 1) {
1512 # subject not in aut, chosen to force anway
1513 # so insert into cataloguentry so its in auth file
1514 $query = "Insert into catalogueentry (entrytype,catalogueentry) values ('s','$subject[$i]')";
1515 my $sth2 = $dbh->prepare($query);
1520 $error = "$subject[$i]\n does not exist in the subject authority file";
1521 $query = "Select * from catalogueentry where entrytype = 's' and (catalogueentry like '$subject[$i] %'
1522 or catalogueentry like '% $subject[$i] %' or catalogueentry like '% $subject[$i]')";
1523 my $sth2 = $dbh->prepare($query);
1525 while (my $data = $sth2->fetchrow_hashref) {
1526 $error .= "<br>$data->{'catalogueentry'}";
1534 my $query = "Delete from bibliosubject where biblionumber = $bibnum";
1535 my $sth = $dbh->prepare($query);
1538 for (my $i = 0; $i < $count; $i++) {
1539 $sth = $dbh->prepare("Insert into bibliosubject values ('$subject[$i]', $bibnum)");
1550 my ($dbh,$biblioitem) = @_;
1551 # my $dbh = C4Connect;
1554 $biblioitem->{'itemtype'} = $dbh->quote($biblioitem->{'itemtype'});
1555 $biblioitem->{'url'} = $dbh->quote($biblioitem->{'url'});
1556 $biblioitem->{'isbn'} = $dbh->quote($biblioitem->{'isbn'});
1557 $biblioitem->{'publishercode'} = $dbh->quote($biblioitem->{'publishercode'});
1558 $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
1559 $biblioitem->{'classification'} = $dbh->quote($biblioitem->{'classification'});
1560 $biblioitem->{'dewey'} = $dbh->quote($biblioitem->{'dewey'});
1561 $biblioitem->{'subclass'} = $dbh->quote($biblioitem->{'subclass'});
1562 $biblioitem->{'illus'} = $dbh->quote($biblioitem->{'illus'});
1563 $biblioitem->{'pages'} = $dbh->quote($biblioitem->{'pages'});
1564 $biblioitem->{'volumeddesc'} = $dbh->quote($biblioitem->{'volumeddesc'});
1565 $biblioitem->{'bnotes'} = $dbh->quote($biblioitem->{'bnotes'});
1566 $biblioitem->{'size'} = $dbh->quote($biblioitem->{'size'});
1567 $biblioitem->{'place'} = $dbh->quote($biblioitem->{'place'});
1569 $query = "Update biblioitems set
1570 itemtype = $biblioitem->{'itemtype'},
1571 url = $biblioitem->{'url'},
1572 isbn = $biblioitem->{'isbn'},
1573 publishercode = $biblioitem->{'publishercode'},
1574 publicationyear = $biblioitem->{'publicationyear'},
1575 classification = $biblioitem->{'classification'},
1576 dewey = $biblioitem->{'dewey'},
1577 subclass = $biblioitem->{'subclass'},
1578 illus = $biblioitem->{'illus'},
1579 pages = $biblioitem->{'pages'},
1580 volumeddesc = $biblioitem->{'volumeddesc'},
1581 notes = $biblioitem->{'bnotes'},
1582 size = $biblioitem->{'size'},
1583 place = $biblioitem->{'place'}
1584 where biblioitemnumber = $biblioitem->{'biblioitemnumber'}";
1592 my ($dbh,$bibitemnum,$note)=@_;
1593 # my $dbh=C4Connect;
1594 my $query="update biblioitems set notes='$note' where
1595 biblioitemnumber='$bibitemnum'";
1596 my $sth=$dbh->prepare($query);
1602 sub OLDnewbiblioitem {
1603 my ($dbh,$biblioitem) = @_;
1604 # my $dbh = C4Connect;
1605 my $query = "Select max(biblioitemnumber) from biblioitems";
1606 my $sth = $dbh->prepare($query);
1611 $data = $sth->fetchrow_arrayref;
1612 $bibitemnum = $$data[0] + 1;
1616 $sth = $dbh->prepare("insert into biblioitems set
1617 biblioitemnumber = ?, biblionumber = ?,
1618 volume = ?, number = ?,
1619 classification = ?, itemtype = ?,
1621 issn = ?, dewey = ?,
1622 subclass = ?, publicationyear = ?,
1623 publishercode = ?, volumedate = ?,
1624 volumeddesc = ?, illus = ?,
1625 pages = ?, notes = ?,
1627 marc = ?, place = ?");
1628 $sth->execute($bibitemnum, $biblioitem->{'biblionumber'},
1629 $biblioitem->{'volume'}, $biblioitem->{'number'},
1630 $biblioitem->{'classification'}, $biblioitem->{'itemtype'},
1631 $biblioitem->{'url'}, $biblioitem->{'isbn'},
1632 $biblioitem->{'issn'}, $biblioitem->{'dewey'},
1633 $biblioitem->{'subclass'}, $biblioitem->{'publicationyear'},
1634 $biblioitem->{'publishercode'}, $biblioitem->{'volumedate'},
1635 $biblioitem->{'volumeddesc'}, $biblioitem->{'illus'},
1636 $biblioitem->{'pages'}, $biblioitem->{'bnotes'},
1637 $biblioitem->{'size'}, $biblioitem->{'lccn'},
1638 $biblioitem->{'marc'}, $biblioitem->{'place'});
1641 return($bibitemnum);
1645 my ($dbh,$bibnum)=@_;
1646 my $query="insert into bibliosubject (biblionumber) values ($bibnum)";
1647 my $sth=$dbh->prepare($query);
1652 sub OLDnewsubtitle {
1653 my ($dbh,$bibnum, $subtitle) = @_;
1654 my $query = "insert into bibliosubtitle set biblionumber = ?, subtitle = ?";
1655 my $sth = $dbh->prepare($query);
1656 $sth->execute($bibnum,$subtitle);
1662 my ($dbh,$item, $barcode) = @_;
1663 # my $dbh = C4Connect;
1664 my $query = "Select max(itemnumber) from items";
1665 my $sth = $dbh->prepare($query);
1671 $data = $sth->fetchrow_hashref;
1672 $itemnumber = $data->{'max(itemnumber)'} + 1;
1674 # if dateaccessioned is provided, use it. Otherwise, set to NOW()
1675 if ($item->{'dateaccessioned'}) {
1676 $sth=$dbh->prepare("Insert into items set
1677 itemnumber = ?, biblionumber = ?,
1678 biblioitemnumber = ?, barcode = ?,
1679 booksellerid = ?, dateaccessioned = ?,
1680 homebranch = ?, holdingbranch = ?,
1681 price = ?, replacementprice = ?,
1682 replacementpricedate = NOW(), itemnotes = ?,
1685 $sth->execute($itemnumber, $item->{'biblionumber'},
1686 $item->{'biblioitemnumber'},$barcode,
1687 $item->{'booksellerid'},$item->{'dateaccessioned'},
1688 $item->{'homebranch'},$item->{'holdingbranch'},
1689 $item->{'price'},$item->{'replacementprice'},
1690 $item->{'itemnotes'},$item->{'loan'});
1692 $sth=$dbh->prepare("Insert into items set
1693 itemnumber = ?, biblionumber = ?,
1694 biblioitemnumber = ?, barcode = ?,
1695 booksellerid = ?, dateaccessioned = NOW(),
1696 homebranch = ?, holdingbranch = ?,
1697 price = ?, replacementprice = ?,
1698 replacementpricedate = NOW(), itemnotes = ?,
1701 $sth->execute($itemnumber, $item->{'biblionumber'},
1702 $item->{'biblioitemnumber'},$barcode,
1703 $item->{'booksellerid'},
1704 $item->{'homebranch'},$item->{'holdingbranch'},
1705 $item->{'price'},$item->{'replacementprice'},
1706 $item->{'itemnotes'},$item->{'loan'});
1708 if (defined $sth->errstr) {
1709 $error .= $sth->errstr;
1712 return($itemnumber,$error);
1716 my ($dbh,$item) = @_;
1717 # my ($dbh,$loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
1718 # my $dbh=C4Connect;
1719 $item->{'itemnum'}=$item->{'itemnumber'} unless $item->{'itemnum'};
1720 my $query="update items set barcode='$item->{'barcode'}',itemnotes='$item->{'notes'}'
1721 where itemnumber=$item->{'itemnum'}";
1722 if ($item->{'barcode'} eq ''){
1723 $item->{'notforloan'}=0 unless $item->{'notforloan'};
1724 $query="update items set notforloan=$item->{'notforloan'} where itemnumber=$item->{'itemnum'}";
1726 if ($item->{'lost'} ne ''){
1727 $query="update items set biblioitemnumber=$item->{'bibitemnum'},
1728 barcode='$item->{'barcode'}',
1729 itemnotes='$item->{'notes'}',
1730 homebranch='$item->{'homebranch'}',
1731 itemlost='$item->{'lost'}',
1732 wthdrawn='$item->{'wthdrawn'}'
1733 where itemnumber=$item->{'itemnum'}";
1735 if ($item->{'replacement'} ne ''){
1736 $query=~ s/ where/,replacementprice='$item->{'replacement'}' where/;
1738 my $sth=$dbh->prepare($query);
1745 my ($dbh,$itemnum)=@_;
1746 # my $dbh=C4Connect;
1747 my $query="select * from items where itemnumber=$itemnum";
1748 my $sth=$dbh->prepare($query);
1750 my @data=$sth->fetchrow_array;
1752 $query="Insert into deleteditems values (";
1753 foreach my $temp (@data){
1754 $query .= "'$temp',";
1758 $sth=$dbh->prepare($query);
1761 $query = "Delete from items where itemnumber=$itemnum";
1762 $sth=$dbh->prepare($query);
1768 sub OLDdeletebiblioitem {
1769 my ($dbh,$biblioitemnumber) = @_;
1770 # my $dbh = C4Connect;
1771 my $query = "Select * from biblioitems
1772 where biblioitemnumber = $biblioitemnumber";
1773 my $sth = $dbh->prepare($query);
1778 if (@results = $sth->fetchrow_array) {
1779 $query = "Insert into deletedbiblioitems values (";
1780 foreach my $value (@results) {
1781 $value = $dbh->quote($value);
1782 $query .= "$value,";
1785 $query =~ s/\,$/\)/;
1788 $query = "Delete from biblioitems
1789 where biblioitemnumber = $biblioitemnumber";
1793 # Now delete all the items attached to the biblioitem
1794 $query = "Select * from items where biblioitemnumber = $biblioitemnumber";
1795 $sth = $dbh->prepare($query);
1797 while (@results = $sth->fetchrow_array) {
1798 $query = "Insert into deleteditems values (";
1799 foreach my $value (@results) {
1800 $value = $dbh->quote($value);
1801 $query .= "$value,";
1803 $query =~ s/\,$/\)/;
1807 $query = "Delete from items where biblioitemnumber = $biblioitemnumber";
1810 } # sub deletebiblioitem
1813 my ($dbh,$biblio)=@_;
1814 # my $dbh=C4Connect;
1815 my $query="select * from biblio where biblionumber=$biblio";
1816 my $sth=$dbh->prepare($query);
1818 if (my @data=$sth->fetchrow_array){
1820 $query="Insert into deletedbiblio values (";
1821 foreach my $temp (@data){
1822 $temp=~ s/\'/\\\'/g;
1823 $query .= "'$temp',";
1827 $sth=$dbh->prepare($query);
1830 $query = "Delete from biblio where biblionumber=$biblio";
1831 $sth=$dbh->prepare($query);
1847 my $dbh = C4::Context->dbh;
1848 my $query="Select count(*) from items where biblionumber=$biblio";
1850 my $sth=$dbh->prepare($query);
1852 my $data=$sth->fetchrow_hashref;
1854 return($data->{'count(*)'});
1859 ($order, $ordernumber) = &getorder($biblioitemnumber, $biblionumber);
1861 Looks up the order with the given biblionumber and biblioitemnumber.
1863 Returns a two-element array. C<$ordernumber> is the order number.
1864 C<$order> is a reference-to-hash describing the order; its keys are
1865 fields from the biblio, biblioitems, aqorders, and aqorderbreakdown
1866 tables of the Koha database.
1870 # FIXME - This is effectively identical to &C4::Catalogue::getorder.
1871 # Pick one and stick with it.
1874 my $dbh = C4::Context->dbh;
1875 my $query="Select ordernumber
1877 where biblionumber=? and biblioitemnumber=?";
1878 my $sth=$dbh->prepare($query);
1879 $sth->execute($bib,$bi);
1880 # FIXME - Use fetchrow_array(), since we're only interested in the one
1882 my $ordnum=$sth->fetchrow_hashref;
1884 my $order=getsingleorder($ordnum->{'ordernumber'});
1886 return ($order,$ordnum->{'ordernumber'});
1889 =item getsingleorder
1891 $order = &getsingleorder($ordernumber);
1893 Looks up an order by order number.
1895 Returns a reference-to-hash describing the order. The keys of
1896 C<$order> are fields from the biblio, biblioitems, aqorders, and
1897 aqorderbreakdown tables of the Koha database.
1901 # FIXME - This is effectively identical to
1902 # &C4::Catalogue::getsingleorder.
1903 # Pick one and stick with it.
1904 sub getsingleorder {
1906 my $dbh = C4::Context->dbh;
1907 my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown
1908 where aqorders.ordernumber=?
1909 and biblio.biblionumber=aqorders.biblionumber and
1910 biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
1911 aqorders.ordernumber=aqorderbreakdown.ordernumber";
1912 my $sth=$dbh->prepare($query);
1913 $sth->execute($ordnum);
1914 my $data=$sth->fetchrow_hashref;
1921 my $dbh = C4::Context->dbh;
1922 my $bibnum=OLDnewbiblio($dbh,$biblio);
1923 # finds new (MARC bibid
1924 # my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$bibnum);
1925 my $record = &MARCkoha2marcBiblio($dbh,$bibnum);
1926 MARCaddbiblio($dbh,$record,$bibnum);
1932 $biblionumber = &modbiblio($biblio);
1934 Update a biblio record.
1936 C<$biblio> is a reference-to-hash whose keys are the fields in the
1937 biblio table in the Koha database. All fields must be present, not
1938 just the ones you wish to change.
1940 C<&modbiblio> updates the record defined by
1941 C<$biblio-E<gt>{biblionumber}> with the values in C<$biblio>.
1943 C<&modbiblio> returns C<$biblio-E<gt>{biblionumber}> whether it was
1950 my $dbh = C4::Context->dbh;
1951 my $biblionumber=OLDmodbiblio($dbh,$biblio);
1952 my $record = MARCkoha2marcBiblio($dbh,$biblionumber,$biblionumber);
1953 # finds new (MARC bibid
1954 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
1955 MARCmodbiblio($dbh,$bibid,$record,0);
1956 return($biblionumber);
1961 &modsubtitle($biblionumber, $subtitle);
1963 Sets the subtitle of a book.
1965 C<$biblionumber> is the biblionumber of the book to modify.
1967 C<$subtitle> is the new subtitle.
1972 my ($bibnum, $subtitle) = @_;
1973 my $dbh = C4::Context->dbh;
1974 &OLDmodsubtitle($dbh,$bibnum,$subtitle);
1979 &modaddauthor($biblionumber, $author);
1981 Replaces all additional authors for the book with biblio number
1982 C<$biblionumber> with C<$author>. If C<$author> is the empty string,
1983 C<&modaddauthor> deletes all additional authors.
1988 my ($bibnum, $author) = @_;
1989 my $dbh = C4::Context->dbh;
1990 &OLDmodaddauthor($dbh,$bibnum,$author);
1991 } # sub modaddauthor
1995 $error = &modsubject($biblionumber, $force, @subjects);
1997 $force - a subject to force
1999 $error - Error message, or undef if successful.
2004 my ($bibnum, $force, @subject) = @_;
2005 my $dbh = C4::Context->dbh;
2006 my $error= &OLDmodsubject($dbh,$bibnum,$force, @subject);
2011 my ($biblioitem) = @_;
2012 my $dbh = C4::Context->dbh;
2013 &OLDmodbibitem($dbh,$biblioitem);
2017 my ($bibitemnum,$note)=@_;
2018 my $dbh = C4::Context->dbh;
2019 &OLDmodnote($dbh,$bibitemnum,$note);
2023 my ($biblioitem) = @_;
2024 my $dbh = C4::Context->dbh;
2025 my $bibitemnum = &OLDnewbiblioitem($dbh,$biblioitem);
2026 my $MARCbiblio= MARCkoha2marcBiblio($dbh,0,$bibitemnum); # the 0 means "do NOT retrieve biblio, only biblioitem, in the MARC record
2027 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblioitem->{biblionumber});
2028 &MARCaddbiblio($dbh,$MARCbiblio,$biblioitem->{biblionumber},$bibid);
2029 return($bibitemnum);
2034 my $dbh = C4::Context->dbh;
2035 &OLDnewsubject($dbh,$bibnum);
2039 my ($bibnum, $subtitle) = @_;
2040 my $dbh = C4::Context->dbh;
2041 &OLDnewsubtitle($dbh,$bibnum,$subtitle);
2045 my ($item, @barcodes) = @_;
2046 my $dbh = C4::Context->dbh;
2050 foreach my $barcode (@barcodes) {
2051 ($itemnumber,$error)=&OLDnewitems($dbh,$item,uc($barcode));
2053 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{biblionumber},$itemnumber);
2054 &MARCadditem($dbh,$MARCitem,$item->{biblionumber});
2061 my $dbh = C4::Context->dbh;
2062 &OLDmoditem($dbh,$item);
2063 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{'biblionumber'},$item->{'itemnum'});
2064 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$item->{biblionumber});
2065 &MARCmoditem($dbh,$MARCitem,$bibid,$item->{itemnum},0);
2069 my ($count,@barcodes)=@_;
2070 my $dbh = C4::Context->dbh;
2072 for (my $i=0;$i<$count;$i++){
2073 $barcodes[$i]=uc $barcodes[$i];
2074 my $query="Select * from items where barcode='$barcodes[$i]'";
2075 my $sth=$dbh->prepare($query);
2077 if (my $data=$sth->fetchrow_hashref){
2078 $error.=" Duplicate Barcode: $barcodes[$i]";
2086 my ($bibitemnum)=@_;
2087 my $dbh = C4::Context->dbh;
2088 my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
2089 my $sth=$dbh->prepare($query);
2091 my $data=$sth->fetchrow_hashref;
2093 return($data->{'count(*)'});
2098 my $dbh = C4::Context->dbh;
2099 &OLDdelitem($dbh,$itemnum);
2102 sub deletebiblioitem {
2103 my ($biblioitemnumber) = @_;
2104 my $dbh = C4::Context->dbh;
2105 &OLDdeletebiblioitem($dbh,$biblioitemnumber);
2106 } # sub deletebiblioitem
2111 my $dbh = C4::Context->dbh;
2112 &OLDdelbiblio($dbh,$biblio);
2116 my $dbh = C4::Context->dbh;
2117 my $query = "select * from itemtypes order by description";
2118 my $sth = $dbh->prepare($query);
2119 # || die "Cannot prepare $query" . $dbh->errstr;
2124 # || die "Cannot execute $query\n" . $sth->errstr;
2125 while (my $data = $sth->fetchrow_hashref) {
2126 $results[$count] = $data;
2131 return($count, @results);
2132 } # sub getitemtypes
2135 my ($biblionumber) = @_;
2136 my $dbh = C4::Context->dbh;
2137 my $query = "Select * from biblio where biblionumber = $biblionumber";
2138 my $sth = $dbh->prepare($query);
2139 # || die "Cannot prepare $query\n" . $dbh->errstr;
2144 # || die "Cannot execute $query\n" . $sth->errstr;
2145 while (my $data = $sth->fetchrow_hashref) {
2146 $results[$count] = $data;
2151 return($count, @results);
2155 my ($biblioitemnum) = @_;
2156 my $dbh = C4::Context->dbh;
2157 my $query = "Select * from biblioitems where
2158 biblioitemnumber = $biblioitemnum";
2159 my $sth = $dbh->prepare($query);
2165 while (my $data = $sth->fetchrow_hashref) {
2166 $results[$count] = $data;
2171 return($count, @results);
2172 } # sub getbiblioitem
2174 sub getbiblioitembybiblionumber {
2175 my ($biblionumber) = @_;
2176 my $dbh = C4::Context->dbh;
2177 my $query = "Select * from biblioitems where biblionumber =
2179 my $sth = $dbh->prepare($query);
2185 while (my $data = $sth->fetchrow_hashref) {
2186 $results[$count] = $data;
2191 return($count, @results);
2194 sub getitemsbybiblioitem {
2195 my ($biblioitemnum) = @_;
2196 my $dbh = C4::Context->dbh;
2197 my $query = "Select * from items, biblio where
2198 biblio.biblionumber = items.biblionumber and biblioitemnumber
2200 my $sth = $dbh->prepare($query);
2201 # || die "Cannot prepare $query\n" . $dbh->errstr;
2206 # || die "Cannot execute $query\n" . $sth->errstr;
2207 while (my $data = $sth->fetchrow_hashref) {
2208 $results[$count] = $data;
2213 return($count, @results);
2214 } # sub getitemsbybiblioitem
2218 # Subroutine to log changes to databases
2219 # Eventually, this subroutine will be used to create a log of all changes made,
2220 # with the possibility of "undo"ing some changes
2222 if ($database eq 'kohadb') {
2228 # print STDERR "KOHA: $type $section $item $original $new\n";
2229 } elsif ($database eq 'marc') {
2231 my $Record_ID=shift;
2234 my $subfield_ID=shift;
2237 # print STDERR "MARC: $type $Record_ID $tag $mark $subfield_ID $original $new\n";
2241 #------------------------------------------------
2244 #---------------------------------------
2245 # Find a biblio entry, or create a new one if it doesn't exist.
2246 # If a "subtitle" entry is in hash, add it to subtitle table
2247 sub getoraddbiblio {
2251 # FIXME - Unused argument
2252 $biblio, # hash ref to fields
2263 $dbh = C4::Context->dbh;
2265 print "<PRE>Looking for biblio </PRE>\n" if $debug;
2266 $sth=$dbh->prepare("select biblionumber
2268 where title=? and author=?
2269 and copyrightdate=? and seriestitle=?");
2271 $biblio->{title}, $biblio->{author},
2272 $biblio->{copyright}, $biblio->{seriestitle} );
2274 ($biblionumber) = $sth->fetchrow;
2275 print "<PRE>Biblio exists with number $biblionumber</PRE>\n" if $debug;
2277 # Doesn't exist. Add new one.
2278 print "<PRE>Adding biblio</PRE>\n" if $debug;
2279 ($biblionumber,$error)=&newbiblio($biblio);
2280 if ( $biblionumber ) {
2281 print "<PRE>Added with biblio number=$biblionumber</PRE>\n" if $debug;
2282 if ( $biblio->{subtitle} ) {
2283 &newsubtitle($biblionumber,$biblio->{subtitle} );
2286 print "<PRE>Couldn't add biblio: $error</PRE>\n" if $debug;
2290 return $biblionumber,$error;
2292 } # sub getoraddbiblio
2295 # converts ISO 5426 coded string to ISO 8859-1
2296 # sloppy code : should be improved in next issue
2297 my ($string,$encoding) = @_ ;
2299 # $encoding = C4::Context->preference("marcflavour") unless $encoding;
2300 if ($encoding eq "UNIMARC") {
2362 # this handles non-sorting blocks (if implementation requires this)
2363 $string = nsb_clean($_) ;
2364 } elsif ($encoding eq "USMARC") {
2417 # this handles non-sorting blocks (if implementation requires this)
2418 $string = nsb_clean($_) ;
2425 my $NSB = '\x88' ; # NSB : begin Non Sorting Block
2426 my $NSE = '\x89' ; # NSE : Non Sorting Block end
2427 # handles non sorting blocks
2431 s/[ ]{0,1}$NSE/) /gm ;
2436 END { } # module clean-up code here (global destructor)
2442 Koha Developement team <info@koha.org>
2444 Paul POULAIN paul.poulain@free.fr