4 # Revision 1.50 2003/07/02 13:57:13 tipaul
5 # fix for #512 (not sure it's enogh. to be checked by NPL)
7 # Revision 1.49 2003/06/17 11:21:13 tipaul
8 # improvments/fixes for z3950 support.
9 # * Works now even on ADD, not only on MODIFY
10 # * able to search on ISBN, author, title
12 # Revision 1.48 2003/06/16 09:22:53 rangi
13 # Just added an order clause to getitemtypes
15 # Revision 1.47 2003/05/20 16:22:44 tipaul
16 # fixing typo in Biblio.pm POD
18 # Revision 1.46 2003/05/19 13:45:18 tipaul
19 # support for subtitles, additional authors, subject.
20 # 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.
21 # Note that some OLD-DB subs are strange (dummy ?) see OLDmodsubject, OLDmodsubtitle, OLDmodaddiauthor in C4/Biblio.pm
22 # 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.
24 # Revision 1.45 2003/04/29 16:50:49 tipaul
25 # really proud of this commit :-)
26 # z3950 search and import seems to works fine.
27 # Let me explain how :
28 # * a "search z3950" button is added in the addbiblio template.
29 # * when clicked, a popup appears and z3950/search.pl is called
30 # * z3950/search.pl calls addz3950search in the DB
31 # * the z3950 daemon retrieve the records and stores them in z3950results AND in marc_breeding table.
32 # * as long as there as searches pending, the popup auto refresh every 2 seconds, and says how many searches are pending.
33 # * when the user clicks on a z3950 result => the parent popup is called with the requested biblio, and auto-filled
36 # * 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.
37 # * 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.
39 # Revision 1.44 2003/04/28 13:07:14 tipaul
40 # Those fixes solves the "internal server error" with MARC::Record 1.12.
41 # It was due to an illegal contruction in Koha : we tried to retrive subfields from <10 tags.
42 # That's not possible. MARC::Record accepted this in 0.93 version, but it was fixed after.
43 # Now, the construct/retrieving is OK !
45 # Revision 1.43 2003/04/10 13:56:02 tipaul
47 # * worked in 1.9.0, but not in 1.9.1 :
48 # - modif of a biblio didn't work
49 # - 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.
51 # * did not work before :
52 # - repeatable subfields now works correctly. Enter 2 subfields separated by | and they will be splitted during saving.
53 # - dropped the last subfield of the MARC form :-(
56 # - 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.
57 # Note the MARCdelbiblio has been rewritted to enable deletion of a biblio WITHOUT deleting items.
59 # Revision 1.42 2003/04/04 08:41:11 tipaul
60 # last commits before 1.9.1
62 # Revision 1.41 2003/04/01 12:26:43 tipaul
65 # Revision 1.40 2003/03/11 15:14:03 tipaul
68 # Revision 1.39 2003/03/07 16:35:42 tipaul
69 # * moving generic functions to Koha.pm
70 # * improvement of SearchMarc.pm
74 # Revision 1.38 2003/02/27 16:51:59 tipaul
75 # * moving prepare / execute to ? form.
78 # * road to 1.9.2 => acquisition and cataloguing merging
80 # Revision 1.37 2003/02/12 11:03:03 tipaul
81 # Support for 000 -> 010 fields.
82 # Those fields doesn't have subfields.
83 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
84 # 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.
86 # Revision 1.36 2003/02/12 11:01:01 tipaul
87 # Support for 000 -> 010 fields.
88 # Those fields doesn't have subfields.
89 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
90 # 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.
92 # Revision 1.35 2003/02/03 18:46:00 acli
93 # Minor factoring in C4/Biblio.pm, plus change to export the per-tag
94 # 'mandatory' property to a per-subfield 'tag_mandatory' template parameter,
95 # so that addbiblio.tmpl can distinguish between mandatory subfields in a
96 # mandatory tag and mandatory subfields in an optional tag
98 # Not-minor factoring in acqui.simple/addbiblio.pl to make the if-else blocks
99 # smaller, and to add some POD; need further testing for this
101 # Added function to check if a MARC subfield name is "koha-internal" (instead
102 # of checking it for 'lib' and 'tag' everywhere); temporarily added to Koha.pm
104 # Use above function in acqui.simple/additem.pl and search.marc/search.pl
106 # Revision 1.34 2003/01/28 14:50:04 tipaul
107 # fixing MARCmodbiblio API and reindenting code
109 # Revision 1.33 2003/01/23 12:22:37 tipaul
110 # adding char_decode to decode MARC21 or UNIMARC extended chars
112 # Revision 1.32 2002/12/16 15:08:50 tipaul
113 # small but important bugfix (fixes a problem in export)
115 # Revision 1.31 2002/12/13 16:22:04 tipaul
116 # 1st draft of marc export
118 # Revision 1.30 2002/12/12 21:26:35 tipaul
119 # YAB ! (Yet Another Bugfix) => related to biblio modif
120 # (some warning cleaning too)
122 # Revision 1.29 2002/12/12 16:35:00 tipaul
123 # adding authentification with Auth.pm and
124 # MAJOR BUGFIX on marc biblio modification
126 # Revision 1.28 2002/12/10 13:30:03 tipaul
127 # fugfixes from Dombes Abbey work
129 # Revision 1.27 2002/11/19 12:36:16 tipaul
131 # various bugfixes, improvments, and migration from acquisition.pm to biblio.pm
133 # Revision 1.26 2002/11/12 15:58:43 tipaul
136 # * 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)
138 # Revision 1.25 2002/10/25 10:58:26 tipaul
140 # * bugfixes and improvements
142 # Revision 1.24 2002/10/24 12:09:01 arensb
143 # Fixed "no title" warning when generating HTML documentation from POD.
145 # Revision 1.23 2002/10/16 12:43:08 arensb
146 # Added some FIXME comments.
148 # Revision 1.22 2002/10/15 13:39:17 tipaul
149 # removing Acquisition.pm
150 # deleting unused code in biblio.pm, rewriting POD and answering most FIXME comments
152 # Revision 1.21 2002/10/13 11:34:14 arensb
153 # Replaced expressions of the form "$x = $x <op> $y" with "$x <op>= $y".
154 # Thus, $x = $x+2 becomes $x += 2, and so forth.
156 # Revision 1.20 2002/10/13 08:28:32 arensb
157 # Deleted unused variables.
158 # Removed trailing whitespace.
160 # Revision 1.19 2002/10/13 05:56:10 arensb
161 # Added some FIXME comments.
163 # Revision 1.18 2002/10/11 12:34:53 arensb
164 # Replaced &requireDBI with C4::Context->dbh
166 # Revision 1.17 2002/10/10 14:48:25 tipaul
169 # Revision 1.16 2002/10/07 14:04:26 tipaul
170 # road to 1.3.1 : viewing MARC biblio
172 # Revision 1.15 2002/10/05 09:49:25 arensb
173 # Merged with arensb-context branch: use C4::Context->dbh instead of
174 # &C4Connect, and generally prefer C4::Context over C4::Database.
176 # Revision 1.14 2002/10/03 11:28:18 tipaul
177 # Extending Context.pm to add stopword management and using it in MARC-API.
178 # First benchmarks show a medium speed improvement, which is nice as this part is heavily called.
180 # Revision 1.13 2002/10/02 16:26:44 tipaul
183 # Revision 1.12.2.4 2002/10/05 07:09:31 arensb
184 # Merged in changes from main branch.
186 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
187 # Added a whole mess of FIXME comments.
189 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
190 # Added some missing semicolons.
192 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
193 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
196 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
197 # Added a whole mess of FIXME comments.
199 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
200 # Added some missing semicolons.
202 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
203 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
206 # Revision 1.12 2002/10/01 11:48:51 arensb
207 # Added some FIXME comments, mostly marking duplicate functions.
209 # Revision 1.11 2002/09/24 13:49:26 tipaul
210 # long WAS the road to 1.3.0...
211 # coming VERY SOON NOW...
212 # modifying installer and buildrelease to update the DB
214 # Revision 1.10 2002/09/22 16:50:08 arensb
215 # Added some FIXME comments.
217 # Revision 1.9 2002/09/20 12:57:46 tipaul
218 # long is the road to 1.4.0
219 # * MARCadditem and MARCmoditem now wroks
220 # * various bugfixes in MARC management
221 # !!! 1.3.0 should be released very soon now. Be careful !!!
223 # Revision 1.8 2002/09/10 13:53:52 tipaul
224 # MARC API continued...
226 # * 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)
228 # Note : it should not be hard for marcimport and marcexport to re-link fields from internal tag/subfield to "legal" tag/subfield.
230 # Revision 1.7 2002/08/14 18:12:51 tonnesen
231 # Added copyright statement to all .pl and .pm files
233 # Revision 1.6 2002/07/25 13:40:31 tipaul
234 # pod documenting the API.
236 # Revision 1.5 2002/07/24 16:11:37 tipaul
238 # Database.pm and Output.pm are almost not modified (var test...)
240 # Biblio.pm is almost completly rewritten.
242 # WHAT DOES IT ??? ==> END of Hitchcock suspens
244 # 1st, it does... nothing...
245 # 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 ...
247 # All old-API functions have been cloned. for example, the "newbiblio" sub, now has become :
248 # * a "newbiblio" sub, with the same parameters. It just call a sub named OLDnewbiblio
249 # * 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.
250 # * 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.
251 # 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 ;-)
253 # 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.
254 # Note we have decided with steve that a old-biblio <=> a MARC-Biblio.
258 # Copyright 2000-2002 Katipo Communications
260 # This file is part of Koha.
262 # Koha is free software; you can redistribute it and/or modify it under the
263 # terms of the GNU General Public License as published by the Free Software
264 # Foundation; either version 2 of the License, or (at your option) any later
267 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
268 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
269 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
271 # You should have received a copy of the GNU General Public License along with
272 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
273 # Suite 330, Boston, MA 02111-1307 USA
281 use vars qw($VERSION @ISA @EXPORT);
283 # set the version for version checking
288 # don't forget MARCxxx subs are here only for testing purposes. Should not be used
289 # as the old-style API and the NEW one are the only public functions.
292 &updateBiblio &updateBiblioItem &updateItem
293 &itemcount &newbiblio &newbiblioitem
294 &modnote &newsubject &newsubtitle
295 &modbiblio &checkitems
296 &newitems &modbibitem
297 &modsubtitle &modsubject &modaddauthor &moditem &countitems
298 &delitem &deletebiblioitem &delbiblio
299 &getitemtypes &getbiblio
300 &getbiblioitembybiblionumber
301 &getbiblioitem &getitemsbybiblioitem
303 &newcompletebiblioitem
305 &MARCfind_oldbiblionumber_from_MARCbibid
306 &MARCfind_MARCbibid_from_oldbiblionumber
307 &MARCfind_marc_from_kohafield
311 &NEWnewbiblio &NEWnewitem
312 &NEWmodbiblio &NEWmoditem
314 &MARCaddbiblio &MARCadditem
315 &MARCmodsubfield &MARCaddsubfield
316 &MARCmodbiblio &MARCmoditem
317 &MARCkoha2marcBiblio &MARCmarc2koha
318 &MARCkoha2marcItem &MARChtml2marc
319 &MARCgetbiblio &MARCgetitem
320 &MARCaddword &MARCdelword
326 # MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC
329 # all the following subs takes a MARC::Record as parameter and manage
330 # the MARC-DB. They are called by the 1.0/1.2 xxx subs, and by the
331 # NEWxxx subs (xxx deals with old-DB parameters, the NEWxxx deals with MARC-DB parameter)
335 C4::Biblio - acquisition, catalog management functions
339 move from 1.2 to 1.4 version :
340 1.2 and previous version uses a specific API to manage biblios. This API uses old-DB style parameters.
341 In the 1.4 version, we want to do 2 differents things :
342 - keep populating the old-DB, that has a LOT less datas than MARC
343 - populate the MARC-DB
344 To populate the DBs we have 2 differents sources :
345 - the standard acquisition system (through book sellers), that does'nt use MARC data
346 - the MARC acquisition system, that uses MARC data.
348 Thus, we have 2 differents cases :
349 - 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
350 - 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
352 That's why we need 4 subs :
353 all I<subs beginning by MARC> manage only MARC tables. They manage MARC-DB with MARC::Record parameters
354 all I<subs beginning by OLD> manage only OLD-DB tables. They manage old-DB with old-DB parameters
355 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
356 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.
358 - NEW and old-style API should be used in koha to manage biblio
359 - MARCsubs are divided in 2 parts :
360 * some of them manage MARC parameters. They are heavily used in koha.
361 * some of them manage MARC biblio : they are mostly used by NEW and old-style subs.
362 - OLD are used internally only
364 all subs requires/use $dbh as 1st parameter.
366 I<NEWxxx related subs>
368 all subs requires/use $dbh as 1st parameter.
369 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
371 I<OLDxxx related subs>
373 all subs requires/use $dbh as 1st parameter.
374 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
376 They all are the exact copy of 1.0/1.2 version of the sub without the OLD.
377 The OLDxxx is called by the original xxx sub.
378 the 1.4 xxx sub also builds MARC::Record an calls the MARCxxx
380 WARNING : there is 1 difference between initialxxx and OLDxxx :
381 the db header $dbh is always passed as parameter to avoid over-DB connexion
387 =item @tagslib = &MARCgettagslib($dbh,1|0);
389 last param is 1 for liblibrarian and 0 for libopac
390 returns a hash with tag/subfield meaning
391 =item ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,$kohafield);
393 finds MARC tag and subfield for a given kohafield
394 kohafield is "table.field" where table= biblio|biblioitems|items, and field a field of the previous table
396 =item $biblionumber = &MARCfind_oldbiblionumber_from_MARCbibid($dbh,$MARCbibi);
398 finds a old-db biblio number for a given MARCbibid number
400 =item $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
402 finds a MARC bibid from a old-db biblionumber
404 =item $MARCRecord = &MARCkoha2marcBiblio($dbh,$biblionumber,biblioitemnumber);
406 MARCkoha2marcBiblio is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB biblio/biblioitem
408 =item $MARCRecord = &MARCkoha2marcItem($dbh,$biblionumber,itemnumber);
410 MARCkoha2marcItem is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB item
412 =item $MARCRecord = &MARCkoha2marcSubtitle($dbh,$biblionumber,$subtitle);
414 MARCkoha2marcSubtitle is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB subtitle
416 =item $olddb = &MARCmarc2koha($dbh,$MARCRecord);
418 builds a hash with old-db datas from a MARC::Record
420 =item &MARCaddbiblio($dbh,$MARC::Record,$biblionumber);
422 creates a biblio (in the MARC tables only). $biblionumber is the old-db biblionumber of the biblio
424 =item &MARCaddsubfield($dbh,$bibid,$tagid,$indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
426 adds a subfield in a biblio (in the MARC tables only).
428 =item $MARCRecord = &MARCgetbiblio($dbh,$bibid);
430 Returns a MARC::Record for the biblio $bibid.
432 =item &MARCmodbiblio($dbh,$bibid,$record,$delete);
434 MARCmodbiblio changes a biblio for a biblio,MARC::Record passed as parameter
435 It 1st delete the biblio, then recreates it.
436 WARNING : the $delete parameter is not used anymore (too much unsolvable cases).
437 =item ($subfieldid,$subfieldvalue) = &MARCmodsubfield($dbh,$subfieldid,$subfieldvalue);
439 MARCmodsubfield changes the value of a given subfield
441 =item $subfieldid = &MARCfindsubfield($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue);
443 MARCfindsubfield returns a subfield number given a bibid/tag/subfieldvalue values.
444 Returns -1 if more than 1 answer
446 =item $subfieldid = &MARCfindsubfieldid($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
448 MARCfindsubfieldid find a subfieldid for a bibid/tag/tagorder/subfield/subfieldorder
450 =item &MARCdelsubfield($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
452 MARCdelsubfield delete a subfield for a bibid/tag/tagorder/subfield/subfieldorder
454 =item &MARCdelbiblio($dbh,$bibid);
456 MARCdelbiblio delete biblio $bibid
458 =item &MARCkoha2marcOnefield
460 used by MARCkoha2marc and should not be useful elsewhere
462 =item &MARCmarc2kohaOnefield
464 used by MARCmarc2koha and should not be useful elsewhere
468 used to manage MARC_word table and should not be useful elsewhere
472 used to manage MARC_word table and should not be useful elsewhere
477 my ($dbh,$forlibrarian)= @_;
479 my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
480 $sth=$dbh->prepare("select tagfield,$libfield as lib,mandatory from marc_tag_structure order by tagfield");
482 my ($lib,$tag,$res,$tab,$mandatory,$repeatable);
483 while ( ($tag,$lib,$mandatory) = $sth->fetchrow) {
484 $res->{$tag}->{lib}=$lib;
485 $res->{$tab}->{tab}=""; # XXX
486 $res->{$tag}->{mandatory}=$mandatory;
489 $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");
493 my $authorised_value;
494 my $thesaurus_category;
497 while ( ($tag, $subfield, $lib, $tab, $mandatory, $repeatable,$authorised_value,$thesaurus_category,$value_builder,$kohafield) = $sth->fetchrow) {
498 $res->{$tag}->{$subfield}->{lib}=$lib;
499 $res->{$tag}->{$subfield}->{tab}=$tab;
500 $res->{$tag}->{$subfield}->{mandatory}=$mandatory;
501 $res->{$tag}->{$subfield}->{repeatable}=$repeatable;
502 $res->{$tag}->{$subfield}->{authorised_value}=$authorised_value;
503 $res->{$tag}->{$subfield}->{thesaurus_category}=$thesaurus_category;
504 $res->{$tag}->{$subfield}->{value_builder}=$value_builder;
505 $res->{$tag}->{$subfield}->{kohafield}=$kohafield;
510 sub MARCfind_marc_from_kohafield {
511 my ($dbh,$kohafield) = @_;
512 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
513 $sth->execute($kohafield);
514 my ($tagfield,$tagsubfield) = $sth->fetchrow;
515 return ($tagfield,$tagsubfield);
518 sub MARCfind_oldbiblionumber_from_MARCbibid {
519 my ($dbh,$MARCbibid) = @_;
520 my $sth=$dbh->prepare("select biblionumber from marc_biblio where bibid=?");
521 $sth->execute($MARCbibid);
522 my ($biblionumber) = $sth->fetchrow;
523 return $biblionumber;
526 sub MARCfind_MARCbibid_from_oldbiblionumber {
527 my ($dbh,$oldbiblionumber) = @_;
528 my $sth=$dbh->prepare("select bibid from marc_biblio where biblionumber=?");
529 $sth->execute($oldbiblionumber);
530 my ($bibid) = $sth->fetchrow;
535 # pass the MARC::Record to this function, and it will create the records in the marc tables
536 my ($dbh,$record,$biblionumber,$bibid) = @_;
537 my @fields=$record->fields();
539 # adding main table, and retrieving bibid
540 # if bibid is sent, then it's not a true add, it's only a re-add, after a delete (ie, a mod)
541 # if bibid empty => true add, find a new bibid number
543 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
544 my $sth=$dbh->prepare("insert into marc_biblio (datecreated,biblionumber) values (now(),?)");
545 $sth->execute($biblionumber);
546 $sth=$dbh->prepare("select max(bibid) from marc_biblio");
548 ($bibid)=$sth->fetchrow;
552 # now, add subfields...
553 foreach my $field (@fields) {
555 if ($field->tag() <10) {
556 &MARCaddsubfield($dbh,$bibid,
565 my @subfields=$field->subfields();
566 foreach my $subfieldcount (0..$#subfields) {
567 &MARCaddsubfield($dbh,$bibid,
569 $field->indicator(1).$field->indicator(2),
571 $subfields[$subfieldcount][0],
573 $subfields[$subfieldcount][1]
578 $dbh->do("unlock tables");
583 # pass the MARC::Record to this function, and it will create the records in the marc tables
584 my ($dbh,$record,$biblionumber) = @_;
585 # warn "adding : ".$record->as_formatted();
586 # search for MARC biblionumber
587 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
588 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
589 my @fields=$record->fields();
590 my $sth = $dbh->prepare("select max(tagorder) from marc_subfield_table where bibid=?");
591 $sth->execute($bibid);
592 my ($fieldcount) = $sth->fetchrow;
593 # now, add subfields...
594 foreach my $field (@fields) {
595 my @subfields=$field->subfields();
597 foreach my $subfieldcount (0..$#subfields) {
598 &MARCaddsubfield($dbh,$bibid,
600 $field->indicator(1).$field->indicator(2),
602 $subfields[$subfieldcount][0],
604 $subfields[$subfieldcount][1]
608 $dbh->do("unlock tables");
612 sub MARCaddsubfield {
613 # Add a new subfield to a tag into the DB.
614 my ($dbh,$bibid,$tagid,$tag_indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalues) = @_;
615 # if not value, end of job, we do nothing
616 if (length($subfieldvalues) ==0) {
619 if (not($subfieldcode)) {
622 my @subfieldvalues = split /\|/,$subfieldvalues;
623 foreach my $subfieldvalue (@subfieldvalues) {
624 if (length($subfieldvalue)>255) {
625 # $dbh->do("lock tables marc_blob_subfield WRITE, marc_subfield_table WRITE");
626 my $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
627 $sth->execute($subfieldvalue);
628 $sth=$dbh->prepare("select max(blobidlink)from marc_blob_subfield");
630 my ($res)=$sth->fetchrow;
631 $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,valuebloblink) values (?,?,?,?,?,?,?)");
632 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$res);
634 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";
636 # $dbh->do("unlock tables");
638 my $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?,?)");
639 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue);
641 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";
644 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
649 # Returns MARC::Record of the biblio passed in parameter.
651 my $record = MARC::Record->new();
652 #---- TODO : the leader is missing
653 $record->leader(' ');
654 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
655 from marc_subfield_table
656 where bibid=? order by tag,tagorder,subfieldcode
658 my $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
659 $sth->execute($bibid);
663 my $field; # for >=10 tags
664 my $prevvalue; # for <10 tags
665 while (my $row=$sth->fetchrow_hashref) {
666 if ($row->{'valuebloblink'}) { #---- search blob if there is one
667 $sth2->execute($row->{'valuebloblink'});
668 my $row2=$sth2->fetchrow_hashref;
670 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
672 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
675 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
677 $record->add_fields($field) unless $prevtag eq "XXX";
680 $prevtagorder=$row->{tagorder};
681 $prevtag = $row->{tag};
682 $previndicator=$row->{tag_indicator};
683 if ($row->{tag}<10) {
684 $prevvalue = $row->{subfieldvalue};
686 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.' ',0,1), substr($row->{tag_indicator}.' ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
689 if ($row->{tag} <10) {
690 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
692 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
694 $prevtag= $row->{tag};
695 $previndicator=$row->{tag_indicator};
698 # the last has not been included inside the loop... do it now !
700 $record->add_fields($prevtag,$prevvalue);
702 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
703 $record->add_fields($field);
708 # Returns MARC::Record of the biblio passed in parameter.
709 my ($dbh,$bibid,$itemnumber)=@_;
710 my $record = MARC::Record->new();
711 # search MARC tagorder
712 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=?");
713 $sth2->execute($bibid,$itemnumber);
714 my ($tagorder) = $sth2->fetchrow_array();
715 #---- TODO : the leader is missing
716 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
717 from marc_subfield_table
718 where bibid=? and tagorder=? order by subfieldcode,subfieldorder
720 $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
721 $sth->execute($bibid,$tagorder);
722 while (my $row=$sth->fetchrow_hashref) {
723 if ($row->{'valuebloblink'}) { #---- search blob if there is one
724 $sth2->execute($row->{'valuebloblink'});
725 my $row2=$sth2->fetchrow_hashref;
727 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
729 if ($record->field($row->{'tag'})) {
731 #--- this test must stay as this, because of strange behaviour of mySQL/Perl DBI with char var containing a number...
732 #--- sometimes, eliminates 0 at beginning, sometimes no ;-\\\
733 if (length($row->{'tag'}) <3) {
734 $row->{'tag'} = "0".$row->{'tag'};
736 $field =$record->field($row->{'tag'});
738 my $x = $field->add_subfields($row->{'subfieldcode'},$row->{'subfieldvalue'});
739 $record->delete_field($field);
740 $record->add_fields($field);
743 if (length($row->{'tag'}) < 3) {
744 $row->{'tag'} = "0".$row->{'tag'};
746 my $temp = MARC::Field->new($row->{'tag'}," "," ", $row->{'subfieldcode'} => $row->{'subfieldvalue'});
747 $record->add_fields($temp);
755 my ($dbh,$bibid,$record,$delete)=@_;
756 my $oldrecord=&MARCgetbiblio($dbh,$bibid);
757 if ($oldrecord eq $record) {
760 # 1st delete the biblio,
762 &MARCdelbiblio($dbh,$bibid,1);
763 my $biblionumber = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
764 &MARCaddbiblio($dbh,$record,$biblionumber,$bibid);
768 my ($dbh,$bibid,$keep_items) = @_;
769 # if the keep_item is set to 1, then all items are preserved.
770 # This flag is set when the delbiblio is called by modbiblio
771 # due to a too complex structure of MARC (repeatable fields and subfields),
772 # the best solution for a modif is to delete / recreate the record.
773 if ($keep_items eq 1) {
774 #search item field code
775 my $sth = $dbh->prepare("select tagfield from marc_subfield_structure where kohafield like 'items.%'");
777 my $itemtag = $sth->fetchrow_hashref->{tagfield};
778 $dbh->do("delete from marc_subfield_table where bibid=$bibid and tag<>$itemtag");
779 $dbh->do("delete from marc_word where bibid=$bibid and tag<>$itemtag");
781 $dbh->do("delete from marc_biblio where bibid=$bibid");
782 $dbh->do("delete from marc_subfield_table where bibid=$bibid");
783 $dbh->do("delete from marc_word where bibid=$bibid");
787 my ($dbh,$record,$bibid,$itemnumber,$delete)=@_;
788 my $oldrecord=&MARCgetitem($dbh,$bibid,$itemnumber);
789 # if nothing to change, don't waste time...
790 if ($oldrecord eq $record) {
794 # otherwise, skip through each subfield...
795 my @fields = $record->fields();
796 # search old MARC item
797 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=?");
798 $sth2->execute($bibid,$itemnumber);
799 my ($tagorder) = $sth2->fetchrow_array();
800 foreach my $field (@fields) {
801 my $oldfield = $oldrecord->field($field->tag());
802 my @subfields=$field->subfields();
804 foreach my $subfield (@subfields) {
806 # warn "compare : $oldfield".$oldfield->subfield(@$subfield[0]);
807 if ($oldfield eq 0 or (length($oldfield->subfield(@$subfield[0])) ==0) ) {
808 # just adding datas...
809 # warn "addfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
810 # warn "NEW subfield : $bibid,".$field->tag().",".$tagorder.",".@$subfield[0].",".$subfieldorder.",".@$subfield[1].")";
811 &MARCaddsubfield($dbh,$bibid,$field->tag(),$field->indicator(1).$field->indicator(2),
812 $tagorder,@$subfield[0],$subfieldorder,@$subfield[1]);
814 # warn "modfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
815 # modify he subfield if it's a different string
816 if ($oldfield->subfield(@$subfield[0]) ne @$subfield[1] ) {
817 my $subfieldid=&MARCfindsubfieldid($dbh,$bibid,$field->tag(),$tagorder,@$subfield[0],$subfieldorder);
818 # warn "changing : $subfieldid, $bibid,".$field->tag(),",$tagorder,@$subfield[0],@$subfield[1],$subfieldorder";
819 &MARCmodsubfield($dbh,$subfieldid,@$subfield[1]);
827 sub MARCmodsubfield {
828 # Subroutine changes a subfield value given a subfieldid.
829 my ($dbh, $subfieldid, $subfieldvalue )=@_;
830 $dbh->do("lock tables marc_blob_subfield WRITE,marc_subfield_table WRITE");
831 my $sth1=$dbh->prepare("select valuebloblink from marc_subfield_table where subfieldid=?");
832 $sth1->execute($subfieldid);
833 my ($oldvaluebloblink)=$sth1->fetchrow;
836 # if too long, use a bloblink
837 if (length($subfieldvalue)>255 ) {
838 # if already a bloblink, update it, otherwise, insert a new one.
839 if ($oldvaluebloblink) {
840 $sth=$dbh->prepare("update marc_blob_subfield set subfieldvalue=? where blobidlink=?");
841 $sth->execute($subfieldvalue,$oldvaluebloblink);
843 $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
844 $sth->execute($subfieldvalue);
845 $sth=$dbh->prepare("select max(blobidlink) from marc_blob_subfield");
847 my ($res)=$sth->fetchrow;
848 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=null, valuebloblink=$res where subfieldid=?");
849 $sth->execute($subfieldid);
852 # note this can leave orphan bloblink. Not a big problem, but we should build somewhere a orphan deleting script...
853 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=?,valuebloblink=null where subfieldid=?");
854 $sth->execute($subfieldvalue, $subfieldid);
856 $dbh->do("unlock tables");
858 $sth=$dbh->prepare("select bibid,tag,tagorder,subfieldcode,subfieldid,subfieldorder from marc_subfield_table where subfieldid=?");
859 $sth->execute($subfieldid);
860 my ($bibid,$tagid,$tagorder,$subfieldcode,$x,$subfieldorder) = $sth->fetchrow;
862 &MARCdelword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder);
863 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
864 return($subfieldid, $subfieldvalue);
867 sub MARCfindsubfield {
868 my ($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue) = @_;
872 my $query="select subfieldid from marc_subfield_table where bibid=? and tag=? and subfieldcode=?";
873 if ($subfieldvalue) {
874 $query .= " and subfieldvalue=".$dbh->quote($subfieldvalue);
876 if ($subfieldorder<1) {
879 $query .= " and subfieldorder=$subfieldorder";
881 my $sti=$dbh->prepare($query);
882 $sti->execute($bibid,$tag, $subfieldcode);
883 while (($subfieldid) = $sti->fetchrow) {
885 $lastsubfieldid=$subfieldid;
887 if ($resultcounter>1) {
888 # Error condition. Values given did not resolve into a unique record. Don't know what to edit
889 # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
892 return $lastsubfieldid;
896 sub MARCfindsubfieldid {
897 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
898 my $sth=$dbh->prepare("select subfieldid from marc_subfield_table
899 where bibid=? and tag=? and tagorder=?
900 and subfieldcode=? and subfieldorder=?");
901 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
902 my ($res) = $sth->fetchrow;
904 $sth=$dbh->prepare("select subfieldid from marc_subfield_table
905 where bibid=? and tag=? and tagorder=?
906 and subfieldcode=?");
907 $sth->execute($bibid,$tag,$tagorder,$subfield);
908 ($res) = $sth->fetchrow;
913 sub MARCdelsubfield {
914 # delete a subfield for $bibid / tag / tagorder / subfield / subfieldorder
915 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
916 $dbh->do("delete from marc_subfield_table where bibid='$bibid' and
917 tag='$tag' and tagorder='$tagorder'
918 and subfieldcode='$subfield' and subfieldorder='$subfieldorder
922 sub MARCkoha2marcBiblio {
923 # this function builds partial MARC::Record from the old koha-DB fields
924 my ($dbh,$biblionumber,$biblioitemnumber) = @_;
925 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
926 my $record = MARC::Record->new();
927 #--- if bibid, then retrieve old-style koha data
928 if ($biblionumber>0) {
929 my $sth2=$dbh->prepare("select biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp
930 from biblio where biblionumber=?");
931 $sth2->execute($biblionumber);
932 my $row=$sth2->fetchrow_hashref;
934 foreach $code (keys %$row) {
936 &MARCkoha2marcOnefield($sth,$record,"biblio.".$code,$row->{$code});
940 #--- if biblioitem, then retrieve old-style koha data
941 if ($biblioitemnumber>0) {
942 my $sth2=$dbh->prepare(" SELECT biblioitemnumber,biblionumber,volume,number,classification,
943 itemtype,url,isbn,issn,dewey,subclass,publicationyear,publishercode,
944 volumedate,volumeddesc,timestamp,illus,pages,notes,size,place
946 WHERE biblionumber=? and biblioitemnumber=?
948 $sth2->execute($biblionumber,$biblioitemnumber);
949 my $row=$sth2->fetchrow_hashref;
951 foreach $code (keys %$row) {
953 &MARCkoha2marcOnefield($sth,$record,"biblioitems.".$code,$row->{$code});
957 # other fields => additional authors, subjects, subtitles
958 my $sth2=$dbh->prepare(" SELECT author FROM additionalauthors WHERE biblionumber=?");
959 $sth2->execute($biblionumber);
960 while (my $row=$sth2->fetchrow_hashref) {
961 &MARCkoha2marcOnefield($sth,$record,"additionalauthors.author",$row->{'author'});
963 my $sth2=$dbh->prepare(" SELECT subject FROM bibliosubject WHERE biblionumber=?");
964 $sth2->execute($biblionumber);
965 while (my $row=$sth2->fetchrow_hashref) {
966 &MARCkoha2marcOnefield($sth,$record,"bibliosubject.subject",$row->{'subject'});
968 my $sth2=$dbh->prepare(" SELECT subtitle FROM bibliosubtitle WHERE biblionumber=?");
969 $sth2->execute($biblionumber);
970 while (my $row=$sth2->fetchrow_hashref) {
971 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.title",$row->{'subtitle'});
976 sub MARCkoha2marcItem {
977 # this function builds partial MARC::Record from the old koha-DB fields
978 my ($dbh,$biblionumber,$itemnumber) = @_;
979 # my $dbh=&C4Connect;
980 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
981 my $record = MARC::Record->new();
982 #--- if item, then retrieve old-style koha data
984 # print STDERR "prepare $biblionumber,$itemnumber\n";
985 my $sth2=$dbh->prepare("SELECT itemnumber,biblionumber,multivolumepart,biblioitemnumber,barcode,dateaccessioned,
986 booksellerid,homebranch,price,replacementprice,replacementpricedate,datelastborrowed,
987 datelastseen,multivolume,stack,notforloan,itemlost,wthdrawn,bulk,issues,renewals,
988 reserves,restricted,binding,itemnotes,holdingbranch,timestamp
990 WHERE itemnumber=?");
991 $sth2->execute($itemnumber);
992 my $row=$sth2->fetchrow_hashref;
994 foreach $code (keys %$row) {
996 &MARCkoha2marcOnefield($sth,$record,"items.".$code,$row->{$code});
1003 sub MARCkoha2marcSubtitle {
1004 # this function builds partial MARC::Record from the old koha-DB fields
1005 my ($dbh,$bibnum,$subtitle) = @_;
1006 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1007 my $record = MARC::Record->new();
1008 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.subtitle",$subtitle);
1012 sub MARCkoha2marcOnefield {
1013 my ($sth,$record,$kohafieldname,$value)=@_;
1016 $sth->execute($kohafieldname);
1017 if (($tagfield,$tagsubfield)=$sth->fetchrow) {
1018 if ($record->field($tagfield)) {
1019 my $tag =$record->field($tagfield);
1021 $tag->add_subfields($tagsubfield,$value);
1022 $record->delete_field($tag);
1023 $record->add_fields($tag);
1026 $record->add_fields($tagfield," "," ",$tagsubfield => $value);
1033 my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
1035 my $record = MARC::Record->new();
1036 # my %subfieldlist=();
1037 my $prevvalue; # if tag <10
1038 my $field; # if tag >=10
1039 for (my $i=0; $i< @$rtags; $i++) {
1040 # rebuild MARC::Record
1041 if (@$rtags[$i] ne $prevtag) {
1042 if ($prevtag < 10) {
1044 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
1048 $record->add_fields($field);
1051 $indicators{@$rtags[$i]}.=' ';
1052 if (@$rtags[$i] <10) {
1053 $prevvalue= @$rvalues[$i];
1055 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
1057 $prevtag = @$rtags[$i];
1059 if (@$rtags[$i] <10) {
1060 $prevvalue=@$rvalues[$i];
1062 if (@$rvalues[$i]) {
1063 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
1066 $prevtag= @$rtags[$i];
1069 # the last has not been included inside the loop... do it now !
1070 $record->add_fields($field);
1071 # warn $record->as_formatted;
1076 my ($dbh,$record) = @_;
1077 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1079 my $sth2=$dbh->prepare("SHOW COLUMNS from biblio");
1082 # print STDERR $record->as_formatted;
1083 while (($field)=$sth2->fetchrow) {
1084 $result=&MARCmarc2kohaOneField($sth,"biblio",$field,$record,$result);
1086 $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
1088 while (($field)=$sth2->fetchrow) {
1089 $result=&MARCmarc2kohaOneField($sth,"biblioitems",$field,$record,$result);
1091 $sth2=$dbh->prepare("SHOW COLUMNS from items");
1093 while (($field)=$sth2->fetchrow) {
1094 $result = &MARCmarc2kohaOneField($sth,"items",$field,$record,$result);
1096 # additional authors : specific
1097 $result = &MARCmarc2kohaOneField($sth,"additionalauthors","additionalauthors",$record,$result);
1101 sub MARCmarc2kohaOneField {
1102 # FIXME ? if a field has a repeatable subfield that is used in old-db, only the 1st will be retrieved...
1103 my ($sth,$kohatable,$kohafield,$record,$result)= @_;
1104 # warn "kohatable / $kohafield / $result / ";
1108 $sth->execute($kohatable.".".$kohafield);
1109 ($tagfield,$subfield) = $sth->fetchrow;
1110 foreach my $field ($record->field($tagfield)) {
1111 if ($field->subfield($subfield)) {
1112 if ($result->{$kohafield}) {
1113 $result->{$kohafield} .= " | ".$field->subfield($subfield);
1115 $result->{$kohafield}=$field->subfield($subfield);
1123 # split a subfield string and adds it into the word table.
1125 my ($dbh,$bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$sentence) =@_;
1126 $sentence =~ s/(\.|\?|\:|\!|\'|,|\-)/ /g;
1127 my @words = split / /,$sentence;
1128 my $stopwords= C4::Context->stopwords;
1129 my $sth=$dbh->prepare("insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word)
1130 values (?,?,?,?,?,?,soundex(?))");
1131 foreach my $word (@words) {
1132 # we record only words longer than 2 car and not in stopwords hash
1133 if (length($word)>1 and !($stopwords->{uc($word)})) {
1134 $sth->execute($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,$word);
1136 warn "ERROR ==> insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word) values ($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,soundex($word))\n";
1143 # delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
1144 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
1145 my $sth=$dbh->prepare("delete from marc_word where bibid=? and tag=? and tagorder=? and subfieldid=? and subfieldorder=?");
1146 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
1151 # NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
1154 # all the following subs are useful to manage MARC-DB with complete MARC records.
1155 # it's used with marcimport, and marc management tools
1159 =item ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbibilio($dbh,$MARCRecord,$oldbiblio,$oldbiblioitem);
1161 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
1162 are builded from the MARC::Record. If they are passed, they are used.
1164 =item NEWnewitem($dbh, $record,$bibid);
1166 adds an item in the db.
1171 my ($dbh, $record, $oldbiblio, $oldbiblioitem) = @_;
1172 # note $oldbiblio and $oldbiblioitem are not mandatory.
1173 # if not present, they will be builded from $record with MARCmarc2koha function
1174 if (($oldbiblio) and not($oldbiblioitem)) {
1175 print STDERR "NEWnewbiblio : missing parameter\n";
1176 print "NEWnewbiblio : missing parameter : contact koha development team\n";
1182 $oldbibnum = OLDnewbiblio($dbh,$oldbiblio);
1183 $oldbiblioitem->{'biblionumber'} = $oldbibnum;
1184 $oldbibitemnum = OLDnewbiblioitem($dbh,$oldbiblioitem);
1186 my $olddata = MARCmarc2koha($dbh,$record);
1187 $oldbibnum = OLDnewbiblio($dbh,$olddata);
1188 $olddata->{'biblionumber'} = $oldbibnum;
1189 $oldbibitemnum = OLDnewbiblioitem($dbh,$olddata);
1191 # search subtiles, addiauthors and subjects
1192 my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author");
1193 my @addiauthfields = $record->field($tagfield);
1194 foreach my $addiauthfield (@addiauthfields) {
1195 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1196 foreach my $subfieldcount (0..$#addiauthsubfields) {
1197 OLDmodaddauthor($dbh,$oldbibnum,$addiauthsubfields[$subfieldcount]);
1200 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle");
1201 my @subtitlefields = $record->field($tagfield);
1202 foreach my $subtitlefield (@subtitlefields) {
1203 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1204 foreach my $subfieldcount (0..$#subtitlesubfields) {
1205 OLDnewsubtitle($dbh,$oldbibnum,$subtitlesubfields[$subfieldcount]);
1208 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject");
1209 my @subj = $record->field($tagfield);
1210 foreach my $subject (@subj) {
1211 my @subjsubfield = $subject->subfield($tagsubfield);
1213 foreach my $subfieldcount (0..$#subjsubfield) {
1214 push @subjects,$subjsubfield[$subfieldcount];
1216 OLDmodsubject($dbh,$oldbibnum,1,@subjects);
1218 # we must add bibnum and bibitemnum in MARC::Record...
1219 # we build the new field with biblionumber and biblioitemnumber
1220 # we drop the original field
1221 # we add the new builded field.
1222 # NOTE : Works only if the field is ONLY for biblionumber and biblioitemnumber
1223 # (steve and paul : thinks 090 is a good choice)
1224 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1225 $sth->execute("biblio.biblionumber");
1226 (my $tagfield1, my $tagsubfield1) = $sth->fetchrow;
1227 $sth->execute("biblioitems.biblioitemnumber");
1228 (my $tagfield2, my $tagsubfield2) = $sth->fetchrow;
1229 if ($tagfield1 != $tagfield2) {
1230 warn "Error in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1231 print "Content-Type: text/html\n\nError in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1234 my $newfield = MARC::Field->new( $tagfield1,'','',
1235 "$tagsubfield1" => $oldbibnum,
1236 "$tagsubfield2" => $oldbibitemnum);
1237 # drop old field and create new one...
1238 my $old_field = $record->field($tagfield1);
1239 $record->delete_field($old_field);
1240 $record->add_fields($newfield);
1241 my $bibid = MARCaddbiblio($dbh,$record,$oldbibnum);
1242 return ($bibid,$oldbibnum,$oldbibitemnum );
1246 my ($dbh,$record,$bibid) =@_;
1247 &MARCmodbiblio($dbh,$bibid,$record,0);
1248 my $oldbiblio = MARCmarc2koha($dbh,$record);
1249 my $oldbiblionumber = OLDmodbiblio($dbh,$oldbiblio);
1250 OLDmodbibitem($dbh,$oldbiblio);
1251 # now, modify addi authors, subject, addititles.
1252 my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author");
1253 my @addiauthfields = $record->field($tagfield);
1254 foreach my $addiauthfield (@addiauthfields) {
1255 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1256 foreach my $subfieldcount (0..$#addiauthsubfields) {
1257 OLDmodaddauthor($dbh,$oldbiblionumber,$addiauthsubfields[$subfieldcount]);
1260 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle");
1261 my @subtitlefields = $record->field($tagfield);
1262 foreach my $subtitlefield (@subtitlefields) {
1263 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1264 foreach my $subfieldcount (0..$#subtitlesubfields) {
1265 OLDmodsubtitle($dbh,$oldbiblionumber,$subtitlesubfields[$subfieldcount]);
1268 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject");
1269 my @subj = $record->field($tagfield);
1270 foreach my $subject (@subj) {
1271 my @subjsubfield = $subject->subfield($tagsubfield);
1273 foreach my $subfieldcount (0..$#subjsubfield) {
1274 push @subjects,$subjsubfield[$subfieldcount];
1276 OLDmodsubject($dbh,$oldbiblionumber,1,@subjects);
1283 my ($dbh, $record,$bibid) = @_;
1284 # add item in old-DB
1285 my $item = &MARCmarc2koha($dbh,$record);
1286 # needs old biblionumber and biblioitemnumber
1287 $item->{'biblionumber'} = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
1288 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
1289 $sth->execute($item->{'biblionumber'});
1290 ($item->{'biblioitemnumber'}) = $sth->fetchrow;
1291 my ($itemnumber,$error) = &OLDnewitems($dbh,$item,$item->{barcode});
1292 # add itemnumber to MARC::Record before adding the item.
1293 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1294 &MARCkoha2marcOnefield($sth,$record,"items.itemnumber",$itemnumber);
1296 my $bib = &MARCadditem($dbh,$record,$item->{'biblionumber'});
1300 my ($dbh,$record,$bibid,$itemnumber,$delete) = @_;
1301 &MARCmoditem($dbh,$record,$bibid,$itemnumber,$delete);
1302 my $olditem = MARCmarc2koha($dbh,$record);
1303 OLDmoditem($dbh,$olditem);
1308 # OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
1312 =item $biblionumber = OLDnewbiblio($dbh,$biblio);
1314 adds a record in biblio table. Datas are in the hash $biblio.
1316 =item $biblionumber = OLDmodbiblio($dbh,$biblio);
1318 modify a record in biblio table. Datas are in the hash $biblio.
1320 =item OLDmodsubtitle($dbh,$bibnum,$subtitle);
1322 modify subtitles in bibliosubtitle table.
1324 =item OLDmodaddauthor($dbh,$bibnum,$author);
1326 adds or modify additional authors
1327 NOTE : Strange sub : seems to delete MANY and add only ONE author... maybe buggy ?
1329 =item $errors = OLDmodsubject($dbh,$bibnum, $force, @subject);
1331 modify/adds subjects
1333 =item OLDmodbibitem($dbh, $biblioitem);
1337 =item OLDmodnote($dbh,$bibitemnum,$note
1339 modify a note for a biblioitem
1341 =item OLDnewbiblioitem($dbh,$biblioitem);
1343 adds a biblioitem ($biblioitem is a hash with the values)
1345 =item OLDnewsubject($dbh,$bibnum);
1349 =item OLDnewsubtitle($dbh,$bibnum,$subtitle);
1351 create a new subtitle
1353 =item ($itemnumber,$errors)= OLDnewitems($dbh,$item,$barcode);
1355 create a item. $item is a hash and $barcode the barcode.
1357 =item OLDmoditem($dbh,$item);
1361 =item OLDdelitem($dbh,$itemnum);
1365 =item OLDdeletebiblioitem($dbh,$biblioitemnumber);
1367 deletes a biblioitem
1368 NOTE : not standard sub name. Should be OLDdelbiblioitem()
1370 =item OLDdelbiblio($dbh,$biblio);
1377 my ($dbh,$biblio) = @_;
1378 # my $dbh = &C4Connect;
1379 my $query = "Select max(biblionumber) from biblio";
1380 my $sth = $dbh->prepare($query);
1382 my $data = $sth->fetchrow_arrayref;
1383 my $bibnum = $$data[0] + 1;
1386 if ($biblio->{'seriestitle'}) { $series = 1 };
1388 $query = "insert into biblio set biblionumber = ?, title = ?, author = ?, copyrightdate = ?,
1389 serial = ?, seriestitle = ?, notes = ?, abstract = ?";
1390 $sth = $dbh->prepare($query);
1391 $sth->execute($bibnum,$biblio->{'title'},$biblio->{'author'},$biblio->{'copyright'},$series,$biblio->{'seriestitle'},$biblio->{'notes'},$biblio->{'abstract'});
1399 my ($dbh,$biblio) = @_;
1400 # my $dbh = C4Connect;
1404 $query = "Update biblio set title = ?, author = ?, abstract = ?, copyrightdate = ?,
1405 seriestitle = ?, serial = ?, unititle = ?, notes = ? where biblionumber = ?";
1406 $sth = $dbh->prepare($query);
1407 $sth->execute($biblio->{'title'},$biblio->{'author'},$biblio->{'abstract'},$biblio->{'copyrightdate'},
1408 $biblio->{'seriestitle'},$biblio->{'serial'},$biblio->{'unititle'},$biblio->{'notes'},$biblio->{'biblionumber'});
1411 return($biblio->{'biblionumber'});
1414 sub OLDmodsubtitle {
1415 my ($dbh,$bibnum, $subtitle) = @_;
1416 my $query = "update bibliosubtitle set subtitle = ? where biblionumber = ?";
1417 my $sth = $dbh->prepare($query);
1418 $sth->execute($subtitle,$bibnum);
1423 sub OLDmodaddauthor {
1424 my ($dbh,$bibnum, $author) = @_;
1425 # my $dbh = C4Connect;
1426 my $query = "Delete from additionalauthors where biblionumber = $bibnum";
1427 my $sth = $dbh->prepare($query);
1432 if ($author ne '') {
1433 $query = "Insert into additionalauthors set
1436 $sth = $dbh->prepare($query);
1438 $sth->execute($author,$bibnum);
1442 } # sub modaddauthor
1446 my ($dbh,$bibnum, $force, @subject) = @_;
1447 # my $dbh = C4Connect;
1448 my $count = @subject;
1450 for (my $i = 0; $i < $count; $i++) {
1451 $subject[$i] =~ s/^ //g;
1452 $subject[$i] =~ s/ $//g;
1453 my $query = "select * from catalogueentry where entrytype = 's' and catalogueentry = '$subject[$i]'";
1454 my $sth = $dbh->prepare($query);
1457 if (my $data = $sth->fetchrow_hashref) {
1459 if ($force eq $subject[$i] || $force eq 1) {
1460 # subject not in aut, chosen to force anway
1461 # so insert into cataloguentry so its in auth file
1462 $query = "Insert into catalogueentry (entrytype,catalogueentry) values ('s','$subject[$i]')";
1463 my $sth2 = $dbh->prepare($query);
1468 $error = "$subject[$i]\n does not exist in the subject authority file";
1469 $query = "Select * from catalogueentry where entrytype = 's' and (catalogueentry like '$subject[$i] %'
1470 or catalogueentry like '% $subject[$i] %' or catalogueentry like '% $subject[$i]')";
1471 my $sth2 = $dbh->prepare($query);
1473 while (my $data = $sth2->fetchrow_hashref) {
1474 $error .= "<br>$data->{'catalogueentry'}";
1482 my $query = "Delete from bibliosubject where biblionumber = $bibnum";
1483 my $sth = $dbh->prepare($query);
1486 for (my $i = 0; $i < $count; $i++) {
1487 $sth = $dbh->prepare("Insert into bibliosubject values ('$subject[$i]', $bibnum)");
1498 my ($dbh,$biblioitem) = @_;
1499 # my $dbh = C4Connect;
1502 $biblioitem->{'itemtype'} = $dbh->quote($biblioitem->{'itemtype'});
1503 $biblioitem->{'url'} = $dbh->quote($biblioitem->{'url'});
1504 $biblioitem->{'isbn'} = $dbh->quote($biblioitem->{'isbn'});
1505 $biblioitem->{'publishercode'} = $dbh->quote($biblioitem->{'publishercode'});
1506 $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
1507 $biblioitem->{'classification'} = $dbh->quote($biblioitem->{'classification'});
1508 $biblioitem->{'dewey'} = $dbh->quote($biblioitem->{'dewey'});
1509 $biblioitem->{'subclass'} = $dbh->quote($biblioitem->{'subclass'});
1510 $biblioitem->{'illus'} = $dbh->quote($biblioitem->{'illus'});
1511 $biblioitem->{'pages'} = $dbh->quote($biblioitem->{'pages'});
1512 $biblioitem->{'volumeddesc'} = $dbh->quote($biblioitem->{'volumeddesc'});
1513 $biblioitem->{'notes'} = $dbh->quote($biblioitem->{'notes'});
1514 $biblioitem->{'size'} = $dbh->quote($biblioitem->{'size'});
1515 $biblioitem->{'place'} = $dbh->quote($biblioitem->{'place'});
1517 $query = "Update biblioitems set
1518 itemtype = $biblioitem->{'itemtype'},
1519 url = $biblioitem->{'url'},
1520 isbn = $biblioitem->{'isbn'},
1521 publishercode = $biblioitem->{'publishercode'},
1522 publicationyear = $biblioitem->{'publicationyear'},
1523 classification = $biblioitem->{'classification'},
1524 dewey = $biblioitem->{'dewey'},
1525 subclass = $biblioitem->{'subclass'},
1526 illus = $biblioitem->{'illus'},
1527 pages = $biblioitem->{'pages'},
1528 volumeddesc = $biblioitem->{'volumeddesc'},
1529 notes = $biblioitem->{'notes'},
1530 size = $biblioitem->{'size'},
1531 place = $biblioitem->{'place'}
1532 where biblioitemnumber = $biblioitem->{'biblioitemnumber'}";
1540 my ($dbh,$bibitemnum,$note)=@_;
1541 # my $dbh=C4Connect;
1542 my $query="update biblioitems set notes='$note' where
1543 biblioitemnumber='$bibitemnum'";
1544 my $sth=$dbh->prepare($query);
1550 sub OLDnewbiblioitem {
1551 my ($dbh,$biblioitem) = @_;
1552 # my $dbh = C4Connect;
1553 my $query = "Select max(biblioitemnumber) from biblioitems";
1554 my $sth = $dbh->prepare($query);
1559 $data = $sth->fetchrow_arrayref;
1560 $bibitemnum = $$data[0] + 1;
1564 $sth = $dbh->prepare("insert into biblioitems set
1565 biblioitemnumber = ?, biblionumber = ?,
1566 volume = ?, number = ?,
1567 classification = ?, itemtype = ?,
1569 issn = ?, dewey = ?,
1570 subclass = ?, publicationyear = ?,
1571 publishercode = ?, volumedate = ?,
1572 volumeddesc = ?, illus = ?,
1573 pages = ?, notes = ?,
1575 marc = ?, place = ?");
1576 $sth->execute($bibitemnum, $biblioitem->{'biblionumber'},
1577 $biblioitem->{'volume'}, $biblioitem->{'number'},
1578 $biblioitem->{'classification'}, $biblioitem->{'itemtype'},
1579 $biblioitem->{'url'}, $biblioitem->{'isbn'},
1580 $biblioitem->{'issn'}, $biblioitem->{'dewey'},
1581 $biblioitem->{'subclass'}, $biblioitem->{'publicationyear'},
1582 $biblioitem->{'publishercode'}, $biblioitem->{'volumedate'},
1583 $biblioitem->{'volumeddesc'}, $biblioitem->{'illus'},
1584 $biblioitem->{'pages'}, $biblioitem->{'notes'},
1585 $biblioitem->{'size'}, $biblioitem->{'lccn'},
1586 $biblioitem->{'marc'}, $biblioitem->{'place'});
1589 return($bibitemnum);
1593 my ($dbh,$bibnum)=@_;
1594 my $query="insert into bibliosubject (biblionumber) values ($bibnum)";
1595 my $sth=$dbh->prepare($query);
1600 sub OLDnewsubtitle {
1601 my ($dbh,$bibnum, $subtitle) = @_;
1602 my $query = "insert into bibliosubtitle set biblionumber = ?, subtitle = ?";
1603 my $sth = $dbh->prepare($query);
1604 $sth->execute($bibnum,$subtitle);
1610 my ($dbh,$item, $barcode) = @_;
1611 # my $dbh = C4Connect;
1612 my $query = "Select max(itemnumber) from items";
1613 my $sth = $dbh->prepare($query);
1619 $data = $sth->fetchrow_hashref;
1620 $itemnumber = $data->{'max(itemnumber)'} + 1;
1623 $sth=$dbh->prepare("Insert into items set
1624 itemnumber = ?, biblionumber = ?,
1625 biblioitemnumber = ?, barcode = ?,
1626 booksellerid = ?, dateaccessioned = NOW(),
1627 homebranch = ?, holdingbranch = ?,
1628 price = ?, replacementprice = ?,
1629 replacementpricedate = NOW(), itemnotes = ?,
1633 $sth->execute($itemnumber, $item->{'biblionumber'},
1634 $item->{'biblioitemnumber'},$barcode,
1635 $item->{'booksellerid'},
1636 $item->{'homebranch'},$item->{'holdingbranch'},
1637 $item->{'price'},$item->{'replacementprice'},
1638 $item->{'itemnotes'},$item->{'loan'});
1639 if (defined $sth->errstr) {
1640 $error .= $sth->errstr;
1643 return($itemnumber,$error);
1647 my ($dbh,$item) = @_;
1648 # my ($dbh,$loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
1649 # my $dbh=C4Connect;
1650 $item->{'itemnum'}=$item->{'itemnumber'} unless $item->{'itemnum'};
1651 my $query="update items set barcode='$item->{'barcode'}',itemnotes='$item->{'notes'}'
1652 where itemnumber=$item->{'itemnum'}";
1653 if ($item->{'barcode'} eq ''){
1654 $query="update items set notforloan=$item->{'loan'} where itemnumber=$item->{'itemnum'}";
1656 if ($item->{'lost'} ne ''){
1657 $query="update items set biblioitemnumber=$item->{'bibitemnum'},
1658 barcode='$item->{'barcode'}',
1659 itemnotes='$item->{'notes'}',
1660 homebranch='$item->{'homebranch'}',
1661 itemlost='$item->{'lost'}',
1662 wthdrawn='$item->{'wthdrawn'}'
1663 where itemnumber=$item->{'itemnum'}";
1665 if ($item->{'replacement'} ne ''){
1666 $query=~ s/ where/,replacementprice='$item->{'replacement'}' where/;
1668 my $sth=$dbh->prepare($query);
1675 my ($dbh,$itemnum)=@_;
1676 # my $dbh=C4Connect;
1677 my $query="select * from items where itemnumber=$itemnum";
1678 my $sth=$dbh->prepare($query);
1680 my @data=$sth->fetchrow_array;
1682 $query="Insert into deleteditems values (";
1683 foreach my $temp (@data){
1684 $query .= "'$temp',";
1688 $sth=$dbh->prepare($query);
1691 $query = "Delete from items where itemnumber=$itemnum";
1692 $sth=$dbh->prepare($query);
1698 sub OLDdeletebiblioitem {
1699 my ($dbh,$biblioitemnumber) = @_;
1700 # my $dbh = C4Connect;
1701 my $query = "Select * from biblioitems
1702 where biblioitemnumber = $biblioitemnumber";
1703 my $sth = $dbh->prepare($query);
1708 if (@results = $sth->fetchrow_array) {
1709 $query = "Insert into deletedbiblioitems values (";
1710 foreach my $value (@results) {
1711 $value = $dbh->quote($value);
1712 $query .= "$value,";
1715 $query =~ s/\,$/\)/;
1718 $query = "Delete from biblioitems
1719 where biblioitemnumber = $biblioitemnumber";
1723 # Now delete all the items attached to the biblioitem
1724 $query = "Select * from items where biblioitemnumber = $biblioitemnumber";
1725 $sth = $dbh->prepare($query);
1727 while (@results = $sth->fetchrow_array) {
1728 $query = "Insert into deleteditems values (";
1729 foreach my $value (@results) {
1730 $value = $dbh->quote($value);
1731 $query .= "$value,";
1733 $query =~ s/\,$/\)/;
1737 $query = "Delete from items where biblioitemnumber = $biblioitemnumber";
1740 } # sub deletebiblioitem
1743 my ($dbh,$biblio)=@_;
1744 # my $dbh=C4Connect;
1745 my $query="select * from biblio where biblionumber=$biblio";
1746 my $sth=$dbh->prepare($query);
1748 if (my @data=$sth->fetchrow_array){
1750 $query="Insert into deletedbiblio values (";
1751 foreach my $temp (@data){
1752 $temp=~ s/\'/\\\'/g;
1753 $query .= "'$temp',";
1757 $sth=$dbh->prepare($query);
1760 $query = "Delete from biblio where biblionumber=$biblio";
1761 $sth=$dbh->prepare($query);
1777 my $dbh = C4::Context->dbh;
1778 my $query="Select count(*) from items where biblionumber=$biblio";
1780 my $sth=$dbh->prepare($query);
1782 my $data=$sth->fetchrow_hashref;
1784 return($data->{'count(*)'});
1789 ($order, $ordernumber) = &getorder($biblioitemnumber, $biblionumber);
1791 Looks up the order with the given biblionumber and biblioitemnumber.
1793 Returns a two-element array. C<$ordernumber> is the order number.
1794 C<$order> is a reference-to-hash describing the order; its keys are
1795 fields from the biblio, biblioitems, aqorders, and aqorderbreakdown
1796 tables of the Koha database.
1800 # FIXME - This is effectively identical to &C4::Catalogue::getorder.
1801 # Pick one and stick with it.
1804 my $dbh = C4::Context->dbh;
1805 my $query="Select ordernumber
1807 where biblionumber=? and biblioitemnumber=?";
1808 my $sth=$dbh->prepare($query);
1809 $sth->execute($bib,$bi);
1810 # FIXME - Use fetchrow_array(), since we're only interested in the one
1812 my $ordnum=$sth->fetchrow_hashref;
1814 my $order=getsingleorder($ordnum->{'ordernumber'});
1816 return ($order,$ordnum->{'ordernumber'});
1819 =item getsingleorder
1821 $order = &getsingleorder($ordernumber);
1823 Looks up an order by order number.
1825 Returns a reference-to-hash describing the order. The keys of
1826 C<$order> are fields from the biblio, biblioitems, aqorders, and
1827 aqorderbreakdown tables of the Koha database.
1831 # FIXME - This is effectively identical to
1832 # &C4::Catalogue::getsingleorder.
1833 # Pick one and stick with it.
1834 sub getsingleorder {
1836 my $dbh = C4::Context->dbh;
1837 my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown
1838 where aqorders.ordernumber=?
1839 and biblio.biblionumber=aqorders.biblionumber and
1840 biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
1841 aqorders.ordernumber=aqorderbreakdown.ordernumber";
1842 my $sth=$dbh->prepare($query);
1843 $sth->execute($ordnum);
1844 my $data=$sth->fetchrow_hashref;
1851 my $dbh = C4::Context->dbh;
1852 my $bibnum=OLDnewbiblio($dbh,$biblio);
1853 # finds new (MARC bibid
1854 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$bibnum);
1855 my $record = &MARCkoha2marcBiblio($dbh,$bibnum);
1856 MARCaddbiblio($dbh,$record,$bibnum);
1863 $biblionumber = &modbiblio($biblio);
1865 Update a biblio record.
1867 C<$biblio> is a reference-to-hash whose keys are the fields in the
1868 biblio table in the Koha database. All fields must be present, not
1869 just the ones you wish to change.
1871 C<&modbiblio> updates the record defined by
1872 C<$biblio-E<gt>{biblionumber}> with the values in C<$biblio>.
1874 C<&modbiblio> returns C<$biblio-E<gt>{biblionumber}> whether it was
1881 my $dbh = C4::Context->dbh;
1882 my $biblionumber=OLDmodbiblio($dbh,$biblio);
1883 my $record = MARCkoha2marcBiblio($dbh,$biblionumber);
1884 # finds new (MARC bibid
1885 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
1886 MARCmodbiblio($dbh,$bibid,$record,0);
1887 return($biblionumber);
1892 &modsubtitle($biblionumber, $subtitle);
1894 Sets the subtitle of a book.
1896 C<$biblionumber> is the biblionumber of the book to modify.
1898 C<$subtitle> is the new subtitle.
1903 my ($bibnum, $subtitle) = @_;
1904 my $dbh = C4::Context->dbh;
1905 &OLDmodsubtitle($dbh,$bibnum,$subtitle);
1910 &modaddauthor($biblionumber, $author);
1912 Replaces all additional authors for the book with biblio number
1913 C<$biblionumber> with C<$author>. If C<$author> is the empty string,
1914 C<&modaddauthor> deletes all additional authors.
1919 my ($bibnum, $author) = @_;
1920 my $dbh = C4::Context->dbh;
1921 &OLDmodaddauthor($dbh,$bibnum,$author);
1922 } # sub modaddauthor
1926 $error = &modsubject($biblionumber, $force, @subjects);
1928 $force - a subject to force
1930 $error - Error message, or undef if successful.
1935 my ($bibnum, $force, @subject) = @_;
1936 my $dbh = C4::Context->dbh;
1937 my $error= &OLDmodsubject($dbh,$bibnum,$force, @subject);
1942 my ($biblioitem) = @_;
1943 my $dbh = C4::Context->dbh;
1944 &OLDmodbibitem($dbh,$biblioitem);
1945 my $MARCbibitem = MARCkoha2marcBiblio($dbh,$biblioitem);
1946 &MARCmodbiblio($dbh,$biblioitem->{biblionumber},$MARCbibitem,0);
1950 my ($bibitemnum,$note)=@_;
1951 my $dbh = C4::Context->dbh;
1952 &OLDmodnote($dbh,$bibitemnum,$note);
1956 my ($biblioitem) = @_;
1957 my $dbh = C4::Context->dbh;
1958 my $bibitemnum = &OLDnewbiblioitem($dbh,$biblioitem);
1959 # print STDERR "bibitemnum : $bibitemnum\n";
1960 my $MARCbiblio= MARCkoha2marcBiblio($dbh,$biblioitem->{biblionumber},$bibitemnum);
1961 # print STDERR $MARCbiblio->as_formatted();
1962 &MARCaddbiblio($dbh,$MARCbiblio,$biblioitem->{biblionumber});
1963 return($bibitemnum);
1968 my $dbh = C4::Context->dbh;
1969 &OLDnewsubject($dbh,$bibnum);
1973 my ($bibnum, $subtitle) = @_;
1974 my $dbh = C4::Context->dbh;
1975 &OLDnewsubtitle($dbh,$bibnum,$subtitle);
1979 my ($item, @barcodes) = @_;
1980 my $dbh = C4::Context->dbh;
1984 foreach my $barcode (@barcodes) {
1985 ($itemnumber,$error)=&OLDnewitems($dbh,$item,uc($barcode));
1987 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{biblionumber},$itemnumber);
1988 &MARCadditem($dbh,$MARCitem,$item->{biblionumber});
1995 my $dbh = C4::Context->dbh;
1996 &OLDmoditem($dbh,$item);
1997 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{'biblionumber'},$item->{'itemnum'});
1998 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$item->{biblionumber});
1999 &MARCmoditem($dbh,$MARCitem,$bibid,$item->{itemnum},0);
2003 my ($count,@barcodes)=@_;
2004 my $dbh = C4::Context->dbh;
2006 for (my $i=0;$i<$count;$i++){
2007 $barcodes[$i]=uc $barcodes[$i];
2008 my $query="Select * from items where barcode='$barcodes[$i]'";
2009 my $sth=$dbh->prepare($query);
2011 if (my $data=$sth->fetchrow_hashref){
2012 $error.=" Duplicate Barcode: $barcodes[$i]";
2020 my ($bibitemnum)=@_;
2021 my $dbh = C4::Context->dbh;
2022 my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
2023 my $sth=$dbh->prepare($query);
2025 my $data=$sth->fetchrow_hashref;
2027 return($data->{'count(*)'});
2032 my $dbh = C4::Context->dbh;
2033 &OLDdelitem($dbh,$itemnum);
2036 sub deletebiblioitem {
2037 my ($biblioitemnumber) = @_;
2038 my $dbh = C4::Context->dbh;
2039 &OLDdeletebiblioitem($dbh,$biblioitemnumber);
2040 } # sub deletebiblioitem
2045 my $dbh = C4::Context->dbh;
2046 &OLDdelbiblio($dbh,$biblio);
2050 my $dbh = C4::Context->dbh;
2051 my $query = "select * from itemtypes order by description";
2052 my $sth = $dbh->prepare($query);
2053 # || die "Cannot prepare $query" . $dbh->errstr;
2058 # || die "Cannot execute $query\n" . $sth->errstr;
2059 while (my $data = $sth->fetchrow_hashref) {
2060 $results[$count] = $data;
2065 return($count, @results);
2066 } # sub getitemtypes
2069 my ($biblionumber) = @_;
2070 my $dbh = C4::Context->dbh;
2071 my $query = "Select * from biblio where biblionumber = $biblionumber";
2072 my $sth = $dbh->prepare($query);
2073 # || die "Cannot prepare $query\n" . $dbh->errstr;
2078 # || die "Cannot execute $query\n" . $sth->errstr;
2079 while (my $data = $sth->fetchrow_hashref) {
2080 $results[$count] = $data;
2085 return($count, @results);
2089 my ($biblioitemnum) = @_;
2090 my $dbh = C4::Context->dbh;
2091 my $query = "Select * from biblioitems where
2092 biblioitemnumber = $biblioitemnum";
2093 my $sth = $dbh->prepare($query);
2099 while (my $data = $sth->fetchrow_hashref) {
2100 $results[$count] = $data;
2105 return($count, @results);
2106 } # sub getbiblioitem
2108 sub getbiblioitembybiblionumber {
2109 my ($biblionumber) = @_;
2110 my $dbh = C4::Context->dbh;
2111 my $query = "Select * from biblioitems where biblionumber =
2113 my $sth = $dbh->prepare($query);
2119 while (my $data = $sth->fetchrow_hashref) {
2120 $results[$count] = $data;
2125 return($count, @results);
2128 sub getitemsbybiblioitem {
2129 my ($biblioitemnum) = @_;
2130 my $dbh = C4::Context->dbh;
2131 my $query = "Select * from items, biblio where
2132 biblio.biblionumber = items.biblionumber and biblioitemnumber
2134 my $sth = $dbh->prepare($query);
2135 # || die "Cannot prepare $query\n" . $dbh->errstr;
2140 # || die "Cannot execute $query\n" . $sth->errstr;
2141 while (my $data = $sth->fetchrow_hashref) {
2142 $results[$count] = $data;
2147 return($count, @results);
2148 } # sub getitemsbybiblioitem
2152 # Subroutine to log changes to databases
2153 # Eventually, this subroutine will be used to create a log of all changes made,
2154 # with the possibility of "undo"ing some changes
2156 if ($database eq 'kohadb') {
2162 # print STDERR "KOHA: $type $section $item $original $new\n";
2163 } elsif ($database eq 'marc') {
2165 my $Record_ID=shift;
2168 my $subfield_ID=shift;
2171 # print STDERR "MARC: $type $Record_ID $tag $mark $subfield_ID $original $new\n";
2175 #------------------------------------------------
2178 #---------------------------------------
2179 # Find a biblio entry, or create a new one if it doesn't exist.
2180 # If a "subtitle" entry is in hash, add it to subtitle table
2181 sub getoraddbiblio {
2185 # FIXME - Unused argument
2186 $biblio, # hash ref to fields
2197 $dbh = C4::Context->dbh;
2199 print "<PRE>Looking for biblio </PRE>\n" if $debug;
2200 $sth=$dbh->prepare("select biblionumber
2202 where title=? and author=?
2203 and copyrightdate=? and seriestitle=?");
2205 $biblio->{title}, $biblio->{author},
2206 $biblio->{copyright}, $biblio->{seriestitle} );
2208 ($biblionumber) = $sth->fetchrow;
2209 print "<PRE>Biblio exists with number $biblionumber</PRE>\n" if $debug;
2211 # Doesn't exist. Add new one.
2212 print "<PRE>Adding biblio</PRE>\n" if $debug;
2213 ($biblionumber,$error)=&newbiblio($biblio);
2214 if ( $biblionumber ) {
2215 print "<PRE>Added with biblio number=$biblionumber</PRE>\n" if $debug;
2216 if ( $biblio->{subtitle} ) {
2217 &newsubtitle($biblionumber,$biblio->{subtitle} );
2220 print "<PRE>Couldn't add biblio: $error</PRE>\n" if $debug;
2224 return $biblionumber,$error;
2226 } # sub getoraddbiblio
2229 # converts ISO 5426 coded string to ISO 8859-1
2230 # sloppy code : should be improved in next issue
2231 my ($string,$encoding) = @_ ;
2233 # $encoding = C4::Context->preference("marcflavour") unless $encoding;
2234 if ($encoding eq "UNIMARC") {
2296 # this handles non-sorting blocks (if implementation requires this)
2297 $string = nsb_clean($_) ;
2298 } elsif ($encoding eq "USMARC") {
2351 # this handles non-sorting blocks (if implementation requires this)
2352 $string = nsb_clean($_) ;
2359 my $NSB = '\x88' ; # NSB : begin Non Sorting Block
2360 my $NSE = '\x89' ; # NSE : Non Sorting Block end
2361 # handles non sorting blocks
2365 s/[ ]{0,1}$NSE/) /gm ;
2370 END { } # module clean-up code here (global destructor)
2376 Koha Developement team <info@koha.org>
2378 Paul POULAIN paul.poulain@free.fr