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 c7b2207b8cd347bb832839900d4a342cb042af11f8fc9c1d6a6f6fa07b356284) Ran 2026-11-08T07:18:18Z (UTC), 141 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 55 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: 45 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 45 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: 45 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: 45 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: 45 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: 45 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: 45 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: 45 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: 45 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: 45 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 45 | 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. ok server: the article has every command (rules query, load rules, stage, list, commit, revert, UPDATE) seen: sudo koha-mysql library -e "SELECT match | sudo koha-shell -c "perl -MC4::Installer | sudo koha-shell -c "perl /usr/share/koha | sudo koha-shell -c "perl /usr/share/koha | sudo koha-shell -c "perl /usr/share/koha | sudo koha-shell -c "perl /usr/share/koha | sudo koha-mysql library -e "UPDATE impor ok server: the box starts with Koha installed ok server: set-up: one record in the catalogue and a two-record /tmp/records.mrc seen: BN=1 ok server: a new install (the web installer's default choices) has no matching rules: the rules query prints nothing ok server: stage_file.pl --match 3 with no rule 3 prints the normal report, "Number of records matched: 0", and no warning seen: ... staging MARC records -- please wait ... processed 2 records ... finished staging MARC records ... looking for matches with records already in database ... processed 2 records ... finished looking for matches MARC record staging report ------------------------------------ Input file: /tmp/records.mrc Record type: biblio Number of input records: 2 Number of valid records: 2 Number of invalid records: 0 Number of records matched: 0 Number of items parsed: 1 Batch number assigned: 1 ok server: (set-up) that batch is thrown away, not committed seen: dropped ok server: the article's load_sql command prints "done" seen: done ok server: the rules query then shows 1 ISBN, 2 ISSN, 3 KohaBiblio (the article's table) seen: +------------+------------+ | matcher_id | code | +------------+------------+ | 1 | ISBN | | 2 | ISSN | | 3 | KohaBiblio | +------------+------------+ ok server: stage_file.pl --match 3, exactly as the article prints it → the article's response (Matcher.pm at /usr/share/koha/lib/C4) seen: ... staging MARC records -- please wait ... processed 2 records ... finished staging MARC records ... looking for matches with records already in database Invalid normalization routine required (Biblionumber) at /usr/share/koha/lib/C4/Matcher.pm line 1014. ... processed 2 records ... finished looking for matches MARC record staging report ------------------------------------ Input file: /tmp/records.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: 2 ok server: commit_file.pl --list-batches → the article's header and the staged batch seen: " # File name Batch comments Status\n----- ------------------------- ------------------------- ----------\n 2 /tmp/records.mrc Supplier load, August staged" ok server: commit_file.pl --batch-number N --framework FA → the article's report (1 added, 1 replaced, 1 item added) seen: ... importing MARC records -- please wait ... processed 2 records ... finished importing MARC records MARC record import report ---------------------------------------- Batch number: 2 Number of new records added: 1 Number of records replaced: 1 Number of records ignored: 0 Number of items added: 1 Number of items replaced: 0 Number of items ignored: 0 Note: an item is ignored if its barcode is a duplicate of one already in the database. ok server: commit_file.pl --revert → the article's "Can't call method "can_edit_items_from" on an undefined value at /usr/share/koha/lib/Koha/Item.pm line …" seen: ... reverting batch -- please wait Can't call method "can_edit_items_from" on an undefined value at /usr/share/koha/lib/Koha/Item.pm line 375. ok server: …and the batch is left at "reverting" seen: reverting ok server: the koha-mysql UPDATE prints nothing and the batch is "imported" again seen: / imported ok server: run 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 163. 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. * On a real server (plan212, 2026-11-08): a fresh Ubuntu 24.04 with Koha installed as our installation guide leaves * it, as the sudo user `librarian` over SSH. The article's five Terminal blocks are read from the article file and run * exactly as printed (so the command a reader copies is the command tested); each response must equal the block's * own, the batch number aside. Set-up (not shown): one record in the catalogue and a two-record /tmp/records.mrc (a * copy of it with its 999$c, plus a new record with one item). The Undo button stays a screen step (local live Koha). */ import { readFileSync } from 'node:fs' import { createBox, ensureKohaImage, KOHA_IMAGE, removeBox, ssh } from './server-box/box.mjs' 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) // 9. The same commands on a real server (plan212). await serverPart(ctx) } /** * Every command in the article's Terminal blocks, in order, as [command, response]: a line starting "$ " is a command * (a trailing backslash continues it on the next line, as in a shell); the lines up to the next "$ " are its response. */ function articleCommands() { const src = readFileSync(new URL('../../../prisma/ks-kb/25-koha-import-marc-records.ts', import.meta.url), 'utf8') const out = [] for (const m of src.matchAll(/label: 'Terminal',\n\s+code: ('(?:[^'\\]|\\.)*')/g)) { const lines = new Function(`return ${m[1]}`)().split('\n') for (let i = 0; i < lines.length; i++) { if (!lines[i].startsWith('$ ')) continue let cmd = lines[i].slice(2) while (cmd.endsWith('\\') && i + 1 < lines.length) cmd += `\n${lines[++i]}` const resp = [] while (i + 1 < lines.length && !lines[i + 1].startsWith('$ ')) resp.push(lines[++i]) out.push([cmd, resp.join('\n').replace(/^\n+/, '')]) } } return out } async function serverPart(ctx) { const cmds = articleCommands() const find = (start) => cmds.find(([c]) => c.startsWith(start)) ?? ['', ''] const [rulesCmd] = find('sudo koha-mysql library -e "SELECT matcher_id') const [loadCmd, loadWant] = find('sudo koha-shell -c "perl -MC4::Installer') const rulesWant = cmds.filter(([c]) => c === rulesCmd)[1]?.[1] ?? '' const [stageCmd, stageWant] = find('sudo koha-shell -c "perl /usr/share/koha/bin/stage_file.pl') const [listCmd, listWant] = find('sudo koha-shell -c "perl /usr/share/koha/bin/commit_file.pl \\\n --list-batches') const [commitCmd, commitWant] = find('sudo koha-shell -c "perl /usr/share/koha/bin/commit_file.pl \\\n --batch-number') const [revertCmd, revertWant] = find('sudo koha-shell -c "perl /usr/share/koha/bin/commit_file.pl \\\n --revert') const [fixCmd] = find('sudo koha-mysql library -e "UPDATE import_batches') const all = [rulesCmd, loadCmd, stageCmd, listCmd, commitCmd, revertCmd, fixCmd] ctx.check('server: the article has every command (rules query, load rules, stage, list, commit, revert, UPDATE)', all.every(Boolean), all.map((c) => c.slice(0, 40)).join(' | ')) if (!all.every(Boolean)) return const err = ensureKohaImage() ?? createBox({ image: KOHA_IMAGE }) ctx.check('server: the box starts with Koha installed', err === null, err ?? '') if (err) return try { const L = (cmd) => ssh('librarian', cmd).replace(/\s+$/, '') const sqlv = (q) => L(`sudo koha-mysql library -N -B -e "${q}"`) await new Promise((r) => setTimeout(r, 10000)) // Set-up: one record already in the catalogue, and the file: a copy of it (with its 999$c) + a new record with an item. const fixture = `use Modern::Perl; use C4::Biblio; use MARC::Record; use MARC::Field; use Koha::Biblios; my $r = MARC::Record->new; $r->leader(' nam a22 7a 4500'); $r->append_fields(MARC::Field->new('245', '1', '0', a => 'Already in the catalogue')); my ($bn) = C4::Biblio::AddBiblio($r, ''); my $copy = Koha::Biblios->find($bn)->metadata->record; my $new = MARC::Record->new; $new->leader(' nam a22 7a 4500'); $new->append_fields(MARC::Field->new('245', '1', '0', a => 'New from the supplier'), MARC::Field->new('952', ' ', ' ', a => 'MAIN', b => 'MAIN', p => 'SUP-0001', y => 'BK')); open my $fh, '>:raw', '/tmp/records.mrc' or die; print $fh $copy->as_usmarc, $new->as_usmarc; close $fh; chmod 0644, '/tmp/records.mrc'; print "BN=$bn\\n";` const made = L(`echo ${Buffer.from(fixture).toString('base64')} | base64 -d > /tmp/kbv.pl && chmod 644 /tmp/kbv.pl && sudo koha-shell library -c "perl /tmp/kbv.pl"; rm -f /tmp/kbv.pl`) ctx.check('server: set-up: one record in the catalogue and a two-record /tmp/records.mrc', /BN=\d+/.test(made), made.slice(-200)) // 1. A server installed with the web installer's defaults has no matching rule, and --match 3 is then silent. const none = L(rulesCmd) ctx.check('server: a new install (the web installer\'s default choices) has no matching rules: the rules query prints nothing', none === '', none) const silent = L(stageCmd) const silentN = (silent.match(/Batch number assigned:\s+(\d+)/) ?? [])[1] ctx.check('server: stage_file.pl --match 3 with no rule 3 prints the normal report, "Number of records matched: 0", and no warning', silent.includes('... looking for matches with records already in database') && silent.includes('Number of records matched: 0') && !/match(er|ing rule)[^\n]*(not|no such|invalid)/i.test(silent) && Boolean(silentN), silent) // set-up: that batch is thrown away (not committed: committing it would duplicate the record, as the article says) const drop = `use C4::ImportBatch qw(CleanBatch DeleteBatch); CleanBatch(${silentN}); DeleteBatch(${silentN}); print "dropped\\n";` const dropped = L(`echo ${Buffer.from(drop).toString('base64')} | base64 -d > /tmp/kbv.pl && chmod 644 /tmp/kbv.pl && sudo koha-shell library -c "perl /tmp/kbv.pl"; rm -f /tmp/kbv.pl`) ctx.check('server: (set-up) that batch is thrown away, not committed', dropped.endsWith('dropped'), dropped) // 2. The article's fix: load Koha's default rules. const load = L(loadCmd) ctx.check('server: the article\'s load_sql command prints "done"', load === loadWant, load) const rules = L(rulesCmd) ctx.check('server: the rules query then shows 1 ISBN, 2 ISSN, 3 KohaBiblio (the article\'s table)', rules === rulesWant, rules) // 3. The article's command-line path, each command exactly as printed. const stage = L(stageCmd) const n = (stage.match(/Batch number assigned:\s+(\d+)/) ?? [])[1] ?? '?' const B = (t) => t.replace(/batch-number 7\b/g, `batch-number ${n}`).replace(/import_batch_id = 7\b/g, `import_batch_id = ${n}`) .replace(/Batch number assigned: 7$/m, `Batch number assigned: ${n}`).replace(/^Batch number:( +)7$/m, (_, sp) => `Batch number:${sp}${n}`) .replace(/^ {4}7 \/tmp\/records\.mrc/m, `${n.padStart(5)} /tmp/records.mrc`) ctx.check('server: stage_file.pl --match 3, exactly as the article prints it → the article\'s response (Matcher.pm at /usr/share/koha/lib/C4)', stage === B(stageWant).replace('…/C4/Matcher.pm', '/usr/share/koha/lib/C4/Matcher.pm'), stage) const list = L(B(listCmd)) ctx.check('server: commit_file.pl --list-batches → the article\'s header and the staged batch', list === B(listWant), JSON.stringify(list)) const commit = L(B(commitCmd)) ctx.check('server: commit_file.pl --batch-number N --framework FA → the article\'s report (1 added, 1 replaced, 1 item added)', commit === B(commitWant), commit) const revert = L(B(revertCmd)) ctx.check('server: commit_file.pl --revert → the article\'s "Can\'t call method "can_edit_items_from" on an undefined value at /usr/share/koha/lib/Koha/Item.pm line …"', revert === B(revertWant), revert) const st = () => sqlv(`SELECT import_status FROM import_batches WHERE import_batch_id = ${n}`) ctx.check('server: …and the batch is left at "reverting"', st() === 'reverting', st()) const fix = L(B(fixCmd)) ctx.check('server: the koha-mysql UPDATE prints nothing and the batch is "imported" again', fix === '' && st() === 'imported', `${fix} / ${st()}`) const bare = L('perl -I/usr/share/koha/lib /usr/share/koha/bin/commit_file.pl --list-batches 2>&1 | head -3') ctx.check('server: run without koha-shell → "unable to locate Koha configuration file koha-conf.xml"', bare.includes('unable to locate Koha configuration file koha-conf.xml'), bare) } finally { removeBox() } }