Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
LintMarcCommand
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 configure
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3/**
4 * Console command: Lint MARC records.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2020.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Console
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development Wiki
28 */
29
30namespace VuFindConsole\Command\Util;
31
32use Symfony\Component\Console\Attribute\AsCommand;
33use Symfony\Component\Console\Command\Command;
34use Symfony\Component\Console\Input\InputArgument;
35use Symfony\Component\Console\Input\InputInterface;
36use Symfony\Component\Console\Output\OutputInterface;
37use VuFind\Marc\MarcCollectionFile;
38use VuFind\Marc\MarcLint;
39
40use function count;
41
42/**
43 * Console command: Lint MARC records.
44 *
45 * @category VuFind
46 * @package  Console
47 * @author   Demian Katz <demian.katz@villanova.edu>
48 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
49 * @link     https://vufind.org/wiki/development Wiki
50 */
51#[AsCommand(
52    name: 'util/lint_marc',
53    description: 'MARC validator'
54)]
55class LintMarcCommand extends Command
56{
57    /**
58     * Configure the command.
59     *
60     * @return void
61     */
62    protected function configure()
63    {
64        $this
65            ->setHelp('This command lets you validate MARC file contents.')
66            ->addArgument('filename', InputArgument::REQUIRED, 'MARC filename');
67    }
68
69    /**
70     * Run the command.
71     *
72     * @param InputInterface  $input  Input object
73     * @param OutputInterface $output Output object
74     *
75     * @return int 0 for success
76     */
77    protected function execute(InputInterface $input, OutputInterface $output)
78    {
79        $filename = $input->getArgument('filename');
80        $collection = new MarcCollectionFile($filename);
81        $linter = new MarcLint();
82        $i = 0;
83        foreach ($collection as $record) {
84            $i++;
85            $field001 = $record->getField('001') ?: 'undefined';
86            $output->writeln("Checking record $i (001 = $field001)...");
87            $warnings = $linter->checkRecord($record);
88            if (count($warnings) > 0) {
89                $output->writeln('Warnings: ' . implode("\n", $warnings));
90            }
91        }
92        return 0;
93    }
94}