Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 100 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
style_update_p1 | |
0.00% |
0 / 100 |
|
0.00% |
0 / 6 |
650 | |
0.00% |
0 / 1 |
effectively_installed | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
depends_on | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
update_schema | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
revert_schema | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
2 | |||
update_data | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
styles_update | |
0.00% |
0 / 75 |
|
0.00% |
0 / 1 |
420 |
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 | |
14 | namespace phpbb\db\migration\data\v310; |
15 | |
16 | use phpbb\json\sanitizer as json_sanitizer; |
17 | |
18 | class style_update_p1 extends \phpbb\db\migration\migration |
19 | { |
20 | public function effectively_installed() |
21 | { |
22 | return !$this->db_tools->sql_table_exists($this->table_prefix . 'styles_imageset'); |
23 | } |
24 | |
25 | public static function depends_on() |
26 | { |
27 | return array('\phpbb\db\migration\data\v30x\release_3_0_11'); |
28 | } |
29 | |
30 | public function update_schema() |
31 | { |
32 | return array( |
33 | 'add_columns' => array( |
34 | $this->table_prefix . 'styles' => array( |
35 | 'style_path' => array('VCHAR:100', ''), |
36 | 'bbcode_bitfield' => array('VCHAR:255', 'kNg='), |
37 | 'style_parent_id' => array('UINT', 0), |
38 | 'style_parent_tree' => array('TEXT', ''), |
39 | ), |
40 | ), |
41 | ); |
42 | } |
43 | |
44 | public function revert_schema() |
45 | { |
46 | return array( |
47 | 'drop_columns' => array( |
48 | $this->table_prefix . 'styles' => array( |
49 | 'style_path', |
50 | 'bbcode_bitfield', |
51 | 'style_parent_id', |
52 | 'style_parent_tree', |
53 | ), |
54 | ), |
55 | ); |
56 | } |
57 | |
58 | public function update_data() |
59 | { |
60 | return array( |
61 | array('custom', array(array($this, 'styles_update'))), |
62 | ); |
63 | } |
64 | |
65 | public function styles_update() |
66 | { |
67 | // Get list of valid 3.1 styles |
68 | $available_styles = array('prosilver'); |
69 | |
70 | $iterator = new \DirectoryIterator($this->phpbb_root_path . 'styles'); |
71 | $skip_dirs = array('.', '..', 'prosilver'); |
72 | foreach ($iterator as $fileinfo) |
73 | { |
74 | if ($fileinfo->isDir() && !in_array($fileinfo->getFilename(), $skip_dirs)) |
75 | { |
76 | if (file_exists($fileinfo->getPathname() . '/style.cfg')) |
77 | { |
78 | $style_cfg = parse_cfg_file($fileinfo->getPathname() . '/style.cfg'); |
79 | if (isset($style_cfg['phpbb_version']) && version_compare($style_cfg['phpbb_version'], '3.1.0-dev', '>=')) |
80 | { |
81 | // 3.1 - 3.3 style |
82 | $available_styles[] = $fileinfo->getFilename(); |
83 | } |
84 | } |
85 | else if (file_exists($fileinfo->getPathname() . '/composer.json')) |
86 | { |
87 | $json = file_get_contents($fileinfo->getPathname() . '/composer.json'); |
88 | $style_data = json_sanitizer::decode($json); |
89 | if (isset($style_data['extra']['phpbb-version']) && version_compare($style_data['extra']['phpbb-version'], '4.0.0-dev', '>=')) |
90 | { |
91 | // 4.x style |
92 | $available_styles[] = $fileinfo->getFilename(); |
93 | } |
94 | } |
95 | } |
96 | } |
97 | |
98 | // Get all installed styles |
99 | if ($this->db_tools->sql_table_exists($this->table_prefix . 'styles_imageset')) |
100 | { |
101 | $sql = 'SELECT s.style_id, t.template_path, t.template_id, t.bbcode_bitfield, t.template_inherits_id, t.template_inherit_path, c.theme_path, c.theme_id, i.imageset_path |
102 | FROM ' . STYLES_TABLE . ' s, ' . $this->table_prefix . 'styles_template t, ' . $this->table_prefix . 'styles_theme c, ' . $this->table_prefix . "styles_imageset i |
103 | WHERE t.template_id = s.template_id |
104 | AND c.theme_id = s.theme_id |
105 | AND i.imageset_id = s.imageset_id"; |
106 | } |
107 | else |
108 | { |
109 | $sql = 'SELECT s.style_id, t.template_path, t.template_id, t.bbcode_bitfield, t.template_inherits_id, t.template_inherit_path, c.theme_path, c.theme_id |
110 | FROM ' . STYLES_TABLE . ' s, ' . $this->table_prefix . 'styles_template t, ' . $this->table_prefix . "styles_theme c |
111 | WHERE t.template_id = s.template_id |
112 | AND c.theme_id = s.theme_id"; |
113 | } |
114 | $result = $this->db->sql_query($sql); |
115 | |
116 | $styles = array(); |
117 | while ($row = $this->db->sql_fetchrow($result)) |
118 | { |
119 | $styles[] = $row; |
120 | } |
121 | $this->db->sql_freeresult($result); |
122 | |
123 | // Decide which styles to keep, all others will be deleted |
124 | $valid_styles = array(); |
125 | foreach ($styles as $style_row) |
126 | { |
127 | if ( |
128 | // Delete styles with parent style (not supported yet) |
129 | $style_row['template_inherits_id'] == 0 && |
130 | // Check if components match |
131 | $style_row['template_path'] == $style_row['theme_path'] && (!isset($style_row['imageset_path']) || $style_row['template_path'] == $style_row['imageset_path']) && |
132 | // Check if components are valid |
133 | in_array($style_row['template_path'], $available_styles) |
134 | ) |
135 | { |
136 | // Valid style. Keep it |
137 | $sql_ary = array( |
138 | 'style_path' => $style_row['template_path'], |
139 | 'bbcode_bitfield' => $style_row['bbcode_bitfield'], |
140 | 'style_parent_id' => 0, |
141 | 'style_parent_tree' => '', |
142 | ); |
143 | $this->sql_query('UPDATE ' . STYLES_TABLE . ' |
144 | SET ' . $this->db->sql_build_array('UPDATE', $sql_ary) . ' |
145 | WHERE style_id = ' . $style_row['style_id']); |
146 | $valid_styles[] = (int) $style_row['style_id']; |
147 | } |
148 | } |
149 | |
150 | // Remove old entries from styles table |
151 | if (!count($valid_styles)) |
152 | { |
153 | // No valid styles: remove everything and add prosilver |
154 | $this->sql_query('DELETE FROM ' . STYLES_TABLE); |
155 | |
156 | $sql_ary = array( |
157 | 'style_name' => 'prosilver', |
158 | 'style_copyright' => '© phpBB Limited', |
159 | 'style_active' => 1, |
160 | 'style_path' => 'prosilver', |
161 | 'bbcode_bitfield' => 'lNg=', |
162 | 'style_parent_id' => 0, |
163 | 'style_parent_tree' => '', |
164 | |
165 | // Will be removed in the next step |
166 | 'imageset_id' => 0, |
167 | 'template_id' => 0, |
168 | 'theme_id' => 0, |
169 | ); |
170 | |
171 | $sql = 'INSERT INTO ' . STYLES_TABLE . ' ' . $this->db->sql_build_array('INSERT', $sql_ary); |
172 | $this->sql_query($sql); |
173 | |
174 | $sql = 'SELECT style_id |
175 | FROM ' . STYLES_TABLE . " |
176 | WHERE style_name = 'prosilver'"; |
177 | $result = $this->sql_query($sql); |
178 | $default_style = (int) $this->db->sql_fetchfield('style_id'); |
179 | $this->db->sql_freeresult($result); |
180 | |
181 | $this->config->set('default_style', $default_style); |
182 | |
183 | $sql = 'UPDATE ' . USERS_TABLE . ' SET user_style = ' . (int) $default_style; |
184 | $this->sql_query($sql); |
185 | } |
186 | else |
187 | { |
188 | // There are valid styles in styles table. Remove styles that are outdated |
189 | $this->sql_query('DELETE FROM ' . STYLES_TABLE . ' |
190 | WHERE ' . $this->db->sql_in_set('style_id', $valid_styles, true)); |
191 | |
192 | // Change default style |
193 | if (!in_array($this->config['default_style'], $valid_styles)) |
194 | { |
195 | $this->sql_query('UPDATE ' . CONFIG_TABLE . " |
196 | SET config_value = '" . $valid_styles[0] . "' |
197 | WHERE config_name = 'default_style'"); |
198 | } |
199 | |
200 | // Reset styles for users |
201 | $this->sql_query('UPDATE ' . USERS_TABLE . " |
202 | SET user_style = '" . (int) $valid_styles[0] . "' |
203 | WHERE " . $this->db->sql_in_set('user_style', $valid_styles, true)); |
204 | } |
205 | } |
206 | } |