4 # Revision 1.67 2003/10/25 08:46:27 tipaul
5 # minor fixes for bilbio deletion (still buggy)
7 # Revision 1.66 2003/10/17 10:02:56 tipaul
8 # Indexing only words longer than 2 letters. Was >=2 before, & 2 letters words usually means nothing.
10 # Revision 1.65 2003/10/14 09:45:29 tipaul
11 # adding rebuildnonmarc.pl script : run this script when you change a link between marc and non MARC DB. It rebuilds the non-MARC DB (long operation)
13 # Revision 1.64 2003/10/06 15:20:51 tipaul
14 # fix for 536 (subtitle error)
16 # Revision 1.63 2003/10/01 13:25:49 tipaul
17 # seems a char encoding problem modified something in char_decode sub... changing back to something that works...
19 # Revision 1.62 2003/09/17 14:21:13 tipaul
20 # fixing bug that makes a MARC biblio disappear when using full acquisition (order => recieve ==> MARC editor).
21 # Before this 2 lines fix, the MARC biblio was deleted during recieve, and had to be entirely recreated :-(
23 # Revision 1.61 2003/09/17 10:24:39 tipaul
24 # notforloan value in itemtype was overwritting notforloan value in a given item.
25 # I changed this behaviour :
26 # if notforloan is set for a given item, and NOT for all items from this itemtype, the notforloan is kept.
27 # If notforloan is set for itemtype, it's used (and impossible to loan a specific item from this itemtype)
29 # Revision 1.60 2003/09/04 14:11:23 tipaul
30 # fix for 593 (data duplication in MARC-DB)
32 # Revision 1.58 2003/08/06 12:54:52 tipaul
33 # fix for publicationyear : extracting numeric value from MARC string, like for copyrightdate.
34 # (note that copyrightdate still extracted to get numeric format)
36 # Revision 1.57 2003/07/15 23:09:18 slef
37 # change show columns to use biblioitems bnotes too
39 # Revision 1.56 2003/07/15 11:34:52 slef
40 # fixes from paul email
42 # Revision 1.55 2003/07/15 00:02:49 slef
43 # Work on bug 515... can we do a single-side rename of notes to bnotes?
45 # Revision 1.54 2003/07/11 11:51:32 tipaul
46 # *** empty log message ***
48 # Revision 1.52 2003/07/10 10:37:19 tipaul
49 # fix for copyrightdate problem, #514
51 # Revision 1.51 2003/07/02 14:47:17 tipaul
52 # fix for #519 : items.dateaccessioned imports incorrectly
54 # Revision 1.49 2003/06/17 11:21:13 tipaul
55 # improvments/fixes for z3950 support.
56 # * Works now even on ADD, not only on MODIFY
57 # * able to search on ISBN, author, title
59 # Revision 1.48 2003/06/16 09:22:53 rangi
60 # Just added an order clause to getitemtypes
62 # Revision 1.47 2003/05/20 16:22:44 tipaul
63 # fixing typo in Biblio.pm POD
65 # Revision 1.46 2003/05/19 13:45:18 tipaul
66 # support for subtitles, additional authors, subject.
67 # 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.
68 # Note that some OLD-DB subs are strange (dummy ?) see OLDmodsubject, OLDmodsubtitle, OLDmodaddiauthor in C4/Biblio.pm
69 # 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.
71 # Revision 1.45 2003/04/29 16:50:49 tipaul
72 # really proud of this commit :-)
73 # z3950 search and import seems to works fine.
74 # Let me explain how :
75 # * a "search z3950" button is added in the addbiblio template.
76 # * when clicked, a popup appears and z3950/search.pl is called
77 # * z3950/search.pl calls addz3950search in the DB
78 # * the z3950 daemon retrieve the records and stores them in z3950results AND in marc_breeding table.
79 # * as long as there as searches pending, the popup auto refresh every 2 seconds, and says how many searches are pending.
80 # * when the user clicks on a z3950 result => the parent popup is called with the requested biblio, and auto-filled
83 # * 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.
84 # * 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.
86 # Revision 1.44 2003/04/28 13:07:14 tipaul
87 # Those fixes solves the "internal server error" with MARC::Record 1.12.
88 # It was due to an illegal contruction in Koha : we tried to retrive subfields from <10 tags.
89 # That's not possible. MARC::Record accepted this in 0.93 version, but it was fixed after.
90 # Now, the construct/retrieving is OK !
92 # Revision 1.43 2003/04/10 13:56:02 tipaul
94 # * worked in 1.9.0, but not in 1.9.1 :
95 # - modif of a biblio didn't work
96 # - 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.
98 # * did not work before :
99 # - repeatable subfields now works correctly. Enter 2 subfields separated by | and they will be splitted during saving.
100 # - dropped the last subfield of the MARC form :-(
103 # - 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.
104 # Note the MARCdelbiblio has been rewritted to enable deletion of a biblio WITHOUT deleting items.
106 # Revision 1.42 2003/04/04 08:41:11 tipaul
107 # last commits before 1.9.1
109 # Revision 1.41 2003/04/01 12:26:43 tipaul
112 # Revision 1.40 2003/03/11 15:14:03 tipaul
115 # Revision 1.39 2003/03/07 16:35:42 tipaul
116 # * moving generic functions to Koha.pm
117 # * improvement of SearchMarc.pm
121 # Revision 1.38 2003/02/27 16:51:59 tipaul
122 # * moving prepare / execute to ? form.
125 # * road to 1.9.2 => acquisition and cataloguing merging
127 # Revision 1.37 2003/02/12 11:03:03 tipaul
128 # Support for 000 -> 010 fields.
129 # Those fields doesn't have subfields.
130 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
131 # 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.
133 # Revision 1.36 2003/02/12 11:01:01 tipaul
134 # Support for 000 -> 010 fields.
135 # Those fields doesn't have subfields.
136 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
137 # 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.
139 # Revision 1.35 2003/02/03 18:46:00 acli
140 # Minor factoring in C4/Biblio.pm, plus change to export the per-tag
141 # 'mandatory' property to a per-subfield 'tag_mandatory' template parameter,
142 # so that addbiblio.tmpl can distinguish between mandatory subfields in a
143 # mandatory tag and mandatory subfields in an optional tag
145 # Not-minor factoring in acqui.simple/addbiblio.pl to make the if-else blocks
146 # smaller, and to add some POD; need further testing for this
148 # Added function to check if a MARC subfield name is "koha-internal" (instead
149 # of checking it for 'lib' and 'tag' everywhere); temporarily added to Koha.pm
151 # Use above function in acqui.simple/additem.pl and search.marc/search.pl
153 # Revision 1.34 2003/01/28 14:50:04 tipaul
154 # fixing MARCmodbiblio API and reindenting code
156 # Revision 1.33 2003/01/23 12:22:37 tipaul
157 # adding char_decode to decode MARC21 or UNIMARC extended chars
159 # Revision 1.32 2002/12/16 15:08:50 tipaul
160 # small but important bugfix (fixes a problem in export)
162 # Revision 1.31 2002/12/13 16:22:04 tipaul
163 # 1st draft of marc export
165 # Revision 1.30 2002/12/12 21:26:35 tipaul
166 # YAB ! (Yet Another Bugfix) => related to biblio modif
167 # (some warning cleaning too)
169 # Revision 1.29 2002/12/12 16:35:00 tipaul
170 # adding authentification with Auth.pm and
171 # MAJOR BUGFIX on marc biblio modification
173 # Revision 1.28 2002/12/10 13:30:03 tipaul
174 # fugfixes from Dombes Abbey work
176 # Revision 1.27 2002/11/19 12:36:16 tipaul
178 # various bugfixes, improvments, and migration from acquisition.pm to biblio.pm
180 # Revision 1.26 2002/11/12 15:58:43 tipaul
183 # * 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)
185 # Revision 1.25 2002/10/25 10:58:26 tipaul
187 # * bugfixes and improvements
189 # Revision 1.24 2002/10/24 12:09:01 arensb
190 # Fixed "no title" warning when generating HTML documentation from POD.
192 # Revision 1.23 2002/10/16 12:43:08 arensb
193 # Added some FIXME comments.
195 # Revision 1.22 2002/10/15 13:39:17 tipaul
196 # removing Acquisition.pm
197 # deleting unused code in biblio.pm, rewriting POD and answering most FIXME comments
199 # Revision 1.21 2002/10/13 11:34:14 arensb
200 # Replaced expressions of the form "$x = $x <op> $y" with "$x <op>= $y".
201 # Thus, $x = $x+2 becomes $x += 2, and so forth.
203 # Revision 1.20 2002/10/13 08:28:32 arensb
204 # Deleted unused variables.
205 # Removed trailing whitespace.
207 # Revision 1.19 2002/10/13 05:56:10 arensb
208 # Added some FIXME comments.
210 # Revision 1.18 2002/10/11 12:34:53 arensb
211 # Replaced &requireDBI with C4::Context->dbh
213 # Revision 1.17 2002/10/10 14:48:25 tipaul
216 # Revision 1.16 2002/10/07 14:04:26 tipaul
217 # road to 1.3.1 : viewing MARC biblio
219 # Revision 1.15 2002/10/05 09:49:25 arensb
220 # Merged with arensb-context branch: use C4::Context->dbh instead of
221 # &C4Connect, and generally prefer C4::Context over C4::Database.
223 # Revision 1.14 2002/10/03 11:28:18 tipaul
224 # Extending Context.pm to add stopword management and using it in MARC-API.
225 # First benchmarks show a medium speed improvement, which is nice as this part is heavily called.
227 # Revision 1.13 2002/10/02 16:26:44 tipaul
230 # Revision 1.12.2.4 2002/10/05 07:09:31 arensb
231 # Merged in changes from main branch.
233 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
234 # Added a whole mess of FIXME comments.
236 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
237 # Added some missing semicolons.
239 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
240 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
243 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
244 # Added a whole mess of FIXME comments.
246 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
247 # Added some missing semicolons.
249 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
250 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
253 # Revision 1.12 2002/10/01 11:48:51 arensb
254 # Added some FIXME comments, mostly marking duplicate functions.
256 # Revision 1.11 2002/09/24 13:49:26 tipaul
257 # long WAS the road to 1.3.0...
258 # coming VERY SOON NOW...
259 # modifying installer and buildrelease to update the DB
261 # Revision 1.10 2002/09/22 16:50:08 arensb
262 # Added some FIXME comments.
264 # Revision 1.9 2002/09/20 12:57:46 tipaul
265 # long is the road to 1.4.0
266 # * MARCadditem and MARCmoditem now wroks
267 # * various bugfixes in MARC management
268 # !!! 1.3.0 should be released very soon now. Be careful !!!
270 # Revision 1.8 2002/09/10 13:53:52 tipaul
271 # MARC API continued...
273 # * 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)
275 # Note : it should not be hard for marcimport and marcexport to re-link fields from internal tag/subfield to "legal" tag/subfield.
277 # Revision 1.7 2002/08/14 18:12:51 tonnesen
278 # Added copyright statement to all .pl and .pm files
280 # Revision 1.6 2002/07/25 13:40:31 tipaul
281 # pod documenting the API.
283 # Revision 1.5 2002/07/24 16:11:37 tipaul
285 # Database.pm and Output.pm are almost not modified (var test...)
287 # Biblio.pm is almost completly rewritten.
289 # WHAT DOES IT ??? ==> END of Hitchcock suspens
291 # 1st, it does... nothing...
292 # 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 ...
294 # All old-API functions have been cloned. for example, the "newbiblio" sub, now has become :
295 # * a "newbiblio" sub, with the same parameters. It just call a sub named OLDnewbiblio
296 # * 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.
297 # * 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.
298 # 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 ;-)
300 # 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.
301 # Note we have decided with steve that a old-biblio <=> a MARC-Biblio.
305 # Copyright 2000-2002 Katipo Communications
307 # This file is part of Koha.
309 # Koha is free software; you can redistribute it and/or modify it under the
310 # terms of the GNU General Public License as published by the Free Software
311 # Foundation; either version 2 of the License, or (at your option) any later
314 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
315 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
316 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
318 # You should have received a copy of the GNU General Public License along with
319 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
320 # Suite 330, Boston, MA 02111-1307 USA
328 use vars qw($VERSION @ISA @EXPORT);
330 # set the version for version checking
335 # don't forget MARCxxx subs are here only for testing purposes. Should not be used
336 # as the old-style API and the NEW one are the only public functions.
339 &updateBiblio &updateBiblioItem &updateItem
340 &itemcount &newbiblio &newbiblioitem
341 &modnote &newsubject &newsubtitle
342 &modbiblio &checkitems
343 &newitems &modbibitem
344 &modsubtitle &modsubject &modaddauthor &moditem &countitems
345 &delitem &deletebiblioitem &delbiblio
346 &getitemtypes &getbiblio
347 &getbiblioitembybiblionumber
348 &getbiblioitem &getitemsbybiblioitem
350 &newcompletebiblioitem
352 &MARCfind_oldbiblionumber_from_MARCbibid
353 &MARCfind_MARCbibid_from_oldbiblionumber
354 &MARCfind_marc_from_kohafield
358 &NEWnewbiblio &NEWnewitem
359 &NEWmodbiblio &NEWmoditem
361 &MARCaddbiblio &MARCadditem
362 &MARCmodsubfield &MARCaddsubfield
363 &MARCmodbiblio &MARCmoditem
364 &MARCkoha2marcBiblio &MARCmarc2koha
365 &MARCkoha2marcItem &MARChtml2marc
366 &MARCgetbiblio &MARCgetitem
367 &MARCaddword &MARCdelword
373 # MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC
376 # all the following subs takes a MARC::Record as parameter and manage
377 # the MARC-DB. They are called by the 1.0/1.2 xxx subs, and by the
378 # NEWxxx subs (xxx deals with old-DB parameters, the NEWxxx deals with MARC-DB parameter)
382 C4::Biblio - acquisition, catalog management functions
386 move from 1.2 to 1.4 version :
387 1.2 and previous version uses a specific API to manage biblios. This API uses old-DB style parameters.
388 In the 1.4 version, we want to do 2 differents things :
389 - keep populating the old-DB, that has a LOT less datas than MARC
390 - populate the MARC-DB
391 To populate the DBs we have 2 differents sources :
392 - the standard acquisition system (through book sellers), that does'nt use MARC data
393 - the MARC acquisition system, that uses MARC data.
395 Thus, we have 2 differents cases :
396 - 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
397 - 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
399 That's why we need 4 subs :
400 all I<subs beginning by MARC> manage only MARC tables. They manage MARC-DB with MARC::Record parameters
401 all I<subs beginning by OLD> manage only OLD-DB tables. They manage old-DB with old-DB parameters
402 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
403 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.
405 - NEW and old-style API should be used in koha to manage biblio
406 - MARCsubs are divided in 2 parts :
407 * some of them manage MARC parameters. They are heavily used in koha.
408 * some of them manage MARC biblio : they are mostly used by NEW and old-style subs.
409 - OLD are used internally only
411 all subs requires/use $dbh as 1st parameter.
413 I<NEWxxx related subs>
415 all subs requires/use $dbh as 1st parameter.
416 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
418 I<OLDxxx related subs>
420 all subs requires/use $dbh as 1st parameter.
421 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
423 They all are the exact copy of 1.0/1.2 version of the sub without the OLD.
424 The OLDxxx is called by the original xxx sub.
425 the 1.4 xxx sub also builds MARC::Record an calls the MARCxxx
427 WARNING : there is 1 difference between initialxxx and OLDxxx :
428 the db header $dbh is always passed as parameter to avoid over-DB connexion
434 =item @tagslib = &MARCgettagslib($dbh,1|0);
436 last param is 1 for liblibrarian and 0 for libopac
437 returns a hash with tag/subfield meaning
438 =item ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,$kohafield);
440 finds MARC tag and subfield for a given kohafield
441 kohafield is "table.field" where table= biblio|biblioitems|items, and field a field of the previous table
443 =item $biblionumber = &MARCfind_oldbiblionumber_from_MARCbibid($dbh,$MARCbibi);
445 finds a old-db biblio number for a given MARCbibid number
447 =item $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
449 finds a MARC bibid from a old-db biblionumber
451 =item $MARCRecord = &MARCkoha2marcBiblio($dbh,$biblionumber,biblioitemnumber);
453 MARCkoha2marcBiblio is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB biblio/biblioitem
455 =item $MARCRecord = &MARCkoha2marcItem($dbh,$biblionumber,itemnumber);
457 MARCkoha2marcItem is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB item
459 =item $MARCRecord = &MARCkoha2marcSubtitle($dbh,$biblionumber,$subtitle);
461 MARCkoha2marcSubtitle is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB subtitle
463 =item $olddb = &MARCmarc2koha($dbh,$MARCRecord);
465 builds a hash with old-db datas from a MARC::Record
467 =item &MARCaddbiblio($dbh,$MARC::Record,$biblionumber);
469 creates a biblio (in the MARC tables only). $biblionumber is the old-db biblionumber of the biblio
471 =item &MARCaddsubfield($dbh,$bibid,$tagid,$indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
473 adds a subfield in a biblio (in the MARC tables only).
475 =item $MARCRecord = &MARCgetbiblio($dbh,$bibid);
477 Returns a MARC::Record for the biblio $bibid.
479 =item &MARCmodbiblio($dbh,$bibid,$record,$delete);
481 MARCmodbiblio changes a biblio for a biblio,MARC::Record passed as parameter
482 It 1st delete the biblio, then recreates it.
483 WARNING : the $delete parameter is not used anymore (too much unsolvable cases).
484 =item ($subfieldid,$subfieldvalue) = &MARCmodsubfield($dbh,$subfieldid,$subfieldvalue);
486 MARCmodsubfield changes the value of a given subfield
488 =item $subfieldid = &MARCfindsubfield($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue);
490 MARCfindsubfield returns a subfield number given a bibid/tag/subfieldvalue values.
491 Returns -1 if more than 1 answer
493 =item $subfieldid = &MARCfindsubfieldid($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
495 MARCfindsubfieldid find a subfieldid for a bibid/tag/tagorder/subfield/subfieldorder
497 =item &MARCdelsubfield($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
499 MARCdelsubfield delete a subfield for a bibid/tag/tagorder/subfield/subfieldorder
501 =item &MARCdelbiblio($dbh,$bibid);
503 MARCdelbiblio delete biblio $bibid
505 =item &MARCkoha2marcOnefield
507 used by MARCkoha2marc and should not be useful elsewhere
509 =item &MARCmarc2kohaOnefield
511 used by MARCmarc2koha and should not be useful elsewhere
515 used to manage MARC_word table and should not be useful elsewhere
519 used to manage MARC_word table and should not be useful elsewhere
524 my ($dbh,$forlibrarian)= @_;
526 my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
527 $sth=$dbh->prepare("select tagfield,$libfield as lib,mandatory from marc_tag_structure order by tagfield");
529 my ($lib,$tag,$res,$tab,$mandatory,$repeatable);
530 while ( ($tag,$lib,$mandatory) = $sth->fetchrow) {
531 $res->{$tag}->{lib}=$lib;
532 $res->{$tab}->{tab}=""; # XXX
533 $res->{$tag}->{mandatory}=$mandatory;
536 $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");
540 my $authorised_value;
541 my $thesaurus_category;
544 while ( ($tag, $subfield, $lib, $tab, $mandatory, $repeatable,$authorised_value,$thesaurus_category,$value_builder,$kohafield) = $sth->fetchrow) {
545 $res->{$tag}->{$subfield}->{lib}=$lib;
546 $res->{$tag}->{$subfield}->{tab}=$tab;
547 $res->{$tag}->{$subfield}->{mandatory}=$mandatory;
548 $res->{$tag}->{$subfield}->{repeatable}=$repeatable;
549 $res->{$tag}->{$subfield}->{authorised_value}=$authorised_value;
550 $res->{$tag}->{$subfield}->{thesaurus_category}=$thesaurus_category;
551 $res->{$tag}->{$subfield}->{value_builder}=$value_builder;
552 $res->{$tag}->{$subfield}->{kohafield}=$kohafield;
557 sub MARCfind_marc_from_kohafield {
558 my ($dbh,$kohafield) = @_;
559 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
560 $sth->execute($kohafield);
561 my ($tagfield,$tagsubfield) = $sth->fetchrow;
562 return ($tagfield,$tagsubfield);
565 sub MARCfind_oldbiblionumber_from_MARCbibid {
566 my ($dbh,$MARCbibid) = @_;
567 my $sth=$dbh->prepare("select biblionumber from marc_biblio where bibid=?");
568 $sth->execute($MARCbibid);
569 my ($biblionumber) = $sth->fetchrow;
570 return $biblionumber;
573 sub MARCfind_MARCbibid_from_oldbiblionumber {
574 my ($dbh,$oldbiblionumber) = @_;
575 my $sth=$dbh->prepare("select bibid from marc_biblio where biblionumber=?");
576 $sth->execute($oldbiblionumber);
577 my ($bibid) = $sth->fetchrow;
582 # pass the MARC::Record to this function, and it will create the records in the marc tables
583 my ($dbh,$record,$biblionumber,$bibid) = @_;
584 my @fields=$record->fields();
585 # warn "IN MARCaddbiblio $bibid => ".$record->as_formatted;
587 # adding main table, and retrieving bibid
588 # if bibid is sent, then it's not a true add, it's only a re-add, after a delete (ie, a mod)
589 # if bibid empty => true add, find a new bibid number
591 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
592 my $sth=$dbh->prepare("insert into marc_biblio (datecreated,biblionumber) values (now(),?)");
593 $sth->execute($biblionumber);
594 $sth=$dbh->prepare("select max(bibid) from marc_biblio");
596 ($bibid)=$sth->fetchrow;
600 # now, add subfields...
601 foreach my $field (@fields) {
603 if ($field->tag() <10) {
604 &MARCaddsubfield($dbh,$bibid,
613 my @subfields=$field->subfields();
614 foreach my $subfieldcount (0..$#subfields) {
615 &MARCaddsubfield($dbh,$bibid,
617 $field->indicator(1).$field->indicator(2),
619 $subfields[$subfieldcount][0],
621 $subfields[$subfieldcount][1]
626 $dbh->do("unlock tables");
631 # pass the MARC::Record to this function, and it will create the records in the marc tables
632 my ($dbh,$record,$biblionumber) = @_;
633 # warn "adding : ".$record->as_formatted();
634 # search for MARC biblionumber
635 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
636 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
637 my @fields=$record->fields();
638 my $sth = $dbh->prepare("select max(tagorder) from marc_subfield_table where bibid=?");
639 $sth->execute($bibid);
640 my ($fieldcount) = $sth->fetchrow;
641 # now, add subfields...
642 foreach my $field (@fields) {
643 my @subfields=$field->subfields();
645 foreach my $subfieldcount (0..$#subfields) {
646 &MARCaddsubfield($dbh,$bibid,
648 $field->indicator(1).$field->indicator(2),
650 $subfields[$subfieldcount][0],
652 $subfields[$subfieldcount][1]
656 $dbh->do("unlock tables");
660 sub MARCaddsubfield {
661 # Add a new subfield to a tag into the DB.
662 my ($dbh,$bibid,$tagid,$tag_indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalues) = @_;
663 # if not value, end of job, we do nothing
664 if (length($subfieldvalues) ==0) {
667 if (not($subfieldcode)) {
670 my @subfieldvalues = split /\|/,$subfieldvalues;
671 foreach my $subfieldvalue (@subfieldvalues) {
672 if (length($subfieldvalue)>255) {
673 $dbh->do("lock tables marc_blob_subfield WRITE, marc_subfield_table WRITE");
674 my $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
675 $sth->execute($subfieldvalue);
676 $sth=$dbh->prepare("select max(blobidlink)from marc_blob_subfield");
678 my ($res)=$sth->fetchrow;
679 $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,valuebloblink) values (?,?,?,?,?,?,?)");
680 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$res);
682 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";
684 $dbh->do("unlock tables");
686 my $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?,?)");
687 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue);
689 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";
692 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
697 # Returns MARC::Record of the biblio passed in parameter.
699 my $record = MARC::Record->new();
700 #---- TODO : the leader is missing
701 $record->leader(' ');
702 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
703 from marc_subfield_table
704 where bibid=? order by tag,tagorder,subfieldcode
706 my $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
707 $sth->execute($bibid);
711 my $field; # for >=10 tags
712 my $prevvalue; # for <10 tags
713 while (my $row=$sth->fetchrow_hashref) {
714 if ($row->{'valuebloblink'}) { #---- search blob if there is one
715 $sth2->execute($row->{'valuebloblink'});
716 my $row2=$sth2->fetchrow_hashref;
718 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
720 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
723 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
725 $record->add_fields($field) unless $prevtag eq "XXX";
728 $prevtagorder=$row->{tagorder};
729 $prevtag = $row->{tag};
730 $previndicator=$row->{tag_indicator};
731 if ($row->{tag}<10) {
732 $prevvalue = $row->{subfieldvalue};
734 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.' ',0,1), substr($row->{tag_indicator}.' ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
737 if ($row->{tag} <10) {
738 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
740 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
742 $prevtag= $row->{tag};
743 $previndicator=$row->{tag_indicator};
746 # the last has not been included inside the loop... do it now !
748 $record->add_fields($prevtag,$prevvalue);
750 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
751 $record->add_fields($field);
756 # Returns MARC::Record of the biblio passed in parameter.
757 my ($dbh,$bibid,$itemnumber)=@_;
758 my $record = MARC::Record->new();
759 # search MARC tagorder
760 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=?");
761 $sth2->execute($bibid,$itemnumber);
762 my ($tagorder) = $sth2->fetchrow_array();
763 #---- TODO : the leader is missing
764 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
765 from marc_subfield_table
766 where bibid=? and tagorder=? order by subfieldcode,subfieldorder
768 $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
769 $sth->execute($bibid,$tagorder);
770 while (my $row=$sth->fetchrow_hashref) {
771 if ($row->{'valuebloblink'}) { #---- search blob if there is one
772 $sth2->execute($row->{'valuebloblink'});
773 my $row2=$sth2->fetchrow_hashref;
775 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
777 if ($record->field($row->{'tag'})) {
779 #--- this test must stay as this, because of strange behaviour of mySQL/Perl DBI with char var containing a number...
780 #--- sometimes, eliminates 0 at beginning, sometimes no ;-\\\
781 if (length($row->{'tag'}) <3) {
782 $row->{'tag'} = "0".$row->{'tag'};
784 $field =$record->field($row->{'tag'});
786 my $x = $field->add_subfields($row->{'subfieldcode'},$row->{'subfieldvalue'});
787 $record->delete_field($field);
788 $record->add_fields($field);
791 if (length($row->{'tag'}) < 3) {
792 $row->{'tag'} = "0".$row->{'tag'};
794 my $temp = MARC::Field->new($row->{'tag'}," "," ", $row->{'subfieldcode'} => $row->{'subfieldvalue'});
795 $record->add_fields($temp);
803 my ($dbh,$bibid,$record,$delete)=@_;
804 my $oldrecord=&MARCgetbiblio($dbh,$bibid);
805 if ($oldrecord eq $record) {
808 # 1st delete the biblio,
810 my $biblionumber = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
811 &MARCdelbiblio($dbh,$bibid,1);
812 &MARCaddbiblio($dbh,$record,$biblionumber,$bibid);
816 my ($dbh,$bibid,$keep_items) = @_;
817 # if the keep_item is set to 1, then all items are preserved.
818 # This flag is set when the delbiblio is called by modbiblio
819 # due to a too complex structure of MARC (repeatable fields and subfields),
820 # the best solution for a modif is to delete / recreate the record.
821 if ($keep_items eq 1) {
822 #search item field code
823 my $sth = $dbh->prepare("select tagfield from marc_subfield_structure where kohafield like 'items.%'");
825 my $itemtag = $sth->fetchrow_hashref->{tagfield};
826 $dbh->do("delete from marc_subfield_table where bibid=$bibid and tag<>$itemtag");
827 $dbh->do("delete from marc_word where bibid=$bibid and tag<>$itemtag");
829 $dbh->do("delete from marc_biblio where bibid=$bibid");
830 $dbh->do("delete from marc_subfield_table where bibid=$bibid");
831 $dbh->do("delete from marc_word where bibid=$bibid");
835 my ($dbh,$record,$bibid,$itemnumber,$delete)=@_;
836 my $oldrecord=&MARCgetitem($dbh,$bibid,$itemnumber);
837 # if nothing to change, don't waste time...
838 if ($oldrecord eq $record) {
842 # otherwise, skip through each subfield...
843 my @fields = $record->fields();
844 # search old MARC item
845 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=?");
846 $sth2->execute($bibid,$itemnumber);
847 my ($tagorder) = $sth2->fetchrow_array();
848 foreach my $field (@fields) {
849 my $oldfield = $oldrecord->field($field->tag());
850 my @subfields=$field->subfields();
852 foreach my $subfield (@subfields) {
854 # warn "compare : $oldfield".$oldfield->subfield(@$subfield[0]);
855 if ($oldfield eq 0 or (length($oldfield->subfield(@$subfield[0])) ==0) ) {
856 # just adding datas...
857 # warn "addfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
858 # warn "NEW subfield : $bibid,".$field->tag().",".$tagorder.",".@$subfield[0].",".$subfieldorder.",".@$subfield[1].")";
859 &MARCaddsubfield($dbh,$bibid,$field->tag(),$field->indicator(1).$field->indicator(2),
860 $tagorder,@$subfield[0],$subfieldorder,@$subfield[1]);
862 # warn "modfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
863 # modify he subfield if it's a different string
864 if ($oldfield->subfield(@$subfield[0]) ne @$subfield[1] ) {
865 my $subfieldid=&MARCfindsubfieldid($dbh,$bibid,$field->tag(),$tagorder,@$subfield[0],$subfieldorder);
866 # warn "changing : $subfieldid, $bibid,".$field->tag(),",$tagorder,@$subfield[0],@$subfield[1],$subfieldorder";
867 &MARCmodsubfield($dbh,$subfieldid,@$subfield[1]);
875 sub MARCmodsubfield {
876 # Subroutine changes a subfield value given a subfieldid.
877 my ($dbh, $subfieldid, $subfieldvalue )=@_;
878 $dbh->do("lock tables marc_blob_subfield WRITE,marc_subfield_table WRITE");
879 my $sth1=$dbh->prepare("select valuebloblink from marc_subfield_table where subfieldid=?");
880 $sth1->execute($subfieldid);
881 my ($oldvaluebloblink)=$sth1->fetchrow;
884 # if too long, use a bloblink
885 if (length($subfieldvalue)>255 ) {
886 # if already a bloblink, update it, otherwise, insert a new one.
887 if ($oldvaluebloblink) {
888 $sth=$dbh->prepare("update marc_blob_subfield set subfieldvalue=? where blobidlink=?");
889 $sth->execute($subfieldvalue,$oldvaluebloblink);
891 $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
892 $sth->execute($subfieldvalue);
893 $sth=$dbh->prepare("select max(blobidlink) from marc_blob_subfield");
895 my ($res)=$sth->fetchrow;
896 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=null, valuebloblink=$res where subfieldid=?");
897 $sth->execute($subfieldid);
900 # note this can leave orphan bloblink. Not a big problem, but we should build somewhere a orphan deleting script...
901 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=?,valuebloblink=null where subfieldid=?");
902 $sth->execute($subfieldvalue, $subfieldid);
904 $dbh->do("unlock tables");
906 $sth=$dbh->prepare("select bibid,tag,tagorder,subfieldcode,subfieldid,subfieldorder from marc_subfield_table where subfieldid=?");
907 $sth->execute($subfieldid);
908 my ($bibid,$tagid,$tagorder,$subfieldcode,$x,$subfieldorder) = $sth->fetchrow;
910 &MARCdelword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder);
911 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
912 return($subfieldid, $subfieldvalue);
915 sub MARCfindsubfield {
916 my ($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue) = @_;
920 my $query="select subfieldid from marc_subfield_table where bibid=? and tag=? and subfieldcode=?";
921 if ($subfieldvalue) {
922 $query .= " and subfieldvalue=".$dbh->quote($subfieldvalue);
924 if ($subfieldorder<1) {
927 $query .= " and subfieldorder=$subfieldorder";
929 my $sti=$dbh->prepare($query);
930 $sti->execute($bibid,$tag, $subfieldcode);
931 while (($subfieldid) = $sti->fetchrow) {
933 $lastsubfieldid=$subfieldid;
935 if ($resultcounter>1) {
936 # Error condition. Values given did not resolve into a unique record. Don't know what to edit
937 # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
940 return $lastsubfieldid;
944 sub MARCfindsubfieldid {
945 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
946 my $sth=$dbh->prepare("select subfieldid from marc_subfield_table
947 where bibid=? and tag=? and tagorder=?
948 and subfieldcode=? and subfieldorder=?");
949 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
950 my ($res) = $sth->fetchrow;
952 $sth=$dbh->prepare("select subfieldid from marc_subfield_table
953 where bibid=? and tag=? and tagorder=?
954 and subfieldcode=?");
955 $sth->execute($bibid,$tag,$tagorder,$subfield);
956 ($res) = $sth->fetchrow;
961 sub MARCdelsubfield {
962 # delete a subfield for $bibid / tag / tagorder / subfield / subfieldorder
963 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
964 $dbh->do("delete from marc_subfield_table where bibid='$bibid' and
965 tag='$tag' and tagorder='$tagorder'
966 and subfieldcode='$subfield' and subfieldorder='$subfieldorder
970 sub MARCkoha2marcBiblio {
971 # this function builds partial MARC::Record from the old koha-DB fields
972 my ($dbh,$biblionumber,$biblioitemnumber) = @_;
973 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
974 my $record = MARC::Record->new();
975 #--- if bibid, then retrieve old-style koha data
976 if ($biblionumber>0) {
977 my $sth2=$dbh->prepare("select biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp
978 from biblio where biblionumber=?");
979 $sth2->execute($biblionumber);
980 my $row=$sth2->fetchrow_hashref;
982 foreach $code (keys %$row) {
984 &MARCkoha2marcOnefield($sth,$record,"biblio.".$code,$row->{$code});
988 #--- if biblioitem, then retrieve old-style koha data
989 if ($biblioitemnumber>0) {
990 my $sth2=$dbh->prepare(" SELECT biblioitemnumber,biblionumber,volume,number,classification,
991 itemtype,url,isbn,issn,dewey,subclass,publicationyear,publishercode,
992 volumedate,volumeddesc,timestamp,illus,pages,notes AS bnotes,size,place
994 WHERE biblioitemnumber=?
996 $sth2->execute($biblioitemnumber);
997 my $row=$sth2->fetchrow_hashref;
999 foreach $code (keys %$row) {
1000 if ($row->{$code}) {
1001 &MARCkoha2marcOnefield($sth,$record,"biblioitems.".$code,$row->{$code});
1005 # other fields => additional authors, subjects, subtitles
1006 my $sth2=$dbh->prepare(" SELECT author FROM additionalauthors WHERE biblionumber=?");
1007 $sth2->execute($biblionumber);
1008 while (my $row=$sth2->fetchrow_hashref) {
1009 &MARCkoha2marcOnefield($sth,$record,"additionalauthors.author",$row->{'author'});
1011 my $sth2=$dbh->prepare(" SELECT subject FROM bibliosubject WHERE biblionumber=?");
1012 $sth2->execute($biblionumber);
1013 while (my $row=$sth2->fetchrow_hashref) {
1014 &MARCkoha2marcOnefield($sth,$record,"bibliosubject.subject",$row->{'subject'});
1016 my $sth2=$dbh->prepare(" SELECT subtitle FROM bibliosubtitle WHERE biblionumber=?");
1017 $sth2->execute($biblionumber);
1018 while (my $row=$sth2->fetchrow_hashref) {
1019 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.title",$row->{'subtitle'});
1024 sub MARCkoha2marcItem {
1025 # this function builds partial MARC::Record from the old koha-DB fields
1026 my ($dbh,$biblionumber,$itemnumber) = @_;
1027 # my $dbh=&C4Connect;
1028 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1029 my $record = MARC::Record->new();
1030 #--- if item, then retrieve old-style koha data
1031 if ($itemnumber>0) {
1032 # print STDERR "prepare $biblionumber,$itemnumber\n";
1033 my $sth2=$dbh->prepare("SELECT itemnumber,biblionumber,multivolumepart,biblioitemnumber,barcode,dateaccessioned,
1034 booksellerid,homebranch,price,replacementprice,replacementpricedate,datelastborrowed,
1035 datelastseen,multivolume,stack,notforloan,itemlost,wthdrawn,bulk,issues,renewals,
1036 reserves,restricted,binding,itemnotes,holdingbranch,timestamp
1038 WHERE itemnumber=?");
1039 $sth2->execute($itemnumber);
1040 my $row=$sth2->fetchrow_hashref;
1042 foreach $code (keys %$row) {
1043 if ($row->{$code}) {
1044 &MARCkoha2marcOnefield($sth,$record,"items.".$code,$row->{$code});
1051 sub MARCkoha2marcSubtitle {
1052 # this function builds partial MARC::Record from the old koha-DB fields
1053 my ($dbh,$bibnum,$subtitle) = @_;
1054 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1055 my $record = MARC::Record->new();
1056 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.subtitle",$subtitle);
1060 sub MARCkoha2marcOnefield {
1061 my ($sth,$record,$kohafieldname,$value)=@_;
1064 $sth->execute($kohafieldname);
1065 if (($tagfield,$tagsubfield)=$sth->fetchrow) {
1066 if ($record->field($tagfield)) {
1067 my $tag =$record->field($tagfield);
1069 $tag->add_subfields($tagsubfield,$value);
1070 $record->delete_field($tag);
1071 $record->add_fields($tag);
1074 $record->add_fields($tagfield," "," ",$tagsubfield => $value);
1081 my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
1083 my $record = MARC::Record->new();
1084 # my %subfieldlist=();
1085 my $prevvalue; # if tag <10
1086 my $field; # if tag >=10
1087 for (my $i=0; $i< @$rtags; $i++) {
1088 # rebuild MARC::Record
1089 if (@$rtags[$i] ne $prevtag) {
1090 if ($prevtag < 10) {
1092 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
1096 $record->add_fields($field);
1099 $indicators{@$rtags[$i]}.=' ';
1100 if (@$rtags[$i] <10) {
1101 $prevvalue= @$rvalues[$i];
1103 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
1105 $prevtag = @$rtags[$i];
1107 if (@$rtags[$i] <10) {
1108 $prevvalue=@$rvalues[$i];
1110 if (@$rvalues[$i]) {
1111 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
1114 $prevtag= @$rtags[$i];
1117 # the last has not been included inside the loop... do it now !
1118 $record->add_fields($field);
1119 # warn $record->as_formatted;
1124 my ($dbh,$record) = @_;
1125 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1127 my $sth2=$dbh->prepare("SHOW COLUMNS from biblio");
1130 # print STDERR $record->as_formatted;
1131 while (($field)=$sth2->fetchrow) {
1132 $result=&MARCmarc2kohaOneField($sth,"biblio",$field,$record,$result);
1134 $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
1136 while (($field)=$sth2->fetchrow) {
1137 if ($field eq 'notes') { $field = 'bnotes'; }
1138 $result=&MARCmarc2kohaOneField($sth,"biblioitems",$field,$record,$result);
1140 $sth2=$dbh->prepare("SHOW COLUMNS from items");
1142 while (($field)=$sth2->fetchrow) {
1143 $result = &MARCmarc2kohaOneField($sth,"items",$field,$record,$result);
1145 # additional authors : specific
1146 $result = &MARCmarc2kohaOneField($sth,"bibliosubtitle","subtitle",$record,$result);
1147 $result = &MARCmarc2kohaOneField($sth,"additionalauthors","additionalauthors",$record,$result);
1148 # modify copyrightdate to keep only the 1st year found
1149 my $temp = $result->{'copyrightdate'};
1150 $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
1152 $result->{'copyrightdate'} = $1;
1153 } else { # if no cYYYY, get the 1st date.
1154 $temp =~ m/(\d\d\d\d)/;
1155 $result->{'copyrightdate'} = $1;
1157 # modify publicationyear to keep only the 1st year found
1158 my $temp = $result->{'publicationyear'};
1159 $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
1161 $result->{'publicationyear'} = $1;
1162 } else { # if no cYYYY, get the 1st date.
1163 $temp =~ m/(\d\d\d\d)/;
1164 $result->{'publicationyear'} = $1;
1169 sub MARCmarc2kohaOneField {
1170 # FIXME ? if a field has a repeatable subfield that is used in old-db, only the 1st will be retrieved...
1171 my ($sth,$kohatable,$kohafield,$record,$result)= @_;
1172 # warn "kohatable / $kohafield / $result / ";
1176 $sth->execute($kohatable.".".$kohafield);
1177 ($tagfield,$subfield) = $sth->fetchrow;
1178 foreach my $field ($record->field($tagfield)) {
1179 if ($field->subfield($subfield)) {
1180 if ($result->{$kohafield}) {
1181 $result->{$kohafield} .= " | ".$field->subfield($subfield);
1183 $result->{$kohafield}=$field->subfield($subfield);
1191 # split a subfield string and adds it into the word table.
1193 my ($dbh,$bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$sentence) =@_;
1194 $sentence =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\})/ /g;
1195 my @words = split / /,$sentence;
1196 my $stopwords= C4::Context->stopwords;
1197 my $sth=$dbh->prepare("insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word)
1198 values (?,?,?,?,?,?,soundex(?))");
1199 foreach my $word (@words) {
1200 # we record only words longer than 2 car and not in stopwords hash
1201 if (length($word)>2 and !($stopwords->{uc($word)})) {
1202 $sth->execute($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,$word);
1204 warn "ERROR ==> insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word) values ($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,soundex($word))\n";
1211 # delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
1212 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
1213 my $sth=$dbh->prepare("delete from marc_word where bibid=? and tag=? and tagorder=? and subfieldid=? and subfieldorder=?");
1214 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
1219 # NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
1222 # all the following subs are useful to manage MARC-DB with complete MARC records.
1223 # it's used with marcimport, and marc management tools
1227 =item ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbibilio($dbh,$MARCRecord,$oldbiblio,$oldbiblioitem);
1229 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
1230 are builded from the MARC::Record. If they are passed, they are used.
1232 =item NEWnewitem($dbh, $record,$bibid);
1234 adds an item in the db.
1239 my ($dbh, $record, $oldbiblio, $oldbiblioitem) = @_;
1240 # note $oldbiblio and $oldbiblioitem are not mandatory.
1241 # if not present, they will be builded from $record with MARCmarc2koha function
1242 if (($oldbiblio) and not($oldbiblioitem)) {
1243 print STDERR "NEWnewbiblio : missing parameter\n";
1244 print "NEWnewbiblio : missing parameter : contact koha development team\n";
1250 $oldbibnum = OLDnewbiblio($dbh,$oldbiblio);
1251 $oldbiblioitem->{'biblionumber'} = $oldbibnum;
1252 $oldbibitemnum = OLDnewbiblioitem($dbh,$oldbiblioitem);
1254 my $olddata = MARCmarc2koha($dbh,$record);
1255 $oldbibnum = OLDnewbiblio($dbh,$olddata);
1256 $olddata->{'biblionumber'} = $oldbibnum;
1257 $oldbibitemnum = OLDnewbiblioitem($dbh,$olddata);
1259 # search subtiles, addiauthors and subjects
1260 my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author");
1261 my @addiauthfields = $record->field($tagfield);
1262 foreach my $addiauthfield (@addiauthfields) {
1263 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1264 foreach my $subfieldcount (0..$#addiauthsubfields) {
1265 OLDmodaddauthor($dbh,$oldbibnum,$addiauthsubfields[$subfieldcount]);
1268 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.title");
1269 my @subtitlefields = $record->field($tagfield);
1270 foreach my $subtitlefield (@subtitlefields) {
1271 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1272 foreach my $subfieldcount (0..$#subtitlesubfields) {
1273 OLDnewsubtitle($dbh,$oldbibnum,$subtitlesubfields[$subfieldcount]);
1276 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject");
1277 my @subj = $record->field($tagfield);
1278 foreach my $subject (@subj) {
1279 my @subjsubfield = $subject->subfield($tagsubfield);
1281 foreach my $subfieldcount (0..$#subjsubfield) {
1282 push @subjects,$subjsubfield[$subfieldcount];
1284 OLDmodsubject($dbh,$oldbibnum,1,@subjects);
1286 # we must add bibnum and bibitemnum in MARC::Record...
1287 # we build the new field with biblionumber and biblioitemnumber
1288 # we drop the original field
1289 # we add the new builded field.
1290 # NOTE : Works only if the field is ONLY for biblionumber and biblioitemnumber
1291 # (steve and paul : thinks 090 is a good choice)
1292 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1293 $sth->execute("biblio.biblionumber");
1294 (my $tagfield1, my $tagsubfield1) = $sth->fetchrow;
1295 $sth->execute("biblioitems.biblioitemnumber");
1296 (my $tagfield2, my $tagsubfield2) = $sth->fetchrow;
1297 if ($tagfield1 != $tagfield2) {
1298 warn "Error in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1299 print "Content-Type: text/html\n\nError in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1302 my $newfield = MARC::Field->new( $tagfield1,'','',
1303 "$tagsubfield1" => $oldbibnum,
1304 "$tagsubfield2" => $oldbibitemnum);
1305 # drop old field and create new one...
1306 my $old_field = $record->field($tagfield1);
1307 $record->delete_field($old_field);
1308 $record->add_fields($newfield);
1309 my $bibid = MARCaddbiblio($dbh,$record,$oldbibnum);
1310 return ($bibid,$oldbibnum,$oldbibitemnum );
1314 my ($dbh,$record,$bibid) =@_;
1315 &MARCmodbiblio($dbh,$bibid,$record,0);
1316 my $oldbiblio = MARCmarc2koha($dbh,$record);
1317 my $oldbiblionumber = OLDmodbiblio($dbh,$oldbiblio);
1318 OLDmodbibitem($dbh,$oldbiblio);
1319 # now, modify addi authors, subject, addititles.
1320 my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author");
1321 my @addiauthfields = $record->field($tagfield);
1322 foreach my $addiauthfield (@addiauthfields) {
1323 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1324 foreach my $subfieldcount (0..$#addiauthsubfields) {
1325 OLDmodaddauthor($dbh,$oldbiblionumber,$addiauthsubfields[$subfieldcount]);
1328 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle");
1329 my @subtitlefields = $record->field($tagfield);
1330 foreach my $subtitlefield (@subtitlefields) {
1331 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1332 foreach my $subfieldcount (0..$#subtitlesubfields) {
1333 OLDmodsubtitle($dbh,$oldbiblionumber,$subtitlesubfields[$subfieldcount]);
1336 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject");
1337 my @subj = $record->field($tagfield);
1338 foreach my $subject (@subj) {
1339 my @subjsubfield = $subject->subfield($tagsubfield);
1341 foreach my $subfieldcount (0..$#subjsubfield) {
1342 push @subjects,$subjsubfield[$subfieldcount];
1344 OLDmodsubject($dbh,$oldbiblionumber,1,@subjects);
1351 my ($dbh, $record,$bibid) = @_;
1352 # add item in old-DB
1353 my $item = &MARCmarc2koha($dbh,$record);
1354 # needs old biblionumber and biblioitemnumber
1355 $item->{'biblionumber'} = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
1356 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
1357 $sth->execute($item->{'biblionumber'});
1358 ($item->{'biblioitemnumber'}) = $sth->fetchrow;
1359 my ($itemnumber,$error) = &OLDnewitems($dbh,$item,$item->{barcode});
1360 # add itemnumber to MARC::Record before adding the item.
1361 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1362 &MARCkoha2marcOnefield($sth,$record,"items.itemnumber",$itemnumber);
1364 my $bib = &MARCadditem($dbh,$record,$item->{'biblionumber'});
1368 my ($dbh,$record,$bibid,$itemnumber,$delete) = @_;
1369 &MARCmoditem($dbh,$record,$bibid,$itemnumber,$delete);
1370 my $olditem = MARCmarc2koha($dbh,$record);
1371 OLDmoditem($dbh,$olditem);
1376 # OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
1380 =item $biblionumber = OLDnewbiblio($dbh,$biblio);
1382 adds a record in biblio table. Datas are in the hash $biblio.
1384 =item $biblionumber = OLDmodbiblio($dbh,$biblio);
1386 modify a record in biblio table. Datas are in the hash $biblio.
1388 =item OLDmodsubtitle($dbh,$bibnum,$subtitle);
1390 modify subtitles in bibliosubtitle table.
1392 =item OLDmodaddauthor($dbh,$bibnum,$author);
1394 adds or modify additional authors
1395 NOTE : Strange sub : seems to delete MANY and add only ONE author... maybe buggy ?
1397 =item $errors = OLDmodsubject($dbh,$bibnum, $force, @subject);
1399 modify/adds subjects
1401 =item OLDmodbibitem($dbh, $biblioitem);
1405 =item OLDmodnote($dbh,$bibitemnum,$note
1407 modify a note for a biblioitem
1409 =item OLDnewbiblioitem($dbh,$biblioitem);
1411 adds a biblioitem ($biblioitem is a hash with the values)
1413 =item OLDnewsubject($dbh,$bibnum);
1417 =item OLDnewsubtitle($dbh,$bibnum,$subtitle);
1419 create a new subtitle
1421 =item ($itemnumber,$errors)= OLDnewitems($dbh,$item,$barcode);
1423 create a item. $item is a hash and $barcode the barcode.
1425 =item OLDmoditem($dbh,$item);
1429 =item OLDdelitem($dbh,$itemnum);
1433 =item OLDdeletebiblioitem($dbh,$biblioitemnumber);
1435 deletes a biblioitem
1436 NOTE : not standard sub name. Should be OLDdelbiblioitem()
1438 =item OLDdelbiblio($dbh,$biblio);
1445 my ($dbh,$biblio) = @_;
1446 # my $dbh = &C4Connect;
1447 my $query = "Select max(biblionumber) from biblio";
1448 my $sth = $dbh->prepare($query);
1450 my $data = $sth->fetchrow_arrayref;
1451 my $bibnum = $$data[0] + 1;
1454 if ($biblio->{'seriestitle'}) { $series = 1 };
1456 $query = "insert into biblio set biblionumber = ?, title = ?, author = ?, copyrightdate = ?,
1457 serial = ?, seriestitle = ?, notes = ?, abstract = ?";
1458 $sth = $dbh->prepare($query);
1459 $sth->execute($bibnum,$biblio->{'title'},$biblio->{'author'},$biblio->{'copyright'},$series,$biblio->{'seriestitle'},$biblio->{'notes'},$biblio->{'abstract'});
1467 my ($dbh,$biblio) = @_;
1468 # my $dbh = C4Connect;
1472 $query = "Update biblio set title = ?, author = ?, abstract = ?, copyrightdate = ?,
1473 seriestitle = ?, serial = ?, unititle = ?, notes = ? where biblionumber = ?";
1474 $sth = $dbh->prepare($query);
1475 $sth->execute($biblio->{'title'},$biblio->{'author'},$biblio->{'abstract'},$biblio->{'copyrightdate'},
1476 $biblio->{'seriestitle'},$biblio->{'serial'},$biblio->{'unititle'},$biblio->{'notes'},$biblio->{'biblionumber'});
1479 return($biblio->{'biblionumber'});
1482 sub OLDmodsubtitle {
1483 my ($dbh,$bibnum, $subtitle) = @_;
1484 my $query = "update bibliosubtitle set subtitle = ? where biblionumber = ?";
1485 my $sth = $dbh->prepare($query);
1486 $sth->execute($subtitle,$bibnum);
1491 sub OLDmodaddauthor {
1492 my ($dbh,$bibnum, $author) = @_;
1493 # my $dbh = C4Connect;
1494 my $query = "Delete from additionalauthors where biblionumber = $bibnum";
1495 my $sth = $dbh->prepare($query);
1500 if ($author ne '') {
1501 $query = "Insert into additionalauthors set
1504 $sth = $dbh->prepare($query);
1506 $sth->execute($author,$bibnum);
1510 } # sub modaddauthor
1514 my ($dbh,$bibnum, $force, @subject) = @_;
1515 # my $dbh = C4Connect;
1516 my $count = @subject;
1518 for (my $i = 0; $i < $count; $i++) {
1519 $subject[$i] =~ s/^ //g;
1520 $subject[$i] =~ s/ $//g;
1521 my $query = "select * from catalogueentry where entrytype = 's' and catalogueentry = '$subject[$i]'";
1522 my $sth = $dbh->prepare($query);
1525 if (my $data = $sth->fetchrow_hashref) {
1527 if ($force eq $subject[$i] || $force eq 1) {
1528 # subject not in aut, chosen to force anway
1529 # so insert into cataloguentry so its in auth file
1530 $query = "Insert into catalogueentry (entrytype,catalogueentry) values ('s','$subject[$i]')";
1531 my $sth2 = $dbh->prepare($query);
1536 $error = "$subject[$i]\n does not exist in the subject authority file";
1537 $query = "Select * from catalogueentry where entrytype = 's' and (catalogueentry like '$subject[$i] %'
1538 or catalogueentry like '% $subject[$i] %' or catalogueentry like '% $subject[$i]')";
1539 my $sth2 = $dbh->prepare($query);
1541 while (my $data = $sth2->fetchrow_hashref) {
1542 $error .= "<br>$data->{'catalogueentry'}";
1550 my $query = "Delete from bibliosubject where biblionumber = $bibnum";
1551 my $sth = $dbh->prepare($query);
1554 for (my $i = 0; $i < $count; $i++) {
1555 $sth = $dbh->prepare("Insert into bibliosubject values ('$subject[$i]', $bibnum)");
1566 my ($dbh,$biblioitem) = @_;
1567 # my $dbh = C4Connect;
1570 $biblioitem->{'itemtype'} = $dbh->quote($biblioitem->{'itemtype'});
1571 $biblioitem->{'url'} = $dbh->quote($biblioitem->{'url'});
1572 $biblioitem->{'isbn'} = $dbh->quote($biblioitem->{'isbn'});
1573 $biblioitem->{'publishercode'} = $dbh->quote($biblioitem->{'publishercode'});
1574 $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
1575 $biblioitem->{'classification'} = $dbh->quote($biblioitem->{'classification'});
1576 $biblioitem->{'dewey'} = $dbh->quote($biblioitem->{'dewey'});
1577 $biblioitem->{'subclass'} = $dbh->quote($biblioitem->{'subclass'});
1578 $biblioitem->{'illus'} = $dbh->quote($biblioitem->{'illus'});
1579 $biblioitem->{'pages'} = $dbh->quote($biblioitem->{'pages'});
1580 $biblioitem->{'volumeddesc'} = $dbh->quote($biblioitem->{'volumeddesc'});
1581 $biblioitem->{'bnotes'} = $dbh->quote($biblioitem->{'bnotes'});
1582 $biblioitem->{'size'} = $dbh->quote($biblioitem->{'size'});
1583 $biblioitem->{'place'} = $dbh->quote($biblioitem->{'place'});
1585 $query = "Update biblioitems set
1586 itemtype = $biblioitem->{'itemtype'},
1587 url = $biblioitem->{'url'},
1588 isbn = $biblioitem->{'isbn'},
1589 publishercode = $biblioitem->{'publishercode'},
1590 publicationyear = $biblioitem->{'publicationyear'},
1591 classification = $biblioitem->{'classification'},
1592 dewey = $biblioitem->{'dewey'},
1593 subclass = $biblioitem->{'subclass'},
1594 illus = $biblioitem->{'illus'},
1595 pages = $biblioitem->{'pages'},
1596 volumeddesc = $biblioitem->{'volumeddesc'},
1597 notes = $biblioitem->{'bnotes'},
1598 size = $biblioitem->{'size'},
1599 place = $biblioitem->{'place'}
1600 where biblioitemnumber = $biblioitem->{'biblioitemnumber'}";
1610 my ($dbh,$bibitemnum,$note)=@_;
1611 # my $dbh=C4Connect;
1612 my $query="update biblioitems set notes='$note' where
1613 biblioitemnumber='$bibitemnum'";
1614 my $sth=$dbh->prepare($query);
1620 sub OLDnewbiblioitem {
1621 my ($dbh,$biblioitem) = @_;
1622 # my $dbh = C4Connect;
1623 my $query = "Select max(biblioitemnumber) from biblioitems";
1624 my $sth = $dbh->prepare($query);
1629 $data = $sth->fetchrow_arrayref;
1630 $bibitemnum = $$data[0] + 1;
1634 $sth = $dbh->prepare("insert into biblioitems set
1635 biblioitemnumber = ?, biblionumber = ?,
1636 volume = ?, number = ?,
1637 classification = ?, itemtype = ?,
1639 issn = ?, dewey = ?,
1640 subclass = ?, publicationyear = ?,
1641 publishercode = ?, volumedate = ?,
1642 volumeddesc = ?, illus = ?,
1643 pages = ?, notes = ?,
1645 marc = ?, place = ?");
1646 $sth->execute($bibitemnum, $biblioitem->{'biblionumber'},
1647 $biblioitem->{'volume'}, $biblioitem->{'number'},
1648 $biblioitem->{'classification'}, $biblioitem->{'itemtype'},
1649 $biblioitem->{'url'}, $biblioitem->{'isbn'},
1650 $biblioitem->{'issn'}, $biblioitem->{'dewey'},
1651 $biblioitem->{'subclass'}, $biblioitem->{'publicationyear'},
1652 $biblioitem->{'publishercode'}, $biblioitem->{'volumedate'},
1653 $biblioitem->{'volumeddesc'}, $biblioitem->{'illus'},
1654 $biblioitem->{'pages'}, $biblioitem->{'bnotes'},
1655 $biblioitem->{'size'}, $biblioitem->{'lccn'},
1656 $biblioitem->{'marc'}, $biblioitem->{'place'});
1659 return($bibitemnum);
1663 my ($dbh,$bibnum)=@_;
1664 my $query="insert into bibliosubject (biblionumber) values ($bibnum)";
1665 my $sth=$dbh->prepare($query);
1670 sub OLDnewsubtitle {
1671 my ($dbh,$bibnum, $subtitle) = @_;
1672 my $query = "insert into bibliosubtitle set biblionumber = ?, subtitle = ?";
1673 my $sth = $dbh->prepare($query);
1674 $sth->execute($bibnum,$subtitle);
1680 my ($dbh,$item, $barcode) = @_;
1681 # my $dbh = C4Connect;
1682 my $query = "Select max(itemnumber) from items";
1683 my $sth = $dbh->prepare($query);
1689 $data = $sth->fetchrow_hashref;
1690 $itemnumber = $data->{'max(itemnumber)'} + 1;
1692 # if dateaccessioned is provided, use it. Otherwise, set to NOW()
1693 if ($item->{'dateaccessioned'}) {
1694 $sth=$dbh->prepare("Insert into items set
1695 itemnumber = ?, biblionumber = ?,
1696 biblioitemnumber = ?, barcode = ?,
1697 booksellerid = ?, dateaccessioned = ?,
1698 homebranch = ?, holdingbranch = ?,
1699 price = ?, replacementprice = ?,
1700 replacementpricedate = NOW(), itemnotes = ?,
1703 $sth->execute($itemnumber, $item->{'biblionumber'},
1704 $item->{'biblioitemnumber'},$barcode,
1705 $item->{'booksellerid'},$item->{'dateaccessioned'},
1706 $item->{'homebranch'},$item->{'holdingbranch'},
1707 $item->{'price'},$item->{'replacementprice'},
1708 $item->{'itemnotes'},$item->{'loan'});
1710 $sth=$dbh->prepare("Insert into items set
1711 itemnumber = ?, biblionumber = ?,
1712 biblioitemnumber = ?, barcode = ?,
1713 booksellerid = ?, dateaccessioned = NOW(),
1714 homebranch = ?, holdingbranch = ?,
1715 price = ?, replacementprice = ?,
1716 replacementpricedate = NOW(), itemnotes = ?,
1719 $sth->execute($itemnumber, $item->{'biblionumber'},
1720 $item->{'biblioitemnumber'},$barcode,
1721 $item->{'booksellerid'},
1722 $item->{'homebranch'},$item->{'holdingbranch'},
1723 $item->{'price'},$item->{'replacementprice'},
1724 $item->{'itemnotes'},$item->{'loan'});
1726 if (defined $sth->errstr) {
1727 $error .= $sth->errstr;
1730 return($itemnumber,$error);
1734 my ($dbh,$item) = @_;
1735 # my ($dbh,$loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
1736 # my $dbh=C4Connect;
1737 $item->{'itemnum'}=$item->{'itemnumber'} unless $item->{'itemnum'};
1738 my $query="update items set barcode='$item->{'barcode'}',itemnotes='$item->{'notes'}'
1739 where itemnumber=$item->{'itemnum'}";
1740 if ($item->{'barcode'} eq ''){
1741 $item->{'notforloan'}=0 unless $item->{'notforloan'};
1742 $query="update items set notforloan=$item->{'notforloan'} where itemnumber=$item->{'itemnum'}";
1744 if ($item->{'lost'} ne ''){
1745 $query="update items set biblioitemnumber=$item->{'bibitemnum'},
1746 barcode='$item->{'barcode'}',
1747 itemnotes='$item->{'notes'}',
1748 homebranch='$item->{'homebranch'}',
1749 itemlost='$item->{'lost'}',
1750 wthdrawn='$item->{'wthdrawn'}'
1751 where itemnumber=$item->{'itemnum'}";
1753 if ($item->{'replacement'} ne ''){
1754 $query=~ s/ where/,replacementprice='$item->{'replacement'}' where/;
1756 my $sth=$dbh->prepare($query);
1763 my ($dbh,$itemnum)=@_;
1764 # my $dbh=C4Connect;
1765 my $query="select * from items where itemnumber=$itemnum";
1766 my $sth=$dbh->prepare($query);
1768 my @data=$sth->fetchrow_array;
1770 $query="Insert into deleteditems values (";
1771 foreach my $temp (@data){
1772 $query .= "'$temp',";
1776 $sth=$dbh->prepare($query);
1779 $query = "Delete from items where itemnumber=$itemnum";
1780 $sth=$dbh->prepare($query);
1786 sub OLDdeletebiblioitem {
1787 my ($dbh,$biblioitemnumber) = @_;
1788 # my $dbh = C4Connect;
1789 my $query = "Select * from biblioitems
1790 where biblioitemnumber = $biblioitemnumber";
1791 my $sth = $dbh->prepare($query);
1796 if (@results = $sth->fetchrow_array) {
1797 $query = "Insert into deletedbiblioitems values (";
1798 foreach my $value (@results) {
1799 $value = $dbh->quote($value);
1800 $query .= "$value,";
1803 $query =~ s/\,$/\)/;
1806 $query = "Delete from biblioitems
1807 where biblioitemnumber = $biblioitemnumber";
1811 # Now delete all the items attached to the biblioitem
1812 $query = "Select * from items where biblioitemnumber = $biblioitemnumber";
1813 $sth = $dbh->prepare($query);
1815 while (@results = $sth->fetchrow_array) {
1816 $query = "Insert into deleteditems values (";
1817 foreach my $value (@results) {
1818 $value = $dbh->quote($value);
1819 $query .= "$value,";
1821 $query =~ s/\,$/\)/;
1825 $query = "Delete from items where biblioitemnumber = $biblioitemnumber";
1828 } # sub deletebiblioitem
1831 my ($dbh,$biblio)=@_;
1832 my $query="select * from biblio where biblionumber=$biblio";
1833 my $sth=$dbh->prepare($query);
1835 if (my @data=$sth->fetchrow_array){
1837 $query="Insert into deletedbiblio values (";
1838 foreach my $temp (@data){
1839 $temp=~ s/\'/\\\'/g;
1840 $query .= "'$temp',";
1843 $sth=$dbh->prepare($query);
1846 $query = "Delete from biblio where biblionumber=$biblio";
1847 $sth=$dbh->prepare($query);
1862 my $dbh = C4::Context->dbh;
1863 my $query="Select count(*) from items where biblionumber=$biblio";
1865 my $sth=$dbh->prepare($query);
1867 my $data=$sth->fetchrow_hashref;
1869 return($data->{'count(*)'});
1874 ($order, $ordernumber) = &getorder($biblioitemnumber, $biblionumber);
1876 Looks up the order with the given biblionumber and biblioitemnumber.
1878 Returns a two-element array. C<$ordernumber> is the order number.
1879 C<$order> is a reference-to-hash describing the order; its keys are
1880 fields from the biblio, biblioitems, aqorders, and aqorderbreakdown
1881 tables of the Koha database.
1885 # FIXME - This is effectively identical to &C4::Catalogue::getorder.
1886 # Pick one and stick with it.
1889 my $dbh = C4::Context->dbh;
1890 my $query="Select ordernumber
1892 where biblionumber=? and biblioitemnumber=?";
1893 my $sth=$dbh->prepare($query);
1894 $sth->execute($bib,$bi);
1895 # FIXME - Use fetchrow_array(), since we're only interested in the one
1897 my $ordnum=$sth->fetchrow_hashref;
1899 my $order=getsingleorder($ordnum->{'ordernumber'});
1901 return ($order,$ordnum->{'ordernumber'});
1904 =item getsingleorder
1906 $order = &getsingleorder($ordernumber);
1908 Looks up an order by order number.
1910 Returns a reference-to-hash describing the order. The keys of
1911 C<$order> are fields from the biblio, biblioitems, aqorders, and
1912 aqorderbreakdown tables of the Koha database.
1916 # FIXME - This is effectively identical to
1917 # &C4::Catalogue::getsingleorder.
1918 # Pick one and stick with it.
1919 sub getsingleorder {
1921 my $dbh = C4::Context->dbh;
1922 my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown
1923 where aqorders.ordernumber=?
1924 and biblio.biblionumber=aqorders.biblionumber and
1925 biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
1926 aqorders.ordernumber=aqorderbreakdown.ordernumber";
1927 my $sth=$dbh->prepare($query);
1928 $sth->execute($ordnum);
1929 my $data=$sth->fetchrow_hashref;
1936 my $dbh = C4::Context->dbh;
1937 my $bibnum=OLDnewbiblio($dbh,$biblio);
1938 # finds new (MARC bibid
1939 # my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$bibnum);
1940 my $record = &MARCkoha2marcBiblio($dbh,$bibnum);
1941 MARCaddbiblio($dbh,$record,$bibnum);
1947 $biblionumber = &modbiblio($biblio);
1949 Update a biblio record.
1951 C<$biblio> is a reference-to-hash whose keys are the fields in the
1952 biblio table in the Koha database. All fields must be present, not
1953 just the ones you wish to change.
1955 C<&modbiblio> updates the record defined by
1956 C<$biblio-E<gt>{biblionumber}> with the values in C<$biblio>.
1958 C<&modbiblio> returns C<$biblio-E<gt>{biblionumber}> whether it was
1965 my $dbh = C4::Context->dbh;
1966 my $biblionumber=OLDmodbiblio($dbh,$biblio);
1967 my $record = MARCkoha2marcBiblio($dbh,$biblionumber,$biblionumber);
1968 # finds new (MARC bibid
1969 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
1970 MARCmodbiblio($dbh,$bibid,$record,0);
1971 return($biblionumber);
1976 &modsubtitle($biblionumber, $subtitle);
1978 Sets the subtitle of a book.
1980 C<$biblionumber> is the biblionumber of the book to modify.
1982 C<$subtitle> is the new subtitle.
1987 my ($bibnum, $subtitle) = @_;
1988 my $dbh = C4::Context->dbh;
1989 &OLDmodsubtitle($dbh,$bibnum,$subtitle);
1994 &modaddauthor($biblionumber, $author);
1996 Replaces all additional authors for the book with biblio number
1997 C<$biblionumber> with C<$author>. If C<$author> is the empty string,
1998 C<&modaddauthor> deletes all additional authors.
2003 my ($bibnum, $author) = @_;
2004 my $dbh = C4::Context->dbh;
2005 &OLDmodaddauthor($dbh,$bibnum,$author);
2006 } # sub modaddauthor
2010 $error = &modsubject($biblionumber, $force, @subjects);
2012 $force - a subject to force
2014 $error - Error message, or undef if successful.
2019 my ($bibnum, $force, @subject) = @_;
2020 my $dbh = C4::Context->dbh;
2021 my $error= &OLDmodsubject($dbh,$bibnum,$force, @subject);
2026 my ($biblioitem) = @_;
2027 my $dbh = C4::Context->dbh;
2028 &OLDmodbibitem($dbh,$biblioitem);
2032 my ($bibitemnum,$note)=@_;
2033 my $dbh = C4::Context->dbh;
2034 &OLDmodnote($dbh,$bibitemnum,$note);
2038 my ($biblioitem) = @_;
2039 my $dbh = C4::Context->dbh;
2040 my $bibitemnum = &OLDnewbiblioitem($dbh,$biblioitem);
2041 my $MARCbiblio= MARCkoha2marcBiblio($dbh,0,$bibitemnum); # the 0 means "do NOT retrieve biblio, only biblioitem, in the MARC record
2042 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblioitem->{biblionumber});
2043 &MARCaddbiblio($dbh,$MARCbiblio,$biblioitem->{biblionumber},$bibid);
2044 return($bibitemnum);
2049 my $dbh = C4::Context->dbh;
2050 &OLDnewsubject($dbh,$bibnum);
2054 my ($bibnum, $subtitle) = @_;
2055 my $dbh = C4::Context->dbh;
2056 &OLDnewsubtitle($dbh,$bibnum,$subtitle);
2060 my ($item, @barcodes) = @_;
2061 my $dbh = C4::Context->dbh;
2065 foreach my $barcode (@barcodes) {
2066 ($itemnumber,$error)=&OLDnewitems($dbh,$item,uc($barcode));
2068 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{biblionumber},$itemnumber);
2069 &MARCadditem($dbh,$MARCitem,$item->{biblionumber});
2076 my $dbh = C4::Context->dbh;
2077 &OLDmoditem($dbh,$item);
2078 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{'biblionumber'},$item->{'itemnum'});
2079 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$item->{biblionumber});
2080 &MARCmoditem($dbh,$MARCitem,$bibid,$item->{itemnum},0);
2084 my ($count,@barcodes)=@_;
2085 my $dbh = C4::Context->dbh;
2087 for (my $i=0;$i<$count;$i++){
2088 $barcodes[$i]=uc $barcodes[$i];
2089 my $query="Select * from items where barcode='$barcodes[$i]'";
2090 my $sth=$dbh->prepare($query);
2092 if (my $data=$sth->fetchrow_hashref){
2093 $error.=" Duplicate Barcode: $barcodes[$i]";
2101 my ($bibitemnum)=@_;
2102 my $dbh = C4::Context->dbh;
2103 my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
2104 my $sth=$dbh->prepare($query);
2106 my $data=$sth->fetchrow_hashref;
2108 return($data->{'count(*)'});
2113 my $dbh = C4::Context->dbh;
2114 &OLDdelitem($dbh,$itemnum);
2117 sub deletebiblioitem {
2118 my ($biblioitemnumber) = @_;
2119 my $dbh = C4::Context->dbh;
2120 &OLDdeletebiblioitem($dbh,$biblioitemnumber);
2121 } # sub deletebiblioitem
2126 my $dbh = C4::Context->dbh;
2127 &OLDdelbiblio($dbh,$biblio);
2128 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblio);
2129 &MARCdelbiblio($dbh,$bibid,0);
2133 my $dbh = C4::Context->dbh;
2134 my $query = "select * from itemtypes order by description";
2135 my $sth = $dbh->prepare($query);
2136 # || die "Cannot prepare $query" . $dbh->errstr;
2141 # || die "Cannot execute $query\n" . $sth->errstr;
2142 while (my $data = $sth->fetchrow_hashref) {
2143 $results[$count] = $data;
2148 return($count, @results);
2149 } # sub getitemtypes
2152 my ($biblionumber) = @_;
2153 my $dbh = C4::Context->dbh;
2154 my $query = "Select * from biblio where biblionumber = $biblionumber";
2155 my $sth = $dbh->prepare($query);
2156 # || die "Cannot prepare $query\n" . $dbh->errstr;
2161 # || die "Cannot execute $query\n" . $sth->errstr;
2162 while (my $data = $sth->fetchrow_hashref) {
2163 $results[$count] = $data;
2168 return($count, @results);
2172 my ($biblioitemnum) = @_;
2173 my $dbh = C4::Context->dbh;
2174 my $query = "Select * from biblioitems where
2175 biblioitemnumber = $biblioitemnum";
2176 my $sth = $dbh->prepare($query);
2182 while (my $data = $sth->fetchrow_hashref) {
2183 $results[$count] = $data;
2188 return($count, @results);
2189 } # sub getbiblioitem
2191 sub getbiblioitembybiblionumber {
2192 my ($biblionumber) = @_;
2193 my $dbh = C4::Context->dbh;
2194 my $query = "Select * from biblioitems where biblionumber =
2196 my $sth = $dbh->prepare($query);
2202 while (my $data = $sth->fetchrow_hashref) {
2203 $results[$count] = $data;
2208 return($count, @results);
2211 sub getitemsbybiblioitem {
2212 my ($biblioitemnum) = @_;
2213 my $dbh = C4::Context->dbh;
2214 my $query = "Select * from items, biblio where
2215 biblio.biblionumber = items.biblionumber and biblioitemnumber
2217 my $sth = $dbh->prepare($query);
2218 # || die "Cannot prepare $query\n" . $dbh->errstr;
2223 # || die "Cannot execute $query\n" . $sth->errstr;
2224 while (my $data = $sth->fetchrow_hashref) {
2225 $results[$count] = $data;
2230 return($count, @results);
2231 } # sub getitemsbybiblioitem
2235 # Subroutine to log changes to databases
2236 # Eventually, this subroutine will be used to create a log of all changes made,
2237 # with the possibility of "undo"ing some changes
2239 if ($database eq 'kohadb') {
2245 # print STDERR "KOHA: $type $section $item $original $new\n";
2246 } elsif ($database eq 'marc') {
2248 my $Record_ID=shift;
2251 my $subfield_ID=shift;
2254 # print STDERR "MARC: $type $Record_ID $tag $mark $subfield_ID $original $new\n";
2258 #------------------------------------------------
2261 #---------------------------------------
2262 # Find a biblio entry, or create a new one if it doesn't exist.
2263 # If a "subtitle" entry is in hash, add it to subtitle table
2264 sub getoraddbiblio {
2268 # FIXME - Unused argument
2269 $biblio, # hash ref to fields
2280 $dbh = C4::Context->dbh;
2282 print "<PRE>Looking for biblio </PRE>\n" if $debug;
2283 $sth=$dbh->prepare("select biblionumber
2285 where title=? and author=?
2286 and copyrightdate=? and seriestitle=?");
2288 $biblio->{title}, $biblio->{author},
2289 $biblio->{copyright}, $biblio->{seriestitle} );
2291 ($biblionumber) = $sth->fetchrow;
2292 print "<PRE>Biblio exists with number $biblionumber</PRE>\n" if $debug;
2294 # Doesn't exist. Add new one.
2295 print "<PRE>Adding biblio</PRE>\n" if $debug;
2296 ($biblionumber,$error)=&newbiblio($biblio);
2297 if ( $biblionumber ) {
2298 print "<PRE>Added with biblio number=$biblionumber</PRE>\n" if $debug;
2299 if ( $biblio->{subtitle} ) {
2300 &newsubtitle($biblionumber,$biblio->{subtitle} );
2303 print "<PRE>Couldn't add biblio: $error</PRE>\n" if $debug;
2307 return $biblionumber,$error;
2309 } # sub getoraddbiblio
2312 # converts ISO 5426 coded string to ISO 8859-1
2313 # sloppy code : should be improved in next issue
2314 my ($string,$encoding) = @_ ;
2316 # $encoding = C4::Context->preference("marcflavour") unless $encoding;
2317 if ($encoding eq "UNIMARC") {
2379 # this handles non-sorting blocks (if implementation requires this)
2380 $string = nsb_clean($_) ;
2381 } elsif ($encoding eq "USMARC" || $encoding eq "MARC21") {
2434 # this handles non-sorting blocks (if implementation requires this)
2435 $string = nsb_clean($_) ;
2442 my $NSB = '\x88' ; # NSB : begin Non Sorting Block
2443 my $NSE = '\x89' ; # NSE : Non Sorting Block end
2444 # handles non sorting blocks
2448 s/[ ]{0,1}$NSE/) /gm ;
2453 END { } # module clean-up code here (global destructor)
2459 Koha Developement team <info@koha.org>
2461 Paul POULAIN paul.poulain@free.fr