Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (warning)
83.33%
30 / 36
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
SuppressedCommand
83.33% covered (warning)
83.33%
30 / 36
33.33% covered (danger)
33.33%
1 / 3
10.46
0.00% covered (danger)
0.00%
0 / 1
 configure
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
 writeToDisk
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
76.19% covered (warning)
76.19%
16 / 21
0.00% covered (danger)
0.00%
0 / 1
8.86
1<?php
2
3/**
4 * Console command: remove suppressed records from index
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\Input\InputInterface;
34use Symfony\Component\Console\Input\InputOption;
35use Symfony\Component\Console\Output\OutputInterface;
36
37use function is_array;
38
39/**
40 * Console command: remove suppressed records from index
41 *
42 * @category VuFind
43 * @package  Console
44 * @author   Demian Katz <demian.katz@villanova.edu>
45 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
46 * @link     https://vufind.org/wiki/development Wiki
47 */
48#[AsCommand(
49    name: 'util/suppressed',
50    description: 'Remove ILS-suppressed records from Solr'
51)]
52class SuppressedCommand extends AbstractSolrAndIlsCommand
53{
54    /**
55     * Configure the command.
56     *
57     * @return void
58     */
59    protected function configure()
60    {
61        $this
62            ->setHelp(
63                'This tool removes ILS-suppressed records from Solr.'
64            )->addOption(
65                'authorities',
66                null,
67                InputOption::VALUE_NONE,
68                'Delete authority records instead of bibliographic records'
69            )->addOption(
70                'outfile',
71                null,
72                InputOption::VALUE_REQUIRED,
73                'Write the ID list to the specified file instead of updating Solr'
74            );
75    }
76
77    /**
78     * Write content to disk.
79     *
80     * @param string $filename Target filename
81     * @param string $content  Content to write
82     *
83     * @return bool
84     */
85    protected function writeToDisk($filename, $content)
86    {
87        return file_put_contents($filename, $content);
88    }
89
90    /**
91     * Run the command.
92     *
93     * @param InputInterface  $input  Input object
94     * @param OutputInterface $output Output object
95     *
96     * @return int 0 for success
97     */
98    protected function execute(InputInterface $input, OutputInterface $output)
99    {
100        // Setup Solr Connection
101        $backend = $input->getOption('authorities') ? 'SolrAuth' : 'Solr';
102
103        // Make ILS Connection
104        try {
105            $result = ($backend == 'SolrAuth')
106                ? $this->catalog->getSuppressedAuthorityRecords()
107                : $this->catalog->getSuppressedRecords();
108        } catch (\Exception $e) {
109            $output->writeln('ILS error -- ' . $e->getMessage());
110            return 1;
111        }
112
113        // Validate result:
114        if (!is_array($result)) {
115            $output->writeln('Could not obtain suppressed record list from ILS.');
116            return 1;
117        } elseif (empty($result)) {
118            $output->writeln('No suppressed records to delete.');
119            return 0;
120        }
121
122        // If 'outfile' set, write the list
123        if ($file = $input->getOption('outfile')) {
124            if (!$this->writeToDisk($file, implode("\n", $result))) {
125                $output->writeln("Problem writing to $file");
126                return 1;
127            }
128        } else {
129            // Default behavior: Delete from Solr index
130            $this->solr->deleteRecords($backend, $result);
131            $this->solr->commit($backend);
132            $this->solr->optimize($backend);
133        }
134        return 0;
135    }
136}