Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
86.49% covered (warning)
86.49%
32 / 37
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CreateHierarchyTreesCommand
86.49% covered (warning)
86.49%
32 / 37
66.67% covered (warning)
66.67%
2 / 3
7.12
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 configure
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
 execute
80.00% covered (warning)
80.00%
20 / 25
0.00% covered (danger)
0.00%
0 / 1
5.20
1<?php
2
3/**
4 * Generic base class for Solr commands.
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\Record\Loader;
38use VuFind\Search\Results\PluginManager;
39
40use function count;
41
42/**
43 * Generic base class for Solr commands.
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/createHierarchyTrees',
53    description: 'Cache populator for hierarchies'
54)]
55class CreateHierarchyTreesCommand extends Command
56{
57    /**
58     * Record loader
59     *
60     * @var Loader
61     */
62    protected $recordLoader;
63
64    /**
65     * Search results manager
66     *
67     * @var PluginManager
68     */
69    protected $resultsManager;
70
71    /**
72     * Constructor
73     *
74     * @param Loader        $loader  Record loader
75     * @param PluginManager $results Search results manager
76     * @param string|null   $name    The name of the command; passing null means it
77     * must be set in configure()
78     */
79    public function __construct(Loader $loader, PluginManager $results, $name = null)
80    {
81        $this->recordLoader = $loader;
82        $this->resultsManager = $results;
83        parent::__construct($name);
84    }
85
86    /**
87     * Configure the command.
88     *
89     * @return void
90     */
91    protected function configure()
92    {
93        $this
94            ->setHelp('Populates the hierarchy tree cache.')
95            ->addArgument(
96                'backend',
97                InputArgument::OPTIONAL,
98                'Search backend, e.g. ' . DEFAULT_SEARCH_BACKEND
99                . ' (default) or Search2',
100                DEFAULT_SEARCH_BACKEND
101            );
102    }
103
104    /**
105     * Run the command.
106     *
107     * @param InputInterface  $input  Input object
108     * @param OutputInterface $output Output object
109     *
110     * @return int 0 for success
111     */
112    protected function execute(InputInterface $input, OutputInterface $output)
113    {
114        $backendId = $input->getArgument('backend');
115        $hierarchies = $this->resultsManager->get($backendId)
116            ->getFullFieldFacets(['hierarchy_top_id']);
117        $list = $hierarchies['hierarchy_top_id']['data']['list'] ?? [];
118        foreach ($list as $hierarchy) {
119            $recordid = $hierarchy['value'];
120            $count = $hierarchy['count'];
121            if (empty($recordid)) {
122                continue;
123            }
124            $output->writeln(
125                "\tBuilding tree for " . $recordid . '... '
126                . number_format($count) . ' records'
127            );
128            try {
129                $driver = $this->recordLoader->load($recordid, $backendId);
130                // Only do this if the record is actually a hierarchy type record
131                if ($driver->getHierarchyType()) {
132                    $driver->getHierarchyDriver()->getTreeSource()->getJSON(
133                        $recordid,
134                        ['refresh' => true]
135                    );
136                }
137            } catch (\VuFind\Exception\RecordMissing $e) {
138                $output->writeln(
139                    'WARNING! - Caught exception: ' . $e->getMessage() . "\n"
140                );
141            }
142        }
143        $output->writeln(count($list) . ' files');
144
145        return 0;
146    }
147}