4 # Revision 1.55 2003/07/15 00:02:49 slef
5 # Work on bug 515... can we do a single-side rename of notes to bnotes?
7 # Revision 1.54 2003/07/11 11:51:32 tipaul
8 # *** empty log message ***
10 # Revision 1.52 2003/07/10 10:37:19 tipaul
11 # fix for copyrightdate problem, #514
13 # Revision 1.51 2003/07/02 14:47:17 tipaul
14 # fix for #519 : items.dateaccessioned imports incorrectly
16 # Revision 1.49 2003/06/17 11:21:13 tipaul
17 # improvments/fixes for z3950 support.
18 # * Works now even on ADD, not only on MODIFY
19 # * able to search on ISBN, author, title
21 # Revision 1.48 2003/06/16 09:22:53 rangi
22 # Just added an order clause to getitemtypes
24 # Revision 1.47 2003/05/20 16:22:44 tipaul
25 # fixing typo in Biblio.pm POD
27 # Revision 1.46 2003/05/19 13:45:18 tipaul
28 # support for subtitles, additional authors, subject.
29 # 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.
30 # Note that some OLD-DB subs are strange (dummy ?) see OLDmodsubject, OLDmodsubtitle, OLDmodaddiauthor in C4/Biblio.pm
31 # 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.
33 # Revision 1.45 2003/04/29 16:50:49 tipaul
34 # really proud of this commit :-)
35 # z3950 search and import seems to works fine.
36 # Let me explain how :
37 # * a "search z3950" button is added in the addbiblio template.
38 # * when clicked, a popup appears and z3950/search.pl is called
39 # * z3950/search.pl calls addz3950search in the DB
40 # * the z3950 daemon retrieve the records and stores them in z3950results AND in marc_breeding table.
41 # * as long as there as searches pending, the popup auto refresh every 2 seconds, and says how many searches are pending.
42 # * when the user clicks on a z3950 result => the parent popup is called with the requested biblio, and auto-filled
45 # * 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.
46 # * 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.
48 # Revision 1.44 2003/04/28 13:07:14 tipaul
49 # Those fixes solves the "internal server error" with MARC::Record 1.12.
50 # It was due to an illegal contruction in Koha : we tried to retrive subfields from <10 tags.
51 # That's not possible. MARC::Record accepted this in 0.93 version, but it was fixed after.
52 # Now, the construct/retrieving is OK !
54 # Revision 1.43 2003/04/10 13:56:02 tipaul
56 # * worked in 1.9.0, but not in 1.9.1 :
57 # - modif of a biblio didn't work
58 # - 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.
60 # * did not work before :
61 # - repeatable subfields now works correctly. Enter 2 subfields separated by | and they will be splitted during saving.
62 # - dropped the last subfield of the MARC form :-(
65 # - 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.
66 # Note the MARCdelbiblio has been rewritted to enable deletion of a biblio WITHOUT deleting items.
68 # Revision 1.42 2003/04/04 08:41:11 tipaul
69 # last commits before 1.9.1
71 # Revision 1.41 2003/04/01 12:26:43 tipaul
74 # Revision 1.40 2003/03/11 15:14:03 tipaul
77 # Revision 1.39 2003/03/07 16:35:42 tipaul
78 # * moving generic functions to Koha.pm
79 # * improvement of SearchMarc.pm
83 # Revision 1.38 2003/02/27 16:51:59 tipaul
84 # * moving prepare / execute to ? form.
87 # * road to 1.9.2 => acquisition and cataloguing merging
89 # Revision 1.37 2003/02/12 11:03:03 tipaul
90 # Support for 000 -> 010 fields.
91 # Those fields doesn't have subfields.
92 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
93 # 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.
95 # Revision 1.36 2003/02/12 11:01:01 tipaul
96 # Support for 000 -> 010 fields.
97 # Those fields doesn't have subfields.
98 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
99 # 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.
101 # Revision 1.35 2003/02/03 18:46:00 acli
102 # Minor factoring in C4/Biblio.pm, plus change to export the per-tag
103 # 'mandatory' property to a per-subfield 'tag_mandatory' template parameter,
104 # so that addbiblio.tmpl can distinguish between mandatory subfields in a
105 # mandatory tag and mandatory subfields in an optional tag
107 # Not-minor factoring in acqui.simple/addbiblio.pl to make the if-else blocks
108 # smaller, and to add some POD; need further testing for this
110 # Added function to check if a MARC subfield name is "koha-internal" (instead
111 # of checking it for 'lib' and 'tag' everywhere); temporarily added to Koha.pm
113 # Use above function in acqui.simple/additem.pl and search.marc/search.pl
115 # Revision 1.34 2003/01/28 14:50:04 tipaul
116 # fixing MARCmodbiblio API and reindenting code
118 # Revision 1.33 2003/01/23 12:22:37 tipaul
119 # adding char_decode to decode MARC21 or UNIMARC extended chars
121 # Revision 1.32 2002/12/16 15:08:50 tipaul
122 # small but important bugfix (fixes a problem in export)
124 # Revision 1.31 2002/12/13 16:22:04 tipaul
125 # 1st draft of marc export
127 # Revision 1.30 2002/12/12 21:26:35 tipaul
128 # YAB ! (Yet Another Bugfix) => related to biblio modif
129 # (some warning cleaning too)
131 # Revision 1.29 2002/12/12 16:35:00 tipaul
132 # adding authentification with Auth.pm and
133 # MAJOR BUGFIX on marc biblio modification
135 # Revision 1.28 2002/12/10 13:30:03 tipaul
136 # fugfixes from Dombes Abbey work
138 # Revision 1.27 2002/11/19 12:36:16 tipaul
140 # various bugfixes, improvments, and migration from acquisition.pm to biblio.pm
142 # Revision 1.26 2002/11/12 15:58:43 tipaul
145 # * 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)
147 # Revision 1.25 2002/10/25 10:58:26 tipaul
149 # * bugfixes and improvements
151 # Revision 1.24 2002/10/24 12:09:01 arensb
152 # Fixed "no title" warning when generating HTML documentation from POD.
154 # Revision 1.23 2002/10/16 12:43:08 arensb
155 # Added some FIXME comments.
157 # Revision 1.22 2002/10/15 13:39:17 tipaul
158 # removing Acquisition.pm
159 # deleting unused code in biblio.pm, rewriting POD and answering most FIXME comments
161 # Revision 1.21 2002/10/13 11:34:14 arensb
162 # Replaced expressions of the form "$x = $x <op> $y" with "$x <op>= $y".
163 # Thus, $x = $x+2 becomes $x += 2, and so forth.
165 # Revision 1.20 2002/10/13 08:28:32 arensb
166 # Deleted unused variables.
167 # Removed trailing whitespace.
169 # Revision 1.19 2002/10/13 05:56:10 arensb
170 # Added some FIXME comments.
172 # Revision 1.18 2002/10/11 12:34:53 arensb
173 # Replaced &requireDBI with C4::Context->dbh
175 # Revision 1.17 2002/10/10 14:48:25 tipaul
178 # Revision 1.16 2002/10/07 14:04:26 tipaul
179 # road to 1.3.1 : viewing MARC biblio
181 # Revision 1.15 2002/10/05 09:49:25 arensb
182 # Merged with arensb-context branch: use C4::Context->dbh instead of
183 # &C4Connect, and generally prefer C4::Context over C4::Database.
185 # Revision 1.14 2002/10/03 11:28:18 tipaul
186 # Extending Context.pm to add stopword management and using it in MARC-API.
187 # First benchmarks show a medium speed improvement, which is nice as this part is heavily called.
189 # Revision 1.13 2002/10/02 16:26:44 tipaul
192 # Revision 1.12.2.4 2002/10/05 07:09:31 arensb
193 # Merged in changes from main branch.
195 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
196 # Added a whole mess of FIXME comments.
198 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
199 # Added some missing semicolons.
201 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
202 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
205 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
206 # Added a whole mess of FIXME comments.
208 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
209 # Added some missing semicolons.
211 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
212 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
215 # Revision 1.12 2002/10/01 11:48:51 arensb
216 # Added some FIXME comments, mostly marking duplicate functions.
218 # Revision 1.11 2002/09/24 13:49:26 tipaul
219 # long WAS the road to 1.3.0...
220 # coming VERY SOON NOW...
221 # modifying installer and buildrelease to update the DB
223 # Revision 1.10 2002/09/22 16:50:08 arensb
224 # Added some FIXME comments.
226 # Revision 1.9 2002/09/20 12:57:46 tipaul
227 # long is the road to 1.4.0
228 # * MARCadditem and MARCmoditem now wroks
229 # * various bugfixes in MARC management
230 # !!! 1.3.0 should be released very soon now. Be careful !!!
232 # Revision 1.8 2002/09/10 13:53:52 tipaul
233 # MARC API continued...
235 # * 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)
237 # Note : it should not be hard for marcimport and marcexport to re-link fields from internal tag/subfield to "legal" tag/subfield.
239 # Revision 1.7 2002/08/14 18:12:51 tonnesen
240 # Added copyright statement to all .pl and .pm files
242 # Revision 1.6 2002/07/25 13:40:31 tipaul
243 # pod documenting the API.
245 # Revision 1.5 2002/07/24 16:11:37 tipaul
247 # Database.pm and Output.pm are almost not modified (var test...)
249 # Biblio.pm is almost completly rewritten.
251 # WHAT DOES IT ??? ==> END of Hitchcock suspens
253 # 1st, it does... nothing...
254 # 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 ...
256 # All old-API functions have been cloned. for example, the "newbiblio" sub, now has become :
257 # * a "newbiblio" sub, with the same parameters. It just call a sub named OLDnewbiblio
258 # * 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.
259 # * 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.
260 # 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 ;-)
262 # 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.
263 # Note we have decided with steve that a old-biblio <=> a MARC-Biblio.
267 # Copyright 2000-2002 Katipo Communications
269 # This file is part of Koha.
271 # Koha is free software; you can redistribute it and/or modify it under the
272 # terms of the GNU General Public License as published by the Free Software
273 # Foundation; either version 2 of the License, or (at your option) any later
276 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
277 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
278 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
280 # You should have received a copy of the GNU General Public License along with
281 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
282 # Suite 330, Boston, MA 02111-1307 USA
290 use vars qw($VERSION @ISA @EXPORT);
292 # set the version for version checking
297 # don't forget MARCxxx subs are here only for testing purposes. Should not be used
298 # as the old-style API and the NEW one are the only public functions.
301 &updateBiblio &updateBiblioItem &updateItem
302 &itemcount &newbiblio &newbiblioitem
303 &modnote &newsubject &newsubtitle
304 &modbiblio &checkitems
305 &newitems &modbibitem
306 &modsubtitle &modsubject &modaddauthor &moditem &countitems
307 &delitem &deletebiblioitem &delbiblio
308 &getitemtypes &getbiblio
309 &getbiblioitembybiblionumber
310 &getbiblioitem &getitemsbybiblioitem
312 &newcompletebiblioitem
314 &MARCfind_oldbiblionumber_from_MARCbibid
315 &MARCfind_MARCbibid_from_oldbiblionumber
316 &MARCfind_marc_from_kohafield
320 &NEWnewbiblio &NEWnewitem
321 &NEWmodbiblio &NEWmoditem
323 &MARCaddbiblio &MARCadditem
324 &MARCmodsubfield &MARCaddsubfield
325 &MARCmodbiblio &MARCmoditem
326 &MARCkoha2marcBiblio &MARCmarc2koha
327 &MARCkoha2marcItem &MARChtml2marc
328 &MARCgetbiblio &MARCgetitem
329 &MARCaddword &MARCdelword
335 # MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC
338 # all the following subs takes a MARC::Record as parameter and manage
339 # the MARC-DB. They are called by the 1.0/1.2 xxx subs, and by the
340 # NEWxxx subs (xxx deals with old-DB parameters, the NEWxxx deals with MARC-DB parameter)
344 C4::Biblio - acquisition, catalog management functions
348 move from 1.2 to 1.4 version :
349 1.2 and previous version uses a specific API to manage biblios. This API uses old-DB style parameters.
350 In the 1.4 version, we want to do 2 differents things :
351 - keep populating the old-DB, that has a LOT less datas than MARC
352 - populate the MARC-DB
353 To populate the DBs we have 2 differents sources :
354 - the standard acquisition system (through book sellers), that does'nt use MARC data
355 - the MARC acquisition system, that uses MARC data.
357 Thus, we have 2 differents cases :
358 - 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
359 - 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
361 That's why we need 4 subs :
362 all I<subs beginning by MARC> manage only MARC tables. They manage MARC-DB with MARC::Record parameters
363 all I<subs beginning by OLD> manage only OLD-DB tables. They manage old-DB with old-DB parameters
364 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
365 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.
367 - NEW and old-style API should be used in koha to manage biblio
368 - MARCsubs are divided in 2 parts :
369 * some of them manage MARC parameters. They are heavily used in koha.
370 * some of them manage MARC biblio : they are mostly used by NEW and old-style subs.
371 - OLD are used internally only
373 all subs requires/use $dbh as 1st parameter.
375 I<NEWxxx related subs>
377 all subs requires/use $dbh as 1st parameter.
378 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
380 I<OLDxxx related subs>
382 all subs requires/use $dbh as 1st parameter.
383 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
385 They all are the exact copy of 1.0/1.2 version of the sub without the OLD.
386 The OLDxxx is called by the original xxx sub.
387 the 1.4 xxx sub also builds MARC::Record an calls the MARCxxx
389 WARNING : there is 1 difference between initialxxx and OLDxxx :
390 the db header $dbh is always passed as parameter to avoid over-DB connexion
396 =item @tagslib = &MARCgettagslib($dbh,1|0);
398 last param is 1 for liblibrarian and 0 for libopac
399 returns a hash with tag/subfield meaning
400 =item ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,$kohafield);
402 finds MARC tag and subfield for a given kohafield
403 kohafield is "table.field" where table= biblio|biblioitems|items, and field a field of the previous table
405 =item $biblionumber = &MARCfind_oldbiblionumber_from_MARCbibid($dbh,$MARCbibi);
407 finds a old-db biblio number for a given MARCbibid number
409 =item $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
411 finds a MARC bibid from a old-db biblionumber
413 =item $MARCRecord = &MARCkoha2marcBiblio($dbh,$biblionumber,biblioitemnumber);
415 MARCkoha2marcBiblio is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB biblio/biblioitem
417 =item $MARCRecord = &MARCkoha2marcItem($dbh,$biblionumber,itemnumber);
419 MARCkoha2marcItem is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB item
421 =item $MARCRecord = &MARCkoha2marcSubtitle($dbh,$biblionumber,$subtitle);
423 MARCkoha2marcSubtitle is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB subtitle
425 =item $olddb = &MARCmarc2koha($dbh,$MARCRecord);
427 builds a hash with old-db datas from a MARC::Record
429 =item &MARCaddbiblio($dbh,$MARC::Record,$biblionumber);
431 creates a biblio (in the MARC tables only). $biblionumber is the old-db biblionumber of the biblio
433 =item &MARCaddsubfield($dbh,$bibid,$tagid,$indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
435 adds a subfield in a biblio (in the MARC tables only).
437 =item $MARCRecord = &MARCgetbiblio($dbh,$bibid);
439 Returns a MARC::Record for the biblio $bibid.
441 =item &MARCmodbiblio($dbh,$bibid,$record,$delete);
443 MARCmodbiblio changes a biblio for a biblio,MARC::Record passed as parameter
444 It 1st delete the biblio, then recreates it.
445 WARNING : the $delete parameter is not used anymore (too much unsolvable cases).
446 =item ($subfieldid,$subfieldvalue) = &MARCmodsubfield($dbh,$subfieldid,$subfieldvalue);
448 MARCmodsubfield changes the value of a given subfield
450 =item $subfieldid = &MARCfindsubfield($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue);
452 MARCfindsubfield returns a subfield number given a bibid/tag/subfieldvalue values.
453 Returns -1 if more than 1 answer
455 =item $subfieldid = &MARCfindsubfieldid($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
457 MARCfindsubfieldid find a subfieldid for a bibid/tag/tagorder/subfield/subfieldorder
459 =item &MARCdelsubfield($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
461 MARCdelsubfield delete a subfield for a bibid/tag/tagorder/subfield/subfieldorder
463 =item &MARCdelbiblio($dbh,$bibid);
465 MARCdelbiblio delete biblio $bibid
467 =item &MARCkoha2marcOnefield
469 used by MARCkoha2marc and should not be useful elsewhere
471 =item &MARCmarc2kohaOnefield
473 used by MARCmarc2koha and should not be useful elsewhere
477 used to manage MARC_word table and should not be useful elsewhere
481 used to manage MARC_word table and should not be useful elsewhere
486 my ($dbh,$forlibrarian)= @_;
488 my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
489 $sth=$dbh->prepare("select tagfield,$libfield as lib,mandatory from marc_tag_structure order by tagfield");
491 my ($lib,$tag,$res,$tab,$mandatory,$repeatable);
492 while ( ($tag,$lib,$mandatory) = $sth->fetchrow) {
493 $res->{$tag}->{lib}=$lib;
494 $res->{$tab}->{tab}=""; # XXX
495 $res->{$tag}->{mandatory}=$mandatory;
498 $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");
502 my $authorised_value;
503 my $thesaurus_category;
506 while ( ($tag, $subfield, $lib, $tab, $mandatory, $repeatable,$authorised_value,$thesaurus_category,$value_builder,$kohafield) = $sth->fetchrow) {
507 $res->{$tag}->{$subfield}->{lib}=$lib;
508 $res->{$tag}->{$subfield}->{tab}=$tab;
509 $res->{$tag}->{$subfield}->{mandatory}=$mandatory;
510 $res->{$tag}->{$subfield}->{repeatable}=$repeatable;
511 $res->{$tag}->{$subfield}->{authorised_value}=$authorised_value;
512 $res->{$tag}->{$subfield}->{thesaurus_category}=$thesaurus_category;
513 $res->{$tag}->{$subfield}->{value_builder}=$value_builder;
514 $res->{$tag}->{$subfield}->{kohafield}=$kohafield;
519 sub MARCfind_marc_from_kohafield {
520 my ($dbh,$kohafield) = @_;
521 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
522 $sth->execute($kohafield);
523 my ($tagfield,$tagsubfield) = $sth->fetchrow;
524 return ($tagfield,$tagsubfield);
527 sub MARCfind_oldbiblionumber_from_MARCbibid {
528 my ($dbh,$MARCbibid) = @_;
529 my $sth=$dbh->prepare("select biblionumber from marc_biblio where bibid=?");
530 $sth->execute($MARCbibid);
531 my ($biblionumber) = $sth->fetchrow;
532 return $biblionumber;
535 sub MARCfind_MARCbibid_from_oldbiblionumber {
536 my ($dbh,$oldbiblionumber) = @_;
537 my $sth=$dbh->prepare("select bibid from marc_biblio where biblionumber=?");
538 $sth->execute($oldbiblionumber);
539 my ($bibid) = $sth->fetchrow;
544 # pass the MARC::Record to this function, and it will create the records in the marc tables
545 my ($dbh,$record,$biblionumber,$bibid) = @_;
546 my @fields=$record->fields();
548 # adding main table, and retrieving bibid
549 # if bibid is sent, then it's not a true add, it's only a re-add, after a delete (ie, a mod)
550 # if bibid empty => true add, find a new bibid number
552 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
553 my $sth=$dbh->prepare("insert into marc_biblio (datecreated,biblionumber) values (now(),?)");
554 $sth->execute($biblionumber);
555 $sth=$dbh->prepare("select max(bibid) from marc_biblio");
557 ($bibid)=$sth->fetchrow;
561 # now, add subfields...
562 foreach my $field (@fields) {
564 if ($field->tag() <10) {
565 &MARCaddsubfield($dbh,$bibid,
574 my @subfields=$field->subfields();
575 foreach my $subfieldcount (0..$#subfields) {
576 &MARCaddsubfield($dbh,$bibid,
578 $field->indicator(1).$field->indicator(2),
580 $subfields[$subfieldcount][0],
582 $subfields[$subfieldcount][1]
587 $dbh->do("unlock tables");
592 # pass the MARC::Record to this function, and it will create the records in the marc tables
593 my ($dbh,$record,$biblionumber) = @_;
594 # warn "adding : ".$record->as_formatted();
595 # search for MARC biblionumber
596 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
597 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
598 my @fields=$record->fields();
599 my $sth = $dbh->prepare("select max(tagorder) from marc_subfield_table where bibid=?");
600 $sth->execute($bibid);
601 my ($fieldcount) = $sth->fetchrow;
602 # now, add subfields...
603 foreach my $field (@fields) {
604 my @subfields=$field->subfields();
606 foreach my $subfieldcount (0..$#subfields) {
607 &MARCaddsubfield($dbh,$bibid,
609 $field->indicator(1).$field->indicator(2),
611 $subfields[$subfieldcount][0],
613 $subfields[$subfieldcount][1]
617 $dbh->do("unlock tables");
621 sub MARCaddsubfield {
622 # Add a new subfield to a tag into the DB.
623 my ($dbh,$bibid,$tagid,$tag_indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalues) = @_;
624 # if not value, end of job, we do nothing
625 if (length($subfieldvalues) ==0) {
628 if (not($subfieldcode)) {
631 my @subfieldvalues = split /\|/,$subfieldvalues;
632 foreach my $subfieldvalue (@subfieldvalues) {
633 if (length($subfieldvalue)>255) {
634 # $dbh->do("lock tables marc_blob_subfield WRITE, marc_subfield_table WRITE");
635 my $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
636 $sth->execute($subfieldvalue);
637 $sth=$dbh->prepare("select max(blobidlink)from marc_blob_subfield");
639 my ($res)=$sth->fetchrow;
640 $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,valuebloblink) values (?,?,?,?,?,?,?)");
641 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$res);
643 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";
645 # $dbh->do("unlock tables");
647 my $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?,?)");
648 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue);
650 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";
653 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
658 # Returns MARC::Record of the biblio passed in parameter.
660 my $record = MARC::Record->new();
661 #---- TODO : the leader is missing
662 $record->leader(' ');
663 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
664 from marc_subfield_table
665 where bibid=? order by tag,tagorder,subfieldcode
667 my $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
668 $sth->execute($bibid);
672 my $field; # for >=10 tags
673 my $prevvalue; # for <10 tags
674 while (my $row=$sth->fetchrow_hashref) {
675 if ($row->{'valuebloblink'}) { #---- search blob if there is one
676 $sth2->execute($row->{'valuebloblink'});
677 my $row2=$sth2->fetchrow_hashref;
679 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
681 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
684 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
686 $record->add_fields($field) unless $prevtag eq "XXX";
689 $prevtagorder=$row->{tagorder};
690 $prevtag = $row->{tag};
691 $previndicator=$row->{tag_indicator};
692 if ($row->{tag}<10) {
693 $prevvalue = $row->{subfieldvalue};
695 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.' ',0,1), substr($row->{tag_indicator}.' ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
698 if ($row->{tag} <10) {
699 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
701 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
703 $prevtag= $row->{tag};
704 $previndicator=$row->{tag_indicator};
707 # the last has not been included inside the loop... do it now !
709 $record->add_fields($prevtag,$prevvalue);
711 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
712 $record->add_fields($field);
717 # Returns MARC::Record of the biblio passed in parameter.
718 my ($dbh,$bibid,$itemnumber)=@_;
719 my $record = MARC::Record->new();
720 # search MARC tagorder
721 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=?");
722 $sth2->execute($bibid,$itemnumber);
723 my ($tagorder) = $sth2->fetchrow_array();
724 #---- TODO : the leader is missing
725 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
726 from marc_subfield_table
727 where bibid=? and tagorder=? order by subfieldcode,subfieldorder
729 $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
730 $sth->execute($bibid,$tagorder);
731 while (my $row=$sth->fetchrow_hashref) {
732 if ($row->{'valuebloblink'}) { #---- search blob if there is one
733 $sth2->execute($row->{'valuebloblink'});
734 my $row2=$sth2->fetchrow_hashref;
736 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
738 if ($record->field($row->{'tag'})) {
740 #--- this test must stay as this, because of strange behaviour of mySQL/Perl DBI with char var containing a number...
741 #--- sometimes, eliminates 0 at beginning, sometimes no ;-\\\
742 if (length($row->{'tag'}) <3) {
743 $row->{'tag'} = "0".$row->{'tag'};
745 $field =$record->field($row->{'tag'});
747 my $x = $field->add_subfields($row->{'subfieldcode'},$row->{'subfieldvalue'});
748 $record->delete_field($field);
749 $record->add_fields($field);
752 if (length($row->{'tag'}) < 3) {
753 $row->{'tag'} = "0".$row->{'tag'};
755 my $temp = MARC::Field->new($row->{'tag'}," "," ", $row->{'subfieldcode'} => $row->{'subfieldvalue'});
756 $record->add_fields($temp);
764 my ($dbh,$bibid,$record,$delete)=@_;
765 my $oldrecord=&MARCgetbiblio($dbh,$bibid);
766 if ($oldrecord eq $record) {
769 # 1st delete the biblio,
771 &MARCdelbiblio($dbh,$bibid,1);
772 my $biblionumber = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
773 &MARCaddbiblio($dbh,$record,$biblionumber,$bibid);
777 my ($dbh,$bibid,$keep_items) = @_;
778 # if the keep_item is set to 1, then all items are preserved.
779 # This flag is set when the delbiblio is called by modbiblio
780 # due to a too complex structure of MARC (repeatable fields and subfields),
781 # the best solution for a modif is to delete / recreate the record.
782 if ($keep_items eq 1) {
783 #search item field code
784 my $sth = $dbh->prepare("select tagfield from marc_subfield_structure where kohafield like 'items.%'");
786 my $itemtag = $sth->fetchrow_hashref->{tagfield};
787 $dbh->do("delete from marc_subfield_table where bibid=$bibid and tag<>$itemtag");
788 $dbh->do("delete from marc_word where bibid=$bibid and tag<>$itemtag");
790 $dbh->do("delete from marc_biblio where bibid=$bibid");
791 $dbh->do("delete from marc_subfield_table where bibid=$bibid");
792 $dbh->do("delete from marc_word where bibid=$bibid");
796 my ($dbh,$record,$bibid,$itemnumber,$delete)=@_;
797 my $oldrecord=&MARCgetitem($dbh,$bibid,$itemnumber);
798 # if nothing to change, don't waste time...
799 if ($oldrecord eq $record) {
803 # otherwise, skip through each subfield...
804 my @fields = $record->fields();
805 # search old MARC item
806 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=?");
807 $sth2->execute($bibid,$itemnumber);
808 my ($tagorder) = $sth2->fetchrow_array();
809 foreach my $field (@fields) {
810 my $oldfield = $oldrecord->field($field->tag());
811 my @subfields=$field->subfields();
813 foreach my $subfield (@subfields) {
815 # warn "compare : $oldfield".$oldfield->subfield(@$subfield[0]);
816 if ($oldfield eq 0 or (length($oldfield->subfield(@$subfield[0])) ==0) ) {
817 # just adding datas...
818 # warn "addfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
819 # warn "NEW subfield : $bibid,".$field->tag().",".$tagorder.",".@$subfield[0].",".$subfieldorder.",".@$subfield[1].")";
820 &MARCaddsubfield($dbh,$bibid,$field->tag(),$field->indicator(1).$field->indicator(2),
821 $tagorder,@$subfield[0],$subfieldorder,@$subfield[1]);
823 # warn "modfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
824 # modify he subfield if it's a different string
825 if ($oldfield->subfield(@$subfield[0]) ne @$subfield[1] ) {
826 my $subfieldid=&MARCfindsubfieldid($dbh,$bibid,$field->tag(),$tagorder,@$subfield[0],$subfieldorder);
827 # warn "changing : $subfieldid, $bibid,".$field->tag(),",$tagorder,@$subfield[0],@$subfield[1],$subfieldorder";
828 &MARCmodsubfield($dbh,$subfieldid,@$subfield[1]);
836 sub MARCmodsubfield {
837 # Subroutine changes a subfield value given a subfieldid.
838 my ($dbh, $subfieldid, $subfieldvalue )=@_;
839 $dbh->do("lock tables marc_blob_subfield WRITE,marc_subfield_table WRITE");
840 my $sth1=$dbh->prepare("select valuebloblink from marc_subfield_table where subfieldid=?");
841 $sth1->execute($subfieldid);
842 my ($oldvaluebloblink)=$sth1->fetchrow;
845 # if too long, use a bloblink
846 if (length($subfieldvalue)>255 ) {
847 # if already a bloblink, update it, otherwise, insert a new one.
848 if ($oldvaluebloblink) {
849 $sth=$dbh->prepare("update marc_blob_subfield set subfieldvalue=? where blobidlink=?");
850 $sth->execute($subfieldvalue,$oldvaluebloblink);
852 $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
853 $sth->execute($subfieldvalue);
854 $sth=$dbh->prepare("select max(blobidlink) from marc_blob_subfield");
856 my ($res)=$sth->fetchrow;
857 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=null, valuebloblink=$res where subfieldid=?");
858 $sth->execute($subfieldid);
861 # note this can leave orphan bloblink. Not a big problem, but we should build somewhere a orphan deleting script...
862 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=?,valuebloblink=null where subfieldid=?");
863 $sth->execute($subfieldvalue, $subfieldid);
865 $dbh->do("unlock tables");
867 $sth=$dbh->prepare("select bibid,tag,tagorder,subfieldcode,subfieldid,subfieldorder from marc_subfield_table where subfieldid=?");
868 $sth->execute($subfieldid);
869 my ($bibid,$tagid,$tagorder,$subfieldcode,$x,$subfieldorder) = $sth->fetchrow;
871 &MARCdelword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder);
872 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
873 return($subfieldid, $subfieldvalue);
876 sub MARCfindsubfield {
877 my ($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue) = @_;
881 my $query="select subfieldid from marc_subfield_table where bibid=? and tag=? and subfieldcode=?";
882 if ($subfieldvalue) {
883 $query .= " and subfieldvalue=".$dbh->quote($subfieldvalue);
885 if ($subfieldorder<1) {
888 $query .= " and subfieldorder=$subfieldorder";
890 my $sti=$dbh->prepare($query);
891 $sti->execute($bibid,$tag, $subfieldcode);
892 while (($subfieldid) = $sti->fetchrow) {
894 $lastsubfieldid=$subfieldid;
896 if ($resultcounter>1) {
897 # Error condition. Values given did not resolve into a unique record. Don't know what to edit
898 # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
901 return $lastsubfieldid;
905 sub MARCfindsubfieldid {
906 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
907 my $sth=$dbh->prepare("select subfieldid from marc_subfield_table
908 where bibid=? and tag=? and tagorder=?
909 and subfieldcode=? and subfieldorder=?");
910 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
911 my ($res) = $sth->fetchrow;
913 $sth=$dbh->prepare("select subfieldid from marc_subfield_table
914 where bibid=? and tag=? and tagorder=?
915 and subfieldcode=?");
916 $sth->execute($bibid,$tag,$tagorder,$subfield);
917 ($res) = $sth->fetchrow;
922 sub MARCdelsubfield {
923 # delete a subfield for $bibid / tag / tagorder / subfield / subfieldorder
924 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
925 $dbh->do("delete from marc_subfield_table where bibid='$bibid' and
926 tag='$tag' and tagorder='$tagorder'
927 and subfieldcode='$subfield' and subfieldorder='$subfieldorder
931 sub MARCkoha2marcBiblio {
932 # this function builds partial MARC::Record from the old koha-DB fields
933 my ($dbh,$biblionumber,$biblioitemnumber) = @_;
934 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
935 my $record = MARC::Record->new();
936 #--- if bibid, then retrieve old-style koha data
937 if ($biblionumber>0) {
938 my $sth2=$dbh->prepare("select biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp
939 from biblio where biblionumber=?");
940 $sth2->execute($biblionumber);
941 my $row=$sth2->fetchrow_hashref;
943 foreach $code (keys %$row) {
945 &MARCkoha2marcOnefield($sth,$record,"biblio.".$code,$row->{$code});
949 #--- if biblioitem, then retrieve old-style koha data
950 if ($biblioitemnumber>0) {
951 my $sth2=$dbh->prepare(" SELECT biblioitemnumber,biblionumber,volume,number,classification,
952 itemtype,url,isbn,issn,dewey,subclass,publicationyear,publishercode,
953 volumedate,volumeddesc,timestamp,illus,pages,notes AS bnotes,size,place
955 WHERE biblionumber=? and biblioitemnumber=?
957 $sth2->execute($biblionumber,$biblioitemnumber);
958 my $row=$sth2->fetchrow_hashref;
960 foreach $code (keys %$row) {
962 &MARCkoha2marcOnefield($sth,$record,"biblioitems.".$code,$row->{$code});
966 # other fields => additional authors, subjects, subtitles
967 my $sth2=$dbh->prepare(" SELECT author FROM additionalauthors WHERE biblionumber=?");
968 $sth2->execute($biblionumber);
969 while (my $row=$sth2->fetchrow_hashref) {
970 &MARCkoha2marcOnefield($sth,$record,"additionalauthors.author",$row->{'author'});
972 my $sth2=$dbh->prepare(" SELECT subject FROM bibliosubject WHERE biblionumber=?");
973 $sth2->execute($biblionumber);
974 while (my $row=$sth2->fetchrow_hashref) {
975 &MARCkoha2marcOnefield($sth,$record,"bibliosubject.subject",$row->{'subject'});
977 my $sth2=$dbh->prepare(" SELECT subtitle FROM bibliosubtitle WHERE biblionumber=?");
978 $sth2->execute($biblionumber);
979 while (my $row=$sth2->fetchrow_hashref) {
980 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.title",$row->{'subtitle'});
985 sub MARCkoha2marcItem {
986 # this function builds partial MARC::Record from the old koha-DB fields
987 my ($dbh,$biblionumber,$itemnumber) = @_;
988 # my $dbh=&C4Connect;
989 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
990 my $record = MARC::Record->new();
991 #--- if item, then retrieve old-style koha data
993 # print STDERR "prepare $biblionumber,$itemnumber\n";
994 my $sth2=$dbh->prepare("SELECT itemnumber,biblionumber,multivolumepart,biblioitemnumber,barcode,dateaccessioned,
995 booksellerid,homebranch,price,replacementprice,replacementpricedate,datelastborrowed,
996 datelastseen,multivolume,stack,notforloan,itemlost,wthdrawn,bulk,issues,renewals,
997 reserves,restricted,binding,itemnotes,holdingbranch,timestamp
999 WHERE itemnumber=?");
1000 $sth2->execute($itemnumber);
1001 my $row=$sth2->fetchrow_hashref;
1003 foreach $code (keys %$row) {
1004 if ($row->{$code}) {
1005 &MARCkoha2marcOnefield($sth,$record,"items.".$code,$row->{$code});
1012 sub MARCkoha2marcSubtitle {
1013 # this function builds partial MARC::Record from the old koha-DB fields
1014 my ($dbh,$bibnum,$subtitle) = @_;
1015 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1016 my $record = MARC::Record->new();
1017 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.subtitle",$subtitle);
1021 sub MARCkoha2marcOnefield {
1022 my ($sth,$record,$kohafieldname,$value)=@_;
1025 $sth->execute($kohafieldname);
1026 if (($tagfield,$tagsubfield)=$sth->fetchrow) {
1027 if ($record->field($tagfield)) {
1028 my $tag =$record->field($tagfield);
1030 $tag->add_subfields($tagsubfield,$value);
1031 $record->delete_field($tag);
1032 $record->add_fields($tag);
1035 $record->add_fields($tagfield," "," ",$tagsubfield => $value);
1042 my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
1044 my $record = MARC::Record->new();
1045 # my %subfieldlist=();
1046 my $prevvalue; # if tag <10
1047 my $field; # if tag >=10
1048 for (my $i=0; $i< @$rtags; $i++) {
1049 # rebuild MARC::Record
1050 if (@$rtags[$i] ne $prevtag) {
1051 if ($prevtag < 10) {
1053 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
1057 $record->add_fields($field);
1060 $indicators{@$rtags[$i]}.=' ';
1061 if (@$rtags[$i] <10) {
1062 $prevvalue= @$rvalues[$i];
1064 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
1066 $prevtag = @$rtags[$i];
1068 if (@$rtags[$i] <10) {
1069 $prevvalue=@$rvalues[$i];
1071 if (@$rvalues[$i]) {
1072 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
1075 $prevtag= @$rtags[$i];
1078 # the last has not been included inside the loop... do it now !
1079 $record->add_fields($field);
1080 # warn $record->as_formatted;
1085 my ($dbh,$record) = @_;
1086 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1088 my $sth2=$dbh->prepare("SHOW COLUMNS from biblio");
1091 # print STDERR $record->as_formatted;
1092 while (($field)=$sth2->fetchrow) {
1093 $result=&MARCmarc2kohaOneField($sth,"biblio",$field,$record,$result);
1095 $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
1097 while (($field)=$sth2->fetchrow) {
1098 $result=&MARCmarc2kohaOneField($sth,"biblioitems",$field,$record,$result);
1100 $sth2=$dbh->prepare("SHOW COLUMNS from items");
1102 while (($field)=$sth2->fetchrow) {
1103 $result = &MARCmarc2kohaOneField($sth,"items",$field,$record,$result);
1105 # additional authors : specific
1106 $result = &MARCmarc2kohaOneField($sth,"additionalauthors","additionalauthors",$record,$result);
1107 # modify copyrightdate to keep only the 1st year found
1108 my $temp = $result->{'copyrightdate'};
1109 $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
1111 $result->{'copyrightdate'} = $1;
1112 } else { # if no cYYYY, get the 1st date.
1113 $temp =~ m/(\d\d\d\d)/;
1114 $result->{'copyrightdate'} = $1;
1119 sub MARCmarc2kohaOneField {
1120 # FIXME ? if a field has a repeatable subfield that is used in old-db, only the 1st will be retrieved...
1121 my ($sth,$kohatable,$kohafield,$record,$result)= @_;
1122 # warn "kohatable / $kohafield / $result / ";
1126 $sth->execute($kohatable.".".$kohafield);
1127 ($tagfield,$subfield) = $sth->fetchrow;
1128 foreach my $field ($record->field($tagfield)) {
1129 if ($field->subfield($subfield)) {
1130 if ($result->{$kohafield}) {
1131 $result->{$kohafield} .= " | ".$field->subfield($subfield);
1133 $result->{$kohafield}=$field->subfield($subfield);
1141 # split a subfield string and adds it into the word table.
1143 my ($dbh,$bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$sentence) =@_;
1144 $sentence =~ s/(\.|\?|\:|\!|\'|,|\-)/ /g;
1145 my @words = split / /,$sentence;
1146 my $stopwords= C4::Context->stopwords;
1147 my $sth=$dbh->prepare("insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word)
1148 values (?,?,?,?,?,?,soundex(?))");
1149 foreach my $word (@words) {
1150 # we record only words longer than 2 car and not in stopwords hash
1151 if (length($word)>1 and !($stopwords->{uc($word)})) {
1152 $sth->execute($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,$word);
1154 warn "ERROR ==> insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word) values ($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,soundex($word))\n";
1161 # delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
1162 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
1163 my $sth=$dbh->prepare("delete from marc_word where bibid=? and tag=? and tagorder=? and subfieldid=? and subfieldorder=?");
1164 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
1169 # NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
1172 # all the following subs are useful to manage MARC-DB with complete MARC records.
1173 # it's used with marcimport, and marc management tools
1177 =item ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbibilio($dbh,$MARCRecord,$oldbiblio,$oldbiblioitem);
1179 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
1180 are builded from the MARC::Record. If they are passed, they are used.
1182 =item NEWnewitem($dbh, $record,$bibid);
1184 adds an item in the db.
1189 my ($dbh, $record, $oldbiblio, $oldbiblioitem) = @_;
1190 # note $oldbiblio and $oldbiblioitem are not mandatory.
1191 # if not present, they will be builded from $record with MARCmarc2koha function
1192 if (($oldbiblio) and not($oldbiblioitem)) {
1193 print STDERR "NEWnewbiblio : missing parameter\n";
1194 print "NEWnewbiblio : missing parameter : contact koha development team\n";
1200 $oldbibnum = OLDnewbiblio($dbh,$oldbiblio);
1201 $oldbiblioitem->{'biblionumber'} = $oldbibnum;
1202 $oldbibitemnum = OLDnewbiblioitem($dbh,$oldbiblioitem);
1204 my $olddata = MARCmarc2koha($dbh,$record);
1205 $oldbibnum = OLDnewbiblio($dbh,$olddata);
1206 $olddata->{'biblionumber'} = $oldbibnum;
1207 $oldbibitemnum = OLDnewbiblioitem($dbh,$olddata);
1209 # search subtiles, addiauthors and subjects
1210 my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author");
1211 my @addiauthfields = $record->field($tagfield);
1212 foreach my $addiauthfield (@addiauthfields) {
1213 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1214 foreach my $subfieldcount (0..$#addiauthsubfields) {
1215 OLDmodaddauthor($dbh,$oldbibnum,$addiauthsubfields[$subfieldcount]);
1218 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle");
1219 my @subtitlefields = $record->field($tagfield);
1220 foreach my $subtitlefield (@subtitlefields) {
1221 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1222 foreach my $subfieldcount (0..$#subtitlesubfields) {
1223 OLDnewsubtitle($dbh,$oldbibnum,$subtitlesubfields[$subfieldcount]);
1226 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject");
1227 my @subj = $record->field($tagfield);
1228 foreach my $subject (@subj) {
1229 my @subjsubfield = $subject->subfield($tagsubfield);
1231 foreach my $subfieldcount (0..$#subjsubfield) {
1232 push @subjects,$subjsubfield[$subfieldcount];
1234 OLDmodsubject($dbh,$oldbibnum,1,@subjects);
1236 # we must add bibnum and bibitemnum in MARC::Record...
1237 # we build the new field with biblionumber and biblioitemnumber
1238 # we drop the original field
1239 # we add the new builded field.
1240 # NOTE : Works only if the field is ONLY for biblionumber and biblioitemnumber
1241 # (steve and paul : thinks 090 is a good choice)
1242 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1243 $sth->execute("biblio.biblionumber");
1244 (my $tagfield1, my $tagsubfield1) = $sth->fetchrow;
1245 $sth->execute("biblioitems.biblioitemnumber");
1246 (my $tagfield2, my $tagsubfield2) = $sth->fetchrow;
1247 if ($tagfield1 != $tagfield2) {
1248 warn "Error in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1249 print "Content-Type: text/html\n\nError in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1252 my $newfield = MARC::Field->new( $tagfield1,'','',
1253 "$tagsubfield1" => $oldbibnum,
1254 "$tagsubfield2" => $oldbibitemnum);
1255 # drop old field and create new one...
1256 my $old_field = $record->field($tagfield1);
1257 $record->delete_field($old_field);
1258 $record->add_fields($newfield);
1259 my $bibid = MARCaddbiblio($dbh,$record,$oldbibnum);
1260 return ($bibid,$oldbibnum,$oldbibitemnum );
1264 my ($dbh,$record,$bibid) =@_;
1265 &MARCmodbiblio($dbh,$bibid,$record,0);
1266 my $oldbiblio = MARCmarc2koha($dbh,$record);
1267 my $oldbiblionumber = OLDmodbiblio($dbh,$oldbiblio);
1268 OLDmodbibitem($dbh,$oldbiblio);
1269 # now, modify addi authors, subject, addititles.
1270 my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author");
1271 my @addiauthfields = $record->field($tagfield);
1272 foreach my $addiauthfield (@addiauthfields) {
1273 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1274 foreach my $subfieldcount (0..$#addiauthsubfields) {
1275 OLDmodaddauthor($dbh,$oldbiblionumber,$addiauthsubfields[$subfieldcount]);
1278 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle");
1279 my @subtitlefields = $record->field($tagfield);
1280 foreach my $subtitlefield (@subtitlefields) {
1281 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1282 foreach my $subfieldcount (0..$#subtitlesubfields) {
1283 OLDmodsubtitle($dbh,$oldbiblionumber,$subtitlesubfields[$subfieldcount]);
1286 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject");
1287 my @subj = $record->field($tagfield);
1288 foreach my $subject (@subj) {
1289 my @subjsubfield = $subject->subfield($tagsubfield);
1291 foreach my $subfieldcount (0..$#subjsubfield) {
1292 push @subjects,$subjsubfield[$subfieldcount];
1294 OLDmodsubject($dbh,$oldbiblionumber,1,@subjects);
1301 my ($dbh, $record,$bibid) = @_;
1302 # add item in old-DB
1303 my $item = &MARCmarc2koha($dbh,$record);
1304 # needs old biblionumber and biblioitemnumber
1305 $item->{'biblionumber'} = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
1306 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
1307 $sth->execute($item->{'biblionumber'});
1308 ($item->{'biblioitemnumber'}) = $sth->fetchrow;
1309 my ($itemnumber,$error) = &OLDnewitems($dbh,$item,$item->{barcode});
1310 # add itemnumber to MARC::Record before adding the item.
1311 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1312 &MARCkoha2marcOnefield($sth,$record,"items.itemnumber",$itemnumber);
1314 my $bib = &MARCadditem($dbh,$record,$item->{'biblionumber'});
1318 my ($dbh,$record,$bibid,$itemnumber,$delete) = @_;
1319 &MARCmoditem($dbh,$record,$bibid,$itemnumber,$delete);
1320 my $olditem = MARCmarc2koha($dbh,$record);
1321 OLDmoditem($dbh,$olditem);
1326 # OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
1330 =item $biblionumber = OLDnewbiblio($dbh,$biblio);
1332 adds a record in biblio table. Datas are in the hash $biblio.
1334 =item $biblionumber = OLDmodbiblio($dbh,$biblio);
1336 modify a record in biblio table. Datas are in the hash $biblio.
1338 =item OLDmodsubtitle($dbh,$bibnum,$subtitle);
1340 modify subtitles in bibliosubtitle table.
1342 =item OLDmodaddauthor($dbh,$bibnum,$author);
1344 adds or modify additional authors
1345 NOTE : Strange sub : seems to delete MANY and add only ONE author... maybe buggy ?
1347 =item $errors = OLDmodsubject($dbh,$bibnum, $force, @subject);
1349 modify/adds subjects
1351 =item OLDmodbibitem($dbh, $biblioitem);
1355 =item OLDmodnote($dbh,$bibitemnum,$note
1357 modify a note for a biblioitem
1359 =item OLDnewbiblioitem($dbh,$biblioitem);
1361 adds a biblioitem ($biblioitem is a hash with the values)
1363 =item OLDnewsubject($dbh,$bibnum);
1367 =item OLDnewsubtitle($dbh,$bibnum,$subtitle);
1369 create a new subtitle
1371 =item ($itemnumber,$errors)= OLDnewitems($dbh,$item,$barcode);
1373 create a item. $item is a hash and $barcode the barcode.
1375 =item OLDmoditem($dbh,$item);
1379 =item OLDdelitem($dbh,$itemnum);
1383 =item OLDdeletebiblioitem($dbh,$biblioitemnumber);
1385 deletes a biblioitem
1386 NOTE : not standard sub name. Should be OLDdelbiblioitem()
1388 =item OLDdelbiblio($dbh,$biblio);
1395 my ($dbh,$biblio) = @_;
1396 # my $dbh = &C4Connect;
1397 my $query = "Select max(biblionumber) from biblio";
1398 my $sth = $dbh->prepare($query);
1400 my $data = $sth->fetchrow_arrayref;
1401 my $bibnum = $$data[0] + 1;
1404 if ($biblio->{'seriestitle'}) { $series = 1 };
1406 $query = "insert into biblio set biblionumber = ?, title = ?, author = ?, copyrightdate = ?,
1407 serial = ?, seriestitle = ?, notes = ?, abstract = ?";
1408 $sth = $dbh->prepare($query);
1409 $sth->execute($bibnum,$biblio->{'title'},$biblio->{'author'},$biblio->{'copyright'},$series,$biblio->{'seriestitle'},$biblio->{'notes'},$biblio->{'abstract'});
1417 my ($dbh,$biblio) = @_;
1418 # my $dbh = C4Connect;
1422 $query = "Update biblio set title = ?, author = ?, abstract = ?, copyrightdate = ?,
1423 seriestitle = ?, serial = ?, unititle = ?, notes = ? where biblionumber = ?";
1424 $sth = $dbh->prepare($query);
1425 $sth->execute($biblio->{'title'},$biblio->{'author'},$biblio->{'abstract'},$biblio->{'copyrightdate'},
1426 $biblio->{'seriestitle'},$biblio->{'serial'},$biblio->{'unititle'},$biblio->{'notes'},$biblio->{'biblionumber'});
1429 return($biblio->{'biblionumber'});
1432 sub OLDmodsubtitle {
1433 my ($dbh,$bibnum, $subtitle) = @_;
1434 my $query = "update bibliosubtitle set subtitle = ? where biblionumber = ?";
1435 my $sth = $dbh->prepare($query);
1436 $sth->execute($subtitle,$bibnum);
1441 sub OLDmodaddauthor {
1442 my ($dbh,$bibnum, $author) = @_;
1443 # my $dbh = C4Connect;
1444 my $query = "Delete from additionalauthors where biblionumber = $bibnum";
1445 my $sth = $dbh->prepare($query);
1450 if ($author ne '') {
1451 $query = "Insert into additionalauthors set
1454 $sth = $dbh->prepare($query);
1456 $sth->execute($author,$bibnum);
1460 } # sub modaddauthor
1464 my ($dbh,$bibnum, $force, @subject) = @_;
1465 # my $dbh = C4Connect;
1466 my $count = @subject;
1468 for (my $i = 0; $i < $count; $i++) {
1469 $subject[$i] =~ s/^ //g;
1470 $subject[$i] =~ s/ $//g;
1471 my $query = "select * from catalogueentry where entrytype = 's' and catalogueentry = '$subject[$i]'";
1472 my $sth = $dbh->prepare($query);
1475 if (my $data = $sth->fetchrow_hashref) {
1477 if ($force eq $subject[$i] || $force eq 1) {
1478 # subject not in aut, chosen to force anway
1479 # so insert into cataloguentry so its in auth file
1480 $query = "Insert into catalogueentry (entrytype,catalogueentry) values ('s','$subject[$i]')";
1481 my $sth2 = $dbh->prepare($query);
1486 $error = "$subject[$i]\n does not exist in the subject authority file";
1487 $query = "Select * from catalogueentry where entrytype = 's' and (catalogueentry like '$subject[$i] %'
1488 or catalogueentry like '% $subject[$i] %' or catalogueentry like '% $subject[$i]')";
1489 my $sth2 = $dbh->prepare($query);
1491 while (my $data = $sth2->fetchrow_hashref) {
1492 $error .= "<br>$data->{'catalogueentry'}";
1500 my $query = "Delete from bibliosubject where biblionumber = $bibnum";
1501 my $sth = $dbh->prepare($query);
1504 for (my $i = 0; $i < $count; $i++) {
1505 $sth = $dbh->prepare("Insert into bibliosubject values ('$subject[$i]', $bibnum)");
1516 my ($dbh,$biblioitem) = @_;
1517 # my $dbh = C4Connect;
1520 $biblioitem->{'itemtype'} = $dbh->quote($biblioitem->{'itemtype'});
1521 $biblioitem->{'url'} = $dbh->quote($biblioitem->{'url'});
1522 $biblioitem->{'isbn'} = $dbh->quote($biblioitem->{'isbn'});
1523 $biblioitem->{'publishercode'} = $dbh->quote($biblioitem->{'publishercode'});
1524 $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
1525 $biblioitem->{'classification'} = $dbh->quote($biblioitem->{'classification'});
1526 $biblioitem->{'dewey'} = $dbh->quote($biblioitem->{'dewey'});
1527 $biblioitem->{'subclass'} = $dbh->quote($biblioitem->{'subclass'});
1528 $biblioitem->{'illus'} = $dbh->quote($biblioitem->{'illus'});
1529 $biblioitem->{'pages'} = $dbh->quote($biblioitem->{'pages'});
1530 $biblioitem->{'volumeddesc'} = $dbh->quote($biblioitem->{'volumeddesc'});
1531 $biblioitem->{'bnotes'} = $dbh->quote($biblioitem->{'bnotes'});
1532 $biblioitem->{'size'} = $dbh->quote($biblioitem->{'size'});
1533 $biblioitem->{'place'} = $dbh->quote($biblioitem->{'place'});
1535 $query = "Update biblioitems set
1536 itemtype = $biblioitem->{'itemtype'},
1537 url = $biblioitem->{'url'},
1538 isbn = $biblioitem->{'isbn'},
1539 publishercode = $biblioitem->{'publishercode'},
1540 publicationyear = $biblioitem->{'publicationyear'},
1541 classification = $biblioitem->{'classification'},
1542 dewey = $biblioitem->{'dewey'},
1543 subclass = $biblioitem->{'subclass'},
1544 illus = $biblioitem->{'illus'},
1545 pages = $biblioitem->{'pages'},
1546 volumeddesc = $biblioitem->{'volumeddesc'},
1547 notes = $biblioitem->{'bnotes'},
1548 size = $biblioitem->{'size'},
1549 place = $biblioitem->{'place'}
1550 where biblioitemnumber = $biblioitem->{'biblioitemnumber'}";
1558 my ($dbh,$bibitemnum,$note)=@_;
1559 # my $dbh=C4Connect;
1560 my $query="update biblioitems set notes='$note' where
1561 biblioitemnumber='$bibitemnum'";
1562 my $sth=$dbh->prepare($query);
1568 sub OLDnewbiblioitem {
1569 my ($dbh,$biblioitem) = @_;
1570 # my $dbh = C4Connect;
1571 my $query = "Select max(biblioitemnumber) from biblioitems";
1572 my $sth = $dbh->prepare($query);
1577 $data = $sth->fetchrow_arrayref;
1578 $bibitemnum = $$data[0] + 1;
1582 $sth = $dbh->prepare("insert into biblioitems set
1583 biblioitemnumber = ?, biblionumber = ?,
1584 volume = ?, number = ?,
1585 classification = ?, itemtype = ?,
1587 issn = ?, dewey = ?,
1588 subclass = ?, publicationyear = ?,
1589 publishercode = ?, volumedate = ?,
1590 volumeddesc = ?, illus = ?,
1591 pages = ?, notes = ?,
1593 marc = ?, place = ?");
1594 $sth->execute($bibitemnum, $biblioitem->{'biblionumber'},
1595 $biblioitem->{'volume'}, $biblioitem->{'number'},
1596 $biblioitem->{'classification'}, $biblioitem->{'itemtype'},
1597 $biblioitem->{'url'}, $biblioitem->{'isbn'},
1598 $biblioitem->{'issn'}, $biblioitem->{'dewey'},
1599 $biblioitem->{'subclass'}, $biblioitem->{'publicationyear'},
1600 $biblioitem->{'publishercode'}, $biblioitem->{'volumedate'},
1601 $biblioitem->{'volumeddesc'}, $biblioitem->{'illus'},
1602 $biblioitem->{'pages'}, $biblioitem->{'binotes'},
1603 $biblioitem->{'size'}, $biblioitem->{'lccn'},
1604 $biblioitem->{'marc'}, $biblioitem->{'place'});
1607 return($bibitemnum);
1611 my ($dbh,$bibnum)=@_;
1612 my $query="insert into bibliosubject (biblionumber) values ($bibnum)";
1613 my $sth=$dbh->prepare($query);
1618 sub OLDnewsubtitle {
1619 my ($dbh,$bibnum, $subtitle) = @_;
1620 my $query = "insert into bibliosubtitle set biblionumber = ?, subtitle = ?";
1621 my $sth = $dbh->prepare($query);
1622 $sth->execute($bibnum,$subtitle);
1628 my ($dbh,$item, $barcode) = @_;
1629 # my $dbh = C4Connect;
1630 my $query = "Select max(itemnumber) from items";
1631 my $sth = $dbh->prepare($query);
1637 $data = $sth->fetchrow_hashref;
1638 $itemnumber = $data->{'max(itemnumber)'} + 1;
1640 # if dateaccessioned is provided, use it. Otherwise, set to NOW()
1641 if ($item->{'dateaccessioned'}) {
1642 $sth=$dbh->prepare("Insert into items set
1643 itemnumber = ?, biblionumber = ?,
1644 biblioitemnumber = ?, barcode = ?,
1645 booksellerid = ?, dateaccessioned = ?,
1646 homebranch = ?, holdingbranch = ?,
1647 price = ?, replacementprice = ?,
1648 replacementpricedate = NOW(), itemnotes = ?,
1651 $sth->execute($itemnumber, $item->{'biblionumber'},
1652 $item->{'biblioitemnumber'},$barcode,
1653 $item->{'booksellerid'},$item->{'dateaccessioned'},
1654 $item->{'homebranch'},$item->{'holdingbranch'},
1655 $item->{'price'},$item->{'replacementprice'},
1656 $item->{'itemnotes'},$item->{'loan'});
1658 $sth=$dbh->prepare("Insert into items set
1659 itemnumber = ?, biblionumber = ?,
1660 biblioitemnumber = ?, barcode = ?,
1661 booksellerid = ?, dateaccessioned = NOW(),
1662 homebranch = ?, holdingbranch = ?,
1663 price = ?, replacementprice = ?,
1664 replacementpricedate = NOW(), itemnotes = ?,
1667 $sth->execute($itemnumber, $item->{'biblionumber'},
1668 $item->{'biblioitemnumber'},$barcode,
1669 $item->{'booksellerid'},
1670 $item->{'homebranch'},$item->{'holdingbranch'},
1671 $item->{'price'},$item->{'replacementprice'},
1672 $item->{'itemnotes'},$item->{'loan'});
1674 if (defined $sth->errstr) {
1675 $error .= $sth->errstr;
1678 return($itemnumber,$error);
1682 my ($dbh,$item) = @_;
1683 # my ($dbh,$loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
1684 # my $dbh=C4Connect;
1685 $item->{'itemnum'}=$item->{'itemnumber'} unless $item->{'itemnum'};
1686 my $query="update items set barcode='$item->{'barcode'}',itemnotes='$item->{'notes'}'
1687 where itemnumber=$item->{'itemnum'}";
1688 if ($item->{'barcode'} eq ''){
1689 $query="update items set notforloan=$item->{'loan'} where itemnumber=$item->{'itemnum'}";
1691 if ($item->{'lost'} ne ''){
1692 $query="update items set biblioitemnumber=$item->{'bibitemnum'},
1693 barcode='$item->{'barcode'}',
1694 itemnotes='$item->{'notes'}',
1695 homebranch='$item->{'homebranch'}',
1696 itemlost='$item->{'lost'}',
1697 wthdrawn='$item->{'wthdrawn'}'
1698 where itemnumber=$item->{'itemnum'}";
1700 if ($item->{'replacement'} ne ''){
1701 $query=~ s/ where/,replacementprice='$item->{'replacement'}' where/;
1703 my $sth=$dbh->prepare($query);
1710 my ($dbh,$itemnum)=@_;
1711 # my $dbh=C4Connect;
1712 my $query="select * from items where itemnumber=$itemnum";
1713 my $sth=$dbh->prepare($query);
1715 my @data=$sth->fetchrow_array;
1717 $query="Insert into deleteditems values (";
1718 foreach my $temp (@data){
1719 $query .= "'$temp',";
1723 $sth=$dbh->prepare($query);
1726 $query = "Delete from items where itemnumber=$itemnum";
1727 $sth=$dbh->prepare($query);
1733 sub OLDdeletebiblioitem {
1734 my ($dbh,$biblioitemnumber) = @_;
1735 # my $dbh = C4Connect;
1736 my $query = "Select * from biblioitems
1737 where biblioitemnumber = $biblioitemnumber";
1738 my $sth = $dbh->prepare($query);
1743 if (@results = $sth->fetchrow_array) {
1744 $query = "Insert into deletedbiblioitems values (";
1745 foreach my $value (@results) {
1746 $value = $dbh->quote($value);
1747 $query .= "$value,";
1750 $query =~ s/\,$/\)/;
1753 $query = "Delete from biblioitems
1754 where biblioitemnumber = $biblioitemnumber";
1758 # Now delete all the items attached to the biblioitem
1759 $query = "Select * from items where biblioitemnumber = $biblioitemnumber";
1760 $sth = $dbh->prepare($query);
1762 while (@results = $sth->fetchrow_array) {
1763 $query = "Insert into deleteditems values (";
1764 foreach my $value (@results) {
1765 $value = $dbh->quote($value);
1766 $query .= "$value,";
1768 $query =~ s/\,$/\)/;
1772 $query = "Delete from items where biblioitemnumber = $biblioitemnumber";
1775 } # sub deletebiblioitem
1778 my ($dbh,$biblio)=@_;
1779 # my $dbh=C4Connect;
1780 my $query="select * from biblio where biblionumber=$biblio";
1781 my $sth=$dbh->prepare($query);
1783 if (my @data=$sth->fetchrow_array){
1785 $query="Insert into deletedbiblio values (";
1786 foreach my $temp (@data){
1787 $temp=~ s/\'/\\\'/g;
1788 $query .= "'$temp',";
1792 $sth=$dbh->prepare($query);
1795 $query = "Delete from biblio where biblionumber=$biblio";
1796 $sth=$dbh->prepare($query);
1812 my $dbh = C4::Context->dbh;
1813 my $query="Select count(*) from items where biblionumber=$biblio";
1815 my $sth=$dbh->prepare($query);
1817 my $data=$sth->fetchrow_hashref;
1819 return($data->{'count(*)'});
1824 ($order, $ordernumber) = &getorder($biblioitemnumber, $biblionumber);
1826 Looks up the order with the given biblionumber and biblioitemnumber.
1828 Returns a two-element array. C<$ordernumber> is the order number.
1829 C<$order> is a reference-to-hash describing the order; its keys are
1830 fields from the biblio, biblioitems, aqorders, and aqorderbreakdown
1831 tables of the Koha database.
1835 # FIXME - This is effectively identical to &C4::Catalogue::getorder.
1836 # Pick one and stick with it.
1839 my $dbh = C4::Context->dbh;
1840 my $query="Select ordernumber
1842 where biblionumber=? and biblioitemnumber=?";
1843 my $sth=$dbh->prepare($query);
1844 $sth->execute($bib,$bi);
1845 # FIXME - Use fetchrow_array(), since we're only interested in the one
1847 my $ordnum=$sth->fetchrow_hashref;
1849 my $order=getsingleorder($ordnum->{'ordernumber'});
1851 return ($order,$ordnum->{'ordernumber'});
1854 =item getsingleorder
1856 $order = &getsingleorder($ordernumber);
1858 Looks up an order by order number.
1860 Returns a reference-to-hash describing the order. The keys of
1861 C<$order> are fields from the biblio, biblioitems, aqorders, and
1862 aqorderbreakdown tables of the Koha database.
1866 # FIXME - This is effectively identical to
1867 # &C4::Catalogue::getsingleorder.
1868 # Pick one and stick with it.
1869 sub getsingleorder {
1871 my $dbh = C4::Context->dbh;
1872 my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown
1873 where aqorders.ordernumber=?
1874 and biblio.biblionumber=aqorders.biblionumber and
1875 biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
1876 aqorders.ordernumber=aqorderbreakdown.ordernumber";
1877 my $sth=$dbh->prepare($query);
1878 $sth->execute($ordnum);
1879 my $data=$sth->fetchrow_hashref;
1886 my $dbh = C4::Context->dbh;
1887 my $bibnum=OLDnewbiblio($dbh,$biblio);
1888 # finds new (MARC bibid
1889 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$bibnum);
1890 my $record = &MARCkoha2marcBiblio($dbh,$bibnum);
1891 MARCaddbiblio($dbh,$record,$bibnum);
1898 $biblionumber = &modbiblio($biblio);
1900 Update a biblio record.
1902 C<$biblio> is a reference-to-hash whose keys are the fields in the
1903 biblio table in the Koha database. All fields must be present, not
1904 just the ones you wish to change.
1906 C<&modbiblio> updates the record defined by
1907 C<$biblio-E<gt>{biblionumber}> with the values in C<$biblio>.
1909 C<&modbiblio> returns C<$biblio-E<gt>{biblionumber}> whether it was
1916 my $dbh = C4::Context->dbh;
1917 my $biblionumber=OLDmodbiblio($dbh,$biblio);
1918 my $record = MARCkoha2marcBiblio($dbh,$biblionumber);
1919 # finds new (MARC bibid
1920 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
1921 MARCmodbiblio($dbh,$bibid,$record,0);
1922 return($biblionumber);
1927 &modsubtitle($biblionumber, $subtitle);
1929 Sets the subtitle of a book.
1931 C<$biblionumber> is the biblionumber of the book to modify.
1933 C<$subtitle> is the new subtitle.
1938 my ($bibnum, $subtitle) = @_;
1939 my $dbh = C4::Context->dbh;
1940 &OLDmodsubtitle($dbh,$bibnum,$subtitle);
1945 &modaddauthor($biblionumber, $author);
1947 Replaces all additional authors for the book with biblio number
1948 C<$biblionumber> with C<$author>. If C<$author> is the empty string,
1949 C<&modaddauthor> deletes all additional authors.
1954 my ($bibnum, $author) = @_;
1955 my $dbh = C4::Context->dbh;
1956 &OLDmodaddauthor($dbh,$bibnum,$author);
1957 } # sub modaddauthor
1961 $error = &modsubject($biblionumber, $force, @subjects);
1963 $force - a subject to force
1965 $error - Error message, or undef if successful.
1970 my ($bibnum, $force, @subject) = @_;
1971 my $dbh = C4::Context->dbh;
1972 my $error= &OLDmodsubject($dbh,$bibnum,$force, @subject);
1977 my ($biblioitem) = @_;
1978 my $dbh = C4::Context->dbh;
1979 &OLDmodbibitem($dbh,$biblioitem);
1980 my $MARCbibitem = MARCkoha2marcBiblio($dbh,$biblioitem);
1981 &MARCmodbiblio($dbh,$biblioitem->{biblionumber},$MARCbibitem,0);
1985 my ($bibitemnum,$note)=@_;
1986 my $dbh = C4::Context->dbh;
1987 &OLDmodnote($dbh,$bibitemnum,$note);
1991 my ($biblioitem) = @_;
1992 my $dbh = C4::Context->dbh;
1993 my $bibitemnum = &OLDnewbiblioitem($dbh,$biblioitem);
1994 # print STDERR "bibitemnum : $bibitemnum\n";
1995 my $MARCbiblio= MARCkoha2marcBiblio($dbh,$biblioitem->{biblionumber},$bibitemnum);
1996 # print STDERR $MARCbiblio->as_formatted();
1997 &MARCaddbiblio($dbh,$MARCbiblio,$biblioitem->{biblionumber});
1998 return($bibitemnum);
2003 my $dbh = C4::Context->dbh;
2004 &OLDnewsubject($dbh,$bibnum);
2008 my ($bibnum, $subtitle) = @_;
2009 my $dbh = C4::Context->dbh;
2010 &OLDnewsubtitle($dbh,$bibnum,$subtitle);
2014 my ($item, @barcodes) = @_;
2015 my $dbh = C4::Context->dbh;
2019 foreach my $barcode (@barcodes) {
2020 ($itemnumber,$error)=&OLDnewitems($dbh,$item,uc($barcode));
2022 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{biblionumber},$itemnumber);
2023 &MARCadditem($dbh,$MARCitem,$item->{biblionumber});
2030 my $dbh = C4::Context->dbh;
2031 &OLDmoditem($dbh,$item);
2032 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{'biblionumber'},$item->{'itemnum'});
2033 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$item->{biblionumber});
2034 &MARCmoditem($dbh,$MARCitem,$bibid,$item->{itemnum},0);
2038 my ($count,@barcodes)=@_;
2039 my $dbh = C4::Context->dbh;
2041 for (my $i=0;$i<$count;$i++){
2042 $barcodes[$i]=uc $barcodes[$i];
2043 my $query="Select * from items where barcode='$barcodes[$i]'";
2044 my $sth=$dbh->prepare($query);
2046 if (my $data=$sth->fetchrow_hashref){
2047 $error.=" Duplicate Barcode: $barcodes[$i]";
2055 my ($bibitemnum)=@_;
2056 my $dbh = C4::Context->dbh;
2057 my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
2058 my $sth=$dbh->prepare($query);
2060 my $data=$sth->fetchrow_hashref;
2062 return($data->{'count(*)'});
2067 my $dbh = C4::Context->dbh;
2068 &OLDdelitem($dbh,$itemnum);
2071 sub deletebiblioitem {
2072 my ($biblioitemnumber) = @_;
2073 my $dbh = C4::Context->dbh;
2074 &OLDdeletebiblioitem($dbh,$biblioitemnumber);
2075 } # sub deletebiblioitem
2080 my $dbh = C4::Context->dbh;
2081 &OLDdelbiblio($dbh,$biblio);
2085 my $dbh = C4::Context->dbh;
2086 my $query = "select * from itemtypes order by description";
2087 my $sth = $dbh->prepare($query);
2088 # || die "Cannot prepare $query" . $dbh->errstr;
2093 # || die "Cannot execute $query\n" . $sth->errstr;
2094 while (my $data = $sth->fetchrow_hashref) {
2095 $results[$count] = $data;
2100 return($count, @results);
2101 } # sub getitemtypes
2104 my ($biblionumber) = @_;
2105 my $dbh = C4::Context->dbh;
2106 my $query = "Select * from biblio where biblionumber = $biblionumber";
2107 my $sth = $dbh->prepare($query);
2108 # || die "Cannot prepare $query\n" . $dbh->errstr;
2113 # || die "Cannot execute $query\n" . $sth->errstr;
2114 while (my $data = $sth->fetchrow_hashref) {
2115 $results[$count] = $data;
2120 return($count, @results);
2124 my ($biblioitemnum) = @_;
2125 my $dbh = C4::Context->dbh;
2126 my $query = "Select * from biblioitems where
2127 biblioitemnumber = $biblioitemnum";
2128 my $sth = $dbh->prepare($query);
2134 while (my $data = $sth->fetchrow_hashref) {
2135 $results[$count] = $data;
2140 return($count, @results);
2141 } # sub getbiblioitem
2143 sub getbiblioitembybiblionumber {
2144 my ($biblionumber) = @_;
2145 my $dbh = C4::Context->dbh;
2146 my $query = "Select * from biblioitems where biblionumber =
2148 my $sth = $dbh->prepare($query);
2154 while (my $data = $sth->fetchrow_hashref) {
2155 $results[$count] = $data;
2160 return($count, @results);
2163 sub getitemsbybiblioitem {
2164 my ($biblioitemnum) = @_;
2165 my $dbh = C4::Context->dbh;
2166 my $query = "Select * from items, biblio where
2167 biblio.biblionumber = items.biblionumber and biblioitemnumber
2169 my $sth = $dbh->prepare($query);
2170 # || die "Cannot prepare $query\n" . $dbh->errstr;
2175 # || die "Cannot execute $query\n" . $sth->errstr;
2176 while (my $data = $sth->fetchrow_hashref) {
2177 $results[$count] = $data;
2182 return($count, @results);
2183 } # sub getitemsbybiblioitem
2187 # Subroutine to log changes to databases
2188 # Eventually, this subroutine will be used to create a log of all changes made,
2189 # with the possibility of "undo"ing some changes
2191 if ($database eq 'kohadb') {
2197 # print STDERR "KOHA: $type $section $item $original $new\n";
2198 } elsif ($database eq 'marc') {
2200 my $Record_ID=shift;
2203 my $subfield_ID=shift;
2206 # print STDERR "MARC: $type $Record_ID $tag $mark $subfield_ID $original $new\n";
2210 #------------------------------------------------
2213 #---------------------------------------
2214 # Find a biblio entry, or create a new one if it doesn't exist.
2215 # If a "subtitle" entry is in hash, add it to subtitle table
2216 sub getoraddbiblio {
2220 # FIXME - Unused argument
2221 $biblio, # hash ref to fields
2232 $dbh = C4::Context->dbh;
2234 print "<PRE>Looking for biblio </PRE>\n" if $debug;
2235 $sth=$dbh->prepare("select biblionumber
2237 where title=? and author=?
2238 and copyrightdate=? and seriestitle=?");
2240 $biblio->{title}, $biblio->{author},
2241 $biblio->{copyright}, $biblio->{seriestitle} );
2243 ($biblionumber) = $sth->fetchrow;
2244 print "<PRE>Biblio exists with number $biblionumber</PRE>\n" if $debug;
2246 # Doesn't exist. Add new one.
2247 print "<PRE>Adding biblio</PRE>\n" if $debug;
2248 ($biblionumber,$error)=&newbiblio($biblio);
2249 if ( $biblionumber ) {
2250 print "<PRE>Added with biblio number=$biblionumber</PRE>\n" if $debug;
2251 if ( $biblio->{subtitle} ) {
2252 &newsubtitle($biblionumber,$biblio->{subtitle} );
2255 print "<PRE>Couldn't add biblio: $error</PRE>\n" if $debug;
2259 return $biblionumber,$error;
2261 } # sub getoraddbiblio
2264 # converts ISO 5426 coded string to ISO 8859-1
2265 # sloppy code : should be improved in next issue
2266 my ($string,$encoding) = @_ ;
2268 # $encoding = C4::Context->preference("marcflavour") unless $encoding;
2269 if ($encoding eq "UNIMARC") {
2331 # this handles non-sorting blocks (if implementation requires this)
2332 $string = nsb_clean($_) ;
2333 } elsif ($encoding eq "USMARC") {
2386 # this handles non-sorting blocks (if implementation requires this)
2387 $string = nsb_clean($_) ;
2394 my $NSB = '\x88' ; # NSB : begin Non Sorting Block
2395 my $NSE = '\x89' ; # NSE : Non Sorting Block end
2396 # handles non sorting blocks
2400 s/[ ]{0,1}$NSE/) /gm ;
2405 END { } # module clean-up code here (global destructor)
2411 Koha Developement team <info@koha.org>
2413 Paul POULAIN paul.poulain@free.fr