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
24 use MARC::File::USMARC;
25 # Force MARC::File::XML to use LibXML SAX Parser
26 #$XML::SAX::ParserPackage = "XML::LibXML::SAX";
29 use POSIX qw(strftime);
32 use C4::Dates qw/format_date/;
33 use C4::Log; # logaction
39 use vars qw($VERSION @ISA @EXPORT);
45 @ISA = qw( Exporter );
59 &GetBiblioItemByBiblioNumber
60 &GetBiblioFromItemNumber
72 &GetAuthorisedValueDesc
76 &GetPublisherNameFromIsbn
91 # To link headings in a bib record
92 # to authority records.
94 &LinkBibHeadingsToAuthorities
98 # those functions are exported but should not be used
99 # they are usefull is few circumstances, so are exported.
100 # but don't use them unless you're a core developer ;-)
107 &TransformHtmlToMarc2
110 &PrepareItemrecordDisplay
117 C4::Biblio - cataloging management functions
121 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:
125 =item 1. in the biblio,biblioitems,items, etc tables, which are limited to a one-to-one mapping to underlying MARC data
127 =item 2. as raw MARC in the Zebra index and storage engine
129 =item 3. as raw MARC the biblioitems.marc and biblioitems.marcxml
133 In the 3.0 version of Koha, the authoritative record-level information is in biblioitems.marcxml
135 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.
139 =item 1. Compared with MySQL, Zebra is slow to update an index for small data changes -- especially for proc-intensive operations like circulation
141 =item 2. Zebra's index has been known to crash and a backup of the data is necessary to rebuild it in such cases
145 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:
149 =item 1. Add*/Mod*/Del*/ - high-level external functions suitable for being called from external scripts to manage the collection
151 =item 2. _koha_* - low-level internal functions for managing the koha tables
153 =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.
155 =item 4. Zebra functions used to update the Zebra index
157 =item 5. internal helper functions such as char_decode, checkitems, etc. Some of these probably belong in Koha.pm
161 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 :
165 =item 1. save datas in biblio and biblioitems table, that gives us a biblionumber and a biblioitemnumber
167 =item 2. add the biblionumber and biblioitemnumber into the MARC records
169 =item 3. save the marc record
173 When dealing with items, we must :
177 =item 1. save the item in items table, that gives us an itemnumber
179 =item 2. add the itemnumber to the item MARC field
181 =item 3. overwrite the MARC record (with the added item) into biblioitems.marc(xml)
183 When modifying a biblio or an item, the behaviour is quite similar.
187 =head1 EXPORTED FUNCTIONS
193 ($biblionumber,$biblioitemnumber) = AddBiblio($record,$frameworkcode);
197 Exported function (core API) for adding a new biblio to koha.
199 The first argument is a C<MARC::Record> object containing the
200 bib to add, while the second argument is the desired MARC
203 This function also accepts a third, optional argument: a hashref
204 to additional options. The only defined option is C<defer_marc_save>,
205 which if present and mapped to a true value, causes C<AddBiblio>
206 to omit the call to save the MARC in C<bibilioitems.marc>
207 and C<biblioitems.marcxml> This option is provided B<only>
208 for the use of scripts such as C<bulkmarcimport.pl> that may need
209 to do some manipulation of the MARC record for item parsing before
210 saving it and which cannot afford the performance hit of saving
211 the MARC record twice. Consequently, do not use that option
212 unless you can guarantee that C<ModBiblioMarc> will be called.
218 my $frameworkcode = shift;
219 my $options = @_ ? shift : undef;
220 my $defer_marc_save = 0;
221 if (defined $options and exists $options->{'defer_marc_save'} and $options->{'defer_marc_save'}) {
222 $defer_marc_save = 1;
225 my ($biblionumber,$biblioitemnumber,$error);
226 my $dbh = C4::Context->dbh;
227 # transform the data into koha-table style data
228 my $olddata = TransformMarcToKoha( $dbh, $record, $frameworkcode );
229 ($biblionumber,$error) = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
230 $olddata->{'biblionumber'} = $biblionumber;
231 ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $olddata );
233 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
235 # update MARC subfield that stores biblioitems.cn_sort
236 _koha_marc_update_biblioitem_cn_sort($record, $olddata, $frameworkcode);
239 $biblionumber = ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $defer_marc_save;
241 logaction("CATALOGUING", "ADD", $biblionumber, "biblio") if C4::Context->preference("CataloguingLog");
243 return ( $biblionumber, $biblioitemnumber );
250 ModBiblio( $record,$biblionumber,$frameworkcode);
254 Replace an existing bib record identified by C<$biblionumber>
255 with one supplied by the MARC::Record object C<$record>. The embedded
256 item, biblioitem, and biblionumber fields from the previous
257 version of the bib record replace any such fields of those tags that
258 are present in C<$record>. Consequently, ModBiblio() is not
259 to be used to try to modify item records.
261 C<$frameworkcode> specifies the MARC framework to use
262 when storing the modified bib record; among other things,
263 this controls how MARC fields get mapped to display columns
264 in the C<biblio> and C<biblioitems> tables, as well as
265 which fields are used to store embedded item, biblioitem,
266 and biblionumber data for indexing.
271 my ( $record, $biblionumber, $frameworkcode ) = @_;
272 if (C4::Context->preference("CataloguingLog")) {
273 my $newrecord = GetMarcBiblio($biblionumber);
274 logaction("CATALOGUING", "MODIFY", $biblionumber, "BEFORE=>".$newrecord->as_formatted);
277 my $dbh = C4::Context->dbh;
279 $frameworkcode = "" unless $frameworkcode;
281 # get the items before and append them to the biblio before updating the record, atm we just have the biblio
282 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
283 my $oldRecord = GetMarcBiblio( $biblionumber );
285 # delete any item fields from incoming record to avoid
286 # duplication or incorrect data - use AddItem() or ModItem()
288 foreach my $field ($record->field($itemtag)) {
289 $record->delete_field($field);
292 # once all the items fields are removed, copy the old ones, in order to keep synchronize
293 $record->append_fields($oldRecord->field( $itemtag ));
295 # update biblionumber and biblioitemnumber in MARC
296 # FIXME - this is assuming a 1 to 1 relationship between
297 # biblios and biblioitems
298 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
299 $sth->execute($biblionumber);
300 my ($biblioitemnumber) = $sth->fetchrow;
302 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
304 # load the koha-table data object
305 my $oldbiblio = TransformMarcToKoha( $dbh, $record, $frameworkcode );
307 # update MARC subfield that stores biblioitems.cn_sort
308 _koha_marc_update_biblioitem_cn_sort($record, $oldbiblio, $frameworkcode);
310 # update the MARC record (that now contains biblio and items) with the new record data
311 &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
313 # modify the other koha tables
314 _koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
315 _koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
319 =head2 ModBiblioframework
321 ModBiblioframework($biblionumber,$frameworkcode);
322 Exported function to modify a biblio framework
326 sub ModBiblioframework {
327 my ( $biblionumber, $frameworkcode ) = @_;
328 my $dbh = C4::Context->dbh;
329 my $sth = $dbh->prepare(
330 "UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"
332 $sth->execute($frameworkcode, $biblionumber);
340 my $error = &DelBiblio($dbh,$biblionumber);
341 Exported function (core API) for deleting a biblio in koha.
342 Deletes biblio record from Zebra and Koha tables (biblio,biblioitems,items)
343 Also backs it up to deleted* tables
344 Checks to make sure there are not issues on any of the items
346 C<$error> : undef unless an error occurs
353 my ( $biblionumber ) = @_;
354 my $dbh = C4::Context->dbh;
355 my $error; # for error handling
357 # First make sure this biblio has no items attached
358 my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber=?");
359 $sth->execute($biblionumber);
360 if (my $itemnumber = $sth->fetchrow){
361 # Fix this to use a status the template can understand
362 $error .= "This Biblio has items attached, please delete them first before deleting this biblio ";
365 return $error if $error;
367 # We delete attached subscriptions
368 my $subscriptions = &C4::Serials::GetFullSubscriptionsFromBiblionumber($biblionumber);
369 foreach my $subscription (@$subscriptions){
370 &C4::Serials::DelSubscription($subscription->{subscriptionid});
373 # Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio
374 # for at least 2 reasons :
375 # - we need to read the biblio if NoZebra is set (to remove it from the indexes
376 # - if something goes wrong, the biblio may be deleted from Koha but not from zebra
377 # 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)
379 if (C4::Context->preference("NoZebra")) {
380 # only NoZebra indexing needs to have
381 # the previous version of the record
382 $oldRecord = GetMarcBiblio($biblionumber);
384 ModZebra($biblionumber, "recordDelete", "biblioserver", $oldRecord, undef);
386 # delete biblioitems and items from Koha tables and save in deletedbiblioitems,deleteditems
389 "SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
390 $sth->execute($biblionumber);
391 while ( my $biblioitemnumber = $sth->fetchrow ) {
393 # delete this biblioitem
394 $error = _koha_delete_biblioitems( $dbh, $biblioitemnumber );
395 return $error if $error;
398 # delete biblio from Koha tables and save in deletedbiblio
399 # must do this *after* _koha_delete_biblioitems, otherwise
400 # delete cascade will prevent deletedbiblioitems rows
401 # from being generated by _koha_delete_biblioitems
402 $error = _koha_delete_biblio( $dbh, $biblionumber );
404 logaction("CATALOGUING", "DELETE", $biblionumber, "") if C4::Context->preference("CataloguingLog");
409 =head2 LinkBibHeadingsToAuthorities
413 my $headings_linked = LinkBibHeadingsToAuthorities($marc);
417 Links bib headings to authority records by checking
418 each authority-controlled field in the C<MARC::Record>
419 object C<$marc>, looking for a matching authority record,
420 and setting the linking subfield $9 to the ID of that
423 If no matching authority exists, or if multiple
424 authorities match, no $9 will be added, and any
425 existing one inthe field will be deleted.
427 Returns the number of heading links changed in the
432 sub LinkBibHeadingsToAuthorities {
435 my $num_headings_changed = 0;
436 foreach my $field ($bib->fields()) {
437 my $heading = C4::Heading->new_from_bib_field($field);
438 next unless defined $heading;
441 my $current_link = $field->subfield('9');
443 # look for matching authorities
444 my $authorities = $heading->authorities();
446 # want only one exact match
447 if ($#{ $authorities } == 0) {
448 my $authority = MARC::Record->new_from_usmarc($authorities->[0]);
449 my $authid = $authority->field('001')->data();
450 next if defined $current_link and $current_link eq $authid;
452 $field->delete_subfield(code => '9') if defined $current_link;
453 $field->add_subfields('9', $authid);
454 $num_headings_changed++;
456 if (defined $current_link) {
457 $field->delete_subfield(code => '9');
458 $num_headings_changed++;
463 return $num_headings_changed;
470 $data = &GetBiblioData($biblionumber);
471 Returns information about the book with the given biblionumber.
472 C<&GetBiblioData> returns a reference-to-hash. The keys are the fields in
473 the C<biblio> and C<biblioitems> tables in the
475 In addition, C<$data-E<gt>{subject}> is the list of the book's
476 subjects, separated by C<" , "> (space, comma, space).
477 If there are multiple biblioitems with the given biblionumber, only
478 the first one is considered.
486 my $dbh = C4::Context->dbh;
488 # my $query = C4::Context->preference('item-level_itypes') ?
489 # " SELECT * , biblioitems.notes AS bnotes, biblio.notes
491 # LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
492 # WHERE biblio.biblionumber = ?
493 # AND biblioitems.biblionumber = biblio.biblionumber
496 my $query = " SELECT * , biblioitems.notes AS bnotes, itemtypes.notforloan as bi_notforloan, biblio.notes
498 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
499 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
500 WHERE biblio.biblionumber = ?
501 AND biblioitems.biblionumber = biblio.biblionumber ";
503 my $sth = $dbh->prepare($query);
504 $sth->execute($bibnum);
506 $data = $sth->fetchrow_hashref;
510 } # sub GetBiblioData
512 =head2 &GetBiblioItemData
516 $itemdata = &GetBiblioItemData($biblioitemnumber);
518 Looks up the biblioitem with the given biblioitemnumber. Returns a
519 reference-to-hash. The keys are the fields from the C<biblio>,
520 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
521 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
528 sub GetBiblioItemData {
529 my ($biblioitemnumber) = @_;
530 my $dbh = C4::Context->dbh;
531 my $query = "SELECT *,biblioitems.notes AS bnotes
532 FROM biblio LEFT JOIN biblioitems on biblio.biblionumber=biblioitems.biblionumber ";
533 unless(C4::Context->preference('item-level_itypes')) {
534 $query .= "LEFT JOIN itemtypes on biblioitems.itemtype=itemtypes.itemtype ";
536 $query .= " WHERE biblioitemnumber = ? ";
537 my $sth = $dbh->prepare($query);
539 $sth->execute($biblioitemnumber);
540 $data = $sth->fetchrow_hashref;
543 } # sub &GetBiblioItemData
545 =head2 GetBiblioItemByBiblioNumber
549 NOTE : This function has been copy/paste from C4/Biblio.pm from head before zebra integration.
555 sub GetBiblioItemByBiblioNumber {
556 my ($biblionumber) = @_;
557 my $dbh = C4::Context->dbh;
558 my $sth = $dbh->prepare("Select * FROM biblioitems WHERE biblionumber = ?");
562 $sth->execute($biblionumber);
564 while ( my $data = $sth->fetchrow_hashref ) {
565 push @results, $data;
572 =head2 GetBiblioFromItemNumber
576 $item = &GetBiblioFromItemNumber($itemnumber,$barcode);
578 Looks up the item with the given itemnumber. if undef, try the barcode.
580 C<&itemnodata> returns a reference-to-hash whose keys are the fields
581 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
589 sub GetBiblioFromItemNumber {
590 my ( $itemnumber, $barcode ) = @_;
591 my $dbh = C4::Context->dbh;
594 $sth=$dbh->prepare( "SELECT * FROM items
595 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
596 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
597 WHERE items.itemnumber = ?") ;
598 $sth->execute($itemnumber);
600 $sth=$dbh->prepare( "SELECT * FROM items
601 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
602 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
603 WHERE items.barcode = ?") ;
604 $sth->execute($barcode);
606 my $data = $sth->fetchrow_hashref;
615 ( $count, @results ) = &GetBiblio($biblionumber);
622 my ($biblionumber) = @_;
623 my $dbh = C4::Context->dbh;
624 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber = ?");
627 $sth->execute($biblionumber);
628 while ( my $data = $sth->fetchrow_hashref ) {
629 $results[$count] = $data;
633 return ( $count, @results );
636 =head2 GetBiblioItemInfosOf
640 GetBiblioItemInfosOf(@biblioitemnumbers);
646 sub GetBiblioItemInfosOf {
647 my @biblioitemnumbers = @_;
650 SELECT biblioitemnumber,
654 WHERE biblioitemnumber IN (' . join( ',', @biblioitemnumbers ) . ')
656 return get_infos_of( $query, 'biblioitemnumber' );
659 =head1 FUNCTIONS FOR HANDLING MARC MANAGEMENT
661 =head2 GetMarcStructure
665 $res = GetMarcStructure($forlibrarian,$frameworkcode);
667 Returns a reference to a big hash of hash, with the Marc structure for the given frameworkcode
668 $forlibrarian :if set to 1, the MARC descriptions are the librarians ones, otherwise it's the public (OPAC) ones
669 $frameworkcode : the framework code to read
675 # cache for results of GetMarcStructure -- needed
677 our $marc_structure_cache;
679 sub GetMarcStructure {
680 my ( $forlibrarian, $frameworkcode ) = @_;
681 my $dbh=C4::Context->dbh;
682 $frameworkcode = "" unless $frameworkcode;
684 if (defined $marc_structure_cache and exists $marc_structure_cache->{$forlibrarian}->{$frameworkcode}) {
685 return $marc_structure_cache->{$forlibrarian}->{$frameworkcode};
689 my $libfield = ( $forlibrarian eq 1 ) ? 'liblibrarian' : 'libopac';
691 # check that framework exists
694 "SELECT COUNT(*) FROM marc_tag_structure WHERE frameworkcode=?");
695 $sth->execute($frameworkcode);
696 my ($total) = $sth->fetchrow;
697 $frameworkcode = "" unless ( $total > 0 );
700 "SELECT tagfield,liblibrarian,libopac,mandatory,repeatable
701 FROM marc_tag_structure
702 WHERE frameworkcode=?
705 $sth->execute($frameworkcode);
706 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
708 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) =
711 $res->{$tag}->{lib} =
712 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
713 $res->{$tag}->{tab} = "";
714 $res->{$tag}->{mandatory} = $mandatory;
715 $res->{$tag}->{repeatable} = $repeatable;
720 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab,mandatory,repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl,link,defaultvalue
721 FROM marc_subfield_structure
722 WHERE frameworkcode=?
723 ORDER BY tagfield,tagsubfield
727 $sth->execute($frameworkcode);
730 my $authorised_value;
742 $tag, $subfield, $liblibrarian,
744 $mandatory, $repeatable, $authorised_value,
745 $authtypecode, $value_builder, $kohafield,
746 $seealso, $hidden, $isurl,
752 $res->{$tag}->{$subfield}->{lib} =
753 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
754 $res->{$tag}->{$subfield}->{tab} = $tab;
755 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
756 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
757 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
758 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
759 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
760 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
761 $res->{$tag}->{$subfield}->{seealso} = $seealso;
762 $res->{$tag}->{$subfield}->{hidden} = $hidden;
763 $res->{$tag}->{$subfield}->{isurl} = $isurl;
764 $res->{$tag}->{$subfield}->{'link'} = $link;
765 $res->{$tag}->{$subfield}->{defaultvalue} = $defaultvalue;
768 $marc_structure_cache->{$forlibrarian}->{$frameworkcode} = $res;
773 =head2 GetUsedMarcStructure
775 the same function as GetMarcStructure expcet it just take field
776 in tab 0-9. (used field)
778 my $results = GetUsedMarcStructure($frameworkcode);
780 L<$results> is a ref to an array which each case containts a ref
781 to a hash which each keys is the columns from marc_subfield_structure
783 L<$frameworkcode> is the framework code.
787 sub GetUsedMarcStructure($){
788 my $frameworkcode = shift || '';
789 my $dbh = C4::Context->dbh;
792 FROM marc_subfield_structure
794 AND frameworkcode = ?
797 my $sth = $dbh->prepare($query);
798 $sth->execute($frameworkcode);
799 while (my $row = $sth->fetchrow_hashref){
805 =head2 GetMarcFromKohaField
809 ($MARCfield,$MARCsubfield)=GetMarcFromKohaField($kohafield,$frameworkcode);
810 Returns the MARC fields & subfields mapped to the koha field
811 for the given frameworkcode
817 sub GetMarcFromKohaField {
818 my ( $kohafield, $frameworkcode ) = @_;
819 return 0, 0 unless $kohafield and defined $frameworkcode;
820 my $relations = C4::Context->marcfromkohafield;
822 $relations->{$frameworkcode}->{$kohafield}->[0],
823 $relations->{$frameworkcode}->{$kohafield}->[1]
831 my $record = GetMarcBiblio($biblionumber);
835 Returns MARC::Record representing bib identified by
836 C<$biblionumber>. If no bib exists, returns undef.
837 The MARC record contains both biblio & item data.
842 my $biblionumber = shift;
843 my $dbh = C4::Context->dbh;
845 $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
846 $sth->execute($biblionumber);
847 my $row = $sth->fetchrow_hashref;
848 my $marcxml = StripNonXmlChars($row->{'marcxml'});
849 MARC::File::XML->default_record_format(C4::Context->preference('marcflavour'));
850 my $record = MARC::Record->new();
852 $record = eval {MARC::Record::new_from_xml( $marcxml, "utf8", C4::Context->preference('marcflavour'))};
853 if ($@) {warn " problem with :$biblionumber : $@ \n$marcxml";}
854 # $record = MARC::Record::new_from_usmarc( $marc) if $marc;
865 my $marcxml = GetXmlBiblio($biblionumber);
867 Returns biblioitems.marcxml of the biblionumber passed in parameter.
868 The XML contains both biblio & item datas
875 my ( $biblionumber ) = @_;
876 my $dbh = C4::Context->dbh;
878 $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
879 $sth->execute($biblionumber);
880 my ($marcxml) = $sth->fetchrow;
884 =head2 GetCOinSBiblio
888 my $coins = GetCOinSBiblio($biblionumber);
890 Returns the COinS(a span) which can be included in a biblio record
897 my ( $biblionumber ) = @_;
898 my $record = GetMarcBiblio($biblionumber);
900 if (defined $record){
901 # get the coin format
902 my $pos7 = substr $record->leader(), 7,1;
903 my $pos6 = substr $record->leader(), 6,1;
906 my ($aulast, $aufirst);
914 if ( C4::Context->preference("marcflavour") eq "UNIMARC" ){
925 'i' => 'audioRecording',
926 'j' => 'audioRecording',
929 'm' => 'computerProgram',
934 'a' => 'journalArticle',
938 $genre = $fmts6->{$pos6} ? $fmts6->{$pos6} : 'book' ;
940 if( $genre eq 'book' ){
941 $genre = $fmts7->{$pos7} if $fmts7->{$pos7};
944 ##### We must transform mtx to a valable mtx and document type ####
945 if( $genre eq 'book' ){
947 }elsif( $genre eq 'journal' ){
949 }elsif( $genre eq 'journalArticle' ){
956 $genre = ($mtx eq 'dc') ? "&rft.type=$genre" : "&rft.genre=$genre";
959 $aulast = $record->subfield('700','a');
960 $aufirst = $record->subfield('700','b');
961 $oauthors = "&rft.au=$aufirst $aulast";
963 if($record->field('200')){
964 for my $au ($record->field('200')->subfield('g')){
965 $oauthors .= "&rft.au=$au";
968 $title = ( $mtx eq 'dc' ) ? "&rft.title=".$record->subfield('200','a') :
969 "&rft.title=".$record->subfield('200','a')."&rft.btitle=".$record->subfield('200','a');
970 $pubyear = $record->subfield('210','d');
971 $publisher = $record->subfield('210','c');
972 $isbn = $record->subfield('010','a');
973 $issn = $record->subfield('011','a');
975 # MARC21 need some improve
978 $genre = "&rft.genre=book";
981 $oauthors .= "&rft.au=".$record->subfield('100','a');
983 if($record->field('700')){
984 for my $au ($record->field('700')->subfield('a')){
985 $oauthors .= "&rft.au=$au";
988 $title = "&rft.btitle=".$record->subfield('245','a');
989 $pubyear = $record->subfield('260','c');
990 $publisher = $record->subfield('260','b');
991 $isbn = $record->subfield('020','a');
992 $issn = $record->subfield('022','a');
995 $coins_value = "ctx_ver=Z39.88-2004&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3A$mtx$genre$title&rft.isbn=$isbn&rft.issn=$issn&rft.aulast=$aulast&rft.aufirst=$aufirst$oauthors&rft.pub=$publisher&rft.date=$pubyear";
996 $coins_value =~ s/\ /\+/g;
997 #<!-- TMPL_VAR NAME="ocoins_format" -->&rft.au=<!-- TMPL_VAR NAME="author" -->&rft.btitle=<!-- TMPL_VAR NAME="title" -->&rft.date=<!-- TMPL_VAR NAME="publicationyear" -->&rft.pages=<!-- TMPL_VAR NAME="pages" -->&rft.isbn=<!-- TMPL_VAR NAME=amazonisbn -->&rft.aucorp=&rft.place=<!-- TMPL_VAR NAME="place" -->&rft.pub=<!-- TMPL_VAR NAME="publishercode" -->&rft.edition=<!-- TMPL_VAR NAME="edition" -->&rft.series=<!-- TMPL_VAR NAME="series" -->&rft.genre="
1002 =head2 GetAuthorisedValueDesc
1006 my $subfieldvalue =get_authorised_value_desc(
1007 $tag, $subf[$i][0],$subf[$i][1], '', $taglib, $category);
1008 Retrieve the complete description for a given authorised value.
1010 Now takes $category and $value pair too.
1011 my $auth_value_desc =GetAuthorisedValueDesc(
1012 '','', 'DVD' ,'','','CCODE');
1018 sub GetAuthorisedValueDesc {
1019 my ( $tag, $subfield, $value, $framework, $tagslib, $category ) = @_;
1020 my $dbh = C4::Context->dbh;
1024 return $value unless defined $tagslib->{$tag}->{$subfield}->{'authorised_value'};
1027 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
1028 return C4::Branch::GetBranchName($value);
1032 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
1033 return getitemtypeinfo($value)->{description};
1036 #---- "true" authorized value
1037 $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'}
1040 if ( $category ne "" ) {
1043 "SELECT lib FROM authorised_values WHERE category = ? AND authorised_value = ?"
1045 $sth->execute( $category, $value );
1046 my $data = $sth->fetchrow_hashref;
1047 return $data->{'lib'};
1050 return $value; # if nothing is found return the original value
1058 $marcnotesarray = GetMarcNotes( $record, $marcflavour );
1059 Get all notes from the MARC record and returns them in an array.
1060 The note are stored in differents places depending on MARC flavour
1067 my ( $record, $marcflavour ) = @_;
1069 if ( $marcflavour eq "MARC21" ) {
1072 else { # assume unimarc if not marc21
1079 foreach my $field ( $record->field($scope) ) {
1080 my $value = $field->as_string();
1081 $value =~ s/\n/<br \/>/g ;
1083 if ( $note ne "" ) {
1084 $marcnote = { marcnote => $note, };
1085 push @marcnotes, $marcnote;
1088 if ( $note ne $value ) {
1089 $note = $note . " " . $value;
1094 $marcnote = { marcnote => $note };
1095 push @marcnotes, $marcnote; #load last tag into array
1098 } # end GetMarcNotes
1100 =head2 GetMarcSubjects
1104 $marcsubjcts = GetMarcSubjects($record,$marcflavour);
1105 Get all subjects from the MARC record and returns them in an array.
1106 The subjects are stored in differents places depending on MARC flavour
1112 sub GetMarcSubjects {
1113 my ( $record, $marcflavour ) = @_;
1114 my ( $mintag, $maxtag );
1115 if ( $marcflavour eq "MARC21" ) {
1119 else { # assume unimarc if not marc21
1129 foreach my $field ( $record->field('6..' )) {
1130 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1132 my @subfields = $field->subfields();
1135 # if there is an authority link, build the link with an= subfield9
1136 my $subfield9 = $field->subfield('9');
1137 for my $subject_subfield (@subfields ) {
1138 # don't load unimarc subfields 3,4,5
1139 next if (($marcflavour eq "UNIMARC") and ($subject_subfield->[0] =~ /3|4|5/ ) );
1140 # don't load MARC21 subfields 2 (FIXME: any more subfields??)
1141 next if (($marcflavour eq "MARC21") and ($subject_subfield->[0] =~ /2/ ) );
1142 my $code = $subject_subfield->[0];
1143 my $value = $subject_subfield->[1];
1144 my $linkvalue = $value;
1145 $linkvalue =~ s/(\(|\))//g;
1146 my $operator = " and " unless $counter==0;
1148 @link_loop = ({'limit' => 'an' ,link => "$subfield9" });
1150 push @link_loop, {'limit' => 'su', link => $linkvalue, operator => $operator };
1152 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
1154 my @this_link_loop = @link_loop;
1155 push @subfields_loop, {code => $code, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($subject_subfield->[0] eq 9 );
1159 push @marcsubjects, { MARCSUBJECT_SUBFIELDS_LOOP => \@subfields_loop };
1162 return \@marcsubjects;
1163 } #end getMARCsubjects
1165 =head2 GetMarcAuthors
1169 authors = GetMarcAuthors($record,$marcflavour);
1170 Get all authors from the MARC record and returns them in an array.
1171 The authors are stored in differents places depending on MARC flavour
1177 sub GetMarcAuthors {
1178 my ( $record, $marcflavour ) = @_;
1179 my ( $mintag, $maxtag );
1180 # tagslib useful for UNIMARC author reponsabilities
1181 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.
1182 if ( $marcflavour eq "MARC21" ) {
1186 elsif ( $marcflavour eq "UNIMARC" ) { # assume unimarc if not marc21
1195 foreach my $field ( $record->fields ) {
1196 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1199 my @subfields = $field->subfields();
1201 # if there is an authority link, build the link with Koha-Auth-Number: subfield9
1202 my $subfield9 = $field->subfield('9');
1203 for my $authors_subfield (@subfields) {
1204 # don't load unimarc subfields 3, 5
1205 next if ($marcflavour eq 'UNIMARC' and ($authors_subfield->[0] =~ /3|5/ ) );
1206 my $subfieldcode = $authors_subfield->[0];
1207 my $value = $authors_subfield->[1];
1208 my $linkvalue = $value;
1209 $linkvalue =~ s/(\(|\))//g;
1210 my $operator = " and " unless $count_auth==0;
1211 # if we have an authority link, use that as the link, otherwise use standard searching
1213 @link_loop = ({'limit' => 'an' ,link => "$subfield9" });
1216 # reset $linkvalue if UNIMARC author responsibility
1217 if ( $marcflavour eq 'UNIMARC' and ($authors_subfield->[0] eq "4")) {
1218 $linkvalue = "(".GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ).")";
1220 push @link_loop, {'limit' => 'au', link => $linkvalue, operator => $operator };
1222 $value = GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ) if ( $marcflavour eq 'UNIMARC' and ($authors_subfield->[0] =~/4/));
1223 my @this_link_loop = @link_loop;
1224 my $separator = C4::Context->preference("authoritysep") unless $count_auth==0;
1225 push @subfields_loop, {code => $subfieldcode, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($authors_subfield->[0] eq '9' );
1228 push @marcauthors, { MARCAUTHOR_SUBFIELDS_LOOP => \@subfields_loop };
1230 return \@marcauthors;
1237 $marcurls = GetMarcUrls($record,$marcflavour);
1238 Returns arrayref of URLs from MARC data, suitable to pass to tmpl loop.
1239 Assumes web resources (not uncommon in MARC21 to omit resource type ind)
1246 my ( $record, $marcflavour ) = @_;
1249 for my $field ( $record->field('856') ) {
1252 for my $note ( $field->subfield('z') ) {
1253 push @notes, { note => $note };
1255 my @urls = $field->subfield('u');
1256 foreach my $url (@urls) {
1257 if ( $marcflavour eq 'MARC21' ) {
1258 my $s3 = $field->subfield('3');
1259 my $link = $field->subfield('y');
1260 unless ( $url =~ /^\w+:/ ) {
1261 if ( $field->indicator(1) eq '7' ) {
1262 $url = $field->subfield('2') . "://" . $url;
1263 } elsif ( $field->indicator(1) eq '1' ) {
1264 $url = 'ftp://' . $url;
1266 # properly, this should be if ind1=4,
1267 # however we will assume http protocol since we're building a link.
1268 $url = 'http://' . $url;
1271 # TODO handle ind 2 (relationship)
1276 $marcurl->{'linktext'} = $link || $s3 || C4::Context->preference('URLLinkText') || $url;
1277 $marcurl->{'part'} = $s3 if ($link);
1278 $marcurl->{'toc'} = 1 if ( defined($s3) && $s3 =~ /^table/i );
1280 $marcurl->{'linktext'} = $field->subfield('2') || C4::Context->preference('URLLinkText') || $url;
1281 $marcurl->{'MARCURL'} = $url;
1283 push @marcurls, $marcurl;
1289 =head2 GetMarcSeries
1293 $marcseriesarray = GetMarcSeries($record,$marcflavour);
1294 Get all series from the MARC record and returns them in an array.
1295 The series are stored in differents places depending on MARC flavour
1302 my ($record, $marcflavour) = @_;
1303 my ($mintag, $maxtag);
1304 if ($marcflavour eq "MARC21") {
1307 } else { # assume unimarc if not marc21
1317 foreach my $field ($record->field('440'), $record->field('490')) {
1319 #my $value = $field->subfield('a');
1320 #$marcsubjct = {MARCSUBJCT => $value,};
1321 my @subfields = $field->subfields();
1322 #warn "subfields:".join " ", @$subfields;
1325 for my $series_subfield (@subfields) {
1327 undef $volume_number;
1328 # see if this is an instance of a volume
1329 if ($series_subfield->[0] eq 'v') {
1333 my $code = $series_subfield->[0];
1334 my $value = $series_subfield->[1];
1335 my $linkvalue = $value;
1336 $linkvalue =~ s/(\(|\))//g;
1337 my $operator = " and " unless $counter==0;
1338 push @link_loop, {link => $linkvalue, operator => $operator };
1339 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
1340 if ($volume_number) {
1341 push @subfields_loop, {volumenum => $value};
1344 push @subfields_loop, {code => $code, value => $value, link_loop => \@link_loop, separator => $separator, volumenum => $volume_number};
1348 push @marcseries, { MARCSERIES_SUBFIELDS_LOOP => \@subfields_loop };
1349 #$marcsubjct = {MARCSUBJCT => $field->as_string(),};
1350 #push @marcsubjcts, $marcsubjct;
1354 my $marcseriessarray=\@marcseries;
1355 return $marcseriessarray;
1356 } #end getMARCseriess
1358 =head2 GetFrameworkCode
1362 $frameworkcode = GetFrameworkCode( $biblionumber )
1368 sub GetFrameworkCode {
1369 my ( $biblionumber ) = @_;
1370 my $dbh = C4::Context->dbh;
1371 my $sth = $dbh->prepare("SELECT frameworkcode FROM biblio WHERE biblionumber=?");
1372 $sth->execute($biblionumber);
1373 my ($frameworkcode) = $sth->fetchrow;
1374 return $frameworkcode;
1377 =head2 GetPublisherNameFromIsbn
1379 $name = GetPublishercodeFromIsbn($isbn);
1386 sub GetPublisherNameFromIsbn($){
1388 $isbn =~ s/[- _]//g;
1390 my @codes = (split '-', DisplayISBN($isbn));
1391 my $code = $codes[0].$codes[1].$codes[2];
1392 my $dbh = C4::Context->dbh;
1394 SELECT distinct publishercode
1397 AND publishercode IS NOT NULL
1400 my $sth = $dbh->prepare($query);
1401 $sth->execute("$code%");
1402 my $name = $sth->fetchrow;
1403 return $name if length $name;
1407 =head2 TransformKohaToMarc
1411 $record = TransformKohaToMarc( $hash )
1412 This function builds partial MARC::Record from a hash
1413 Hash entries can be from biblio or biblioitems.
1414 This function is called in acquisition module, to create a basic catalogue entry from user entry
1420 sub TransformKohaToMarc {
1422 my $sth = C4::Context->dbh->prepare(
1423 "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
1425 my $record = MARC::Record->new();
1426 SetMarcUnicodeFlag($record, C4::Context->preference("marcflavour"));
1427 foreach (keys %{$hash}) {
1428 &TransformKohaToMarcOneField( $sth, $record, $_, $hash->{$_}, '' );
1433 =head2 TransformKohaToMarcOneField
1437 $record = TransformKohaToMarcOneField( $sth, $record, $kohafieldname, $value, $frameworkcode );
1443 sub TransformKohaToMarcOneField {
1444 my ( $sth, $record, $kohafieldname, $value, $frameworkcode ) = @_;
1445 $frameworkcode='' unless $frameworkcode;
1449 if ( !defined $sth ) {
1450 my $dbh = C4::Context->dbh;
1451 $sth = $dbh->prepare(
1452 "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
1455 $sth->execute( $frameworkcode, $kohafieldname );
1456 if ( ( $tagfield, $tagsubfield ) = $sth->fetchrow ) {
1457 my $tag = $record->field($tagfield);
1459 $tag->update( $tagsubfield => $value );
1460 $record->delete_field($tag);
1461 $record->insert_fields_ordered($tag);
1464 $record->add_fields( $tagfield, " ", " ", $tagsubfield => $value );
1470 =head2 TransformHtmlToXml
1474 $xml = TransformHtmlToXml( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type )
1476 $auth_type contains :
1477 - nothing : rebuild a biblio, un UNIMARC the encoding is in 100$a pos 26/27
1478 - UNIMARCAUTH : rebuild an authority. In UNIMARC, the encoding is in 100$a pos 13/14
1479 - ITEM : rebuild an item : in UNIMARC, 100$a, it's in the biblio ! (otherwise, we would get 2 100 fields !)
1485 sub TransformHtmlToXml {
1486 my ( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type ) = @_;
1487 my $xml = MARC::File::XML::header('UTF-8');
1488 $auth_type = C4::Context->preference('marcflavour') unless $auth_type;
1489 MARC::File::XML->default_record_format($auth_type);
1490 # in UNIMARC, field 100 contains the encoding
1491 # check that there is one, otherwise the
1492 # MARC::Record->new_from_xml will fail (and Koha will die)
1493 my $unimarc_and_100_exist=0;
1494 $unimarc_and_100_exist=1 if $auth_type eq 'ITEM'; # if we rebuild an item, no need of a 100 field
1499 for ( my $i = 0 ; $i < @$tags ; $i++ ) {
1500 if (C4::Context->preference('marcflavour') eq 'UNIMARC' and @$tags[$i] eq "100" and @$subfields[$i] eq "a") {
1501 # if we have a 100 field and it's values are not correct, skip them.
1502 # if we don't have any valid 100 field, we will create a default one at the end
1503 my $enc = substr( @$values[$i], 26, 2 );
1504 if ($enc eq '01' or $enc eq '50' or $enc eq '03') {
1505 $unimarc_and_100_exist=1;
1510 @$values[$i] =~ s/&/&/g;
1511 @$values[$i] =~ s/</</g;
1512 @$values[$i] =~ s/>/>/g;
1513 @$values[$i] =~ s/"/"/g;
1514 @$values[$i] =~ s/'/'/g;
1515 # if ( !utf8::is_utf8( @$values[$i] ) ) {
1516 # utf8::decode( @$values[$i] );
1518 if ( ( @$tags[$i] ne $prevtag ) ) {
1519 $j++ unless ( @$tags[$i] eq "" );
1521 $xml .= "</datafield>\n";
1522 if ( ( @$tags[$i] && @$tags[$i] > 10 )
1523 && ( @$values[$i] ne "" ) )
1525 my $ind1 = substr( @$indicator[$j], 0, 1 );
1527 if ( @$indicator[$j] ) {
1528 $ind2 = substr( @$indicator[$j], 1, 1 );
1531 warn "Indicator in @$tags[$i] is empty";
1535 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1537 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1545 if ( @$values[$i] ne "" ) {
1548 if ( @$tags[$i] eq "000" ) {
1549 $xml .= "<leader>@$values[$i]</leader>\n";
1552 # rest of the fixed fields
1554 elsif ( @$tags[$i] < 10 ) {
1556 "<controlfield tag=\"@$tags[$i]\">@$values[$i]</controlfield>\n";
1560 my $ind1 = substr( @$indicator[$j], 0, 1 );
1561 my $ind2 = substr( @$indicator[$j], 1, 1 );
1563 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1565 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1571 else { # @$tags[$i] eq $prevtag
1572 if ( @$values[$i] eq "" ) {
1576 my $ind1 = substr( @$indicator[$j], 0, 1 );
1577 my $ind2 = substr( @$indicator[$j], 1, 1 );
1579 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1583 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1586 $prevtag = @$tags[$i];
1588 $xml .= "</datafield>\n" if @$tags > 0;
1589 if (C4::Context->preference('marcflavour') eq 'UNIMARC' and !$unimarc_and_100_exist) {
1590 # warn "SETTING 100 for $auth_type";
1591 my $string = strftime( "%Y%m%d", localtime(time) );
1592 # set 50 to position 26 is biblios, 13 if authorities
1594 $pos=13 if $auth_type eq 'UNIMARCAUTH';
1595 $string = sprintf( "%-*s", 35, $string );
1596 substr( $string, $pos , 6, "50" );
1597 $xml .= "<datafield tag=\"100\" ind1=\"\" ind2=\"\">\n";
1598 $xml .= "<subfield code=\"a\">$string</subfield>\n";
1599 $xml .= "</datafield>\n";
1601 $xml .= MARC::File::XML::footer();
1605 =head2 TransformHtmlToMarc
1607 L<$record> = TransformHtmlToMarc(L<$params>,L<$cgi>)
1608 L<$params> is a ref to an array as below:
1610 'tag_010_indicator1_531951' ,
1611 'tag_010_indicator2_531951' ,
1612 'tag_010_code_a_531951_145735' ,
1613 'tag_010_subfield_a_531951_145735' ,
1614 'tag_200_indicator1_873510' ,
1615 'tag_200_indicator2_873510' ,
1616 'tag_200_code_a_873510_673465' ,
1617 'tag_200_subfield_a_873510_673465' ,
1618 'tag_200_code_b_873510_704318' ,
1619 'tag_200_subfield_b_873510_704318' ,
1620 'tag_200_code_e_873510_280822' ,
1621 'tag_200_subfield_e_873510_280822' ,
1622 'tag_200_code_f_873510_110730' ,
1623 'tag_200_subfield_f_873510_110730' ,
1625 L<$cgi> is the CGI object which containts the value.
1626 L<$record> is the MARC::Record object.
1630 sub TransformHtmlToMarc {
1634 # explicitly turn on the UTF-8 flag for all
1635 # 'tag_' parameters to avoid incorrect character
1636 # conversion later on
1637 my $cgi_params = $cgi->Vars;
1638 foreach my $param_name (keys %$cgi_params) {
1639 if ($param_name =~ /^tag_/) {
1640 my $param_value = $cgi_params->{$param_name};
1641 if (utf8::decode($param_value)) {
1642 $cgi_params->{$param_name} = $param_value;
1644 # FIXME - need to do something if string is not valid UTF-8
1648 # creating a new record
1649 my $record = MARC::Record->new();
1652 while ($params->[$i]){ # browse all CGI params
1653 my $param = $params->[$i];
1655 # if we are on biblionumber, store it in the MARC::Record (it may not be in the edited fields)
1656 if ($param eq 'biblionumber') {
1657 my ( $biblionumbertagfield, $biblionumbertagsubfield ) =
1658 &GetMarcFromKohaField( "biblio.biblionumber", '' );
1659 if ($biblionumbertagfield < 10) {
1660 $newfield = MARC::Field->new(
1661 $biblionumbertagfield,
1662 $cgi->param($param),
1665 $newfield = MARC::Field->new(
1666 $biblionumbertagfield,
1669 "$biblionumbertagsubfield" => $cgi->param($param),
1672 push @fields,$newfield if($newfield);
1674 elsif ($param =~ /^tag_(\d*)_indicator1_/){ # new field start when having 'input name="..._indicator1_..."
1677 my $ind1 = substr($cgi->param($param),0,1);
1678 my $ind2 = substr($cgi->param($params->[$i+1]),0,1);
1682 if($tag < 10){ # no code for theses fields
1683 # in MARC editor, 000 contains the leader.
1684 if ($tag eq '000' ) {
1685 $record->leader($cgi->param($params->[$j+1])) if length($cgi->param($params->[$j+1]))==24;
1686 # between 001 and 009 (included)
1687 } elsif ($cgi->param($params->[$j+1]) ne '') {
1688 $newfield = MARC::Field->new(
1690 $cgi->param($params->[$j+1]),
1693 # > 009, deal with subfields
1695 while(defined $params->[$j] && $params->[$j] =~ /_code_/){ # browse all it's subfield
1696 my $inner_param = $params->[$j];
1698 if($cgi->param($params->[$j+1]) ne ''){ # only if there is a value (code => value)
1699 $newfield->add_subfields(
1700 $cgi->param($inner_param) => $cgi->param($params->[$j+1])
1704 if ( $cgi->param($params->[$j+1]) ne '' ) { # creating only if there is a value (code => value)
1705 $newfield = MARC::Field->new(
1709 $cgi->param($inner_param) => $cgi->param($params->[$j+1]),
1716 push @fields,$newfield if($newfield);
1721 $record->append_fields(@fields);
1725 # cache inverted MARC field map
1726 our $inverted_field_map;
1728 =head2 TransformMarcToKoha
1732 $result = TransformMarcToKoha( $dbh, $record, $frameworkcode )
1736 Extract data from a MARC bib record into a hashref representing
1737 Koha biblio, biblioitems, and items fields.
1740 sub TransformMarcToKoha {
1741 my ( $dbh, $record, $frameworkcode, $limit_table ) = @_;
1744 $limit_table=$limit_table||0;
1745 $frameworkcode = '' unless defined $frameworkcode;
1747 unless (defined $inverted_field_map) {
1748 $inverted_field_map = _get_inverted_marc_field_map();
1752 if ( defined $limit_table && $limit_table eq 'items') {
1753 $tables{'items'} = 1;
1755 $tables{'items'} = 1;
1756 $tables{'biblio'} = 1;
1757 $tables{'biblioitems'} = 1;
1760 # traverse through record
1761 MARCFIELD: foreach my $field ($record->fields()) {
1762 my $tag = $field->tag();
1763 next MARCFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag};
1764 if ($field->is_control_field()) {
1765 my $kohafields = $inverted_field_map->{$frameworkcode}->{$tag}->{list};
1766 ENTRY: foreach my $entry (@{ $kohafields }) {
1767 my ($subfield, $table, $column) = @{ $entry };
1768 next ENTRY unless exists $tables{$table};
1769 my $key = _disambiguate($table, $column);
1770 if ($result->{$key}) {
1771 unless (($key eq "biblionumber" or $key eq "biblioitemnumber") and ($field->data() eq "")) {
1772 $result->{$key} .= " | " . $field->data();
1775 $result->{$key} = $field->data();
1779 # deal with subfields
1780 MARCSUBFIELD: foreach my $sf ($field->subfields()) {
1781 my $code = $sf->[0];
1782 next MARCSUBFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code};
1783 my $value = $sf->[1];
1784 SFENTRY: foreach my $entry (@{ $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code} }) {
1785 my ($table, $column) = @{ $entry };
1786 next SFENTRY unless exists $tables{$table};
1787 my $key = _disambiguate($table, $column);
1788 if ($result->{$key}) {
1789 unless (($key eq "biblionumber" or $key eq "biblioitemnumber") and ($value eq "")) {
1790 $result->{$key} .= " | " . $value;
1793 $result->{$key} = $value;
1800 # modify copyrightdate to keep only the 1st year found
1801 if (exists $result->{'copyrightdate'}) {
1802 my $temp = $result->{'copyrightdate'};
1803 $temp =~ m/c(\d\d\d\d)/;
1804 if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
1805 $result->{'copyrightdate'} = $1;
1807 else { # if no cYYYY, get the 1st date.
1808 $temp =~ m/(\d\d\d\d)/;
1809 $result->{'copyrightdate'} = $1;
1813 # modify publicationyear to keep only the 1st year found
1814 if (exists $result->{'publicationyear'}) {
1815 my $temp = $result->{'publicationyear'};
1816 if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
1817 $result->{'publicationyear'} = $1;
1819 else { # if no cYYYY, get the 1st date.
1820 $temp =~ m/(\d\d\d\d)/;
1821 $result->{'publicationyear'} = $1;
1828 sub _get_inverted_marc_field_map {
1830 my $relations = C4::Context->marcfromkohafield;
1832 foreach my $frameworkcode (keys %{ $relations }) {
1833 foreach my $kohafield (keys %{ $relations->{$frameworkcode} }) {
1834 next unless @{ $relations->{$frameworkcode}->{$kohafield} }; # not all columns are mapped to MARC tag & subfield
1835 my $tag = $relations->{$frameworkcode}->{$kohafield}->[0];
1836 my $subfield = $relations->{$frameworkcode}->{$kohafield}->[1];
1837 my ($table, $column) = split /[.]/, $kohafield, 2;
1838 push @{ $field_map->{$frameworkcode}->{$tag}->{list} }, [ $subfield, $table, $column ];
1839 push @{ $field_map->{$frameworkcode}->{$tag}->{sfs}->{$subfield} }, [ $table, $column ];
1845 =head2 _disambiguate
1849 $newkey = _disambiguate($table, $field);
1851 This is a temporary hack to distinguish between the
1852 following sets of columns when using TransformMarcToKoha.
1854 items.cn_source & biblioitems.cn_source
1855 items.cn_sort & biblioitems.cn_sort
1857 Columns that are currently NOT distinguished (FIXME
1858 due to lack of time to fully test) are:
1860 biblio.notes and biblioitems.notes
1865 FIXME - this is necessary because prefixing each column
1866 name with the table name would require changing lots
1867 of code and templates, and exposing more of the DB
1868 structure than is good to the UI templates, particularly
1869 since biblio and bibloitems may well merge in a future
1870 version. In the future, it would also be good to
1871 separate DB access and UI presentation field names
1879 my ($table, $column) = @_;
1880 if ($column eq "cn_sort" or $column eq "cn_source") {
1881 return $table . '.' . $column;
1888 =head2 get_koha_field_from_marc
1892 $result->{_disambiguate($table, $field)} = get_koha_field_from_marc($table,$field,$record,$frameworkcode);
1894 Internal function to map data from the MARC record to a specific non-MARC field.
1895 FIXME: this is meant to replace TransformMarcToKohaOneField after more testing.
1901 sub get_koha_field_from_marc {
1902 my ($koha_table,$koha_column,$record,$frameworkcode) = @_;
1903 my ( $tagfield, $subfield ) = GetMarcFromKohaField( $koha_table.'.'.$koha_column, $frameworkcode );
1905 foreach my $field ( $record->field($tagfield) ) {
1906 if ( $field->tag() < 10 ) {
1908 $kohafield .= " | " . $field->data();
1911 $kohafield = $field->data();
1915 if ( $field->subfields ) {
1916 my @subfields = $field->subfields();
1917 foreach my $subfieldcount ( 0 .. $#subfields ) {
1918 if ( $subfields[$subfieldcount][0] eq $subfield ) {
1921 " | " . $subfields[$subfieldcount][1];
1925 $subfields[$subfieldcount][1];
1936 =head2 TransformMarcToKohaOneField
1940 $result = TransformMarcToKohaOneField( $kohatable, $kohafield, $record, $result, $frameworkcode )
1946 sub TransformMarcToKohaOneField {
1948 # FIXME ? if a field has a repeatable subfield that is used in old-db,
1949 # only the 1st will be retrieved...
1950 my ( $kohatable, $kohafield, $record, $result, $frameworkcode ) = @_;
1952 my ( $tagfield, $subfield ) =
1953 GetMarcFromKohaField( $kohatable . "." . $kohafield,
1955 foreach my $field ( $record->field($tagfield) ) {
1956 if ( $field->tag() < 10 ) {
1957 if ( $result->{$kohafield} ) {
1958 $result->{$kohafield} .= " | " . $field->data();
1961 $result->{$kohafield} = $field->data();
1965 if ( $field->subfields ) {
1966 my @subfields = $field->subfields();
1967 foreach my $subfieldcount ( 0 .. $#subfields ) {
1968 if ( $subfields[$subfieldcount][0] eq $subfield ) {
1969 if ( $result->{$kohafield} ) {
1970 $result->{$kohafield} .=
1971 " | " . $subfields[$subfieldcount][1];
1974 $result->{$kohafield} =
1975 $subfields[$subfieldcount][1];
1985 =head1 OTHER FUNCTIONS
1988 =head2 PrepareItemrecordDisplay
1992 PrepareItemrecordDisplay($itemrecord,$bibnum,$itemumber);
1994 Returns a hash with all the fields for Display a given item data in a template
2000 sub PrepareItemrecordDisplay {
2002 my ( $bibnum, $itemnum, $defaultvalues ) = @_;
2004 my $dbh = C4::Context->dbh;
2005 my $frameworkcode = &GetFrameworkCode( $bibnum );
2006 my ( $itemtagfield, $itemtagsubfield ) =
2007 &GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
2008 my $tagslib = &GetMarcStructure( 1, $frameworkcode );
2009 my $itemrecord = C4::Items::GetMarcItem( $bibnum, $itemnum) if ($itemnum);
2011 my $authorised_values_sth =
2013 "SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib"
2015 foreach my $tag ( sort keys %{$tagslib} ) {
2016 my $previous_tag = '';
2018 # loop through each subfield
2020 foreach my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
2021 next if ( subfield_is_koha_internal_p($subfield) );
2022 next if ( $tagslib->{$tag}->{$subfield}->{'tab'} ne "10" );
2024 $subfield_data{tag} = $tag;
2025 $subfield_data{subfield} = $subfield;
2026 $subfield_data{countsubfield} = $cntsubf++;
2027 $subfield_data{kohafield} =
2028 $tagslib->{$tag}->{$subfield}->{'kohafield'};
2030 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
2031 $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib};
2032 $subfield_data{mandatory} =
2033 $tagslib->{$tag}->{$subfield}->{mandatory};
2034 $subfield_data{repeatable} =
2035 $tagslib->{$tag}->{$subfield}->{repeatable};
2036 $subfield_data{hidden} = "display:none"
2037 if $tagslib->{$tag}->{$subfield}->{hidden};
2039 ( $x, $value ) = _find_value( $tag, $subfield, $itemrecord )
2041 $value =~ s/"/"/g;
2043 # search for itemcallnumber if applicable
2044 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
2045 'items.itemcallnumber'
2046 && C4::Context->preference('itemcallnumber') )
2049 substr( C4::Context->preference('itemcallnumber'), 0, 3 );
2051 substr( C4::Context->preference('itemcallnumber'), 3, 1 );
2052 my $temp = $itemrecord->field($CNtag) if ($itemrecord);
2054 $value = $temp->subfield($CNsubfield);
2057 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
2058 'items.itemcallnumber'
2059 && $defaultvalues->{'callnumber'} )
2061 my $temp = $itemrecord->field($subfield) if ($itemrecord);
2063 $value = $defaultvalues->{'callnumber'};
2066 if ( ($tagslib->{$tag}->{$subfield}->{kohafield} eq
2067 'items.holdingbranch' ||
2068 $tagslib->{$tag}->{$subfield}->{kohafield} eq
2070 && $defaultvalues->{'branchcode'} )
2072 my $temp = $itemrecord->field($subfield) if ($itemrecord);
2074 $value = $defaultvalues->{branchcode};
2077 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
2078 my @authorised_values;
2081 # builds list, depending on authorised value...
2083 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq
2086 if ( ( C4::Context->preference("IndependantBranches") )
2087 && ( C4::Context->userenv->{flags} != 1 ) )
2091 "SELECT branchcode,branchname FROM branches WHERE branchcode = ? ORDER BY branchname"
2093 $sth->execute( C4::Context->userenv->{branch} );
2094 push @authorised_values, ""
2096 $tagslib->{$tag}->{$subfield}->{mandatory} );
2097 while ( my ( $branchcode, $branchname ) =
2098 $sth->fetchrow_array )
2100 push @authorised_values, $branchcode;
2101 $authorised_lib{$branchcode} = $branchname;
2107 "SELECT branchcode,branchname FROM branches ORDER BY branchname"
2110 push @authorised_values, ""
2112 $tagslib->{$tag}->{$subfield}->{mandatory} );
2113 while ( my ( $branchcode, $branchname ) =
2114 $sth->fetchrow_array )
2116 push @authorised_values, $branchcode;
2117 $authorised_lib{$branchcode} = $branchname;
2123 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq
2128 "SELECT itemtype,description FROM itemtypes ORDER BY description"
2131 push @authorised_values, ""
2132 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2133 while ( my ( $itemtype, $description ) =
2134 $sth->fetchrow_array )
2136 push @authorised_values, $itemtype;
2137 $authorised_lib{$itemtype} = $description;
2140 #---- "true" authorised value
2143 $authorised_values_sth->execute(
2144 $tagslib->{$tag}->{$subfield}->{authorised_value} );
2145 push @authorised_values, ""
2146 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2147 while ( my ( $value, $lib ) =
2148 $authorised_values_sth->fetchrow_array )
2150 push @authorised_values, $value;
2151 $authorised_lib{$value} = $lib;
2154 $subfield_data{marc_value} = CGI::scrolling_list(
2155 -name => 'field_value',
2156 -values => \@authorised_values,
2157 -default => "$value",
2158 -labels => \%authorised_lib,
2165 $subfield_data{marc_value} =
2166 "<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"50\" maxlength=\"255\" />";
2168 push( @loop_data, \%subfield_data );
2172 my $itemnumber = $itemrecord->subfield( $itemtagfield, $itemtagsubfield )
2173 if ( $itemrecord && $itemrecord->field($itemtagfield) );
2175 'itemtagfield' => $itemtagfield,
2176 'itemtagsubfield' => $itemtagsubfield,
2177 'itemnumber' => $itemnumber,
2178 'iteminformation' => \@loop_data
2184 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2186 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2187 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2188 # =head2 ModZebrafiles
2190 # &ModZebrafiles( $dbh, $biblionumber, $record, $folder, $server );
2194 # sub ModZebrafiles {
2196 # my ( $dbh, $biblionumber, $record, $folder, $server ) = @_;
2200 # C4::Context->zebraconfig($server)->{directory} . "/" . $folder . "/";
2201 # unless ( opendir( DIR, "$zebradir" ) ) {
2202 # warn "$zebradir not found";
2206 # my $filename = $zebradir . $biblionumber;
2209 # open( OUTPUT, ">", $filename . ".xml" );
2210 # print OUTPUT $record;
2219 ModZebra( $biblionumber, $op, $server, $oldRecord, $newRecord );
2221 $biblionumber is the biblionumber we want to index
2222 $op is specialUpdate or delete, and is used to know what we want to do
2223 $server is the server that we want to update
2224 $oldRecord is the MARC::Record containing the previous version of the record. This is used only when
2225 NoZebra=1, as NoZebra indexing needs to know the previous version of a record in order to
2227 $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.
2234 ###Accepts a $server variable thus we can use it for biblios authorities or other zebra dbs
2235 my ( $biblionumber, $op, $server, $oldRecord, $newRecord ) = @_;
2236 my $dbh=C4::Context->dbh;
2238 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2240 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2241 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2243 if (C4::Context->preference("NoZebra")) {
2244 # lock the nozebra table : we will read index lines, update them in Perl process
2245 # and write everything in 1 transaction.
2246 # lock the table to avoid someone else overwriting what we are doing
2247 $dbh->do('LOCK TABLES nozebra WRITE,biblio WRITE,biblioitems WRITE, systempreferences WRITE, auth_types WRITE, auth_header WRITE, auth_subfield_structure READ');
2248 my %result; # the result hash that will be built by deletion / add, and written on mySQL at the end, to improve speed
2249 if ($op eq 'specialUpdate') {
2250 # OK, we have to add or update the record
2251 # 1st delete (virtually, in indexes), if record actually exists
2253 %result = _DelBiblioNoZebra($biblionumber,$oldRecord,$server);
2255 # ... add the record
2256 %result=_AddBiblioNoZebra($biblionumber,$newRecord, $server, %result);
2258 # it's a deletion, delete the record...
2259 # warn "DELETE the record $biblionumber on $server".$record->as_formatted;
2260 %result=_DelBiblioNoZebra($biblionumber,$oldRecord,$server);
2262 # ok, now update the database...
2263 my $sth = $dbh->prepare("UPDATE nozebra SET biblionumbers=? WHERE server=? AND indexname=? AND value=?");
2264 foreach my $key (keys %result) {
2265 foreach my $index (keys %{$result{$key}}) {
2266 $sth->execute($result{$key}->{$index}, $server, $key, $index);
2269 $dbh->do('UNLOCK TABLES');
2272 # we use zebra, just fill zebraqueue table
2274 my $check_sql = "SELECT COUNT(*) FROM zebraqueue
2276 AND biblio_auth_number = ?
2279 my $check_sth = $dbh->prepare_cached($check_sql);
2280 $check_sth->execute($server, $biblionumber, $op);
2281 my ($count) = $check_sth->fetchrow_array;
2282 $check_sth->finish();
2284 my $sth=$dbh->prepare("INSERT INTO zebraqueue (biblio_auth_number,server,operation) VALUES(?,?,?)");
2285 $sth->execute($biblionumber,$server,$op);
2291 =head2 GetNoZebraIndexes
2293 %indexes = GetNoZebraIndexes;
2295 return the data from NoZebraIndexes syspref.
2299 sub GetNoZebraIndexes {
2300 my $no_zebra_indexes = C4::Context->preference('NoZebraIndexes');
2302 INDEX: foreach my $line (split /['"],[\n\r]*/,$no_zebra_indexes) {
2303 $line =~ /(.*)=>(.*)/;
2304 my $index = $1; # initial ' or " is removed afterwards
2306 $index =~ s/'|"|\s//g;
2307 $fields =~ s/'|"|\s//g;
2308 $indexes{$index}=$fields;
2313 =head1 INTERNAL FUNCTIONS
2315 =head2 _DelBiblioNoZebra($biblionumber,$record,$server);
2317 function to delete a biblio in NoZebra indexes
2318 This function does NOT delete anything in database : it reads all the indexes entries
2319 that have to be deleted & delete them in the hash
2320 The SQL part is done either :
2321 - after the Add if we are modifying a biblio (delete + add again)
2322 - immediatly after this sub if we are doing a true deletion.
2323 $server can be 'biblioserver' or 'authorityserver' : it indexes biblios or authorities (in the same table, $server being part of the table itself
2328 sub _DelBiblioNoZebra {
2329 my ($biblionumber, $record, $server)=@_;
2332 my $dbh = C4::Context->dbh;
2336 if ($server eq 'biblioserver') {
2337 %index=GetNoZebraIndexes;
2338 # get title of the record (to store the 10 first letters with the index)
2339 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title','');
2340 $title = lc($record->subfield($titletag,$titlesubfield));
2342 # for authorities, the "title" is the $a mainentry
2343 my ($auth_type_tag, $auth_type_sf) = C4::AuthoritiesMarc::get_auth_type_location();
2344 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield($auth_type_tag, $auth_type_sf));
2345 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
2346 $title = $record->subfield($authref->{auth_tag_to_report},'a');
2347 $index{'mainmainentry'}= $authref->{'auth_tag_to_report'}.'a';
2348 $index{'mainentry'} = $authref->{'auth_tag_to_report'}.'*';
2349 $index{'auth_type'} = "${auth_type_tag}${auth_type_sf}";
2353 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2354 $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
2355 # limit to 10 char, should be enough, and limit the DB size
2356 $title = substr($title,0,10);
2358 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2359 foreach my $field ($record->fields()) {
2360 #parse each subfield
2361 next if $field->tag <10;
2362 foreach my $subfield ($field->subfields()) {
2363 my $tag = $field->tag();
2364 my $subfieldcode = $subfield->[0];
2366 # check each index to see if the subfield is stored somewhere
2367 # otherwise, store it in __RAW__ index
2368 foreach my $key (keys %index) {
2369 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2370 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
2372 my $line= lc $subfield->[1];
2373 # remove meaningless value in the field...
2374 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2375 # ... and split in words
2376 foreach (split / /,$line) {
2377 next unless $_; # skip empty values (multiple spaces)
2378 # if the entry is already here, do nothing, the biblionumber has already be removed
2379 unless ( defined( $result{$key}->{$_} ) && ( $result{$key}->{$_} =~ /$biblionumber,$title\-(\d);/) ) {
2380 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2381 $sth2->execute($server,$key,$_);
2382 my $existing_biblionumbers = $sth2->fetchrow;
2384 if ($existing_biblionumbers) {
2385 # warn " existing for $key $_: $existing_biblionumbers";
2386 $result{$key}->{$_} =$existing_biblionumbers;
2387 $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2393 # the subfield is not indexed, store it in __RAW__ index anyway
2395 my $line= lc $subfield->[1];
2396 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2397 # ... and split in words
2398 foreach (split / /,$line) {
2399 next unless $_; # skip empty values (multiple spaces)
2400 # if the entry is already here, do nothing, the biblionumber has already be removed
2401 unless ($result{'__RAW__'}->{$_} =~ /$biblionumber,$title\-(\d);/) {
2402 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2403 $sth2->execute($server,'__RAW__',$_);
2404 my $existing_biblionumbers = $sth2->fetchrow;
2406 if ($existing_biblionumbers) {
2407 $result{'__RAW__'}->{$_} =$existing_biblionumbers;
2408 $result{'__RAW__'}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2418 =head2 _AddBiblioNoZebra($biblionumber, $record, $server, %result);
2420 function to add a biblio in NoZebra indexes
2424 sub _AddBiblioNoZebra {
2425 my ($biblionumber, $record, $server, %result)=@_;
2426 my $dbh = C4::Context->dbh;
2430 if ($server eq 'biblioserver') {
2431 %index=GetNoZebraIndexes;
2432 # get title of the record (to store the 10 first letters with the index)
2433 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title','');
2434 $title = lc($record->subfield($titletag,$titlesubfield));
2436 # warn "server : $server";
2437 # for authorities, the "title" is the $a mainentry
2438 my ($auth_type_tag, $auth_type_sf) = C4::AuthoritiesMarc::get_auth_type_location();
2439 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield($auth_type_tag, $auth_type_sf));
2440 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
2441 $title = $record->subfield($authref->{auth_tag_to_report},'a');
2442 $index{'mainmainentry'} = $authref->{auth_tag_to_report}.'a';
2443 $index{'mainentry'} = $authref->{auth_tag_to_report}.'*';
2444 $index{'auth_type'} = "${auth_type_tag}${auth_type_sf}";
2447 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2448 $title =~ s/ |\.|,|;|\[|\]|\(|\)|\*|-|'|:|=|\r|\n//g;
2449 # limit to 10 char, should be enough, and limit the DB size
2450 $title = substr($title,0,10);
2452 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2453 foreach my $field ($record->fields()) {
2454 #parse each subfield
2455 ###FIXME: impossible to index a 001-009 value with NoZebra
2456 next if $field->tag <10;
2457 foreach my $subfield ($field->subfields()) {
2458 my $tag = $field->tag();
2459 my $subfieldcode = $subfield->[0];
2461 # warn "INDEXING :".$subfield->[1];
2462 # check each index to see if the subfield is stored somewhere
2463 # otherwise, store it in __RAW__ index
2464 foreach my $key (keys %index) {
2465 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2466 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
2468 my $line= lc $subfield->[1];
2469 # remove meaningless value in the field...
2470 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g;
2471 # ... and split in words
2472 foreach (split / /,$line) {
2473 next unless $_; # skip empty values (multiple spaces)
2474 # if the entry is already here, improve weight
2475 # warn "managing $_";
2476 if ( exists $result{$key}->{$_} && $result{$key}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d+);/) {
2477 my $weight = $1 + 1;
2478 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
2479 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2481 # get the value if it exist in the nozebra table, otherwise, create it
2482 $sth2->execute($server,$key,$_);
2483 my $existing_biblionumbers = $sth2->fetchrow;
2485 if ($existing_biblionumbers) {
2486 $result{$key}->{"$_"} =$existing_biblionumbers;
2487 my $weight = defined $1 ? $1 + 1 : 1;
2488 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
2489 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2490 # create a new ligne for this entry
2492 # warn "INSERT : $server / $key / $_";
2493 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname='.$dbh->quote($key).',value='.$dbh->quote($_));
2494 $result{$key}->{"$_"}.="$biblionumber,$title-1;";
2500 # the subfield is not indexed, store it in __RAW__ index anyway
2502 my $line= lc $subfield->[1];
2503 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g;
2504 # ... and split in words
2505 foreach (split / /,$line) {
2506 next unless $_; # skip empty values (multiple spaces)
2507 # if the entry is already here, improve weight
2508 if ($result{'__RAW__'}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d+);/) {
2510 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
2511 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2513 # get the value if it exist in the nozebra table, otherwise, create it
2514 $sth2->execute($server,'__RAW__',$_);
2515 my $existing_biblionumbers = $sth2->fetchrow;
2517 if ($existing_biblionumbers) {
2518 $result{'__RAW__'}->{"$_"} =$existing_biblionumbers;
2520 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
2521 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2522 # create a new ligne for this entry
2524 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname="__RAW__",value='.$dbh->quote($_));
2525 $result{'__RAW__'}->{"$_"}.="$biblionumber,$title-1;";
2540 ($indicators, $value) = _find_value($tag, $subfield, $record,$encoding);
2542 Find the given $subfield in the given $tag in the given
2543 MARC::Record $record. If the subfield is found, returns
2544 the (indicators, value) pair; otherwise, (undef, undef) is
2548 Such a function is used in addbiblio AND additem and serial-edit and maybe could be used in Authorities.
2549 I suggest we export it from this module.
2556 my ( $tagfield, $insubfield, $record, $encoding ) = @_;
2559 if ( $tagfield < 10 ) {
2560 if ( $record->field($tagfield) ) {
2561 push @result, $record->field($tagfield)->data();
2568 foreach my $field ( $record->field($tagfield) ) {
2569 my @subfields = $field->subfields();
2570 foreach my $subfield (@subfields) {
2571 if ( @$subfield[0] eq $insubfield ) {
2572 push @result, @$subfield[1];
2573 $indicator = $field->indicator(1) . $field->indicator(2);
2578 return ( $indicator, @result );
2581 =head2 _koha_marc_update_bib_ids
2585 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
2587 Internal function to add or update biblionumber and biblioitemnumber to
2594 sub _koha_marc_update_bib_ids {
2595 my ($record, $frameworkcode, $biblionumber, $biblioitemnumber) = @_;
2597 # we must add bibnum and bibitemnum in MARC::Record...
2598 # we build the new field with biblionumber and biblioitemnumber
2599 # we drop the original field
2600 # we add the new builded field.
2601 my ($biblio_tag, $biblio_subfield ) = GetMarcFromKohaField("biblio.biblionumber",$frameworkcode);
2602 my ($biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField("biblioitems.biblioitemnumber",$frameworkcode);
2604 if ($biblio_tag != $biblioitem_tag) {
2605 # biblionumber & biblioitemnumber are in different fields
2607 # deal with biblionumber
2608 my ($new_field, $old_field);
2609 if ($biblio_tag < 10) {
2610 $new_field = MARC::Field->new( $biblio_tag, $biblionumber );
2613 MARC::Field->new( $biblio_tag, '', '',
2614 "$biblio_subfield" => $biblionumber );
2617 # drop old field and create new one...
2618 $old_field = $record->field($biblio_tag);
2619 $record->delete_field($old_field) if $old_field;
2620 $record->append_fields($new_field);
2622 # deal with biblioitemnumber
2623 if ($biblioitem_tag < 10) {
2624 $new_field = MARC::Field->new( $biblioitem_tag, $biblioitemnumber, );
2627 MARC::Field->new( $biblioitem_tag, '', '',
2628 "$biblioitem_subfield" => $biblioitemnumber, );
2630 # drop old field and create new one...
2631 $old_field = $record->field($biblioitem_tag);
2632 $record->delete_field($old_field) if $old_field;
2633 $record->insert_fields_ordered($new_field);
2636 # biblionumber & biblioitemnumber are in the same field (can't be <10 as fields <10 have only 1 value)
2637 my $new_field = MARC::Field->new(
2638 $biblio_tag, '', '',
2639 "$biblio_subfield" => $biblionumber,
2640 "$biblioitem_subfield" => $biblioitemnumber
2643 # drop old field and create new one...
2644 my $old_field = $record->field($biblio_tag);
2645 $record->delete_field($old_field) if $old_field;
2646 $record->insert_fields_ordered($new_field);
2650 =head2 _koha_marc_update_biblioitem_cn_sort
2654 _koha_marc_update_biblioitem_cn_sort($marc, $biblioitem, $frameworkcode);
2658 Given a MARC bib record and the biblioitem hash, update the
2659 subfield that contains a copy of the value of biblioitems.cn_sort.
2663 sub _koha_marc_update_biblioitem_cn_sort {
2665 my $biblioitem = shift;
2666 my $frameworkcode= shift;
2668 my ($biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField("biblioitems.cn_sort",$frameworkcode);
2669 return unless $biblioitem_tag;
2671 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2673 if (my $field = $marc->field($biblioitem_tag)) {
2674 $field->delete_subfield(code => $biblioitem_subfield);
2675 if ($cn_sort ne '') {
2676 $field->add_subfields($biblioitem_subfield => $cn_sort);
2679 # if we get here, no biblioitem tag is present in the MARC record, so
2680 # we'll create it if $cn_sort is not empty -- this would be
2681 # an odd combination of events, however
2683 $marc->insert_grouped_field(MARC::Field->new($biblioitem_tag, ' ', ' ', $biblioitem_subfield => $cn_sort));
2688 =head2 _koha_add_biblio
2692 my ($biblionumber,$error) = _koha_add_biblio($dbh,$biblioitem);
2694 Internal function to add a biblio ($biblio is a hash with the values)
2700 sub _koha_add_biblio {
2701 my ( $dbh, $biblio, $frameworkcode ) = @_;
2705 # set the series flag
2707 if ( $biblio->{'seriestitle'} ) { $serial = 1 };
2711 SET frameworkcode = ?,
2722 my $sth = $dbh->prepare($query);
2725 $biblio->{'author'},
2727 $biblio->{'unititle'},
2730 $biblio->{'seriestitle'},
2731 $biblio->{'copyrightdate'},
2732 $biblio->{'abstract'}
2735 my $biblionumber = $dbh->{'mysql_insertid'};
2736 if ( $dbh->errstr ) {
2737 $error.="ERROR in _koha_add_biblio $query".$dbh->errstr;
2742 #warn "LEAVING _koha_add_biblio: ".$biblionumber."\n";
2743 return ($biblionumber,$error);
2746 =head2 _koha_modify_biblio
2750 my ($biblionumber,$error) == _koha_modify_biblio($dbh,$biblio,$frameworkcode);
2752 Internal function for updating the biblio table
2758 sub _koha_modify_biblio {
2759 my ( $dbh, $biblio, $frameworkcode ) = @_;
2764 SET frameworkcode = ?,
2773 WHERE biblionumber = ?
2776 my $sth = $dbh->prepare($query);
2780 $biblio->{'author'},
2782 $biblio->{'unititle'},
2784 $biblio->{'serial'},
2785 $biblio->{'seriestitle'},
2786 $biblio->{'copyrightdate'},
2787 $biblio->{'abstract'},
2788 $biblio->{'biblionumber'}
2789 ) if $biblio->{'biblionumber'};
2791 if ( $dbh->errstr || !$biblio->{'biblionumber'} ) {
2792 $error.="ERROR in _koha_modify_biblio $query".$dbh->errstr;
2795 return ( $biblio->{'biblionumber'},$error );
2798 =head2 _koha_modify_biblioitem_nonmarc
2802 my ($biblioitemnumber,$error) = _koha_modify_biblioitem_nonmarc( $dbh, $biblioitem );
2804 Updates biblioitems row except for marc and marcxml, which should be changed
2811 sub _koha_modify_biblioitem_nonmarc {
2812 my ( $dbh, $biblioitem ) = @_;
2815 # re-calculate the cn_sort, it may have changed
2816 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2820 SET biblionumber = ?,
2826 publicationyear = ?,
2830 collectiontitle = ?,
2832 collectionvolume= ?,
2833 editionstatement= ?,
2834 editionresponsibility = ?,
2848 where biblioitemnumber = ?
2850 my $sth = $dbh->prepare($query);
2852 $biblioitem->{'biblionumber'},
2853 $biblioitem->{'volume'},
2854 $biblioitem->{'number'},
2855 $biblioitem->{'itemtype'},
2856 $biblioitem->{'isbn'},
2857 $biblioitem->{'issn'},
2858 $biblioitem->{'publicationyear'},
2859 $biblioitem->{'publishercode'},
2860 $biblioitem->{'volumedate'},
2861 $biblioitem->{'volumedesc'},
2862 $biblioitem->{'collectiontitle'},
2863 $biblioitem->{'collectionissn'},
2864 $biblioitem->{'collectionvolume'},
2865 $biblioitem->{'editionstatement'},
2866 $biblioitem->{'editionresponsibility'},
2867 $biblioitem->{'illus'},
2868 $biblioitem->{'pages'},
2869 $biblioitem->{'bnotes'},
2870 $biblioitem->{'size'},
2871 $biblioitem->{'place'},
2872 $biblioitem->{'lccn'},
2873 $biblioitem->{'url'},
2874 $biblioitem->{'biblioitems.cn_source'},
2875 $biblioitem->{'cn_class'},
2876 $biblioitem->{'cn_item'},
2877 $biblioitem->{'cn_suffix'},
2879 $biblioitem->{'totalissues'},
2880 $biblioitem->{'biblioitemnumber'}
2882 if ( $dbh->errstr ) {
2883 $error.="ERROR in _koha_modify_biblioitem_nonmarc $query".$dbh->errstr;
2886 return ($biblioitem->{'biblioitemnumber'},$error);
2889 =head2 _koha_add_biblioitem
2893 my ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $biblioitem );
2895 Internal function to add a biblioitem
2901 sub _koha_add_biblioitem {
2902 my ( $dbh, $biblioitem ) = @_;
2905 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2907 "INSERT INTO biblioitems SET
2914 publicationyear = ?,
2918 collectiontitle = ?,
2920 collectionvolume= ?,
2921 editionstatement= ?,
2922 editionresponsibility = ?,
2938 my $sth = $dbh->prepare($query);
2940 $biblioitem->{'biblionumber'},
2941 $biblioitem->{'volume'},
2942 $biblioitem->{'number'},
2943 $biblioitem->{'itemtype'},
2944 $biblioitem->{'isbn'},
2945 $biblioitem->{'issn'},
2946 $biblioitem->{'publicationyear'},
2947 $biblioitem->{'publishercode'},
2948 $biblioitem->{'volumedate'},
2949 $biblioitem->{'volumedesc'},
2950 $biblioitem->{'collectiontitle'},
2951 $biblioitem->{'collectionissn'},
2952 $biblioitem->{'collectionvolume'},
2953 $biblioitem->{'editionstatement'},
2954 $biblioitem->{'editionresponsibility'},
2955 $biblioitem->{'illus'},
2956 $biblioitem->{'pages'},
2957 $biblioitem->{'bnotes'},
2958 $biblioitem->{'size'},
2959 $biblioitem->{'place'},
2960 $biblioitem->{'lccn'},
2961 $biblioitem->{'marc'},
2962 $biblioitem->{'url'},
2963 $biblioitem->{'biblioitems.cn_source'},
2964 $biblioitem->{'cn_class'},
2965 $biblioitem->{'cn_item'},
2966 $biblioitem->{'cn_suffix'},
2968 $biblioitem->{'totalissues'}
2970 my $bibitemnum = $dbh->{'mysql_insertid'};
2971 if ( $dbh->errstr ) {
2972 $error.="ERROR in _koha_add_biblioitem $query".$dbh->errstr;
2976 return ($bibitemnum,$error);
2979 =head2 _koha_delete_biblio
2983 $error = _koha_delete_biblio($dbh,$biblionumber);
2985 Internal sub for deleting from biblio table -- also saves to deletedbiblio
2987 C<$dbh> - the database handle
2988 C<$biblionumber> - the biblionumber of the biblio to be deleted
2994 # FIXME: add error handling
2996 sub _koha_delete_biblio {
2997 my ( $dbh, $biblionumber ) = @_;
2999 # get all the data for this biblio
3000 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber=?");
3001 $sth->execute($biblionumber);
3003 if ( my $data = $sth->fetchrow_hashref ) {
3005 # save the record in deletedbiblio
3006 # find the fields to save
3007 my $query = "INSERT INTO deletedbiblio SET ";
3009 foreach my $temp ( keys %$data ) {
3010 $query .= "$temp = ?,";
3011 push( @bind, $data->{$temp} );
3014 # replace the last , by ",?)"
3016 my $bkup_sth = $dbh->prepare($query);
3017 $bkup_sth->execute(@bind);
3021 my $del_sth = $dbh->prepare("DELETE FROM biblio WHERE biblionumber=?");
3022 $del_sth->execute($biblionumber);
3029 =head2 _koha_delete_biblioitems
3033 $error = _koha_delete_biblioitems($dbh,$biblioitemnumber);
3035 Internal sub for deleting from biblioitems table -- also saves to deletedbiblioitems
3037 C<$dbh> - the database handle
3038 C<$biblionumber> - the biblioitemnumber of the biblioitem to be deleted
3044 # FIXME: add error handling
3046 sub _koha_delete_biblioitems {
3047 my ( $dbh, $biblioitemnumber ) = @_;
3049 # get all the data for this biblioitem
3051 $dbh->prepare("SELECT * FROM biblioitems WHERE biblioitemnumber=?");
3052 $sth->execute($biblioitemnumber);
3054 if ( my $data = $sth->fetchrow_hashref ) {
3056 # save the record in deletedbiblioitems
3057 # find the fields to save
3058 my $query = "INSERT INTO deletedbiblioitems SET ";
3060 foreach my $temp ( keys %$data ) {
3061 $query .= "$temp = ?,";
3062 push( @bind, $data->{$temp} );
3065 # replace the last , by ",?)"
3067 my $bkup_sth = $dbh->prepare($query);
3068 $bkup_sth->execute(@bind);
3071 # delete the biblioitem
3073 $dbh->prepare("DELETE FROM biblioitems WHERE biblioitemnumber=?");
3074 $del_sth->execute($biblioitemnumber);
3081 =head1 UNEXPORTED FUNCTIONS
3083 =head2 ModBiblioMarc
3085 &ModBiblioMarc($newrec,$biblionumber,$frameworkcode);
3087 Add MARC data for a biblio to koha
3089 Function exported, but should NOT be used, unless you really know what you're doing
3095 # pass the MARC::Record to this function, and it will create the records in the marc field
3096 my ( $record, $biblionumber, $frameworkcode ) = @_;
3097 my $dbh = C4::Context->dbh;
3098 my @fields = $record->fields();
3099 if ( !$frameworkcode ) {
3100 $frameworkcode = "";
3103 $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?");
3104 $sth->execute( $frameworkcode, $biblionumber );
3106 my $encoding = C4::Context->preference("marcflavour");
3108 # deal with UNIMARC field 100 (encoding) : create it if needed & set encoding to unicode
3109 if ( $encoding eq "UNIMARC" ) {
3111 if ( length($record->subfield( 100, "a" )) == 35 ) {
3112 $string = $record->subfield( 100, "a" );
3113 my $f100 = $record->field(100);
3114 $record->delete_field($f100);
3117 $string = POSIX::strftime( "%Y%m%d", localtime );
3119 $string = sprintf( "%-*s", 35, $string );
3121 substr( $string, 22, 6, "frey50" );
3122 unless ( $record->subfield( 100, "a" ) ) {
3123 $record->insert_grouped_field(
3124 MARC::Field->new( 100, "", "", "a" => $string ) );
3128 if (C4::Context->preference("NoZebra")) {
3129 # only NoZebra indexing needs to have
3130 # the previous version of the record
3131 $oldRecord = GetMarcBiblio($biblionumber);
3135 "UPDATE biblioitems SET marc=?,marcxml=? WHERE biblionumber=?");
3136 $sth->execute( $record->as_usmarc(), $record->as_xml_record($encoding),
3139 ModZebra($biblionumber,"specialUpdate","biblioserver",$oldRecord,$record);
3140 return $biblionumber;
3143 =head2 z3950_extended_services
3145 z3950_extended_services($serviceType,$serviceOptions,$record);
3147 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.
3149 C<$serviceType> one of: itemorder,create,drop,commit,update,xmlupdate
3151 C<$serviceOptions> a has of key/value pairs. For instance, if service_type is 'update', $service_options should contain:
3153 action => update action, one of specialUpdate, recordInsert, recordReplace, recordDelete, elementUpdate.
3157 recordidOpaque => Opaque Record ID (user supplied) or recordidNumber => Record ID number (system number).
3158 syntax => the record syntax (transfer syntax)
3159 databaseName = Database from connection object
3161 To set serviceOptions, call set_service_options($serviceType)
3163 C<$record> the record, if one is needed for the service type
3165 A record should be in XML. You can convert it to XML from MARC by running it through marc2xml().
3169 sub z3950_extended_services {
3170 my ( $server, $serviceType, $action, $serviceOptions ) = @_;
3172 # get our connection object
3173 my $Zconn = C4::Context->Zconn( $server, 0, 1 );
3175 # create a new package object
3176 my $Zpackage = $Zconn->package();
3179 $Zpackage->option( action => $action );
3181 if ( $serviceOptions->{'databaseName'} ) {
3182 $Zpackage->option( databaseName => $serviceOptions->{'databaseName'} );
3184 if ( $serviceOptions->{'recordIdNumber'} ) {
3186 recordIdNumber => $serviceOptions->{'recordIdNumber'} );
3188 if ( $serviceOptions->{'recordIdOpaque'} ) {
3190 recordIdOpaque => $serviceOptions->{'recordIdOpaque'} );
3193 # this is an ILL request (Zebra doesn't support it, but Koha could eventually)
3194 #if ($serviceType eq 'itemorder') {
3195 # $Zpackage->option('contact-name' => $serviceOptions->{'contact-name'});
3196 # $Zpackage->option('contact-phone' => $serviceOptions->{'contact-phone'});
3197 # $Zpackage->option('contact-email' => $serviceOptions->{'contact-email'});
3198 # $Zpackage->option('itemorder-item' => $serviceOptions->{'itemorder-item'});
3201 if ( $serviceOptions->{record} ) {
3202 $Zpackage->option( record => $serviceOptions->{record} );
3204 # can be xml or marc
3205 if ( $serviceOptions->{'syntax'} ) {
3206 $Zpackage->option( syntax => $serviceOptions->{'syntax'} );
3210 # send the request, handle any exception encountered
3211 eval { $Zpackage->send($serviceType) };
3212 if ( $@ && $@->isa("ZOOM::Exception") ) {
3213 return "error: " . $@->code() . " " . $@->message() . "\n";
3216 # free up package resources
3217 $Zpackage->destroy();
3220 =head2 set_service_options
3222 my $serviceOptions = set_service_options($serviceType);
3224 C<$serviceType> itemorder,create,drop,commit,update,xmlupdate
3226 Currently, we only support 'create', 'commit', and 'update'. 'drop' support will be added as soon as Zebra supports it.
3230 sub set_service_options {
3231 my ($serviceType) = @_;
3234 # FIXME: This needs to be an OID ... if we ever need 'syntax' this sub will need to change
3235 # $serviceOptions->{ 'syntax' } = ''; #zebra doesn't support syntaxes other than xml
3237 if ( $serviceType eq 'commit' ) {
3241 if ( $serviceType eq 'create' ) {
3245 if ( $serviceType eq 'drop' ) {
3246 die "ERROR: 'drop' not currently supported (by Zebra)";
3248 return $serviceOptions;
3251 =head3 get_biblio_authorised_values
3253 find the types and values for all authorised values assigned to this biblio.
3257 MARC::Record of the bib
3259 returns: a hashref malling the authorised value to the value set for this biblionumber
3261 $authorised_values = {
3262 'Scent' => 'flowery',
3263 'Audience' => 'Young Adult',
3264 'itemtypes' => 'SER',
3267 Notes: forlibrarian should probably be passed in, and called something different.
3272 sub get_biblio_authorised_values {
3273 my $biblionumber = shift;
3276 my $forlibrarian = 1; # are we in staff or opac?
3277 my $frameworkcode = GetFrameworkCode( $biblionumber );
3279 my $authorised_values;
3281 my $tagslib = GetMarcStructure( $forlibrarian, $frameworkcode )
3282 or return $authorised_values;
3284 # assume that these entries in the authorised_value table are bibliolevel.
3285 # ones that start with 'item%' are item level.
3286 my $query = q(SELECT distinct authorised_value, kohafield
3287 FROM marc_subfield_structure
3288 WHERE authorised_value !=''
3289 AND (kohafield like 'biblio%'
3290 OR kohafield like '') );
3291 my $bibliolevel_authorised_values = C4::Context->dbh->selectall_hashref( $query, 'authorised_value' );
3293 foreach my $tag ( keys( %$tagslib ) ) {
3294 foreach my $subfield ( keys( %{$tagslib->{ $tag }} ) ) {
3295 # warn "checking $subfield. type is: " . ref $tagslib->{ $tag }{ $subfield };
3296 if ( 'HASH' eq ref $tagslib->{ $tag }{ $subfield } ) {
3297 if ( defined $tagslib->{ $tag }{ $subfield }{'authorised_value'} && exists $bibliolevel_authorised_values->{ $tagslib->{ $tag }{ $subfield }{'authorised_value'} } ) {
3298 if ( defined $record->field( $tag ) ) {
3299 my $this_subfield_value = $record->field( $tag )->subfield( $subfield );
3300 if ( defined $this_subfield_value ) {
3301 $authorised_values->{ $tagslib->{ $tag }{ $subfield }{'authorised_value'} } = $this_subfield_value;
3308 # warn ( Data::Dumper->Dump( [ $authorised_values ], [ 'authorised_values' ] ) );
3309 return $authorised_values;
3319 Koha Developement team <info@koha.org>
3321 Paul POULAIN paul.poulain@free.fr
3323 Joshua Ferraro jmf@liblime.com