Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
add_default_data
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 7
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 execute_step
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
20
 replace_dbms_specific_sql
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
30
 lang_replace_callback
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 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 Doctrine\DBAL\Connection;
17use phpbb\install\database_task;
18use phpbb\install\helper\config;
19use phpbb\install\helper\database;
20use phpbb\install\helper\iohandler\iohandler_interface;
21use phpbb\install\sequential_task;
22use phpbb\language\language;
23
24/**
25 * Create database schema
26 */
27class add_default_data extends database_task
28{
29    use sequential_task;
30
31    /**
32     * @var Connection
33     */
34    protected $db;
35
36    /**
37     * @var database
38     */
39    protected $database_helper;
40
41    /**
42     * @var config
43     */
44    protected $config;
45
46    /**
47     * @var language
48     */
49    protected $language;
50
51    /**
52     * @var string
53     */
54    protected $phpbb_root_path;
55
56    /**
57     * Constructor
58     *
59     * @param database                $db_helper    Installer's database helper
60     * @param config                $config        Installer config
61     * @param iohandler_interface    $iohandler    Installer's input-output handler
62     * @param language                $language    Language service
63     * @param string                $root_path    Root path of phpBB
64     */
65    public function __construct(database $db_helper,
66                                config $config,
67                                iohandler_interface $iohandler,
68                                language $language,
69                                string $root_path)
70    {
71        $this->db                = self::get_doctrine_connection($db_helper, $config);
72        $this->database_helper    = $db_helper;
73        $this->config            = $config;
74        $this->language            = $language;
75        $this->phpbb_root_path    = $root_path;
76
77        parent::__construct($this->db, $iohandler, true);
78    }
79
80    /**
81     * {@inheritdoc}
82     */
83    public function run()
84    {
85        $table_prefix = $this->config->get('table_prefix');
86        $dbms = $this->config->get('dbms');
87        $dbms_info = $this->database_helper->get_available_dbms($dbms);
88
89        // Get schema data from file
90        $sql_query = @file_get_contents($this->phpbb_root_path . 'install/schemas/schema_data.sql');
91
92        // Clean up SQL
93        $sql_query = $this->replace_dbms_specific_sql($sql_query);
94        $sql_query = preg_replace('# phpbb_([^\s]*) #i', ' ' . $table_prefix . '\1 ', $sql_query);
95        $sql_query = preg_replace_callback('#\{L_([A-Z0-9\-_]*)\}#s', array($this, 'lang_replace_callback'), $sql_query);
96        $sql_query = $this->database_helper->remove_comments($sql_query);
97        $sql_query = $this->database_helper->split_sql_file($sql_query, $dbms_info[$dbms]['DELIM']);
98
99        $this->execute($this->config, $sql_query);
100    }
101
102    /**
103     * {@inheritdoc}
104     */
105    protected function execute_step($key, $value) : void
106    {
107        $sql = trim($value);
108        switch ($sql)
109        {
110            case 'BEGIN':
111                $this->db->beginTransaction();
112            break;
113
114            case 'COMMIT':
115                $this->db->commit();
116            break;
117
118            default:
119                $this->exec_sql($sql);
120            break;
121        }
122    }
123
124    /**
125     * Process DB specific SQL
126     *
127     * @param string $query
128     *
129     * @return string
130     */
131    protected function replace_dbms_specific_sql(string $query) : string
132    {
133        $dbms = $this->config->get('dbms');
134        switch ($dbms)
135        {
136            case 'mssql_odbc':
137            case 'mssqlnative':
138                $query = preg_replace('#\# MSSQL IDENTITY (phpbb_[a-z_]+) (ON|OFF) \##s', 'SET IDENTITY_INSERT \1 \2;', $query);
139            break;
140
141            case 'postgres':
142                $query = preg_replace('#\# POSTGRES (BEGIN|COMMIT) \##s', '\1; ', $query);
143            break;
144
145            case 'mysqli':
146                $query = str_replace('\\', '\\\\', $query);
147            break;
148        }
149
150        return $query;
151    }
152
153    /**
154     * Callback function for language replacing
155     *
156     * @param array    $matches
157     * @return string
158     */
159    public function lang_replace_callback(array $matches) : string
160    {
161        if (!empty($matches[1]))
162        {
163            $translation = $this->language->lang($matches[1]);
164
165            // This is might not be secure, but these queries should not be malicious anyway.
166            $quoted = $this->db->quote($translation) ?: '\'' . addcslashes($translation, '\'') . '\'';
167            return substr($quoted, 1, -1);
168        }
169
170        return '';
171    }
172
173    /**
174     * {@inheritdoc}
175     */
176    public static function get_step_count() : int
177    {
178        return 1;
179    }
180
181    /**
182     * {@inheritdoc}
183     */
184    public function get_task_lang_name() : string
185    {
186        return 'TASK_ADD_DEFAULT_DATA';
187    }
188}