Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 66
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
create_schema_file
0.00% covered (danger)
0.00%
0 / 66
0.00% covered (danger)
0.00%
0 / 4
110
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 / 38
0.00% covered (danger)
0.00%
0 / 1
56
 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_database\task;
15
16use phpbb\db\doctrine\connection_factory;
17use phpbb\install\exception\resource_limit_reached_exception;
18
19/**
20 * Create database schema
21 */
22class create_schema_file extends \phpbb\install\task_base
23{
24    /**
25     * @var \phpbb\install\helper\config
26     */
27    protected $config;
28
29    /**
30     * @var \phpbb\db\driver\driver_interface
31     */
32    protected $db;
33
34    /**
35     * @var \Doctrine\DBAL\Connection
36     */
37    protected $db_doctrine;
38
39    /**
40     * @var \phpbb\filesystem\filesystem_interface
41     */
42    protected $filesystem;
43
44    /**
45     * @var string
46     */
47    protected $phpbb_root_path;
48
49    /**
50     * @var string
51     */
52    protected $php_ext;
53
54    /**
55     * @var bool
56     */
57    protected $finder_cache;
58
59    /**
60     * Constructor
61     *
62     * @param \phpbb\install\helper\config                            $config                Installer's config provider
63     * @param \phpbb\install\helper\database                        $db_helper            Installer's database helper
64     * @param \phpbb\filesystem\filesystem_interface                $filesystem            Filesystem service
65     * @param string                                                $phpbb_root_path    Path phpBB's root
66     * @param string                                                $php_ext            Extension of PHP files
67     * @param bool                                                    $finder_cache        Flag whether to cache finder
68     */
69    public function __construct(\phpbb\install\helper\config $config,
70                                \phpbb\install\helper\database $db_helper,
71                                \phpbb\filesystem\filesystem_interface $filesystem,
72                                $phpbb_root_path,
73                                $php_ext,
74                                $finder_cache)
75    {
76        $dbms = $db_helper->get_available_dbms($config->get('dbms'));
77        $dbms = $dbms[$config->get('dbms')]['DRIVER'];
78
79        $this->db                = new $dbms();
80        $this->db->sql_connect(
81            $config->get('dbhost'),
82            $config->get('dbuser'),
83            $config->get('dbpasswd'),
84            $config->get('dbname'),
85            $config->get('dbport'),
86            false,
87            false
88        );
89
90        $this->db_doctrine = connection_factory::get_connection_from_params(
91            $config->get('dbms'),
92            $config->get('dbhost'),
93            $config->get('dbuser'),
94            $config->get('dbpasswd'),
95            $config->get('dbname'),
96            $config->get('dbport')
97        );
98
99        $this->config            = $config;
100        $this->filesystem        = $filesystem;
101        $this->phpbb_root_path    = $phpbb_root_path;
102        $this->php_ext            = $php_ext;
103        $this->finder_cache        = $finder_cache;
104
105        parent::__construct(true);
106    }
107
108    /**
109     * {@inheritdoc}
110     */
111    public function run()
112    {
113        // Generate database schema
114        if ($this->filesystem->exists($this->phpbb_root_path . 'install/schemas/schema.json'))
115        {
116            $db_table_schema = @file_get_contents($this->phpbb_root_path . 'install/schemas/schema.json');
117            $this->config->set('change_table_prefix', true);
118        }
119        else
120        {
121            global $table_prefix;
122
123            // As this task may take a large amount of time to complete refreshing the page might be necessary for some
124            // server configurations with limited resources
125            if (!$this->config->get('pre_schema_forced_refresh', false))
126            {
127                if ($this->config->get_time_remaining() < 5)
128                {
129                    $this->config->set('pre_schema_forced_refresh', true);
130                    throw new resource_limit_reached_exception();
131                }
132            }
133
134            $table_prefix = $this->config->get('table_prefix');
135
136            if (!defined('CONFIG_TABLE'))
137            {
138                // We need to include the constants file for the table constants
139                // when we generate the schema from the migration files.
140                include ($this->phpbb_root_path . 'includes/constants.' . $this->php_ext);
141            }
142
143            $finder_factory = new \phpbb\finder\factory(null, $this->finder_cache, $this->phpbb_root_path, $this->php_ext);
144            $finder = $finder_factory->get();
145            $migrator_classes = $finder->core_path('phpbb/db/migration/data/')->get_classes();
146            $factory = new \phpbb\db\tools\factory();
147            $db_tools = $factory->get($this->db_doctrine, true);
148            $tables_data = \Symfony\Component\Yaml\Yaml::parseFile($this->phpbb_root_path . '/config/default/container/tables.yml');
149            $tables = [];
150            foreach ($tables_data['parameters'] as $parameter => $table)
151            {
152                $tables[str_replace('tables.', '', (string) $parameter)] = str_replace('%core.table_prefix%', $table_prefix, $table);
153            }
154
155            $schema_generator = new \phpbb\db\migration\schema_generator(
156                $migrator_classes,
157                new \phpbb\config\config(array()),
158                $this->db,
159                $db_tools,
160                $this->phpbb_root_path,
161                $this->php_ext,
162                $table_prefix,
163                $tables
164            );
165            $db_table_schema = $schema_generator->get_schema();
166            $db_table_schema = json_encode($db_table_schema, JSON_PRETTY_PRINT);
167
168            $this->config->set('change_table_prefix', false);
169        }
170
171        $fp = @fopen($this->phpbb_root_path . 'store/schema.json', 'wb');
172        if (!$fp)
173        {
174            throw new \Exception('INST_SCHEMA_FILE_NOT_WRITABLE');
175        }
176
177        fwrite($fp, $db_table_schema);
178        fclose($fp);
179    }
180
181    /**
182     * {@inheritdoc}
183     */
184    public static function get_step_count()
185    {
186        return 1;
187    }
188
189    /**
190     * {@inheritdoc}
191     */
192    public function get_task_lang_name()
193    {
194        return 'TASK_CREATE_DATABASE_SCHEMA_FILE';
195    }
196}