Koha::Metadata - Koha Metadata Object class
Metadata specific store method to catch errant characters prior to committing to the database.
If the MARCXML cannot be parsed but can be recovered,
the recovered version is saved and an error row per fault is written to biblio_metadata_errors so callers and the UI can notify the user.
Two recoverable faults are currently handled,
and either or both may apply to the same record:
nonxml_stripped,
recovered via StripNonXmlChars)MARC::File::XML refuses to parse (recorded as empty_datafield_stripped,
recovered by removing the empty datafield)Any pre-existing error row is left alone when the record parses cleanly - they are review flags requiring explicit human resolution and should not be silently cleared by a routine re-save.
If the MARCXML cannot be recovered at all, a Koha::Exceptions::Metadata::Invalid exception is thrown.
my $errors = $metadata->metadata_errors;
Returns a Koha::Biblio::Metadata::Errors resultset for errors associated with this metadata record.
$self->_add_error_once( $error_type, $message, $occurrence );
Records a biblio_metadata_errors row for this metadata record, unless an identical row (same error_type and message) already exists - so re-triggering the same fault on a source record that stays dirty across repeated saves doesn't pile up duplicate rows. $occurrence is the optional occurrence hashref produced by _find_nonxml_chars/_find_empty_datafields; when its field key looks like TAG$sub or TAG, the tag/subfield columns are populated so callers (e.g. the cataloguing UI) don't need to parse them back out of the free-text message.
my ( $tag, $subfield ) = _split_field_ref( $field_ref );
Splits a human-readable field reference such as 245$a into its tag and subfield parts. Field references with no subfield (control fields, or a datafield reported as a whole e.g. from _find_empty_datafields) return an undefined subfield.
my $record = $metadata->record;
Returns an object representing the metadata record. The expected record type corresponds to this table:
-------------------------------
| format | object type |
-------------------------------
| marcxml | MARC::Record |
-------------------------------
$record = $biblio->metadata->record({
{
embed_items => 0|1
itemnumbers => $itemnumbers,
opac => $opac
}
);
Koha::Biblio::Metadata::record(
{
record => $record,
embed_items => 1,
biblionumber => $biblionumber,
itemnumbers => $itemnumbers,
opac => $opac
}
);
Given a MARC::Record object containing a bib record, modify it to include the items attached to it as 9XX per the bib's MARC framework. if $itemnumbers is defined, only specified itemnumbers are embedded.
If $opac is true, then opac-relevant suppressions are included.
If opac filtering will be done, patron should be passed to properly override if necessary.
object_messages, so later reads find clean metadata. If the repair cannot be saved the repaired record is still returned. if ( $metadata->source_allows_editing ) { ... }
Returns a boolean denoting whether the metadata's record source allows it to be edited.
my $record_source = $metadata->record_source;
Returns a Koha::RecordSource object for the linked record source.
my $snippet = _context_snippet( \@chars, $pos_0, $char_ord );
Given an array-ref of characters \@chars, a 0-based position $pos_0 of a bad character, and its ordinal $char_ord, returns a two-line string:
␡ for DEL, ? otherwise) and ellipses when the window is truncated.^) aligned beneath the visible glyph.Example output (4-space indent):
t $xtac arr␈\ $btxt V $2rdacontent
^
my ( $record, $repaired_xml, $recoveries ) =
Koha::Biblio::Metadata->repair_marcxml( $marcxml, $marc_schema );
Attempts to repair a MARCXML string that fails to parse, by stripping whichever of the known-recoverable faults (non-XML characters, subfield-less datafields) are present. This is the same recovery logic used by store, exposed as a class method so other callers that parse MARCXML ahead of a Koha::Biblio::Metadata save - such as Koha::Import::Record staging records - benefit from the same repair capability instead of re-implementing a weaker version of it.
Returns, on success, the repaired MARC::Record, the repaired XML string, and a hashref of error_type => \@occurrences (see _find_nonxml_chars/_find_empty_datafields) describing what was fixed. Returns an empty list if the XML has none of the known-recoverable faults, or if it still doesn't parse once they're stripped.
my @occurrences = _find_nonxml_chars( $marcxml_string );
Scans a raw MARC XML string for every individual character that is illegal in XML 1.0. Returns a list of hashrefs (one per character occurrence) with the following keys:
field - human-readable field reference, e.g. 245$a or 001char_ord - ordinal (decimal) value of the bad characterposition - 1-based character offset within the field valuesnippet - two-line string with context window and caret pointer (see _context_snippet)The same character class is used as "StripNonXmlChars" in C4::Charset, so every occurrence returned here is exactly one that would be stripped.
my @occurrences = _find_empty_datafields( $marcxml_string );
Scans a raw MARC XML string for <datafield> elements with no <subfield> children at all. MARC::File::XML refuses to parse such a field (it dies with "Field NNN must have at least one subfield"), which happens with legacy data saved before this validation existed.
Returns a list of hashrefs (one per occurrence) with the single key field, the datafield's tag.
my $cleaned_marcxml = _strip_empty_datafields( $marcxml_string );
Removes every <datafield> element with no <subfield> children from a raw MARC XML string, so the record can be parsed and saved. The removed field's data cannot be recovered - callers should record an error row (see _find_empty_datafields) so a cataloguer can review and re-add it.
my $formatter = _error_message_formatter( $error_type );
my $message = $formatter->( $occurrence );
Returns a coderef that turns one occurrence hashref (as produced by _find_nonxml_chars or _find_empty_datafields) into a human-readable message line for the given $error_type.