Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.67% covered (warning)
83.67%
41 / 49
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
create
83.67% covered (warning)
83.67%
41 / 49
66.67% covered (warning)
66.67%
2 / 3
8.28
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 configure
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 execute
77.14% covered (warning)
77.14%
27 / 35
0.00% covered (danger)
0.00%
0 / 1
6.43
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14namespace phpbb\console\command\searchindex;
15
16use phpbb\console\command\command;
17use phpbb\language\language;
18use phpbb\log\log;
19use phpbb\posting\post_helper;
20use phpbb\search\exception\no_search_backend_found_exception;
21use phpbb\search\search_backend_factory;
22use phpbb\search\state_helper;
23use phpbb\user;
24use Symfony\Component\Console\Command\Command as symfony_command;
25use Symfony\Component\Console\Input\InputArgument;
26use Symfony\Component\Console\Input\InputInterface;
27use Symfony\Component\Console\Output\OutputInterface;
28use Symfony\Component\Console\Style\SymfonyStyle;
29
30class create extends command
31{
32    /** @var language */
33    protected $language;
34
35    /** @var log */
36    protected $log;
37
38    /** @var post_helper */
39    protected $post_helper;
40
41    /** @var search_backend_factory */
42    protected $search_backend_factory;
43
44    /** @var state_helper */
45    protected $state_helper;
46
47    /**
48     * Construct method
49     *
50     * @param language                    $language
51     * @param log                        $log
52     * @param post_helper                $post_helper
53     * @param search_backend_factory    $search_backend_factory
54     * @param state_helper                $state_helper
55     * @param user                        $user
56     */
57    public function __construct(language $language, log $log, post_helper $post_helper, search_backend_factory $search_backend_factory, state_helper $state_helper, user $user)
58    {
59        $this->language = $language;
60        $this->log = $log;
61        $this->post_helper = $post_helper;
62        $this->search_backend_factory = $search_backend_factory;
63        $this->state_helper = $state_helper;
64
65        $this->language->add_lang(array('acp/common', 'acp/search'));
66
67        parent::__construct($user);
68    }
69
70    /**
71     * Sets the command name and description
72     *
73     * @return void
74     */
75    protected function configure()
76    {
77        $this->setName('searchindex:create')
78            ->setDescription($this->language->lang('CLI_DESCRIPTION_SEARCHINDEX_CREATE'))
79            ->addArgument(
80                'search-backend',
81                InputArgument::REQUIRED,
82                $this->language->lang('CLI_SEARCHINDEX_SEARCH_BACKEND_NAME')
83            );
84    }
85
86    /**
87     * Executes the command searchindex:create
88     *
89     * Create search index
90     *
91     * @param InputInterface    $input  The input stream used to get the options
92     * @param OutputInterface    $output The output stream, used to print messages
93     *
94     * @return int 0 if all is well, 1 if any errors occurred
95     */
96    protected function execute(InputInterface $input, OutputInterface $output): int
97    {
98        $io = new SymfonyStyle($input, $output);
99
100        $io->section($this->language->lang('CLI_DESCRIPTION_SEARCHINDEX_CREATE'));
101
102        $search_backend = $input->getArgument('search-backend');
103
104        try
105        {
106            $search = $this->search_backend_factory->get($search_backend);
107            $name = $search->get_name();
108        }
109        catch (no_search_backend_found_exception $e)
110        {
111            $io->error($this->language->lang('CLI_SEARCHINDEX_BACKEND_NOT_FOUND', $search_backend));
112            return symfony_command::FAILURE;
113        }
114
115        if ($this->state_helper->is_action_in_progress())
116        {
117            $io->error($this->language->lang('CLI_SEARCHINDEX_ACTION_IN_PROGRESS', $search_backend));
118            return symfony_command::FAILURE;
119        }
120
121        if (!$search->is_available())
122        {
123            $io->error($this->language->lang('CLI_SEARCHINDEX_BACKEND_NOT_AVAILABLE', $search_backend));
124            return symfony_command::FAILURE;
125        }
126
127        try
128        {
129            $progress = $this->create_progress_bar($this->post_helper->get_max_post_id(), $io, $output, true);
130            $progress->setMessage('');
131            $progress->start();
132
133            $this->state_helper->init($search->get_type(), 'create');
134
135            $counter = 0;
136            while (($status = $search->create_index($counter)) !== null)
137            {
138                $this->state_helper->update_counter($status['post_counter']);
139
140                $progress->setProgress($status['post_counter']);
141                $progress->setMessage($this->language->lang('CLI_ROWS_PER_SECOND', round($status['rows_per_second'], 2)));
142            }
143
144            $progress->finish();
145
146            $io->newLine(2);
147        }
148        catch (\Exception $e)
149        {
150            $this->state_helper->clear_state(); // Unexpected error, cancel action
151            $io->error($e->getMessage()); // Show also exception message like in acp
152            $io->error($this->language->lang('CLI_SEARCHINDEX_CREATE_FAILURE', $name));
153            return symfony_command::FAILURE;
154        }
155
156        $search->tidy();
157
158        $this->state_helper->clear_state();
159
160        $this->log->add('admin', ANONYMOUS, '', 'LOG_SEARCH_INDEX_CREATED', false, array($name));
161        $io->success($this->language->lang('CLI_SEARCHINDEX_CREATE_SUCCESS', $name));
162
163        return symfony_command::SUCCESS;
164    }
165}