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;
29 use C4::Dates qw/format_date/;
30 use C4::Log; # logaction
34 use vars qw($VERSION @ISA @EXPORT);
40 @ISA = qw( Exporter );
54 &GetBiblioItemByBiblioNumber
55 &GetBiblioFromItemNumber
66 &GetAuthorisedValueDesc
70 &GetPublisherNameFromIsbn
85 # To link headings in a bib record
86 # to authority records.
88 &LinkBibHeadingsToAuthorities
92 # those functions are exported but should not be used
93 # they are usefull is few circumstances, so are exported.
94 # but don't use them unless you're a core developer ;-)
101 &TransformHtmlToMarc2
104 &PrepareItemrecordDisplay
111 C4::Biblio - cataloging management functions
115 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:
119 =item 1. in the biblio,biblioitems,items, etc tables, which are limited to a one-to-one mapping to underlying MARC data
121 =item 2. as raw MARC in the Zebra index and storage engine
123 =item 3. as raw MARC the biblioitems.marc and biblioitems.marcxml
127 In the 3.0 version of Koha, the authoritative record-level information is in biblioitems.marcxml
129 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.
133 =item 1. Compared with MySQL, Zebra is slow to update an index for small data changes -- especially for proc-intensive operations like circulation
135 =item 2. Zebra's index has been known to crash and a backup of the data is necessary to rebuild it in such cases
139 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:
143 =item 1. Add*/Mod*/Del*/ - high-level external functions suitable for being called from external scripts to manage the collection
145 =item 2. _koha_* - low-level internal functions for managing the koha tables
147 =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.
149 =item 4. Zebra functions used to update the Zebra index
151 =item 5. internal helper functions such as char_decode, checkitems, etc. Some of these probably belong in Koha.pm
155 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 :
159 =item 1. save datas in biblio and biblioitems table, that gives us a biblionumber and a biblioitemnumber
161 =item 2. add the biblionumber and biblioitemnumber into the MARC records
163 =item 3. save the marc record
167 When dealing with items, we must :
171 =item 1. save the item in items table, that gives us an itemnumber
173 =item 2. add the itemnumber to the item MARC field
175 =item 3. overwrite the MARC record (with the added item) into biblioitems.marc(xml)
177 When modifying a biblio or an item, the behaviour is quite similar.
181 =head1 EXPORTED FUNCTIONS
187 ($biblionumber,$biblioitemnumber) = AddBiblio($record,$frameworkcode);
191 Exported function (core API) for adding a new biblio to koha.
193 The first argument is a C<MARC::Record> object containing the
194 bib to add, while the second argument is the desired MARC
197 This function also accepts a third, optional argument: a hashref
198 to additional options. The only defined option is C<defer_marc_save>,
199 which if present and mapped to a true value, causes C<AddBiblio>
200 to omit the call to save the MARC in C<bibilioitems.marc>
201 and C<biblioitems.marcxml> This option is provided B<only>
202 for the use of scripts such as C<bulkmarcimport.pl> that may need
203 to do some manipulation of the MARC record for item parsing before
204 saving it and which cannot afford the performance hit of saving
205 the MARC record twice. Consequently, do not use that option
206 unless you can guarantee that C<ModBiblioMarc> will be called.
212 my $frameworkcode = shift;
213 my $options = @_ ? shift : undef;
214 my $defer_marc_save = 0;
215 if (defined $options and exists $options->{'defer_marc_save'} and $options->{'defer_marc_save'}) {
216 $defer_marc_save = 1;
219 my ($biblionumber,$biblioitemnumber,$error);
220 my $dbh = C4::Context->dbh;
221 # transform the data into koha-table style data
222 my $olddata = TransformMarcToKoha( $dbh, $record, $frameworkcode );
223 ($biblionumber,$error) = _koha_add_biblio( $dbh, $olddata, $frameworkcode );
224 $olddata->{'biblionumber'} = $biblionumber;
225 ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $olddata );
227 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
229 # update MARC subfield that stores biblioitems.cn_sort
230 _koha_marc_update_biblioitem_cn_sort($record, $olddata, $frameworkcode);
233 $biblionumber = ModBiblioMarc( $record, $biblionumber, $frameworkcode ) unless $defer_marc_save;
235 logaction("CATALOGUING", "ADD", $biblionumber, "biblio") if C4::Context->preference("CataloguingLog");
237 return ( $biblionumber, $biblioitemnumber );
244 ModBiblio( $record,$biblionumber,$frameworkcode);
248 Replace an existing bib record identified by C<$biblionumber>
249 with one supplied by the MARC::Record object C<$record>. The embedded
250 item, biblioitem, and biblionumber fields from the previous
251 version of the bib record replace any such fields of those tags that
252 are present in C<$record>. Consequently, ModBiblio() is not
253 to be used to try to modify item records.
255 C<$frameworkcode> specifies the MARC framework to use
256 when storing the modified bib record; among other things,
257 this controls how MARC fields get mapped to display columns
258 in the C<biblio> and C<biblioitems> tables, as well as
259 which fields are used to store embedded item, biblioitem,
260 and biblionumber data for indexing.
265 my ( $record, $biblionumber, $frameworkcode ) = @_;
266 if (C4::Context->preference("CataloguingLog")) {
267 my $newrecord = GetMarcBiblio($biblionumber);
268 logaction("CATALOGUING", "MODIFY", $biblionumber, "BEFORE=>".$newrecord->as_formatted);
271 my $dbh = C4::Context->dbh;
273 $frameworkcode = "" unless $frameworkcode;
275 # get the items before and append them to the biblio before updating the record, atm we just have the biblio
276 my ( $itemtag, $itemsubfield ) = GetMarcFromKohaField("items.itemnumber",$frameworkcode);
277 my $oldRecord = GetMarcBiblio( $biblionumber );
279 # delete any item fields from incoming record to avoid
280 # duplication or incorrect data - use AddItem() or ModItem()
282 foreach my $field ($record->field($itemtag)) {
283 $record->delete_field($field);
286 # parse each item, and, for an unknown reason, re-encode each subfield
287 # if you don't do that, the record will have encoding mixed
288 # and the biblio will be re-encoded.
289 # strange, I (Paul P.) searched more than 1 day to understand what happends
290 # but could only solve the problem this way...
291 my @fields = $oldRecord->field( $itemtag );
292 foreach my $fielditem ( @fields ){
294 foreach ($fielditem->subfields()) {
296 $field->add_subfields(Encode::encode('utf-8',$_->[0]) => Encode::encode('utf-8',$_->[1]));
298 $field = MARC::Field->new("$itemtag",'','',Encode::encode('utf-8',$_->[0]) => Encode::encode('utf-8',$_->[1]));
301 $record->append_fields($field);
304 # update biblionumber and biblioitemnumber in MARC
305 # FIXME - this is assuming a 1 to 1 relationship between
306 # biblios and biblioitems
307 my $sth = $dbh->prepare("select biblioitemnumber from biblioitems where biblionumber=?");
308 $sth->execute($biblionumber);
309 my ($biblioitemnumber) = $sth->fetchrow;
311 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
313 # load the koha-table data object
314 my $oldbiblio = TransformMarcToKoha( $dbh, $record, $frameworkcode );
316 # update MARC subfield that stores biblioitems.cn_sort
317 _koha_marc_update_biblioitem_cn_sort($record, $oldbiblio, $frameworkcode);
319 # update the MARC record (that now contains biblio and items) with the new record data
320 &ModBiblioMarc( $record, $biblionumber, $frameworkcode );
322 # modify the other koha tables
323 _koha_modify_biblio( $dbh, $oldbiblio, $frameworkcode );
324 _koha_modify_biblioitem_nonmarc( $dbh, $oldbiblio );
328 =head2 ModBiblioframework
330 ModBiblioframework($biblionumber,$frameworkcode);
331 Exported function to modify a biblio framework
335 sub ModBiblioframework {
336 my ( $biblionumber, $frameworkcode ) = @_;
337 my $dbh = C4::Context->dbh;
338 my $sth = $dbh->prepare(
339 "UPDATE biblio SET frameworkcode=? WHERE biblionumber=?"
341 $sth->execute($frameworkcode, $biblionumber);
349 my $error = &DelBiblio($dbh,$biblionumber);
350 Exported function (core API) for deleting a biblio in koha.
351 Deletes biblio record from Zebra and Koha tables (biblio,biblioitems,items)
352 Also backs it up to deleted* tables
353 Checks to make sure there are not issues on any of the items
355 C<$error> : undef unless an error occurs
362 my ( $biblionumber ) = @_;
363 my $dbh = C4::Context->dbh;
364 my $error; # for error handling
366 # First make sure this biblio has no items attached
367 my $sth = $dbh->prepare("SELECT itemnumber FROM items WHERE biblionumber=?");
368 $sth->execute($biblionumber);
369 if (my $itemnumber = $sth->fetchrow){
370 # Fix this to use a status the template can understand
371 $error .= "This Biblio has items attached, please delete them first before deleting this biblio ";
374 return $error if $error;
376 # Delete in Zebra. Be careful NOT to move this line after _koha_delete_biblio
377 # for at least 2 reasons :
378 # - we need to read the biblio if NoZebra is set (to remove it from the indexes
379 # - if something goes wrong, the biblio may be deleted from Koha but not from zebra
380 # 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)
382 if (C4::Context->preference("NoZebra")) {
383 # only NoZebra indexing needs to have
384 # the previous version of the record
385 $oldRecord = GetMarcBiblio($biblionumber);
387 ModZebra($biblionumber, "recordDelete", "biblioserver", $oldRecord, undef);
389 # delete biblioitems and items from Koha tables and save in deletedbiblioitems,deleteditems
392 "SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=?");
393 $sth->execute($biblionumber);
394 while ( my $biblioitemnumber = $sth->fetchrow ) {
396 # delete this biblioitem
397 $error = _koha_delete_biblioitems( $dbh, $biblioitemnumber );
398 return $error if $error;
401 # delete biblio from Koha tables and save in deletedbiblio
402 # must do this *after* _koha_delete_biblioitems, otherwise
403 # delete cascade will prevent deletedbiblioitems rows
404 # from being generated by _koha_delete_biblioitems
405 $error = _koha_delete_biblio( $dbh, $biblionumber );
407 logaction("CATALOGUING", "DELETE", $biblionumber, "") if C4::Context->preference("CataloguingLog");
412 =head2 LinkBibHeadingsToAuthorities
416 my $headings_linked = LinkBibHeadingsToAuthorities($marc);
420 Links bib headings to authority records by checking
421 each authority-controlled field in the C<MARC::Record>
422 object C<$marc>, looking for a matching authority record,
423 and setting the linking subfield $9 to the ID of that
426 If no matching authority exists, or if multiple
427 authorities match, no $9 will be added, and any
428 existing one inthe field will be deleted.
430 Returns the number of heading links changed in the
435 sub LinkBibHeadingsToAuthorities {
438 my $num_headings_changed = 0;
439 foreach my $field ($bib->fields()) {
440 my $heading = C4::Heading->new_from_bib_field($field);
441 next unless defined $heading;
444 my $current_link = $field->subfield('9');
446 # look for matching authorities
447 my $authorities = $heading->authorities();
449 # want only one exact match
450 if ($#{ $authorities } == 0) {
451 my $authority = MARC::Record->new_from_usmarc($authorities->[0]);
452 my $authid = $authority->field('001')->data();
453 next if defined $current_link and $current_link eq $authid;
455 $field->delete_subfield(code => '9') if defined $current_link;
456 $field->add_subfields('9', $authid);
457 $num_headings_changed++;
459 if (defined $current_link) {
460 $field->delete_subfield(code => '9');
461 $num_headings_changed++;
466 return $num_headings_changed;
473 $data = &GetBiblioData($biblionumber);
474 Returns information about the book with the given biblionumber.
475 C<&GetBiblioData> returns a reference-to-hash. The keys are the fields in
476 the C<biblio> and C<biblioitems> tables in the
478 In addition, C<$data-E<gt>{subject}> is the list of the book's
479 subjects, separated by C<" , "> (space, comma, space).
480 If there are multiple biblioitems with the given biblionumber, only
481 the first one is considered.
489 my $dbh = C4::Context->dbh;
491 # my $query = C4::Context->preference('item-level_itypes') ?
492 # " SELECT * , biblioitems.notes AS bnotes, biblio.notes
494 # LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
495 # WHERE biblio.biblionumber = ?
496 # AND biblioitems.biblionumber = biblio.biblionumber
499 my $query = " SELECT * , biblioitems.notes AS bnotes, itemtypes.notforloan as bi_notforloan, biblio.notes
501 LEFT JOIN biblioitems ON biblio.biblionumber = biblioitems.biblionumber
502 LEFT JOIN itemtypes ON biblioitems.itemtype = itemtypes.itemtype
503 WHERE biblio.biblionumber = ?
504 AND biblioitems.biblionumber = biblio.biblionumber ";
506 my $sth = $dbh->prepare($query);
507 $sth->execute($bibnum);
509 $data = $sth->fetchrow_hashref;
513 } # sub GetBiblioData
515 =head2 &GetBiblioItemData
519 $itemdata = &GetBiblioItemData($biblioitemnumber);
521 Looks up the biblioitem with the given biblioitemnumber. Returns a
522 reference-to-hash. The keys are the fields from the C<biblio>,
523 C<biblioitems>, and C<itemtypes> tables in the Koha database, except
524 that C<biblioitems.notes> is given as C<$itemdata-E<gt>{bnotes}>.
531 sub GetBiblioItemData {
532 my ($biblioitemnumber) = @_;
533 my $dbh = C4::Context->dbh;
534 my $query = "SELECT *,biblioitems.notes AS bnotes
535 FROM biblio LEFT JOIN biblioitems on biblio.biblionumber=biblioitems.biblionumber ";
536 unless(C4::Context->preference('item-level_itypes')) {
537 $query .= "LEFT JOIN itemtypes on biblioitems.itemtype=itemtypes.itemtype ";
539 $query .= " WHERE biblioitemnumber = ? ";
540 my $sth = $dbh->prepare($query);
542 $sth->execute($biblioitemnumber);
543 $data = $sth->fetchrow_hashref;
546 } # sub &GetBiblioItemData
548 =head2 GetBiblioItemByBiblioNumber
552 NOTE : This function has been copy/paste from C4/Biblio.pm from head before zebra integration.
558 sub GetBiblioItemByBiblioNumber {
559 my ($biblionumber) = @_;
560 my $dbh = C4::Context->dbh;
561 my $sth = $dbh->prepare("Select * FROM biblioitems WHERE biblionumber = ?");
565 $sth->execute($biblionumber);
567 while ( my $data = $sth->fetchrow_hashref ) {
568 push @results, $data;
575 =head2 GetBiblioFromItemNumber
579 $item = &GetBiblioFromItemNumber($itemnumber,$barcode);
581 Looks up the item with the given itemnumber. if undef, try the barcode.
583 C<&itemnodata> returns a reference-to-hash whose keys are the fields
584 from the C<biblio>, C<biblioitems>, and C<items> tables in the Koha
592 sub GetBiblioFromItemNumber {
593 my ( $itemnumber, $barcode ) = @_;
594 my $dbh = C4::Context->dbh;
597 $sth=$dbh->prepare( "SELECT * FROM items
598 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
599 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
600 WHERE items.itemnumber = ?") ;
601 $sth->execute($itemnumber);
603 $sth=$dbh->prepare( "SELECT * FROM items
604 LEFT JOIN biblio ON biblio.biblionumber = items.biblionumber
605 LEFT JOIN biblioitems ON biblioitems.biblioitemnumber = items.biblioitemnumber
606 WHERE items.barcode = ?") ;
607 $sth->execute($barcode);
609 my $data = $sth->fetchrow_hashref;
618 ( $count, @results ) = &GetBiblio($biblionumber);
625 my ($biblionumber) = @_;
626 my $dbh = C4::Context->dbh;
627 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber = ?");
630 $sth->execute($biblionumber);
631 while ( my $data = $sth->fetchrow_hashref ) {
632 $results[$count] = $data;
636 return ( $count, @results );
639 =head2 GetBiblioItemInfosOf
643 GetBiblioItemInfosOf(@biblioitemnumbers);
649 sub GetBiblioItemInfosOf {
650 my @biblioitemnumbers = @_;
653 SELECT biblioitemnumber,
657 WHERE biblioitemnumber IN (' . join( ',', @biblioitemnumbers ) . ')
659 return get_infos_of( $query, 'biblioitemnumber' );
662 =head1 FUNCTIONS FOR HANDLING MARC MANAGEMENT
664 =head2 GetMarcStructure
668 $res = GetMarcStructure($forlibrarian,$frameworkcode);
670 Returns a reference to a big hash of hash, with the Marc structure for the given frameworkcode
671 $forlibrarian :if set to 1, the MARC descriptions are the librarians ones, otherwise it's the public (OPAC) ones
672 $frameworkcode : the framework code to read
678 # cache for results of GetMarcStructure -- needed
680 our $marc_structure_cache;
682 sub GetMarcStructure {
683 my ( $forlibrarian, $frameworkcode ) = @_;
684 my $dbh=C4::Context->dbh;
685 $frameworkcode = "" unless $frameworkcode;
687 if (defined $marc_structure_cache and exists $marc_structure_cache->{$forlibrarian}->{$frameworkcode}) {
688 return $marc_structure_cache->{$forlibrarian}->{$frameworkcode};
692 my $libfield = ( $forlibrarian eq 1 ) ? 'liblibrarian' : 'libopac';
694 # check that framework exists
697 "SELECT COUNT(*) FROM marc_tag_structure WHERE frameworkcode=?");
698 $sth->execute($frameworkcode);
699 my ($total) = $sth->fetchrow;
700 $frameworkcode = "" unless ( $total > 0 );
703 "SELECT tagfield,liblibrarian,libopac,mandatory,repeatable
704 FROM marc_tag_structure
705 WHERE frameworkcode=?
708 $sth->execute($frameworkcode);
709 my ( $liblibrarian, $libopac, $tag, $res, $tab, $mandatory, $repeatable );
711 while ( ( $tag, $liblibrarian, $libopac, $mandatory, $repeatable ) =
714 $res->{$tag}->{lib} =
715 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
716 $res->{$tag}->{tab} = "";
717 $res->{$tag}->{mandatory} = $mandatory;
718 $res->{$tag}->{repeatable} = $repeatable;
723 "SELECT tagfield,tagsubfield,liblibrarian,libopac,tab,mandatory,repeatable,authorised_value,authtypecode,value_builder,kohafield,seealso,hidden,isurl,link,defaultvalue
724 FROM marc_subfield_structure
725 WHERE frameworkcode=?
726 ORDER BY tagfield,tagsubfield
730 $sth->execute($frameworkcode);
733 my $authorised_value;
745 $tag, $subfield, $liblibrarian,
747 $mandatory, $repeatable, $authorised_value,
748 $authtypecode, $value_builder, $kohafield,
749 $seealso, $hidden, $isurl,
755 $res->{$tag}->{$subfield}->{lib} =
756 ( $forlibrarian or !$libopac ) ? $liblibrarian : $libopac;
757 $res->{$tag}->{$subfield}->{tab} = $tab;
758 $res->{$tag}->{$subfield}->{mandatory} = $mandatory;
759 $res->{$tag}->{$subfield}->{repeatable} = $repeatable;
760 $res->{$tag}->{$subfield}->{authorised_value} = $authorised_value;
761 $res->{$tag}->{$subfield}->{authtypecode} = $authtypecode;
762 $res->{$tag}->{$subfield}->{value_builder} = $value_builder;
763 $res->{$tag}->{$subfield}->{kohafield} = $kohafield;
764 $res->{$tag}->{$subfield}->{seealso} = $seealso;
765 $res->{$tag}->{$subfield}->{hidden} = $hidden;
766 $res->{$tag}->{$subfield}->{isurl} = $isurl;
767 $res->{$tag}->{$subfield}->{'link'} = $link;
768 $res->{$tag}->{$subfield}->{defaultvalue} = $defaultvalue;
771 $marc_structure_cache->{$forlibrarian}->{$frameworkcode} = $res;
776 =head2 GetUsedMarcStructure
778 the same function as GetMarcStructure expcet it just take field
779 in tab 0-9. (used field)
781 my $results = GetUsedMarcStructure($frameworkcode);
783 L<$results> is a ref to an array which each case containts a ref
784 to a hash which each keys is the columns from marc_subfield_structure
786 L<$frameworkcode> is the framework code.
790 sub GetUsedMarcStructure($){
791 my $frameworkcode = shift || '';
792 my $dbh = C4::Context->dbh;
795 FROM marc_subfield_structure
797 AND frameworkcode = ?
800 my $sth = $dbh->prepare($query);
801 $sth->execute($frameworkcode);
802 while (my $row = $sth->fetchrow_hashref){
808 =head2 GetMarcFromKohaField
812 ($MARCfield,$MARCsubfield)=GetMarcFromKohaField($kohafield,$frameworkcode);
813 Returns the MARC fields & subfields mapped to the koha field
814 for the given frameworkcode
820 sub GetMarcFromKohaField {
821 my ( $kohafield, $frameworkcode ) = @_;
822 return 0, 0 unless $kohafield and defined $frameworkcode;
823 my $relations = C4::Context->marcfromkohafield;
825 $relations->{$frameworkcode}->{$kohafield}->[0],
826 $relations->{$frameworkcode}->{$kohafield}->[1]
834 my $record = GetMarcBiblio($biblionumber);
838 Returns MARC::Record representing bib identified by
839 C<$biblionumber>. If no bib exists, returns undef.
840 The MARC record contains both biblio & item data.
845 my $biblionumber = shift;
846 my $dbh = C4::Context->dbh;
848 $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
849 $sth->execute($biblionumber);
850 my $row = $sth->fetchrow_hashref;
851 my $marcxml = StripNonXmlChars($row->{'marcxml'});
852 MARC::File::XML->default_record_format(C4::Context->preference('marcflavour'));
853 my $record = MARC::Record->new();
855 $record = eval {MARC::Record::new_from_xml( $marcxml, "utf8", C4::Context->preference('marcflavour'))};
856 if ($@) {warn " problem with :$biblionumber : $@ \n$marcxml";}
857 # $record = MARC::Record::new_from_usmarc( $marc) if $marc;
868 my $marcxml = GetXmlBiblio($biblionumber);
870 Returns biblioitems.marcxml of the biblionumber passed in parameter.
871 The XML contains both biblio & item datas
878 my ( $biblionumber ) = @_;
879 my $dbh = C4::Context->dbh;
881 $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
882 $sth->execute($biblionumber);
883 my ($marcxml) = $sth->fetchrow;
887 =head2 GetAuthorisedValueDesc
891 my $subfieldvalue =get_authorised_value_desc(
892 $tag, $subf[$i][0],$subf[$i][1], '', $taglib, $category);
893 Retrieve the complete description for a given authorised value.
895 Now takes $category and $value pair too.
896 my $auth_value_desc =GetAuthorisedValueDesc(
897 '','', 'DVD' ,'','','CCODE');
903 sub GetAuthorisedValueDesc {
904 my ( $tag, $subfield, $value, $framework, $tagslib, $category ) = @_;
905 my $dbh = C4::Context->dbh;
909 return $value unless defined $tagslib->{$tag}->{$subfield}->{'authorised_value'};
912 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "branches" ) {
913 return C4::Branch::GetBranchName($value);
917 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq "itemtypes" ) {
918 return getitemtypeinfo($value)->{description};
921 #---- "true" authorized value
922 $category = $tagslib->{$tag}->{$subfield}->{'authorised_value'}
925 if ( $category ne "" ) {
928 "SELECT lib FROM authorised_values WHERE category = ? AND authorised_value = ?"
930 $sth->execute( $category, $value );
931 my $data = $sth->fetchrow_hashref;
932 return $data->{'lib'};
935 return $value; # if nothing is found return the original value
943 $marcnotesarray = GetMarcNotes( $record, $marcflavour );
944 Get all notes from the MARC record and returns them in an array.
945 The note are stored in differents places depending on MARC flavour
952 my ( $record, $marcflavour ) = @_;
954 if ( $marcflavour eq "MARC21" ) {
957 else { # assume unimarc if not marc21
964 foreach my $field ( $record->field($scope) ) {
965 my $value = $field->as_string();
967 $marcnote = { marcnote => $note, };
968 push @marcnotes, $marcnote;
971 if ( $note ne $value ) {
972 $note = $note . " " . $value;
977 $marcnote = { marcnote => $note };
978 push @marcnotes, $marcnote; #load last tag into array
983 =head2 GetMarcSubjects
987 $marcsubjcts = GetMarcSubjects($record,$marcflavour);
988 Get all subjects from the MARC record and returns them in an array.
989 The subjects are stored in differents places depending on MARC flavour
995 sub GetMarcSubjects {
996 my ( $record, $marcflavour ) = @_;
997 my ( $mintag, $maxtag );
998 if ( $marcflavour eq "MARC21" ) {
1002 else { # assume unimarc if not marc21
1012 foreach my $field ( $record->field('6..' )) {
1013 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1015 my @subfields = $field->subfields();
1018 # if there is an authority link, build the link with an= subfield9
1019 my $subfield9 = $field->subfield('9');
1020 for my $subject_subfield (@subfields ) {
1021 # don't load unimarc subfields 3,4,5
1022 next if (($marcflavour eq "UNIMARC") and ($subject_subfield->[0] =~ /3|4|5/ ) );
1023 my $code = $subject_subfield->[0];
1024 my $value = $subject_subfield->[1];
1025 my $linkvalue = $value;
1026 $linkvalue =~ s/(\(|\))//g;
1027 my $operator = " and " unless $counter==0;
1029 @link_loop = ({'limit' => 'an' ,link => "$subfield9" });
1031 push @link_loop, {'limit' => 'su', link => $linkvalue, operator => $operator };
1033 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
1035 my @this_link_loop = @link_loop;
1036 push @subfields_loop, {code => $code, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($subject_subfield->[0] eq 9 );
1040 push @marcsubjects, { MARCSUBJECT_SUBFIELDS_LOOP => \@subfields_loop };
1043 return \@marcsubjects;
1044 } #end getMARCsubjects
1046 =head2 GetMarcAuthors
1050 authors = GetMarcAuthors($record,$marcflavour);
1051 Get all authors from the MARC record and returns them in an array.
1052 The authors are stored in differents places depending on MARC flavour
1058 sub GetMarcAuthors {
1059 my ( $record, $marcflavour ) = @_;
1060 my ( $mintag, $maxtag );
1061 # tagslib useful for UNIMARC author reponsabilities
1062 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.
1063 if ( $marcflavour eq "MARC21" ) {
1067 elsif ( $marcflavour eq "UNIMARC" ) { # assume unimarc if not marc21
1076 foreach my $field ( $record->fields ) {
1077 next unless $field->tag() >= $mintag && $field->tag() <= $maxtag;
1080 my @subfields = $field->subfields();
1082 # if there is an authority link, build the link with Koha-Auth-Number: subfield9
1083 my $subfield9 = $field->subfield('9');
1084 for my $authors_subfield (@subfields) {
1085 # don't load unimarc subfields 3, 5
1086 next if ($marcflavour eq 'UNIMARC' and ($authors_subfield->[0] =~ /3|5/ ) );
1087 my $subfieldcode = $authors_subfield->[0];
1088 my $value = $authors_subfield->[1];
1089 my $linkvalue = $value;
1090 $linkvalue =~ s/(\(|\))//g;
1091 my $operator = " and " unless $count_auth==0;
1092 # if we have an authority link, use that as the link, otherwise use standard searching
1094 @link_loop = ({'limit' => 'an' ,link => "$subfield9" });
1097 # reset $linkvalue if UNIMARC author responsibility
1098 if ( $marcflavour eq 'UNIMARC' and ($authors_subfield->[0] eq "4")) {
1099 $linkvalue = "(".GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ).")";
1101 push @link_loop, {'limit' => 'au', link => $linkvalue, operator => $operator };
1103 $value = GetAuthorisedValueDesc( $field->tag(), $authors_subfield->[0], $authors_subfield->[1], '', $tagslib ) if ( $marcflavour eq 'UNIMARC' and ($authors_subfield->[0] =~/4/));
1104 my @this_link_loop = @link_loop;
1105 my $separator = C4::Context->preference("authoritysep") unless $count_auth==0;
1106 push @subfields_loop, {code => $subfieldcode, value => $value, link_loop => \@this_link_loop, separator => $separator} unless ($authors_subfield->[0] eq '9' );
1109 push @marcauthors, { MARCAUTHOR_SUBFIELDS_LOOP => \@subfields_loop };
1111 return \@marcauthors;
1118 $marcurls = GetMarcUrls($record,$marcflavour);
1119 Returns arrayref of URLs from MARC data, suitable to pass to tmpl loop.
1120 Assumes web resources (not uncommon in MARC21 to omit resource type ind)
1127 my ($record, $marcflavour) = @_;
1129 for my $field ($record->field('856')) {
1131 my $url = $field->subfield('u');
1133 for my $note ( $field->subfield('z')) {
1134 push @notes , {note => $note};
1136 if($marcflavour eq 'MARC21') {
1137 my $s3 = $field->subfield('3');
1138 my $link = $field->subfield('y');
1139 unless($url =~ /^\w+:/) {
1140 if($field->indicator(1) eq '7') {
1141 $url = $field->subfield('2') . "://" . $url;
1142 } elsif ($field->indicator(1) eq '1') {
1143 $url = 'ftp://' . $url;
1145 # properly, this should be if ind1=4,
1146 # however we will assume http protocol since we're building a link.
1147 $url = 'http://' . $url;
1150 # TODO handle ind 2 (relationship)
1151 $marcurl = { MARCURL => $url,
1154 $marcurl->{'linktext'} = $link || $s3 || C4::Context->preference('URLLinkText') || $url ;;
1155 $marcurl->{'part'} = $s3 if($link);
1156 $marcurl->{'toc'} = 1 if($s3 =~ /^[Tt]able/) ;
1158 $marcurl->{'linktext'} = $field->subfield('z') || C4::Context->preference('URLLinkText') || $url;
1159 $marcurl->{'MARCURL'} = $url ;
1161 push @marcurls, $marcurl;
1166 =head2 GetMarcSeries
1170 $marcseriesarray = GetMarcSeries($record,$marcflavour);
1171 Get all series from the MARC record and returns them in an array.
1172 The series are stored in differents places depending on MARC flavour
1179 my ($record, $marcflavour) = @_;
1180 my ($mintag, $maxtag);
1181 if ($marcflavour eq "MARC21") {
1184 } else { # assume unimarc if not marc21
1194 foreach my $field ($record->field('440'), $record->field('490')) {
1196 #my $value = $field->subfield('a');
1197 #$marcsubjct = {MARCSUBJCT => $value,};
1198 my @subfields = $field->subfields();
1199 #warn "subfields:".join " ", @$subfields;
1202 for my $series_subfield (@subfields) {
1204 undef $volume_number;
1205 # see if this is an instance of a volume
1206 if ($series_subfield->[0] eq 'v') {
1210 my $code = $series_subfield->[0];
1211 my $value = $series_subfield->[1];
1212 my $linkvalue = $value;
1213 $linkvalue =~ s/(\(|\))//g;
1214 my $operator = " and " unless $counter==0;
1215 push @link_loop, {link => $linkvalue, operator => $operator };
1216 my $separator = C4::Context->preference("authoritysep") unless $counter==0;
1217 if ($volume_number) {
1218 push @subfields_loop, {volumenum => $value};
1221 push @subfields_loop, {code => $code, value => $value, link_loop => \@link_loop, separator => $separator, volumenum => $volume_number};
1225 push @marcseries, { MARCSERIES_SUBFIELDS_LOOP => \@subfields_loop };
1226 #$marcsubjct = {MARCSUBJCT => $field->as_string(),};
1227 #push @marcsubjcts, $marcsubjct;
1231 my $marcseriessarray=\@marcseries;
1232 return $marcseriessarray;
1233 } #end getMARCseriess
1235 =head2 GetFrameworkCode
1239 $frameworkcode = GetFrameworkCode( $biblionumber )
1245 sub GetFrameworkCode {
1246 my ( $biblionumber ) = @_;
1247 my $dbh = C4::Context->dbh;
1248 my $sth = $dbh->prepare("SELECT frameworkcode FROM biblio WHERE biblionumber=?");
1249 $sth->execute($biblionumber);
1250 my ($frameworkcode) = $sth->fetchrow;
1251 return $frameworkcode;
1254 =head2 GetPublisherNameFromIsbn
1256 $name = GetPublishercodeFromIsbn($isbn);
1263 sub GetPublisherNameFromIsbn($){
1265 $isbn =~ s/[- _]//g;
1267 my @codes = (split '-', DisplayISBN($isbn));
1268 my $code = $codes[0].$codes[1].$codes[2];
1269 my $dbh = C4::Context->dbh;
1271 SELECT distinct publishercode
1274 AND publishercode IS NOT NULL
1277 my $sth = $dbh->prepare($query);
1278 $sth->execute("$code%");
1279 my $name = $sth->fetchrow;
1280 return $name if length $name;
1284 =head2 TransformKohaToMarc
1288 $record = TransformKohaToMarc( $hash )
1289 This function builds partial MARC::Record from a hash
1290 Hash entries can be from biblio or biblioitems.
1291 This function is called in acquisition module, to create a basic catalogue entry from user entry
1297 sub TransformKohaToMarc {
1300 my $dbh = C4::Context->dbh;
1303 "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
1305 my $record = MARC::Record->new();
1306 foreach (keys %{$hash}) {
1307 &TransformKohaToMarcOneField( $sth, $record, $_,
1313 =head2 TransformKohaToMarcOneField
1317 $record = TransformKohaToMarcOneField( $sth, $record, $kohafieldname, $value, $frameworkcode );
1323 sub TransformKohaToMarcOneField {
1324 my ( $sth, $record, $kohafieldname, $value, $frameworkcode ) = @_;
1325 $frameworkcode='' unless $frameworkcode;
1329 if ( !defined $sth ) {
1330 my $dbh = C4::Context->dbh;
1331 $sth = $dbh->prepare(
1332 "SELECT tagfield,tagsubfield FROM marc_subfield_structure WHERE frameworkcode=? AND kohafield=?"
1335 $sth->execute( $frameworkcode, $kohafieldname );
1336 if ( ( $tagfield, $tagsubfield ) = $sth->fetchrow ) {
1337 my $tag = $record->field($tagfield);
1339 $tag->update( $tagsubfield => $value );
1340 $record->delete_field($tag);
1341 $record->insert_fields_ordered($tag);
1344 $record->add_fields( $tagfield, " ", " ", $tagsubfield => $value );
1350 =head2 TransformHtmlToXml
1354 $xml = TransformHtmlToXml( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type )
1356 $auth_type contains :
1357 - nothing : rebuild a biblio, un UNIMARC the encoding is in 100$a pos 26/27
1358 - UNIMARCAUTH : rebuild an authority. In UNIMARC, the encoding is in 100$a pos 13/14
1359 - ITEM : rebuild an item : in UNIMARC, 100$a, it's in the biblio ! (otherwise, we would get 2 100 fields !)
1365 sub TransformHtmlToXml {
1366 my ( $tags, $subfields, $values, $indicator, $ind_tag, $auth_type ) = @_;
1367 my $xml = MARC::File::XML::header('UTF-8');
1368 $auth_type = C4::Context->preference('marcflavour') unless $auth_type;
1369 MARC::File::XML->default_record_format($auth_type);
1370 # in UNIMARC, field 100 contains the encoding
1371 # check that there is one, otherwise the
1372 # MARC::Record->new_from_xml will fail (and Koha will die)
1373 my $unimarc_and_100_exist=0;
1374 $unimarc_and_100_exist=1 if $auth_type eq 'ITEM'; # if we rebuild an item, no need of a 100 field
1379 for ( my $i = 0 ; $i < @$tags ; $i++ ) {
1380 if (C4::Context->preference('marcflavour') eq 'UNIMARC' and @$tags[$i] eq "100" and @$subfields[$i] eq "a") {
1381 # if we have a 100 field and it's values are not correct, skip them.
1382 # if we don't have any valid 100 field, we will create a default one at the end
1383 my $enc = substr( @$values[$i], 26, 2 );
1384 if ($enc eq '01' or $enc eq '50' or $enc eq '03') {
1385 $unimarc_and_100_exist=1;
1390 @$values[$i] =~ s/&/&/g;
1391 @$values[$i] =~ s/</</g;
1392 @$values[$i] =~ s/>/>/g;
1393 @$values[$i] =~ s/"/"/g;
1394 @$values[$i] =~ s/'/'/g;
1395 # if ( !utf8::is_utf8( @$values[$i] ) ) {
1396 # utf8::decode( @$values[$i] );
1398 if ( ( @$tags[$i] ne $prevtag ) ) {
1399 $j++ unless ( @$tags[$i] eq "" );
1401 $xml .= "</datafield>\n";
1402 if ( ( @$tags[$i] && @$tags[$i] > 10 )
1403 && ( @$values[$i] ne "" ) )
1405 my $ind1 = substr( @$indicator[$j], 0, 1 );
1407 if ( @$indicator[$j] ) {
1408 $ind2 = substr( @$indicator[$j], 1, 1 );
1411 warn "Indicator in @$tags[$i] is empty";
1415 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1417 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1425 if ( @$values[$i] ne "" ) {
1428 if ( @$tags[$i] eq "000" ) {
1429 $xml .= "<leader>@$values[$i]</leader>\n";
1432 # rest of the fixed fields
1434 elsif ( @$tags[$i] < 10 ) {
1436 "<controlfield tag=\"@$tags[$i]\">@$values[$i]</controlfield>\n";
1440 my $ind1 = substr( @$indicator[$j], 0, 1 );
1441 my $ind2 = substr( @$indicator[$j], 1, 1 );
1443 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1445 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1451 else { # @$tags[$i] eq $prevtag
1452 if ( @$values[$i] eq "" ) {
1456 my $ind1 = substr( @$indicator[$j], 0, 1 );
1457 my $ind2 = substr( @$indicator[$j], 1, 1 );
1459 "<datafield tag=\"@$tags[$i]\" ind1=\"$ind1\" ind2=\"$ind2\">\n";
1463 "<subfield code=\"@$subfields[$i]\">@$values[$i]</subfield>\n";
1466 $prevtag = @$tags[$i];
1468 $xml .= "</datafield>\n" if @$tags > 0;
1469 if (C4::Context->preference('marcflavour') eq 'UNIMARC' and !$unimarc_and_100_exist) {
1470 # warn "SETTING 100 for $auth_type";
1471 use POSIX qw(strftime);
1472 my $string = strftime( "%Y%m%d", localtime(time) );
1473 # set 50 to position 26 is biblios, 13 if authorities
1475 $pos=13 if $auth_type eq 'UNIMARCAUTH';
1476 $string = sprintf( "%-*s", 35, $string );
1477 substr( $string, $pos , 6, "50" );
1478 $xml .= "<datafield tag=\"100\" ind1=\"\" ind2=\"\">\n";
1479 $xml .= "<subfield code=\"a\">$string</subfield>\n";
1480 $xml .= "</datafield>\n";
1482 $xml .= MARC::File::XML::footer();
1486 =head2 TransformHtmlToMarc
1488 L<$record> = TransformHtmlToMarc(L<$params>,L<$cgi>)
1489 L<$params> is a ref to an array as below:
1491 'tag_010_indicator1_531951' ,
1492 'tag_010_indicator2_531951' ,
1493 'tag_010_code_a_531951_145735' ,
1494 'tag_010_subfield_a_531951_145735' ,
1495 'tag_200_indicator1_873510' ,
1496 'tag_200_indicator2_873510' ,
1497 'tag_200_code_a_873510_673465' ,
1498 'tag_200_subfield_a_873510_673465' ,
1499 'tag_200_code_b_873510_704318' ,
1500 'tag_200_subfield_b_873510_704318' ,
1501 'tag_200_code_e_873510_280822' ,
1502 'tag_200_subfield_e_873510_280822' ,
1503 'tag_200_code_f_873510_110730' ,
1504 'tag_200_subfield_f_873510_110730' ,
1506 L<$cgi> is the CGI object which containts the value.
1507 L<$record> is the MARC::Record object.
1511 sub TransformHtmlToMarc {
1515 # explicitly turn on the UTF-8 flag for all
1516 # 'tag_' parameters to avoid incorrect character
1517 # conversion later on
1518 my $cgi_params = $cgi->Vars;
1519 foreach my $param_name (keys %$cgi_params) {
1520 if ($param_name =~ /^tag_/) {
1521 my $param_value = $cgi_params->{$param_name};
1522 if (utf8::decode($param_value)) {
1523 $cgi_params->{$param_name} = $param_value;
1525 # FIXME - need to do something if string is not valid UTF-8
1529 # creating a new record
1530 my $record = MARC::Record->new();
1533 while ($params->[$i]){ # browse all CGI params
1534 my $param = $params->[$i];
1536 # if we are on biblionumber, store it in the MARC::Record (it may not be in the edited fields)
1537 if ($param eq 'biblionumber') {
1538 my ( $biblionumbertagfield, $biblionumbertagsubfield ) =
1539 &GetMarcFromKohaField( "biblio.biblionumber", '' );
1540 if ($biblionumbertagfield < 10) {
1541 $newfield = MARC::Field->new(
1542 $biblionumbertagfield,
1543 $cgi->param($param),
1546 $newfield = MARC::Field->new(
1547 $biblionumbertagfield,
1550 "$biblionumbertagsubfield" => $cgi->param($param),
1553 push @fields,$newfield if($newfield);
1555 elsif ($param =~ /^tag_(\d*)_indicator1_/){ # new field start when having 'input name="..._indicator1_..."
1558 my $ind1 = substr($cgi->param($param),0,1);
1559 my $ind2 = substr($cgi->param($params->[$i+1]),0,1);
1563 if($tag < 10){ # no code for theses fields
1564 # in MARC editor, 000 contains the leader.
1565 if ($tag eq '000' ) {
1566 $record->leader($cgi->param($params->[$j+1])) if length($cgi->param($params->[$j+1]))==24;
1567 # between 001 and 009 (included)
1568 } elsif ($cgi->param($params->[$j+1]) ne '') {
1569 $newfield = MARC::Field->new(
1571 $cgi->param($params->[$j+1]),
1574 # > 009, deal with subfields
1576 while(defined $params->[$j] && $params->[$j] =~ /_code_/){ # browse all it's subfield
1577 my $inner_param = $params->[$j];
1579 if($cgi->param($params->[$j+1]) ne ''){ # only if there is a value (code => value)
1580 $newfield->add_subfields(
1581 $cgi->param($inner_param) => $cgi->param($params->[$j+1])
1585 if ( $cgi->param($params->[$j+1]) ne '' ) { # creating only if there is a value (code => value)
1586 $newfield = MARC::Field->new(
1590 $cgi->param($inner_param) => $cgi->param($params->[$j+1]),
1597 push @fields,$newfield if($newfield);
1602 $record->append_fields(@fields);
1606 # cache inverted MARC field map
1607 our $inverted_field_map;
1609 =head2 TransformMarcToKoha
1613 $result = TransformMarcToKoha( $dbh, $record, $frameworkcode )
1617 Extract data from a MARC bib record into a hashref representing
1618 Koha biblio, biblioitems, and items fields.
1621 sub TransformMarcToKoha {
1622 my ( $dbh, $record, $frameworkcode, $limit_table ) = @_;
1625 $limit_table=$limit_table||0;
1626 $frameworkcode = '' unless defined $frameworkcode;
1628 unless (defined $inverted_field_map) {
1629 $inverted_field_map = _get_inverted_marc_field_map();
1633 if ( defined $limit_table && $limit_table eq 'items') {
1634 $tables{'items'} = 1;
1636 $tables{'items'} = 1;
1637 $tables{'biblio'} = 1;
1638 $tables{'biblioitems'} = 1;
1641 # traverse through record
1642 MARCFIELD: foreach my $field ($record->fields()) {
1643 my $tag = $field->tag();
1644 next MARCFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag};
1645 if ($field->is_control_field()) {
1646 my $kohafields = $inverted_field_map->{$frameworkcode}->{$tag}->{list};
1647 ENTRY: foreach my $entry (@{ $kohafields }) {
1648 my ($subfield, $table, $column) = @{ $entry };
1649 next ENTRY unless exists $tables{$table};
1650 my $key = _disambiguate($table, $column);
1651 if ($result->{$key}) {
1652 unless (($key eq "biblionumber" or $key eq "biblioitemnumber") and ($field->data() eq "")) {
1653 $result->{$key} .= " | " . $field->data();
1656 $result->{$key} = $field->data();
1660 # deal with subfields
1661 MARCSUBFIELD: foreach my $sf ($field->subfields()) {
1662 my $code = $sf->[0];
1663 next MARCSUBFIELD unless exists $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code};
1664 my $value = $sf->[1];
1665 SFENTRY: foreach my $entry (@{ $inverted_field_map->{$frameworkcode}->{$tag}->{sfs}->{$code} }) {
1666 my ($table, $column) = @{ $entry };
1667 next SFENTRY unless exists $tables{$table};
1668 my $key = _disambiguate($table, $column);
1669 if ($result->{$key}) {
1670 unless (($key eq "biblionumber" or $key eq "biblioitemnumber") and ($value eq "")) {
1671 $result->{$key} .= " | " . $value;
1674 $result->{$key} = $value;
1681 # modify copyrightdate to keep only the 1st year found
1682 if (exists $result->{'copyrightdate'}) {
1683 my $temp = $result->{'copyrightdate'};
1684 $temp =~ m/c(\d\d\d\d)/;
1685 if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
1686 $result->{'copyrightdate'} = $1;
1688 else { # if no cYYYY, get the 1st date.
1689 $temp =~ m/(\d\d\d\d)/;
1690 $result->{'copyrightdate'} = $1;
1694 # modify publicationyear to keep only the 1st year found
1695 if (exists $result->{'publicationyear'}) {
1696 my $temp = $result->{'publicationyear'};
1697 if ( $temp =~ m/c(\d\d\d\d)/ and $1 > 0 ) { # search cYYYY first
1698 $result->{'publicationyear'} = $1;
1700 else { # if no cYYYY, get the 1st date.
1701 $temp =~ m/(\d\d\d\d)/;
1702 $result->{'publicationyear'} = $1;
1709 sub _get_inverted_marc_field_map {
1711 my $relations = C4::Context->marcfromkohafield;
1713 foreach my $frameworkcode (keys %{ $relations }) {
1714 foreach my $kohafield (keys %{ $relations->{$frameworkcode} }) {
1715 next unless @{ $relations->{$frameworkcode}->{$kohafield} }; # not all columns are mapped to MARC tag & subfield
1716 my $tag = $relations->{$frameworkcode}->{$kohafield}->[0];
1717 my $subfield = $relations->{$frameworkcode}->{$kohafield}->[1];
1718 my ($table, $column) = split /[.]/, $kohafield, 2;
1719 push @{ $field_map->{$frameworkcode}->{$tag}->{list} }, [ $subfield, $table, $column ];
1720 push @{ $field_map->{$frameworkcode}->{$tag}->{sfs}->{$subfield} }, [ $table, $column ];
1726 =head2 _disambiguate
1730 $newkey = _disambiguate($table, $field);
1732 This is a temporary hack to distinguish between the
1733 following sets of columns when using TransformMarcToKoha.
1735 items.cn_source & biblioitems.cn_source
1736 items.cn_sort & biblioitems.cn_sort
1738 Columns that are currently NOT distinguished (FIXME
1739 due to lack of time to fully test) are:
1741 biblio.notes and biblioitems.notes
1746 FIXME - this is necessary because prefixing each column
1747 name with the table name would require changing lots
1748 of code and templates, and exposing more of the DB
1749 structure than is good to the UI templates, particularly
1750 since biblio and bibloitems may well merge in a future
1751 version. In the future, it would also be good to
1752 separate DB access and UI presentation field names
1760 my ($table, $column) = @_;
1761 if ($column eq "cn_sort" or $column eq "cn_source") {
1762 return $table . '.' . $column;
1769 =head2 get_koha_field_from_marc
1773 $result->{_disambiguate($table, $field)} = get_koha_field_from_marc($table,$field,$record,$frameworkcode);
1775 Internal function to map data from the MARC record to a specific non-MARC field.
1776 FIXME: this is meant to replace TransformMarcToKohaOneField after more testing.
1782 sub get_koha_field_from_marc {
1783 my ($koha_table,$koha_column,$record,$frameworkcode) = @_;
1784 my ( $tagfield, $subfield ) = GetMarcFromKohaField( $koha_table.'.'.$koha_column, $frameworkcode );
1786 foreach my $field ( $record->field($tagfield) ) {
1787 if ( $field->tag() < 10 ) {
1789 $kohafield .= " | " . $field->data();
1792 $kohafield = $field->data();
1796 if ( $field->subfields ) {
1797 my @subfields = $field->subfields();
1798 foreach my $subfieldcount ( 0 .. $#subfields ) {
1799 if ( $subfields[$subfieldcount][0] eq $subfield ) {
1802 " | " . $subfields[$subfieldcount][1];
1806 $subfields[$subfieldcount][1];
1817 =head2 TransformMarcToKohaOneField
1821 $result = TransformMarcToKohaOneField( $kohatable, $kohafield, $record, $result, $frameworkcode )
1827 sub TransformMarcToKohaOneField {
1829 # FIXME ? if a field has a repeatable subfield that is used in old-db,
1830 # only the 1st will be retrieved...
1831 my ( $kohatable, $kohafield, $record, $result, $frameworkcode ) = @_;
1833 my ( $tagfield, $subfield ) =
1834 GetMarcFromKohaField( $kohatable . "." . $kohafield,
1836 foreach my $field ( $record->field($tagfield) ) {
1837 if ( $field->tag() < 10 ) {
1838 if ( $result->{$kohafield} ) {
1839 $result->{$kohafield} .= " | " . $field->data();
1842 $result->{$kohafield} = $field->data();
1846 if ( $field->subfields ) {
1847 my @subfields = $field->subfields();
1848 foreach my $subfieldcount ( 0 .. $#subfields ) {
1849 if ( $subfields[$subfieldcount][0] eq $subfield ) {
1850 if ( $result->{$kohafield} ) {
1851 $result->{$kohafield} .=
1852 " | " . $subfields[$subfieldcount][1];
1855 $result->{$kohafield} =
1856 $subfields[$subfieldcount][1];
1866 =head1 OTHER FUNCTIONS
1869 =head2 PrepareItemrecordDisplay
1873 PrepareItemrecordDisplay($itemrecord,$bibnum,$itemumber);
1875 Returns a hash with all the fields for Display a given item data in a template
1881 sub PrepareItemrecordDisplay {
1883 my ( $bibnum, $itemnum, $defaultvalues ) = @_;
1885 my $dbh = C4::Context->dbh;
1886 my $frameworkcode = &GetFrameworkCode( $bibnum );
1887 my ( $itemtagfield, $itemtagsubfield ) =
1888 &GetMarcFromKohaField( "items.itemnumber", $frameworkcode );
1889 my $tagslib = &GetMarcStructure( 1, $frameworkcode );
1890 my $itemrecord = C4::Items::GetMarcItem( $bibnum, $itemnum) if ($itemnum);
1892 my $authorised_values_sth =
1894 "SELECT authorised_value,lib FROM authorised_values WHERE category=? ORDER BY lib"
1896 foreach my $tag ( sort keys %{$tagslib} ) {
1897 my $previous_tag = '';
1899 # loop through each subfield
1901 foreach my $subfield ( sort keys %{ $tagslib->{$tag} } ) {
1902 next if ( subfield_is_koha_internal_p($subfield) );
1903 next if ( $tagslib->{$tag}->{$subfield}->{'tab'} ne "10" );
1905 $subfield_data{tag} = $tag;
1906 $subfield_data{subfield} = $subfield;
1907 $subfield_data{countsubfield} = $cntsubf++;
1908 $subfield_data{kohafield} =
1909 $tagslib->{$tag}->{$subfield}->{'kohafield'};
1911 # $subfield_data{marc_lib}=$tagslib->{$tag}->{$subfield}->{lib};
1912 $subfield_data{marc_lib} = $tagslib->{$tag}->{$subfield}->{lib};
1913 $subfield_data{mandatory} =
1914 $tagslib->{$tag}->{$subfield}->{mandatory};
1915 $subfield_data{repeatable} =
1916 $tagslib->{$tag}->{$subfield}->{repeatable};
1917 $subfield_data{hidden} = "display:none"
1918 if $tagslib->{$tag}->{$subfield}->{hidden};
1920 ( $x, $value ) = _find_value( $tag, $subfield, $itemrecord )
1922 $value =~ s/"/"/g;
1924 # search for itemcallnumber if applicable
1925 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
1926 'items.itemcallnumber'
1927 && C4::Context->preference('itemcallnumber') )
1930 substr( C4::Context->preference('itemcallnumber'), 0, 3 );
1932 substr( C4::Context->preference('itemcallnumber'), 3, 1 );
1933 my $temp = $itemrecord->field($CNtag) if ($itemrecord);
1935 $value = $temp->subfield($CNsubfield);
1938 if ( $tagslib->{$tag}->{$subfield}->{kohafield} eq
1939 'items.itemcallnumber'
1940 && $defaultvalues->{'callnumber'} )
1942 my $temp = $itemrecord->field($subfield) if ($itemrecord);
1944 $value = $defaultvalues->{'callnumber'};
1947 if ( ($tagslib->{$tag}->{$subfield}->{kohafield} eq
1948 'items.holdingbranch' ||
1949 $tagslib->{$tag}->{$subfield}->{kohafield} eq
1951 && $defaultvalues->{'branchcode'} )
1953 my $temp = $itemrecord->field($subfield) if ($itemrecord);
1955 $value = $defaultvalues->{branchcode};
1958 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
1959 my @authorised_values;
1962 # builds list, depending on authorised value...
1964 if ( $tagslib->{$tag}->{$subfield}->{'authorised_value'} eq
1967 if ( ( C4::Context->preference("IndependantBranches") )
1968 && ( C4::Context->userenv->{flags} != 1 ) )
1972 "SELECT branchcode,branchname FROM branches WHERE branchcode = ? ORDER BY branchname"
1974 $sth->execute( C4::Context->userenv->{branch} );
1975 push @authorised_values, ""
1977 $tagslib->{$tag}->{$subfield}->{mandatory} );
1978 while ( my ( $branchcode, $branchname ) =
1979 $sth->fetchrow_array )
1981 push @authorised_values, $branchcode;
1982 $authorised_lib{$branchcode} = $branchname;
1988 "SELECT branchcode,branchname FROM branches ORDER BY branchname"
1991 push @authorised_values, ""
1993 $tagslib->{$tag}->{$subfield}->{mandatory} );
1994 while ( my ( $branchcode, $branchname ) =
1995 $sth->fetchrow_array )
1997 push @authorised_values, $branchcode;
1998 $authorised_lib{$branchcode} = $branchname;
2004 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq
2009 "SELECT itemtype,description FROM itemtypes ORDER BY description"
2012 push @authorised_values, ""
2013 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2014 while ( my ( $itemtype, $description ) =
2015 $sth->fetchrow_array )
2017 push @authorised_values, $itemtype;
2018 $authorised_lib{$itemtype} = $description;
2021 #---- "true" authorised value
2024 $authorised_values_sth->execute(
2025 $tagslib->{$tag}->{$subfield}->{authorised_value} );
2026 push @authorised_values, ""
2027 unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
2028 while ( my ( $value, $lib ) =
2029 $authorised_values_sth->fetchrow_array )
2031 push @authorised_values, $value;
2032 $authorised_lib{$value} = $lib;
2035 $subfield_data{marc_value} = CGI::scrolling_list(
2036 -name => 'field_value',
2037 -values => \@authorised_values,
2038 -default => "$value",
2039 -labels => \%authorised_lib,
2046 $subfield_data{marc_value} =
2047 "<input type=\"text\" name=\"field_value\" value=\"$value\" size=\"50\" maxlength=\"255\" />";
2049 push( @loop_data, \%subfield_data );
2053 my $itemnumber = $itemrecord->subfield( $itemtagfield, $itemtagsubfield )
2054 if ( $itemrecord && $itemrecord->field($itemtagfield) );
2056 'itemtagfield' => $itemtagfield,
2057 'itemtagsubfield' => $itemtagsubfield,
2058 'itemnumber' => $itemnumber,
2059 'iteminformation' => \@loop_data
2065 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2067 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2068 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2069 # =head2 ModZebrafiles
2071 # &ModZebrafiles( $dbh, $biblionumber, $record, $folder, $server );
2075 # sub ModZebrafiles {
2077 # my ( $dbh, $biblionumber, $record, $folder, $server ) = @_;
2081 # C4::Context->zebraconfig($server)->{directory} . "/" . $folder . "/";
2082 # unless ( opendir( DIR, "$zebradir" ) ) {
2083 # warn "$zebradir not found";
2087 # my $filename = $zebradir . $biblionumber;
2090 # open( OUTPUT, ">", $filename . ".xml" );
2091 # print OUTPUT $record;
2100 ModZebra( $biblionumber, $op, $server, $oldRecord, $newRecord );
2102 $biblionumber is the biblionumber we want to index
2103 $op is specialUpdate or delete, and is used to know what we want to do
2104 $server is the server that we want to update
2105 $oldRecord is the MARC::Record containing the previous version of the record. This is used only when
2106 NoZebra=1, as NoZebra indexing needs to know the previous version of a record in order to
2108 $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.
2115 ###Accepts a $server variable thus we can use it for biblios authorities or other zebra dbs
2116 my ( $biblionumber, $op, $server, $oldRecord, $newRecord ) = @_;
2117 my $dbh=C4::Context->dbh;
2119 # true ModZebra commented until indexdata fixes zebraDB crashes (it seems they occur on multiple updates
2121 # replaced by a zebraqueue table, that is filled with ModZebra to run.
2122 # the table is emptied by misc/cronjobs/zebraqueue_start.pl script
2124 if (C4::Context->preference("NoZebra")) {
2125 # lock the nozebra table : we will read index lines, update them in Perl process
2126 # and write everything in 1 transaction.
2127 # lock the table to avoid someone else overwriting what we are doing
2128 $dbh->do('LOCK TABLES nozebra WRITE,biblio WRITE,biblioitems WRITE, systempreferences WRITE, auth_types WRITE, auth_header WRITE, auth_subfield_structure READ');
2129 my %result; # the result hash that will be built by deletion / add, and written on mySQL at the end, to improve speed
2130 if ($op eq 'specialUpdate') {
2131 # OK, we have to add or update the record
2132 # 1st delete (virtually, in indexes), if record actually exists
2134 %result = _DelBiblioNoZebra($biblionumber,$oldRecord,$server);
2136 # ... add the record
2137 %result=_AddBiblioNoZebra($biblionumber,$newRecord, $server, %result);
2139 # it's a deletion, delete the record...
2140 # warn "DELETE the record $biblionumber on $server".$record->as_formatted;
2141 %result=_DelBiblioNoZebra($biblionumber,$oldRecord,$server);
2143 # ok, now update the database...
2144 my $sth = $dbh->prepare("UPDATE nozebra SET biblionumbers=? WHERE server=? AND indexname=? AND value=?");
2145 foreach my $key (keys %result) {
2146 foreach my $index (keys %{$result{$key}}) {
2147 $sth->execute($result{$key}->{$index}, $server, $key, $index);
2150 $dbh->do('UNLOCK TABLES');
2153 # we use zebra, just fill zebraqueue table
2155 my $check_sql = "SELECT COUNT(*) FROM zebraqueue
2157 AND biblio_auth_number = ?
2160 my $check_sth = $dbh->prepare_cached($check_sql);
2161 $check_sth->execute($server, $biblionumber, $op);
2162 my ($count) = $check_sth->fetchrow_array;
2163 $check_sth->finish();
2165 my $sth=$dbh->prepare("INSERT INTO zebraqueue (biblio_auth_number,server,operation) VALUES(?,?,?)");
2166 $sth->execute($biblionumber,$server,$op);
2172 =head2 GetNoZebraIndexes
2174 %indexes = GetNoZebraIndexes;
2176 return the data from NoZebraIndexes syspref.
2180 sub GetNoZebraIndexes {
2181 my $no_zebra_indexes = C4::Context->preference('NoZebraIndexes');
2183 INDEX: foreach my $line (split /['"],[\n\r]*/,$no_zebra_indexes) {
2184 $line =~ /(.*)=>(.*)/;
2185 my $index = $1; # initial ' or " is removed afterwards
2187 $index =~ s/'|"|\s//g;
2188 $fields =~ s/'|"|\s//g;
2189 $indexes{$index}=$fields;
2194 =head1 INTERNAL FUNCTIONS
2196 =head2 _DelBiblioNoZebra($biblionumber,$record,$server);
2198 function to delete a biblio in NoZebra indexes
2199 This function does NOT delete anything in database : it reads all the indexes entries
2200 that have to be deleted & delete them in the hash
2201 The SQL part is done either :
2202 - after the Add if we are modifying a biblio (delete + add again)
2203 - immediatly after this sub if we are doing a true deletion.
2204 $server can be 'biblioserver' or 'authorityserver' : it indexes biblios or authorities (in the same table, $server being part of the table itself
2209 sub _DelBiblioNoZebra {
2210 my ($biblionumber, $record, $server)=@_;
2213 my $dbh = C4::Context->dbh;
2217 if ($server eq 'biblioserver') {
2218 %index=GetNoZebraIndexes;
2219 # get title of the record (to store the 10 first letters with the index)
2220 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title');
2221 $title = lc($record->subfield($titletag,$titlesubfield));
2223 # for authorities, the "title" is the $a mainentry
2224 my ($auth_type_tag, $auth_type_sf) = C4::AuthoritiesMarc::get_auth_type_location();
2225 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield($auth_type_tag, $auth_type_sf));
2226 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
2227 $title = $record->subfield($authref->{auth_tag_to_report},'a');
2228 $index{'mainmainentry'}= $authref->{'auth_tag_to_report'}.'a';
2229 $index{'mainentry'} = $authref->{'auth_tag_to_report'}.'*';
2230 $index{'auth_type'} = "${auth_type_tag}${auth_type_sf}";
2234 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2235 $title =~ s/ |,|;|\[|\]|\(|\)|\*|-|'|=//g;
2236 # limit to 10 char, should be enough, and limit the DB size
2237 $title = substr($title,0,10);
2239 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2240 foreach my $field ($record->fields()) {
2241 #parse each subfield
2242 next if $field->tag <10;
2243 foreach my $subfield ($field->subfields()) {
2244 my $tag = $field->tag();
2245 my $subfieldcode = $subfield->[0];
2247 # check each index to see if the subfield is stored somewhere
2248 # otherwise, store it in __RAW__ index
2249 foreach my $key (keys %index) {
2250 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2251 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
2253 my $line= lc $subfield->[1];
2254 # remove meaningless value in the field...
2255 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2256 # ... and split in words
2257 foreach (split / /,$line) {
2258 next unless $_; # skip empty values (multiple spaces)
2259 # if the entry is already here, do nothing, the biblionumber has already be removed
2260 unless ( defined( $result{$key}->{$_} ) && ( $result{$key}->{$_} =~ /$biblionumber,$title\-(\d);/) ) {
2261 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2262 $sth2->execute($server,$key,$_);
2263 my $existing_biblionumbers = $sth2->fetchrow;
2265 if ($existing_biblionumbers) {
2266 # warn " existing for $key $_: $existing_biblionumbers";
2267 $result{$key}->{$_} =$existing_biblionumbers;
2268 $result{$key}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2274 # the subfield is not indexed, store it in __RAW__ index anyway
2276 my $line= lc $subfield->[1];
2277 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:/ /g;
2278 # ... and split in words
2279 foreach (split / /,$line) {
2280 next unless $_; # skip empty values (multiple spaces)
2281 # if the entry is already here, do nothing, the biblionumber has already be removed
2282 unless ($result{'__RAW__'}->{$_} =~ /$biblionumber,$title\-(\d);/) {
2283 # get the index value if it exist in the nozebra table and remove the entry, otherwise, do nothing
2284 $sth2->execute($server,'__RAW__',$_);
2285 my $existing_biblionumbers = $sth2->fetchrow;
2287 if ($existing_biblionumbers) {
2288 $result{'__RAW__'}->{$_} =$existing_biblionumbers;
2289 $result{'__RAW__'}->{$_} =~ s/$biblionumber,$title\-(\d);//;
2299 =head2 _AddBiblioNoZebra($biblionumber, $record, $server, %result);
2301 function to add a biblio in NoZebra indexes
2305 sub _AddBiblioNoZebra {
2306 my ($biblionumber, $record, $server, %result)=@_;
2307 my $dbh = C4::Context->dbh;
2311 if ($server eq 'biblioserver') {
2312 %index=GetNoZebraIndexes;
2313 # get title of the record (to store the 10 first letters with the index)
2314 my ($titletag,$titlesubfield) = GetMarcFromKohaField('biblio.title');
2315 $title = lc($record->subfield($titletag,$titlesubfield));
2317 # warn "server : $server";
2318 # for authorities, the "title" is the $a mainentry
2319 my ($auth_type_tag, $auth_type_sf) = C4::AuthoritiesMarc::get_auth_type_location();
2320 my $authref = C4::AuthoritiesMarc::GetAuthType($record->subfield($auth_type_tag, $auth_type_sf));
2321 warn "ERROR : authtype undefined for ".$record->as_formatted unless $authref;
2322 $title = $record->subfield($authref->{auth_tag_to_report},'a');
2323 $index{'mainmainentry'} = $authref->{auth_tag_to_report}.'a';
2324 $index{'mainentry'} = $authref->{auth_tag_to_report}.'*';
2325 $index{'auth_type'} = "${auth_type_tag}${auth_type_sf}";
2328 # remove blancks comma (that could cause problem when decoding the string for CQL retrieval) and regexp specific values
2329 $title =~ s/ |\.|,|;|\[|\]|\(|\)|\*|-|'|:|=|\r|\n//g;
2330 # limit to 10 char, should be enough, and limit the DB size
2331 $title = substr($title,0,10);
2333 my $sth2=$dbh->prepare('SELECT biblionumbers FROM nozebra WHERE server=? AND indexname=? AND value=?');
2334 foreach my $field ($record->fields()) {
2335 #parse each subfield
2336 ###FIXME: impossible to index a 001-009 value with NoZebra
2337 next if $field->tag <10;
2338 foreach my $subfield ($field->subfields()) {
2339 my $tag = $field->tag();
2340 my $subfieldcode = $subfield->[0];
2342 # warn "INDEXING :".$subfield->[1];
2343 # check each index to see if the subfield is stored somewhere
2344 # otherwise, store it in __RAW__ index
2345 foreach my $key (keys %index) {
2346 # warn "examining $key index : ".$index{$key}." for $tag $subfieldcode";
2347 if ($index{$key} =~ /$tag\*/ or $index{$key} =~ /$tag$subfieldcode/) {
2349 my $line= lc $subfield->[1];
2350 # remove meaningless value in the field...
2351 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g;
2352 # ... and split in words
2353 foreach (split / /,$line) {
2354 next unless $_; # skip empty values (multiple spaces)
2355 # if the entry is already here, improve weight
2356 # warn "managing $_";
2357 if ( exists $result{$key}->{$_} && $result{$key}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d+);/) {
2358 my $weight = $1 + 1;
2359 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
2360 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2362 # get the value if it exist in the nozebra table, otherwise, create it
2363 $sth2->execute($server,$key,$_);
2364 my $existing_biblionumbers = $sth2->fetchrow;
2366 if ($existing_biblionumbers) {
2367 $result{$key}->{"$_"} =$existing_biblionumbers;
2368 my $weight = defined $1 ? $1 + 1 : 1;
2369 $result{$key}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//g;
2370 $result{$key}->{"$_"} .= "$biblionumber,$title-$weight;";
2371 # create a new ligne for this entry
2373 # warn "INSERT : $server / $key / $_";
2374 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname='.$dbh->quote($key).',value='.$dbh->quote($_));
2375 $result{$key}->{"$_"}.="$biblionumber,$title-1;";
2381 # the subfield is not indexed, store it in __RAW__ index anyway
2383 my $line= lc $subfield->[1];
2384 $line =~ s/-|\.|\?|,|;|!|'|\(|\)|\[|\]|{|}|"|<|>|&|\+|\*|\/|=|:|\r|\n/ /g;
2385 # ... and split in words
2386 foreach (split / /,$line) {
2387 next unless $_; # skip empty values (multiple spaces)
2388 # if the entry is already here, improve weight
2389 if ($result{'__RAW__'}->{"$_"} =~ /$biblionumber,\Q$title\E\-(\d+);/) {
2391 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
2392 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2394 # get the value if it exist in the nozebra table, otherwise, create it
2395 $sth2->execute($server,'__RAW__',$_);
2396 my $existing_biblionumbers = $sth2->fetchrow;
2398 if ($existing_biblionumbers) {
2399 $result{'__RAW__'}->{"$_"} =$existing_biblionumbers;
2401 $result{'__RAW__'}->{"$_"} =~ s/$biblionumber,\Q$title\E\-(\d+);//;
2402 $result{'__RAW__'}->{"$_"} .= "$biblionumber,$title-$weight;";
2403 # create a new ligne for this entry
2405 $dbh->do('INSERT INTO nozebra SET server='.$dbh->quote($server).', indexname="__RAW__",value='.$dbh->quote($_));
2406 $result{'__RAW__'}->{"$_"}.="$biblionumber,$title-1;";
2421 ($indicators, $value) = _find_value($tag, $subfield, $record,$encoding);
2423 Find the given $subfield in the given $tag in the given
2424 MARC::Record $record. If the subfield is found, returns
2425 the (indicators, value) pair; otherwise, (undef, undef) is
2429 Such a function is used in addbiblio AND additem and serial-edit and maybe could be used in Authorities.
2430 I suggest we export it from this module.
2437 my ( $tagfield, $insubfield, $record, $encoding ) = @_;
2440 if ( $tagfield < 10 ) {
2441 if ( $record->field($tagfield) ) {
2442 push @result, $record->field($tagfield)->data();
2449 foreach my $field ( $record->field($tagfield) ) {
2450 my @subfields = $field->subfields();
2451 foreach my $subfield (@subfields) {
2452 if ( @$subfield[0] eq $insubfield ) {
2453 push @result, @$subfield[1];
2454 $indicator = $field->indicator(1) . $field->indicator(2);
2459 return ( $indicator, @result );
2462 =head2 _koha_marc_update_bib_ids
2466 _koha_marc_update_bib_ids($record, $frameworkcode, $biblionumber, $biblioitemnumber);
2468 Internal function to add or update biblionumber and biblioitemnumber to
2475 sub _koha_marc_update_bib_ids {
2476 my ($record, $frameworkcode, $biblionumber, $biblioitemnumber) = @_;
2478 # we must add bibnum and bibitemnum in MARC::Record...
2479 # we build the new field with biblionumber and biblioitemnumber
2480 # we drop the original field
2481 # we add the new builded field.
2482 my ($biblio_tag, $biblio_subfield ) = GetMarcFromKohaField("biblio.biblionumber",$frameworkcode);
2483 my ($biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField("biblioitems.biblioitemnumber",$frameworkcode);
2485 if ($biblio_tag != $biblioitem_tag) {
2486 # biblionumber & biblioitemnumber are in different fields
2488 # deal with biblionumber
2489 my ($new_field, $old_field);
2490 if ($biblio_tag < 10) {
2491 $new_field = MARC::Field->new( $biblio_tag, $biblionumber );
2494 MARC::Field->new( $biblio_tag, '', '',
2495 "$biblio_subfield" => $biblionumber );
2498 # drop old field and create new one...
2499 $old_field = $record->field($biblio_tag);
2500 $record->delete_field($old_field) if $old_field;
2501 $record->append_fields($new_field);
2503 # deal with biblioitemnumber
2504 if ($biblioitem_tag < 10) {
2505 $new_field = MARC::Field->new( $biblioitem_tag, $biblioitemnumber, );
2508 MARC::Field->new( $biblioitem_tag, '', '',
2509 "$biblioitem_subfield" => $biblioitemnumber, );
2511 # drop old field and create new one...
2512 $old_field = $record->field($biblioitem_tag);
2513 $record->delete_field($old_field) if $old_field;
2514 $record->insert_fields_ordered($new_field);
2517 # biblionumber & biblioitemnumber are in the same field (can't be <10 as fields <10 have only 1 value)
2518 my $new_field = MARC::Field->new(
2519 $biblio_tag, '', '',
2520 "$biblio_subfield" => $biblionumber,
2521 "$biblioitem_subfield" => $biblioitemnumber
2524 # drop old field and create new one...
2525 my $old_field = $record->field($biblio_tag);
2526 $record->delete_field($old_field) if $old_field;
2527 $record->insert_fields_ordered($new_field);
2531 =head2 _koha_marc_update_biblioitem_cn_sort
2535 _koha_marc_update_biblioitem_cn_sort($marc, $biblioitem, $frameworkcode);
2539 Given a MARC bib record and the biblioitem hash, update the
2540 subfield that contains a copy of the value of biblioitems.cn_sort.
2544 sub _koha_marc_update_biblioitem_cn_sort {
2546 my $biblioitem = shift;
2547 my $frameworkcode= shift;
2549 my ($biblioitem_tag, $biblioitem_subfield ) = GetMarcFromKohaField("biblioitems.cn_sort",$frameworkcode);
2550 return unless $biblioitem_tag;
2552 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2554 if (my $field = $marc->field($biblioitem_tag)) {
2555 $field->delete_subfield(code => $biblioitem_subfield);
2556 if ($cn_sort ne '') {
2557 $field->add_subfields($biblioitem_subfield => $cn_sort);
2560 # if we get here, no biblioitem tag is present in the MARC record, so
2561 # we'll create it if $cn_sort is not empty -- this would be
2562 # an odd combination of events, however
2564 $marc->insert_grouped_field(MARC::Field->new($biblioitem_tag, ' ', ' ', $biblioitem_subfield => $cn_sort));
2569 =head2 _koha_add_biblio
2573 my ($biblionumber,$error) = _koha_add_biblio($dbh,$biblioitem);
2575 Internal function to add a biblio ($biblio is a hash with the values)
2581 sub _koha_add_biblio {
2582 my ( $dbh, $biblio, $frameworkcode ) = @_;
2586 # set the series flag
2588 if ( $biblio->{'seriestitle'} ) { $serial = 1 };
2592 SET frameworkcode = ?,
2603 my $sth = $dbh->prepare($query);
2606 $biblio->{'author'},
2608 $biblio->{'unititle'},
2611 $biblio->{'seriestitle'},
2612 $biblio->{'copyrightdate'},
2613 $biblio->{'abstract'}
2616 my $biblionumber = $dbh->{'mysql_insertid'};
2617 if ( $dbh->errstr ) {
2618 $error.="ERROR in _koha_add_biblio $query".$dbh->errstr;
2623 #warn "LEAVING _koha_add_biblio: ".$biblionumber."\n";
2624 return ($biblionumber,$error);
2627 =head2 _koha_modify_biblio
2631 my ($biblionumber,$error) == _koha_modify_biblio($dbh,$biblio,$frameworkcode);
2633 Internal function for updating the biblio table
2639 sub _koha_modify_biblio {
2640 my ( $dbh, $biblio, $frameworkcode ) = @_;
2645 SET frameworkcode = ?,
2654 WHERE biblionumber = ?
2657 my $sth = $dbh->prepare($query);
2661 $biblio->{'author'},
2663 $biblio->{'unititle'},
2665 $biblio->{'serial'},
2666 $biblio->{'seriestitle'},
2667 $biblio->{'copyrightdate'},
2668 $biblio->{'abstract'},
2669 $biblio->{'biblionumber'}
2670 ) if $biblio->{'biblionumber'};
2672 if ( $dbh->errstr || !$biblio->{'biblionumber'} ) {
2673 $error.="ERROR in _koha_modify_biblio $query".$dbh->errstr;
2676 return ( $biblio->{'biblionumber'},$error );
2679 =head2 _koha_modify_biblioitem_nonmarc
2683 my ($biblioitemnumber,$error) = _koha_modify_biblioitem_nonmarc( $dbh, $biblioitem );
2685 Updates biblioitems row except for marc and marcxml, which should be changed
2692 sub _koha_modify_biblioitem_nonmarc {
2693 my ( $dbh, $biblioitem ) = @_;
2696 # re-calculate the cn_sort, it may have changed
2697 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2701 SET biblionumber = ?,
2707 publicationyear = ?,
2711 collectiontitle = ?,
2713 collectionvolume= ?,
2714 editionstatement= ?,
2715 editionresponsibility = ?,
2729 where biblioitemnumber = ?
2731 my $sth = $dbh->prepare($query);
2733 $biblioitem->{'biblionumber'},
2734 $biblioitem->{'volume'},
2735 $biblioitem->{'number'},
2736 $biblioitem->{'itemtype'},
2737 $biblioitem->{'isbn'},
2738 $biblioitem->{'issn'},
2739 $biblioitem->{'publicationyear'},
2740 $biblioitem->{'publishercode'},
2741 $biblioitem->{'volumedate'},
2742 $biblioitem->{'volumedesc'},
2743 $biblioitem->{'collectiontitle'},
2744 $biblioitem->{'collectionissn'},
2745 $biblioitem->{'collectionvolume'},
2746 $biblioitem->{'editionstatement'},
2747 $biblioitem->{'editionresponsibility'},
2748 $biblioitem->{'illus'},
2749 $biblioitem->{'pages'},
2750 $biblioitem->{'bnotes'},
2751 $biblioitem->{'size'},
2752 $biblioitem->{'place'},
2753 $biblioitem->{'lccn'},
2754 $biblioitem->{'url'},
2755 $biblioitem->{'biblioitems.cn_source'},
2756 $biblioitem->{'cn_class'},
2757 $biblioitem->{'cn_item'},
2758 $biblioitem->{'cn_suffix'},
2760 $biblioitem->{'totalissues'},
2761 $biblioitem->{'biblioitemnumber'}
2763 if ( $dbh->errstr ) {
2764 $error.="ERROR in _koha_modify_biblioitem_nonmarc $query".$dbh->errstr;
2767 return ($biblioitem->{'biblioitemnumber'},$error);
2770 =head2 _koha_add_biblioitem
2774 my ($biblioitemnumber,$error) = _koha_add_biblioitem( $dbh, $biblioitem );
2776 Internal function to add a biblioitem
2782 sub _koha_add_biblioitem {
2783 my ( $dbh, $biblioitem ) = @_;
2786 my ($cn_sort) = GetClassSort($biblioitem->{'biblioitems.cn_source'}, $biblioitem->{'cn_class'}, $biblioitem->{'cn_item'} );
2788 "INSERT INTO biblioitems SET
2795 publicationyear = ?,
2799 collectiontitle = ?,
2801 collectionvolume= ?,
2802 editionstatement= ?,
2803 editionresponsibility = ?,
2819 my $sth = $dbh->prepare($query);
2821 $biblioitem->{'biblionumber'},
2822 $biblioitem->{'volume'},
2823 $biblioitem->{'number'},
2824 $biblioitem->{'itemtype'},
2825 $biblioitem->{'isbn'},
2826 $biblioitem->{'issn'},
2827 $biblioitem->{'publicationyear'},
2828 $biblioitem->{'publishercode'},
2829 $biblioitem->{'volumedate'},
2830 $biblioitem->{'volumedesc'},
2831 $biblioitem->{'collectiontitle'},
2832 $biblioitem->{'collectionissn'},
2833 $biblioitem->{'collectionvolume'},
2834 $biblioitem->{'editionstatement'},
2835 $biblioitem->{'editionresponsibility'},
2836 $biblioitem->{'illus'},
2837 $biblioitem->{'pages'},
2838 $biblioitem->{'bnotes'},
2839 $biblioitem->{'size'},
2840 $biblioitem->{'place'},
2841 $biblioitem->{'lccn'},
2842 $biblioitem->{'marc'},
2843 $biblioitem->{'url'},
2844 $biblioitem->{'biblioitems.cn_source'},
2845 $biblioitem->{'cn_class'},
2846 $biblioitem->{'cn_item'},
2847 $biblioitem->{'cn_suffix'},
2849 $biblioitem->{'totalissues'}
2851 my $bibitemnum = $dbh->{'mysql_insertid'};
2852 if ( $dbh->errstr ) {
2853 $error.="ERROR in _koha_add_biblioitem $query".$dbh->errstr;
2857 return ($bibitemnum,$error);
2860 =head2 _koha_delete_biblio
2864 $error = _koha_delete_biblio($dbh,$biblionumber);
2866 Internal sub for deleting from biblio table -- also saves to deletedbiblio
2868 C<$dbh> - the database handle
2869 C<$biblionumber> - the biblionumber of the biblio to be deleted
2875 # FIXME: add error handling
2877 sub _koha_delete_biblio {
2878 my ( $dbh, $biblionumber ) = @_;
2880 # get all the data for this biblio
2881 my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber=?");
2882 $sth->execute($biblionumber);
2884 if ( my $data = $sth->fetchrow_hashref ) {
2886 # save the record in deletedbiblio
2887 # find the fields to save
2888 my $query = "INSERT INTO deletedbiblio SET ";
2890 foreach my $temp ( keys %$data ) {
2891 $query .= "$temp = ?,";
2892 push( @bind, $data->{$temp} );
2895 # replace the last , by ",?)"
2897 my $bkup_sth = $dbh->prepare($query);
2898 $bkup_sth->execute(@bind);
2902 my $del_sth = $dbh->prepare("DELETE FROM biblio WHERE biblionumber=?");
2903 $del_sth->execute($biblionumber);
2910 =head2 _koha_delete_biblioitems
2914 $error = _koha_delete_biblioitems($dbh,$biblioitemnumber);
2916 Internal sub for deleting from biblioitems table -- also saves to deletedbiblioitems
2918 C<$dbh> - the database handle
2919 C<$biblionumber> - the biblioitemnumber of the biblioitem to be deleted
2925 # FIXME: add error handling
2927 sub _koha_delete_biblioitems {
2928 my ( $dbh, $biblioitemnumber ) = @_;
2930 # get all the data for this biblioitem
2932 $dbh->prepare("SELECT * FROM biblioitems WHERE biblioitemnumber=?");
2933 $sth->execute($biblioitemnumber);
2935 if ( my $data = $sth->fetchrow_hashref ) {
2937 # save the record in deletedbiblioitems
2938 # find the fields to save
2939 my $query = "INSERT INTO deletedbiblioitems SET ";
2941 foreach my $temp ( keys %$data ) {
2942 $query .= "$temp = ?,";
2943 push( @bind, $data->{$temp} );
2946 # replace the last , by ",?)"
2948 my $bkup_sth = $dbh->prepare($query);
2949 $bkup_sth->execute(@bind);
2952 # delete the biblioitem
2954 $dbh->prepare("DELETE FROM biblioitems WHERE biblioitemnumber=?");
2955 $del_sth->execute($biblioitemnumber);
2962 =head1 UNEXPORTED FUNCTIONS
2964 =head2 ModBiblioMarc
2966 &ModBiblioMarc($newrec,$biblionumber,$frameworkcode);
2968 Add MARC data for a biblio to koha
2970 Function exported, but should NOT be used, unless you really know what you're doing
2976 # pass the MARC::Record to this function, and it will create the records in the marc field
2977 my ( $record, $biblionumber, $frameworkcode ) = @_;
2978 my $dbh = C4::Context->dbh;
2979 my @fields = $record->fields();
2980 if ( !$frameworkcode ) {
2981 $frameworkcode = "";
2984 $dbh->prepare("UPDATE biblio SET frameworkcode=? WHERE biblionumber=?");
2985 $sth->execute( $frameworkcode, $biblionumber );
2987 my $encoding = C4::Context->preference("marcflavour");
2989 # deal with UNIMARC field 100 (encoding) : create it if needed & set encoding to unicode
2990 if ( $encoding eq "UNIMARC" ) {
2992 if ( length($record->subfield( 100, "a" )) == 35 ) {
2993 $string = $record->subfield( 100, "a" );
2994 my $f100 = $record->field(100);
2995 $record->delete_field($f100);
2998 $string = POSIX::strftime( "%Y%m%d", localtime );
3000 $string = sprintf( "%-*s", 35, $string );
3002 substr( $string, 22, 6, "frey50" );
3003 unless ( $record->subfield( 100, "a" ) ) {
3004 $record->insert_grouped_field(
3005 MARC::Field->new( 100, "", "", "a" => $string ) );
3009 if (C4::Context->preference("NoZebra")) {
3010 # only NoZebra indexing needs to have
3011 # the previous version of the record
3012 $oldRecord = GetMarcBiblio($biblionumber);
3016 "UPDATE biblioitems SET marc=?,marcxml=? WHERE biblionumber=?");
3017 $sth->execute( $record->as_usmarc(), $record->as_xml_record($encoding),
3020 ModZebra($biblionumber,"specialUpdate","biblioserver",$oldRecord,$record);
3021 return $biblionumber;
3024 =head2 z3950_extended_services
3026 z3950_extended_services($serviceType,$serviceOptions,$record);
3028 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.
3030 C<$serviceType> one of: itemorder,create,drop,commit,update,xmlupdate
3032 C<$serviceOptions> a has of key/value pairs. For instance, if service_type is 'update', $service_options should contain:
3034 action => update action, one of specialUpdate, recordInsert, recordReplace, recordDelete, elementUpdate.
3038 recordidOpaque => Opaque Record ID (user supplied) or recordidNumber => Record ID number (system number).
3039 syntax => the record syntax (transfer syntax)
3040 databaseName = Database from connection object
3042 To set serviceOptions, call set_service_options($serviceType)
3044 C<$record> the record, if one is needed for the service type
3046 A record should be in XML. You can convert it to XML from MARC by running it through marc2xml().
3050 sub z3950_extended_services {
3051 my ( $server, $serviceType, $action, $serviceOptions ) = @_;
3053 # get our connection object
3054 my $Zconn = C4::Context->Zconn( $server, 0, 1 );
3056 # create a new package object
3057 my $Zpackage = $Zconn->package();
3060 $Zpackage->option( action => $action );
3062 if ( $serviceOptions->{'databaseName'} ) {
3063 $Zpackage->option( databaseName => $serviceOptions->{'databaseName'} );
3065 if ( $serviceOptions->{'recordIdNumber'} ) {
3067 recordIdNumber => $serviceOptions->{'recordIdNumber'} );
3069 if ( $serviceOptions->{'recordIdOpaque'} ) {
3071 recordIdOpaque => $serviceOptions->{'recordIdOpaque'} );
3074 # this is an ILL request (Zebra doesn't support it, but Koha could eventually)
3075 #if ($serviceType eq 'itemorder') {
3076 # $Zpackage->option('contact-name' => $serviceOptions->{'contact-name'});
3077 # $Zpackage->option('contact-phone' => $serviceOptions->{'contact-phone'});
3078 # $Zpackage->option('contact-email' => $serviceOptions->{'contact-email'});
3079 # $Zpackage->option('itemorder-item' => $serviceOptions->{'itemorder-item'});
3082 if ( $serviceOptions->{record} ) {
3083 $Zpackage->option( record => $serviceOptions->{record} );
3085 # can be xml or marc
3086 if ( $serviceOptions->{'syntax'} ) {
3087 $Zpackage->option( syntax => $serviceOptions->{'syntax'} );
3091 # send the request, handle any exception encountered
3092 eval { $Zpackage->send($serviceType) };
3093 if ( $@ && $@->isa("ZOOM::Exception") ) {
3094 return "error: " . $@->code() . " " . $@->message() . "\n";
3097 # free up package resources
3098 $Zpackage->destroy();
3101 =head2 set_service_options
3103 my $serviceOptions = set_service_options($serviceType);
3105 C<$serviceType> itemorder,create,drop,commit,update,xmlupdate
3107 Currently, we only support 'create', 'commit', and 'update'. 'drop' support will be added as soon as Zebra supports it.
3111 sub set_service_options {
3112 my ($serviceType) = @_;
3115 # FIXME: This needs to be an OID ... if we ever need 'syntax' this sub will need to change
3116 # $serviceOptions->{ 'syntax' } = ''; #zebra doesn't support syntaxes other than xml
3118 if ( $serviceType eq 'commit' ) {
3122 if ( $serviceType eq 'create' ) {
3126 if ( $serviceType eq 'drop' ) {
3127 die "ERROR: 'drop' not currently supported (by Zebra)";
3129 return $serviceOptions;
3132 =head3 get_biblio_authorised_values
3134 find the types and values for all authorised values assigned to this biblio.
3139 returns: a hashref malling the authorised value to the value set for this biblionumber
3141 $authorised_values = {
3142 'Scent' => 'flowery',
3143 'Audience' => 'Young Adult',
3144 'itemtypes' => 'SER',
3147 Notes: forlibrarian should probably be passed in, and called something different.
3152 sub get_biblio_authorised_values {
3153 my $biblionumber = shift;
3155 my $forlibrarian = 1; # are we in staff or opac?
3156 my $frameworkcode = GetFrameworkCode( $biblionumber );
3158 my $authorised_values;
3160 my $record = GetMarcBiblio( $biblionumber )
3161 or return $authorised_values;
3162 my $tagslib = GetMarcStructure( $forlibrarian, $frameworkcode )
3163 or return $authorised_values;
3165 # assume that these entries in the authorised_value table are bibliolevel.
3166 # ones that start with 'item%' are item level.
3167 my $query = q(SELECT distinct authorised_value, kohafield
3168 FROM marc_subfield_structure
3169 WHERE authorised_value !=''
3170 AND (kohafield like 'biblio%'
3171 OR kohafield like '') );
3172 my $bibliolevel_authorised_values = C4::Context->dbh->selectall_hashref( $query, 'authorised_value' );
3174 foreach my $tag ( keys( %$tagslib ) ) {
3175 foreach my $subfield ( keys( %{$tagslib->{ $tag }} ) ) {
3176 # warn "checking $subfield. type is: " . ref $tagslib->{ $tag }{ $subfield };
3177 if ( 'HASH' eq ref $tagslib->{ $tag }{ $subfield } ) {
3178 if ( defined $tagslib->{ $tag }{ $subfield }{'authorised_value'} && exists $bibliolevel_authorised_values->{ $tagslib->{ $tag }{ $subfield }{'authorised_value'} } ) {
3179 if ( defined $record->field( $tag ) ) {
3180 my $this_subfield_value = $record->field( $tag )->subfield( $subfield );
3181 if ( defined $this_subfield_value ) {
3182 $authorised_values->{ $tagslib->{ $tag }{ $subfield }{'authorised_value'} } = $this_subfield_value;
3189 # warn ( Data::Dumper->Dump( [ $authorised_values ], [ 'authorised_values' ] ) );
3190 return $authorised_values;
3200 Koha Developement team <info@koha.org>
3202 Paul POULAIN paul.poulain@free.fr
3204 Joshua Ferraro jmf@liblime.com