Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
install_extensions
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 6
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
6
 run
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 execute_step
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
72
 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
 get_extensions
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
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_finish\task;
15
16use Doctrine\DBAL\Connection;
17use Doctrine\DBAL\Exception as DoctrineException;
18use phpbb\config\db;
19use phpbb\db\driver\driver_interface;
20use phpbb\install\database_task;
21use phpbb\install\helper\config;
22use phpbb\install\helper\container_factory;
23use phpbb\install\helper\database;
24use phpbb\install\helper\iohandler\iohandler_interface;
25use phpbb\install\sequential_task;
26
27/**
28 * Installs extensions that exist in ext folder upon install
29 */
30class install_extensions extends database_task
31{
32    use sequential_task;
33
34    /**
35     * @var config
36     */
37    protected $install_config;
38
39    /**
40     * @var iohandler_interface
41     */
42    protected $iohandler;
43
44    /**
45     * @var \phpbb\config\config
46     */
47    protected $config;
48
49    /**
50     * @var \phpbb\log\log_interface
51     */
52    protected $log;
53
54    /**
55     * @var \phpbb\user
56     */
57    protected $user;
58
59    /** @var \phpbb\extension\manager */
60    protected $extension_manager;
61
62    /** @var string Extension table */
63    protected $extension_table;
64
65    /** @var Connection */
66    protected $db;
67
68    /**
69     * Constructor
70     *
71     * @param container_factory        $container
72     * @param config                $install_config
73     * @param database                $db_helper
74     * @param iohandler_interface    $iohandler
75     */
76    public function __construct(
77        container_factory $container,
78        config $install_config,
79        database $db_helper,
80        iohandler_interface $iohandler)
81    {
82        $this->install_config    = $install_config;
83        $this->iohandler        = $iohandler;
84        $this->extension_table    = $container->get_parameter('tables.ext');
85        $this->db                = self::get_doctrine_connection($db_helper, $install_config);
86
87        $this->log                    = $container->get('log');
88        $this->config                = $container->get('config');
89        $this->user                    = $container->get('user');
90        $this->extension_manager     = $container->get('ext.manager');
91
92        /** @var driver_interface $db */
93        $db = $container->get('dbal.conn');
94
95        /** @var \phpbb\cache\driver\driver_interface $cache */
96        $cache = $container->get('cache.driver');
97        $cache->destroy('config');
98
99        global $config;
100        $config = new db(
101            $db,
102            $cache,
103            $container->get_parameter('tables.config')
104        );
105
106        // Make sure asset version exists in config. Otherwise we might try to
107        // insert the assets_version setting into the database and cause a
108        // duplicate entry error.
109        if (!$this->config->offsetExists('assets_version'))
110        {
111            $this->config->offsetSet('assets_version', 0);
112        }
113
114        parent::__construct(
115            $this->db,
116            $this->iohandler,
117            true
118        );
119    }
120
121    /**
122     * {@inheritdoc}
123     */
124    public function run()
125    {
126        $this->user->session_begin();
127        $this->user->setup(array('common', 'acp/common', 'cli'));
128        $all_available_extensions = $this->extension_manager->all_available();
129        $this->execute($this->install_config, $all_available_extensions);
130    }
131
132    /**
133     * {@inheritdoc}
134     */
135    protected function execute_step($key, $value) : void
136    {
137        $install_extensions = $this->iohandler->get_input('install-extensions', array());
138
139        if (!empty($install_extensions) && $install_extensions !== ['all'] && !in_array($key, $install_extensions))
140        {
141            return;
142        }
143
144        try
145        {
146            $extension = $this->extension_manager->get_extension($key);
147
148            if (!$extension->is_enableable())
149            {
150                $this->iohandler->add_log_message(array('CLI_EXTENSION_NOT_ENABLEABLE', $key));
151                return;
152            }
153
154            $this->extension_manager->enable($key);
155            $extensions = $this->get_extensions();
156
157            if (isset($extensions[$key]) && $extensions[$key]['ext_active'])
158            {
159                // Create log
160                $this->log->add('admin', ANONYMOUS, $this->user->ip, 'LOG_EXT_ENABLE', time(), array($key));
161                $this->iohandler->add_success_message(array('CLI_EXTENSION_ENABLE_SUCCESS', $key));
162            }
163            else
164            {
165                $this->iohandler->add_log_message(array('CLI_EXTENSION_ENABLE_FAILURE', $key));
166            }
167        }
168        catch (\Exception $e)
169        {
170            // Add fail log and continue
171            $this->iohandler->add_log_message(array('CLI_EXTENSION_ENABLE_FAILURE', $key));
172        }
173    }
174
175    /**
176     * {@inheritdoc}
177     */
178    public static function get_step_count()
179    {
180        return 1;
181    }
182
183    /**
184     * {@inheritdoc}
185     */
186    public function get_task_lang_name()
187    {
188        return 'TASK_INSTALL_EXTENSIONS';
189    }
190
191    /**
192     * Get extensions from database
193     *
194     * @return array List of extensions
195     */
196    private function get_extensions() : array
197    {
198        try
199        {
200            $extensions_row = $this->db->fetchAllAssociative('SELECT * FROM ' . $this->extension_table);
201        }
202        catch (DoctrineException $e)
203        {
204            $this->iohandler->add_error_message('INST_ERR_DB', $e->getMessage());
205            return [];
206        }
207
208        $extensions = [];
209        foreach ($extensions_row as $extension)
210        {
211            $extensions[$extension['ext_name']] = $extension;
212        }
213
214        ksort($extensions);
215
216        return $extensions;
217    }
218}