4 # Revision 1.56 2003/07/15 11:34:52 slef
5 # fixes from paul email
7 # Revision 1.55 2003/07/15 00:02:49 slef
8 # Work on bug 515... can we do a single-side rename of notes to bnotes?
10 # Revision 1.54 2003/07/11 11:51:32 tipaul
11 # *** empty log message ***
13 # Revision 1.52 2003/07/10 10:37:19 tipaul
14 # fix for copyrightdate problem, #514
16 # Revision 1.51 2003/07/02 14:47:17 tipaul
17 # fix for #519 : items.dateaccessioned imports incorrectly
19 # Revision 1.49 2003/06/17 11:21:13 tipaul
20 # improvments/fixes for z3950 support.
21 # * Works now even on ADD, not only on MODIFY
22 # * able to search on ISBN, author, title
24 # Revision 1.48 2003/06/16 09:22:53 rangi
25 # Just added an order clause to getitemtypes
27 # Revision 1.47 2003/05/20 16:22:44 tipaul
28 # fixing typo in Biblio.pm POD
30 # Revision 1.46 2003/05/19 13:45:18 tipaul
31 # support for subtitles, additional authors, subject.
32 # 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.
33 # Note that some OLD-DB subs are strange (dummy ?) see OLDmodsubject, OLDmodsubtitle, OLDmodaddiauthor in C4/Biblio.pm
34 # 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.
36 # Revision 1.45 2003/04/29 16:50:49 tipaul
37 # really proud of this commit :-)
38 # z3950 search and import seems to works fine.
39 # Let me explain how :
40 # * a "search z3950" button is added in the addbiblio template.
41 # * when clicked, a popup appears and z3950/search.pl is called
42 # * z3950/search.pl calls addz3950search in the DB
43 # * the z3950 daemon retrieve the records and stores them in z3950results AND in marc_breeding table.
44 # * as long as there as searches pending, the popup auto refresh every 2 seconds, and says how many searches are pending.
45 # * when the user clicks on a z3950 result => the parent popup is called with the requested biblio, and auto-filled
48 # * 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.
49 # * 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.
51 # Revision 1.44 2003/04/28 13:07:14 tipaul
52 # Those fixes solves the "internal server error" with MARC::Record 1.12.
53 # It was due to an illegal contruction in Koha : we tried to retrive subfields from <10 tags.
54 # That's not possible. MARC::Record accepted this in 0.93 version, but it was fixed after.
55 # Now, the construct/retrieving is OK !
57 # Revision 1.43 2003/04/10 13:56:02 tipaul
59 # * worked in 1.9.0, but not in 1.9.1 :
60 # - modif of a biblio didn't work
61 # - 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.
63 # * did not work before :
64 # - repeatable subfields now works correctly. Enter 2 subfields separated by | and they will be splitted during saving.
65 # - dropped the last subfield of the MARC form :-(
68 # - 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.
69 # Note the MARCdelbiblio has been rewritted to enable deletion of a biblio WITHOUT deleting items.
71 # Revision 1.42 2003/04/04 08:41:11 tipaul
72 # last commits before 1.9.1
74 # Revision 1.41 2003/04/01 12:26:43 tipaul
77 # Revision 1.40 2003/03/11 15:14:03 tipaul
80 # Revision 1.39 2003/03/07 16:35:42 tipaul
81 # * moving generic functions to Koha.pm
82 # * improvement of SearchMarc.pm
86 # Revision 1.38 2003/02/27 16:51:59 tipaul
87 # * moving prepare / execute to ? form.
90 # * road to 1.9.2 => acquisition and cataloguing merging
92 # Revision 1.37 2003/02/12 11:03:03 tipaul
93 # Support for 000 -> 010 fields.
94 # Those fields doesn't have subfields.
95 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
96 # 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.
98 # Revision 1.36 2003/02/12 11:01:01 tipaul
99 # Support for 000 -> 010 fields.
100 # Those fields doesn't have subfields.
101 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
102 # 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.
104 # Revision 1.35 2003/02/03 18:46:00 acli
105 # Minor factoring in C4/Biblio.pm, plus change to export the per-tag
106 # 'mandatory' property to a per-subfield 'tag_mandatory' template parameter,
107 # so that addbiblio.tmpl can distinguish between mandatory subfields in a
108 # mandatory tag and mandatory subfields in an optional tag
110 # Not-minor factoring in acqui.simple/addbiblio.pl to make the if-else blocks
111 # smaller, and to add some POD; need further testing for this
113 # Added function to check if a MARC subfield name is "koha-internal" (instead
114 # of checking it for 'lib' and 'tag' everywhere); temporarily added to Koha.pm
116 # Use above function in acqui.simple/additem.pl and search.marc/search.pl
118 # Revision 1.34 2003/01/28 14:50:04 tipaul
119 # fixing MARCmodbiblio API and reindenting code
121 # Revision 1.33 2003/01/23 12:22:37 tipaul
122 # adding char_decode to decode MARC21 or UNIMARC extended chars
124 # Revision 1.32 2002/12/16 15:08:50 tipaul
125 # small but important bugfix (fixes a problem in export)
127 # Revision 1.31 2002/12/13 16:22:04 tipaul
128 # 1st draft of marc export
130 # Revision 1.30 2002/12/12 21:26:35 tipaul
131 # YAB ! (Yet Another Bugfix) => related to biblio modif
132 # (some warning cleaning too)
134 # Revision 1.29 2002/12/12 16:35:00 tipaul
135 # adding authentification with Auth.pm and
136 # MAJOR BUGFIX on marc biblio modification
138 # Revision 1.28 2002/12/10 13:30:03 tipaul
139 # fugfixes from Dombes Abbey work
141 # Revision 1.27 2002/11/19 12:36:16 tipaul
143 # various bugfixes, improvments, and migration from acquisition.pm to biblio.pm
145 # Revision 1.26 2002/11/12 15:58:43 tipaul
148 # * 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)
150 # Revision 1.25 2002/10/25 10:58:26 tipaul
152 # * bugfixes and improvements
154 # Revision 1.24 2002/10/24 12:09:01 arensb
155 # Fixed "no title" warning when generating HTML documentation from POD.
157 # Revision 1.23 2002/10/16 12:43:08 arensb
158 # Added some FIXME comments.
160 # Revision 1.22 2002/10/15 13:39:17 tipaul
161 # removing Acquisition.pm
162 # deleting unused code in biblio.pm, rewriting POD and answering most FIXME comments
164 # Revision 1.21 2002/10/13 11:34:14 arensb
165 # Replaced expressions of the form "$x = $x <op> $y" with "$x <op>= $y".
166 # Thus, $x = $x+2 becomes $x += 2, and so forth.
168 # Revision 1.20 2002/10/13 08:28:32 arensb
169 # Deleted unused variables.
170 # Removed trailing whitespace.
172 # Revision 1.19 2002/10/13 05:56:10 arensb
173 # Added some FIXME comments.
175 # Revision 1.18 2002/10/11 12:34:53 arensb
176 # Replaced &requireDBI with C4::Context->dbh
178 # Revision 1.17 2002/10/10 14:48:25 tipaul
181 # Revision 1.16 2002/10/07 14:04:26 tipaul
182 # road to 1.3.1 : viewing MARC biblio
184 # Revision 1.15 2002/10/05 09:49:25 arensb
185 # Merged with arensb-context branch: use C4::Context->dbh instead of
186 # &C4Connect, and generally prefer C4::Context over C4::Database.
188 # Revision 1.14 2002/10/03 11:28:18 tipaul
189 # Extending Context.pm to add stopword management and using it in MARC-API.
190 # First benchmarks show a medium speed improvement, which is nice as this part is heavily called.
192 # Revision 1.13 2002/10/02 16:26:44 tipaul
195 # Revision 1.12.2.4 2002/10/05 07:09:31 arensb
196 # Merged in changes from main branch.
198 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
199 # Added a whole mess of FIXME comments.
201 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
202 # Added some missing semicolons.
204 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
205 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
208 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
209 # Added a whole mess of FIXME comments.
211 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
212 # Added some missing semicolons.
214 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
215 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
218 # Revision 1.12 2002/10/01 11:48:51 arensb
219 # Added some FIXME comments, mostly marking duplicate functions.
221 # Revision 1.11 2002/09/24 13:49:26 tipaul
222 # long WAS the road to 1.3.0...
223 # coming VERY SOON NOW...
224 # modifying installer and buildrelease to update the DB
226 # Revision 1.10 2002/09/22 16:50:08 arensb
227 # Added some FIXME comments.
229 # Revision 1.9 2002/09/20 12:57:46 tipaul
230 # long is the road to 1.4.0
231 # * MARCadditem and MARCmoditem now wroks
232 # * various bugfixes in MARC management
233 # !!! 1.3.0 should be released very soon now. Be careful !!!
235 # Revision 1.8 2002/09/10 13:53:52 tipaul
236 # MARC API continued...
238 # * 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)
240 # Note : it should not be hard for marcimport and marcexport to re-link fields from internal tag/subfield to "legal" tag/subfield.
242 # Revision 1.7 2002/08/14 18:12:51 tonnesen
243 # Added copyright statement to all .pl and .pm files
245 # Revision 1.6 2002/07/25 13:40:31 tipaul
246 # pod documenting the API.
248 # Revision 1.5 2002/07/24 16:11:37 tipaul
250 # Database.pm and Output.pm are almost not modified (var test...)
252 # Biblio.pm is almost completly rewritten.
254 # WHAT DOES IT ??? ==> END of Hitchcock suspens
256 # 1st, it does... nothing...
257 # 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 ...
259 # All old-API functions have been cloned. for example, the "newbiblio" sub, now has become :
260 # * a "newbiblio" sub, with the same parameters. It just call a sub named OLDnewbiblio
261 # * 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.
262 # * 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.
263 # 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 ;-)
265 # 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.
266 # Note we have decided with steve that a old-biblio <=> a MARC-Biblio.
270 # Copyright 2000-2002 Katipo Communications
272 # This file is part of Koha.
274 # Koha is free software; you can redistribute it and/or modify it under the
275 # terms of the GNU General Public License as published by the Free Software
276 # Foundation; either version 2 of the License, or (at your option) any later
279 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
280 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
281 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
283 # You should have received a copy of the GNU General Public License along with
284 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
285 # Suite 330, Boston, MA 02111-1307 USA
293 use vars qw($VERSION @ISA @EXPORT);
295 # set the version for version checking
300 # don't forget MARCxxx subs are here only for testing purposes. Should not be used
301 # as the old-style API and the NEW one are the only public functions.
304 &updateBiblio &updateBiblioItem &updateItem
305 &itemcount &newbiblio &newbiblioitem
306 &modnote &newsubject &newsubtitle
307 &modbiblio &checkitems
308 &newitems &modbibitem
309 &modsubtitle &modsubject &modaddauthor &moditem &countitems
310 &delitem &deletebiblioitem &delbiblio
311 &getitemtypes &getbiblio
312 &getbiblioitembybiblionumber
313 &getbiblioitem &getitemsbybiblioitem
315 &newcompletebiblioitem
317 &MARCfind_oldbiblionumber_from_MARCbibid
318 &MARCfind_MARCbibid_from_oldbiblionumber
319 &MARCfind_marc_from_kohafield
323 &NEWnewbiblio &NEWnewitem
324 &NEWmodbiblio &NEWmoditem
326 &MARCaddbiblio &MARCadditem
327 &MARCmodsubfield &MARCaddsubfield
328 &MARCmodbiblio &MARCmoditem
329 &MARCkoha2marcBiblio &MARCmarc2koha
330 &MARCkoha2marcItem &MARChtml2marc
331 &MARCgetbiblio &MARCgetitem
332 &MARCaddword &MARCdelword
338 # MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC
341 # all the following subs takes a MARC::Record as parameter and manage
342 # the MARC-DB. They are called by the 1.0/1.2 xxx subs, and by the
343 # NEWxxx subs (xxx deals with old-DB parameters, the NEWxxx deals with MARC-DB parameter)
347 C4::Biblio - acquisition, catalog management functions
351 move from 1.2 to 1.4 version :
352 1.2 and previous version uses a specific API to manage biblios. This API uses old-DB style parameters.
353 In the 1.4 version, we want to do 2 differents things :
354 - keep populating the old-DB, that has a LOT less datas than MARC
355 - populate the MARC-DB
356 To populate the DBs we have 2 differents sources :
357 - the standard acquisition system (through book sellers), that does'nt use MARC data
358 - the MARC acquisition system, that uses MARC data.
360 Thus, we have 2 differents cases :
361 - 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
362 - 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
364 That's why we need 4 subs :
365 all I<subs beginning by MARC> manage only MARC tables. They manage MARC-DB with MARC::Record parameters
366 all I<subs beginning by OLD> manage only OLD-DB tables. They manage old-DB with old-DB parameters
367 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
368 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.
370 - NEW and old-style API should be used in koha to manage biblio
371 - MARCsubs are divided in 2 parts :
372 * some of them manage MARC parameters. They are heavily used in koha.
373 * some of them manage MARC biblio : they are mostly used by NEW and old-style subs.
374 - OLD are used internally only
376 all subs requires/use $dbh as 1st parameter.
378 I<NEWxxx related subs>
380 all subs requires/use $dbh as 1st parameter.
381 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
383 I<OLDxxx related subs>
385 all subs requires/use $dbh as 1st parameter.
386 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
388 They all are the exact copy of 1.0/1.2 version of the sub without the OLD.
389 The OLDxxx is called by the original xxx sub.
390 the 1.4 xxx sub also builds MARC::Record an calls the MARCxxx
392 WARNING : there is 1 difference between initialxxx and OLDxxx :
393 the db header $dbh is always passed as parameter to avoid over-DB connexion
399 =item @tagslib = &MARCgettagslib($dbh,1|0);
401 last param is 1 for liblibrarian and 0 for libopac
402 returns a hash with tag/subfield meaning
403 =item ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,$kohafield);
405 finds MARC tag and subfield for a given kohafield
406 kohafield is "table.field" where table= biblio|biblioitems|items, and field a field of the previous table
408 =item $biblionumber = &MARCfind_oldbiblionumber_from_MARCbibid($dbh,$MARCbibi);
410 finds a old-db biblio number for a given MARCbibid number
412 =item $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
414 finds a MARC bibid from a old-db biblionumber
416 =item $MARCRecord = &MARCkoha2marcBiblio($dbh,$biblionumber,biblioitemnumber);
418 MARCkoha2marcBiblio is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB biblio/biblioitem
420 =item $MARCRecord = &MARCkoha2marcItem($dbh,$biblionumber,itemnumber);
422 MARCkoha2marcItem is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB item
424 =item $MARCRecord = &MARCkoha2marcSubtitle($dbh,$biblionumber,$subtitle);
426 MARCkoha2marcSubtitle is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB subtitle
428 =item $olddb = &MARCmarc2koha($dbh,$MARCRecord);
430 builds a hash with old-db datas from a MARC::Record
432 =item &MARCaddbiblio($dbh,$MARC::Record,$biblionumber);
434 creates a biblio (in the MARC tables only). $biblionumber is the old-db biblionumber of the biblio
436 =item &MARCaddsubfield($dbh,$bibid,$tagid,$indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
438 adds a subfield in a biblio (in the MARC tables only).
440 =item $MARCRecord = &MARCgetbiblio($dbh,$bibid);
442 Returns a MARC::Record for the biblio $bibid.
444 =item &MARCmodbiblio($dbh,$bibid,$record,$delete);
446 MARCmodbiblio changes a biblio for a biblio,MARC::Record passed as parameter
447 It 1st delete the biblio, then recreates it.
448 WARNING : the $delete parameter is not used anymore (too much unsolvable cases).
449 =item ($subfieldid,$subfieldvalue) = &MARCmodsubfield($dbh,$subfieldid,$subfieldvalue);
451 MARCmodsubfield changes the value of a given subfield
453 =item $subfieldid = &MARCfindsubfield($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue);
455 MARCfindsubfield returns a subfield number given a bibid/tag/subfieldvalue values.
456 Returns -1 if more than 1 answer
458 =item $subfieldid = &MARCfindsubfieldid($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
460 MARCfindsubfieldid find a subfieldid for a bibid/tag/tagorder/subfield/subfieldorder
462 =item &MARCdelsubfield($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
464 MARCdelsubfield delete a subfield for a bibid/tag/tagorder/subfield/subfieldorder
466 =item &MARCdelbiblio($dbh,$bibid);
468 MARCdelbiblio delete biblio $bibid
470 =item &MARCkoha2marcOnefield
472 used by MARCkoha2marc and should not be useful elsewhere
474 =item &MARCmarc2kohaOnefield
476 used by MARCmarc2koha and should not be useful elsewhere
480 used to manage MARC_word table and should not be useful elsewhere
484 used to manage MARC_word table and should not be useful elsewhere
489 my ($dbh,$forlibrarian)= @_;
491 my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
492 $sth=$dbh->prepare("select tagfield,$libfield as lib,mandatory from marc_tag_structure order by tagfield");
494 my ($lib,$tag,$res,$tab,$mandatory,$repeatable);
495 while ( ($tag,$lib,$mandatory) = $sth->fetchrow) {
496 $res->{$tag}->{lib}=$lib;
497 $res->{$tab}->{tab}=""; # XXX
498 $res->{$tag}->{mandatory}=$mandatory;
501 $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");
505 my $authorised_value;
506 my $thesaurus_category;
509 while ( ($tag, $subfield, $lib, $tab, $mandatory, $repeatable,$authorised_value,$thesaurus_category,$value_builder,$kohafield) = $sth->fetchrow) {
510 $res->{$tag}->{$subfield}->{lib}=$lib;
511 $res->{$tag}->{$subfield}->{tab}=$tab;
512 $res->{$tag}->{$subfield}->{mandatory}=$mandatory;
513 $res->{$tag}->{$subfield}->{repeatable}=$repeatable;
514 $res->{$tag}->{$subfield}->{authorised_value}=$authorised_value;
515 $res->{$tag}->{$subfield}->{thesaurus_category}=$thesaurus_category;
516 $res->{$tag}->{$subfield}->{value_builder}=$value_builder;
517 $res->{$tag}->{$subfield}->{kohafield}=$kohafield;
522 sub MARCfind_marc_from_kohafield {
523 my ($dbh,$kohafield) = @_;
524 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
525 $sth->execute($kohafield);
526 my ($tagfield,$tagsubfield) = $sth->fetchrow;
527 return ($tagfield,$tagsubfield);
530 sub MARCfind_oldbiblionumber_from_MARCbibid {
531 my ($dbh,$MARCbibid) = @_;
532 my $sth=$dbh->prepare("select biblionumber from marc_biblio where bibid=?");
533 $sth->execute($MARCbibid);
534 my ($biblionumber) = $sth->fetchrow;
535 return $biblionumber;
538 sub MARCfind_MARCbibid_from_oldbiblionumber {
539 my ($dbh,$oldbiblionumber) = @_;
540 my $sth=$dbh->prepare("select bibid from marc_biblio where biblionumber=?");
541 $sth->execute($oldbiblionumber);
542 my ($bibid) = $sth->fetchrow;
547 # pass the MARC::Record to this function, and it will create the records in the marc tables
548 my ($dbh,$record,$biblionumber,$bibid) = @_;
549 my @fields=$record->fields();
551 # adding main table, and retrieving bibid
552 # if bibid is sent, then it's not a true add, it's only a re-add, after a delete (ie, a mod)
553 # if bibid empty => true add, find a new bibid number
555 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
556 my $sth=$dbh->prepare("insert into marc_biblio (datecreated,biblionumber) values (now(),?)");
557 $sth->execute($biblionumber);
558 $sth=$dbh->prepare("select max(bibid) from marc_biblio");
560 ($bibid)=$sth->fetchrow;
564 # now, add subfields...
565 foreach my $field (@fields) {
567 if ($field->tag() <10) {
568 &MARCaddsubfield($dbh,$bibid,
577 my @subfields=$field->subfields();
578 foreach my $subfieldcount (0..$#subfields) {
579 &MARCaddsubfield($dbh,$bibid,
581 $field->indicator(1).$field->indicator(2),
583 $subfields[$subfieldcount][0],
585 $subfields[$subfieldcount][1]
590 $dbh->do("unlock tables");
595 # pass the MARC::Record to this function, and it will create the records in the marc tables
596 my ($dbh,$record,$biblionumber) = @_;
597 # warn "adding : ".$record->as_formatted();
598 # search for MARC biblionumber
599 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
600 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
601 my @fields=$record->fields();
602 my $sth = $dbh->prepare("select max(tagorder) from marc_subfield_table where bibid=?");
603 $sth->execute($bibid);
604 my ($fieldcount) = $sth->fetchrow;
605 # now, add subfields...
606 foreach my $field (@fields) {
607 my @subfields=$field->subfields();
609 foreach my $subfieldcount (0..$#subfields) {
610 &MARCaddsubfield($dbh,$bibid,
612 $field->indicator(1).$field->indicator(2),
614 $subfields[$subfieldcount][0],
616 $subfields[$subfieldcount][1]
620 $dbh->do("unlock tables");
624 sub MARCaddsubfield {
625 # Add a new subfield to a tag into the DB.
626 my ($dbh,$bibid,$tagid,$tag_indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalues) = @_;
627 # if not value, end of job, we do nothing
628 if (length($subfieldvalues) ==0) {
631 if (not($subfieldcode)) {
634 my @subfieldvalues = split /\|/,$subfieldvalues;
635 foreach my $subfieldvalue (@subfieldvalues) {
636 if (length($subfieldvalue)>255) {
637 # $dbh->do("lock tables marc_blob_subfield WRITE, marc_subfield_table WRITE");
638 my $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
639 $sth->execute($subfieldvalue);
640 $sth=$dbh->prepare("select max(blobidlink)from marc_blob_subfield");
642 my ($res)=$sth->fetchrow;
643 $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,valuebloblink) values (?,?,?,?,?,?,?)");
644 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$res);
646 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";
648 # $dbh->do("unlock tables");
650 my $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?,?)");
651 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue);
653 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";
656 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
661 # Returns MARC::Record of the biblio passed in parameter.
663 my $record = MARC::Record->new();
664 #---- TODO : the leader is missing
665 $record->leader(' ');
666 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
667 from marc_subfield_table
668 where bibid=? order by tag,tagorder,subfieldcode
670 my $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
671 $sth->execute($bibid);
675 my $field; # for >=10 tags
676 my $prevvalue; # for <10 tags
677 while (my $row=$sth->fetchrow_hashref) {
678 if ($row->{'valuebloblink'}) { #---- search blob if there is one
679 $sth2->execute($row->{'valuebloblink'});
680 my $row2=$sth2->fetchrow_hashref;
682 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
684 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
687 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
689 $record->add_fields($field) unless $prevtag eq "XXX";
692 $prevtagorder=$row->{tagorder};
693 $prevtag = $row->{tag};
694 $previndicator=$row->{tag_indicator};
695 if ($row->{tag}<10) {
696 $prevvalue = $row->{subfieldvalue};
698 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.' ',0,1), substr($row->{tag_indicator}.' ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
701 if ($row->{tag} <10) {
702 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
704 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
706 $prevtag= $row->{tag};
707 $previndicator=$row->{tag_indicator};
710 # the last has not been included inside the loop... do it now !
712 $record->add_fields($prevtag,$prevvalue);
714 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
715 $record->add_fields($field);
720 # Returns MARC::Record of the biblio passed in parameter.
721 my ($dbh,$bibid,$itemnumber)=@_;
722 my $record = MARC::Record->new();
723 # search MARC tagorder
724 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=?");
725 $sth2->execute($bibid,$itemnumber);
726 my ($tagorder) = $sth2->fetchrow_array();
727 #---- TODO : the leader is missing
728 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
729 from marc_subfield_table
730 where bibid=? and tagorder=? order by subfieldcode,subfieldorder
732 $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
733 $sth->execute($bibid,$tagorder);
734 while (my $row=$sth->fetchrow_hashref) {
735 if ($row->{'valuebloblink'}) { #---- search blob if there is one
736 $sth2->execute($row->{'valuebloblink'});
737 my $row2=$sth2->fetchrow_hashref;
739 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
741 if ($record->field($row->{'tag'})) {
743 #--- this test must stay as this, because of strange behaviour of mySQL/Perl DBI with char var containing a number...
744 #--- sometimes, eliminates 0 at beginning, sometimes no ;-\\\
745 if (length($row->{'tag'}) <3) {
746 $row->{'tag'} = "0".$row->{'tag'};
748 $field =$record->field($row->{'tag'});
750 my $x = $field->add_subfields($row->{'subfieldcode'},$row->{'subfieldvalue'});
751 $record->delete_field($field);
752 $record->add_fields($field);
755 if (length($row->{'tag'}) < 3) {
756 $row->{'tag'} = "0".$row->{'tag'};
758 my $temp = MARC::Field->new($row->{'tag'}," "," ", $row->{'subfieldcode'} => $row->{'subfieldvalue'});
759 $record->add_fields($temp);
767 my ($dbh,$bibid,$record,$delete)=@_;
768 my $oldrecord=&MARCgetbiblio($dbh,$bibid);
769 if ($oldrecord eq $record) {
772 # 1st delete the biblio,
774 &MARCdelbiblio($dbh,$bibid,1);
775 my $biblionumber = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
776 &MARCaddbiblio($dbh,$record,$biblionumber,$bibid);
780 my ($dbh,$bibid,$keep_items) = @_;
781 # if the keep_item is set to 1, then all items are preserved.
782 # This flag is set when the delbiblio is called by modbiblio
783 # due to a too complex structure of MARC (repeatable fields and subfields),
784 # the best solution for a modif is to delete / recreate the record.
785 if ($keep_items eq 1) {
786 #search item field code
787 my $sth = $dbh->prepare("select tagfield from marc_subfield_structure where kohafield like 'items.%'");
789 my $itemtag = $sth->fetchrow_hashref->{tagfield};
790 $dbh->do("delete from marc_subfield_table where bibid=$bibid and tag<>$itemtag");
791 $dbh->do("delete from marc_word where bibid=$bibid and tag<>$itemtag");
793 $dbh->do("delete from marc_biblio where bibid=$bibid");
794 $dbh->do("delete from marc_subfield_table where bibid=$bibid");
795 $dbh->do("delete from marc_word where bibid=$bibid");
799 my ($dbh,$record,$bibid,$itemnumber,$delete)=@_;
800 my $oldrecord=&MARCgetitem($dbh,$bibid,$itemnumber);
801 # if nothing to change, don't waste time...
802 if ($oldrecord eq $record) {
806 # otherwise, skip through each subfield...
807 my @fields = $record->fields();
808 # search old MARC item
809 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=?");
810 $sth2->execute($bibid,$itemnumber);
811 my ($tagorder) = $sth2->fetchrow_array();
812 foreach my $field (@fields) {
813 my $oldfield = $oldrecord->field($field->tag());
814 my @subfields=$field->subfields();
816 foreach my $subfield (@subfields) {
818 # warn "compare : $oldfield".$oldfield->subfield(@$subfield[0]);
819 if ($oldfield eq 0 or (length($oldfield->subfield(@$subfield[0])) ==0) ) {
820 # just adding datas...
821 # warn "addfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
822 # warn "NEW subfield : $bibid,".$field->tag().",".$tagorder.",".@$subfield[0].",".$subfieldorder.",".@$subfield[1].")";
823 &MARCaddsubfield($dbh,$bibid,$field->tag(),$field->indicator(1).$field->indicator(2),
824 $tagorder,@$subfield[0],$subfieldorder,@$subfield[1]);
826 # warn "modfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
827 # modify he subfield if it's a different string
828 if ($oldfield->subfield(@$subfield[0]) ne @$subfield[1] ) {
829 my $subfieldid=&MARCfindsubfieldid($dbh,$bibid,$field->tag(),$tagorder,@$subfield[0],$subfieldorder);
830 # warn "changing : $subfieldid, $bibid,".$field->tag(),",$tagorder,@$subfield[0],@$subfield[1],$subfieldorder";
831 &MARCmodsubfield($dbh,$subfieldid,@$subfield[1]);
839 sub MARCmodsubfield {
840 # Subroutine changes a subfield value given a subfieldid.
841 my ($dbh, $subfieldid, $subfieldvalue )=@_;
842 $dbh->do("lock tables marc_blob_subfield WRITE,marc_subfield_table WRITE");
843 my $sth1=$dbh->prepare("select valuebloblink from marc_subfield_table where subfieldid=?");
844 $sth1->execute($subfieldid);
845 my ($oldvaluebloblink)=$sth1->fetchrow;
848 # if too long, use a bloblink
849 if (length($subfieldvalue)>255 ) {
850 # if already a bloblink, update it, otherwise, insert a new one.
851 if ($oldvaluebloblink) {
852 $sth=$dbh->prepare("update marc_blob_subfield set subfieldvalue=? where blobidlink=?");
853 $sth->execute($subfieldvalue,$oldvaluebloblink);
855 $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
856 $sth->execute($subfieldvalue);
857 $sth=$dbh->prepare("select max(blobidlink) from marc_blob_subfield");
859 my ($res)=$sth->fetchrow;
860 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=null, valuebloblink=$res where subfieldid=?");
861 $sth->execute($subfieldid);
864 # note this can leave orphan bloblink. Not a big problem, but we should build somewhere a orphan deleting script...
865 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=?,valuebloblink=null where subfieldid=?");
866 $sth->execute($subfieldvalue, $subfieldid);
868 $dbh->do("unlock tables");
870 $sth=$dbh->prepare("select bibid,tag,tagorder,subfieldcode,subfieldid,subfieldorder from marc_subfield_table where subfieldid=?");
871 $sth->execute($subfieldid);
872 my ($bibid,$tagid,$tagorder,$subfieldcode,$x,$subfieldorder) = $sth->fetchrow;
874 &MARCdelword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder);
875 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
876 return($subfieldid, $subfieldvalue);
879 sub MARCfindsubfield {
880 my ($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue) = @_;
884 my $query="select subfieldid from marc_subfield_table where bibid=? and tag=? and subfieldcode=?";
885 if ($subfieldvalue) {
886 $query .= " and subfieldvalue=".$dbh->quote($subfieldvalue);
888 if ($subfieldorder<1) {
891 $query .= " and subfieldorder=$subfieldorder";
893 my $sti=$dbh->prepare($query);
894 $sti->execute($bibid,$tag, $subfieldcode);
895 while (($subfieldid) = $sti->fetchrow) {
897 $lastsubfieldid=$subfieldid;
899 if ($resultcounter>1) {
900 # Error condition. Values given did not resolve into a unique record. Don't know what to edit
901 # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
904 return $lastsubfieldid;
908 sub MARCfindsubfieldid {
909 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
910 my $sth=$dbh->prepare("select subfieldid from marc_subfield_table
911 where bibid=? and tag=? and tagorder=?
912 and subfieldcode=? and subfieldorder=?");
913 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
914 my ($res) = $sth->fetchrow;
916 $sth=$dbh->prepare("select subfieldid from marc_subfield_table
917 where bibid=? and tag=? and tagorder=?
918 and subfieldcode=?");
919 $sth->execute($bibid,$tag,$tagorder,$subfield);
920 ($res) = $sth->fetchrow;
925 sub MARCdelsubfield {
926 # delete a subfield for $bibid / tag / tagorder / subfield / subfieldorder
927 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
928 $dbh->do("delete from marc_subfield_table where bibid='$bibid' and
929 tag='$tag' and tagorder='$tagorder'
930 and subfieldcode='$subfield' and subfieldorder='$subfieldorder
934 sub MARCkoha2marcBiblio {
935 # this function builds partial MARC::Record from the old koha-DB fields
936 my ($dbh,$biblionumber,$biblioitemnumber) = @_;
937 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
938 my $record = MARC::Record->new();
939 #--- if bibid, then retrieve old-style koha data
940 if ($biblionumber>0) {
941 my $sth2=$dbh->prepare("select biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp
942 from biblio where biblionumber=?");
943 $sth2->execute($biblionumber);
944 my $row=$sth2->fetchrow_hashref;
946 foreach $code (keys %$row) {
948 &MARCkoha2marcOnefield($sth,$record,"biblio.".$code,$row->{$code});
952 #--- if biblioitem, then retrieve old-style koha data
953 if ($biblioitemnumber>0) {
954 my $sth2=$dbh->prepare(" SELECT biblioitemnumber,biblionumber,volume,number,classification,
955 itemtype,url,isbn,issn,dewey,subclass,publicationyear,publishercode,
956 volumedate,volumeddesc,timestamp,illus,pages,notes AS bnotes,size,place
958 WHERE biblionumber=? and biblioitemnumber=?
960 $sth2->execute($biblionumber,$biblioitemnumber);
961 my $row=$sth2->fetchrow_hashref;
963 foreach $code (keys %$row) {
965 &MARCkoha2marcOnefield($sth,$record,"biblioitems.".$code,$row->{$code});
969 # other fields => additional authors, subjects, subtitles
970 my $sth2=$dbh->prepare(" SELECT author FROM additionalauthors WHERE biblionumber=?");
971 $sth2->execute($biblionumber);
972 while (my $row=$sth2->fetchrow_hashref) {
973 &MARCkoha2marcOnefield($sth,$record,"additionalauthors.author",$row->{'author'});
975 my $sth2=$dbh->prepare(" SELECT subject FROM bibliosubject WHERE biblionumber=?");
976 $sth2->execute($biblionumber);
977 while (my $row=$sth2->fetchrow_hashref) {
978 &MARCkoha2marcOnefield($sth,$record,"bibliosubject.subject",$row->{'subject'});
980 my $sth2=$dbh->prepare(" SELECT subtitle FROM bibliosubtitle WHERE biblionumber=?");
981 $sth2->execute($biblionumber);
982 while (my $row=$sth2->fetchrow_hashref) {
983 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.title",$row->{'subtitle'});
988 sub MARCkoha2marcItem {
989 # this function builds partial MARC::Record from the old koha-DB fields
990 my ($dbh,$biblionumber,$itemnumber) = @_;
991 # my $dbh=&C4Connect;
992 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
993 my $record = MARC::Record->new();
994 #--- if item, then retrieve old-style koha data
996 # print STDERR "prepare $biblionumber,$itemnumber\n";
997 my $sth2=$dbh->prepare("SELECT itemnumber,biblionumber,multivolumepart,biblioitemnumber,barcode,dateaccessioned,
998 booksellerid,homebranch,price,replacementprice,replacementpricedate,datelastborrowed,
999 datelastseen,multivolume,stack,notforloan,itemlost,wthdrawn,bulk,issues,renewals,
1000 reserves,restricted,binding,itemnotes,holdingbranch,timestamp
1002 WHERE itemnumber=?");
1003 $sth2->execute($itemnumber);
1004 my $row=$sth2->fetchrow_hashref;
1006 foreach $code (keys %$row) {
1007 if ($row->{$code}) {
1008 &MARCkoha2marcOnefield($sth,$record,"items.".$code,$row->{$code});
1015 sub MARCkoha2marcSubtitle {
1016 # this function builds partial MARC::Record from the old koha-DB fields
1017 my ($dbh,$bibnum,$subtitle) = @_;
1018 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1019 my $record = MARC::Record->new();
1020 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.subtitle",$subtitle);
1024 sub MARCkoha2marcOnefield {
1025 my ($sth,$record,$kohafieldname,$value)=@_;
1028 $sth->execute($kohafieldname);
1029 if (($tagfield,$tagsubfield)=$sth->fetchrow) {
1030 if ($record->field($tagfield)) {
1031 my $tag =$record->field($tagfield);
1033 $tag->add_subfields($tagsubfield,$value);
1034 $record->delete_field($tag);
1035 $record->add_fields($tag);
1038 $record->add_fields($tagfield," "," ",$tagsubfield => $value);
1045 my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
1047 my $record = MARC::Record->new();
1048 # my %subfieldlist=();
1049 my $prevvalue; # if tag <10
1050 my $field; # if tag >=10
1051 for (my $i=0; $i< @$rtags; $i++) {
1052 # rebuild MARC::Record
1053 if (@$rtags[$i] ne $prevtag) {
1054 if ($prevtag < 10) {
1056 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
1060 $record->add_fields($field);
1063 $indicators{@$rtags[$i]}.=' ';
1064 if (@$rtags[$i] <10) {
1065 $prevvalue= @$rvalues[$i];
1067 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
1069 $prevtag = @$rtags[$i];
1071 if (@$rtags[$i] <10) {
1072 $prevvalue=@$rvalues[$i];
1074 if (@$rvalues[$i]) {
1075 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
1078 $prevtag= @$rtags[$i];
1081 # the last has not been included inside the loop... do it now !
1082 $record->add_fields($field);
1083 # warn $record->as_formatted;
1088 my ($dbh,$record) = @_;
1089 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1091 my $sth2=$dbh->prepare("SHOW COLUMNS from biblio");
1094 # print STDERR $record->as_formatted;
1095 while (($field)=$sth2->fetchrow) {
1096 $result=&MARCmarc2kohaOneField($sth,"biblio",$field,$record,$result);
1098 $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
1100 while (($field)=$sth2->fetchrow) {
1101 $result=&MARCmarc2kohaOneField($sth,"biblioitems",$field,$record,$result);
1103 $sth2=$dbh->prepare("SHOW COLUMNS from items");
1105 while (($field)=$sth2->fetchrow) {
1106 $result = &MARCmarc2kohaOneField($sth,"items",$field,$record,$result);
1108 # additional authors : specific
1109 $result = &MARCmarc2kohaOneField($sth,"additionalauthors","additionalauthors",$record,$result);
1110 # modify copyrightdate to keep only the 1st year found
1111 my $temp = $result->{'copyrightdate'};
1112 $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
1114 $result->{'copyrightdate'} = $1;
1115 } else { # if no cYYYY, get the 1st date.
1116 $temp =~ m/(\d\d\d\d)/;
1117 $result->{'copyrightdate'} = $1;
1122 sub MARCmarc2kohaOneField {
1123 # FIXME ? if a field has a repeatable subfield that is used in old-db, only the 1st will be retrieved...
1124 my ($sth,$kohatable,$kohafield,$record,$result)= @_;
1125 # warn "kohatable / $kohafield / $result / ";
1129 $sth->execute($kohatable.".".$kohafield);
1130 ($tagfield,$subfield) = $sth->fetchrow;
1131 foreach my $field ($record->field($tagfield)) {
1132 if ($field->subfield($subfield)) {
1133 if ($result->{$kohafield}) {
1134 $result->{$kohafield} .= " | ".$field->subfield($subfield);
1136 $result->{$kohafield}=$field->subfield($subfield);
1144 # split a subfield string and adds it into the word table.
1146 my ($dbh,$bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$sentence) =@_;
1147 $sentence =~ s/(\.|\?|\:|\!|\'|,|\-)/ /g;
1148 my @words = split / /,$sentence;
1149 my $stopwords= C4::Context->stopwords;
1150 my $sth=$dbh->prepare("insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word)
1151 values (?,?,?,?,?,?,soundex(?))");
1152 foreach my $word (@words) {
1153 # we record only words longer than 2 car and not in stopwords hash
1154 if (length($word)>1 and !($stopwords->{uc($word)})) {
1155 $sth->execute($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,$word);
1157 warn "ERROR ==> insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word) values ($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,soundex($word))\n";
1164 # delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
1165 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
1166 my $sth=$dbh->prepare("delete from marc_word where bibid=? and tag=? and tagorder=? and subfieldid=? and subfieldorder=?");
1167 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
1172 # NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
1175 # all the following subs are useful to manage MARC-DB with complete MARC records.
1176 # it's used with marcimport, and marc management tools
1180 =item ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbibilio($dbh,$MARCRecord,$oldbiblio,$oldbiblioitem);
1182 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
1183 are builded from the MARC::Record. If they are passed, they are used.
1185 =item NEWnewitem($dbh, $record,$bibid);
1187 adds an item in the db.
1192 my ($dbh, $record, $oldbiblio, $oldbiblioitem) = @_;
1193 # note $oldbiblio and $oldbiblioitem are not mandatory.
1194 # if not present, they will be builded from $record with MARCmarc2koha function
1195 if (($oldbiblio) and not($oldbiblioitem)) {
1196 print STDERR "NEWnewbiblio : missing parameter\n";
1197 print "NEWnewbiblio : missing parameter : contact koha development team\n";
1203 $oldbibnum = OLDnewbiblio($dbh,$oldbiblio);
1204 $oldbiblioitem->{'biblionumber'} = $oldbibnum;
1205 $oldbibitemnum = OLDnewbiblioitem($dbh,$oldbiblioitem);
1207 my $olddata = MARCmarc2koha($dbh,$record);
1208 $oldbibnum = OLDnewbiblio($dbh,$olddata);
1209 $olddata->{'biblionumber'} = $oldbibnum;
1210 $oldbibitemnum = OLDnewbiblioitem($dbh,$olddata);
1212 # search subtiles, addiauthors and subjects
1213 my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author");
1214 my @addiauthfields = $record->field($tagfield);
1215 foreach my $addiauthfield (@addiauthfields) {
1216 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1217 foreach my $subfieldcount (0..$#addiauthsubfields) {
1218 OLDmodaddauthor($dbh,$oldbibnum,$addiauthsubfields[$subfieldcount]);
1221 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle");
1222 my @subtitlefields = $record->field($tagfield);
1223 foreach my $subtitlefield (@subtitlefields) {
1224 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1225 foreach my $subfieldcount (0..$#subtitlesubfields) {
1226 OLDnewsubtitle($dbh,$oldbibnum,$subtitlesubfields[$subfieldcount]);
1229 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject");
1230 my @subj = $record->field($tagfield);
1231 foreach my $subject (@subj) {
1232 my @subjsubfield = $subject->subfield($tagsubfield);
1234 foreach my $subfieldcount (0..$#subjsubfield) {
1235 push @subjects,$subjsubfield[$subfieldcount];
1237 OLDmodsubject($dbh,$oldbibnum,1,@subjects);
1239 # we must add bibnum and bibitemnum in MARC::Record...
1240 # we build the new field with biblionumber and biblioitemnumber
1241 # we drop the original field
1242 # we add the new builded field.
1243 # NOTE : Works only if the field is ONLY for biblionumber and biblioitemnumber
1244 # (steve and paul : thinks 090 is a good choice)
1245 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1246 $sth->execute("biblio.biblionumber");
1247 (my $tagfield1, my $tagsubfield1) = $sth->fetchrow;
1248 $sth->execute("biblioitems.biblioitemnumber");
1249 (my $tagfield2, my $tagsubfield2) = $sth->fetchrow;
1250 if ($tagfield1 != $tagfield2) {
1251 warn "Error in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1252 print "Content-Type: text/html\n\nError in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1255 my $newfield = MARC::Field->new( $tagfield1,'','',
1256 "$tagsubfield1" => $oldbibnum,
1257 "$tagsubfield2" => $oldbibitemnum);
1258 # drop old field and create new one...
1259 my $old_field = $record->field($tagfield1);
1260 $record->delete_field($old_field);
1261 $record->add_fields($newfield);
1262 my $bibid = MARCaddbiblio($dbh,$record,$oldbibnum);
1263 return ($bibid,$oldbibnum,$oldbibitemnum );
1267 my ($dbh,$record,$bibid) =@_;
1268 &MARCmodbiblio($dbh,$bibid,$record,0);
1269 my $oldbiblio = MARCmarc2koha($dbh,$record);
1270 my $oldbiblionumber = OLDmodbiblio($dbh,$oldbiblio);
1271 OLDmodbibitem($dbh,$oldbiblio);
1272 # now, modify addi authors, subject, addititles.
1273 my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author");
1274 my @addiauthfields = $record->field($tagfield);
1275 foreach my $addiauthfield (@addiauthfields) {
1276 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1277 foreach my $subfieldcount (0..$#addiauthsubfields) {
1278 OLDmodaddauthor($dbh,$oldbiblionumber,$addiauthsubfields[$subfieldcount]);
1281 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle");
1282 my @subtitlefields = $record->field($tagfield);
1283 foreach my $subtitlefield (@subtitlefields) {
1284 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1285 foreach my $subfieldcount (0..$#subtitlesubfields) {
1286 OLDmodsubtitle($dbh,$oldbiblionumber,$subtitlesubfields[$subfieldcount]);
1289 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject");
1290 my @subj = $record->field($tagfield);
1291 foreach my $subject (@subj) {
1292 my @subjsubfield = $subject->subfield($tagsubfield);
1294 foreach my $subfieldcount (0..$#subjsubfield) {
1295 push @subjects,$subjsubfield[$subfieldcount];
1297 OLDmodsubject($dbh,$oldbiblionumber,1,@subjects);
1304 my ($dbh, $record,$bibid) = @_;
1305 # add item in old-DB
1306 my $item = &MARCmarc2koha($dbh,$record);
1307 # needs old biblionumber and biblioitemnumber
1308 $item->{'biblionumber'} = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
1309 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
1310 $sth->execute($item->{'biblionumber'});
1311 ($item->{'biblioitemnumber'}) = $sth->fetchrow;
1312 my ($itemnumber,$error) = &OLDnewitems($dbh,$item,$item->{barcode});
1313 # add itemnumber to MARC::Record before adding the item.
1314 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1315 &MARCkoha2marcOnefield($sth,$record,"items.itemnumber",$itemnumber);
1317 my $bib = &MARCadditem($dbh,$record,$item->{'biblionumber'});
1321 my ($dbh,$record,$bibid,$itemnumber,$delete) = @_;
1322 &MARCmoditem($dbh,$record,$bibid,$itemnumber,$delete);
1323 my $olditem = MARCmarc2koha($dbh,$record);
1324 OLDmoditem($dbh,$olditem);
1329 # OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
1333 =item $biblionumber = OLDnewbiblio($dbh,$biblio);
1335 adds a record in biblio table. Datas are in the hash $biblio.
1337 =item $biblionumber = OLDmodbiblio($dbh,$biblio);
1339 modify a record in biblio table. Datas are in the hash $biblio.
1341 =item OLDmodsubtitle($dbh,$bibnum,$subtitle);
1343 modify subtitles in bibliosubtitle table.
1345 =item OLDmodaddauthor($dbh,$bibnum,$author);
1347 adds or modify additional authors
1348 NOTE : Strange sub : seems to delete MANY and add only ONE author... maybe buggy ?
1350 =item $errors = OLDmodsubject($dbh,$bibnum, $force, @subject);
1352 modify/adds subjects
1354 =item OLDmodbibitem($dbh, $biblioitem);
1358 =item OLDmodnote($dbh,$bibitemnum,$note
1360 modify a note for a biblioitem
1362 =item OLDnewbiblioitem($dbh,$biblioitem);
1364 adds a biblioitem ($biblioitem is a hash with the values)
1366 =item OLDnewsubject($dbh,$bibnum);
1370 =item OLDnewsubtitle($dbh,$bibnum,$subtitle);
1372 create a new subtitle
1374 =item ($itemnumber,$errors)= OLDnewitems($dbh,$item,$barcode);
1376 create a item. $item is a hash and $barcode the barcode.
1378 =item OLDmoditem($dbh,$item);
1382 =item OLDdelitem($dbh,$itemnum);
1386 =item OLDdeletebiblioitem($dbh,$biblioitemnumber);
1388 deletes a biblioitem
1389 NOTE : not standard sub name. Should be OLDdelbiblioitem()
1391 =item OLDdelbiblio($dbh,$biblio);
1398 my ($dbh,$biblio) = @_;
1399 # my $dbh = &C4Connect;
1400 my $query = "Select max(biblionumber) from biblio";
1401 my $sth = $dbh->prepare($query);
1403 my $data = $sth->fetchrow_arrayref;
1404 my $bibnum = $$data[0] + 1;
1407 if ($biblio->{'seriestitle'}) { $series = 1 };
1409 $query = "insert into biblio set biblionumber = ?, title = ?, author = ?, copyrightdate = ?,
1410 serial = ?, seriestitle = ?, notes = ?, abstract = ?";
1411 $sth = $dbh->prepare($query);
1412 $sth->execute($bibnum,$biblio->{'title'},$biblio->{'author'},$biblio->{'copyright'},$series,$biblio->{'seriestitle'},$biblio->{'notes'},$biblio->{'abstract'});
1420 my ($dbh,$biblio) = @_;
1421 # my $dbh = C4Connect;
1425 $query = "Update biblio set title = ?, author = ?, abstract = ?, copyrightdate = ?,
1426 seriestitle = ?, serial = ?, unititle = ?, notes = ? where biblionumber = ?";
1427 $sth = $dbh->prepare($query);
1428 $sth->execute($biblio->{'title'},$biblio->{'author'},$biblio->{'abstract'},$biblio->{'copyrightdate'},
1429 $biblio->{'seriestitle'},$biblio->{'serial'},$biblio->{'unititle'},$biblio->{'notes'},$biblio->{'biblionumber'});
1432 return($biblio->{'biblionumber'});
1435 sub OLDmodsubtitle {
1436 my ($dbh,$bibnum, $subtitle) = @_;
1437 my $query = "update bibliosubtitle set subtitle = ? where biblionumber = ?";
1438 my $sth = $dbh->prepare($query);
1439 $sth->execute($subtitle,$bibnum);
1444 sub OLDmodaddauthor {
1445 my ($dbh,$bibnum, $author) = @_;
1446 # my $dbh = C4Connect;
1447 my $query = "Delete from additionalauthors where biblionumber = $bibnum";
1448 my $sth = $dbh->prepare($query);
1453 if ($author ne '') {
1454 $query = "Insert into additionalauthors set
1457 $sth = $dbh->prepare($query);
1459 $sth->execute($author,$bibnum);
1463 } # sub modaddauthor
1467 my ($dbh,$bibnum, $force, @subject) = @_;
1468 # my $dbh = C4Connect;
1469 my $count = @subject;
1471 for (my $i = 0; $i < $count; $i++) {
1472 $subject[$i] =~ s/^ //g;
1473 $subject[$i] =~ s/ $//g;
1474 my $query = "select * from catalogueentry where entrytype = 's' and catalogueentry = '$subject[$i]'";
1475 my $sth = $dbh->prepare($query);
1478 if (my $data = $sth->fetchrow_hashref) {
1480 if ($force eq $subject[$i] || $force eq 1) {
1481 # subject not in aut, chosen to force anway
1482 # so insert into cataloguentry so its in auth file
1483 $query = "Insert into catalogueentry (entrytype,catalogueentry) values ('s','$subject[$i]')";
1484 my $sth2 = $dbh->prepare($query);
1489 $error = "$subject[$i]\n does not exist in the subject authority file";
1490 $query = "Select * from catalogueentry where entrytype = 's' and (catalogueentry like '$subject[$i] %'
1491 or catalogueentry like '% $subject[$i] %' or catalogueentry like '% $subject[$i]')";
1492 my $sth2 = $dbh->prepare($query);
1494 while (my $data = $sth2->fetchrow_hashref) {
1495 $error .= "<br>$data->{'catalogueentry'}";
1503 my $query = "Delete from bibliosubject where biblionumber = $bibnum";
1504 my $sth = $dbh->prepare($query);
1507 for (my $i = 0; $i < $count; $i++) {
1508 $sth = $dbh->prepare("Insert into bibliosubject values ('$subject[$i]', $bibnum)");
1519 my ($dbh,$biblioitem) = @_;
1520 # my $dbh = C4Connect;
1523 $biblioitem->{'itemtype'} = $dbh->quote($biblioitem->{'itemtype'});
1524 $biblioitem->{'url'} = $dbh->quote($biblioitem->{'url'});
1525 $biblioitem->{'isbn'} = $dbh->quote($biblioitem->{'isbn'});
1526 $biblioitem->{'publishercode'} = $dbh->quote($biblioitem->{'publishercode'});
1527 $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
1528 $biblioitem->{'classification'} = $dbh->quote($biblioitem->{'classification'});
1529 $biblioitem->{'dewey'} = $dbh->quote($biblioitem->{'dewey'});
1530 $biblioitem->{'subclass'} = $dbh->quote($biblioitem->{'subclass'});
1531 $biblioitem->{'illus'} = $dbh->quote($biblioitem->{'illus'});
1532 $biblioitem->{'pages'} = $dbh->quote($biblioitem->{'pages'});
1533 $biblioitem->{'volumeddesc'} = $dbh->quote($biblioitem->{'volumeddesc'});
1534 $biblioitem->{'bnotes'} = $dbh->quote($biblioitem->{'bnotes'});
1535 $biblioitem->{'size'} = $dbh->quote($biblioitem->{'size'});
1536 $biblioitem->{'place'} = $dbh->quote($biblioitem->{'place'});
1538 $query = "Update biblioitems set
1539 itemtype = $biblioitem->{'itemtype'},
1540 url = $biblioitem->{'url'},
1541 isbn = $biblioitem->{'isbn'},
1542 publishercode = $biblioitem->{'publishercode'},
1543 publicationyear = $biblioitem->{'publicationyear'},
1544 classification = $biblioitem->{'classification'},
1545 dewey = $biblioitem->{'dewey'},
1546 subclass = $biblioitem->{'subclass'},
1547 illus = $biblioitem->{'illus'},
1548 pages = $biblioitem->{'pages'},
1549 volumeddesc = $biblioitem->{'volumeddesc'},
1550 notes = $biblioitem->{'bnotes'},
1551 size = $biblioitem->{'size'},
1552 place = $biblioitem->{'place'}
1553 where biblioitemnumber = $biblioitem->{'biblioitemnumber'}";
1561 my ($dbh,$bibitemnum,$note)=@_;
1562 # my $dbh=C4Connect;
1563 my $query="update biblioitems set notes='$note' where
1564 biblioitemnumber='$bibitemnum'";
1565 my $sth=$dbh->prepare($query);
1571 sub OLDnewbiblioitem {
1572 my ($dbh,$biblioitem) = @_;
1573 # my $dbh = C4Connect;
1574 my $query = "Select max(biblioitemnumber) from biblioitems";
1575 my $sth = $dbh->prepare($query);
1580 $data = $sth->fetchrow_arrayref;
1581 $bibitemnum = $$data[0] + 1;
1585 $sth = $dbh->prepare("insert into biblioitems set
1586 biblioitemnumber = ?, biblionumber = ?,
1587 volume = ?, number = ?,
1588 classification = ?, itemtype = ?,
1590 issn = ?, dewey = ?,
1591 subclass = ?, publicationyear = ?,
1592 publishercode = ?, volumedate = ?,
1593 volumeddesc = ?, illus = ?,
1594 pages = ?, notes = ?,
1596 marc = ?, place = ?");
1597 $sth->execute($bibitemnum, $biblioitem->{'biblionumber'},
1598 $biblioitem->{'volume'}, $biblioitem->{'number'},
1599 $biblioitem->{'classification'}, $biblioitem->{'itemtype'},
1600 $biblioitem->{'url'}, $biblioitem->{'isbn'},
1601 $biblioitem->{'issn'}, $biblioitem->{'dewey'},
1602 $biblioitem->{'subclass'}, $biblioitem->{'publicationyear'},
1603 $biblioitem->{'publishercode'}, $biblioitem->{'volumedate'},
1604 $biblioitem->{'volumeddesc'}, $biblioitem->{'illus'},
1605 $biblioitem->{'pages'}, $biblioitem->{'bnotes'},
1606 $biblioitem->{'size'}, $biblioitem->{'lccn'},
1607 $biblioitem->{'marc'}, $biblioitem->{'place'});
1610 return($bibitemnum);
1614 my ($dbh,$bibnum)=@_;
1615 my $query="insert into bibliosubject (biblionumber) values ($bibnum)";
1616 my $sth=$dbh->prepare($query);
1621 sub OLDnewsubtitle {
1622 my ($dbh,$bibnum, $subtitle) = @_;
1623 my $query = "insert into bibliosubtitle set biblionumber = ?, subtitle = ?";
1624 my $sth = $dbh->prepare($query);
1625 $sth->execute($bibnum,$subtitle);
1631 my ($dbh,$item, $barcode) = @_;
1632 # my $dbh = C4Connect;
1633 my $query = "Select max(itemnumber) from items";
1634 my $sth = $dbh->prepare($query);
1640 $data = $sth->fetchrow_hashref;
1641 $itemnumber = $data->{'max(itemnumber)'} + 1;
1643 # if dateaccessioned is provided, use it. Otherwise, set to NOW()
1644 if ($item->{'dateaccessioned'}) {
1645 $sth=$dbh->prepare("Insert into items set
1646 itemnumber = ?, biblionumber = ?,
1647 biblioitemnumber = ?, barcode = ?,
1648 booksellerid = ?, dateaccessioned = ?,
1649 homebranch = ?, holdingbranch = ?,
1650 price = ?, replacementprice = ?,
1651 replacementpricedate = NOW(), itemnotes = ?,
1654 $sth->execute($itemnumber, $item->{'biblionumber'},
1655 $item->{'biblioitemnumber'},$barcode,
1656 $item->{'booksellerid'},$item->{'dateaccessioned'},
1657 $item->{'homebranch'},$item->{'holdingbranch'},
1658 $item->{'price'},$item->{'replacementprice'},
1659 $item->{'itemnotes'},$item->{'loan'});
1661 $sth=$dbh->prepare("Insert into items set
1662 itemnumber = ?, biblionumber = ?,
1663 biblioitemnumber = ?, barcode = ?,
1664 booksellerid = ?, dateaccessioned = NOW(),
1665 homebranch = ?, holdingbranch = ?,
1666 price = ?, replacementprice = ?,
1667 replacementpricedate = NOW(), itemnotes = ?,
1670 $sth->execute($itemnumber, $item->{'biblionumber'},
1671 $item->{'biblioitemnumber'},$barcode,
1672 $item->{'booksellerid'},
1673 $item->{'homebranch'},$item->{'holdingbranch'},
1674 $item->{'price'},$item->{'replacementprice'},
1675 $item->{'itemnotes'},$item->{'loan'});
1677 if (defined $sth->errstr) {
1678 $error .= $sth->errstr;
1681 return($itemnumber,$error);
1685 my ($dbh,$item) = @_;
1686 # my ($dbh,$loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
1687 # my $dbh=C4Connect;
1688 $item->{'itemnum'}=$item->{'itemnumber'} unless $item->{'itemnum'};
1689 my $query="update items set barcode='$item->{'barcode'}',itemnotes='$item->{'notes'}'
1690 where itemnumber=$item->{'itemnum'}";
1691 if ($item->{'barcode'} eq ''){
1692 $query="update items set notforloan=$item->{'loan'} where itemnumber=$item->{'itemnum'}";
1694 if ($item->{'lost'} ne ''){
1695 $query="update items set biblioitemnumber=$item->{'bibitemnum'},
1696 barcode='$item->{'barcode'}',
1697 itemnotes='$item->{'notes'}',
1698 homebranch='$item->{'homebranch'}',
1699 itemlost='$item->{'lost'}',
1700 wthdrawn='$item->{'wthdrawn'}'
1701 where itemnumber=$item->{'itemnum'}";
1703 if ($item->{'replacement'} ne ''){
1704 $query=~ s/ where/,replacementprice='$item->{'replacement'}' where/;
1706 my $sth=$dbh->prepare($query);
1713 my ($dbh,$itemnum)=@_;
1714 # my $dbh=C4Connect;
1715 my $query="select * from items where itemnumber=$itemnum";
1716 my $sth=$dbh->prepare($query);
1718 my @data=$sth->fetchrow_array;
1720 $query="Insert into deleteditems values (";
1721 foreach my $temp (@data){
1722 $query .= "'$temp',";
1726 $sth=$dbh->prepare($query);
1729 $query = "Delete from items where itemnumber=$itemnum";
1730 $sth=$dbh->prepare($query);
1736 sub OLDdeletebiblioitem {
1737 my ($dbh,$biblioitemnumber) = @_;
1738 # my $dbh = C4Connect;
1739 my $query = "Select * from biblioitems
1740 where biblioitemnumber = $biblioitemnumber";
1741 my $sth = $dbh->prepare($query);
1746 if (@results = $sth->fetchrow_array) {
1747 $query = "Insert into deletedbiblioitems values (";
1748 foreach my $value (@results) {
1749 $value = $dbh->quote($value);
1750 $query .= "$value,";
1753 $query =~ s/\,$/\)/;
1756 $query = "Delete from biblioitems
1757 where biblioitemnumber = $biblioitemnumber";
1761 # Now delete all the items attached to the biblioitem
1762 $query = "Select * from items where biblioitemnumber = $biblioitemnumber";
1763 $sth = $dbh->prepare($query);
1765 while (@results = $sth->fetchrow_array) {
1766 $query = "Insert into deleteditems values (";
1767 foreach my $value (@results) {
1768 $value = $dbh->quote($value);
1769 $query .= "$value,";
1771 $query =~ s/\,$/\)/;
1775 $query = "Delete from items where biblioitemnumber = $biblioitemnumber";
1778 } # sub deletebiblioitem
1781 my ($dbh,$biblio)=@_;
1782 # my $dbh=C4Connect;
1783 my $query="select * from biblio where biblionumber=$biblio";
1784 my $sth=$dbh->prepare($query);
1786 if (my @data=$sth->fetchrow_array){
1788 $query="Insert into deletedbiblio values (";
1789 foreach my $temp (@data){
1790 $temp=~ s/\'/\\\'/g;
1791 $query .= "'$temp',";
1795 $sth=$dbh->prepare($query);
1798 $query = "Delete from biblio where biblionumber=$biblio";
1799 $sth=$dbh->prepare($query);
1815 my $dbh = C4::Context->dbh;
1816 my $query="Select count(*) from items where biblionumber=$biblio";
1818 my $sth=$dbh->prepare($query);
1820 my $data=$sth->fetchrow_hashref;
1822 return($data->{'count(*)'});
1827 ($order, $ordernumber) = &getorder($biblioitemnumber, $biblionumber);
1829 Looks up the order with the given biblionumber and biblioitemnumber.
1831 Returns a two-element array. C<$ordernumber> is the order number.
1832 C<$order> is a reference-to-hash describing the order; its keys are
1833 fields from the biblio, biblioitems, aqorders, and aqorderbreakdown
1834 tables of the Koha database.
1838 # FIXME - This is effectively identical to &C4::Catalogue::getorder.
1839 # Pick one and stick with it.
1842 my $dbh = C4::Context->dbh;
1843 my $query="Select ordernumber
1845 where biblionumber=? and biblioitemnumber=?";
1846 my $sth=$dbh->prepare($query);
1847 $sth->execute($bib,$bi);
1848 # FIXME - Use fetchrow_array(), since we're only interested in the one
1850 my $ordnum=$sth->fetchrow_hashref;
1852 my $order=getsingleorder($ordnum->{'ordernumber'});
1854 return ($order,$ordnum->{'ordernumber'});
1857 =item getsingleorder
1859 $order = &getsingleorder($ordernumber);
1861 Looks up an order by order number.
1863 Returns a reference-to-hash describing the order. The keys of
1864 C<$order> are fields from the biblio, biblioitems, aqorders, and
1865 aqorderbreakdown tables of the Koha database.
1869 # FIXME - This is effectively identical to
1870 # &C4::Catalogue::getsingleorder.
1871 # Pick one and stick with it.
1872 sub getsingleorder {
1874 my $dbh = C4::Context->dbh;
1875 my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown
1876 where aqorders.ordernumber=?
1877 and biblio.biblionumber=aqorders.biblionumber and
1878 biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
1879 aqorders.ordernumber=aqorderbreakdown.ordernumber";
1880 my $sth=$dbh->prepare($query);
1881 $sth->execute($ordnum);
1882 my $data=$sth->fetchrow_hashref;
1889 my $dbh = C4::Context->dbh;
1890 my $bibnum=OLDnewbiblio($dbh,$biblio);
1891 # finds new (MARC bibid
1892 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$bibnum);
1893 my $record = &MARCkoha2marcBiblio($dbh,$bibnum);
1894 MARCaddbiblio($dbh,$record,$bibnum);
1901 $biblionumber = &modbiblio($biblio);
1903 Update a biblio record.
1905 C<$biblio> is a reference-to-hash whose keys are the fields in the
1906 biblio table in the Koha database. All fields must be present, not
1907 just the ones you wish to change.
1909 C<&modbiblio> updates the record defined by
1910 C<$biblio-E<gt>{biblionumber}> with the values in C<$biblio>.
1912 C<&modbiblio> returns C<$biblio-E<gt>{biblionumber}> whether it was
1919 my $dbh = C4::Context->dbh;
1920 my $biblionumber=OLDmodbiblio($dbh,$biblio);
1921 my $record = MARCkoha2marcBiblio($dbh,$biblionumber);
1922 # finds new (MARC bibid
1923 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
1924 MARCmodbiblio($dbh,$bibid,$record,0);
1925 return($biblionumber);
1930 &modsubtitle($biblionumber, $subtitle);
1932 Sets the subtitle of a book.
1934 C<$biblionumber> is the biblionumber of the book to modify.
1936 C<$subtitle> is the new subtitle.
1941 my ($bibnum, $subtitle) = @_;
1942 my $dbh = C4::Context->dbh;
1943 &OLDmodsubtitle($dbh,$bibnum,$subtitle);
1948 &modaddauthor($biblionumber, $author);
1950 Replaces all additional authors for the book with biblio number
1951 C<$biblionumber> with C<$author>. If C<$author> is the empty string,
1952 C<&modaddauthor> deletes all additional authors.
1957 my ($bibnum, $author) = @_;
1958 my $dbh = C4::Context->dbh;
1959 &OLDmodaddauthor($dbh,$bibnum,$author);
1960 } # sub modaddauthor
1964 $error = &modsubject($biblionumber, $force, @subjects);
1966 $force - a subject to force
1968 $error - Error message, or undef if successful.
1973 my ($bibnum, $force, @subject) = @_;
1974 my $dbh = C4::Context->dbh;
1975 my $error= &OLDmodsubject($dbh,$bibnum,$force, @subject);
1980 my ($biblioitem) = @_;
1981 my $dbh = C4::Context->dbh;
1982 &OLDmodbibitem($dbh,$biblioitem);
1983 my $MARCbibitem = MARCkoha2marcBiblio($dbh,$biblioitem);
1984 &MARCmodbiblio($dbh,$biblioitem->{biblionumber},$MARCbibitem,0);
1988 my ($bibitemnum,$note)=@_;
1989 my $dbh = C4::Context->dbh;
1990 &OLDmodnote($dbh,$bibitemnum,$note);
1994 my ($biblioitem) = @_;
1995 my $dbh = C4::Context->dbh;
1996 my $bibitemnum = &OLDnewbiblioitem($dbh,$biblioitem);
1997 # print STDERR "bibitemnum : $bibitemnum\n";
1998 my $MARCbiblio= MARCkoha2marcBiblio($dbh,$biblioitem->{biblionumber},$bibitemnum);
1999 # print STDERR $MARCbiblio->as_formatted();
2000 &MARCaddbiblio($dbh,$MARCbiblio,$biblioitem->{biblionumber});
2001 return($bibitemnum);
2006 my $dbh = C4::Context->dbh;
2007 &OLDnewsubject($dbh,$bibnum);
2011 my ($bibnum, $subtitle) = @_;
2012 my $dbh = C4::Context->dbh;
2013 &OLDnewsubtitle($dbh,$bibnum,$subtitle);
2017 my ($item, @barcodes) = @_;
2018 my $dbh = C4::Context->dbh;
2022 foreach my $barcode (@barcodes) {
2023 ($itemnumber,$error)=&OLDnewitems($dbh,$item,uc($barcode));
2025 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{biblionumber},$itemnumber);
2026 &MARCadditem($dbh,$MARCitem,$item->{biblionumber});
2033 my $dbh = C4::Context->dbh;
2034 &OLDmoditem($dbh,$item);
2035 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{'biblionumber'},$item->{'itemnum'});
2036 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$item->{biblionumber});
2037 &MARCmoditem($dbh,$MARCitem,$bibid,$item->{itemnum},0);
2041 my ($count,@barcodes)=@_;
2042 my $dbh = C4::Context->dbh;
2044 for (my $i=0;$i<$count;$i++){
2045 $barcodes[$i]=uc $barcodes[$i];
2046 my $query="Select * from items where barcode='$barcodes[$i]'";
2047 my $sth=$dbh->prepare($query);
2049 if (my $data=$sth->fetchrow_hashref){
2050 $error.=" Duplicate Barcode: $barcodes[$i]";
2058 my ($bibitemnum)=@_;
2059 my $dbh = C4::Context->dbh;
2060 my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
2061 my $sth=$dbh->prepare($query);
2063 my $data=$sth->fetchrow_hashref;
2065 return($data->{'count(*)'});
2070 my $dbh = C4::Context->dbh;
2071 &OLDdelitem($dbh,$itemnum);
2074 sub deletebiblioitem {
2075 my ($biblioitemnumber) = @_;
2076 my $dbh = C4::Context->dbh;
2077 &OLDdeletebiblioitem($dbh,$biblioitemnumber);
2078 } # sub deletebiblioitem
2083 my $dbh = C4::Context->dbh;
2084 &OLDdelbiblio($dbh,$biblio);
2088 my $dbh = C4::Context->dbh;
2089 my $query = "select * from itemtypes order by description";
2090 my $sth = $dbh->prepare($query);
2091 # || die "Cannot prepare $query" . $dbh->errstr;
2096 # || die "Cannot execute $query\n" . $sth->errstr;
2097 while (my $data = $sth->fetchrow_hashref) {
2098 $results[$count] = $data;
2103 return($count, @results);
2104 } # sub getitemtypes
2107 my ($biblionumber) = @_;
2108 my $dbh = C4::Context->dbh;
2109 my $query = "Select * from biblio where biblionumber = $biblionumber";
2110 my $sth = $dbh->prepare($query);
2111 # || die "Cannot prepare $query\n" . $dbh->errstr;
2116 # || die "Cannot execute $query\n" . $sth->errstr;
2117 while (my $data = $sth->fetchrow_hashref) {
2118 $results[$count] = $data;
2123 return($count, @results);
2127 my ($biblioitemnum) = @_;
2128 my $dbh = C4::Context->dbh;
2129 my $query = "Select * from biblioitems where
2130 biblioitemnumber = $biblioitemnum";
2131 my $sth = $dbh->prepare($query);
2137 while (my $data = $sth->fetchrow_hashref) {
2138 $results[$count] = $data;
2143 return($count, @results);
2144 } # sub getbiblioitem
2146 sub getbiblioitembybiblionumber {
2147 my ($biblionumber) = @_;
2148 my $dbh = C4::Context->dbh;
2149 my $query = "Select * from biblioitems where biblionumber =
2151 my $sth = $dbh->prepare($query);
2157 while (my $data = $sth->fetchrow_hashref) {
2158 $results[$count] = $data;
2163 return($count, @results);
2166 sub getitemsbybiblioitem {
2167 my ($biblioitemnum) = @_;
2168 my $dbh = C4::Context->dbh;
2169 my $query = "Select * from items, biblio where
2170 biblio.biblionumber = items.biblionumber and biblioitemnumber
2172 my $sth = $dbh->prepare($query);
2173 # || die "Cannot prepare $query\n" . $dbh->errstr;
2178 # || die "Cannot execute $query\n" . $sth->errstr;
2179 while (my $data = $sth->fetchrow_hashref) {
2180 $results[$count] = $data;
2185 return($count, @results);
2186 } # sub getitemsbybiblioitem
2190 # Subroutine to log changes to databases
2191 # Eventually, this subroutine will be used to create a log of all changes made,
2192 # with the possibility of "undo"ing some changes
2194 if ($database eq 'kohadb') {
2200 # print STDERR "KOHA: $type $section $item $original $new\n";
2201 } elsif ($database eq 'marc') {
2203 my $Record_ID=shift;
2206 my $subfield_ID=shift;
2209 # print STDERR "MARC: $type $Record_ID $tag $mark $subfield_ID $original $new\n";
2213 #------------------------------------------------
2216 #---------------------------------------
2217 # Find a biblio entry, or create a new one if it doesn't exist.
2218 # If a "subtitle" entry is in hash, add it to subtitle table
2219 sub getoraddbiblio {
2223 # FIXME - Unused argument
2224 $biblio, # hash ref to fields
2235 $dbh = C4::Context->dbh;
2237 print "<PRE>Looking for biblio </PRE>\n" if $debug;
2238 $sth=$dbh->prepare("select biblionumber
2240 where title=? and author=?
2241 and copyrightdate=? and seriestitle=?");
2243 $biblio->{title}, $biblio->{author},
2244 $biblio->{copyright}, $biblio->{seriestitle} );
2246 ($biblionumber) = $sth->fetchrow;
2247 print "<PRE>Biblio exists with number $biblionumber</PRE>\n" if $debug;
2249 # Doesn't exist. Add new one.
2250 print "<PRE>Adding biblio</PRE>\n" if $debug;
2251 ($biblionumber,$error)=&newbiblio($biblio);
2252 if ( $biblionumber ) {
2253 print "<PRE>Added with biblio number=$biblionumber</PRE>\n" if $debug;
2254 if ( $biblio->{subtitle} ) {
2255 &newsubtitle($biblionumber,$biblio->{subtitle} );
2258 print "<PRE>Couldn't add biblio: $error</PRE>\n" if $debug;
2262 return $biblionumber,$error;
2264 } # sub getoraddbiblio
2267 # converts ISO 5426 coded string to ISO 8859-1
2268 # sloppy code : should be improved in next issue
2269 my ($string,$encoding) = @_ ;
2271 # $encoding = C4::Context->preference("marcflavour") unless $encoding;
2272 if ($encoding eq "UNIMARC") {
2334 # this handles non-sorting blocks (if implementation requires this)
2335 $string = nsb_clean($_) ;
2336 } elsif ($encoding eq "USMARC") {
2389 # this handles non-sorting blocks (if implementation requires this)
2390 $string = nsb_clean($_) ;
2397 my $NSB = '\x88' ; # NSB : begin Non Sorting Block
2398 my $NSE = '\x89' ; # NSE : Non Sorting Block end
2399 # handles non sorting blocks
2403 s/[ ]{0,1}$NSE/) /gm ;
2408 END { } # module clean-up code here (global destructor)
2414 Koha Developement team <info@koha.org>
2416 Paul POULAIN paul.poulain@free.fr