Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
97.83% |
45 / 46 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
ImportXslCommand | |
97.83% |
45 / 46 |
|
66.67% |
2 / 3 |
10 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
configure | |
100.00% |
27 / 27 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
94.12% |
16 / 17 |
|
0.00% |
0 / 1 |
8.01 |
1 | <?php |
2 | |
3 | /** |
4 | * Console command: XSLT importer |
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 | |
30 | namespace VuFindConsole\Command\Import; |
31 | |
32 | use Symfony\Component\Console\Attribute\AsCommand; |
33 | use Symfony\Component\Console\Command\Command; |
34 | use Symfony\Component\Console\Input\InputArgument; |
35 | use Symfony\Component\Console\Input\InputInterface; |
36 | use Symfony\Component\Console\Input\InputOption; |
37 | use Symfony\Component\Console\Output\OutputInterface; |
38 | use VuFind\XSLT\Importer; |
39 | |
40 | use function is_callable; |
41 | |
42 | /** |
43 | * Console command: XSLT importer |
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: 'import/import-xsl', |
53 | description: 'XSLT importer' |
54 | )] |
55 | class ImportXslCommand extends Command |
56 | { |
57 | /** |
58 | * XSLT importer |
59 | * |
60 | * @var Importer |
61 | */ |
62 | protected $importer; |
63 | |
64 | /** |
65 | * Constructor |
66 | * |
67 | * @param Importer $importer XSLT importer |
68 | * @param string|null $name The name of the command; passing null means it |
69 | * must be set in configure() |
70 | */ |
71 | public function __construct(Importer $importer, $name = null) |
72 | { |
73 | $this->importer = $importer; |
74 | parent::__construct($name); |
75 | } |
76 | |
77 | /** |
78 | * Configure the command. |
79 | * |
80 | * @return void |
81 | */ |
82 | protected function configure() |
83 | { |
84 | $this |
85 | ->setHelp('Indexes XML into Solr using XSLT.') |
86 | ->addArgument( |
87 | 'XML_file', |
88 | InputArgument::REQUIRED, |
89 | 'source file to index' |
90 | )->addArgument( |
91 | 'properties_file', |
92 | InputArgument::REQUIRED, |
93 | 'import configuration file ($VUFIND_LOCAL_DIR/import and ' |
94 | . ' $VUFIND_HOME/import will' |
95 | . "\nbe searched for this filename; see ojs.properties " |
96 | . 'for configuration examples)' |
97 | )->addOption( |
98 | 'test-only', |
99 | null, |
100 | InputOption::VALUE_NONE, |
101 | 'activates test mode, which displays transformed XML without ' |
102 | . 'updating Solr' |
103 | )->addOption( |
104 | 'index', |
105 | null, |
106 | InputOption::VALUE_OPTIONAL, |
107 | 'name of search backend to index content into (could be overridden ' |
108 | . "with,\nfor example, SolrAuth to index authority records)", |
109 | 'Solr' |
110 | ); |
111 | } |
112 | |
113 | /** |
114 | * Run the command. |
115 | * |
116 | * @param InputInterface $input Input object |
117 | * @param OutputInterface $output Output object |
118 | * |
119 | * @return int 0 for success |
120 | */ |
121 | protected function execute(InputInterface $input, OutputInterface $output) |
122 | { |
123 | $testMode = $input->getOption('test-only') ? true : false; |
124 | $index = $input->getOption('index'); |
125 | $xml = $input->getArgument('XML_file'); |
126 | $properties = $input->getArgument('properties_file'); |
127 | // Try to import the document if successful: |
128 | try { |
129 | $result = $this->importer->save($xml, $properties, $index, $testMode); |
130 | if ($testMode) { |
131 | $output->writeln($result); |
132 | } |
133 | } catch (\Exception $e) { |
134 | $output->writeln('Fatal error: ' . $e->getMessage()); |
135 | if (is_callable([$e, 'getPrevious']) && $e = $e->getPrevious()) { |
136 | while ($e) { |
137 | $output->writeln('Previous exception: ' . $e->getMessage()); |
138 | $e = $e->getPrevious(); |
139 | } |
140 | } |
141 | return 1; |
142 | } |
143 | if (!$testMode) { |
144 | $output->writeln("Successfully imported $xml..."); |
145 | } |
146 | return 0; |
147 | } |
148 | } |