4 # Revision 1.63 2003/10/01 13:25:49 tipaul
5 # seems a char encoding problem modified something in char_decode sub... changing back to something that works...
7 # Revision 1.62 2003/09/17 14:21:13 tipaul
8 # fixing bug that makes a MARC biblio disappear when using full acquisition (order => recieve ==> MARC editor).
9 # Before this 2 lines fix, the MARC biblio was deleted during recieve, and had to be entirely recreated :-(
11 # Revision 1.61 2003/09/17 10:24:39 tipaul
12 # notforloan value in itemtype was overwritting notforloan value in a given item.
13 # I changed this behaviour :
14 # if notforloan is set for a given item, and NOT for all items from this itemtype, the notforloan is kept.
15 # If notforloan is set for itemtype, it's used (and impossible to loan a specific item from this itemtype)
17 # Revision 1.60 2003/09/04 14:11:23 tipaul
18 # fix for 593 (data duplication in MARC-DB)
20 # Revision 1.58 2003/08/06 12:54:52 tipaul
21 # fix for publicationyear : extracting numeric value from MARC string, like for copyrightdate.
22 # (note that copyrightdate still extracted to get numeric format)
24 # Revision 1.57 2003/07/15 23:09:18 slef
25 # change show columns to use biblioitems bnotes too
27 # Revision 1.56 2003/07/15 11:34:52 slef
28 # fixes from paul email
30 # Revision 1.55 2003/07/15 00:02:49 slef
31 # Work on bug 515... can we do a single-side rename of notes to bnotes?
33 # Revision 1.54 2003/07/11 11:51:32 tipaul
34 # *** empty log message ***
36 # Revision 1.52 2003/07/10 10:37:19 tipaul
37 # fix for copyrightdate problem, #514
39 # Revision 1.51 2003/07/02 14:47:17 tipaul
40 # fix for #519 : items.dateaccessioned imports incorrectly
42 # Revision 1.49 2003/06/17 11:21:13 tipaul
43 # improvments/fixes for z3950 support.
44 # * Works now even on ADD, not only on MODIFY
45 # * able to search on ISBN, author, title
47 # Revision 1.48 2003/06/16 09:22:53 rangi
48 # Just added an order clause to getitemtypes
50 # Revision 1.47 2003/05/20 16:22:44 tipaul
51 # fixing typo in Biblio.pm POD
53 # Revision 1.46 2003/05/19 13:45:18 tipaul
54 # support for subtitles, additional authors, subject.
55 # 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.
56 # Note that some OLD-DB subs are strange (dummy ?) see OLDmodsubject, OLDmodsubtitle, OLDmodaddiauthor in C4/Biblio.pm
57 # 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.
59 # Revision 1.45 2003/04/29 16:50:49 tipaul
60 # really proud of this commit :-)
61 # z3950 search and import seems to works fine.
62 # Let me explain how :
63 # * a "search z3950" button is added in the addbiblio template.
64 # * when clicked, a popup appears and z3950/search.pl is called
65 # * z3950/search.pl calls addz3950search in the DB
66 # * the z3950 daemon retrieve the records and stores them in z3950results AND in marc_breeding table.
67 # * as long as there as searches pending, the popup auto refresh every 2 seconds, and says how many searches are pending.
68 # * when the user clicks on a z3950 result => the parent popup is called with the requested biblio, and auto-filled
71 # * 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.
72 # * 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.
74 # Revision 1.44 2003/04/28 13:07:14 tipaul
75 # Those fixes solves the "internal server error" with MARC::Record 1.12.
76 # It was due to an illegal contruction in Koha : we tried to retrive subfields from <10 tags.
77 # That's not possible. MARC::Record accepted this in 0.93 version, but it was fixed after.
78 # Now, the construct/retrieving is OK !
80 # Revision 1.43 2003/04/10 13:56:02 tipaul
82 # * worked in 1.9.0, but not in 1.9.1 :
83 # - modif of a biblio didn't work
84 # - 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.
86 # * did not work before :
87 # - repeatable subfields now works correctly. Enter 2 subfields separated by | and they will be splitted during saving.
88 # - dropped the last subfield of the MARC form :-(
91 # - 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.
92 # Note the MARCdelbiblio has been rewritted to enable deletion of a biblio WITHOUT deleting items.
94 # Revision 1.42 2003/04/04 08:41:11 tipaul
95 # last commits before 1.9.1
97 # Revision 1.41 2003/04/01 12:26:43 tipaul
100 # Revision 1.40 2003/03/11 15:14:03 tipaul
103 # Revision 1.39 2003/03/07 16:35:42 tipaul
104 # * moving generic functions to Koha.pm
105 # * improvement of SearchMarc.pm
109 # Revision 1.38 2003/02/27 16:51:59 tipaul
110 # * moving prepare / execute to ? form.
113 # * road to 1.9.2 => acquisition and cataloguing merging
115 # Revision 1.37 2003/02/12 11:03:03 tipaul
116 # Support for 000 -> 010 fields.
117 # Those fields doesn't have subfields.
118 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
119 # 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.
121 # Revision 1.36 2003/02/12 11:01:01 tipaul
122 # Support for 000 -> 010 fields.
123 # Those fields doesn't have subfields.
124 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
125 # 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.
127 # Revision 1.35 2003/02/03 18:46:00 acli
128 # Minor factoring in C4/Biblio.pm, plus change to export the per-tag
129 # 'mandatory' property to a per-subfield 'tag_mandatory' template parameter,
130 # so that addbiblio.tmpl can distinguish between mandatory subfields in a
131 # mandatory tag and mandatory subfields in an optional tag
133 # Not-minor factoring in acqui.simple/addbiblio.pl to make the if-else blocks
134 # smaller, and to add some POD; need further testing for this
136 # Added function to check if a MARC subfield name is "koha-internal" (instead
137 # of checking it for 'lib' and 'tag' everywhere); temporarily added to Koha.pm
139 # Use above function in acqui.simple/additem.pl and search.marc/search.pl
141 # Revision 1.34 2003/01/28 14:50:04 tipaul
142 # fixing MARCmodbiblio API and reindenting code
144 # Revision 1.33 2003/01/23 12:22:37 tipaul
145 # adding char_decode to decode MARC21 or UNIMARC extended chars
147 # Revision 1.32 2002/12/16 15:08:50 tipaul
148 # small but important bugfix (fixes a problem in export)
150 # Revision 1.31 2002/12/13 16:22:04 tipaul
151 # 1st draft of marc export
153 # Revision 1.30 2002/12/12 21:26:35 tipaul
154 # YAB ! (Yet Another Bugfix) => related to biblio modif
155 # (some warning cleaning too)
157 # Revision 1.29 2002/12/12 16:35:00 tipaul
158 # adding authentification with Auth.pm and
159 # MAJOR BUGFIX on marc biblio modification
161 # Revision 1.28 2002/12/10 13:30:03 tipaul
162 # fugfixes from Dombes Abbey work
164 # Revision 1.27 2002/11/19 12:36:16 tipaul
166 # various bugfixes, improvments, and migration from acquisition.pm to biblio.pm
168 # Revision 1.26 2002/11/12 15:58:43 tipaul
171 # * 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)
173 # Revision 1.25 2002/10/25 10:58:26 tipaul
175 # * bugfixes and improvements
177 # Revision 1.24 2002/10/24 12:09:01 arensb
178 # Fixed "no title" warning when generating HTML documentation from POD.
180 # Revision 1.23 2002/10/16 12:43:08 arensb
181 # Added some FIXME comments.
183 # Revision 1.22 2002/10/15 13:39:17 tipaul
184 # removing Acquisition.pm
185 # deleting unused code in biblio.pm, rewriting POD and answering most FIXME comments
187 # Revision 1.21 2002/10/13 11:34:14 arensb
188 # Replaced expressions of the form "$x = $x <op> $y" with "$x <op>= $y".
189 # Thus, $x = $x+2 becomes $x += 2, and so forth.
191 # Revision 1.20 2002/10/13 08:28:32 arensb
192 # Deleted unused variables.
193 # Removed trailing whitespace.
195 # Revision 1.19 2002/10/13 05:56:10 arensb
196 # Added some FIXME comments.
198 # Revision 1.18 2002/10/11 12:34:53 arensb
199 # Replaced &requireDBI with C4::Context->dbh
201 # Revision 1.17 2002/10/10 14:48:25 tipaul
204 # Revision 1.16 2002/10/07 14:04:26 tipaul
205 # road to 1.3.1 : viewing MARC biblio
207 # Revision 1.15 2002/10/05 09:49:25 arensb
208 # Merged with arensb-context branch: use C4::Context->dbh instead of
209 # &C4Connect, and generally prefer C4::Context over C4::Database.
211 # Revision 1.14 2002/10/03 11:28:18 tipaul
212 # Extending Context.pm to add stopword management and using it in MARC-API.
213 # First benchmarks show a medium speed improvement, which is nice as this part is heavily called.
215 # Revision 1.13 2002/10/02 16:26:44 tipaul
218 # Revision 1.12.2.4 2002/10/05 07:09:31 arensb
219 # Merged in changes from main branch.
221 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
222 # Added a whole mess of FIXME comments.
224 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
225 # Added some missing semicolons.
227 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
228 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
231 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
232 # Added a whole mess of FIXME comments.
234 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
235 # Added some missing semicolons.
237 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
238 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
241 # Revision 1.12 2002/10/01 11:48:51 arensb
242 # Added some FIXME comments, mostly marking duplicate functions.
244 # Revision 1.11 2002/09/24 13:49:26 tipaul
245 # long WAS the road to 1.3.0...
246 # coming VERY SOON NOW...
247 # modifying installer and buildrelease to update the DB
249 # Revision 1.10 2002/09/22 16:50:08 arensb
250 # Added some FIXME comments.
252 # Revision 1.9 2002/09/20 12:57:46 tipaul
253 # long is the road to 1.4.0
254 # * MARCadditem and MARCmoditem now wroks
255 # * various bugfixes in MARC management
256 # !!! 1.3.0 should be released very soon now. Be careful !!!
258 # Revision 1.8 2002/09/10 13:53:52 tipaul
259 # MARC API continued...
261 # * 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)
263 # Note : it should not be hard for marcimport and marcexport to re-link fields from internal tag/subfield to "legal" tag/subfield.
265 # Revision 1.7 2002/08/14 18:12:51 tonnesen
266 # Added copyright statement to all .pl and .pm files
268 # Revision 1.6 2002/07/25 13:40:31 tipaul
269 # pod documenting the API.
271 # Revision 1.5 2002/07/24 16:11:37 tipaul
273 # Database.pm and Output.pm are almost not modified (var test...)
275 # Biblio.pm is almost completly rewritten.
277 # WHAT DOES IT ??? ==> END of Hitchcock suspens
279 # 1st, it does... nothing...
280 # 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 ...
282 # All old-API functions have been cloned. for example, the "newbiblio" sub, now has become :
283 # * a "newbiblio" sub, with the same parameters. It just call a sub named OLDnewbiblio
284 # * 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.
285 # * 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.
286 # 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 ;-)
288 # 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.
289 # Note we have decided with steve that a old-biblio <=> a MARC-Biblio.
293 # Copyright 2000-2002 Katipo Communications
295 # This file is part of Koha.
297 # Koha is free software; you can redistribute it and/or modify it under the
298 # terms of the GNU General Public License as published by the Free Software
299 # Foundation; either version 2 of the License, or (at your option) any later
302 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
303 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
304 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
306 # You should have received a copy of the GNU General Public License along with
307 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
308 # Suite 330, Boston, MA 02111-1307 USA
316 use vars qw($VERSION @ISA @EXPORT);
318 # set the version for version checking
323 # don't forget MARCxxx subs are here only for testing purposes. Should not be used
324 # as the old-style API and the NEW one are the only public functions.
327 &updateBiblio &updateBiblioItem &updateItem
328 &itemcount &newbiblio &newbiblioitem
329 &modnote &newsubject &newsubtitle
330 &modbiblio &checkitems
331 &newitems &modbibitem
332 &modsubtitle &modsubject &modaddauthor &moditem &countitems
333 &delitem &deletebiblioitem &delbiblio
334 &getitemtypes &getbiblio
335 &getbiblioitembybiblionumber
336 &getbiblioitem &getitemsbybiblioitem
338 &newcompletebiblioitem
340 &MARCfind_oldbiblionumber_from_MARCbibid
341 &MARCfind_MARCbibid_from_oldbiblionumber
342 &MARCfind_marc_from_kohafield
346 &NEWnewbiblio &NEWnewitem
347 &NEWmodbiblio &NEWmoditem
349 &MARCaddbiblio &MARCadditem
350 &MARCmodsubfield &MARCaddsubfield
351 &MARCmodbiblio &MARCmoditem
352 &MARCkoha2marcBiblio &MARCmarc2koha
353 &MARCkoha2marcItem &MARChtml2marc
354 &MARCgetbiblio &MARCgetitem
355 &MARCaddword &MARCdelword
361 # MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC
364 # all the following subs takes a MARC::Record as parameter and manage
365 # the MARC-DB. They are called by the 1.0/1.2 xxx subs, and by the
366 # NEWxxx subs (xxx deals with old-DB parameters, the NEWxxx deals with MARC-DB parameter)
370 C4::Biblio - acquisition, catalog management functions
374 move from 1.2 to 1.4 version :
375 1.2 and previous version uses a specific API to manage biblios. This API uses old-DB style parameters.
376 In the 1.4 version, we want to do 2 differents things :
377 - keep populating the old-DB, that has a LOT less datas than MARC
378 - populate the MARC-DB
379 To populate the DBs we have 2 differents sources :
380 - the standard acquisition system (through book sellers), that does'nt use MARC data
381 - the MARC acquisition system, that uses MARC data.
383 Thus, we have 2 differents cases :
384 - 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
385 - 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
387 That's why we need 4 subs :
388 all I<subs beginning by MARC> manage only MARC tables. They manage MARC-DB with MARC::Record parameters
389 all I<subs beginning by OLD> manage only OLD-DB tables. They manage old-DB with old-DB parameters
390 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
391 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.
393 - NEW and old-style API should be used in koha to manage biblio
394 - MARCsubs are divided in 2 parts :
395 * some of them manage MARC parameters. They are heavily used in koha.
396 * some of them manage MARC biblio : they are mostly used by NEW and old-style subs.
397 - OLD are used internally only
399 all subs requires/use $dbh as 1st parameter.
401 I<NEWxxx related subs>
403 all subs requires/use $dbh as 1st parameter.
404 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
406 I<OLDxxx related subs>
408 all subs requires/use $dbh as 1st parameter.
409 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
411 They all are the exact copy of 1.0/1.2 version of the sub without the OLD.
412 The OLDxxx is called by the original xxx sub.
413 the 1.4 xxx sub also builds MARC::Record an calls the MARCxxx
415 WARNING : there is 1 difference between initialxxx and OLDxxx :
416 the db header $dbh is always passed as parameter to avoid over-DB connexion
422 =item @tagslib = &MARCgettagslib($dbh,1|0);
424 last param is 1 for liblibrarian and 0 for libopac
425 returns a hash with tag/subfield meaning
426 =item ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,$kohafield);
428 finds MARC tag and subfield for a given kohafield
429 kohafield is "table.field" where table= biblio|biblioitems|items, and field a field of the previous table
431 =item $biblionumber = &MARCfind_oldbiblionumber_from_MARCbibid($dbh,$MARCbibi);
433 finds a old-db biblio number for a given MARCbibid number
435 =item $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
437 finds a MARC bibid from a old-db biblionumber
439 =item $MARCRecord = &MARCkoha2marcBiblio($dbh,$biblionumber,biblioitemnumber);
441 MARCkoha2marcBiblio is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB biblio/biblioitem
443 =item $MARCRecord = &MARCkoha2marcItem($dbh,$biblionumber,itemnumber);
445 MARCkoha2marcItem is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB item
447 =item $MARCRecord = &MARCkoha2marcSubtitle($dbh,$biblionumber,$subtitle);
449 MARCkoha2marcSubtitle is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB subtitle
451 =item $olddb = &MARCmarc2koha($dbh,$MARCRecord);
453 builds a hash with old-db datas from a MARC::Record
455 =item &MARCaddbiblio($dbh,$MARC::Record,$biblionumber);
457 creates a biblio (in the MARC tables only). $biblionumber is the old-db biblionumber of the biblio
459 =item &MARCaddsubfield($dbh,$bibid,$tagid,$indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
461 adds a subfield in a biblio (in the MARC tables only).
463 =item $MARCRecord = &MARCgetbiblio($dbh,$bibid);
465 Returns a MARC::Record for the biblio $bibid.
467 =item &MARCmodbiblio($dbh,$bibid,$record,$delete);
469 MARCmodbiblio changes a biblio for a biblio,MARC::Record passed as parameter
470 It 1st delete the biblio, then recreates it.
471 WARNING : the $delete parameter is not used anymore (too much unsolvable cases).
472 =item ($subfieldid,$subfieldvalue) = &MARCmodsubfield($dbh,$subfieldid,$subfieldvalue);
474 MARCmodsubfield changes the value of a given subfield
476 =item $subfieldid = &MARCfindsubfield($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue);
478 MARCfindsubfield returns a subfield number given a bibid/tag/subfieldvalue values.
479 Returns -1 if more than 1 answer
481 =item $subfieldid = &MARCfindsubfieldid($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
483 MARCfindsubfieldid find a subfieldid for a bibid/tag/tagorder/subfield/subfieldorder
485 =item &MARCdelsubfield($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
487 MARCdelsubfield delete a subfield for a bibid/tag/tagorder/subfield/subfieldorder
489 =item &MARCdelbiblio($dbh,$bibid);
491 MARCdelbiblio delete biblio $bibid
493 =item &MARCkoha2marcOnefield
495 used by MARCkoha2marc and should not be useful elsewhere
497 =item &MARCmarc2kohaOnefield
499 used by MARCmarc2koha and should not be useful elsewhere
503 used to manage MARC_word table and should not be useful elsewhere
507 used to manage MARC_word table and should not be useful elsewhere
512 my ($dbh,$forlibrarian)= @_;
514 my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
515 $sth=$dbh->prepare("select tagfield,$libfield as lib,mandatory from marc_tag_structure order by tagfield");
517 my ($lib,$tag,$res,$tab,$mandatory,$repeatable);
518 while ( ($tag,$lib,$mandatory) = $sth->fetchrow) {
519 $res->{$tag}->{lib}=$lib;
520 $res->{$tab}->{tab}=""; # XXX
521 $res->{$tag}->{mandatory}=$mandatory;
524 $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");
528 my $authorised_value;
529 my $thesaurus_category;
532 while ( ($tag, $subfield, $lib, $tab, $mandatory, $repeatable,$authorised_value,$thesaurus_category,$value_builder,$kohafield) = $sth->fetchrow) {
533 $res->{$tag}->{$subfield}->{lib}=$lib;
534 $res->{$tag}->{$subfield}->{tab}=$tab;
535 $res->{$tag}->{$subfield}->{mandatory}=$mandatory;
536 $res->{$tag}->{$subfield}->{repeatable}=$repeatable;
537 $res->{$tag}->{$subfield}->{authorised_value}=$authorised_value;
538 $res->{$tag}->{$subfield}->{thesaurus_category}=$thesaurus_category;
539 $res->{$tag}->{$subfield}->{value_builder}=$value_builder;
540 $res->{$tag}->{$subfield}->{kohafield}=$kohafield;
545 sub MARCfind_marc_from_kohafield {
546 my ($dbh,$kohafield) = @_;
547 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
548 $sth->execute($kohafield);
549 my ($tagfield,$tagsubfield) = $sth->fetchrow;
550 return ($tagfield,$tagsubfield);
553 sub MARCfind_oldbiblionumber_from_MARCbibid {
554 my ($dbh,$MARCbibid) = @_;
555 my $sth=$dbh->prepare("select biblionumber from marc_biblio where bibid=?");
556 $sth->execute($MARCbibid);
557 my ($biblionumber) = $sth->fetchrow;
558 return $biblionumber;
561 sub MARCfind_MARCbibid_from_oldbiblionumber {
562 my ($dbh,$oldbiblionumber) = @_;
563 my $sth=$dbh->prepare("select bibid from marc_biblio where biblionumber=?");
564 $sth->execute($oldbiblionumber);
565 my ($bibid) = $sth->fetchrow;
570 # pass the MARC::Record to this function, and it will create the records in the marc tables
571 my ($dbh,$record,$biblionumber,$bibid) = @_;
572 my @fields=$record->fields();
573 # warn "IN MARCaddbiblio $bibid => ".$record->as_formatted;
575 # adding main table, and retrieving bibid
576 # if bibid is sent, then it's not a true add, it's only a re-add, after a delete (ie, a mod)
577 # if bibid empty => true add, find a new bibid number
579 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
580 my $sth=$dbh->prepare("insert into marc_biblio (datecreated,biblionumber) values (now(),?)");
581 $sth->execute($biblionumber);
582 $sth=$dbh->prepare("select max(bibid) from marc_biblio");
584 ($bibid)=$sth->fetchrow;
588 # now, add subfields...
589 foreach my $field (@fields) {
591 if ($field->tag() <10) {
592 &MARCaddsubfield($dbh,$bibid,
601 my @subfields=$field->subfields();
602 foreach my $subfieldcount (0..$#subfields) {
603 &MARCaddsubfield($dbh,$bibid,
605 $field->indicator(1).$field->indicator(2),
607 $subfields[$subfieldcount][0],
609 $subfields[$subfieldcount][1]
614 $dbh->do("unlock tables");
619 # pass the MARC::Record to this function, and it will create the records in the marc tables
620 my ($dbh,$record,$biblionumber) = @_;
621 # warn "adding : ".$record->as_formatted();
622 # search for MARC biblionumber
623 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
624 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
625 my @fields=$record->fields();
626 my $sth = $dbh->prepare("select max(tagorder) from marc_subfield_table where bibid=?");
627 $sth->execute($bibid);
628 my ($fieldcount) = $sth->fetchrow;
629 # now, add subfields...
630 foreach my $field (@fields) {
631 my @subfields=$field->subfields();
633 foreach my $subfieldcount (0..$#subfields) {
634 &MARCaddsubfield($dbh,$bibid,
636 $field->indicator(1).$field->indicator(2),
638 $subfields[$subfieldcount][0],
640 $subfields[$subfieldcount][1]
644 $dbh->do("unlock tables");
648 sub MARCaddsubfield {
649 # Add a new subfield to a tag into the DB.
650 my ($dbh,$bibid,$tagid,$tag_indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalues) = @_;
651 # if not value, end of job, we do nothing
652 if (length($subfieldvalues) ==0) {
655 if (not($subfieldcode)) {
658 my @subfieldvalues = split /\|/,$subfieldvalues;
659 foreach my $subfieldvalue (@subfieldvalues) {
660 if (length($subfieldvalue)>255) {
661 $dbh->do("lock tables marc_blob_subfield WRITE, marc_subfield_table WRITE");
662 my $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
663 $sth->execute($subfieldvalue);
664 $sth=$dbh->prepare("select max(blobidlink)from marc_blob_subfield");
666 my ($res)=$sth->fetchrow;
667 $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,valuebloblink) values (?,?,?,?,?,?,?)");
668 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$res);
670 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";
672 $dbh->do("unlock tables");
674 my $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?,?)");
675 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue);
677 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";
680 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
685 # Returns MARC::Record of the biblio passed in parameter.
687 my $record = MARC::Record->new();
688 #---- TODO : the leader is missing
689 $record->leader(' ');
690 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
691 from marc_subfield_table
692 where bibid=? order by tag,tagorder,subfieldcode
694 my $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
695 $sth->execute($bibid);
699 my $field; # for >=10 tags
700 my $prevvalue; # for <10 tags
701 while (my $row=$sth->fetchrow_hashref) {
702 if ($row->{'valuebloblink'}) { #---- search blob if there is one
703 $sth2->execute($row->{'valuebloblink'});
704 my $row2=$sth2->fetchrow_hashref;
706 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
708 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
711 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
713 $record->add_fields($field) unless $prevtag eq "XXX";
716 $prevtagorder=$row->{tagorder};
717 $prevtag = $row->{tag};
718 $previndicator=$row->{tag_indicator};
719 if ($row->{tag}<10) {
720 $prevvalue = $row->{subfieldvalue};
722 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.' ',0,1), substr($row->{tag_indicator}.' ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
725 if ($row->{tag} <10) {
726 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
728 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
730 $prevtag= $row->{tag};
731 $previndicator=$row->{tag_indicator};
734 # the last has not been included inside the loop... do it now !
736 $record->add_fields($prevtag,$prevvalue);
738 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
739 $record->add_fields($field);
744 # Returns MARC::Record of the biblio passed in parameter.
745 my ($dbh,$bibid,$itemnumber)=@_;
746 my $record = MARC::Record->new();
747 # search MARC tagorder
748 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=?");
749 $sth2->execute($bibid,$itemnumber);
750 my ($tagorder) = $sth2->fetchrow_array();
751 #---- TODO : the leader is missing
752 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
753 from marc_subfield_table
754 where bibid=? and tagorder=? order by subfieldcode,subfieldorder
756 $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
757 $sth->execute($bibid,$tagorder);
758 while (my $row=$sth->fetchrow_hashref) {
759 if ($row->{'valuebloblink'}) { #---- search blob if there is one
760 $sth2->execute($row->{'valuebloblink'});
761 my $row2=$sth2->fetchrow_hashref;
763 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
765 if ($record->field($row->{'tag'})) {
767 #--- this test must stay as this, because of strange behaviour of mySQL/Perl DBI with char var containing a number...
768 #--- sometimes, eliminates 0 at beginning, sometimes no ;-\\\
769 if (length($row->{'tag'}) <3) {
770 $row->{'tag'} = "0".$row->{'tag'};
772 $field =$record->field($row->{'tag'});
774 my $x = $field->add_subfields($row->{'subfieldcode'},$row->{'subfieldvalue'});
775 $record->delete_field($field);
776 $record->add_fields($field);
779 if (length($row->{'tag'}) < 3) {
780 $row->{'tag'} = "0".$row->{'tag'};
782 my $temp = MARC::Field->new($row->{'tag'}," "," ", $row->{'subfieldcode'} => $row->{'subfieldvalue'});
783 $record->add_fields($temp);
791 my ($dbh,$bibid,$record,$delete)=@_;
792 my $oldrecord=&MARCgetbiblio($dbh,$bibid);
793 if ($oldrecord eq $record) {
796 # 1st delete the biblio,
798 my $biblionumber = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
799 &MARCdelbiblio($dbh,$bibid,1);
800 &MARCaddbiblio($dbh,$record,$biblionumber,$bibid);
804 my ($dbh,$bibid,$keep_items) = @_;
805 # if the keep_item is set to 1, then all items are preserved.
806 # This flag is set when the delbiblio is called by modbiblio
807 # due to a too complex structure of MARC (repeatable fields and subfields),
808 # the best solution for a modif is to delete / recreate the record.
809 if ($keep_items eq 1) {
810 #search item field code
811 my $sth = $dbh->prepare("select tagfield from marc_subfield_structure where kohafield like 'items.%'");
813 my $itemtag = $sth->fetchrow_hashref->{tagfield};
814 $dbh->do("delete from marc_subfield_table where bibid=$bibid and tag<>$itemtag");
815 $dbh->do("delete from marc_word where bibid=$bibid and tag<>$itemtag");
817 $dbh->do("delete from marc_biblio where bibid=$bibid");
818 $dbh->do("delete from marc_subfield_table where bibid=$bibid");
819 $dbh->do("delete from marc_word where bibid=$bibid");
823 my ($dbh,$record,$bibid,$itemnumber,$delete)=@_;
824 my $oldrecord=&MARCgetitem($dbh,$bibid,$itemnumber);
825 # if nothing to change, don't waste time...
826 if ($oldrecord eq $record) {
830 # otherwise, skip through each subfield...
831 my @fields = $record->fields();
832 # search old MARC item
833 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=?");
834 $sth2->execute($bibid,$itemnumber);
835 my ($tagorder) = $sth2->fetchrow_array();
836 foreach my $field (@fields) {
837 my $oldfield = $oldrecord->field($field->tag());
838 my @subfields=$field->subfields();
840 foreach my $subfield (@subfields) {
842 # warn "compare : $oldfield".$oldfield->subfield(@$subfield[0]);
843 if ($oldfield eq 0 or (length($oldfield->subfield(@$subfield[0])) ==0) ) {
844 # just adding datas...
845 # warn "addfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
846 # warn "NEW subfield : $bibid,".$field->tag().",".$tagorder.",".@$subfield[0].",".$subfieldorder.",".@$subfield[1].")";
847 &MARCaddsubfield($dbh,$bibid,$field->tag(),$field->indicator(1).$field->indicator(2),
848 $tagorder,@$subfield[0],$subfieldorder,@$subfield[1]);
850 # warn "modfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
851 # modify he subfield if it's a different string
852 if ($oldfield->subfield(@$subfield[0]) ne @$subfield[1] ) {
853 my $subfieldid=&MARCfindsubfieldid($dbh,$bibid,$field->tag(),$tagorder,@$subfield[0],$subfieldorder);
854 # warn "changing : $subfieldid, $bibid,".$field->tag(),",$tagorder,@$subfield[0],@$subfield[1],$subfieldorder";
855 &MARCmodsubfield($dbh,$subfieldid,@$subfield[1]);
863 sub MARCmodsubfield {
864 # Subroutine changes a subfield value given a subfieldid.
865 my ($dbh, $subfieldid, $subfieldvalue )=@_;
866 $dbh->do("lock tables marc_blob_subfield WRITE,marc_subfield_table WRITE");
867 my $sth1=$dbh->prepare("select valuebloblink from marc_subfield_table where subfieldid=?");
868 $sth1->execute($subfieldid);
869 my ($oldvaluebloblink)=$sth1->fetchrow;
872 # if too long, use a bloblink
873 if (length($subfieldvalue)>255 ) {
874 # if already a bloblink, update it, otherwise, insert a new one.
875 if ($oldvaluebloblink) {
876 $sth=$dbh->prepare("update marc_blob_subfield set subfieldvalue=? where blobidlink=?");
877 $sth->execute($subfieldvalue,$oldvaluebloblink);
879 $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
880 $sth->execute($subfieldvalue);
881 $sth=$dbh->prepare("select max(blobidlink) from marc_blob_subfield");
883 my ($res)=$sth->fetchrow;
884 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=null, valuebloblink=$res where subfieldid=?");
885 $sth->execute($subfieldid);
888 # note this can leave orphan bloblink. Not a big problem, but we should build somewhere a orphan deleting script...
889 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=?,valuebloblink=null where subfieldid=?");
890 $sth->execute($subfieldvalue, $subfieldid);
892 $dbh->do("unlock tables");
894 $sth=$dbh->prepare("select bibid,tag,tagorder,subfieldcode,subfieldid,subfieldorder from marc_subfield_table where subfieldid=?");
895 $sth->execute($subfieldid);
896 my ($bibid,$tagid,$tagorder,$subfieldcode,$x,$subfieldorder) = $sth->fetchrow;
898 &MARCdelword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder);
899 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
900 return($subfieldid, $subfieldvalue);
903 sub MARCfindsubfield {
904 my ($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue) = @_;
908 my $query="select subfieldid from marc_subfield_table where bibid=? and tag=? and subfieldcode=?";
909 if ($subfieldvalue) {
910 $query .= " and subfieldvalue=".$dbh->quote($subfieldvalue);
912 if ($subfieldorder<1) {
915 $query .= " and subfieldorder=$subfieldorder";
917 my $sti=$dbh->prepare($query);
918 $sti->execute($bibid,$tag, $subfieldcode);
919 while (($subfieldid) = $sti->fetchrow) {
921 $lastsubfieldid=$subfieldid;
923 if ($resultcounter>1) {
924 # Error condition. Values given did not resolve into a unique record. Don't know what to edit
925 # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
928 return $lastsubfieldid;
932 sub MARCfindsubfieldid {
933 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
934 my $sth=$dbh->prepare("select subfieldid from marc_subfield_table
935 where bibid=? and tag=? and tagorder=?
936 and subfieldcode=? and subfieldorder=?");
937 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
938 my ($res) = $sth->fetchrow;
940 $sth=$dbh->prepare("select subfieldid from marc_subfield_table
941 where bibid=? and tag=? and tagorder=?
942 and subfieldcode=?");
943 $sth->execute($bibid,$tag,$tagorder,$subfield);
944 ($res) = $sth->fetchrow;
949 sub MARCdelsubfield {
950 # delete a subfield for $bibid / tag / tagorder / subfield / subfieldorder
951 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
952 $dbh->do("delete from marc_subfield_table where bibid='$bibid' and
953 tag='$tag' and tagorder='$tagorder'
954 and subfieldcode='$subfield' and subfieldorder='$subfieldorder
958 sub MARCkoha2marcBiblio {
959 # this function builds partial MARC::Record from the old koha-DB fields
960 my ($dbh,$biblionumber,$biblioitemnumber) = @_;
961 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
962 my $record = MARC::Record->new();
963 #--- if bibid, then retrieve old-style koha data
964 if ($biblionumber>0) {
965 my $sth2=$dbh->prepare("select biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp
966 from biblio where biblionumber=?");
967 $sth2->execute($biblionumber);
968 my $row=$sth2->fetchrow_hashref;
970 foreach $code (keys %$row) {
972 &MARCkoha2marcOnefield($sth,$record,"biblio.".$code,$row->{$code});
976 #--- if biblioitem, then retrieve old-style koha data
977 if ($biblioitemnumber>0) {
978 my $sth2=$dbh->prepare(" SELECT biblioitemnumber,biblionumber,volume,number,classification,
979 itemtype,url,isbn,issn,dewey,subclass,publicationyear,publishercode,
980 volumedate,volumeddesc,timestamp,illus,pages,notes AS bnotes,size,place
982 WHERE biblioitemnumber=?
984 $sth2->execute($biblioitemnumber);
985 my $row=$sth2->fetchrow_hashref;
987 foreach $code (keys %$row) {
989 &MARCkoha2marcOnefield($sth,$record,"biblioitems.".$code,$row->{$code});
993 # other fields => additional authors, subjects, subtitles
994 my $sth2=$dbh->prepare(" SELECT author FROM additionalauthors WHERE biblionumber=?");
995 $sth2->execute($biblionumber);
996 while (my $row=$sth2->fetchrow_hashref) {
997 &MARCkoha2marcOnefield($sth,$record,"additionalauthors.author",$row->{'author'});
999 my $sth2=$dbh->prepare(" SELECT subject FROM bibliosubject WHERE biblionumber=?");
1000 $sth2->execute($biblionumber);
1001 while (my $row=$sth2->fetchrow_hashref) {
1002 &MARCkoha2marcOnefield($sth,$record,"bibliosubject.subject",$row->{'subject'});
1004 my $sth2=$dbh->prepare(" SELECT subtitle FROM bibliosubtitle WHERE biblionumber=?");
1005 $sth2->execute($biblionumber);
1006 while (my $row=$sth2->fetchrow_hashref) {
1007 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.title",$row->{'subtitle'});
1012 sub MARCkoha2marcItem {
1013 # this function builds partial MARC::Record from the old koha-DB fields
1014 my ($dbh,$biblionumber,$itemnumber) = @_;
1015 # my $dbh=&C4Connect;
1016 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1017 my $record = MARC::Record->new();
1018 #--- if item, then retrieve old-style koha data
1019 if ($itemnumber>0) {
1020 # print STDERR "prepare $biblionumber,$itemnumber\n";
1021 my $sth2=$dbh->prepare("SELECT itemnumber,biblionumber,multivolumepart,biblioitemnumber,barcode,dateaccessioned,
1022 booksellerid,homebranch,price,replacementprice,replacementpricedate,datelastborrowed,
1023 datelastseen,multivolume,stack,notforloan,itemlost,wthdrawn,bulk,issues,renewals,
1024 reserves,restricted,binding,itemnotes,holdingbranch,timestamp
1026 WHERE itemnumber=?");
1027 $sth2->execute($itemnumber);
1028 my $row=$sth2->fetchrow_hashref;
1030 foreach $code (keys %$row) {
1031 if ($row->{$code}) {
1032 &MARCkoha2marcOnefield($sth,$record,"items.".$code,$row->{$code});
1039 sub MARCkoha2marcSubtitle {
1040 # this function builds partial MARC::Record from the old koha-DB fields
1041 my ($dbh,$bibnum,$subtitle) = @_;
1042 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1043 my $record = MARC::Record->new();
1044 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.subtitle",$subtitle);
1048 sub MARCkoha2marcOnefield {
1049 my ($sth,$record,$kohafieldname,$value)=@_;
1052 $sth->execute($kohafieldname);
1053 if (($tagfield,$tagsubfield)=$sth->fetchrow) {
1054 if ($record->field($tagfield)) {
1055 my $tag =$record->field($tagfield);
1057 $tag->add_subfields($tagsubfield,$value);
1058 $record->delete_field($tag);
1059 $record->add_fields($tag);
1062 $record->add_fields($tagfield," "," ",$tagsubfield => $value);
1069 my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
1071 my $record = MARC::Record->new();
1072 # my %subfieldlist=();
1073 my $prevvalue; # if tag <10
1074 my $field; # if tag >=10
1075 for (my $i=0; $i< @$rtags; $i++) {
1076 # rebuild MARC::Record
1077 if (@$rtags[$i] ne $prevtag) {
1078 if ($prevtag < 10) {
1080 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
1084 $record->add_fields($field);
1087 $indicators{@$rtags[$i]}.=' ';
1088 if (@$rtags[$i] <10) {
1089 $prevvalue= @$rvalues[$i];
1091 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
1093 $prevtag = @$rtags[$i];
1095 if (@$rtags[$i] <10) {
1096 $prevvalue=@$rvalues[$i];
1098 if (@$rvalues[$i]) {
1099 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
1102 $prevtag= @$rtags[$i];
1105 # the last has not been included inside the loop... do it now !
1106 $record->add_fields($field);
1107 # warn $record->as_formatted;
1112 my ($dbh,$record) = @_;
1113 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1115 my $sth2=$dbh->prepare("SHOW COLUMNS from biblio");
1118 # print STDERR $record->as_formatted;
1119 while (($field)=$sth2->fetchrow) {
1120 $result=&MARCmarc2kohaOneField($sth,"biblio",$field,$record,$result);
1122 $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
1124 while (($field)=$sth2->fetchrow) {
1125 if ($field eq 'notes') { $field = 'bnotes'; }
1126 $result=&MARCmarc2kohaOneField($sth,"biblioitems",$field,$record,$result);
1128 $sth2=$dbh->prepare("SHOW COLUMNS from items");
1130 while (($field)=$sth2->fetchrow) {
1131 $result = &MARCmarc2kohaOneField($sth,"items",$field,$record,$result);
1133 # additional authors : specific
1134 $result = &MARCmarc2kohaOneField($sth,"additionalauthors","additionalauthors",$record,$result);
1135 # modify copyrightdate to keep only the 1st year found
1136 my $temp = $result->{'copyrightdate'};
1137 $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
1139 $result->{'copyrightdate'} = $1;
1140 } else { # if no cYYYY, get the 1st date.
1141 $temp =~ m/(\d\d\d\d)/;
1142 $result->{'copyrightdate'} = $1;
1144 # modify publicationyear to keep only the 1st year found
1145 my $temp = $result->{'publicationyear'};
1146 $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
1148 $result->{'publicationyear'} = $1;
1149 } else { # if no cYYYY, get the 1st date.
1150 $temp =~ m/(\d\d\d\d)/;
1151 $result->{'publicationyear'} = $1;
1156 sub MARCmarc2kohaOneField {
1157 # FIXME ? if a field has a repeatable subfield that is used in old-db, only the 1st will be retrieved...
1158 my ($sth,$kohatable,$kohafield,$record,$result)= @_;
1159 # warn "kohatable / $kohafield / $result / ";
1163 $sth->execute($kohatable.".".$kohafield);
1164 ($tagfield,$subfield) = $sth->fetchrow;
1165 foreach my $field ($record->field($tagfield)) {
1166 if ($field->subfield($subfield)) {
1167 if ($result->{$kohafield}) {
1168 $result->{$kohafield} .= " | ".$field->subfield($subfield);
1170 $result->{$kohafield}=$field->subfield($subfield);
1178 # split a subfield string and adds it into the word table.
1180 my ($dbh,$bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$sentence) =@_;
1181 $sentence =~ s/(\.|\?|\:|\!|\'|,|\-|\"|\(|\)|\[|\]|\{|\})/ /g;
1182 my @words = split / /,$sentence;
1183 my $stopwords= C4::Context->stopwords;
1184 my $sth=$dbh->prepare("insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word)
1185 values (?,?,?,?,?,?,soundex(?))");
1186 foreach my $word (@words) {
1187 # we record only words longer than 2 car and not in stopwords hash
1188 if (length($word)>1 and !($stopwords->{uc($word)})) {
1189 $sth->execute($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,$word);
1191 warn "ERROR ==> insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word) values ($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,soundex($word))\n";
1198 # delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
1199 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
1200 my $sth=$dbh->prepare("delete from marc_word where bibid=? and tag=? and tagorder=? and subfieldid=? and subfieldorder=?");
1201 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
1206 # NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
1209 # all the following subs are useful to manage MARC-DB with complete MARC records.
1210 # it's used with marcimport, and marc management tools
1214 =item ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbibilio($dbh,$MARCRecord,$oldbiblio,$oldbiblioitem);
1216 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
1217 are builded from the MARC::Record. If they are passed, they are used.
1219 =item NEWnewitem($dbh, $record,$bibid);
1221 adds an item in the db.
1226 my ($dbh, $record, $oldbiblio, $oldbiblioitem) = @_;
1227 # note $oldbiblio and $oldbiblioitem are not mandatory.
1228 # if not present, they will be builded from $record with MARCmarc2koha function
1229 if (($oldbiblio) and not($oldbiblioitem)) {
1230 print STDERR "NEWnewbiblio : missing parameter\n";
1231 print "NEWnewbiblio : missing parameter : contact koha development team\n";
1237 $oldbibnum = OLDnewbiblio($dbh,$oldbiblio);
1238 $oldbiblioitem->{'biblionumber'} = $oldbibnum;
1239 $oldbibitemnum = OLDnewbiblioitem($dbh,$oldbiblioitem);
1241 my $olddata = MARCmarc2koha($dbh,$record);
1242 $oldbibnum = OLDnewbiblio($dbh,$olddata);
1243 $olddata->{'biblionumber'} = $oldbibnum;
1244 $oldbibitemnum = OLDnewbiblioitem($dbh,$olddata);
1246 # search subtiles, addiauthors and subjects
1247 my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author");
1248 my @addiauthfields = $record->field($tagfield);
1249 foreach my $addiauthfield (@addiauthfields) {
1250 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1251 foreach my $subfieldcount (0..$#addiauthsubfields) {
1252 OLDmodaddauthor($dbh,$oldbibnum,$addiauthsubfields[$subfieldcount]);
1255 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle");
1256 my @subtitlefields = $record->field($tagfield);
1257 foreach my $subtitlefield (@subtitlefields) {
1258 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1259 foreach my $subfieldcount (0..$#subtitlesubfields) {
1260 OLDnewsubtitle($dbh,$oldbibnum,$subtitlesubfields[$subfieldcount]);
1263 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject");
1264 my @subj = $record->field($tagfield);
1265 foreach my $subject (@subj) {
1266 my @subjsubfield = $subject->subfield($tagsubfield);
1268 foreach my $subfieldcount (0..$#subjsubfield) {
1269 push @subjects,$subjsubfield[$subfieldcount];
1271 OLDmodsubject($dbh,$oldbibnum,1,@subjects);
1273 # we must add bibnum and bibitemnum in MARC::Record...
1274 # we build the new field with biblionumber and biblioitemnumber
1275 # we drop the original field
1276 # we add the new builded field.
1277 # NOTE : Works only if the field is ONLY for biblionumber and biblioitemnumber
1278 # (steve and paul : thinks 090 is a good choice)
1279 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1280 $sth->execute("biblio.biblionumber");
1281 (my $tagfield1, my $tagsubfield1) = $sth->fetchrow;
1282 $sth->execute("biblioitems.biblioitemnumber");
1283 (my $tagfield2, my $tagsubfield2) = $sth->fetchrow;
1284 if ($tagfield1 != $tagfield2) {
1285 warn "Error in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1286 print "Content-Type: text/html\n\nError in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1289 my $newfield = MARC::Field->new( $tagfield1,'','',
1290 "$tagsubfield1" => $oldbibnum,
1291 "$tagsubfield2" => $oldbibitemnum);
1292 # drop old field and create new one...
1293 my $old_field = $record->field($tagfield1);
1294 $record->delete_field($old_field);
1295 $record->add_fields($newfield);
1296 my $bibid = MARCaddbiblio($dbh,$record,$oldbibnum);
1297 return ($bibid,$oldbibnum,$oldbibitemnum );
1301 my ($dbh,$record,$bibid) =@_;
1302 &MARCmodbiblio($dbh,$bibid,$record,0);
1303 my $oldbiblio = MARCmarc2koha($dbh,$record);
1304 my $oldbiblionumber = OLDmodbiblio($dbh,$oldbiblio);
1305 OLDmodbibitem($dbh,$oldbiblio);
1306 # now, modify addi authors, subject, addititles.
1307 my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author");
1308 my @addiauthfields = $record->field($tagfield);
1309 foreach my $addiauthfield (@addiauthfields) {
1310 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1311 foreach my $subfieldcount (0..$#addiauthsubfields) {
1312 OLDmodaddauthor($dbh,$oldbiblionumber,$addiauthsubfields[$subfieldcount]);
1315 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle");
1316 my @subtitlefields = $record->field($tagfield);
1317 foreach my $subtitlefield (@subtitlefields) {
1318 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1319 foreach my $subfieldcount (0..$#subtitlesubfields) {
1320 OLDmodsubtitle($dbh,$oldbiblionumber,$subtitlesubfields[$subfieldcount]);
1323 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject");
1324 my @subj = $record->field($tagfield);
1325 foreach my $subject (@subj) {
1326 my @subjsubfield = $subject->subfield($tagsubfield);
1328 foreach my $subfieldcount (0..$#subjsubfield) {
1329 push @subjects,$subjsubfield[$subfieldcount];
1331 OLDmodsubject($dbh,$oldbiblionumber,1,@subjects);
1338 my ($dbh, $record,$bibid) = @_;
1339 # add item in old-DB
1340 my $item = &MARCmarc2koha($dbh,$record);
1341 # needs old biblionumber and biblioitemnumber
1342 $item->{'biblionumber'} = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
1343 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
1344 $sth->execute($item->{'biblionumber'});
1345 ($item->{'biblioitemnumber'}) = $sth->fetchrow;
1346 my ($itemnumber,$error) = &OLDnewitems($dbh,$item,$item->{barcode});
1347 # add itemnumber to MARC::Record before adding the item.
1348 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1349 &MARCkoha2marcOnefield($sth,$record,"items.itemnumber",$itemnumber);
1351 my $bib = &MARCadditem($dbh,$record,$item->{'biblionumber'});
1355 my ($dbh,$record,$bibid,$itemnumber,$delete) = @_;
1356 &MARCmoditem($dbh,$record,$bibid,$itemnumber,$delete);
1357 my $olditem = MARCmarc2koha($dbh,$record);
1358 OLDmoditem($dbh,$olditem);
1363 # OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
1367 =item $biblionumber = OLDnewbiblio($dbh,$biblio);
1369 adds a record in biblio table. Datas are in the hash $biblio.
1371 =item $biblionumber = OLDmodbiblio($dbh,$biblio);
1373 modify a record in biblio table. Datas are in the hash $biblio.
1375 =item OLDmodsubtitle($dbh,$bibnum,$subtitle);
1377 modify subtitles in bibliosubtitle table.
1379 =item OLDmodaddauthor($dbh,$bibnum,$author);
1381 adds or modify additional authors
1382 NOTE : Strange sub : seems to delete MANY and add only ONE author... maybe buggy ?
1384 =item $errors = OLDmodsubject($dbh,$bibnum, $force, @subject);
1386 modify/adds subjects
1388 =item OLDmodbibitem($dbh, $biblioitem);
1392 =item OLDmodnote($dbh,$bibitemnum,$note
1394 modify a note for a biblioitem
1396 =item OLDnewbiblioitem($dbh,$biblioitem);
1398 adds a biblioitem ($biblioitem is a hash with the values)
1400 =item OLDnewsubject($dbh,$bibnum);
1404 =item OLDnewsubtitle($dbh,$bibnum,$subtitle);
1406 create a new subtitle
1408 =item ($itemnumber,$errors)= OLDnewitems($dbh,$item,$barcode);
1410 create a item. $item is a hash and $barcode the barcode.
1412 =item OLDmoditem($dbh,$item);
1416 =item OLDdelitem($dbh,$itemnum);
1420 =item OLDdeletebiblioitem($dbh,$biblioitemnumber);
1422 deletes a biblioitem
1423 NOTE : not standard sub name. Should be OLDdelbiblioitem()
1425 =item OLDdelbiblio($dbh,$biblio);
1432 my ($dbh,$biblio) = @_;
1433 # my $dbh = &C4Connect;
1434 my $query = "Select max(biblionumber) from biblio";
1435 my $sth = $dbh->prepare($query);
1437 my $data = $sth->fetchrow_arrayref;
1438 my $bibnum = $$data[0] + 1;
1441 if ($biblio->{'seriestitle'}) { $series = 1 };
1443 $query = "insert into biblio set biblionumber = ?, title = ?, author = ?, copyrightdate = ?,
1444 serial = ?, seriestitle = ?, notes = ?, abstract = ?";
1445 $sth = $dbh->prepare($query);
1446 $sth->execute($bibnum,$biblio->{'title'},$biblio->{'author'},$biblio->{'copyright'},$series,$biblio->{'seriestitle'},$biblio->{'notes'},$biblio->{'abstract'});
1454 my ($dbh,$biblio) = @_;
1455 # my $dbh = C4Connect;
1459 $query = "Update biblio set title = ?, author = ?, abstract = ?, copyrightdate = ?,
1460 seriestitle = ?, serial = ?, unititle = ?, notes = ? where biblionumber = ?";
1461 $sth = $dbh->prepare($query);
1462 $sth->execute($biblio->{'title'},$biblio->{'author'},$biblio->{'abstract'},$biblio->{'copyrightdate'},
1463 $biblio->{'seriestitle'},$biblio->{'serial'},$biblio->{'unititle'},$biblio->{'notes'},$biblio->{'biblionumber'});
1466 return($biblio->{'biblionumber'});
1469 sub OLDmodsubtitle {
1470 my ($dbh,$bibnum, $subtitle) = @_;
1471 my $query = "update bibliosubtitle set subtitle = ? where biblionumber = ?";
1472 my $sth = $dbh->prepare($query);
1473 $sth->execute($subtitle,$bibnum);
1478 sub OLDmodaddauthor {
1479 my ($dbh,$bibnum, $author) = @_;
1480 # my $dbh = C4Connect;
1481 my $query = "Delete from additionalauthors where biblionumber = $bibnum";
1482 my $sth = $dbh->prepare($query);
1487 if ($author ne '') {
1488 $query = "Insert into additionalauthors set
1491 $sth = $dbh->prepare($query);
1493 $sth->execute($author,$bibnum);
1497 } # sub modaddauthor
1501 my ($dbh,$bibnum, $force, @subject) = @_;
1502 # my $dbh = C4Connect;
1503 my $count = @subject;
1505 for (my $i = 0; $i < $count; $i++) {
1506 $subject[$i] =~ s/^ //g;
1507 $subject[$i] =~ s/ $//g;
1508 my $query = "select * from catalogueentry where entrytype = 's' and catalogueentry = '$subject[$i]'";
1509 my $sth = $dbh->prepare($query);
1512 if (my $data = $sth->fetchrow_hashref) {
1514 if ($force eq $subject[$i] || $force eq 1) {
1515 # subject not in aut, chosen to force anway
1516 # so insert into cataloguentry so its in auth file
1517 $query = "Insert into catalogueentry (entrytype,catalogueentry) values ('s','$subject[$i]')";
1518 my $sth2 = $dbh->prepare($query);
1523 $error = "$subject[$i]\n does not exist in the subject authority file";
1524 $query = "Select * from catalogueentry where entrytype = 's' and (catalogueentry like '$subject[$i] %'
1525 or catalogueentry like '% $subject[$i] %' or catalogueentry like '% $subject[$i]')";
1526 my $sth2 = $dbh->prepare($query);
1528 while (my $data = $sth2->fetchrow_hashref) {
1529 $error .= "<br>$data->{'catalogueentry'}";
1537 my $query = "Delete from bibliosubject where biblionumber = $bibnum";
1538 my $sth = $dbh->prepare($query);
1541 for (my $i = 0; $i < $count; $i++) {
1542 $sth = $dbh->prepare("Insert into bibliosubject values ('$subject[$i]', $bibnum)");
1553 my ($dbh,$biblioitem) = @_;
1554 # my $dbh = C4Connect;
1557 $biblioitem->{'itemtype'} = $dbh->quote($biblioitem->{'itemtype'});
1558 $biblioitem->{'url'} = $dbh->quote($biblioitem->{'url'});
1559 $biblioitem->{'isbn'} = $dbh->quote($biblioitem->{'isbn'});
1560 $biblioitem->{'publishercode'} = $dbh->quote($biblioitem->{'publishercode'});
1561 $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
1562 $biblioitem->{'classification'} = $dbh->quote($biblioitem->{'classification'});
1563 $biblioitem->{'dewey'} = $dbh->quote($biblioitem->{'dewey'});
1564 $biblioitem->{'subclass'} = $dbh->quote($biblioitem->{'subclass'});
1565 $biblioitem->{'illus'} = $dbh->quote($biblioitem->{'illus'});
1566 $biblioitem->{'pages'} = $dbh->quote($biblioitem->{'pages'});
1567 $biblioitem->{'volumeddesc'} = $dbh->quote($biblioitem->{'volumeddesc'});
1568 $biblioitem->{'bnotes'} = $dbh->quote($biblioitem->{'bnotes'});
1569 $biblioitem->{'size'} = $dbh->quote($biblioitem->{'size'});
1570 $biblioitem->{'place'} = $dbh->quote($biblioitem->{'place'});
1572 $query = "Update biblioitems set
1573 itemtype = $biblioitem->{'itemtype'},
1574 url = $biblioitem->{'url'},
1575 isbn = $biblioitem->{'isbn'},
1576 publishercode = $biblioitem->{'publishercode'},
1577 publicationyear = $biblioitem->{'publicationyear'},
1578 classification = $biblioitem->{'classification'},
1579 dewey = $biblioitem->{'dewey'},
1580 subclass = $biblioitem->{'subclass'},
1581 illus = $biblioitem->{'illus'},
1582 pages = $biblioitem->{'pages'},
1583 volumeddesc = $biblioitem->{'volumeddesc'},
1584 notes = $biblioitem->{'bnotes'},
1585 size = $biblioitem->{'size'},
1586 place = $biblioitem->{'place'}
1587 where biblioitemnumber = $biblioitem->{'biblioitemnumber'}";
1595 my ($dbh,$bibitemnum,$note)=@_;
1596 # my $dbh=C4Connect;
1597 my $query="update biblioitems set notes='$note' where
1598 biblioitemnumber='$bibitemnum'";
1599 my $sth=$dbh->prepare($query);
1605 sub OLDnewbiblioitem {
1606 my ($dbh,$biblioitem) = @_;
1607 # my $dbh = C4Connect;
1608 my $query = "Select max(biblioitemnumber) from biblioitems";
1609 my $sth = $dbh->prepare($query);
1614 $data = $sth->fetchrow_arrayref;
1615 $bibitemnum = $$data[0] + 1;
1619 $sth = $dbh->prepare("insert into biblioitems set
1620 biblioitemnumber = ?, biblionumber = ?,
1621 volume = ?, number = ?,
1622 classification = ?, itemtype = ?,
1624 issn = ?, dewey = ?,
1625 subclass = ?, publicationyear = ?,
1626 publishercode = ?, volumedate = ?,
1627 volumeddesc = ?, illus = ?,
1628 pages = ?, notes = ?,
1630 marc = ?, place = ?");
1631 $sth->execute($bibitemnum, $biblioitem->{'biblionumber'},
1632 $biblioitem->{'volume'}, $biblioitem->{'number'},
1633 $biblioitem->{'classification'}, $biblioitem->{'itemtype'},
1634 $biblioitem->{'url'}, $biblioitem->{'isbn'},
1635 $biblioitem->{'issn'}, $biblioitem->{'dewey'},
1636 $biblioitem->{'subclass'}, $biblioitem->{'publicationyear'},
1637 $biblioitem->{'publishercode'}, $biblioitem->{'volumedate'},
1638 $biblioitem->{'volumeddesc'}, $biblioitem->{'illus'},
1639 $biblioitem->{'pages'}, $biblioitem->{'bnotes'},
1640 $biblioitem->{'size'}, $biblioitem->{'lccn'},
1641 $biblioitem->{'marc'}, $biblioitem->{'place'});
1644 return($bibitemnum);
1648 my ($dbh,$bibnum)=@_;
1649 my $query="insert into bibliosubject (biblionumber) values ($bibnum)";
1650 my $sth=$dbh->prepare($query);
1655 sub OLDnewsubtitle {
1656 my ($dbh,$bibnum, $subtitle) = @_;
1657 my $query = "insert into bibliosubtitle set biblionumber = ?, subtitle = ?";
1658 my $sth = $dbh->prepare($query);
1659 $sth->execute($bibnum,$subtitle);
1665 my ($dbh,$item, $barcode) = @_;
1666 # my $dbh = C4Connect;
1667 my $query = "Select max(itemnumber) from items";
1668 my $sth = $dbh->prepare($query);
1674 $data = $sth->fetchrow_hashref;
1675 $itemnumber = $data->{'max(itemnumber)'} + 1;
1677 # if dateaccessioned is provided, use it. Otherwise, set to NOW()
1678 if ($item->{'dateaccessioned'}) {
1679 $sth=$dbh->prepare("Insert into items set
1680 itemnumber = ?, biblionumber = ?,
1681 biblioitemnumber = ?, barcode = ?,
1682 booksellerid = ?, dateaccessioned = ?,
1683 homebranch = ?, holdingbranch = ?,
1684 price = ?, replacementprice = ?,
1685 replacementpricedate = NOW(), itemnotes = ?,
1688 $sth->execute($itemnumber, $item->{'biblionumber'},
1689 $item->{'biblioitemnumber'},$barcode,
1690 $item->{'booksellerid'},$item->{'dateaccessioned'},
1691 $item->{'homebranch'},$item->{'holdingbranch'},
1692 $item->{'price'},$item->{'replacementprice'},
1693 $item->{'itemnotes'},$item->{'loan'});
1695 $sth=$dbh->prepare("Insert into items set
1696 itemnumber = ?, biblionumber = ?,
1697 biblioitemnumber = ?, barcode = ?,
1698 booksellerid = ?, dateaccessioned = NOW(),
1699 homebranch = ?, holdingbranch = ?,
1700 price = ?, replacementprice = ?,
1701 replacementpricedate = NOW(), itemnotes = ?,
1704 $sth->execute($itemnumber, $item->{'biblionumber'},
1705 $item->{'biblioitemnumber'},$barcode,
1706 $item->{'booksellerid'},
1707 $item->{'homebranch'},$item->{'holdingbranch'},
1708 $item->{'price'},$item->{'replacementprice'},
1709 $item->{'itemnotes'},$item->{'loan'});
1711 if (defined $sth->errstr) {
1712 $error .= $sth->errstr;
1715 return($itemnumber,$error);
1719 my ($dbh,$item) = @_;
1720 # my ($dbh,$loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
1721 # my $dbh=C4Connect;
1722 $item->{'itemnum'}=$item->{'itemnumber'} unless $item->{'itemnum'};
1723 my $query="update items set barcode='$item->{'barcode'}',itemnotes='$item->{'notes'}'
1724 where itemnumber=$item->{'itemnum'}";
1725 if ($item->{'barcode'} eq ''){
1726 $item->{'notforloan'}=0 unless $item->{'notforloan'};
1727 $query="update items set notforloan=$item->{'notforloan'} where itemnumber=$item->{'itemnum'}";
1729 if ($item->{'lost'} ne ''){
1730 $query="update items set biblioitemnumber=$item->{'bibitemnum'},
1731 barcode='$item->{'barcode'}',
1732 itemnotes='$item->{'notes'}',
1733 homebranch='$item->{'homebranch'}',
1734 itemlost='$item->{'lost'}',
1735 wthdrawn='$item->{'wthdrawn'}'
1736 where itemnumber=$item->{'itemnum'}";
1738 if ($item->{'replacement'} ne ''){
1739 $query=~ s/ where/,replacementprice='$item->{'replacement'}' where/;
1741 my $sth=$dbh->prepare($query);
1748 my ($dbh,$itemnum)=@_;
1749 # my $dbh=C4Connect;
1750 my $query="select * from items where itemnumber=$itemnum";
1751 my $sth=$dbh->prepare($query);
1753 my @data=$sth->fetchrow_array;
1755 $query="Insert into deleteditems values (";
1756 foreach my $temp (@data){
1757 $query .= "'$temp',";
1761 $sth=$dbh->prepare($query);
1764 $query = "Delete from items where itemnumber=$itemnum";
1765 $sth=$dbh->prepare($query);
1771 sub OLDdeletebiblioitem {
1772 my ($dbh,$biblioitemnumber) = @_;
1773 # my $dbh = C4Connect;
1774 my $query = "Select * from biblioitems
1775 where biblioitemnumber = $biblioitemnumber";
1776 my $sth = $dbh->prepare($query);
1781 if (@results = $sth->fetchrow_array) {
1782 $query = "Insert into deletedbiblioitems values (";
1783 foreach my $value (@results) {
1784 $value = $dbh->quote($value);
1785 $query .= "$value,";
1788 $query =~ s/\,$/\)/;
1791 $query = "Delete from biblioitems
1792 where biblioitemnumber = $biblioitemnumber";
1796 # Now delete all the items attached to the biblioitem
1797 $query = "Select * from items where biblioitemnumber = $biblioitemnumber";
1798 $sth = $dbh->prepare($query);
1800 while (@results = $sth->fetchrow_array) {
1801 $query = "Insert into deleteditems values (";
1802 foreach my $value (@results) {
1803 $value = $dbh->quote($value);
1804 $query .= "$value,";
1806 $query =~ s/\,$/\)/;
1810 $query = "Delete from items where biblioitemnumber = $biblioitemnumber";
1813 } # sub deletebiblioitem
1816 my ($dbh,$biblio)=@_;
1817 # my $dbh=C4Connect;
1818 my $query="select * from biblio where biblionumber=$biblio";
1819 my $sth=$dbh->prepare($query);
1821 if (my @data=$sth->fetchrow_array){
1823 $query="Insert into deletedbiblio values (";
1824 foreach my $temp (@data){
1825 $temp=~ s/\'/\\\'/g;
1826 $query .= "'$temp',";
1830 $sth=$dbh->prepare($query);
1833 $query = "Delete from biblio where biblionumber=$biblio";
1834 $sth=$dbh->prepare($query);
1850 my $dbh = C4::Context->dbh;
1851 my $query="Select count(*) from items where biblionumber=$biblio";
1853 my $sth=$dbh->prepare($query);
1855 my $data=$sth->fetchrow_hashref;
1857 return($data->{'count(*)'});
1862 ($order, $ordernumber) = &getorder($biblioitemnumber, $biblionumber);
1864 Looks up the order with the given biblionumber and biblioitemnumber.
1866 Returns a two-element array. C<$ordernumber> is the order number.
1867 C<$order> is a reference-to-hash describing the order; its keys are
1868 fields from the biblio, biblioitems, aqorders, and aqorderbreakdown
1869 tables of the Koha database.
1873 # FIXME - This is effectively identical to &C4::Catalogue::getorder.
1874 # Pick one and stick with it.
1877 my $dbh = C4::Context->dbh;
1878 my $query="Select ordernumber
1880 where biblionumber=? and biblioitemnumber=?";
1881 my $sth=$dbh->prepare($query);
1882 $sth->execute($bib,$bi);
1883 # FIXME - Use fetchrow_array(), since we're only interested in the one
1885 my $ordnum=$sth->fetchrow_hashref;
1887 my $order=getsingleorder($ordnum->{'ordernumber'});
1889 return ($order,$ordnum->{'ordernumber'});
1892 =item getsingleorder
1894 $order = &getsingleorder($ordernumber);
1896 Looks up an order by order number.
1898 Returns a reference-to-hash describing the order. The keys of
1899 C<$order> are fields from the biblio, biblioitems, aqorders, and
1900 aqorderbreakdown tables of the Koha database.
1904 # FIXME - This is effectively identical to
1905 # &C4::Catalogue::getsingleorder.
1906 # Pick one and stick with it.
1907 sub getsingleorder {
1909 my $dbh = C4::Context->dbh;
1910 my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown
1911 where aqorders.ordernumber=?
1912 and biblio.biblionumber=aqorders.biblionumber and
1913 biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
1914 aqorders.ordernumber=aqorderbreakdown.ordernumber";
1915 my $sth=$dbh->prepare($query);
1916 $sth->execute($ordnum);
1917 my $data=$sth->fetchrow_hashref;
1924 my $dbh = C4::Context->dbh;
1925 my $bibnum=OLDnewbiblio($dbh,$biblio);
1926 # finds new (MARC bibid
1927 # my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$bibnum);
1928 my $record = &MARCkoha2marcBiblio($dbh,$bibnum);
1929 MARCaddbiblio($dbh,$record,$bibnum);
1935 $biblionumber = &modbiblio($biblio);
1937 Update a biblio record.
1939 C<$biblio> is a reference-to-hash whose keys are the fields in the
1940 biblio table in the Koha database. All fields must be present, not
1941 just the ones you wish to change.
1943 C<&modbiblio> updates the record defined by
1944 C<$biblio-E<gt>{biblionumber}> with the values in C<$biblio>.
1946 C<&modbiblio> returns C<$biblio-E<gt>{biblionumber}> whether it was
1953 my $dbh = C4::Context->dbh;
1954 my $biblionumber=OLDmodbiblio($dbh,$biblio);
1955 my $record = MARCkoha2marcBiblio($dbh,$biblionumber,$biblionumber);
1956 # finds new (MARC bibid
1957 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
1958 MARCmodbiblio($dbh,$bibid,$record,0);
1959 return($biblionumber);
1964 &modsubtitle($biblionumber, $subtitle);
1966 Sets the subtitle of a book.
1968 C<$biblionumber> is the biblionumber of the book to modify.
1970 C<$subtitle> is the new subtitle.
1975 my ($bibnum, $subtitle) = @_;
1976 my $dbh = C4::Context->dbh;
1977 &OLDmodsubtitle($dbh,$bibnum,$subtitle);
1982 &modaddauthor($biblionumber, $author);
1984 Replaces all additional authors for the book with biblio number
1985 C<$biblionumber> with C<$author>. If C<$author> is the empty string,
1986 C<&modaddauthor> deletes all additional authors.
1991 my ($bibnum, $author) = @_;
1992 my $dbh = C4::Context->dbh;
1993 &OLDmodaddauthor($dbh,$bibnum,$author);
1994 } # sub modaddauthor
1998 $error = &modsubject($biblionumber, $force, @subjects);
2000 $force - a subject to force
2002 $error - Error message, or undef if successful.
2007 my ($bibnum, $force, @subject) = @_;
2008 my $dbh = C4::Context->dbh;
2009 my $error= &OLDmodsubject($dbh,$bibnum,$force, @subject);
2014 my ($biblioitem) = @_;
2015 my $dbh = C4::Context->dbh;
2016 &OLDmodbibitem($dbh,$biblioitem);
2020 my ($bibitemnum,$note)=@_;
2021 my $dbh = C4::Context->dbh;
2022 &OLDmodnote($dbh,$bibitemnum,$note);
2026 my ($biblioitem) = @_;
2027 my $dbh = C4::Context->dbh;
2028 my $bibitemnum = &OLDnewbiblioitem($dbh,$biblioitem);
2029 my $MARCbiblio= MARCkoha2marcBiblio($dbh,0,$bibitemnum); # the 0 means "do NOT retrieve biblio, only biblioitem, in the MARC record
2030 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblioitem->{biblionumber});
2031 &MARCaddbiblio($dbh,$MARCbiblio,$biblioitem->{biblionumber},$bibid);
2032 return($bibitemnum);
2037 my $dbh = C4::Context->dbh;
2038 &OLDnewsubject($dbh,$bibnum);
2042 my ($bibnum, $subtitle) = @_;
2043 my $dbh = C4::Context->dbh;
2044 &OLDnewsubtitle($dbh,$bibnum,$subtitle);
2048 my ($item, @barcodes) = @_;
2049 my $dbh = C4::Context->dbh;
2053 foreach my $barcode (@barcodes) {
2054 ($itemnumber,$error)=&OLDnewitems($dbh,$item,uc($barcode));
2056 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{biblionumber},$itemnumber);
2057 &MARCadditem($dbh,$MARCitem,$item->{biblionumber});
2064 my $dbh = C4::Context->dbh;
2065 &OLDmoditem($dbh,$item);
2066 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{'biblionumber'},$item->{'itemnum'});
2067 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$item->{biblionumber});
2068 &MARCmoditem($dbh,$MARCitem,$bibid,$item->{itemnum},0);
2072 my ($count,@barcodes)=@_;
2073 my $dbh = C4::Context->dbh;
2075 for (my $i=0;$i<$count;$i++){
2076 $barcodes[$i]=uc $barcodes[$i];
2077 my $query="Select * from items where barcode='$barcodes[$i]'";
2078 my $sth=$dbh->prepare($query);
2080 if (my $data=$sth->fetchrow_hashref){
2081 $error.=" Duplicate Barcode: $barcodes[$i]";
2089 my ($bibitemnum)=@_;
2090 my $dbh = C4::Context->dbh;
2091 my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
2092 my $sth=$dbh->prepare($query);
2094 my $data=$sth->fetchrow_hashref;
2096 return($data->{'count(*)'});
2101 my $dbh = C4::Context->dbh;
2102 &OLDdelitem($dbh,$itemnum);
2105 sub deletebiblioitem {
2106 my ($biblioitemnumber) = @_;
2107 my $dbh = C4::Context->dbh;
2108 &OLDdeletebiblioitem($dbh,$biblioitemnumber);
2109 } # sub deletebiblioitem
2114 my $dbh = C4::Context->dbh;
2115 &OLDdelbiblio($dbh,$biblio);
2119 my $dbh = C4::Context->dbh;
2120 my $query = "select * from itemtypes order by description";
2121 my $sth = $dbh->prepare($query);
2122 # || die "Cannot prepare $query" . $dbh->errstr;
2127 # || die "Cannot execute $query\n" . $sth->errstr;
2128 while (my $data = $sth->fetchrow_hashref) {
2129 $results[$count] = $data;
2134 return($count, @results);
2135 } # sub getitemtypes
2138 my ($biblionumber) = @_;
2139 my $dbh = C4::Context->dbh;
2140 my $query = "Select * from biblio where biblionumber = $biblionumber";
2141 my $sth = $dbh->prepare($query);
2142 # || die "Cannot prepare $query\n" . $dbh->errstr;
2147 # || die "Cannot execute $query\n" . $sth->errstr;
2148 while (my $data = $sth->fetchrow_hashref) {
2149 $results[$count] = $data;
2154 return($count, @results);
2158 my ($biblioitemnum) = @_;
2159 my $dbh = C4::Context->dbh;
2160 my $query = "Select * from biblioitems where
2161 biblioitemnumber = $biblioitemnum";
2162 my $sth = $dbh->prepare($query);
2168 while (my $data = $sth->fetchrow_hashref) {
2169 $results[$count] = $data;
2174 return($count, @results);
2175 } # sub getbiblioitem
2177 sub getbiblioitembybiblionumber {
2178 my ($biblionumber) = @_;
2179 my $dbh = C4::Context->dbh;
2180 my $query = "Select * from biblioitems where biblionumber =
2182 my $sth = $dbh->prepare($query);
2188 while (my $data = $sth->fetchrow_hashref) {
2189 $results[$count] = $data;
2194 return($count, @results);
2197 sub getitemsbybiblioitem {
2198 my ($biblioitemnum) = @_;
2199 my $dbh = C4::Context->dbh;
2200 my $query = "Select * from items, biblio where
2201 biblio.biblionumber = items.biblionumber and biblioitemnumber
2203 my $sth = $dbh->prepare($query);
2204 # || die "Cannot prepare $query\n" . $dbh->errstr;
2209 # || die "Cannot execute $query\n" . $sth->errstr;
2210 while (my $data = $sth->fetchrow_hashref) {
2211 $results[$count] = $data;
2216 return($count, @results);
2217 } # sub getitemsbybiblioitem
2221 # Subroutine to log changes to databases
2222 # Eventually, this subroutine will be used to create a log of all changes made,
2223 # with the possibility of "undo"ing some changes
2225 if ($database eq 'kohadb') {
2231 # print STDERR "KOHA: $type $section $item $original $new\n";
2232 } elsif ($database eq 'marc') {
2234 my $Record_ID=shift;
2237 my $subfield_ID=shift;
2240 # print STDERR "MARC: $type $Record_ID $tag $mark $subfield_ID $original $new\n";
2244 #------------------------------------------------
2247 #---------------------------------------
2248 # Find a biblio entry, or create a new one if it doesn't exist.
2249 # If a "subtitle" entry is in hash, add it to subtitle table
2250 sub getoraddbiblio {
2254 # FIXME - Unused argument
2255 $biblio, # hash ref to fields
2266 $dbh = C4::Context->dbh;
2268 print "<PRE>Looking for biblio </PRE>\n" if $debug;
2269 $sth=$dbh->prepare("select biblionumber
2271 where title=? and author=?
2272 and copyrightdate=? and seriestitle=?");
2274 $biblio->{title}, $biblio->{author},
2275 $biblio->{copyright}, $biblio->{seriestitle} );
2277 ($biblionumber) = $sth->fetchrow;
2278 print "<PRE>Biblio exists with number $biblionumber</PRE>\n" if $debug;
2280 # Doesn't exist. Add new one.
2281 print "<PRE>Adding biblio</PRE>\n" if $debug;
2282 ($biblionumber,$error)=&newbiblio($biblio);
2283 if ( $biblionumber ) {
2284 print "<PRE>Added with biblio number=$biblionumber</PRE>\n" if $debug;
2285 if ( $biblio->{subtitle} ) {
2286 &newsubtitle($biblionumber,$biblio->{subtitle} );
2289 print "<PRE>Couldn't add biblio: $error</PRE>\n" if $debug;
2293 return $biblionumber,$error;
2295 } # sub getoraddbiblio
2298 # converts ISO 5426 coded string to ISO 8859-1
2299 # sloppy code : should be improved in next issue
2300 my ($string,$encoding) = @_ ;
2302 # $encoding = C4::Context->preference("marcflavour") unless $encoding;
2303 if ($encoding eq "UNIMARC") {
2365 # this handles non-sorting blocks (if implementation requires this)
2366 $string = nsb_clean($_) ;
2367 } elsif ($encoding eq "USMARC" || $encoding eq "MARC21") {
2420 # this handles non-sorting blocks (if implementation requires this)
2421 $string = nsb_clean($_) ;
2428 my $NSB = '\x88' ; # NSB : begin Non Sorting Block
2429 my $NSE = '\x89' ; # NSE : Non Sorting Block end
2430 # handles non sorting blocks
2434 s/[ ]{0,1}$NSE/) /gm ;
2439 END { } # module clean-up code here (global destructor)
2445 Koha Developement team <info@koha.org>
2447 Paul POULAIN paul.poulain@free.fr