Completed
Push — master ( b9fa67...e31f65 )
by Andreas
24:00 queued 04:20
created

reindex::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 1
rs 10
c 2
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.console
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
namespace midcom\console\command;
10
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use midcom;
15
use midcom_error;
16
use midcom_helper_nav;
17
use midcom_services_indexer_client;
18
use Symfony\Component\Console\Input\InputOption;
19
use GuzzleHttp\Exception\RequestException;
20
use Symfony\Component\DomCrawler\Crawler;
21
use midcom\console\loginhelper;
22
23
/**
24
 * Redinex command
25
 *
26
 * Drops the index, then iterates over all existing topics, retrieves the corresponding
27
 * interface class and invokes the reindexing.
28
 *
29
 * This may take some time.
30
 *
31
 * @package midcom.console
32
 */
33
class reindex extends Command
34
{
35
    use loginhelper;
0 ignored issues
show
Bug introduced by
The trait midcom\console\loginhelper requires the property $auth which is not provided by midcom\console\command\reindex.
Loading history...
36
37 1
    protected function configure()
38
    {
39 1
        $this->setName('midcom:reindex')
40 1
            ->setAliases(['reindex'])
41 1
            ->setDescription('Reindex')
42 1
            ->addOption('id', 'i', InputOption::VALUE_OPTIONAL, 'Start node (root if empty)');
43 1
    }
44
45
    protected function interact(InputInterface $input, OutputInterface $output)
46
    {
47
        if (midcom::get()->config->get('indexer_backend') === false) {
48
            throw new midcom_error('No indexer backend has been defined. Aborting.');
49
        }
50
51
        if (empty($input->getParameterOption(['--servername', '-s'], null))) {
52
            throw new midcom_error('Please specify host name (with --servername or -s)');
53
        }
54
55
        $dialog = $this->getHelperSet()->get('question');
56
        $this->require_admin($dialog, $input, $output);
57
    }
58
59
    protected function execute(InputInterface $input, OutputInterface $output) : int
60
    {
61
        $nap = new midcom_helper_nav();
62
        $nodes = [];
63
        $nodeid = $input->getOption('id') ?: $nap->get_root_node();
64
65
        while ($nodeid !== null) {
66
            // Reindex the node...
67
            $node = $nap->get_node($nodeid);
68
69
            $output->write("Processing Node #$nodeid, {$node[MIDCOM_NAV_FULLURL]}...");
70
            if (!midcom::get()->indexer->delete_all("__TOPIC_GUID:{$node[MIDCOM_NAV_OBJECT]->guid}")) {
71
                $output->writeln("\n<error>Failed to remove documents from index.</error>");
72
            }
73
            $interface = midcom::get()->componentloader->get_interface_class($node[MIDCOM_NAV_COMPONENT]);
74
            $stat = $interface->reindex($node[MIDCOM_NAV_OBJECT]);
75
            if (is_a($stat, midcom_services_indexer_client::class)) {
0 ignored issues
show
Bug introduced by
$stat of type true is incompatible with the type object|string expected by parameter $object of is_a(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
            if (is_a(/** @scrutinizer ignore-type */ $stat, midcom_services_indexer_client::class)) {
Loading history...
76
                try {
77
                    $stat->reindex();
78
                } catch (RequestException $e) {
79
                    if ($e->hasResponse()) {
80
                        $crawler = new Crawler($e->getResponse()->getBody()->getContents());
81
                        $body = $crawler->filter('body')->html();
82
                        $output->writeln("\n<error>" . strip_tags($body) . '</error>');
83
                    } else {
84
                        $output->writeln("\n<error>" . $e->getMessage() . '</error>');
85
                    }
86
                }
87
            } elseif ($stat === false) {
88
                $output->writeln("\n<error>Failed to reindex the node {$nodeid} which is of {$node[MIDCOM_NAV_COMPONENT]}.</error>");
89
            }
90
91
            // Retrieve all child nodes and append them to $nodes:
92
            $children = $nap->list_nodes($nodeid);
93
            if ($children === false) {
94
                throw new midcom_error("Failed to list the child nodes of {$nodeid}.");
95
            }
96
            $nodes = array_merge($nodes, $children);
97
            $nodeid = array_shift($nodes);
98
            $output->writeln("Done");
99
        }
100
101
        $output->writeln('Reindex complete');
102
        return 0;
103
    }
104
}
105