use Koha::Caches;
use Koha::Authority::Types;
use Koha::Acquisition::Currencies;
+use Koha::Biblio::Metadata;
+use Koha::Biblio::Metadatas;
use Koha::SearchEngine;
use Koha::Libraries;
GetBiblionumberFromItemnumber
&GetRecordValue
- &GetFieldMapping
- &SetFieldMapping
- &DeleteFieldMapping
&GetISBDView
&CountItemsIssued
&CountBiblioInOrders
&GetSubscriptionsId
- &GetHolds
);
# To modify something
push @EXPORT, qw(
&ModBiblio
- &ModBiblioframework
&ModZebra
&UpdateTotalIssues
&RemoveAllNsb
=item 2. as raw MARC in the Zebra index and storage engine
-=item 3. as raw MARC the biblioitems.marc and biblioitems.marcxml
+=item 3. as MARC XML in biblio_metadata.metadata
=back
-In the 3.0 version of Koha, the authoritative record-level information is in biblioitems.marcxml
+In the 3.0 version of Koha, the authoritative record-level information is in biblio_metadata.metadata
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.
=item 2. _koha_* - low-level internal functions for managing the koha tables
-=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.
+=item 3. Marc management function : as the MARC record is stored in biblio_metadata.metadata, 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.
=item 4. Zebra functions used to update the Zebra index
=back
-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 :
+The MARC record (in biblio_metadata.metadata) 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 :
=over 4
=back
-When dealing with items, we must :
-
-=over 4
-
-=item 1. save the item in items table, that gives us an itemnumber
-
-=item 2. add the itemnumber to the item MARC field
-
-=item 3. overwrite the MARC record (with the added item) into biblioitems.marc(xml)
-
-When modifying a biblio or an item, the behaviour is quite similar.
-
-=back
-
=head1 EXPORTED FUNCTIONS
=head2 AddBiblio
This function also accepts a third, optional argument: a hashref
to additional options. The only defined option is C<defer_marc_save>,
which if present and mapped to a true value, causes C<AddBiblio>
-to omit the call to save the MARC in C<bibilioitems.marc>
-and C<biblioitems.marcxml> This option is provided B<only>
+to omit the call to save the MARC in C<biblio_metadata.metadata>
+This option is provided B<only>
for the use of scripts such as C<bulkmarcimport.pl> that may need
to do some manipulation of the MARC record for item parsing before
saving it and which cannot afford the performance hit of saving
}
}
-=head2 ModBiblioframework
-
- ModBiblioframework($biblionumber,$frameworkcode);
-
-Exported function to modify a biblio framework
-
-=cut
-
-sub ModBiblioframework {
- my ( $biblionumber, $frameworkcode ) = @_;
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare( "UPDATE biblio SET frameworkcode=? WHERE biblionumber=?" );
- $sth->execute( $frameworkcode, $biblionumber );
- return 1;
-}
-
=head2 DelBiblio
my $error = &DelBiblio($biblionumber);
return $error if $error;
}
+
# delete biblio from Koha tables and save in deletedbiblio
# must do this *after* _koha_delete_biblioitems, otherwise
# delete cascade will prevent deletedbiblioitems rows
return \@result;
}
-=head2 SetFieldMapping
-
- SetFieldMapping($framework, $field, $fieldcode, $subfieldcode);
-
-Set a Field to MARC mapping value, if it already exists we don't add a new one.
-
-=cut
-
-sub SetFieldMapping {
- my ( $framework, $field, $fieldcode, $subfieldcode ) = @_;
- my $dbh = C4::Context->dbh;
-
- my $sth = $dbh->prepare('SELECT * FROM fieldmapping WHERE fieldcode = ? AND subfieldcode = ? AND frameworkcode = ? AND field = ?');
- $sth->execute( $fieldcode, $subfieldcode, $framework, $field );
- if ( not $sth->fetchrow_hashref ) {
- my @args;
- $sth = $dbh->prepare('INSERT INTO fieldmapping (fieldcode, subfieldcode, frameworkcode, field) VALUES(?,?,?,?)');
-
- $sth->execute( $fieldcode, $subfieldcode, $framework, $field );
- }
-}
-
-=head2 DeleteFieldMapping
-
- DeleteFieldMapping($id);
-
-Delete a field mapping from an $id.
-
-=cut
-
-sub DeleteFieldMapping {
- my ($id) = @_;
- my $dbh = C4::Context->dbh;
-
- my $sth = $dbh->prepare('DELETE FROM fieldmapping WHERE id = ?');
- $sth->execute($id);
-}
-
-=head2 GetFieldMapping
-
- GetFieldMapping($frameworkcode);
-
-Get all field mappings for a specified frameworkcode
-
-=cut
-
-sub GetFieldMapping {
- my ($framework) = @_;
- my $dbh = C4::Context->dbh;
-
- my $sth = $dbh->prepare('SELECT * FROM fieldmapping where frameworkcode = ?');
- $sth->execute($framework);
-
- my @return;
- while ( my $row = $sth->fetchrow_hashref ) {
- push @return, $row;
- }
- return \@return;
-}
-
=head2 GetBiblioData
$data = &GetBiblioData($biblionumber);
my $biblioitemnumber_values = @biblioitemnumbers ? join( ',', @biblioitemnumbers ) : "''";
+ my $dbh = C4::Context->dbh;
my $query = "
SELECT biblioitemnumber,
publicationyear,
FROM biblioitems
WHERE biblioitemnumber IN ($biblioitemnumber_values)
";
- return get_infos_of( $query, 'biblioitemnumber' );
+ return $dbh->selectall_hashref($query, 'biblioitemnumber');
}
=head1 FUNCTIONS FOR HANDLING MARC MANAGEMENT
}
my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare("SELECT biblioitemnumber, marcxml FROM biblioitems WHERE biblionumber=? ");
+ my $sth = $dbh->prepare("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber=? ");
$sth->execute($biblionumber);
my $row = $sth->fetchrow_hashref;
my $biblioitemnumber = $row->{'biblioitemnumber'};
- my $marcxml = StripNonXmlChars( $row->{'marcxml'} );
+ my $marcxml = GetXmlBiblio( $biblionumber );
+ $marcxml = StripNonXmlChars( $marcxml );
my $frameworkcode = GetFrameworkCode($biblionumber);
MARC::File::XML->default_record_format( C4::Context->preference('marcflavour') );
my $record = MARC::Record->new();
my $marcxml = GetXmlBiblio($biblionumber);
-Returns biblioitems.marcxml of the biblionumber passed in parameter.
+Returns biblio_metadata.metadata/marcxml of the biblionumber passed in parameter.
The XML should only contain biblio information (item information is no longer stored in marcxml field)
=cut
sub GetXmlBiblio {
my ($biblionumber) = @_;
- my $dbh = C4::Context->dbh;
- my $sth = $dbh->prepare("SELECT marcxml FROM biblioitems WHERE biblionumber=? ");
- $sth->execute($biblionumber);
- my ($marcxml) = $sth->fetchrow;
+ my $dbh = C4::Context->dbh;
+ return unless $biblionumber;
+ my ($marcxml) = $dbh->selectrow_array(
+ q|
+ SELECT metadata
+ FROM biblio_metadata
+ WHERE biblionumber=?
+ AND format='marcxml'
+ AND marcflavour=?
+ |, undef, $biblionumber, C4::Context->preference('marcflavour')
+ );
return $marcxml;
}
my ($biblioitemnumber,$error) = _koha_modify_biblioitem_nonmarc( $dbh, $biblioitem );
-Updates biblioitems row except for marc and marcxml, which should be changed
-via ModBiblioMarc
-
=cut
sub _koha_modify_biblioitem_nonmarc {
size = ?,
place = ?,
lccn = ?,
- marc = ?,
url = ?,
cn_source = ?,
cn_class = ?,
$biblioitem->{'volumedate'}, $biblioitem->{'volumedesc'}, $biblioitem->{'collectiontitle'}, $biblioitem->{'collectionissn'},
$biblioitem->{'collectionvolume'}, $biblioitem->{'editionstatement'}, $biblioitem->{'editionresponsibility'}, $biblioitem->{'illus'},
$biblioitem->{'pages'}, $biblioitem->{'bnotes'}, $biblioitem->{'size'}, $biblioitem->{'place'},
- $biblioitem->{'lccn'}, $biblioitem->{'marc'}, $biblioitem->{'url'}, $biblioitem->{'biblioitems.cn_source'},
+ $biblioitem->{'lccn'}, $biblioitem->{'url'}, $biblioitem->{'biblioitems.cn_source'},
$biblioitem->{'cn_class'}, $biblioitem->{'cn_item'}, $biblioitem->{'cn_suffix'}, $cn_sort,
$biblioitem->{'totalissues'}, $biblioitem->{'ean'}, $biblioitem->{'agerestriction'}
);
my $sth = $dbh->prepare("SELECT * FROM biblio WHERE biblionumber=?");
$sth->execute($biblionumber);
+ # FIXME There is a transaction in _koha_delete_biblio_metadata
+ # But actually all the following should be done inside a single transaction
if ( my $data = $sth->fetchrow_hashref ) {
# save the record in deletedbiblio
$bkup_sth->execute(@bind);
$bkup_sth->finish;
+ _koha_delete_biblio_metadata( $biblionumber );
+
# delete the biblio
my $sth2 = $dbh->prepare("DELETE FROM biblio WHERE biblionumber=?");
$sth2->execute($biblionumber);
return;
}
+=head2 _koha_delete_biblio_metadata
+
+ $error = _koha_delete_biblio_metadata($biblionumber);
+
+C<$biblionumber> - the biblionumber of the biblio metadata to be deleted
+
+=cut
+
+sub _koha_delete_biblio_metadata {
+ my ($biblionumber) = @_;
+
+ my $dbh = C4::Context->dbh;
+ my $schema = Koha::Database->new->schema;
+ $schema->txn_do(
+ sub {
+ $dbh->do( q|
+ INSERT INTO deletedbiblio_metadata (biblionumber, format, marcflavour, metadata)
+ SELECT biblionumber, format, marcflavour, metadata FROM biblio_metadata WHERE biblionumber=?
+ |, undef, $biblionumber );
+ $dbh->do( q|DELETE FROM biblio_metadata WHERE biblionumber=?|,
+ undef, $biblionumber );
+ }
+ );
+}
+
=head1 UNEXPORTED FUNCTIONS
=head2 ModBiblioMarc
&ModBiblioMarc($newrec,$biblionumber,$frameworkcode);
-Add MARC data for a biblio to koha
+Add MARC XML data for a biblio to koha
Function exported, but should NOT be used, unless you really know what you're doing
sub ModBiblioMarc {
# pass the MARC::Record to this function, and it will create the records in
- # the marc field
+ # the marcxml field
my ( $record, $biblionumber, $frameworkcode ) = @_;
if ( !$record ) {
carp 'ModBiblioMarc passed an undefined record';
$f005->update(sprintf("%4d%02d%02d%02d%02d%04.1f",@a)) if $f005;
}
- $sth = $dbh->prepare("UPDATE biblioitems SET marc=?,marcxml=? WHERE biblionumber=?");
- $sth->execute( $record->as_usmarc(), $record->as_xml_record($encoding), $biblionumber );
- $sth->finish;
+ my $metadata = {
+ biblionumber => $biblionumber,
+ format => 'marcxml',
+ marcflavour => C4::Context->preference('marcflavour'),
+ };
+ # FIXME To replace with ->find_or_create?
+ if ( my $m_rs = Koha::Biblio::Metadatas->find($metadata) ) {
+ $m_rs->metadata( $record->as_xml_record($encoding) );
+ $m_rs->store;
+ } else {
+ my $m_rs = Koha::Biblio::Metadata->new($metadata);
+ $m_rs->metadata( $record->as_xml_record($encoding) );
+ $m_rs->store;
+ }
ModZebra( $biblionumber, "specialUpdate", "biblioserver", $record );
return $biblionumber;
}
return (@subscriptions);
}
-=head2 GetHolds
-
- $holds = &GetHolds($biblionumber);
-
-This function return the count of holds with $biblionumber
-
-=cut
-
-sub GetHolds {
- my ($biblionumber) = @_;
- my $dbh = C4::Context->dbh;
- my $query = "SELECT count(*)
- FROM reserves
- WHERE biblionumber=?";
- my $sth = $dbh->prepare($query);
- $sth->execute($biblionumber);
- my $holds = $sth->fetchrow;
- return ($holds);
-}
-
=head2 prepare_host_field
$marcfield = prepare_host_field( $hostbiblioitem, $marcflavour );