Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
create_search_index
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 5
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 execute_step
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 get_step_count
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_task_lang_name
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
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\install\module\install_data\task;
15
16use Doctrine\DBAL\Exception;
17use phpbb\auth\auth;
18use phpbb\db\driver\driver_interface;
19use phpbb\db\tools\tools_interface;
20use phpbb\event\dispatcher;
21use phpbb\install\database_task;
22use phpbb\install\helper\config;
23use phpbb\install\helper\container_factory;
24use phpbb\install\helper\database;
25use phpbb\install\helper\iohandler\iohandler_interface;
26use phpbb\install\sequential_task;
27use phpbb\search\backend\fulltext_native;
28use phpbb\user;
29
30class create_search_index extends database_task
31{
32    use sequential_task;
33
34    /**
35     * @var auth
36     */
37    protected $auth;
38
39    /**
40     * @var \phpbb\config\config
41     */
42    protected $config;
43
44    /**
45     * @var \Doctrine\DBAL\Connection
46     */
47    protected $conn;
48
49    /**
50     * @var driver_interface
51     */
52    protected $db;
53
54    /**
55     * @var tools_interface
56     */
57    protected $db_tools;
58
59    /**
60     * @var config
61     */
62    protected $installer_config;
63
64    /**
65     * @var iohandler_interface
66     */
67    protected $iohandler;
68
69    /**
70     * @var dispatcher
71     */
72    protected $phpbb_dispatcher;
73
74    /**
75     * @var user
76     */
77    protected $user;
78
79    /**
80     * @var fulltext_native
81     */
82    protected $search_indexer;
83
84    /**
85     * @var string phpBB root path
86     */
87    protected $phpbb_root_path;
88
89    /**
90     * @var string PHP file extension
91     */
92    protected $php_ext;
93
94    /**
95     * @var string
96     */
97    protected $posts_table;
98
99    /**
100     * @var mixed
101     */
102    protected $error;
103
104    /**
105     * Constructor
106     *
107     * @param config $config Installer config.
108     * @param database $db_helper Database helper.
109     * @param container_factory $container Installer's DI container
110     * @param iohandler_interface $iohandler IO manager.
111     * @param string $phpbb_root_path phpBB root path
112     * @param string $php_ext PHP file extension
113     */
114    public function __construct(
115        config $config,
116        database $db_helper,
117        container_factory $container,
118        iohandler_interface $iohandler,
119        string $phpbb_root_path,
120        string $php_ext)
121    {
122        $this->conn = self::get_doctrine_connection($db_helper, $config);
123
124        $this->auth                = $container->get('auth');
125        $this->config            = $container->get('config');
126        $this->db                = $container->get('dbal.conn');
127        $this->db_tools            = $container->get('dbal.tools');
128        $this->iohandler        = $iohandler;
129        $this->installer_config    = $config;
130        $this->phpbb_dispatcher = $container->get('event_dispatcher');
131        $this->user             = $container->get('user');
132        $this->phpbb_root_path    = $phpbb_root_path;
133        $this->php_ext            = $php_ext;
134
135        $this->posts_table = $container->get_parameter('tables.posts');
136
137        $this->search_indexer = new fulltext_native(
138            $this->config,
139            $this->db,
140            $this->db_tools,
141            $this->phpbb_dispatcher,
142            $container->get('language'),
143            $this->user,
144            SEARCH_RESULTS_TABLE,
145            SEARCH_WORDLIST_TABLE,
146            SEARCH_WORDMATCH_TABLE,
147            $this->phpbb_root_path,
148            $this->php_ext
149        );
150
151        parent::__construct($this->conn, $iohandler, true);
152    }
153
154    /**
155     * {@inheritdoc}
156     */
157    public function run()
158    {
159        // Make sure fulltext native load update is set
160        $this->config->set('fulltext_native_load_upd', 1);
161
162        try
163        {
164            $sql = 'SELECT post_id, post_subject, post_text, poster_id, forum_id FROM ' . $this->posts_table;
165            $rows = $this->conn->fetchAllAssociative($sql);
166        }
167        catch (Exception $e)
168        {
169            $this->iohandler->add_error_message('INST_ERR_DB', $e->getMessage());
170            $rows = [];
171        }
172
173        $this->execute($this->installer_config, $rows);
174    }
175
176    /**
177     * {@inheritdoc}
178     */
179    protected function execute_step($key, $value) : void
180    {
181        $this->search_indexer->index(
182            'post',
183            (int) $value['post_id'],
184            $value['post_text'],
185            $value['post_subject'],
186            (int) $value['poster_id'],
187            (int) $value['forum_id']
188        );
189    }
190
191    /**
192     * {@inheritdoc}
193     */
194    public static function get_step_count() : int
195    {
196        return 1;
197    }
198
199    /**
200     * {@inheritdoc}
201     */
202    public function get_task_lang_name() : string
203    {
204        return 'TASK_CREATE_SEARCH_INDEX';
205    }
206}