Koha Solutions knowledge base: run log of a live check Article https://kohasolutions.com/knowledge-base/koha-import-marc-records Check scripts/kb/live/koha-import-marc-records.mjs (SHA-256 16f9a2f0e8a772df1d71bbd404688ecac474f2be1b049d7cb1106fe7d80c6893) Ran 2026-11-05T14:29:40Z (UTC), 36 s Koha 26.05.02 (Koha::version 26.05.02.002, database version 26.0600002) Source https://git.koha-community.org/Koha-community/Koha/src/commit/d3fc229bc5eaea25dfe88f86149386408fe5d32d System Debian GNU/Linux 13 (trixie) Result PASS: all 40 claims held Each claim is something the article says. "seen" is what the check read back when it is not just a yes/no. Changes the check made to the test server were undone afterwards, as its header describes. == Claims == ok test file written (biblio 6 + one new record with an item) seen: made ok stage_file.pl prints "... staging MARC records -- please wait" seen: ... staging MARC records -- please wait ... processed 2 records ... finished staging MARC records MARC record staging report ------------------------------------ Input file: /tmp/kbv-import.mrc Record type: biblio Number of input records: 2 Number of valid records: ok stage_file.pl prints "... finished staging MARC records" seen: ... staging MARC records -- please wait ... processed 2 records ... finished staging MARC records MARC record staging report ------------------------------------ Input file: /tmp/kbv-import.mrc Record type: biblio Number of input records: 2 Number of valid records: ok stage_file.pl prints "MARC record staging report" seen: ... staging MARC records -- please wait ... processed 2 records ... finished staging MARC records MARC record staging report ------------------------------------ Input file: /tmp/kbv-import.mrc Record type: biblio Number of input records: 2 Number of valid records: ok stage_file.pl prints "Number of input records: 2" seen: ... staging MARC records -- please wait ... processed 2 records ... finished staging MARC records MARC record staging report ------------------------------------ Input file: /tmp/kbv-import.mrc Record type: biblio Number of input records: 2 Number of valid records: ok stage_file.pl prints "Number of valid records: 2" seen: ... staging MARC records -- please wait ... processed 2 records ... finished staging MARC records MARC record staging report ------------------------------------ Input file: /tmp/kbv-import.mrc Record type: biblio Number of input records: 2 Number of valid records: ok stage_file.pl prints "Number of invalid records: 0" seen: ... staging MARC records -- please wait ... processed 2 records ... finished staging MARC records MARC record staging report ------------------------------------ Input file: /tmp/kbv-import.mrc Record type: biblio Number of input records: 2 Number of valid records: ok stage_file.pl prints "Incoming records not matched against existing records (--match option not supplied)" seen: ... staging MARC records -- please wait ... processed 2 records ... finished staging MARC records MARC record staging report ------------------------------------ Input file: /tmp/kbv-import.mrc Record type: biblio Number of input records: 2 Number of valid records: ok stage_file.pl prints "Number of items parsed: 1" seen: ... staging MARC records -- please wait ... processed 2 records ... finished staging MARC records MARC record staging report ------------------------------------ Input file: /tmp/kbv-import.mrc Record type: biblio Number of input records: 2 Number of valid records: ok stage_file.pl prints "Batch number assigned:" seen: ... staging MARC records -- please wait ... processed 2 records ... finished staging MARC records MARC record staging report ------------------------------------ Input file: /tmp/kbv-import.mrc Record type: biblio Number of input records: 2 Number of valid records: ok without --match, the copy of a record already in the catalogue is staged as no_match (commit would duplicate it) ok …and the batch is set to Add incoming record / Always add items ok --format iso2709 (lowercase) → "Can't use an undefined value as an ARRAY reference" seen: Can't use an undefined value as an ARRAY reference at /usr/share/koha/bin/stage_file.pl line 136. EXIT=11 ok with --match: "... looking for matches with records already in database" ok with --match: "Number of records matched: 1" seen: ---------------------------------- Input file: /tmp/kbv-import.mrc Record type: biblio Number of input records: 2 Number of valid records: 2 Number of invalid records: 0 Number of records matched: 1 Number of items parsed: 1 Batch number assigned: 37 EXIT=0 ok KohaBiblio prints the harmless "Invalid normalization routine required (Biblionumber)" warning ok with --match and no --no-replace the batch is set to replace matched records ok --list-batches header "# File name Batch comments Status" seen: # File name Batch comments Status ----- ------------------------- ------------------------- ---------- 8 KCMPMVVJEIX7-imp2-1.mrc koha-compat reverted ok --list-batches lists the staged batch ok --revert on a staged batch → "import batch N status is 'staged', and therefore cannot be imported" seen: /usr/share/koha/bin/commit_file.pl: import batch 37 status is 'staged', and therefore cannot be imported EXIT=11 ok commit_file.pl prints "... importing MARC records -- please wait" seen: ... importing MARC records -- please wait ... processed 2 records ... finished importing MARC records MARC record import report ---------------------------------------- Batch number: 37 Number of new records added: 1 Number of records replaced: 1 Number of records ignore ok commit_file.pl prints "... finished importing MARC records" seen: ... importing MARC records -- please wait ... processed 2 records ... finished importing MARC records MARC record import report ---------------------------------------- Batch number: 37 Number of new records added: 1 Number of records replaced: 1 Number of records ignore ok commit_file.pl prints "MARC record import report" seen: ... importing MARC records -- please wait ... processed 2 records ... finished importing MARC records MARC record import report ---------------------------------------- Batch number: 37 Number of new records added: 1 Number of records replaced: 1 Number of records ignore ok commit_file.pl prints "Number of new records added: 1" seen: ... importing MARC records -- please wait ... processed 2 records ... finished importing MARC records MARC record import report ---------------------------------------- Batch number: 37 Number of new records added: 1 Number of records replaced: 1 Number of records ignore ok commit_file.pl prints "Number of records replaced: 1" seen: ... importing MARC records -- please wait ... processed 2 records ... finished importing MARC records MARC record import report ---------------------------------------- Batch number: 37 Number of new records added: 1 Number of records replaced: 1 Number of records ignore ok commit_file.pl prints "Number of records ignored: 0" seen: ... importing MARC records -- please wait ... processed 2 records ... finished importing MARC records MARC record import report ---------------------------------------- Batch number: 37 Number of new records added: 1 Number of records replaced: 1 Number of records ignore ok commit_file.pl prints "Number of items added: 1" seen: ... importing MARC records -- please wait ... processed 2 records ... finished importing MARC records MARC record import report ---------------------------------------- Batch number: 37 Number of new records added: 1 Number of records replaced: 1 Number of records ignore ok commit_file.pl prints "Note: an item is ignored if its barcode is a" seen: ... importing MARC records -- please wait ... processed 2 records ... finished importing MARC records MARC record import report ---------------------------------------- Batch number: 37 Number of new records added: 1 Number of records replaced: 1 Number of records ignore ok --framework FA puts the NEW record in FA; the replaced biblio 6 keeps BKS ok an imported batch drops out of --list-batches ok committing it again → "status is 'imported', and therefore cannot be imported" ok commit_file.pl --revert prints "... reverting batch -- please wait" ok …then dies: "Can't call method "can_edit_items_from" on an undefined value at …/Koha/Item.pm" seen: ... reverting batch -- please wait Can't call method "can_edit_items_from" on an undefined value at /kohadevbox/koha/Koha/Item.pm line 374. EXIT=11 ok …leaving the batch at status "reverting" and the new record and item in the catalogue ok the staff screen shows Status: Reverting and no "Undo import into catalog" button seen: Circulation | Patrons | Search | More | koha Centerville | | Search catalog | Check out | Check in | Renew | Search catalog | Home | Cataloging | Manage staged MARC records | Batch 37 | Help | ok the koha-mysql UPDATE prints nothing and sets the batch back to imported ok Undo import into catalog (a background job) finishes the revert: status reverted seen: reverted ok …the record and item the batch created are gone, biblio 6 is still there ok the screen then shows Status: Reverted ok without koha-shell: "unable to locate Koha configuration file koha-conf.xml" seen: unable to locate Koha configuration file koha-conf.xml at /usr/share/koha/lib/C4/Context.pm line 159. Use of uninitialized value $file in exists at /usr/share/koha/lib/Koha/Config.pm line 78. Use of uninitialized value $file in hash element at /usr/share/koha/lib/Koha/Config.pm line 79. == The check that produced this log (scripts/kb/live/koha-import-marc-records.mjs) == /** * Live check for prisma/ks-kb/25-koha-import-marc-records.ts (plan203 §A2 #18). Writes a two-record ISO2709 file into * the container (a copy of sample biblio 6 "X Power Tools", which carries its own 999$c, plus a new record * "Kbverify import test" with one item, barcode KBVERIFYI1), then runs the command-line path the article shows: * stage without and with --match, commit, revert from the command line (which crashes in 26.05 when the batch added * items), the SQL recovery, and Undo import into catalog in the staff interface. Undone: every batch whose file is * /tmp/kbv-import.mrc is reverted (as user koha) and deleted, the Kbverify record and item are gone, the file removed. * The article shows /usr/share/koha/lib/Koha/Item.pm in the crash line (a package install); this dev container runs the * same file from /kohadevbox/koha, so the check asserts the message, not the path. * Biblio 6 is overlaid by an identical copy of itself and then put back by the revert. */ export const meta = { koha: '26.05.02', passed: '2026-11-05' } const FILE = '/tmp/kbv-import.mrc' const BARCODE = 'KBVERIFYI1' export default async function run(ctx) { const I = ctx.INSTANCE const perl = (code) => { ctx.sh(`cat > /tmp/kbv-imp.pl <<'PL'\n${code}\nPL\nchmod 644 /tmp/kbv-imp.pl`) const r = ctx.sh(`koha-shell ${I} -c "perl /tmp/kbv-imp.pl" 2>&1; rm -f /tmp/kbv-imp.pl`) return r.out.trim() } // Every command the article shows, run as the article runs it (koha-shell), stdout and stderr together. const ks = (script, args) => { const r = ctx.sh(`koha-shell -c "perl /usr/share/koha/bin/${script} ${args}" ${I} 2>&1; echo "EXIT=$?"`) return r.out } const batchOf = (out) => Number((out.match(/Batch number assigned:\s+(\d+)/) ?? [])[1] ?? 0) // Clean-up, also run first so a crashed earlier run cannot leave a batch behind. const cleanUp = () => { perl(`use C4::Context; use C4::ImportBatch qw(BatchRevertRecords CleanBatch DeleteBatch); C4::Context->set_userenv(51, 'koha', '', 'Koha', 'Admin', 'CPL', 'Centerville', 1); my $dbh = C4::Context->dbh; for my $id (@{ $dbh->selectcol_arrayref("SELECT import_batch_id FROM import_batches WHERE file_name = '${FILE}'") }) { $dbh->do("UPDATE import_batches SET import_status = 'imported' WHERE import_batch_id = ? AND import_status = 'reverting'", undef, $id); my ($s) = $dbh->selectrow_array("SELECT import_status FROM import_batches WHERE import_batch_id = ?", undef, $id); BatchRevertRecords($id) if $s eq 'imported'; CleanBatch($id); DeleteBatch($id); } for my $bn (@{ $dbh->selectcol_arrayref("SELECT biblionumber FROM biblio WHERE title = 'Kbverify import test'") }) { my $b = Koha::Biblios->find($bn); $_->delete for $b->items->as_list; C4::Biblio::DelBiblio($bn); } print "clean\\n";`) ctx.sh(`rm -f ${FILE}`) } cleanUp() ctx.restore('revert and delete every kbv-import batch, the Kbverify record and the file', cleanUp) const made = perl(`use Koha::Biblios; use MARC::Record; use MARC::Field; my $old = Koha::Biblios->find(6)->metadata->record; my $r = MARC::Record->new; $r->leader(' nam a22 7a 4500'); $r->encoding('UTF-8'); $r->append_fields(MARC::Field->new('020',' ',' ','a'=>'9780000000019'), MARC::Field->new('100','1',' ','a'=>'Kbverify, Author'), MARC::Field->new('245','1','0','a'=>'Kbverify import test'), MARC::Field->new('952',' ',' ','a'=>'CPL','b'=>'CPL','p'=>'${BARCODE}','y'=>'BK')); open my $fh, '>:raw', '${FILE}'; print $fh $old->as_usmarc, $r->as_usmarc; close $fh; print "made\\n";`) ctx.check('test file written (biblio 6 + one new record with an item)', made.endsWith('made'), made) // 1. Stage exactly as the article's first command does: no --match. const s1 = ks('stage_file.pl', `--file ${FILE} --format ISO2709 --encoding UTF-8 --add-items --comment \\"Kbverify supplier load\\"`) const b1 = batchOf(s1) for (const line of ['... staging MARC records -- please wait', '... finished staging MARC records', 'MARC record staging report', 'Number of input records: 2', 'Number of valid records: 2', 'Number of invalid records: 0', 'Incoming records not matched against existing records (--match option not supplied)', 'Number of items parsed: 1', 'Batch number assigned: ']) { ctx.check(`stage_file.pl prints "${line.trim()}"`, s1.includes(line), s1.slice(0, 300)) } ctx.check('without --match, the copy of a record already in the catalogue is staged as no_match (commit would duplicate it)', ctx.sqlValue(`SELECT COUNT(*) FROM import_records WHERE import_batch_id = ${b1} AND overlay_status = 'no_match'`) === '2') ctx.check('…and the batch is set to Add incoming record / Always add items', ctx.sql(`SELECT overlay_action, nomatch_action, item_action FROM import_batches WHERE import_batch_id = ${b1}`)[0]?.join('/') === 'create_new/create_new/always_add') // 2. A lowercase --format passes the check and then dies. const lc = ks('stage_file.pl', `--file ${FILE} --format iso2709`) ctx.check('--format iso2709 (lowercase) → "Can\'t use an undefined value as an ARRAY reference"', lc.includes("Can't use an undefined value as an ARRAY reference at /usr/share/koha/bin/stage_file.pl"), lc.slice(0, 200)) // 3. With --match (KohaBiblio, 999$c). const kohaBiblio = ctx.sqlValue("SELECT matcher_id FROM marc_matchers WHERE code = 'KohaBiblio'") const s2 = ks('stage_file.pl', `--file ${FILE} --match ${kohaBiblio} --add-items --comment \\"Kbverify supplier load\\"`) const b2 = batchOf(s2) ctx.check('with --match: "... looking for matches with records already in database"', s2.includes('... looking for matches with records already in database')) ctx.check('with --match: "Number of records matched: 1"', s2.includes('Number of records matched: 1'), s2.slice(-300)) ctx.check('KohaBiblio prints the harmless "Invalid normalization routine required (Biblionumber)" warning', s2.includes('Invalid normalization routine required (Biblionumber)')) ctx.check('with --match and no --no-replace the batch is set to replace matched records', ctx.sqlValue(`SELECT overlay_action FROM import_batches WHERE import_batch_id = ${b2}`) === 'replace') // 4. --list-batches lists staged batches; a revert of a staged batch is refused. const l1 = ks('commit_file.pl', '--list-batches') ctx.check('--list-batches header "# File name Batch comments Status"', l1.includes('# File name Batch comments Status'), l1.slice(0, 200)) ctx.check('--list-batches lists the staged batch', new RegExp(`\\b${b2} /tmp/kbv-import.mrc\\s+Kbverify supplier load\\s+staged`).test(l1)) const r0 = ks('commit_file.pl', `--revert --batch-number ${b2}`) ctx.check(`--revert on a staged batch → "import batch N status is 'staged', and therefore cannot be imported"`, r0.includes(`import batch ${b2} status is 'staged', and therefore cannot be imported`) && r0.includes('EXIT=11'), r0) // 5. Commit. const c = ks('commit_file.pl', `--batch-number ${b2} --framework FA`) for (const line of ['... importing MARC records -- please wait', '... finished importing MARC records', 'MARC record import report', 'Number of new records added: 1', 'Number of records replaced: 1', 'Number of records ignored: 0', 'Number of items added: 1', 'Note: an item is ignored if its barcode is a']) { ctx.check(`commit_file.pl prints "${line.trim()}"`, c.includes(line), c.slice(0, 300)) } ctx.check('--framework FA puts the NEW record in FA; the replaced biblio 6 keeps BKS', ctx.sqlValue("SELECT frameworkcode FROM biblio WHERE title = 'Kbverify import test'") === 'FA' && ctx.sqlValue('SELECT frameworkcode FROM biblio WHERE biblionumber = 6') === 'BKS') const l2 = ks('commit_file.pl', '--list-batches') ctx.check('an imported batch drops out of --list-batches', !new RegExp(`\\b${b2} /tmp/kbv-import.mrc`).test(l2)) const c2 = ks('commit_file.pl', `--batch-number ${b2}`) ctx.check(`committing it again → "status is 'imported', and therefore cannot be imported"`, c2.includes(`import batch ${b2} status is 'imported', and therefore cannot be imported`)) // 6. Revert from the command line: crashes on the item, leaves the batch at "reverting". const rv = ks('commit_file.pl', `--revert --batch-number ${b2}`) ctx.check('commit_file.pl --revert prints "... reverting batch -- please wait"', rv.includes('... reverting batch -- please wait')) ctx.check(`…then dies: "Can't call method "can_edit_items_from" on an undefined value at …/Koha/Item.pm"`, rv.includes(`Can't call method "can_edit_items_from" on an undefined value`) && rv.includes('EXIT=11'), rv) ctx.check('…leaving the batch at status "reverting" and the new record and item in the catalogue', ctx.sqlValue(`SELECT import_status FROM import_batches WHERE import_batch_id = ${b2}`) === 'reverting' && ctx.sqlValue(`SELECT COUNT(*) FROM items WHERE barcode = '${BARCODE}'`) === '1') const staff = await ctx.newPage() await ctx.staffLogin(staff) const url = `${ctx.STAFF}/cgi-bin/koha/tools/manage-marc-import.pl?import_batch_id=${b2}` await staff.goto(url) let t = await ctx.pageText(staff) ctx.check('the staff screen shows Status: Reverting and no "Undo import into catalog" button', t.includes('Reverting') && (await staff.locator('input[value="Undo import into catalog"]').count()) === 0, t.slice(0, 200)) // 7. The recovery the article gives: set the batch back to imported, then Undo in the staff interface. const fix = ctx.sh(`koha-mysql ${I} -e "UPDATE import_batches SET import_status = 'imported' WHERE import_batch_id = ${b2} AND import_status = 'reverting'" 2>&1`) ctx.check('the koha-mysql UPDATE prints nothing and sets the batch back to imported', fix.out.trim() === '' && ctx.sqlValue(`SELECT import_status FROM import_batches WHERE import_batch_id = ${b2}`) === 'imported', fix.out) await staff.goto(url) staff.removeAllListeners('dialog') staff.on('dialog', (d) => d.accept()) await staff.locator('input[value="Undo import into catalog"]').click() await staff.waitForLoadState() let status = '' for (let i = 0; i < 30 && status !== 'reverted'; i++) { await new Promise((r) => setTimeout(r, 2000)) status = ctx.sqlValue(`SELECT import_status FROM import_batches WHERE import_batch_id = ${b2}`) } ctx.check('Undo import into catalog (a background job) finishes the revert: status reverted', status === 'reverted', status) ctx.check('…the record and item the batch created are gone, biblio 6 is still there', ctx.sqlValue("SELECT COUNT(*) FROM biblio WHERE title = 'Kbverify import test'") === '0' && ctx.sqlValue(`SELECT COUNT(*) FROM items WHERE barcode = '${BARCODE}'`) === '0' && ctx.sqlValue('SELECT COUNT(*) FROM biblio WHERE biblionumber = 6') === '1') await staff.goto(url) t = await ctx.pageText(staff) ctx.check('the screen then shows Status: Reverted', t.includes('Reverted')) // 8. Run outside koha-shell (no KOHA_CONF): the article's koha-conf.xml warning. const bare = ctx.sh('env -u KOHA_CONF -u PERL5LIB perl -I/usr/share/koha/lib /usr/share/koha/bin/commit_file.pl --list-batches 2>&1 | head -3').out ctx.check('without koha-shell: "unable to locate Koha configuration file koha-conf.xml"', bare.includes('unable to locate Koha configuration file koha-conf.xml'), bare) }