3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
23 use MARC::File::USMARC;
30 use C4::Dates qw/format_date/;
31 use C4::Log; # logaction
35 use vars qw($VERSION @ISA @EXPORT);
41 @ISA = qw( Exporter );
55 &GetBiblioItemByBiblioNumber
56 &GetBiblioFromItemNumber
67 &GetAuthorisedValueDesc
71 &GetPublisherNameFromIsbn
86 # To link headings in a bib record
87 # to authority records.
89 &LinkBibHeadingsToAuthorities
93 # those functions are exported but should not be used
94 # they are usefull is few circumstances, so are exported.
95 # but don't use them unless you're a core developer ;-)
102 &TransformHtmlToMarc2
105 &PrepareItemrecordDisplay
112 C4::Biblio - cataloging management functions
116 Biblio.pm contains functions for managing storage and editing of bibliographic data within Koha. Most of the functions in this module are used for cataloging records: adding, editing, or removing biblios, biblioitems, or items. Koha's stores bibliographic information in three places:
120 =item 1. in the biblio,biblioitems,items, etc tables, which are limited to a one-to-one mapping to underlying MARC data
122 =item 2. as raw MARC in the Zebra index and storage engine
124 =item 3. as raw MARC the biblioitems.marc and biblioitems.marcxml
128 In the 3.0 version of Koha, the authoritative record-level information is in biblioitems.marcxml
130 Because the data isn't completely normalized there's a chance for information to get out of sync. The design choice to go with a un-normalized schema was driven by performance and stability concerns. However, if this occur, it can be considered as a bug : The API is (or should be) complete & the only entry point for all biblio/items managements.
134 =item 1. Compared with MySQL, Zebra is slow to update an index for small data changes -- especially for proc-intensive operations like circulation
136 =item 2. Zebra's index has been known to crash and a backup of the data is necessary to rebuild it in such cases
140 Because of this design choice, the process of managing storage and editing is a bit convoluted. Historically, Biblio.pm's grown to an unmanagable size and as a result we have several types of functions currently:
144 =item 1. Add*/Mod*/Del*/ - high-level external functions suitable for being called from external scripts to manage the collection
146 =item 2. _koha_* - low-level internal functions for managing the koha tables
148 =item 3. Marc management function : as the MARC record is stored in biblioitems.marc(xml), some subs dedicated to it's management are in this package. They should be used only internally by Biblio.pm, the only official entry points being AddBiblio, AddItem, ModBiblio, ModItem.
150 =item 4. Zebra functions used to update the Zebra index
152 =item 5. internal helper functions such as char_decode, checkitems, etc. Some of these probably belong in Koha.pm
156 The MARC record (in biblioitems.marcxml) contains the complete marc record, including items. It also contains the biblionumber. That is the reason why it is not stored directly by AddBiblio, with all other fields . To save a biblio, we need to :
160 =item 1. save datas in biblio and biblioitems table, that gives us a biblionumber and a biblioitemnumber
162 =item 2. add the biblionumber and biblioitemnumber into the MARC records
164 =item 3. save the marc record
168 When dealing with items, we must :
172 =item 1. save the item in items table, that gives us an itemnumber
174 =item 2. add the itemnumber to the item MARC field
176 =item 3. overwrite the MARC record (with the added item) into biblioitems.marc(xml)
178 When modifying a biblio or an item, the behaviour is quite similar.
182 =head1 EXPORTED FUNCTIONS
188 ($biblionumber,$biblioitemnumber) = AddBiblio($record,$frameworkcode);
192 Exported function (core API) for adding a new biblio to koha.
194 The first argument is a C<MARC::Record> object containing the
195 bib to add, while the second argument is the desired MARC
198 This function also accepts a third, optional argument: a hashref
199 to additional options. The only defined option is C<defer_marc_save>,
200 which if present and mapped to a true value, causes C<AddBiblio>
201 to omit the call to save the MARC in C<bibilioitems.marc>
202 and C<biblioitems.marcxml> This option is provided B<only>
203 for the use of scripts such as C<bulkmarcimport.pl> that may need
204 to do some manipulation of the MARC record for item parsing before
205 saving it and which cannot afford the performance hit of saving
206 the MARC record twice. Consequently, do not use that option
207 unless you can guarantee that C<ModBiblioMarc> will be called.
213 my $frameworkcode = shift;
214 my $options = @_ ? shift : undef;
215 my $defer_marc_save = 0;
216 if (defined $options and exists $options->{'defer_marc_save'} and $options->{'defer_marc_save'}) {
217 $defer_marc_save = 1;
220 my ($biblionumber,$biblioitemnumber,$error);
221 my $dbh = C4::Context->dbh;
222 # transform the data into koha-table style data
223 my $olddata = TransformMarcToKoha( $dbh, $record, $frameworkcode );
224 ($biblionumber,$error) = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
225 $olddata->{'biblionumber'} = $biblionumber;
226 ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $olddata );
228 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
231 $biblionumber = ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $defer_marc_save;
233 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","ADD",$biblionumber,"biblio")
234 if C4::Context->preference("CataloguingLog");
236 return ( $biblionumber, $biblioitemnumber );
241 ModBiblio( $record,$biblionumber,$frameworkcode);
242 Exported function (core API) to modify a biblio
247 my ( $record, $biblionumber, $frameworkcode ) = @_;
248 if (C4::Context->preference("CataloguingLog")) {
249 my $newrecord = GetMarcBiblio($biblionumber);
250 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","MODIFY",$biblionumber,"BEFORE=>".$newrecord->as_formatted);
253 my $dbh = C4::Context->dbh;
255 $frameworkcode = "" unless $frameworkcode;
257 # get the items before and append them to the biblio before updating the record, atm we just have the biblio
258 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
259 my $oldRecord = GetMarcBiblio( $biblionumber );
261 # parse each item, and, for an unknown reason, re-encode each subfield
262 # if you don't do that, the record will have encoding mixed
263 # and the biblio will be re-encoded.
264 # strange, I (Paul P.) searched more than 1 day to understand what happends
265 # but could only solve the problem this way...
266 my @fields = $oldRecord->field( $itemtag );
267 foreach my $fielditem ( @fields ){
269 foreach ($fielditem->subfields()) {
271 $field->add_subfields(Encode::encode('utf-8',$_->[0]) => Encode::encode('utf-8',$_->[1]));
273 $field = MARC::Field->new("$itemtag",'','',Encode::encode('utf-8',$_->[0]) => Encode::encode('utf-8',$_->[1]));
276 $record->append_fields($field);
279 # update biblionumber and biblioitemnumber in MARC
280 # FIXME - this is assuming a 1 to 1 relationship between
281 # biblios and biblioitems
282 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
283 $sth->execute($biblionumber);
284 my ($biblioitemnumber) = $sth->fetchrow;
286 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
288 # update the MARC record (that now contains biblio and items) with the new record data
289 &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
291 # load the koha-table data object
292 my $oldbiblio = TransformMarcToKoha( $dbh, $record, $frameworkcode );
294 # modify the other koha tables
295 _koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
296 _koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
300 =head2 ModBiblioframework
302 ModBiblioframework($biblionumber,$frameworkcode);
303 Exported function to modify a biblio framework
307 sub ModBiblioframework {
308 my ( $biblionumber, $frameworkcode ) = @_;
309 my $dbh = C4::Context->dbh;
310 my $sth = $dbh->prepare(
311 "UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"
313 $sth->execute($frameworkcode, $biblionumber);
321 my $error = &DelBiblio($dbh,$biblionumber);
322 Exported function (core API) for deleting a biblio in koha.
323 Deletes biblio record from Zebra and Koha tables (biblio,biblioitems,items)
324 Also backs it up to deleted* tables
325 Checks to make sure there are not issues on any of the items
327 C<$error> : undef unless an error occurs
334 my ( $biblionumber ) = @_;
335 my $dbh = C4::Context->dbh;
336 my $error; # for error handling
338 # First make sure this biblio has no items attached
339 my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber=?");
340 $sth->execute($biblionumber);
341 if (my $itemnumber = $sth->fetchrow){
342 # Fix this to use a status the template can understand
343 $error .= "This Biblio has items attached, please delete them first before deleting this biblio ";
346 return $error if $error;
348 # Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio
349 # for at least 2 reasons :
350 # - we need to read the biblio if NoZebra is set (to remove it from the indexes
351 # - if something goes wrong, the biblio may be deleted from Koha but not from zebra
352 # and we would have no way to remove it (except manually in zebra, but I bet it would be very hard to handle the problem)
353 ModZebra($biblionumber, "recordDelete", "biblioserver", undef);
355 # delete biblioitems and items from Koha tables and save in deletedbiblioitems,deleteditems
358 "SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
359 $sth->execute($biblionumber);
360 while ( my $biblioitemnumber = $sth->fetchrow ) {
362 # delete this biblioitem
363 $error = _koha_delete_biblioitems( $dbh, $biblioitemnumber );
364 return $error if $error;
367 # delete biblio from Koha tables and save in deletedbiblio
368 # must do this *after* _koha_delete_biblioitems, otherwise
369 # delete cascade will prevent deletedbiblioitems rows
370 # from being generated by _koha_delete_biblioitems
371 $error = _koha_delete_biblio( $dbh, $biblionumber );
373 &logaction(C4::Context->userenv->{'number'},"CATALOGUING","DELETE",$biblionumber,"")
374 if C4::Context->preference("CataloguingLog");
378 =head2 LinkBibHeadingsToAuthorities
382 my $headings_linked = LinkBibHeadingsToAuthorities($marc);
386 Links bib headings to authority records by checking
387 each authority-controlled field in the C<MARC::Record>
388 object C<$marc>, looking for a matching authority record,
389 and setting the linking subfield $9 to the ID of that
392 If no matching authority exists, or if multiple
393 authorities match, no $9 will be added, and any
394 existing one inthe field will be deleted.
396 Returns the number of heading links changed in the
401 sub LinkBibHeadingsToAuthorities {
404 my $num_headings_changed = 0;
405 foreach my $field ($bib->fields()) {
406 my $heading = C4::Heading->new_from_bib_field($field);
407 next unless defined $heading;
410 my $current_link = $field->subfield('9');
412 # look for matching authorities
413 my $authorities = $heading->authorities();
415 # want only one exact match
416 if ($#{ $authorities } == 0) {
417 my $authority = MARC::Record->new_from_usmarc($authorities->[0]);
418 my $authid = $authority->field('001')->data();
419 next if defined $current_link and $current_link eq $authid;
421 $field->delete_subfield(code => '9') if defined $current_link;
422 $field->add_subfields('9', $authid);
423 $num_headings_changed++;
425 if (defined $current_link) {
426 $field->delete_subfield(code => '9');
427 $num_headings_changed++;
432 return $num_headings_changed;
439 $data = &GetBiblioData($biblionumber);
440 Returns information about the book with the given biblionumber.
441 C<&GetBiblioData> returns a reference-to-hash. The keys are the fields in
442 the C<biblio> and C<biblioitems> tables in the
444 In addition, C<$data-E<gt>{subject}> is the list of the book's
445 subjects, separated by C<" , "> (space, comma, space).
446 If there are multiple biblioitems with the given biblionumber, only
447 the first one is considered.
455 my $dbh = C4::Context->dbh;
457 # my $query = C4::Context->preference('item-level_itypes') ?
458 # " SELECT * , biblioitems.notes AS bnotes, biblio.notes
460 # LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
461 # WHERE biblio.biblionumber = ?
462 # AND biblioitems.biblionumber = biblio.biblionumber
465 my $query = " SELECT * , biblioitems.notes AS bnotes, itemtypes.notforloan as bi_notforloan, biblio.notes
467 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
468 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
469 WHERE biblio.biblionumber = ?
470 AND biblioitems.biblionumber = biblio.biblionumber ";
472 my $sth = $dbh->prepare($query);
473 $sth->execute($bibnum);
475 $data = $sth->fetchrow_hashref;
479 } # sub GetBiblioData
481 =head2 &GetBiblioItemData
485 $itemdata = &GetBiblioItemData($biblioitemnumber);
487 Looks up the biblioitem with the given biblioitemnumber. Returns a
488 reference-to-hash. The keys are the fields from the C<biblio>,
489 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
490 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
497 sub GetBiblioItemData {
498 my ($biblioitemnumber) = @_;
499 my $dbh = C4::Context->dbh;
500 my $query = "SELECT *,biblioitems.notes AS bnotes
501 FROM biblio LEFT JOIN biblioitems on biblio.biblionumber=biblioitems.biblioitemnumber ";
502 unless(C4::Context->preference('item-level_itypes')) {
503 $query .= "LEFT JOIN itemtypes on biblioitems.itemtype=itemtypes.itemtype ";
505 $query .= " WHERE biblioitemnumber = ? ";
506 my $sth = $dbh->prepare($query);
508 $sth->execute($biblioitemnumber);
509 $data = $sth->fetchrow_hashref;
512 } # sub &GetBiblioItemData
514 =head2 GetBiblioItemByBiblioNumber
518 NOTE : This function has been copy/paste from C4/Biblio.pm from head before zebra integration.
524 sub GetBiblioItemByBiblioNumber {
525 my ($biblionumber) = @_;
526 my $dbh = C4::Context->dbh;
527 my $sth = $dbh->prepare("Select * FROM biblioitems WHERE biblionumber = ?");
531 $sth->execute($biblionumber);
533 while ( my $data = $sth->fetchrow_hashref ) {
534 push @results, $data;
541 =head2 GetBiblioFromItemNumber
545 $item = &GetBiblioFromItemNumber($itemnumber,$barcode);
547 Looks up the item with the given itemnumber. if undef, try the barcode.
549 C<&itemnodata> returns a reference-to-hash whose keys are the fields
550 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
558 sub GetBiblioFromItemNumber {
559 my ( $itemnumber, $barcode ) = @_;
560 my $dbh = C4::Context->dbh;
563 $sth=$dbh->prepare( "SELECT * FROM items
564 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
565 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
566 WHERE items.itemnumber = ?") ;
567 $sth->execute($itemnumber);
569 $sth=$dbh->prepare( "SELECT * FROM items
570 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
571 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
572 WHERE items.barcode = ?") ;
573 $sth->execute($barcode);
575 my $data = $sth->fetchrow_hashref;
584 ( $count, @results ) = &GetBiblio($biblionumber);
591 my ($biblionumber) = @_;
592 my $dbh = C4::Context->dbh;
593 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber = ?");
596 $sth->execute($biblionumber);
597 while ( my $data = $sth->fetchrow_hashref ) {
598 $results[$count] = $data;
602 return ( $count, @results );
605 =head2 GetBiblioItemInfosOf
609 GetBiblioItemInfosOf(@biblioitemnumbers);
615 sub GetBiblioItemInfosOf {
616 my @biblioitemnumbers = @_;
619 SELECT biblioitemnumber,
623 WHERE biblioitemnumber IN (' . join( ',', @biblioitemnumbers ) . ')
625 return get_infos_of( $query, 'biblioitemnumber' );
628 =head1 FUNCTIONS FOR HANDLING MARC MANAGEMENT
630 =head2 GetMarcStructure
634 $res = GetMarcStructure($forlibrarian,$frameworkcode);
636 Returns a reference to a big hash of hash, with the Marc structure for the given frameworkcode
637 $forlibrarian :if set to 1, the MARC descriptions are the librarians ones, otherwise it's the public (OPAC) ones
638 $frameworkcode : the framework code to read
644 # cache for results of GetMarcStructure -- needed
646 our $marc_structure_cache;
648 sub GetMarcStructure {
649 my ( $forlibrarian, $frameworkcode ) = @_;
650 my $dbh=C4::Context->dbh;
651 $frameworkcode = "" unless $frameworkcode;
653 if (defined $marc_structure_cache and exists $marc_structure_cache->{$forlibrarian}->{$frameworkcode}) {
654 return $marc_structure_cache->{$forlibrarian}->{$frameworkcode};
658 my $libfield = ( $forlibrarian eq 1 ) ? 'liblibrarian' : 'libopac';
660 # check that framework exists
663 "SELECT COUNT(*) FROM marc_tag_structure WHERE frameworkcode=?");
664 $sth->execute($frameworkcode);
665 my ($total) = $sth->fetchrow;
666 $frameworkcode = "" unless ( $total > 0 );
669 "SELECT tagfield,liblibrarian,libopac,mandatory,repeatable
670 FROM marc_tag_structure
671 WHERE frameworkcode=?
674 $sth->execute($frameworkcode);
675 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
677 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) =
680 $res->{$tag}->{lib} =
681 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
682 $res->{$tab}->{tab} = "";
683 $res->{$tag}->{mandatory} = $mandatory;
684 $res->{$tag}->{repeatable} = $repeatable;
689 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab,mandatory,repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl,link,defaultvalue
690 FROM marc_subfield_structure
691 WHERE frameworkcode=?
692 ORDER BY tagfield,tagsubfield
696 $sth->execute($frameworkcode);
699 my $authorised_value;
711 $tag, $subfield, $liblibrarian,
713 $mandatory, $repeatable, $authorised_value,
714 $authtypecode, $value_builder, $kohafield,
715 $seealso, $hidden, $isurl,
721 $res->{$tag}->{$subfield}->{lib} =
722 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
723 $res->{$tag}->{$subfield}->{tab} = $tab;
724 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
725 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
726 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
727 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
728 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
729 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
730 $res->{$tag}->{$subfield}->{seealso} = $seealso;
731 $res->{$tag}->{$subfield}->{hidden} = $hidden;
732 $res->{$tag}->{$subfield}->{isurl} = $isurl;
733 $res->{$tag}->{$subfield}->{'link'} = $link;
734 $res->{$tag}->{$subfield}->{defaultvalue} = $defaultvalue;
737 $marc_structure_cache->{$forlibrarian}->{$frameworkcode} = $res;
742 =head2 GetUsedMarcStructure
744 the same function as GetMarcStructure expcet it just take field
745 in tab 0-9. (used field)
747 my $results = GetUsedMarcStructure($frameworkcode);
749 L<$results> is a ref to an array which each case containts a ref
750 to a hash which each keys is the columns from marc_subfield_structure
752 L<$frameworkcode> is the framework code.
756 sub GetUsedMarcStructure($){
757 my $frameworkcode = shift || '';
758 my $dbh = C4::Context->dbh;
761 FROM marc_subfield_structure
763 AND frameworkcode = ?
766 my $sth = $dbh->prepare($query);
767 $sth->execute($frameworkcode);
768 while (my $row = $sth->fetchrow_hashref){
774 =head2 GetMarcFromKohaField
778 ($MARCfield,$MARCsubfield)=GetMarcFromKohaField($kohafield,$frameworkcode);
779 Returns the MARC fields & subfields mapped to the koha field
780 for the given frameworkcode
786 sub GetMarcFromKohaField {
787 my ( $kohafield, $frameworkcode ) = @_;
788 return 0, 0 unless $kohafield;
789 my $relations = C4::Context->marcfromkohafield;
791 $relations->{$frameworkcode}->{$kohafield}->[0],
792 $relations->{$frameworkcode}->{$kohafield}->[1]
800 Returns MARC::Record of the biblionumber passed in parameter.
801 the marc record contains both biblio & item datas
808 my $biblionumber = shift;
809 my $dbh = C4::Context->dbh;
811 $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
812 $sth->execute($biblionumber);
813 my ($marcxml) = $sth->fetchrow;
814 MARC::File::XML->default_record_format(C4::Context->preference('marcflavour'));
815 $marcxml =~ s/\x1e//g;
816 $marcxml =~ s/\x1f//g;
817 $marcxml =~ s/\x1d//g;
818 $marcxml =~ s/\x0f//g;
819 $marcxml =~ s/\x0c//g;
821 my $record = MARC::Record->new();
823 $record = eval {MARC::Record::new_from_xml( $marcxml, "utf8", C4::Context->preference('marcflavour'))};
824 if ($@) {warn " problem with :$biblionumber : $@ \n$marcxml";}
825 # $record = MARC::Record::new_from_usmarc( $marc) if $marc;
836 my $marcxml = GetXmlBiblio($biblionumber);
838 Returns biblioitems.marcxml of the biblionumber passed in parameter.
839 The XML contains both biblio & item datas
846 my ( $biblionumber ) = @_;
847 my $dbh = C4::Context->dbh;
849 $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
850 $sth->execute($biblionumber);
851 my ($marcxml) = $sth->fetchrow;
855 =head2 GetAuthorisedValueDesc
859 my $subfieldvalue =get_authorised_value_desc(
860 $tag, $subf[$i][0],$subf[$i][1], '', $taglib, $category);
861 Retrieve the complete description for a given authorised value.
863 Now takes $category and $value pair too.
864 my $auth_value_desc =GetAuthorisedValueDesc(
865 '','', 'DVD' ,'','','CCODE');
871 sub GetAuthorisedValueDesc {
872 my ( $tag, $subfield, $value, $framework, $tagslib, $category ) = @_;
873 my $dbh = C4::Context->dbh;
877 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
878 return C4::Branch::GetBranchName($value);
882 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
883 return getitemtypeinfo($value)->{description};
886 #---- "true" authorized value
887 $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'}
890 if ( $category ne "" ) {
893 "SELECT lib FROM authorised_values WHERE category = ? AND authorised_value = ?"
895 $sth->execute( $category, $value );
896 my $data = $sth->fetchrow_hashref;
897 return $data->{'lib'};
900 return $value; # if nothing is found return the original value
908 $marcnotesarray = GetMarcNotes( $record, $marcflavour );
909 Get all notes from the MARC record and returns them in an array.
910 The note are stored in differents places depending on MARC flavour
917 my ( $record, $marcflavour ) = @_;
919 if ( $marcflavour eq "MARC21" ) {
922 else { # assume unimarc if not marc21
929 foreach my $field ( $record->field($scope) ) {
930 my $value = $field->as_string();
932 $marcnote = { marcnote => $note, };
933 push @marcnotes, $marcnote;
936 if ( $note ne $value ) {
937 $note = $note . " " . $value;
942 $marcnote = { marcnote => $note };
943 push @marcnotes, $marcnote; #load last tag into array
948 =head2 GetMarcSubjects
952 $marcsubjcts = GetMarcSubjects($record,$marcflavour);
953 Get all subjects from the MARC record and returns them in an array.
954 The subjects are stored in differents places depending on MARC flavour
960 sub GetMarcSubjects {
961 my ( $record, $marcflavour ) = @_;
962 my ( $mintag, $maxtag );
963 if ( $marcflavour eq "MARC21" ) {
967 else { # assume unimarc if not marc21
977 foreach my $field ( $record->field('6..' )) {
978 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
980 my @subfields = $field->subfields();
983 # if there is an authority link, build the link with an= subfield9
984 my $subfield9 = $field->subfield('9');
985 for my $subject_subfield (@subfields ) {
986 # don't load unimarc subfields 3,4,5
987 next if (($marcflavour eq "UNIMARC") and ($subject_subfield->[0] =~ (3|4|5) ) );
988 my $code = $subject_subfield->[0];
989 my $value = $subject_subfield->[1];
990 my $linkvalue = $value;
991 $linkvalue =~ s/(\(|\))//g;
992 my $operator = " and " unless $counter==0;
994 @link_loop = ({'limit' => 'an' ,link => "$subfield9" });
996 push @link_loop, {'limit' => 'su', link => $linkvalue, operator => $operator };
998 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
1000 my @this_link_loop = @link_loop;
1001 push @subfields_loop, {code => $code, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($subject_subfield->[0] == 9 );
1005 push @marcsubjects, { MARCSUBJECT_SUBFIELDS_LOOP => \@subfields_loop };
1008 return \@marcsubjects;
1009 } #end getMARCsubjects
1011 =head2 GetMarcAuthors
1015 authors = GetMarcAuthors($record,$marcflavour);
1016 Get all authors from the MARC record and returns them in an array.
1017 The authors are stored in differents places depending on MARC flavour
1023 sub GetMarcAuthors {
1024 my ( $record, $marcflavour ) = @_;
1025 my ( $mintag, $maxtag );
1026 # tagslib useful for UNIMARC author reponsabilities
1027 my $tagslib = &GetMarcStructure( 1, '' ); # FIXME : we don't have the framework available, we take the default framework. May be bugguy on some setups, will be usually correct.
1028 if ( $marcflavour eq "MARC21" ) {
1032 elsif ( $marcflavour eq "UNIMARC" ) { # assume unimarc if not marc21
1041 foreach my $field ( $record->fields ) {
1042 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1045 my @subfields = $field->subfields();
1047 # if there is an authority link, build the link with Koha-Auth-Number: subfield9
1048 my $subfield9 = $field->subfield('9');
1049 for my $authors_subfield (@subfields) {
1050 # don't load unimarc subfields 3, 5
1051 next if ($marcflavour eq 'UNIMARC' and ($authors_subfield->[0] =~ (3|5) ) );
1052 my $subfieldcode = $authors_subfield->[0];
1053 my $value = $authors_subfield->[1];
1054 my $linkvalue = $value;
1055 $linkvalue =~ s/(\(|\))//g;
1056 my $operator = " and " unless $count_auth==0;
1057 # if we have an authority link, use that as the link, otherwise use standard searching
1059 @link_loop = ({'limit' => 'Koha-Auth-Number' ,link => "$subfield9" });
1062 # reset $linkvalue if UNIMARC author responsibility
1063 if ( $marcflavour eq 'UNIMARC' and ($authors_subfield->[0] eq "4")) {
1064 $linkvalue = "(".GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ).")";
1066 push @link_loop, {'limit' => 'au', link => $linkvalue, operator => $operator };
1068 $value = GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ) if ( $marcflavour eq 'UNIMARC' and ($authors_subfield->[0] =~/4/));
1069 my @this_link_loop = @link_loop;
1070 my $separator = C4::Context->preference("authoritysep") unless $count_auth==0;
1071 push @subfields_loop, {code => $subfieldcode, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($authors_subfield->[0] == 9 );
1074 push @marcauthors, { MARCAUTHOR_SUBFIELDS_LOOP => \@subfields_loop };
1076 return \@marcauthors;
1083 $marcurls = GetMarcUrls($record,$marcflavour);
1084 Returns arrayref of URLs from MARC data, suitable to pass to tmpl loop.
1085 Assumes web resources (not uncommon in MARC21 to omit resource type ind)
1092 my ($record, $marcflavour) = @_;
1095 for my $field ($record->field('856')) {
1096 my $url = $field->subfield('u');
1098 for my $note ( $field->subfield('z')) {
1099 push @notes , {note => $note};
1101 $marcurl = { MARCURL => $url,
1104 if($marcflavour eq 'MARC21') {
1105 my $s3 = $field->subfield('3');
1106 my $link = $field->subfield('y');
1107 $marcurl->{'linktext'} = $link || $s3 || $url ;;
1108 $marcurl->{'part'} = $s3 if($link);
1109 $marcurl->{'toc'} = 1 if($s3 =~ /^[Tt]able/) ;
1111 $marcurl->{'linktext'} = $url;
1113 push @marcurls, $marcurl;
1118 =head2 GetMarcSeries
1122 $marcseriesarray = GetMarcSeries($record,$marcflavour);
1123 Get all series from the MARC record and returns them in an array.
1124 The series are stored in differents places depending on MARC flavour
1131 my ($record, $marcflavour) = @_;
1132 my ($mintag, $maxtag);
1133 if ($marcflavour eq "MARC21") {
1136 } else { # assume unimarc if not marc21
1146 foreach my $field ($record->field('440'), $record->field('490')) {
1148 #my $value = $field->subfield('a');
1149 #$marcsubjct = {MARCSUBJCT => $value,};
1150 my @subfields = $field->subfields();
1151 #warn "subfields:".join " ", @$subfields;
1154 for my $series_subfield (@subfields) {
1156 undef $volume_number;
1157 # see if this is an instance of a volume
1158 if ($series_subfield->[0] eq 'v') {
1162 my $code = $series_subfield->[0];
1163 my $value = $series_subfield->[1];
1164 my $linkvalue = $value;
1165 $linkvalue =~ s/(\(|\))//g;
1166 my $operator = " and " unless $counter==0;
1167 push @link_loop, {link => $linkvalue, operator => $operator };
1168 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
1169 if ($volume_number) {
1170 push @subfields_loop, {volumenum => $value};
1173 push @subfields_loop, {code => $code, value => $value, link_loop => \@link_loop, separator => $separator, volumenum => $volume_number};
1177 push @marcseries, { MARCSERIES_SUBFIELDS_LOOP => \@subfields_loop };
1178 #$marcsubjct = {MARCSUBJCT => $field->as_string(),};
1179 #push @marcsubjcts, $marcsubjct;
1183 my $marcseriessarray=\@marcseries;
1184 return $marcseriessarray;
1185 } #end getMARCseriess
1187 =head2 GetFrameworkCode
1191 $frameworkcode = GetFrameworkCode( $biblionumber )
1197 sub GetFrameworkCode {
1198 my ( $biblionumber ) = @_;
1199 my $dbh = C4::Context->dbh;
1200 my $sth = $dbh->prepare("SELECT frameworkcode FROM biblio WHERE biblionumber=?");
1201 $sth->execute($biblionumber);
1202 my ($frameworkcode) = $sth->fetchrow;
1203 return $frameworkcode;
1206 =head2 GetPublisherNameFromIsbn
1208 $name = GetPublishercodeFromIsbn($isbn);
1215 sub GetPublisherNameFromIsbn($){
1217 $isbn =~ s/[- _]//g;
1219 my @codes = (split '-', DisplayISBN($isbn));
1220 my $code = $codes[0].$codes[1].$codes[2];
1221 my $dbh = C4::Context->dbh;
1223 SELECT distinct publishercode
1226 AND publishercode IS NOT NULL
1229 my $sth = $dbh->prepare($query);
1230 $sth->execute("$code%");
1231 my $name = $sth->fetchrow;
1232 return $name if length $name;
1236 =head2 TransformKohaToMarc
1240 $record = TransformKohaToMarc( $hash )
1241 This function builds partial MARC::Record from a hash
1242 Hash entries can be from biblio or biblioitems.
1243 This function is called in acquisition module, to create a basic catalogue entry from user entry
1249 sub TransformKohaToMarc {
1252 my $dbh = C4::Context->dbh;
1255 "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
1257 my $record = MARC::Record->new();
1258 foreach (keys %{$hash}) {
1259 &TransformKohaToMarcOneField( $sth, $record, $_,
1265 =head2 TransformKohaToMarcOneField
1269 $record = TransformKohaToMarcOneField( $sth, $record, $kohafieldname, $value, $frameworkcode );
1275 sub TransformKohaToMarcOneField {
1276 my ( $sth, $record, $kohafieldname, $value, $frameworkcode ) = @_;
1277 $frameworkcode='' unless $frameworkcode;
1281 if ( !defined $sth ) {
1282 my $dbh = C4::Context->dbh;
1283 $sth = $dbh->prepare(
1284 "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
1287 $sth->execute( $frameworkcode, $kohafieldname );
1288 if ( ( $tagfield, $tagsubfield ) = $sth->fetchrow ) {
1289 my $tag = $record->field($tagfield);
1291 $tag->update( $tagsubfield => $value );
1292 $record->delete_field($tag);
1293 $record->insert_fields_ordered($tag);
1296 $record->add_fields( $tagfield, " ", " ", $tagsubfield => $value );
1302 =head2 TransformHtmlToXml
1306 $xml = TransformHtmlToXml( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type )
1308 $auth_type contains :
1309 - nothing : rebuild a biblio, un UNIMARC the encoding is in 100$a pos 26/27
1310 - UNIMARCAUTH : rebuild an authority. In UNIMARC, the encoding is in 100$a pos 13/14
1311 - ITEM : rebuild an item : in UNIMARC, 100$a, it's in the biblio ! (otherwise, we would get 2 100 fields !)
1317 sub TransformHtmlToXml {
1318 my ( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type ) = @_;
1319 my $xml = MARC::File::XML::header('UTF-8');
1320 $auth_type = C4::Context->preference('marcflavour') unless $auth_type;
1321 MARC::File::XML->default_record_format($auth_type);
1322 # in UNIMARC, field 100 contains the encoding
1323 # check that there is one, otherwise the
1324 # MARC::Record->new_from_xml will fail (and Koha will die)
1325 my $unimarc_and_100_exist=0;
1326 $unimarc_and_100_exist=1 if $auth_type eq 'ITEM'; # if we rebuild an item, no need of a 100 field
1331 for ( my $i = 0 ; $i <= @$tags ; $i++ ) {
1332 if (C4::Context->preference('marcflavour') eq 'UNIMARC' and @$tags[$i] eq "100" and @$subfields[$i] eq "a") {
1333 # if we have a 100 field and it's values are not correct, skip them.
1334 # if we don't have any valid 100 field, we will create a default one at the end
1335 my $enc = substr( @$values[$i], 26, 2 );
1336 if ($enc eq '01' or $enc eq '50' or $enc eq '03') {
1337 $unimarc_and_100_exist=1;
1342 @$values[$i] =~ s/&/&/g;
1343 @$values[$i] =~ s/</</g;
1344 @$values[$i] =~ s/>/>/g;
1345 @$values[$i] =~ s/"/"/g;
1346 @$values[$i] =~ s/'/'/g;
1347 # if ( !utf8::is_utf8( @$values[$i] ) ) {
1348 # utf8::decode( @$values[$i] );
1350 if ( ( @$tags[$i] ne $prevtag ) ) {
1351 $j++ unless ( @$tags[$i] eq "" );
1353 $xml .= "</datafield>\n";
1354 if ( ( @$tags[$i] && @$tags[$i] > 10 )
1355 && ( @$values[$i] ne "" ) )
1357 my $ind1 = substr( @$indicator[$j], 0, 1 );
1359 if ( @$indicator[$j] ) {
1360 $ind2 = substr( @$indicator[$j], 1, 1 );
1363 warn "Indicator in @$tags[$i] is empty";
1367 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1369 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1377 if ( @$values[$i] ne "" ) {
1380 if ( @$tags[$i] eq "000" ) {
1381 $xml .= "<leader>@$values[$i]</leader>\n";
1384 # rest of the fixed fields
1386 elsif ( @$tags[$i] < 10 ) {
1388 "<controlfield tag=\"@$tags[$i]\">@$values[$i]</controlfield>\n";
1392 my $ind1 = substr( @$indicator[$j], 0, 1 );
1393 my $ind2 = substr( @$indicator[$j], 1, 1 );
1395 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1397 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1403 else { # @$tags[$i] eq $prevtag
1404 if ( @$values[$i] eq "" ) {
1408 my $ind1 = substr( @$indicator[$j], 0, 1 );
1409 my $ind2 = substr( @$indicator[$j], 1, 1 );
1411 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1415 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1418 $prevtag = @$tags[$i];
1420 if (C4::Context->preference('marcflavour') eq 'UNIMARC' and !$unimarc_and_100_exist) {
1421 # warn "SETTING 100 for $auth_type";
1422 use POSIX qw(strftime);
1423 my $string = strftime( "%Y%m%d", localtime(time) );
1424 # set 50 to position 26 is biblios, 13 if authorities
1426 $pos=13 if $auth_type eq 'UNIMARCAUTH';
1427 $string = sprintf( "%-*s", 35, $string );
1428 substr( $string, $pos , 6, "50" );
1429 $xml .= "<datafield tag=\"100\" ind1=\"\" ind2=\"\">\n";
1430 $xml .= "<subfield code=\"a\">$string</subfield>\n";
1431 $xml .= "</datafield>\n";
1433 $xml .= MARC::File::XML::footer();
1437 =head2 TransformHtmlToMarc
1439 L<$record> = TransformHtmlToMarc(L<$params>,L<$cgi>)
1440 L<$params> is a ref to an array as below:
1442 'tag_010_indicator_531951' ,
1443 'tag_010_code_a_531951_145735' ,
1444 'tag_010_subfield_a_531951_145735' ,
1445 'tag_200_indicator_873510' ,
1446 'tag_200_code_a_873510_673465' ,
1447 'tag_200_subfield_a_873510_673465' ,
1448 'tag_200_code_b_873510_704318' ,
1449 'tag_200_subfield_b_873510_704318' ,
1450 'tag_200_code_e_873510_280822' ,
1451 'tag_200_subfield_e_873510_280822' ,
1452 'tag_200_code_f_873510_110730' ,
1453 'tag_200_subfield_f_873510_110730' ,
1455 L<$cgi> is the CGI object which containts the value.
1456 L<$record> is the MARC::Record object.
1460 sub TransformHtmlToMarc {
1464 # creating a new record
1465 my $record = MARC::Record->new();
1468 while ($params->[$i]){ # browse all CGI params
1469 my $param = $params->[$i];
1471 # if we are on biblionumber, store it in the MARC::Record (it may not be in the edited fields)
1472 if ($param eq 'biblionumber') {
1473 my ( $biblionumbertagfield, $biblionumbertagsubfield ) =
1474 &GetMarcFromKohaField( "biblio.biblionumber", '' );
1475 if ($biblionumbertagfield < 10) {
1476 $newfield = MARC::Field->new(
1477 $biblionumbertagfield,
1478 $cgi->param($param),
1481 $newfield = MARC::Field->new(
1482 $biblionumbertagfield,
1485 "$biblionumbertagsubfield" => $cgi->param($param),
1488 push @fields,$newfield if($newfield);
1490 elsif ($param =~ /^tag_(\d*)_indicator_/){ # new field start when having 'input name="..._indicator_..."
1493 my $ind1 = substr($cgi->param($param),0,1);
1494 my $ind2 = substr($cgi->param($param),1,1);
1498 if($tag < 10){ # no code for theses fields
1499 # in MARC editor, 000 contains the leader.
1500 if ($tag eq '000' ) {
1501 $record->leader($cgi->param($params->[$j+1])) if length($cgi->param($params->[$j+1]))==24;
1502 # between 001 and 009 (included)
1504 $newfield = MARC::Field->new(
1506 $cgi->param($params->[$j+1]),
1509 # > 009, deal with subfields
1511 while($params->[$j] =~ /_code_/){ # browse all it's subfield
1512 my $inner_param = $params->[$j];
1514 if($cgi->param($params->[$j+1])){ # only if there is a value (code => value)
1515 $newfield->add_subfields(
1516 $cgi->param($inner_param) => $cgi->param($params->[$j+1])
1520 if ( $cgi->param($params->[$j+1]) ) { # creating only if there is a value (code => value)
1521 $newfield = MARC::Field->new(
1525 $cgi->param($inner_param) => $cgi->param($params->[$j+1]),
1532 push @fields,$newfield if($newfield);
1537 $record->append_fields(@fields);
1541 # cache inverted MARC field map
1542 our $inverted_field_map;
1544 =head2 TransformMarcToKoha
1548 $result = TransformMarcToKoha( $dbh, $record, $frameworkcode )
1552 Extract data from a MARC bib record into a hashref representing
1553 Koha biblio, biblioitems, and items fields.
1556 sub TransformMarcToKoha {
1557 my ( $dbh, $record, $frameworkcode, $limit_table ) = @_;
1561 unless (defined $inverted_field_map) {
1562 $inverted_field_map = _get_inverted_marc_field_map();
1566 if ($limit_table eq 'items') {
1567 $tables{'items'} = 1;
1569 $tables{'items'} = 1;
1570 $tables{'biblio'} = 1;
1571 $tables{'biblioitems'} = 1;
1574 # traverse through record
1575 MARCFIELD: foreach my $field ($record->fields()) {
1576 my $tag = $field->tag();
1577 next MARCFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag};
1578 if ($field->is_control_field()) {
1579 my $kohafields = $inverted_field_map->{$frameworkcode}->{$tag}->{list};
1580 ENTRY: foreach my $entry (@{ $kohafields }) {
1581 my ($subfield, $table, $column) = @{ $entry };
1582 next ENTRY unless exists $tables{$table};
1583 my $key = _disambiguate($table, $column);
1584 if ($result->{$key}) {
1585 unless (($key eq "biblionumber" or $key eq "biblioitemnumber") and ($field->data() eq "")) {
1586 $result->{$key} .= " | " . $field->data();
1589 $result->{$key} = $field->data();
1593 # deal with subfields
1594 MARCSUBFIELD: foreach my $sf ($field->subfields()) {
1595 my $code = $sf->[0];
1596 next MARCSUBFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code};
1597 my $value = $sf->[1];
1598 SFENTRY: foreach my $entry (@{ $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code} }) {
1599 my ($table, $column) = @{ $entry };
1600 next SFENTRY unless exists $tables{$table};
1601 my $key = _disambiguate($table, $column);
1602 if ($result->{$key}) {
1603 unless (($key eq "biblionumber" or $key eq "biblioitemnumber") and ($value eq "")) {
1604 $result->{$key} .= " | " . $value;
1607 $result->{$key} = $value;
1614 # modify copyrightdate to keep only the 1st year found
1615 if (exists $result->{'copyrightdate'}) {
1616 my $temp = $result->{'copyrightdate'};
1617 $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
1619 $result->{'copyrightdate'} = $1;
1621 else { # if no cYYYY, get the 1st date.
1622 $temp =~ m/(\d\d\d\d)/;
1623 $result->{'copyrightdate'} = $1;
1627 # modify publicationyear to keep only the 1st year found
1628 if (exists $result->{'publicationyear'}) {
1629 my $temp = $result->{'publicationyear'};
1630 $temp =~ m/c(\d\d\d\d)/; # search cYYYY first
1632 $result->{'publicationyear'} = $1;
1634 else { # if no cYYYY, get the 1st date.
1635 $temp =~ m/(\d\d\d\d)/;
1636 $result->{'publicationyear'} = $1;
1643 sub _get_inverted_marc_field_map {
1645 my $relations = C4::Context->marcfromkohafield;
1647 foreach my $frameworkcode (keys %{ $relations }) {
1648 foreach my $kohafield (keys %{ $relations->{$frameworkcode} }) {
1649 my $tag = $relations->{$frameworkcode}->{$kohafield}->[0];
1650 my $subfield = $relations->{$frameworkcode}->{$kohafield}->[1];
1651 my ($table, $column) = split /[.]/, $kohafield, 2;
1652 push @{ $field_map->{$frameworkcode}->{$tag}->{list} }, [ $subfield, $table, $column ];
1653 push @{ $field_map->{$frameworkcode}->{$tag}->{sfs}->{$subfield} }, [ $table, $column ];
1659 =head2 _disambiguate
1663 $newkey = _disambiguate($table, $field);
1665 This is a temporary hack to distinguish between the
1666 following sets of columns when using TransformMarcToKoha.
1668 items.cn_source & biblioitems.cn_source
1669 items.cn_sort & biblioitems.cn_sort
1671 Columns that are currently NOT distinguished (FIXME
1672 due to lack of time to fully test) are:
1674 biblio.notes and biblioitems.notes
1679 FIXME - this is necessary because prefixing each column
1680 name with the table name would require changing lots
1681 of code and templates, and exposing more of the DB
1682 structure than is good to the UI templates, particularly
1683 since biblio and bibloitems may well merge in a future
1684 version. In the future, it would also be good to
1685 separate DB access and UI presentation field names
1693 my ($table, $column) = @_;
1694 if ($column eq "cn_sort" or $column eq "cn_source") {
1695 return $table . '.' . $column;
1702 =head2 get_koha_field_from_marc
1706 $result->{_disambiguate($table, $field)} = get_koha_field_from_marc($table,$field,$record,$frameworkcode);
1708 Internal function to map data from the MARC record to a specific non-MARC field.
1709 FIXME: this is meant to replace TransformMarcToKohaOneField after more testing.
1715 sub get_koha_field_from_marc {
1716 my ($koha_table,$koha_column,$record,$frameworkcode) = @_;
1717 my ( $tagfield, $subfield ) = GetMarcFromKohaField( $koha_table.'.'.$koha_column, $frameworkcode );
1719 foreach my $field ( $record->field($tagfield) ) {
1720 if ( $field->tag() < 10 ) {
1722 $kohafield .= " | " . $field->data();
1725 $kohafield = $field->data();
1729 if ( $field->subfields ) {
1730 my @subfields = $field->subfields();
1731 foreach my $subfieldcount ( 0 .. $#subfields ) {
1732 if ( $subfields[$subfieldcount][0] eq $subfield ) {
1735 " | " . $subfields[$subfieldcount][1];
1739 $subfields[$subfieldcount][1];
1750 =head2 TransformMarcToKohaOneField
1754 $result = TransformMarcToKohaOneField( $kohatable, $kohafield, $record, $result, $frameworkcode )
1760 sub TransformMarcToKohaOneField {
1762 # FIXME ? if a field has a repeatable subfield that is used in old-db,
1763 # only the 1st will be retrieved...
1764 my ( $kohatable, $kohafield, $record, $result, $frameworkcode ) = @_;
1766 my ( $tagfield, $subfield ) =
1767 GetMarcFromKohaField( $kohatable . "." . $kohafield,
1769 foreach my $field ( $record->field($tagfield) ) {
1770 if ( $field->tag() < 10 ) {
1771 if ( $result->{$kohafield} ) {
1772 $result->{$kohafield} .= " | " . $field->data();
1775 $result->{$kohafield} = $field->data();
1779 if ( $field->subfields ) {
1780 my @subfields = $field->subfields();
1781 foreach my $subfieldcount ( 0 .. $#subfields ) {
1782 if ( $subfields[$subfieldcount][0] eq $subfield ) {
1783 if ( $result->{$kohafield} ) {
1784 $result->{$kohafield} .=
1785 " | " . $subfields[$subfieldcount][1];
1788 $result->{$kohafield} =
1789 $subfields[$subfieldcount][1];
1799 =head1 OTHER FUNCTIONS
1802 =head2 PrepareItemrecordDisplay
1806 PrepareItemrecordDisplay($itemrecord,$bibnum,$itemumber);
1808 Returns a hash with all the fields for Display a given item data in a template
1814 sub PrepareItemrecordDisplay {
1816 my ( $bibnum, $itemnum ) = @_;
1818 my $dbh = C4::Context->dbh;
1819 my $frameworkcode = &GetFrameworkCode( $bibnum );
1820 my ( $itemtagfield, $itemtagsubfield ) =
1821 &GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
1822 my $tagslib = &GetMarcStructure( 1, $frameworkcode );
1823 my $itemrecord = C4::Items::GetMarcItem( $bibnum, $itemnum) if ($itemnum);
1825 my $authorised_values_sth =
1827 "SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib"
1829 foreach my $tag ( sort keys %{$tagslib} ) {
1830 my $previous_tag = '';
1832 # loop through each subfield
1834 foreach my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
1835 next if ( subfield_is_koha_internal_p($subfield) );
1836 next if ( $tagslib->{$tag}->{$subfield}->{'tab'} ne "10" );
1838 $subfield_data{tag} = $tag;
1839 $subfield_data{subfield} = $subfield;
1840 $subfield_data{countsubfield} = $cntsubf++;
1841 $subfield_data{kohafield} =
1842 $tagslib->{$tag}->{$subfield}->{'kohafield'};
1844 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
1845 $subfield_data{marc_lib} =
1846 "<span id=\"error\" title=\""
1847 . $tagslib->{$tag}->{$subfield}->{lib} . "\">"
1848 . substr( $tagslib->{$tag}->{$subfield}->{lib}, 0, 12 )
1850 $subfield_data{mandatory} =
1851 $tagslib->{$tag}->{$subfield}->{mandatory};
1852 $subfield_data{repeatable} =
1853 $tagslib->{$tag}->{$subfield}->{repeatable};
1854 $subfield_data{hidden} = "display:none"
1855 if $tagslib->{$tag}->{$subfield}->{hidden};
1857 ( $x, $value ) = _find_value( $tag, $subfield, $itemrecord )
1859 $value =~ s/"/"/g;
1861 # search for itemcallnumber if applicable
1862 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
1863 'items.itemcallnumber'
1864 && C4::Context->preference('itemcallnumber') )
1867 substr( C4::Context->preference('itemcallnumber'), 0, 3 );
1869 substr( C4::Context->preference('itemcallnumber'), 3, 1 );
1870 my $temp = $itemrecord->field($CNtag) if ($itemrecord);
1872 $value = $temp->subfield($CNsubfield);
1875 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
1876 my @authorised_values;
1879 # builds list, depending on authorised value...
1881 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq
1884 if ( ( C4::Context->preference("IndependantBranches") )
1885 && ( C4::Context->userenv->{flags} != 1 ) )
1889 "SELECT branchcode,branchname FROM branches WHERE branchcode = ? ORDER BY branchname"
1891 $sth->execute( C4::Context->userenv->{branch} );
1892 push @authorised_values, ""
1894 $tagslib->{$tag}->{$subfield}->{mandatory} );
1895 while ( my ( $branchcode, $branchname ) =
1896 $sth->fetchrow_array )
1898 push @authorised_values, $branchcode;
1899 $authorised_lib{$branchcode} = $branchname;
1905 "SELECT branchcode,branchname FROM branches ORDER BY branchname"
1908 push @authorised_values, ""
1910 $tagslib->{$tag}->{$subfield}->{mandatory} );
1911 while ( my ( $branchcode, $branchname ) =
1912 $sth->fetchrow_array )
1914 push @authorised_values, $branchcode;
1915 $authorised_lib{$branchcode} = $branchname;
1921 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq
1926 "SELECT itemtype,description FROM itemtypes ORDER BY description"
1929 push @authorised_values, ""
1930 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
1931 while ( my ( $itemtype, $description ) =
1932 $sth->fetchrow_array )
1934 push @authorised_values, $itemtype;
1935 $authorised_lib{$itemtype} = $description;
1938 #---- "true" authorised value
1941 $authorised_values_sth->execute(
1942 $tagslib->{$tag}->{$subfield}->{authorised_value} );
1943 push @authorised_values, ""
1944 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
1945 while ( my ( $value, $lib ) =
1946 $authorised_values_sth->fetchrow_array )
1948 push @authorised_values, $value;
1949 $authorised_lib{$value} = $lib;
1952 $subfield_data{marc_value} = CGI::scrolling_list(
1953 -name => 'field_value',
1954 -values => \@authorised_values,
1955 -default => "$value",
1956 -labels => \%authorised_lib,
1962 elsif ( $tagslib->{$tag}->{$subfield}->{thesaurus_category} ) {
1963 $subfield_data{marc_value} =
1964 "<input type=\"text\" name=\"field_value\" size=47 maxlength=255> <a href=\"javascript:Dopop('cataloguing/thesaurus_popup.pl?category=$tagslib->{$tag}->{$subfield}->{thesaurus_category}&index=',)\">...</a>";
1967 # COMMENTED OUT because No $i is provided with this API.
1968 # And thus, no value_builder can be activated.
1969 # BUT could be thought over.
1970 # } elsif ($tagslib->{$tag}->{$subfield}->{'value_builder'}) {
1971 # my $plugin="value_builder/".$tagslib->{$tag}->{$subfield}->{'value_builder'};
1973 # my $extended_param = plugin_parameters($dbh,$itemrecord,$tagslib,$i,0);
1974 # my ($function_name,$javascript) = plugin_javascript($dbh,$record,$tagslib,$i,0);
1975 # $subfield_data{marc_value}="<input type=\"text\" value=\"$value\" name=\"field_value\" size=47 maxlength=255 DISABLE READONLY OnFocus=\"javascript:Focus$function_name()\" OnBlur=\"javascript:Blur$function_name()\"> <a href=\"javascript:Clic$function_name()\">...</a> $javascript";
1978 $subfield_data{marc_value} =
1979 "<input type=\"text\" name=\"field_value\" value=\"$value\" size=50 maxlength=255>";
1981 push( @loop_data, \%subfield_data );
1985 my $itemnumber = $itemrecord->subfield( $itemtagfield, $itemtagsubfield )
1986 if ( $itemrecord && $itemrecord->field($itemtagfield) );
1988 'itemtagfield' => $itemtagfield,
1989 'itemtagsubfield' => $itemtagsubfield,
1990 'itemnumber' => $itemnumber,
1991 'iteminformation' => \@loop_data
1997 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
1999 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2000 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2001 # =head2 ModZebrafiles
2003 # &ModZebrafiles( $dbh, $biblionumber, $record, $folder, $server );
2007 # sub ModZebrafiles {
2009 # my ( $dbh, $biblionumber, $record, $folder, $server ) = @_;
2013 # C4::Context->zebraconfig($server)->{directory} . "/" . $folder . "/";
2014 # unless ( opendir( DIR, "$zebradir" ) ) {
2015 # warn "$zebradir not found";
2019 # my $filename = $zebradir . $biblionumber;
2022 # open( OUTPUT, ">", $filename . ".xml" );
2023 # print OUTPUT $record;
2032 ModZebra( $biblionumber, $op, $server, $newRecord );
2034 $biblionumber is the biblionumber we want to index
2035 $op is specialUpdate or delete, and is used to know what we want to do
2036 $server is the server that we want to update
2037 $newRecord is the MARC::Record containing the new record. It is usefull only when NoZebra=1, and is used to know what to add to the nozebra database. (the record in mySQL being, if it exist, the previous record, the one just before the modif. We need both : the previous and the new one.
2044 ###Accepts a $server variable thus we can use it for biblios authorities or other zebra dbs
2045 my ( $biblionumber, $op, $server, $newRecord ) = @_;
2046 my $dbh=C4::Context->dbh;
2048 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2050 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2051 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2053 if (C4::Context->preference("NoZebra")) {
2054 # lock the nozebra table : we will read index lines, update them in Perl process
2055 # and write everything in 1 transaction.
2056 # lock the table to avoid someone else overwriting what we are doing
2057 $dbh->do('LOCK TABLES nozebra WRITE,biblio WRITE,biblioitems WRITE, systempreferences WRITE, auth_types WRITE, auth_header WRITE');
2058 my %result; # the result hash that will be builded by deletion / add, and written on mySQL at the end, to improve speed
2060 if ($server eq 'biblioserver') {
2061 $record= GetMarcBiblio($biblionumber);
2063 $record= C4::AuthoritiesMarc::GetAuthority($biblionumber);
2065 if ($op eq 'specialUpdate') {
2066 # OK, we have to add or update the record
2067 # 1st delete (virtually, in indexes), if record actually exists
2069 %result = _DelBiblioNoZebra($biblionumber,$record,$server);
2071 # ... add the record
2072 %result=_AddBiblioNoZebra($biblionumber,$newRecord, $server, %result);
2074 # it's a deletion, delete the record...
2075 # warn "DELETE the record $biblionumber on $server".$record->as_formatted;
2076 %result=_DelBiblioNoZebra($biblionumber,$record,$server);
2078 # ok, now update the database...
2079 my $sth = $dbh->prepare("UPDATE nozebra SET biblionumbers=? WHERE server=? AND indexname=? AND value=?");
2080 foreach my $key (keys %result) {
2081 foreach my $index (keys %{$result{$key}}) {
2082 $sth->execute($result{$key}->{$index}, $server, $key, $index);
2085 $dbh->do('UNLOCK TABLES');
2089 # we use zebra, just fill zebraqueue table
2091 my $sth=$dbh->prepare("INSERT INTO zebraqueue (biblio_auth_number,server,operation) VALUES(?,?,?)");
2092 $sth->execute($biblionumber,$server,$op);
2097 =head2 GetNoZebraIndexes
2099 %indexes = GetNoZebraIndexes;
2101 return the data from NoZebraIndexes syspref.
2105 sub GetNoZebraIndexes {
2106 my $index = C4::Context->preference('NoZebraIndexes');
2108 foreach my $line (split /('|"),/,$index) {
2109 $line =~ /(.*)=>(.*)/;
2110 my $index = substr($1,1); # get the index, don't forget to remove initial ' or "
2112 $index =~ s/'|"|\s//g;
2115 $fields =~ s/'|"|\s//g;
2116 $indexes{$index}=$fields;
2121 =head1 INTERNAL FUNCTIONS
2123 =head2 _DelBiblioNoZebra($biblionumber,$record,$server);
2125 function to delete a biblio in NoZebra indexes
2126 This function does NOT delete anything in database : it reads all the indexes entries
2127 that have to be deleted & delete them in the hash
2128 The SQL part is done either :
2129 - after the Add if we are modifying a biblio (delete + add again)
2130 - immediatly after this sub if we are doing a true deletion.
2131 $server can be 'biblioserver' or 'authorityserver' : it indexes biblios or authorities (in the same table, $server being part of the table itself
2136 sub _DelBiblioNoZebra {
2137 my ($biblionumber, $record, $server)=@_;
2140 my $dbh = C4::Context->dbh;
2144 if ($server eq 'biblioserver') {
2145 %index=GetNoZebraIndexes;
2146 # get title of the record (to store the 10 first letters with the index)
2147 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title');
2148 $title = lc($record->subfield($titletag,$titlesubfield));
2150 # for authorities, the "title" is the $a mainentry
2151 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield(152,'b'));
2152 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
2153 $title = $record->subfield($authref->{auth_tag_to_report},'a');
2154 $index{'mainmainentry'}= $authref->{'auth_tag_to_report'}.'a';
2155 $index{'mainentry'} = $authref->{'auth_tag_to_report'}.'*';
2156 $index{'auth_type'} = '152b';
2160 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2161 $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
2162 # limit to 10 char, should be enough, and limit the DB size
2163 $title = substr($title,0,10);
2165 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2166 foreach my $field ($record->fields()) {
2167 #parse each subfield
2168 next if $field->tag <10;
2169 foreach my $subfield ($field->subfields()) {
2170 my $tag = $field->tag();
2171 my $subfieldcode = $subfield->[0];
2173 # check each index to see if the subfield is stored somewhere
2174 # otherwise, store it in __RAW__ index
2175 foreach my $key (keys %index) {
2176 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2177 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
2179 my $line= lc $subfield->[1];
2180 # remove meaningless value in the field...
2181 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2182 # ... and split in words
2183 foreach (split / /,$line) {
2184 next unless $_; # skip empty values (multiple spaces)
2185 # if the entry is already here, do nothing, the biblionumber has already be removed
2186 unless ($result{$key}->{$_} =~ /$biblionumber,$title\-(\d);/) {
2187 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2188 $sth2->execute($server,$key,$_);
2189 my $existing_biblionumbers = $sth2->fetchrow;
2191 if ($existing_biblionumbers) {
2192 # warn " existing for $key $_: $existing_biblionumbers";
2193 $result{$key}->{$_} =$existing_biblionumbers;
2194 $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2200 # the subfield is not indexed, store it in __RAW__ index anyway
2202 my $line= lc $subfield->[1];
2203 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2204 # ... and split in words
2205 foreach (split / /,$line) {
2206 next unless $_; # skip empty values (multiple spaces)
2207 # if the entry is already here, do nothing, the biblionumber has already be removed
2208 unless ($result{'__RAW__'}->{$_} =~ /$biblionumber,$title\-(\d);/) {
2209 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2210 $sth2->execute($server,'__RAW__',$_);
2211 my $existing_biblionumbers = $sth2->fetchrow;
2213 if ($existing_biblionumbers) {
2214 $result{'__RAW__'}->{$_} =$existing_biblionumbers;
2215 $result{'__RAW__'}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2225 =head2 _AddBiblioNoZebra($biblionumber, $record, $server, %result);
2227 function to add a biblio in NoZebra indexes
2231 sub _AddBiblioNoZebra {
2232 my ($biblionumber, $record, $server, %result)=@_;
2233 my $dbh = C4::Context->dbh;
2237 if ($server eq 'biblioserver') {
2238 %index=GetNoZebraIndexes;
2239 # get title of the record (to store the 10 first letters with the index)
2240 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title');
2241 $title = lc($record->subfield($titletag,$titlesubfield));
2243 # warn "server : $server";
2244 # for authorities, the "title" is the $a mainentry
2245 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield(152,'b'));
2246 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
2247 $title = $record->subfield($authref->{auth_tag_to_report},'a');
2248 $index{'mainmainentry'} = $authref->{auth_tag_to_report}.'a';
2249 $index{'mainentry'} = $authref->{auth_tag_to_report}.'*';
2250 $index{'auth_type'} = '152b';
2253 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2254 $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
2255 # limit to 10 char, should be enough, and limit the DB size
2256 $title = substr($title,0,10);
2258 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2259 foreach my $field ($record->fields()) {
2260 #parse each subfield
2261 next if $field->tag <10;
2262 foreach my $subfield ($field->subfields()) {
2263 my $tag = $field->tag();
2264 my $subfieldcode = $subfield->[0];
2266 # check each index to see if the subfield is stored somewhere
2267 # otherwise, store it in __RAW__ index
2268 foreach my $key (keys %index) {
2269 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2270 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
2272 my $line= lc $subfield->[1];
2273 # remove meaningless value in the field...
2274 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2275 # ... and split in words
2276 foreach (split / /,$line) {
2277 next unless $_; # skip empty values (multiple spaces)
2278 # if the entry is already here, improve weight
2279 # warn "managing $_";
2280 if ($result{$key}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d);/) {
2282 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d);//;
2283 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2285 # get the value if it exist in the nozebra table, otherwise, create it
2286 $sth2->execute($server,$key,$_);
2287 my $existing_biblionumbers = $sth2->fetchrow;
2289 if ($existing_biblionumbers) {
2290 $result{$key}->{"$_"} =$existing_biblionumbers;
2292 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d);//;
2293 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2294 # create a new ligne for this entry
2296 # warn "INSERT : $server / $key / $_";
2297 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname='.$dbh->quote($key).',value='.$dbh->quote($_));
2298 $result{$key}->{"$_"}.="$biblionumber,$title-1;";
2304 # the subfield is not indexed, store it in __RAW__ index anyway
2306 my $line= lc $subfield->[1];
2307 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2308 # ... and split in words
2309 foreach (split / /,$line) {
2310 next unless $_; # skip empty values (multiple spaces)
2311 # if the entry is already here, improve weight
2312 if ($result{'__RAW__'}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d);/) {
2314 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d);//;
2315 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2317 # get the value if it exist in the nozebra table, otherwise, create it
2318 $sth2->execute($server,'__RAW__',$_);
2319 my $existing_biblionumbers = $sth2->fetchrow;
2321 if ($existing_biblionumbers) {
2322 $result{'__RAW__'}->{"$_"} =$existing_biblionumbers;
2324 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d);//;
2325 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2326 # create a new ligne for this entry
2328 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname="__RAW__",value='.$dbh->quote($_));
2329 $result{'__RAW__'}->{"$_"}.="$biblionumber,$title-1;";
2344 ($indicators, $value) = _find_value($tag, $subfield, $record,$encoding);
2346 Find the given $subfield in the given $tag in the given
2347 MARC::Record $record. If the subfield is found, returns
2348 the (indicators, value) pair; otherwise, (undef, undef) is
2352 Such a function is used in addbiblio AND additem and serial-edit and maybe could be used in Authorities.
2353 I suggest we export it from this module.
2360 my ( $tagfield, $insubfield, $record, $encoding ) = @_;
2363 if ( $tagfield < 10 ) {
2364 if ( $record->field($tagfield) ) {
2365 push @result, $record->field($tagfield)->data();
2372 foreach my $field ( $record->field($tagfield) ) {
2373 my @subfields = $field->subfields();
2374 foreach my $subfield (@subfields) {
2375 if ( @$subfield[0] eq $insubfield ) {
2376 push @result, @$subfield[1];
2377 $indicator = $field->indicator(1) . $field->indicator(2);
2382 return ( $indicator, @result );
2385 =head2 _koha_marc_update_bib_ids
2389 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
2391 Internal function to add or update biblionumber and biblioitemnumber to
2398 sub _koha_marc_update_bib_ids {
2399 my ($record, $frameworkcode, $biblionumber, $biblioitemnumber) = @_;
2401 # we must add bibnum and bibitemnum in MARC::Record...
2402 # we build the new field with biblionumber and biblioitemnumber
2403 # we drop the original field
2404 # we add the new builded field.
2405 my ($biblio_tag, $biblio_subfield ) = GetMarcFromKohaField("biblio.biblionumber",$frameworkcode);
2406 my ($biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField("biblioitems.biblioitemnumber",$frameworkcode);
2408 if ($biblio_tag != $biblioitem_tag) {
2409 # biblionumber & biblioitemnumber are in different fields
2411 # deal with biblionumber
2412 my ($new_field, $old_field);
2413 if ($biblio_tag < 10) {
2414 $new_field = MARC::Field->new( $biblio_tag, $biblionumber );
2417 MARC::Field->new( $biblio_tag, '', '',
2418 "$biblio_subfield" => $biblionumber );
2421 # drop old field and create new one...
2422 $old_field = $record->field($biblio_tag);
2423 $record->delete_field($old_field);
2424 $record->append_fields($new_field);
2426 # deal with biblioitemnumber
2427 if ($biblioitem_tag < 10) {
2428 $new_field = MARC::Field->new( $biblioitem_tag, $biblioitemnumber, );
2431 MARC::Field->new( $biblioitem_tag, '', '',
2432 "$biblioitem_subfield" => $biblioitemnumber, );
2434 # drop old field and create new one...
2435 $old_field = $record->field($biblioitem_tag);
2436 $record->delete_field($old_field);
2437 $record->insert_fields_ordered($new_field);
2440 # biblionumber & biblioitemnumber are in the same field (can't be <10 as fields <10 have only 1 value)
2441 my $new_field = MARC::Field->new(
2442 $biblio_tag, '', '',
2443 "$biblio_subfield" => $biblionumber,
2444 "$biblioitem_subfield" => $biblioitemnumber
2447 # drop old field and create new one...
2448 my $old_field = $record->field($biblio_tag);
2449 $record->delete_field($old_field);
2450 $record->insert_fields_ordered($new_field);
2454 =head2 _koha_add_biblio
2458 my ($biblionumber,$error) = _koha_add_biblio($dbh,$biblioitem);
2460 Internal function to add a biblio ($biblio is a hash with the values)
2466 sub _koha_add_biblio {
2467 my ( $dbh, $biblio, $frameworkcode ) = @_;
2471 # set the series flag
2473 if ( $biblio->{'seriestitle'} ) { $serial = 1 };
2477 SET frameworkcode = ?,
2488 my $sth = $dbh->prepare($query);
2491 $biblio->{'author'},
2493 $biblio->{'unititle'},
2496 $biblio->{'seriestitle'},
2497 $biblio->{'copyrightdate'},
2498 $biblio->{'abstract'}
2501 my $biblionumber = $dbh->{'mysql_insertid'};
2502 if ( $dbh->errstr ) {
2503 $error.="ERROR in _koha_add_biblio $query".$dbh->errstr;
2508 #warn "LEAVING _koha_add_biblio: ".$biblionumber."\n";
2509 return ($biblionumber,$error);
2512 =head2 _koha_modify_biblio
2516 my ($biblionumber,$error) == _koha_modify_biblio($dbh,$biblio,$frameworkcode);
2518 Internal function for updating the biblio table
2524 sub _koha_modify_biblio {
2525 my ( $dbh, $biblio, $frameworkcode ) = @_;
2530 SET frameworkcode = ?,
2539 WHERE biblionumber = ?
2542 my $sth = $dbh->prepare($query);
2546 $biblio->{'author'},
2548 $biblio->{'unititle'},
2550 $biblio->{'serial'},
2551 $biblio->{'seriestitle'},
2552 $biblio->{'copyrightdate'},
2553 $biblio->{'abstract'},
2554 $biblio->{'biblionumber'}
2555 ) if $biblio->{'biblionumber'};
2557 if ( $dbh->errstr || !$biblio->{'biblionumber'} ) {
2558 $error.="ERROR in _koha_modify_biblio $query".$dbh->errstr;
2561 return ( $biblio->{'biblionumber'},$error );
2564 =head2 _koha_modify_biblioitem_nonmarc
2568 my ($biblioitemnumber,$error) = _koha_modify_biblioitem_nonmarc( $dbh, $biblioitem );
2570 Updates biblioitems row except for marc and marcxml, which should be changed
2577 sub _koha_modify_biblioitem_nonmarc {
2578 my ( $dbh, $biblioitem ) = @_;
2581 # re-calculate the cn_sort, it may have changed
2582 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2586 SET biblionumber = ?,
2592 publicationyear = ?,
2596 collectiontitle = ?,
2598 collectionvolume= ?,
2599 editionstatement= ?,
2600 editionresponsibility = ?,
2614 where biblioitemnumber = ?
2616 my $sth = $dbh->prepare($query);
2618 $biblioitem->{'biblionumber'},
2619 $biblioitem->{'volume'},
2620 $biblioitem->{'number'},
2621 $biblioitem->{'itemtype'},
2622 $biblioitem->{'isbn'},
2623 $biblioitem->{'issn'},
2624 $biblioitem->{'publicationyear'},
2625 $biblioitem->{'publishercode'},
2626 $biblioitem->{'volumedate'},
2627 $biblioitem->{'volumedesc'},
2628 $biblioitem->{'collectiontitle'},
2629 $biblioitem->{'collectionissn'},
2630 $biblioitem->{'collectionvolume'},
2631 $biblioitem->{'editionstatement'},
2632 $biblioitem->{'editionresponsibility'},
2633 $biblioitem->{'illus'},
2634 $biblioitem->{'pages'},
2635 $biblioitem->{'bnotes'},
2636 $biblioitem->{'size'},
2637 $biblioitem->{'place'},
2638 $biblioitem->{'lccn'},
2639 $biblioitem->{'url'},
2640 $biblioitem->{'biblioitems.cn_source'},
2641 $biblioitem->{'cn_class'},
2642 $biblioitem->{'cn_item'},
2643 $biblioitem->{'cn_suffix'},
2645 $biblioitem->{'totalissues'},
2646 $biblioitem->{'biblioitemnumber'}
2648 if ( $dbh->errstr ) {
2649 $error.="ERROR in _koha_modify_biblioitem_nonmarc $query".$dbh->errstr;
2652 return ($biblioitem->{'biblioitemnumber'},$error);
2655 =head2 _koha_add_biblioitem
2659 my ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $biblioitem );
2661 Internal function to add a biblioitem
2667 sub _koha_add_biblioitem {
2668 my ( $dbh, $biblioitem ) = @_;
2671 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2673 "INSERT INTO biblioitems SET
2680 publicationyear = ?,
2684 collectiontitle = ?,
2686 collectionvolume= ?,
2687 editionstatement= ?,
2688 editionresponsibility = ?,
2704 my $sth = $dbh->prepare($query);
2706 $biblioitem->{'biblionumber'},
2707 $biblioitem->{'volume'},
2708 $biblioitem->{'number'},
2709 $biblioitem->{'itemtype'},
2710 $biblioitem->{'isbn'},
2711 $biblioitem->{'issn'},
2712 $biblioitem->{'publicationyear'},
2713 $biblioitem->{'publishercode'},
2714 $biblioitem->{'volumedate'},
2715 $biblioitem->{'volumedesc'},
2716 $biblioitem->{'collectiontitle'},
2717 $biblioitem->{'collectionissn'},
2718 $biblioitem->{'collectionvolume'},
2719 $biblioitem->{'editionstatement'},
2720 $biblioitem->{'editionresponsibility'},
2721 $biblioitem->{'illus'},
2722 $biblioitem->{'pages'},
2723 $biblioitem->{'bnotes'},
2724 $biblioitem->{'size'},
2725 $biblioitem->{'place'},
2726 $biblioitem->{'lccn'},
2727 $biblioitem->{'marc'},
2728 $biblioitem->{'url'},
2729 $biblioitem->{'biblioitems.cn_source'},
2730 $biblioitem->{'cn_class'},
2731 $biblioitem->{'cn_item'},
2732 $biblioitem->{'cn_suffix'},
2734 $biblioitem->{'totalissues'}
2736 my $bibitemnum = $dbh->{'mysql_insertid'};
2737 if ( $dbh->errstr ) {
2738 $error.="ERROR in _koha_add_biblioitem $query".$dbh->errstr;
2742 return ($bibitemnum,$error);
2745 =head2 _koha_delete_biblio
2749 $error = _koha_delete_biblio($dbh,$biblionumber);
2751 Internal sub for deleting from biblio table -- also saves to deletedbiblio
2753 C<$dbh> - the database handle
2754 C<$biblionumber> - the biblionumber of the biblio to be deleted
2760 # FIXME: add error handling
2762 sub _koha_delete_biblio {
2763 my ( $dbh, $biblionumber ) = @_;
2765 # get all the data for this biblio
2766 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber=?");
2767 $sth->execute($biblionumber);
2769 if ( my $data = $sth->fetchrow_hashref ) {
2771 # save the record in deletedbiblio
2772 # find the fields to save
2773 my $query = "INSERT INTO deletedbiblio SET ";
2775 foreach my $temp ( keys %$data ) {
2776 $query .= "$temp = ?,";
2777 push( @bind, $data->{$temp} );
2780 # replace the last , by ",?)"
2782 my $bkup_sth = $dbh->prepare($query);
2783 $bkup_sth->execute(@bind);
2787 my $del_sth = $dbh->prepare("DELETE FROM biblio WHERE biblionumber=?");
2788 $del_sth->execute($biblionumber);
2795 =head2 _koha_delete_biblioitems
2799 $error = _koha_delete_biblioitems($dbh,$biblioitemnumber);
2801 Internal sub for deleting from biblioitems table -- also saves to deletedbiblioitems
2803 C<$dbh> - the database handle
2804 C<$biblionumber> - the biblioitemnumber of the biblioitem to be deleted
2810 # FIXME: add error handling
2812 sub _koha_delete_biblioitems {
2813 my ( $dbh, $biblioitemnumber ) = @_;
2815 # get all the data for this biblioitem
2817 $dbh->prepare("SELECT * FROM biblioitems WHERE biblioitemnumber=?");
2818 $sth->execute($biblioitemnumber);
2820 if ( my $data = $sth->fetchrow_hashref ) {
2822 # save the record in deletedbiblioitems
2823 # find the fields to save
2824 my $query = "INSERT INTO deletedbiblioitems SET ";
2826 foreach my $temp ( keys %$data ) {
2827 $query .= "$temp = ?,";
2828 push( @bind, $data->{$temp} );
2831 # replace the last , by ",?)"
2833 my $bkup_sth = $dbh->prepare($query);
2834 $bkup_sth->execute(@bind);
2837 # delete the biblioitem
2839 $dbh->prepare("DELETE FROM biblioitems WHERE biblioitemnumber=?");
2840 $del_sth->execute($biblioitemnumber);
2847 =head1 UNEXPORTED FUNCTIONS
2849 =head2 ModBiblioMarc
2851 &ModBiblioMarc($newrec,$biblionumber,$frameworkcode);
2853 Add MARC data for a biblio to koha
2855 Function exported, but should NOT be used, unless you really know what you're doing
2861 # pass the MARC::Record to this function, and it will create the records in the marc field
2862 my ( $record, $biblionumber, $frameworkcode ) = @_;
2863 my $dbh = C4::Context->dbh;
2864 my @fields = $record->fields();
2865 if ( !$frameworkcode ) {
2866 $frameworkcode = "";
2869 $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?");
2870 $sth->execute( $frameworkcode, $biblionumber );
2872 my $encoding = C4::Context->preference("marcflavour");
2874 # deal with UNIMARC field 100 (encoding) : create it if needed & set encoding to unicode
2875 if ( $encoding eq "UNIMARC" ) {
2877 if ( length($record->subfield( 100, "a" )) == 35 ) {
2878 $string = $record->subfield( 100, "a" );
2879 my $f100 = $record->field(100);
2880 $record->delete_field($f100);
2883 $string = POSIX::strftime( "%Y%m%d", localtime );
2885 $string = sprintf( "%-*s", 35, $string );
2887 substr( $string, 22, 6, "frey50" );
2888 unless ( $record->subfield( 100, "a" ) ) {
2889 $record->insert_grouped_field(
2890 MARC::Field->new( 100, "", "", "a" => $string ) );
2893 ModZebra($biblionumber,"specialUpdate","biblioserver",$record);
2896 "UPDATE biblioitems SET marc=?,marcxml=? WHERE biblionumber=?");
2897 $sth->execute( $record->as_usmarc(), $record->as_xml_record($encoding),
2900 return $biblionumber;
2903 =head2 z3950_extended_services
2905 z3950_extended_services($serviceType,$serviceOptions,$record);
2907 z3950_extended_services is used to handle all interactions with Zebra's extended serices package, which is employed to perform all management of the MARC data stored in Zebra.
2909 C<$serviceType> one of: itemorder,create,drop,commit,update,xmlupdate
2911 C<$serviceOptions> a has of key/value pairs. For instance, if service_type is 'update', $service_options should contain:
2913 action => update action, one of specialUpdate, recordInsert, recordReplace, recordDelete, elementUpdate.
2917 recordidOpaque => Opaque Record ID (user supplied) or recordidNumber => Record ID number (system number).
2918 syntax => the record syntax (transfer syntax)
2919 databaseName = Database from connection object
2921 To set serviceOptions, call set_service_options($serviceType)
2923 C<$record> the record, if one is needed for the service type
2925 A record should be in XML. You can convert it to XML from MARC by running it through marc2xml().
2929 sub z3950_extended_services {
2930 my ( $server, $serviceType, $action, $serviceOptions ) = @_;
2932 # get our connection object
2933 my $Zconn = C4::Context->Zconn( $server, 0, 1 );
2935 # create a new package object
2936 my $Zpackage = $Zconn->package();
2939 $Zpackage->option( action => $action );
2941 if ( $serviceOptions->{'databaseName'} ) {
2942 $Zpackage->option( databaseName => $serviceOptions->{'databaseName'} );
2944 if ( $serviceOptions->{'recordIdNumber'} ) {
2946 recordIdNumber => $serviceOptions->{'recordIdNumber'} );
2948 if ( $serviceOptions->{'recordIdOpaque'} ) {
2950 recordIdOpaque => $serviceOptions->{'recordIdOpaque'} );
2953 # this is an ILL request (Zebra doesn't support it, but Koha could eventually)
2954 #if ($serviceType eq 'itemorder') {
2955 # $Zpackage->option('contact-name' => $serviceOptions->{'contact-name'});
2956 # $Zpackage->option('contact-phone' => $serviceOptions->{'contact-phone'});
2957 # $Zpackage->option('contact-email' => $serviceOptions->{'contact-email'});
2958 # $Zpackage->option('itemorder-item' => $serviceOptions->{'itemorder-item'});
2961 if ( $serviceOptions->{record} ) {
2962 $Zpackage->option( record => $serviceOptions->{record} );
2964 # can be xml or marc
2965 if ( $serviceOptions->{'syntax'} ) {
2966 $Zpackage->option( syntax => $serviceOptions->{'syntax'} );
2970 # send the request, handle any exception encountered
2971 eval { $Zpackage->send($serviceType) };
2972 if ( $@ && $@->isa("ZOOM::Exception") ) {
2973 return "error: " . $@->code() . " " . $@->message() . "\n";
2976 # free up package resources
2977 $Zpackage->destroy();
2980 =head2 set_service_options
2982 my $serviceOptions = set_service_options($serviceType);
2984 C<$serviceType> itemorder,create,drop,commit,update,xmlupdate
2986 Currently, we only support 'create', 'commit', and 'update'. 'drop' support will be added as soon as Zebra supports it.
2990 sub set_service_options {
2991 my ($serviceType) = @_;
2994 # FIXME: This needs to be an OID ... if we ever need 'syntax' this sub will need to change
2995 # $serviceOptions->{ 'syntax' } = ''; #zebra doesn't support syntaxes other than xml
2997 if ( $serviceType eq 'commit' ) {
3001 if ( $serviceType eq 'create' ) {
3005 if ( $serviceType eq 'drop' ) {
3006 die "ERROR: 'drop' not currently supported (by Zebra)";
3008 return $serviceOptions;
3017 Koha Developement team <info@koha.org>
3019 Paul POULAIN paul.poulain@free.fr
3021 Joshua Ferraro jmf@liblime.com