4 # Revision 1.48 2003/06/16 09:22:53 rangi
5 # Just added an order clause to getitemtypes
7 # Revision 1.47 2003/05/20 16:22:44 tipaul
8 # fixing typo in Biblio.pm POD
10 # Revision 1.46 2003/05/19 13:45:18 tipaul
11 # support for subtitles, additional authors, subject.
12 # 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.
13 # Note that some OLD-DB subs are strange (dummy ?) see OLDmodsubject, OLDmodsubtitle, OLDmodaddiauthor in C4/Biblio.pm
14 # 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.
16 # Revision 1.45 2003/04/29 16:50:49 tipaul
17 # really proud of this commit :-)
18 # z3950 search and import seems to works fine.
19 # Let me explain how :
20 # * a "search z3950" button is added in the addbiblio template.
21 # * when clicked, a popup appears and z3950/search.pl is called
22 # * z3950/search.pl calls addz3950search in the DB
23 # * the z3950 daemon retrieve the records and stores them in z3950results AND in marc_breeding table.
24 # * as long as there as searches pending, the popup auto refresh every 2 seconds, and says how many searches are pending.
25 # * when the user clicks on a z3950 result => the parent popup is called with the requested biblio, and auto-filled
28 # * 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.
29 # * 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.
31 # Revision 1.44 2003/04/28 13:07:14 tipaul
32 # Those fixes solves the "internal server error" with MARC::Record 1.12.
33 # It was due to an illegal contruction in Koha : we tried to retrive subfields from <10 tags.
34 # That's not possible. MARC::Record accepted this in 0.93 version, but it was fixed after.
35 # Now, the construct/retrieving is OK !
37 # Revision 1.43 2003/04/10 13:56:02 tipaul
39 # * worked in 1.9.0, but not in 1.9.1 :
40 # - modif of a biblio didn't work
41 # - 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.
43 # * did not work before :
44 # - repeatable subfields now works correctly. Enter 2 subfields separated by | and they will be splitted during saving.
45 # - dropped the last subfield of the MARC form :-(
48 # - 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.
49 # Note the MARCdelbiblio has been rewritted to enable deletion of a biblio WITHOUT deleting items.
51 # Revision 1.42 2003/04/04 08:41:11 tipaul
52 # last commits before 1.9.1
54 # Revision 1.41 2003/04/01 12:26:43 tipaul
57 # Revision 1.40 2003/03/11 15:14:03 tipaul
60 # Revision 1.39 2003/03/07 16:35:42 tipaul
61 # * moving generic functions to Koha.pm
62 # * improvement of SearchMarc.pm
66 # Revision 1.38 2003/02/27 16:51:59 tipaul
67 # * moving prepare / execute to ? form.
70 # * road to 1.9.2 => acquisition and cataloguing merging
72 # Revision 1.37 2003/02/12 11:03:03 tipaul
73 # Support for 000 -> 010 fields.
74 # Those fields doesn't have subfields.
75 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
76 # 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.
78 # Revision 1.36 2003/02/12 11:01:01 tipaul
79 # Support for 000 -> 010 fields.
80 # Those fields doesn't have subfields.
81 # In koha, we will use a specific "trick" : fields <10 will have a "virtual" subfield : "@".
82 # 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.
84 # Revision 1.35 2003/02/03 18:46:00 acli
85 # Minor factoring in C4/Biblio.pm, plus change to export the per-tag
86 # 'mandatory' property to a per-subfield 'tag_mandatory' template parameter,
87 # so that addbiblio.tmpl can distinguish between mandatory subfields in a
88 # mandatory tag and mandatory subfields in an optional tag
90 # Not-minor factoring in acqui.simple/addbiblio.pl to make the if-else blocks
91 # smaller, and to add some POD; need further testing for this
93 # Added function to check if a MARC subfield name is "koha-internal" (instead
94 # of checking it for 'lib' and 'tag' everywhere); temporarily added to Koha.pm
96 # Use above function in acqui.simple/additem.pl and search.marc/search.pl
98 # Revision 1.34 2003/01/28 14:50:04 tipaul
99 # fixing MARCmodbiblio API and reindenting code
101 # Revision 1.33 2003/01/23 12:22:37 tipaul
102 # adding char_decode to decode MARC21 or UNIMARC extended chars
104 # Revision 1.32 2002/12/16 15:08:50 tipaul
105 # small but important bugfix (fixes a problem in export)
107 # Revision 1.31 2002/12/13 16:22:04 tipaul
108 # 1st draft of marc export
110 # Revision 1.30 2002/12/12 21:26:35 tipaul
111 # YAB ! (Yet Another Bugfix) => related to biblio modif
112 # (some warning cleaning too)
114 # Revision 1.29 2002/12/12 16:35:00 tipaul
115 # adding authentification with Auth.pm and
116 # MAJOR BUGFIX on marc biblio modification
118 # Revision 1.28 2002/12/10 13:30:03 tipaul
119 # fugfixes from Dombes Abbey work
121 # Revision 1.27 2002/11/19 12:36:16 tipaul
123 # various bugfixes, improvments, and migration from acquisition.pm to biblio.pm
125 # Revision 1.26 2002/11/12 15:58:43 tipaul
128 # * 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)
130 # Revision 1.25 2002/10/25 10:58:26 tipaul
132 # * bugfixes and improvements
134 # Revision 1.24 2002/10/24 12:09:01 arensb
135 # Fixed "no title" warning when generating HTML documentation from POD.
137 # Revision 1.23 2002/10/16 12:43:08 arensb
138 # Added some FIXME comments.
140 # Revision 1.22 2002/10/15 13:39:17 tipaul
141 # removing Acquisition.pm
142 # deleting unused code in biblio.pm, rewriting POD and answering most FIXME comments
144 # Revision 1.21 2002/10/13 11:34:14 arensb
145 # Replaced expressions of the form "$x = $x <op> $y" with "$x <op>= $y".
146 # Thus, $x = $x+2 becomes $x += 2, and so forth.
148 # Revision 1.20 2002/10/13 08:28:32 arensb
149 # Deleted unused variables.
150 # Removed trailing whitespace.
152 # Revision 1.19 2002/10/13 05:56:10 arensb
153 # Added some FIXME comments.
155 # Revision 1.18 2002/10/11 12:34:53 arensb
156 # Replaced &requireDBI with C4::Context->dbh
158 # Revision 1.17 2002/10/10 14:48:25 tipaul
161 # Revision 1.16 2002/10/07 14:04:26 tipaul
162 # road to 1.3.1 : viewing MARC biblio
164 # Revision 1.15 2002/10/05 09:49:25 arensb
165 # Merged with arensb-context branch: use C4::Context->dbh instead of
166 # &C4Connect, and generally prefer C4::Context over C4::Database.
168 # Revision 1.14 2002/10/03 11:28:18 tipaul
169 # Extending Context.pm to add stopword management and using it in MARC-API.
170 # First benchmarks show a medium speed improvement, which is nice as this part is heavily called.
172 # Revision 1.13 2002/10/02 16:26:44 tipaul
175 # Revision 1.12.2.4 2002/10/05 07:09:31 arensb
176 # Merged in changes from main branch.
178 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
179 # Added a whole mess of FIXME comments.
181 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
182 # Added some missing semicolons.
184 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
185 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
188 # Revision 1.12.2.3 2002/10/05 06:12:10 arensb
189 # Added a whole mess of FIXME comments.
191 # Revision 1.12.2.2 2002/10/05 04:03:14 arensb
192 # Added some missing semicolons.
194 # Revision 1.12.2.1 2002/10/04 02:24:01 arensb
195 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
198 # Revision 1.12 2002/10/01 11:48:51 arensb
199 # Added some FIXME comments, mostly marking duplicate functions.
201 # Revision 1.11 2002/09/24 13:49:26 tipaul
202 # long WAS the road to 1.3.0...
203 # coming VERY SOON NOW...
204 # modifying installer and buildrelease to update the DB
206 # Revision 1.10 2002/09/22 16:50:08 arensb
207 # Added some FIXME comments.
209 # Revision 1.9 2002/09/20 12:57:46 tipaul
210 # long is the road to 1.4.0
211 # * MARCadditem and MARCmoditem now wroks
212 # * various bugfixes in MARC management
213 # !!! 1.3.0 should be released very soon now. Be careful !!!
215 # Revision 1.8 2002/09/10 13:53:52 tipaul
216 # MARC API continued...
218 # * 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)
220 # Note : it should not be hard for marcimport and marcexport to re-link fields from internal tag/subfield to "legal" tag/subfield.
222 # Revision 1.7 2002/08/14 18:12:51 tonnesen
223 # Added copyright statement to all .pl and .pm files
225 # Revision 1.6 2002/07/25 13:40:31 tipaul
226 # pod documenting the API.
228 # Revision 1.5 2002/07/24 16:11:37 tipaul
230 # Database.pm and Output.pm are almost not modified (var test...)
232 # Biblio.pm is almost completly rewritten.
234 # WHAT DOES IT ??? ==> END of Hitchcock suspens
236 # 1st, it does... nothing...
237 # 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 ...
239 # All old-API functions have been cloned. for example, the "newbiblio" sub, now has become :
240 # * a "newbiblio" sub, with the same parameters. It just call a sub named OLDnewbiblio
241 # * 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.
242 # * 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.
243 # 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 ;-)
245 # 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.
246 # Note we have decided with steve that a old-biblio <=> a MARC-Biblio.
250 # Copyright 2000-2002 Katipo Communications
252 # This file is part of Koha.
254 # Koha is free software; you can redistribute it and/or modify it under the
255 # terms of the GNU General Public License as published by the Free Software
256 # Foundation; either version 2 of the License, or (at your option) any later
259 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
260 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
261 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
263 # You should have received a copy of the GNU General Public License along with
264 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
265 # Suite 330, Boston, MA 02111-1307 USA
273 use vars qw($VERSION @ISA @EXPORT);
275 # set the version for version checking
280 # don't forget MARCxxx subs are here only for testing purposes. Should not be used
281 # as the old-style API and the NEW one are the only public functions.
284 &updateBiblio &updateBiblioItem &updateItem
285 &itemcount &newbiblio &newbiblioitem
286 &modnote &newsubject &newsubtitle
287 &modbiblio &checkitems
288 &newitems &modbibitem
289 &modsubtitle &modsubject &modaddauthor &moditem &countitems
290 &delitem &deletebiblioitem &delbiblio
291 &getitemtypes &getbiblio
292 &getbiblioitembybiblionumber
293 &getbiblioitem &getitemsbybiblioitem
295 &newcompletebiblioitem
297 &MARCfind_oldbiblionumber_from_MARCbibid
298 &MARCfind_MARCbibid_from_oldbiblionumber
299 &MARCfind_marc_from_kohafield
303 &NEWnewbiblio &NEWnewitem
304 &NEWmodbiblio &NEWmoditem
306 &MARCaddbiblio &MARCadditem
307 &MARCmodsubfield &MARCaddsubfield
308 &MARCmodbiblio &MARCmoditem
309 &MARCkoha2marcBiblio &MARCmarc2koha
310 &MARCkoha2marcItem &MARChtml2marc
311 &MARCgetbiblio &MARCgetitem
312 &MARCaddword &MARCdelword
318 # MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC MARC
321 # all the following subs takes a MARC::Record as parameter and manage
322 # the MARC-DB. They are called by the 1.0/1.2 xxx subs, and by the
323 # NEWxxx subs (xxx deals with old-DB parameters, the NEWxxx deals with MARC-DB parameter)
327 C4::Biblio - acquisition, catalog management functions
331 move from 1.2 to 1.4 version :
332 1.2 and previous version uses a specific API to manage biblios. This API uses old-DB style parameters.
333 In the 1.4 version, we want to do 2 differents things :
334 - keep populating the old-DB, that has a LOT less datas than MARC
335 - populate the MARC-DB
336 To populate the DBs we have 2 differents sources :
337 - the standard acquisition system (through book sellers), that does'nt use MARC data
338 - the MARC acquisition system, that uses MARC data.
340 Thus, we have 2 differents cases :
341 - 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
342 - 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
344 That's why we need 4 subs :
345 all I<subs beginning by MARC> manage only MARC tables. They manage MARC-DB with MARC::Record parameters
346 all I<subs beginning by OLD> manage only OLD-DB tables. They manage old-DB with old-DB parameters
347 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
348 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.
350 - NEW and old-style API should be used in koha to manage biblio
351 - MARCsubs are divided in 2 parts :
352 * some of them manage MARC parameters. They are heavily used in koha.
353 * some of them manage MARC biblio : they are mostly used by NEW and old-style subs.
354 - OLD are used internally only
356 all subs requires/use $dbh as 1st parameter.
358 I<NEWxxx related subs>
360 all subs requires/use $dbh as 1st parameter.
361 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
363 I<OLDxxx related subs>
365 all subs requires/use $dbh as 1st parameter.
366 those subs are used by the MARC-compliant version of koha : marc import, or marc management.
368 They all are the exact copy of 1.0/1.2 version of the sub without the OLD.
369 The OLDxxx is called by the original xxx sub.
370 the 1.4 xxx sub also builds MARC::Record an calls the MARCxxx
372 WARNING : there is 1 difference between initialxxx and OLDxxx :
373 the db header $dbh is always passed as parameter to avoid over-DB connexion
379 =item @tagslib = &MARCgettagslib($dbh,1|0);
381 last param is 1 for liblibrarian and 0 for libopac
382 returns a hash with tag/subfield meaning
383 =item ($tagfield,$tagsubfield) = &MARCfind_marc_from_kohafield($dbh,$kohafield);
385 finds MARC tag and subfield for a given kohafield
386 kohafield is "table.field" where table= biblio|biblioitems|items, and field a field of the previous table
388 =item $biblionumber = &MARCfind_oldbiblionumber_from_MARCbibid($dbh,$MARCbibi);
390 finds a old-db biblio number for a given MARCbibid number
392 =item $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$oldbiblionumber);
394 finds a MARC bibid from a old-db biblionumber
396 =item $MARCRecord = &MARCkoha2marcBiblio($dbh,$biblionumber,biblioitemnumber);
398 MARCkoha2marcBiblio is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB biblio/biblioitem
400 =item $MARCRecord = &MARCkoha2marcItem($dbh,$biblionumber,itemnumber);
402 MARCkoha2marcItem is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB item
404 =item $MARCRecord = &MARCkoha2marcSubtitle($dbh,$biblionumber,$subtitle);
406 MARCkoha2marcSubtitle is a wrapper between old-DB and MARC-DB. It returns a MARC::Record builded with old-DB subtitle
408 =item $olddb = &MARCmarc2koha($dbh,$MARCRecord);
410 builds a hash with old-db datas from a MARC::Record
412 =item &MARCaddbiblio($dbh,$MARC::Record,$biblionumber);
414 creates a biblio (in the MARC tables only). $biblionumber is the old-db biblionumber of the biblio
416 =item &MARCaddsubfield($dbh,$bibid,$tagid,$indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
418 adds a subfield in a biblio (in the MARC tables only).
420 =item $MARCRecord = &MARCgetbiblio($dbh,$bibid);
422 Returns a MARC::Record for the biblio $bibid.
424 =item &MARCmodbiblio($dbh,$bibid,$record,$delete);
426 MARCmodbiblio changes a biblio for a biblio,MARC::Record passed as parameter
427 It 1st delete the biblio, then recreates it.
428 WARNING : the $delete parameter is not used anymore (too much unsolvable cases).
429 =item ($subfieldid,$subfieldvalue) = &MARCmodsubfield($dbh,$subfieldid,$subfieldvalue);
431 MARCmodsubfield changes the value of a given subfield
433 =item $subfieldid = &MARCfindsubfield($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue);
435 MARCfindsubfield returns a subfield number given a bibid/tag/subfieldvalue values.
436 Returns -1 if more than 1 answer
438 =item $subfieldid = &MARCfindsubfieldid($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
440 MARCfindsubfieldid find a subfieldid for a bibid/tag/tagorder/subfield/subfieldorder
442 =item &MARCdelsubfield($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder);
444 MARCdelsubfield delete a subfield for a bibid/tag/tagorder/subfield/subfieldorder
446 =item &MARCdelbiblio($dbh,$bibid);
448 MARCdelbiblio delete biblio $bibid
450 =item &MARCkoha2marcOnefield
452 used by MARCkoha2marc and should not be useful elsewhere
454 =item &MARCmarc2kohaOnefield
456 used by MARCmarc2koha and should not be useful elsewhere
460 used to manage MARC_word table and should not be useful elsewhere
464 used to manage MARC_word table and should not be useful elsewhere
469 my ($dbh,$forlibrarian)= @_;
471 my $libfield = ($forlibrarian eq 1)? 'liblibrarian' : 'libopac';
472 $sth=$dbh->prepare("select tagfield,$libfield as lib,mandatory from marc_tag_structure order by tagfield");
474 my ($lib,$tag,$res,$tab,$mandatory,$repeatable);
475 while ( ($tag,$lib,$mandatory) = $sth->fetchrow) {
476 $res->{$tag}->{lib}=$lib;
477 $res->{$tab}->{tab}=""; # XXX
478 $res->{$tag}->{mandatory}=$mandatory;
481 $sth=$dbh->prepare("select tagfield,tagsubfield,$libfield as lib,tab, mandatory, repeatable,authorised_value,thesaurus_category,value_builder from marc_subfield_structure order by tagfield,tagsubfield");
485 my $authorised_value;
486 my $thesaurus_category;
488 while ( ($tag, $subfield, $lib, $tab, $mandatory, $repeatable,$authorised_value,$thesaurus_category,$value_builder) = $sth->fetchrow) {
489 $res->{$tag}->{$subfield}->{lib}=$lib;
490 $res->{$tag}->{$subfield}->{tab}=$tab;
491 $res->{$tag}->{$subfield}->{mandatory}=$mandatory;
492 $res->{$tag}->{$subfield}->{repeatable}=$repeatable;
493 $res->{$tag}->{$subfield}->{authorised_value}=$authorised_value;
494 $res->{$tag}->{$subfield}->{thesaurus_category}=$thesaurus_category;
495 $res->{$tag}->{$subfield}->{value_builder}=$value_builder;
500 sub MARCfind_marc_from_kohafield {
501 my ($dbh,$kohafield) = @_;
502 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
503 $sth->execute($kohafield);
504 my ($tagfield,$tagsubfield) = $sth->fetchrow;
505 return ($tagfield,$tagsubfield);
508 sub MARCfind_oldbiblionumber_from_MARCbibid {
509 my ($dbh,$MARCbibid) = @_;
510 my $sth=$dbh->prepare("select biblionumber from marc_biblio where bibid=?");
511 $sth->execute($MARCbibid);
512 my ($biblionumber) = $sth->fetchrow;
513 return $biblionumber;
516 sub MARCfind_MARCbibid_from_oldbiblionumber {
517 my ($dbh,$oldbiblionumber) = @_;
518 my $sth=$dbh->prepare("select bibid from marc_biblio where biblionumber=?");
519 $sth->execute($oldbiblionumber);
520 my ($bibid) = $sth->fetchrow;
525 # pass the MARC::Record to this function, and it will create the records in the marc tables
526 my ($dbh,$record,$biblionumber,$bibid) = @_;
527 my @fields=$record->fields();
529 # adding main table, and retrieving bibid
530 # if bibid is sent, then it's not a true add, it's only a re-add, after a delete (ie, a mod)
531 # if bibid empty => true add, find a new bibid number
533 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
534 my $sth=$dbh->prepare("insert into marc_biblio (datecreated,biblionumber) values (now(),?)");
535 $sth->execute($biblionumber);
536 $sth=$dbh->prepare("select max(bibid) from marc_biblio");
538 ($bibid)=$sth->fetchrow;
542 # now, add subfields...
543 foreach my $field (@fields) {
545 if ($field->tag() <10) {
546 &MARCaddsubfield($dbh,$bibid,
555 my @subfields=$field->subfields();
556 foreach my $subfieldcount (0..$#subfields) {
557 &MARCaddsubfield($dbh,$bibid,
559 $field->indicator(1).$field->indicator(2),
561 $subfields[$subfieldcount][0],
563 $subfields[$subfieldcount][1]
568 $dbh->do("unlock tables");
573 # pass the MARC::Record to this function, and it will create the records in the marc tables
574 my ($dbh,$record,$biblionumber) = @_;
575 # warn "adding : ".$record->as_formatted();
576 # search for MARC biblionumber
577 $dbh->do("lock tables marc_biblio WRITE,marc_subfield_table WRITE, marc_word WRITE, marc_blob_subfield WRITE, stopwords READ");
578 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
579 my @fields=$record->fields();
580 my $sth = $dbh->prepare("select max(tagorder) from marc_subfield_table where bibid=?");
581 $sth->execute($bibid);
582 my ($fieldcount) = $sth->fetchrow;
583 # now, add subfields...
584 foreach my $field (@fields) {
585 my @subfields=$field->subfields();
587 foreach my $subfieldcount (0..$#subfields) {
588 &MARCaddsubfield($dbh,$bibid,
590 $field->indicator(1).$field->indicator(2),
592 $subfields[$subfieldcount][0],
594 $subfields[$subfieldcount][1]
598 $dbh->do("unlock tables");
602 sub MARCaddsubfield {
603 # Add a new subfield to a tag into the DB.
604 my ($dbh,$bibid,$tagid,$tag_indicator,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalues) = @_;
605 # if not value, end of job, we do nothing
606 if (length($subfieldvalues) ==0) {
609 if (not($subfieldcode)) {
612 my @subfieldvalues = split /\|/,$subfieldvalues;
613 foreach my $subfieldvalue (@subfieldvalues) {
614 if (length($subfieldvalue)>255) {
615 # $dbh->do("lock tables marc_blob_subfield WRITE, marc_subfield_table WRITE");
616 my $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
617 $sth->execute($subfieldvalue);
618 $sth=$dbh->prepare("select max(blobidlink)from marc_blob_subfield");
620 my ($res)=$sth->fetchrow;
621 $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,valuebloblink) values (?,?,?,?,?,?,?)");
622 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$res);
624 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";
626 # $dbh->do("unlock tables");
628 my $sth=$dbh->prepare("insert into marc_subfield_table (bibid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue) values (?,?,?,?,?,?,?)");
629 $sth->execute($bibid,(sprintf "%03s",$tagid),$tagorder,$tag_indicator,$subfieldcode,$subfieldorder,$subfieldvalue);
631 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";
634 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
639 # Returns MARC::Record of the biblio passed in parameter.
641 my $record = MARC::Record->new();
642 #---- TODO : the leader is missing
643 $record->leader(' ');
644 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
645 from marc_subfield_table
646 where bibid=? order by tag,tagorder,subfieldcode
648 my $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
649 $sth->execute($bibid);
653 my $field; # for >=10 tags
654 my $prevvalue; # for <10 tags
655 while (my $row=$sth->fetchrow_hashref) {
656 if ($row->{'valuebloblink'}) { #---- search blob if there is one
657 $sth2->execute($row->{'valuebloblink'});
658 my $row2=$sth2->fetchrow_hashref;
660 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
662 if ($row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag) {
665 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue) unless $prevtag eq "XXX"; # ignore the 1st loop
667 $record->add_fields($field) unless $prevtag eq "XXX";
670 $prevtagorder=$row->{tagorder};
671 $prevtag = $row->{tag};
672 $previndicator=$row->{tag_indicator};
673 if ($row->{tag}<10) {
674 $prevvalue = $row->{subfieldvalue};
676 $field = MARC::Field->new((sprintf "%03s",$prevtag), substr($row->{tag_indicator}.' ',0,1), substr($row->{tag_indicator}.' ',1,1), $row->{'subfieldcode'}, $row->{'subfieldvalue'} );
679 if ($row->{tag} <10) {
680 $record->add_fields((sprintf "%03s",$row->{tag}), $row->{'subfieldvalue'});
682 $field->add_subfields($row->{'subfieldcode'}, $row->{'subfieldvalue'} );
684 $prevtag= $row->{tag};
685 $previndicator=$row->{tag_indicator};
688 # the last has not been included inside the loop... do it now !
690 $record->add_fields($prevtag,$prevvalue);
692 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
693 $record->add_fields($field);
698 # Returns MARC::Record of the biblio passed in parameter.
699 my ($dbh,$bibid,$itemnumber)=@_;
700 my $record = MARC::Record->new();
701 # search MARC tagorder
702 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=?");
703 $sth2->execute($bibid,$itemnumber);
704 my ($tagorder) = $sth2->fetchrow_array();
705 #---- TODO : the leader is missing
706 my $sth=$dbh->prepare("select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
707 from marc_subfield_table
708 where bibid=? and tagorder=? order by subfieldcode,subfieldorder
710 $sth2=$dbh->prepare("select subfieldvalue from marc_blob_subfield where blobidlink=?");
711 $sth->execute($bibid,$tagorder);
712 while (my $row=$sth->fetchrow_hashref) {
713 if ($row->{'valuebloblink'}) { #---- search blob if there is one
714 $sth2->execute($row->{'valuebloblink'});
715 my $row2=$sth2->fetchrow_hashref;
717 $row->{'subfieldvalue'}=$row2->{'subfieldvalue'};
719 if ($record->field($row->{'tag'})) {
721 #--- this test must stay as this, because of strange behaviour of mySQL/Perl DBI with char var containing a number...
722 #--- sometimes, eliminates 0 at beginning, sometimes no ;-\\\
723 if (length($row->{'tag'}) <3) {
724 $row->{'tag'} = "0".$row->{'tag'};
726 $field =$record->field($row->{'tag'});
728 my $x = $field->add_subfields($row->{'subfieldcode'},$row->{'subfieldvalue'});
729 $record->delete_field($field);
730 $record->add_fields($field);
733 if (length($row->{'tag'}) < 3) {
734 $row->{'tag'} = "0".$row->{'tag'};
736 my $temp = MARC::Field->new($row->{'tag'}," "," ", $row->{'subfieldcode'} => $row->{'subfieldvalue'});
737 $record->add_fields($temp);
745 my ($dbh,$bibid,$record,$delete)=@_;
746 my $oldrecord=&MARCgetbiblio($dbh,$bibid);
747 if ($oldrecord eq $record) {
750 # 1st delete the biblio,
752 &MARCdelbiblio($dbh,$bibid,1);
753 my $biblionumber = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
754 &MARCaddbiblio($dbh,$record,$biblionumber,$bibid);
758 my ($dbh,$bibid,$keep_items) = @_;
759 # if the keep_item is set to 1, then all items are preserved.
760 # This flag is set when the delbiblio is called by modbiblio
761 # due to a too complex structure of MARC (repeatable fields and subfields),
762 # the best solution for a modif is to delete / recreate the record.
763 if ($keep_items eq 1) {
764 #search item field code
765 my $sth = $dbh->prepare("select tagfield from marc_subfield_structure where kohafield like 'items.%'");
767 my $itemtag = $sth->fetchrow_hashref->{tagfield};
768 $dbh->do("delete from marc_subfield_table where bibid=$bibid and tag<>$itemtag");
769 $dbh->do("delete from marc_word where bibid=$bibid and tag<>$itemtag");
771 $dbh->do("delete from marc_biblio where bibid=$bibid");
772 $dbh->do("delete from marc_subfield_table where bibid=$bibid");
773 $dbh->do("delete from marc_word where bibid=$bibid");
777 my ($dbh,$record,$bibid,$itemnumber,$delete)=@_;
778 my $oldrecord=&MARCgetitem($dbh,$bibid,$itemnumber);
779 # if nothing to change, don't waste time...
780 if ($oldrecord eq $record) {
784 # otherwise, skip through each subfield...
785 my @fields = $record->fields();
786 # search old MARC item
787 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=?");
788 $sth2->execute($bibid,$itemnumber);
789 my ($tagorder) = $sth2->fetchrow_array();
790 foreach my $field (@fields) {
791 my $oldfield = $oldrecord->field($field->tag());
792 my @subfields=$field->subfields();
794 foreach my $subfield (@subfields) {
796 # warn "compare : $oldfield".$oldfield->subfield(@$subfield[0]);
797 if ($oldfield eq 0 or (length($oldfield->subfield(@$subfield[0])) ==0) ) {
798 # just adding datas...
799 # warn "addfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
800 # warn "NEW subfield : $bibid,".$field->tag().",".$tagorder.",".@$subfield[0].",".$subfieldorder.",".@$subfield[1].")";
801 &MARCaddsubfield($dbh,$bibid,$field->tag(),$field->indicator(1).$field->indicator(2),
802 $tagorder,@$subfield[0],$subfieldorder,@$subfield[1]);
804 # warn "modfield : / $subfieldorder / @$subfield[0] - @$subfield[1]";
805 # modify he subfield if it's a different string
806 if ($oldfield->subfield(@$subfield[0]) ne @$subfield[1] ) {
807 my $subfieldid=&MARCfindsubfieldid($dbh,$bibid,$field->tag(),$tagorder,@$subfield[0],$subfieldorder);
808 # warn "changing : $subfieldid, $bibid,".$field->tag(),",$tagorder,@$subfield[0],@$subfield[1],$subfieldorder";
809 &MARCmodsubfield($dbh,$subfieldid,@$subfield[1]);
817 sub MARCmodsubfield {
818 # Subroutine changes a subfield value given a subfieldid.
819 my ($dbh, $subfieldid, $subfieldvalue )=@_;
820 $dbh->do("lock tables marc_blob_subfield WRITE,marc_subfield_table WRITE");
821 my $sth1=$dbh->prepare("select valuebloblink from marc_subfield_table where subfieldid=?");
822 $sth1->execute($subfieldid);
823 my ($oldvaluebloblink)=$sth1->fetchrow;
826 # if too long, use a bloblink
827 if (length($subfieldvalue)>255 ) {
828 # if already a bloblink, update it, otherwise, insert a new one.
829 if ($oldvaluebloblink) {
830 $sth=$dbh->prepare("update marc_blob_subfield set subfieldvalue=? where blobidlink=?");
831 $sth->execute($subfieldvalue,$oldvaluebloblink);
833 $sth=$dbh->prepare("insert into marc_blob_subfield (subfieldvalue) values (?)");
834 $sth->execute($subfieldvalue);
835 $sth=$dbh->prepare("select max(blobidlink) from marc_blob_subfield");
837 my ($res)=$sth->fetchrow;
838 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=null, valuebloblink=$res where subfieldid=?");
839 $sth->execute($subfieldid);
842 # note this can leave orphan bloblink. Not a big problem, but we should build somewhere a orphan deleting script...
843 $sth=$dbh->prepare("update marc_subfield_table set subfieldvalue=?,valuebloblink=null where subfieldid=?");
844 $sth->execute($subfieldvalue, $subfieldid);
846 $dbh->do("unlock tables");
848 $sth=$dbh->prepare("select bibid,tag,tagorder,subfieldcode,subfieldid,subfieldorder from marc_subfield_table where subfieldid=?");
849 $sth->execute($subfieldid);
850 my ($bibid,$tagid,$tagorder,$subfieldcode,$x,$subfieldorder) = $sth->fetchrow;
852 &MARCdelword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder);
853 &MARCaddword($dbh,$bibid,$tagid,$tagorder,$subfieldcode,$subfieldorder,$subfieldvalue);
854 return($subfieldid, $subfieldvalue);
857 sub MARCfindsubfield {
858 my ($dbh,$bibid,$tag,$subfieldcode,$subfieldorder,$subfieldvalue) = @_;
862 my $query="select subfieldid from marc_subfield_table where bibid=? and tag=? and subfieldcode=?";
863 if ($subfieldvalue) {
864 $query .= " and subfieldvalue=".$dbh->quote($subfieldvalue);
866 if ($subfieldorder<1) {
869 $query .= " and subfieldorder=$subfieldorder";
871 my $sti=$dbh->prepare($query);
872 $sti->execute($bibid,$tag, $subfieldcode);
873 while (($subfieldid) = $sti->fetchrow) {
875 $lastsubfieldid=$subfieldid;
877 if ($resultcounter>1) {
878 # Error condition. Values given did not resolve into a unique record. Don't know what to edit
879 # should rarely occur (only if we use subfieldvalue with a value that exists twice, which is strange)
882 return $lastsubfieldid;
886 sub MARCfindsubfieldid {
887 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
888 my $sth=$dbh->prepare("select subfieldid from marc_subfield_table
889 where bibid=? and tag=? and tagorder=?
890 and subfieldcode=? and subfieldorder=?");
891 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
892 my ($res) = $sth->fetchrow;
894 $sth=$dbh->prepare("select subfieldid from marc_subfield_table
895 where bibid=? and tag=? and tagorder=?
896 and subfieldcode=?");
897 $sth->execute($bibid,$tag,$tagorder,$subfield);
898 ($res) = $sth->fetchrow;
903 sub MARCdelsubfield {
904 # delete a subfield for $bibid / tag / tagorder / subfield / subfieldorder
905 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
906 $dbh->do("delete from marc_subfield_table where bibid='$bibid' and
907 tag='$tag' and tagorder='$tagorder'
908 and subfieldcode='$subfield' and subfieldorder='$subfieldorder
912 sub MARCkoha2marcBiblio {
913 # this function builds partial MARC::Record from the old koha-DB fields
914 my ($dbh,$biblionumber,$biblioitemnumber) = @_;
915 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
916 my $record = MARC::Record->new();
917 #--- if bibid, then retrieve old-style koha data
918 if ($biblionumber>0) {
919 my $sth2=$dbh->prepare("select biblionumber,author,title,unititle,notes,abstract,serial,seriestitle,copyrightdate,timestamp
920 from biblio where biblionumber=?");
921 $sth2->execute($biblionumber);
922 my $row=$sth2->fetchrow_hashref;
924 foreach $code (keys %$row) {
926 &MARCkoha2marcOnefield($sth,$record,"biblio.".$code,$row->{$code});
930 #--- if biblioitem, then retrieve old-style koha data
931 if ($biblioitemnumber>0) {
932 my $sth2=$dbh->prepare(" SELECT biblioitemnumber,biblionumber,volume,number,classification,
933 itemtype,url,isbn,issn,dewey,subclass,publicationyear,publishercode,
934 volumedate,volumeddesc,timestamp,illus,pages,notes,size,place
936 WHERE biblionumber=? and biblioitemnumber=?
938 $sth2->execute($biblionumber,$biblioitemnumber);
939 my $row=$sth2->fetchrow_hashref;
941 foreach $code (keys %$row) {
943 &MARCkoha2marcOnefield($sth,$record,"biblioitems.".$code,$row->{$code});
947 # other fields => additional authors, subjects, subtitles
948 my $sth2=$dbh->prepare(" SELECT author FROM additionalauthors WHERE biblionumber=?");
949 $sth2->execute($biblionumber);
950 while (my $row=$sth2->fetchrow_hashref) {
951 &MARCkoha2marcOnefield($sth,$record,"additionalauthors.author",$row->{'author'});
953 my $sth2=$dbh->prepare(" SELECT subject FROM bibliosubject WHERE biblionumber=?");
954 $sth2->execute($biblionumber);
955 while (my $row=$sth2->fetchrow_hashref) {
956 &MARCkoha2marcOnefield($sth,$record,"bibliosubject.subject",$row->{'subject'});
958 my $sth2=$dbh->prepare(" SELECT subtitle FROM bibliosubtitle WHERE biblionumber=?");
959 $sth2->execute($biblionumber);
960 while (my $row=$sth2->fetchrow_hashref) {
961 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.title",$row->{'subtitle'});
966 sub MARCkoha2marcItem {
967 # this function builds partial MARC::Record from the old koha-DB fields
968 my ($dbh,$biblionumber,$itemnumber) = @_;
969 # my $dbh=&C4Connect;
970 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
971 my $record = MARC::Record->new();
972 #--- if item, then retrieve old-style koha data
974 # print STDERR "prepare $biblionumber,$itemnumber\n";
975 my $sth2=$dbh->prepare("SELECT itemnumber,biblionumber,multivolumepart,biblioitemnumber,barcode,dateaccessioned,
976 booksellerid,homebranch,price,replacementprice,replacementpricedate,datelastborrowed,
977 datelastseen,multivolume,stack,notforloan,itemlost,wthdrawn,bulk,issues,renewals,
978 reserves,restricted,binding,itemnotes,holdingbranch,timestamp
980 WHERE itemnumber=?");
981 $sth2->execute($itemnumber);
982 my $row=$sth2->fetchrow_hashref;
984 foreach $code (keys %$row) {
986 &MARCkoha2marcOnefield($sth,$record,"items.".$code,$row->{$code});
993 sub MARCkoha2marcSubtitle {
994 # this function builds partial MARC::Record from the old koha-DB fields
995 my ($dbh,$bibnum,$subtitle) = @_;
996 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
997 my $record = MARC::Record->new();
998 &MARCkoha2marcOnefield($sth,$record,"bibliosubtitle.subtitle",$subtitle);
1002 sub MARCkoha2marcOnefield {
1003 my ($sth,$record,$kohafieldname,$value)=@_;
1006 $sth->execute($kohafieldname);
1007 if (($tagfield,$tagsubfield)=$sth->fetchrow) {
1008 if ($record->field($tagfield)) {
1009 my $tag =$record->field($tagfield);
1011 $tag->add_subfields($tagsubfield,$value);
1012 $record->delete_field($tag);
1013 $record->add_fields($tag);
1016 $record->add_fields($tagfield," "," ",$tagsubfield => $value);
1023 my ($dbh,$rtags,$rsubfields,$rvalues,%indicators) = @_;
1025 my $record = MARC::Record->new();
1026 # my %subfieldlist=();
1027 my $prevvalue; # if tag <10
1028 my $field; # if tag >=10
1029 for (my $i=0; $i< @$rtags; $i++) {
1030 # rebuild MARC::Record
1031 if (@$rtags[$i] ne $prevtag) {
1032 if ($prevtag < 10) {
1034 $record->add_fields((sprintf "%03s",$prevtag),$prevvalue);
1038 $record->add_fields($field);
1041 $indicators{@$rtags[$i]}.=' ';
1042 if (@$rtags[$i] <10) {
1043 $prevvalue= @$rvalues[$i];
1045 $field = MARC::Field->new( (sprintf "%03s",@$rtags[$i]), substr($indicators{@$rtags[$i]},0,1),substr($indicators{@$rtags[$i]},1,1), @$rsubfields[$i] => @$rvalues[$i]);
1047 $prevtag = @$rtags[$i];
1049 if (@$rtags[$i] <10) {
1050 $prevvalue=@$rvalues[$i];
1052 if (@$rvalues[$i]) {
1053 $field->add_subfields(@$rsubfields[$i] => @$rvalues[$i]);
1056 $prevtag= @$rtags[$i];
1059 # the last has not been included inside the loop... do it now !
1060 $record->add_fields($field);
1061 # warn $record->as_formatted;
1066 my ($dbh,$record) = @_;
1067 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1069 my $sth2=$dbh->prepare("SHOW COLUMNS from biblio");
1072 # print STDERR $record->as_formatted;
1073 while (($field)=$sth2->fetchrow) {
1074 $result=&MARCmarc2kohaOneField($sth,"biblio",$field,$record,$result);
1076 $sth2=$dbh->prepare("SHOW COLUMNS from biblioitems");
1078 while (($field)=$sth2->fetchrow) {
1079 $result=&MARCmarc2kohaOneField($sth,"biblioitems",$field,$record,$result);
1081 $sth2=$dbh->prepare("SHOW COLUMNS from items");
1083 while (($field)=$sth2->fetchrow) {
1084 $result = &MARCmarc2kohaOneField($sth,"items",$field,$record,$result);
1086 # additional authors : specific
1087 $result = &MARCmarc2kohaOneField($sth,"additionalauthors","additionalauthors",$record,$result);
1091 sub MARCmarc2kohaOneField {
1092 # FIXME ? if a field has a repeatable subfield that is used in old-db, only the 1st will be retrieved...
1093 my ($sth,$kohatable,$kohafield,$record,$result)= @_;
1094 # warn "kohatable / $kohafield / $result / ";
1098 $sth->execute($kohatable.".".$kohafield);
1099 ($tagfield,$subfield) = $sth->fetchrow;
1100 foreach my $field ($record->field($tagfield)) {
1101 if ($field->subfield($subfield)) {
1102 if ($result->{$kohafield}) {
1103 $result->{$kohafield} .= " | ".$field->subfield($subfield);
1105 $result->{$kohafield}=$field->subfield($subfield);
1113 # split a subfield string and adds it into the word table.
1115 my ($dbh,$bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$sentence) =@_;
1116 $sentence =~ s/(\.|\?|\:|\!|\'|,|\-)/ /g;
1117 my @words = split / /,$sentence;
1118 my $stopwords= C4::Context->stopwords;
1119 my $sth=$dbh->prepare("insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word)
1120 values (?,?,?,?,?,?,soundex(?))");
1121 foreach my $word (@words) {
1122 # we record only words longer than 2 car and not in stopwords hash
1123 if (length($word)>1 and !($stopwords->{uc($word)})) {
1124 $sth->execute($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,$word);
1126 warn "ERROR ==> insert into marc_word (bibid, tag, tagorder, subfieldid, subfieldorder, word, sndx_word) values ($bibid,$tag,$tagorder,$subfieldid,$subfieldorder,$word,soundex($word))\n";
1133 # delete words. this sub deletes all the words from a sentence. a subfield modif is done by a delete then a add
1134 my ($dbh,$bibid,$tag,$tagorder,$subfield,$subfieldorder) = @_;
1135 my $sth=$dbh->prepare("delete from marc_word where bibid=? and tag=? and tagorder=? and subfieldid=? and subfieldorder=?");
1136 $sth->execute($bibid,$tag,$tagorder,$subfield,$subfieldorder);
1141 # NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW NEW
1144 # all the following subs are useful to manage MARC-DB with complete MARC records.
1145 # it's used with marcimport, and marc management tools
1149 =item ($bibid,$oldbibnum,$oldbibitemnum) = NEWnewbibilio($dbh,$MARCRecord,$oldbiblio,$oldbiblioitem);
1151 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
1152 are builded from the MARC::Record. If they are passed, they are used.
1154 =item NEWnewitem($dbh, $record,$bibid);
1156 adds an item in the db.
1161 my ($dbh, $record, $oldbiblio, $oldbiblioitem) = @_;
1162 # note $oldbiblio and $oldbiblioitem are not mandatory.
1163 # if not present, they will be builded from $record with MARCmarc2koha function
1164 if (($oldbiblio) and not($oldbiblioitem)) {
1165 print STDERR "NEWnewbiblio : missing parameter\n";
1166 print "NEWnewbiblio : missing parameter : contact koha development team\n";
1172 $oldbibnum = OLDnewbiblio($dbh,$oldbiblio);
1173 $oldbiblioitem->{'biblionumber'} = $oldbibnum;
1174 $oldbibitemnum = OLDnewbiblioitem($dbh,$oldbiblioitem);
1176 my $olddata = MARCmarc2koha($dbh,$record);
1177 $oldbibnum = OLDnewbiblio($dbh,$olddata);
1178 $olddata->{'biblionumber'} = $oldbibnum;
1179 $oldbibitemnum = OLDnewbiblioitem($dbh,$olddata);
1181 # search subtiles, addiauthors and subjects
1182 my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author");
1183 my @addiauthfields = $record->field($tagfield);
1184 foreach my $addiauthfield (@addiauthfields) {
1185 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1186 foreach my $subfieldcount (0..$#addiauthsubfields) {
1187 OLDmodaddauthor($dbh,$oldbibnum,$addiauthsubfields[$subfieldcount]);
1190 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle");
1191 my @subtitlefields = $record->field($tagfield);
1192 foreach my $subtitlefield (@subtitlefields) {
1193 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1194 foreach my $subfieldcount (0..$#subtitlesubfields) {
1195 OLDnewsubtitle($dbh,$oldbibnum,$subtitlesubfields[$subfieldcount]);
1198 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject");
1199 my @subj = $record->field($tagfield);
1200 foreach my $subject (@subj) {
1201 my @subjsubfield = $subject->subfield($tagsubfield);
1203 foreach my $subfieldcount (0..$#subjsubfield) {
1204 push @subjects,$subjsubfield[$subfieldcount];
1206 OLDmodsubject($dbh,$oldbibnum,1,@subjects);
1208 # we must add bibnum and bibitemnum in MARC::Record...
1209 # we build the new field with biblionumber and biblioitemnumber
1210 # we drop the original field
1211 # we add the new builded field.
1212 # NOTE : Works only if the field is ONLY for biblionumber and biblioitemnumber
1213 # (steve and paul : thinks 090 is a good choice)
1214 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1215 $sth->execute("biblio.biblionumber");
1216 (my $tagfield1, my $tagsubfield1) = $sth->fetchrow;
1217 $sth->execute("biblioitems.biblioitemnumber");
1218 (my $tagfield2, my $tagsubfield2) = $sth->fetchrow;
1219 if ($tagfield1 != $tagfield2) {
1220 warn "Error in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1221 print "Content-Type: text/html\n\nError in NEWnewbiblio : biblio.biblionumber and biblioitems.biblioitemnumber MUST have the same field number";
1224 my $newfield = MARC::Field->new( $tagfield1,'','',
1225 "$tagsubfield1" => $oldbibnum,
1226 "$tagsubfield2" => $oldbibitemnum);
1227 # drop old field and create new one...
1228 my $old_field = $record->field($tagfield1);
1229 $record->delete_field($old_field);
1230 $record->add_fields($newfield);
1231 my $bibid = MARCaddbiblio($dbh,$record,$oldbibnum);
1232 return ($bibid,$oldbibnum,$oldbibitemnum );
1236 my ($dbh,$record,$bibid) =@_;
1237 &MARCmodbiblio($dbh,$bibid,$record,0);
1238 my $oldbiblio = MARCmarc2koha($dbh,$record);
1239 my $oldbiblionumber = OLDmodbiblio($dbh,$oldbiblio);
1240 OLDmodbibitem($dbh,$oldbiblio);
1241 # now, modify addi authors, subject, addititles.
1242 my ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"additionalauthors.author");
1243 my @addiauthfields = $record->field($tagfield);
1244 foreach my $addiauthfield (@addiauthfields) {
1245 my @addiauthsubfields = $addiauthfield->subfield($tagsubfield);
1246 foreach my $subfieldcount (0..$#addiauthsubfields) {
1247 OLDmodaddauthor($dbh,$oldbiblionumber,$addiauthsubfields[$subfieldcount]);
1250 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubtitle.subtitle");
1251 my @subtitlefields = $record->field($tagfield);
1252 foreach my $subtitlefield (@subtitlefields) {
1253 my @subtitlesubfields = $subtitlefield->subfield($tagsubfield);
1254 foreach my $subfieldcount (0..$#subtitlesubfields) {
1255 OLDmodsubtitle($dbh,$oldbiblionumber,$subtitlesubfields[$subfieldcount]);
1258 ($tagfield,$tagsubfield) = MARCfind_marc_from_kohafield($dbh,"bibliosubject.subject");
1259 my @subj = $record->field($tagfield);
1260 foreach my $subject (@subj) {
1261 my @subjsubfield = $subject->subfield($tagsubfield);
1263 foreach my $subfieldcount (0..$#subjsubfield) {
1264 push @subjects,$subjsubfield[$subfieldcount];
1266 OLDmodsubject($dbh,$oldbiblionumber,1,@subjects);
1273 my ($dbh, $record,$bibid) = @_;
1274 # add item in old-DB
1275 my $item = &MARCmarc2koha($dbh,$record);
1276 # needs old biblionumber and biblioitemnumber
1277 $item->{'biblionumber'} = MARCfind_oldbiblionumber_from_MARCbibid($dbh,$bibid);
1278 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
1279 $sth->execute($item->{'biblionumber'});
1280 ($item->{'biblioitemnumber'}) = $sth->fetchrow;
1281 my ($itemnumber,$error) = &OLDnewitems($dbh,$item,$item->{barcode});
1282 # add itemnumber to MARC::Record before adding the item.
1283 my $sth=$dbh->prepare("select tagfield,tagsubfield from marc_subfield_structure where kohafield=?");
1284 &MARCkoha2marcOnefield($sth,$record,"items.itemnumber",$itemnumber);
1286 my $bib = &MARCadditem($dbh,$record,$item->{'biblionumber'});
1290 my ($dbh,$record,$bibid,$itemnumber,$delete) = @_;
1291 &MARCmoditem($dbh,$record,$bibid,$itemnumber,$delete);
1292 my $olditem = MARCmarc2koha($dbh,$record);
1293 OLDmoditem($dbh,$olditem);
1298 # OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD OLD
1302 =item $biblionumber = OLDnewbiblio($dbh,$biblio);
1304 adds a record in biblio table. Datas are in the hash $biblio.
1306 =item $biblionumber = OLDmodbiblio($dbh,$biblio);
1308 modify a record in biblio table. Datas are in the hash $biblio.
1310 =item OLDmodsubtitle($dbh,$bibnum,$subtitle);
1312 modify subtitles in bibliosubtitle table.
1314 =item OLDmodaddauthor($dbh,$bibnum,$author);
1316 adds or modify additional authors
1317 NOTE : Strange sub : seems to delete MANY and add only ONE author... maybe buggy ?
1319 =item $errors = OLDmodsubject($dbh,$bibnum, $force, @subject);
1321 modify/adds subjects
1323 =item OLDmodbibitem($dbh, $biblioitem);
1327 =item OLDmodnote($dbh,$bibitemnum,$note
1329 modify a note for a biblioitem
1331 =item OLDnewbiblioitem($dbh,$biblioitem);
1333 adds a biblioitem ($biblioitem is a hash with the values)
1335 =item OLDnewsubject($dbh,$bibnum);
1339 =item OLDnewsubtitle($dbh,$bibnum,$subtitle);
1341 create a new subtitle
1343 =item ($itemnumber,$errors)= OLDnewitems($dbh,$item,$barcode);
1345 create a item. $item is a hash and $barcode the barcode.
1347 =item OLDmoditem($dbh,$item);
1351 =item OLDdelitem($dbh,$itemnum);
1355 =item OLDdeletebiblioitem($dbh,$biblioitemnumber);
1357 deletes a biblioitem
1358 NOTE : not standard sub name. Should be OLDdelbiblioitem()
1360 =item OLDdelbiblio($dbh,$biblio);
1367 my ($dbh,$biblio) = @_;
1368 # my $dbh = &C4Connect;
1369 my $query = "Select max(biblionumber) from biblio";
1370 my $sth = $dbh->prepare($query);
1372 my $data = $sth->fetchrow_arrayref;
1373 my $bibnum = $$data[0] + 1;
1376 if ($biblio->{'seriestitle'}) { $series = 1 };
1378 $query = "insert into biblio set biblionumber = ?, title = ?, author = ?, copyrightdate = ?,
1379 serial = ?, seriestitle = ?, notes = ?, abstract = ?";
1380 $sth = $dbh->prepare($query);
1381 $sth->execute($bibnum,$biblio->{'title'},$biblio->{'author'},$biblio->{'copyright'},$series,$biblio->{'seriestitle'},$biblio->{'notes'},$biblio->{'abstract'});
1389 my ($dbh,$biblio) = @_;
1390 # my $dbh = C4Connect;
1394 $query = "Update biblio set title = ?, author = ?, abstract = ?, copyrightdate = ?,
1395 seriestitle = ?, serial = ?, unititle = ?, notes = ? where biblionumber = ?";
1396 $sth = $dbh->prepare($query);
1397 $sth->execute($biblio->{'title'},$biblio->{'author'},$biblio->{'abstract'},$biblio->{'copyrightdate'},
1398 $biblio->{'seriestitle'},$biblio->{'serial'},$biblio->{'unititle'},$biblio->{'notes'},$biblio->{'biblionumber'});
1401 return($biblio->{'biblionumber'});
1404 sub OLDmodsubtitle {
1405 my ($dbh,$bibnum, $subtitle) = @_;
1406 my $query = "update bibliosubtitle set subtitle = ? where biblionumber = ?";
1407 my $sth = $dbh->prepare($query);
1408 $sth->execute($subtitle,$bibnum);
1413 sub OLDmodaddauthor {
1414 my ($dbh,$bibnum, $author) = @_;
1415 # my $dbh = C4Connect;
1416 my $query = "Delete from additionalauthors where biblionumber = $bibnum";
1417 my $sth = $dbh->prepare($query);
1422 if ($author ne '') {
1423 $query = "Insert into additionalauthors set
1426 $sth = $dbh->prepare($query);
1428 $sth->execute($author,$bibnum);
1432 } # sub modaddauthor
1436 my ($dbh,$bibnum, $force, @subject) = @_;
1437 # my $dbh = C4Connect;
1438 my $count = @subject;
1440 for (my $i = 0; $i < $count; $i++) {
1441 $subject[$i] =~ s/^ //g;
1442 $subject[$i] =~ s/ $//g;
1443 my $query = "select * from catalogueentry where entrytype = 's' and catalogueentry = '$subject[$i]'";
1444 my $sth = $dbh->prepare($query);
1447 if (my $data = $sth->fetchrow_hashref) {
1449 if ($force eq $subject[$i]) {
1450 # subject not in aut, chosen to force anway
1451 # so insert into cataloguentry so its in auth file
1452 $query = "Insert into catalogueentry (entrytype,catalogueentry) values ('s','$subject[$i]')";
1453 my $sth2 = $dbh->prepare($query);
1458 $error = "$subject[$i]\n does not exist in the subject authority file";
1459 $query = "Select * from catalogueentry where entrytype = 's' and (catalogueentry like '$subject[$i] %'
1460 or catalogueentry like '% $subject[$i] %' or catalogueentry like '% $subject[$i]')";
1461 my $sth2 = $dbh->prepare($query);
1463 while (my $data = $sth2->fetchrow_hashref) {
1464 $error .= "<br>$data->{'catalogueentry'}";
1472 my $query = "Delete from bibliosubject where biblionumber = $bibnum";
1473 my $sth = $dbh->prepare($query);
1476 for (my $i = 0; $i < $count; $i++) {
1477 $sth = $dbh->prepare("Insert into bibliosubject values ('$subject[$i]', $bibnum)");
1488 my ($dbh,$biblioitem) = @_;
1489 # my $dbh = C4Connect;
1492 $biblioitem->{'itemtype'} = $dbh->quote($biblioitem->{'itemtype'});
1493 $biblioitem->{'url'} = $dbh->quote($biblioitem->{'url'});
1494 $biblioitem->{'isbn'} = $dbh->quote($biblioitem->{'isbn'});
1495 $biblioitem->{'publishercode'} = $dbh->quote($biblioitem->{'publishercode'});
1496 $biblioitem->{'publicationyear'} = $dbh->quote($biblioitem->{'publicationyear'});
1497 $biblioitem->{'classification'} = $dbh->quote($biblioitem->{'classification'});
1498 $biblioitem->{'dewey'} = $dbh->quote($biblioitem->{'dewey'});
1499 $biblioitem->{'subclass'} = $dbh->quote($biblioitem->{'subclass'});
1500 $biblioitem->{'illus'} = $dbh->quote($biblioitem->{'illus'});
1501 $biblioitem->{'pages'} = $dbh->quote($biblioitem->{'pages'});
1502 $biblioitem->{'volumeddesc'} = $dbh->quote($biblioitem->{'volumeddesc'});
1503 $biblioitem->{'notes'} = $dbh->quote($biblioitem->{'notes'});
1504 $biblioitem->{'size'} = $dbh->quote($biblioitem->{'size'});
1505 $biblioitem->{'place'} = $dbh->quote($biblioitem->{'place'});
1507 $query = "Update biblioitems set
1508 itemtype = $biblioitem->{'itemtype'},
1509 url = $biblioitem->{'url'},
1510 isbn = $biblioitem->{'isbn'},
1511 publishercode = $biblioitem->{'publishercode'},
1512 publicationyear = $biblioitem->{'publicationyear'},
1513 classification = $biblioitem->{'classification'},
1514 dewey = $biblioitem->{'dewey'},
1515 subclass = $biblioitem->{'subclass'},
1516 illus = $biblioitem->{'illus'},
1517 pages = $biblioitem->{'pages'},
1518 volumeddesc = $biblioitem->{'volumeddesc'},
1519 notes = $biblioitem->{'notes'},
1520 size = $biblioitem->{'size'},
1521 place = $biblioitem->{'place'}
1522 where biblioitemnumber = $biblioitem->{'biblioitemnumber'}";
1530 my ($dbh,$bibitemnum,$note)=@_;
1531 # my $dbh=C4Connect;
1532 my $query="update biblioitems set notes='$note' where
1533 biblioitemnumber='$bibitemnum'";
1534 my $sth=$dbh->prepare($query);
1540 sub OLDnewbiblioitem {
1541 my ($dbh,$biblioitem) = @_;
1542 # my $dbh = C4Connect;
1543 my $query = "Select max(biblioitemnumber) from biblioitems";
1544 my $sth = $dbh->prepare($query);
1549 $data = $sth->fetchrow_arrayref;
1550 $bibitemnum = $$data[0] + 1;
1554 $sth = $dbh->prepare("insert into biblioitems set
1555 biblioitemnumber = ?, biblionumber = ?,
1556 volume = ?, number = ?,
1557 classification = ?, itemtype = ?,
1559 issn = ?, dewey = ?,
1560 subclass = ?, publicationyear = ?,
1561 publishercode = ?, volumedate = ?,
1562 volumeddesc = ?, illus = ?,
1563 pages = ?, notes = ?,
1565 marc = ?, place = ?");
1566 $sth->execute($bibitemnum, $biblioitem->{'biblionumber'},
1567 $biblioitem->{'volume'}, $biblioitem->{'number'},
1568 $biblioitem->{'classification'}, $biblioitem->{'itemtype'},
1569 $biblioitem->{'url'}, $biblioitem->{'isbn'},
1570 $biblioitem->{'issn'}, $biblioitem->{'dewey'},
1571 $biblioitem->{'subclass'}, $biblioitem->{'publicationyear'},
1572 $biblioitem->{'publishercode'}, $biblioitem->{'volumedate'},
1573 $biblioitem->{'volumeddesc'}, $biblioitem->{'illus'},
1574 $biblioitem->{'pages'}, $biblioitem->{'notes'},
1575 $biblioitem->{'size'}, $biblioitem->{'lccn'},
1576 $biblioitem->{'marc'}, $biblioitem->{'place'});
1579 return($bibitemnum);
1583 my ($dbh,$bibnum)=@_;
1584 my $query="insert into bibliosubject (biblionumber) values ($bibnum)";
1585 my $sth=$dbh->prepare($query);
1590 sub OLDnewsubtitle {
1591 my ($dbh,$bibnum, $subtitle) = @_;
1592 my $query = "insert into bibliosubtitle set biblionumber = ?, subtitle = ?";
1593 my $sth = $dbh->prepare($query);
1594 $sth->execute($bibnum,$subtitle);
1600 my ($dbh,$item, $barcode) = @_;
1601 # my $dbh = C4Connect;
1602 my $query = "Select max(itemnumber) from items";
1603 my $sth = $dbh->prepare($query);
1609 $data = $sth->fetchrow_hashref;
1610 $itemnumber = $data->{'max(itemnumber)'} + 1;
1613 $sth=$dbh->prepare("Insert into items set
1614 itemnumber = ?, biblionumber = ?,
1615 biblioitemnumber = ?, barcode = ?,
1616 booksellerid = ?, dateaccessioned = NOW(),
1617 homebranch = ?, holdingbranch = ?,
1618 price = ?, replacementprice = ?,
1619 replacementpricedate = NOW(), itemnotes = ?,
1623 $sth->execute($itemnumber, $item->{'biblionumber'},
1624 $item->{'biblioitemnumber'},$barcode,
1625 $item->{'booksellerid'},
1626 $item->{'homebranch'},$item->{'holdingbranch'},
1627 $item->{'price'},$item->{'replacementprice'},
1628 $item->{'itemnotes'},$item->{'loan'});
1629 if (defined $sth->errstr) {
1630 $error .= $sth->errstr;
1633 return($itemnumber,$error);
1637 my ($dbh,$item) = @_;
1638 # my ($dbh,$loan,$itemnum,$bibitemnum,$barcode,$notes,$homebranch,$lost,$wthdrawn,$replacement)=@_;
1639 # my $dbh=C4Connect;
1640 $item->{'itemnum'}=$item->{'itemnumber'} unless $item->{'itemnum'};
1641 my $query="update items set barcode='$item->{'barcode'}',itemnotes='$item->{'notes'}'
1642 where itemnumber=$item->{'itemnum'}";
1643 if ($item->{'barcode'} eq ''){
1644 $query="update items set notforloan=$item->{'loan'} where itemnumber=$item->{'itemnum'}";
1646 if ($item->{'lost'} ne ''){
1647 $query="update items set biblioitemnumber=$item->{'bibitemnum'},
1648 barcode='$item->{'barcode'}',
1649 itemnotes='$item->{'notes'}',
1650 homebranch='$item->{'homebranch'}',
1651 itemlost='$item->{'lost'}',
1652 wthdrawn='$item->{'wthdrawn'}'
1653 where itemnumber=$item->{'itemnum'}";
1655 if ($item->{'replacement'} ne ''){
1656 $query=~ s/ where/,replacementprice='$item->{'replacement'}' where/;
1658 my $sth=$dbh->prepare($query);
1665 my ($dbh,$itemnum)=@_;
1666 # my $dbh=C4Connect;
1667 my $query="select * from items where itemnumber=$itemnum";
1668 my $sth=$dbh->prepare($query);
1670 my @data=$sth->fetchrow_array;
1672 $query="Insert into deleteditems values (";
1673 foreach my $temp (@data){
1674 $query .= "'$temp',";
1678 $sth=$dbh->prepare($query);
1681 $query = "Delete from items where itemnumber=$itemnum";
1682 $sth=$dbh->prepare($query);
1688 sub OLDdeletebiblioitem {
1689 my ($dbh,$biblioitemnumber) = @_;
1690 # my $dbh = C4Connect;
1691 my $query = "Select * from biblioitems
1692 where biblioitemnumber = $biblioitemnumber";
1693 my $sth = $dbh->prepare($query);
1698 if (@results = $sth->fetchrow_array) {
1699 $query = "Insert into deletedbiblioitems values (";
1700 foreach my $value (@results) {
1701 $value = $dbh->quote($value);
1702 $query .= "$value,";
1705 $query =~ s/\,$/\)/;
1708 $query = "Delete from biblioitems
1709 where biblioitemnumber = $biblioitemnumber";
1713 # Now delete all the items attached to the biblioitem
1714 $query = "Select * from items where biblioitemnumber = $biblioitemnumber";
1715 $sth = $dbh->prepare($query);
1717 while (@results = $sth->fetchrow_array) {
1718 $query = "Insert into deleteditems values (";
1719 foreach my $value (@results) {
1720 $value = $dbh->quote($value);
1721 $query .= "$value,";
1723 $query =~ s/\,$/\)/;
1727 $query = "Delete from items where biblioitemnumber = $biblioitemnumber";
1730 } # sub deletebiblioitem
1733 my ($dbh,$biblio)=@_;
1734 # my $dbh=C4Connect;
1735 my $query="select * from biblio where biblionumber=$biblio";
1736 my $sth=$dbh->prepare($query);
1738 if (my @data=$sth->fetchrow_array){
1740 $query="Insert into deletedbiblio values (";
1741 foreach my $temp (@data){
1742 $temp=~ s/\'/\\\'/g;
1743 $query .= "'$temp',";
1747 $sth=$dbh->prepare($query);
1750 $query = "Delete from biblio where biblionumber=$biblio";
1751 $sth=$dbh->prepare($query);
1767 my $dbh = C4::Context->dbh;
1768 my $query="Select count(*) from items where biblionumber=$biblio";
1770 my $sth=$dbh->prepare($query);
1772 my $data=$sth->fetchrow_hashref;
1774 return($data->{'count(*)'});
1779 ($order, $ordernumber) = &getorder($biblioitemnumber, $biblionumber);
1781 Looks up the order with the given biblionumber and biblioitemnumber.
1783 Returns a two-element array. C<$ordernumber> is the order number.
1784 C<$order> is a reference-to-hash describing the order; its keys are
1785 fields from the biblio, biblioitems, aqorders, and aqorderbreakdown
1786 tables of the Koha database.
1790 # FIXME - This is effectively identical to &C4::Catalogue::getorder.
1791 # Pick one and stick with it.
1794 my $dbh = C4::Context->dbh;
1795 my $query="Select ordernumber
1797 where biblionumber=? and biblioitemnumber=?";
1798 my $sth=$dbh->prepare($query);
1799 $sth->execute($bib,$bi);
1800 # FIXME - Use fetchrow_array(), since we're only interested in the one
1802 my $ordnum=$sth->fetchrow_hashref;
1804 my $order=getsingleorder($ordnum->{'ordernumber'});
1806 return ($order,$ordnum->{'ordernumber'});
1809 =item getsingleorder
1811 $order = &getsingleorder($ordernumber);
1813 Looks up an order by order number.
1815 Returns a reference-to-hash describing the order. The keys of
1816 C<$order> are fields from the biblio, biblioitems, aqorders, and
1817 aqorderbreakdown tables of the Koha database.
1821 # FIXME - This is effectively identical to
1822 # &C4::Catalogue::getsingleorder.
1823 # Pick one and stick with it.
1824 sub getsingleorder {
1826 my $dbh = C4::Context->dbh;
1827 my $query="Select * from biblio,biblioitems,aqorders,aqorderbreakdown
1828 where aqorders.ordernumber=?
1829 and biblio.biblionumber=aqorders.biblionumber and
1830 biblioitems.biblioitemnumber=aqorders.biblioitemnumber and
1831 aqorders.ordernumber=aqorderbreakdown.ordernumber";
1832 my $sth=$dbh->prepare($query);
1833 $sth->execute($ordnum);
1834 my $data=$sth->fetchrow_hashref;
1841 my $dbh = C4::Context->dbh;
1842 my $bibnum=OLDnewbiblio($dbh,$biblio);
1843 # finds new (MARC bibid
1844 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$bibnum);
1845 my $record = &MARCkoha2marcBiblio($dbh,$bibnum);
1846 MARCaddbiblio($dbh,$record,$bibnum);
1853 $biblionumber = &modbiblio($biblio);
1855 Update a biblio record.
1857 C<$biblio> is a reference-to-hash whose keys are the fields in the
1858 biblio table in the Koha database. All fields must be present, not
1859 just the ones you wish to change.
1861 C<&modbiblio> updates the record defined by
1862 C<$biblio-E<gt>{biblionumber}> with the values in C<$biblio>.
1864 C<&modbiblio> returns C<$biblio-E<gt>{biblionumber}> whether it was
1871 my $dbh = C4::Context->dbh;
1872 my $biblionumber=OLDmodbiblio($dbh,$biblio);
1873 my $record = MARCkoha2marcBiblio($dbh,$biblionumber);
1874 # finds new (MARC bibid
1875 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$biblionumber);
1876 MARCmodbiblio($dbh,$bibid,$record,0);
1877 return($biblionumber);
1882 &modsubtitle($biblionumber, $subtitle);
1884 Sets the subtitle of a book.
1886 C<$biblionumber> is the biblionumber of the book to modify.
1888 C<$subtitle> is the new subtitle.
1893 my ($bibnum, $subtitle) = @_;
1894 my $dbh = C4::Context->dbh;
1895 &OLDmodsubtitle($dbh,$bibnum,$subtitle);
1900 &modaddauthor($biblionumber, $author);
1902 Replaces all additional authors for the book with biblio number
1903 C<$biblionumber> with C<$author>. If C<$author> is the empty string,
1904 C<&modaddauthor> deletes all additional authors.
1909 my ($bibnum, $author) = @_;
1910 my $dbh = C4::Context->dbh;
1911 &OLDmodaddauthor($dbh,$bibnum,$author);
1912 } # sub modaddauthor
1916 $error = &modsubject($biblionumber, $force, @subjects);
1918 $force - a subject to force
1920 $error - Error message, or undef if successful.
1925 my ($bibnum, $force, @subject) = @_;
1926 my $dbh = C4::Context->dbh;
1927 my $error= &OLDmodsubject($dbh,$bibnum,$force, @subject);
1932 my ($biblioitem) = @_;
1933 my $dbh = C4::Context->dbh;
1934 &OLDmodbibitem($dbh,$biblioitem);
1935 my $MARCbibitem = MARCkoha2marcBiblio($dbh,$biblioitem);
1936 &MARCmodbiblio($dbh,$biblioitem->{biblionumber},$MARCbibitem,0);
1940 my ($bibitemnum,$note)=@_;
1941 my $dbh = C4::Context->dbh;
1942 &OLDmodnote($dbh,$bibitemnum,$note);
1946 my ($biblioitem) = @_;
1947 my $dbh = C4::Context->dbh;
1948 my $bibitemnum = &OLDnewbiblioitem($dbh,$biblioitem);
1949 # print STDERR "bibitemnum : $bibitemnum\n";
1950 my $MARCbiblio= MARCkoha2marcBiblio($dbh,$biblioitem->{biblionumber},$bibitemnum);
1951 # print STDERR $MARCbiblio->as_formatted();
1952 &MARCaddbiblio($dbh,$MARCbiblio,$biblioitem->{biblionumber});
1953 return($bibitemnum);
1958 my $dbh = C4::Context->dbh;
1959 &OLDnewsubject($dbh,$bibnum);
1963 my ($bibnum, $subtitle) = @_;
1964 my $dbh = C4::Context->dbh;
1965 &OLDnewsubtitle($dbh,$bibnum,$subtitle);
1969 my ($item, @barcodes) = @_;
1970 my $dbh = C4::Context->dbh;
1974 foreach my $barcode (@barcodes) {
1975 ($itemnumber,$error)=&OLDnewitems($dbh,$item,uc($barcode));
1977 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{biblionumber},$itemnumber);
1978 &MARCadditem($dbh,$MARCitem,$item->{biblionumber});
1985 my $dbh = C4::Context->dbh;
1986 &OLDmoditem($dbh,$item);
1987 my $MARCitem = &MARCkoha2marcItem($dbh,$item->{'biblionumber'},$item->{'itemnum'});
1988 my $bibid = &MARCfind_MARCbibid_from_oldbiblionumber($dbh,$item->{biblionumber});
1989 &MARCmoditem($dbh,$MARCitem,$bibid,$item->{itemnum},0);
1993 my ($count,@barcodes)=@_;
1994 my $dbh = C4::Context->dbh;
1996 for (my $i=0;$i<$count;$i++){
1997 $barcodes[$i]=uc $barcodes[$i];
1998 my $query="Select * from items where barcode='$barcodes[$i]'";
1999 my $sth=$dbh->prepare($query);
2001 if (my $data=$sth->fetchrow_hashref){
2002 $error.=" Duplicate Barcode: $barcodes[$i]";
2010 my ($bibitemnum)=@_;
2011 my $dbh = C4::Context->dbh;
2012 my $query="Select count(*) from items where biblioitemnumber='$bibitemnum'";
2013 my $sth=$dbh->prepare($query);
2015 my $data=$sth->fetchrow_hashref;
2017 return($data->{'count(*)'});
2022 my $dbh = C4::Context->dbh;
2023 &OLDdelitem($dbh,$itemnum);
2026 sub deletebiblioitem {
2027 my ($biblioitemnumber) = @_;
2028 my $dbh = C4::Context->dbh;
2029 &OLDdeletebiblioitem($dbh,$biblioitemnumber);
2030 } # sub deletebiblioitem
2035 my $dbh = C4::Context->dbh;
2036 &OLDdelbiblio($dbh,$biblio);
2040 my $dbh = C4::Context->dbh;
2041 my $query = "select * from itemtypes order by description";
2042 my $sth = $dbh->prepare($query);
2043 # || die "Cannot prepare $query" . $dbh->errstr;
2048 # || die "Cannot execute $query\n" . $sth->errstr;
2049 while (my $data = $sth->fetchrow_hashref) {
2050 $results[$count] = $data;
2055 return($count, @results);
2056 } # sub getitemtypes
2059 my ($biblionumber) = @_;
2060 my $dbh = C4::Context->dbh;
2061 my $query = "Select * from biblio where biblionumber = $biblionumber";
2062 my $sth = $dbh->prepare($query);
2063 # || die "Cannot prepare $query\n" . $dbh->errstr;
2068 # || die "Cannot execute $query\n" . $sth->errstr;
2069 while (my $data = $sth->fetchrow_hashref) {
2070 $results[$count] = $data;
2075 return($count, @results);
2079 my ($biblioitemnum) = @_;
2080 my $dbh = C4::Context->dbh;
2081 my $query = "Select * from biblioitems where
2082 biblioitemnumber = $biblioitemnum";
2083 my $sth = $dbh->prepare($query);
2089 while (my $data = $sth->fetchrow_hashref) {
2090 $results[$count] = $data;
2095 return($count, @results);
2096 } # sub getbiblioitem
2098 sub getbiblioitembybiblionumber {
2099 my ($biblionumber) = @_;
2100 my $dbh = C4::Context->dbh;
2101 my $query = "Select * from biblioitems where biblionumber =
2103 my $sth = $dbh->prepare($query);
2109 while (my $data = $sth->fetchrow_hashref) {
2110 $results[$count] = $data;
2115 return($count, @results);
2118 sub getitemsbybiblioitem {
2119 my ($biblioitemnum) = @_;
2120 my $dbh = C4::Context->dbh;
2121 my $query = "Select * from items, biblio where
2122 biblio.biblionumber = items.biblionumber and biblioitemnumber
2124 my $sth = $dbh->prepare($query);
2125 # || die "Cannot prepare $query\n" . $dbh->errstr;
2130 # || die "Cannot execute $query\n" . $sth->errstr;
2131 while (my $data = $sth->fetchrow_hashref) {
2132 $results[$count] = $data;
2137 return($count, @results);
2138 } # sub getitemsbybiblioitem
2142 # Subroutine to log changes to databases
2143 # Eventually, this subroutine will be used to create a log of all changes made,
2144 # with the possibility of "undo"ing some changes
2146 if ($database eq 'kohadb') {
2152 # print STDERR "KOHA: $type $section $item $original $new\n";
2153 } elsif ($database eq 'marc') {
2155 my $Record_ID=shift;
2158 my $subfield_ID=shift;
2161 # print STDERR "MARC: $type $Record_ID $tag $mark $subfield_ID $original $new\n";
2165 #------------------------------------------------
2168 #---------------------------------------
2169 # Find a biblio entry, or create a new one if it doesn't exist.
2170 # If a "subtitle" entry is in hash, add it to subtitle table
2171 sub getoraddbiblio {
2175 # FIXME - Unused argument
2176 $biblio, # hash ref to fields
2187 $dbh = C4::Context->dbh;
2189 print "<PRE>Looking for biblio </PRE>\n" if $debug;
2190 $sth=$dbh->prepare("select biblionumber
2192 where title=? and author=?
2193 and copyrightdate=? and seriestitle=?");
2195 $biblio->{title}, $biblio->{author},
2196 $biblio->{copyright}, $biblio->{seriestitle} );
2198 ($biblionumber) = $sth->fetchrow;
2199 print "<PRE>Biblio exists with number $biblionumber</PRE>\n" if $debug;
2201 # Doesn't exist. Add new one.
2202 print "<PRE>Adding biblio</PRE>\n" if $debug;
2203 ($biblionumber,$error)=&newbiblio($biblio);
2204 if ( $biblionumber ) {
2205 print "<PRE>Added with biblio number=$biblionumber</PRE>\n" if $debug;
2206 if ( $biblio->{subtitle} ) {
2207 &newsubtitle($biblionumber,$biblio->{subtitle} );
2210 print "<PRE>Couldn't add biblio: $error</PRE>\n" if $debug;
2214 return $biblionumber,$error;
2216 } # sub getoraddbiblio
2219 # converts ISO 5426 coded string to ISO 8859-1
2220 # sloppy code : should be improved in next issue
2221 my ($string,$encoding) = @_ ;
2223 # $encoding = C4::Context->preference("marcflavour") unless $encoding;
2224 if ($encoding eq "UNIMARC") {
2286 # this handles non-sorting blocks (if implementation requires this)
2287 $string = nsb_clean($_) ;
2288 } elsif ($encoding eq "USMARC") {
2341 # this handles non-sorting blocks (if implementation requires this)
2342 $string = nsb_clean($_) ;
2349 my $NSB = '\x88' ; # NSB : begin Non Sorting Block
2350 my $NSE = '\x89' ; # NSE : Non Sorting Block end
2351 # handles non sorting blocks
2355 s/[ ]{0,1}$NSE/) /gm ;
2360 END { } # module clean-up code here (global destructor)
2366 Koha Developement team <info@koha.org>
2368 Paul POULAIN paul.poulain@free.fr