From b3e42635ca201eea2fa82fb057f021768daf6786 Mon Sep 17 00:00:00 2001 From: Vjacheslav Trushkin Date: Tue, 10 Apr 2012 22:41:59 +0300 Subject: [PATCH 1/6] [ticket/10759] Fixing style in database updater Fixing error in database updater caused by style components merge PHPBB3-10759 --- phpBB/install/database_update.php | 155 ++++++++++++++++++++++++------ 1 file changed, 127 insertions(+), 28 deletions(-) diff --git a/phpBB/install/database_update.php b/phpBB/install/database_update.php index 5368ec06bb..0b33168496 100644 --- a/phpBB/install/database_update.php +++ b/phpBB/install/database_update.php @@ -1110,20 +1110,6 @@ function database_update_info() 'group_legend' => array('UINT', 0), ), ), - 'drop_columns' => array( - STYLES_TABLE => array( - 'imageset_id', - 'template_id', - 'theme_id', - ), - ), - 'drop_tables' => array( - STYLES_IMAGESET_TABLE, - STYLES_IMAGESET_DATA_TABLE, - STYLES_TEMPLATE_TABLE, - STYLES_TEMPLATE_DATA_TABLE, - STYLES_THEME_TABLE, - ), ), ); } @@ -1135,7 +1121,7 @@ function database_update_info() *****************************************************************************/ function change_database_data(&$no_updates, $version) { - global $db, $errored, $error_ary, $config, $phpbb_root_path, $phpEx; + global $db, $errored, $error_ary, $config, $phpbb_root_path, $phpEx, $db_tools; switch ($version) { @@ -2312,13 +2298,6 @@ function change_database_data(&$no_updates, $version) 'auth' => 'acl_a_styles', 'cat' => 'ACP_STYLE_MANAGEMENT', ), - 'edit' => array( - 'base' => 'acp_styles', - 'class' => 'acp', - 'title' => 'ACP_STYLES_EDIT', - 'auth' => 'acl_a_styles', - 'cat' => 'ACP_STYLE_MANAGEMENT', - ), 'cache' => array( 'base' => 'acp_styles', 'class' => 'acp', @@ -2421,13 +2400,133 @@ function change_database_data(&$no_updates, $version) { set_config('teampage_memberships', '1'); } - - // Clear styles table and add prosilver entry - _sql('DELETE FROM ' . STYLES_TABLE, $errored, $error_ary); - $sql = 'INSERT INTO ' . STYLES_TABLE . " (style_name, style_copyright, style_active, style_path, bbcode_bitfield, style_parent_id, style_parent_tree) VALUES ('prosilver', '© phpBB Group', 1, 'prosilver', 'kNg=', 0, '')"; - _sql($sql, $errored, $error_ary); - + // Check if styles table was already updated + if ($db_tools->sql_table_exists(STYLES_THEME_TABLE)) + { + // Get list of valid installed styles + $available_styles = array('prosilver'); + + $iterator = new DirectoryIterator($phpbb_root_path . 'styles'); + $skip_dirs = array('.', '..', 'prosilver'); + foreach ($iterator as $fileinfo) + { + if ($fileinfo->isDir() && !in_array($fileinfo->getFilename(), $skip_dirs) && file_exists($fileinfo->getPathname() . '/style.cfg')) + { + $style_cfg = parse_cfg_file($fileinfo->getPathname() . '/style.cfg'); + if (isset($style_cfg['phpbb_version']) && version_compare($style_cfg['phpbb_version'], '3.1.0-dev', '>=')) + { + // 3.1 style + $available_styles[] = $fileinfo->getFilename(); + } + } + } + + // Get all installed styles + if ($db_tools->sql_table_exists(STYLES_IMAGESET_TABLE)) + { + $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, i.imageset_id + FROM ' . STYLES_TABLE . ' s, ' . STYLES_TEMPLATE_TABLE . ' t, ' . STYLES_THEME_TABLE . ' c, ' . STYLES_IMAGESET_TABLE . " i + WHERE t.template_id = s.template_id + AND c.theme_id = s.theme_id + AND i.imageset_id = s.imageset_id"; + } + else + { + $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 + FROM ' . STYLES_TABLE . ' s, ' . STYLES_TEMPLATE_TABLE . ' t, ' . STYLES_THEME_TABLE . " c + WHERE t.template_id = s.template_id + AND c.theme_id = s.theme_id"; + } + $result = $db->sql_query($sql); + + $styles = array(); + while ($row = $db->sql_fetchrow($result)) + { + $styles[] = $row; + } + $db->sql_freeresult($result); + + // Check each style + $valid_styles = array(); + foreach ($styles as $style_row) + { + if ( + // Ignore styles with parent style + $style_row['template_inherits_id'] == 0 && + // Check if components match + $style_row['template_path'] == $style_row['theme_path'] && (!isset($style_row['imageset_path']) || $style_row['template_path'] == $style_row['imageset_path']) && + // Check if components are valid + in_array($style_row['template_path'], $available_styles) + ) + { + // Valid style. Keep it + $sql_ary = array( + 'style_path' => $style_row['template_path'], + 'bbcode_bitfield' => $style_row['bbcode_bitfield'], + 'style_parent_id' => 0, + 'style_parent_tree' => '', + ); + _sql('UPDATE ' . STYLES_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . ' WHERE style_id = ' . $style_row['style_id'], $errored, $error_ary); + $valid_styles[] = $style_row['style_id']; + } + } + + // Remove old styles tables + $changes = array( + 'drop_columns' => array( + STYLES_TABLE => array( + 'imageset_id', + 'template_id', + 'theme_id', + ), + ), + + 'drop_tables' => array( + STYLES_IMAGESET_TABLE, + STYLES_IMAGESET_DATA_TABLE, + STYLES_TEMPLATE_TABLE, + STYLES_TEMPLATE_DATA_TABLE, + STYLES_THEME_TABLE, + ) + ); + $statements = $db_tools->perform_schema_changes($changes); + + foreach ($statements as $sql) + { + _sql($sql, $errored, $error_ary); + } + + // Remove old entries from styles table + if (!sizeof($valid_styles)) + { + // No valid styles: remove everything and add prosilver + _sql('DELETE FROM ' . STYLES_TABLE, $errored, $error_ary); + + $sql = 'INSERT INTO ' . STYLES_TABLE . " (style_id, style_name, style_copyright, style_active, style_path, bbcode_bitfield, style_parent_id, style_parent_tree) VALUES (1, 'prosilver', '© phpBB Group', 1, 'prosilver', 'kNg=', 0, '')"; + _sql($sql, $errored, $error_ary); + + set_config('default_style', '1'); + + $sql = 'UPDATE ' . USERS_TABLE . ' SET user_style = 0'; + _sql($sql, $errored, $error_ary); + } + else + { + // There are valid styles in styles table. Remove styles that are outdated + _sql('DELETE FROM ' . STYLES_TABLE . ' WHERE ' . $db->sql_in_set('style_id', $valid_styles, true), $errored, $error_ary); + + // Change default style + if (!in_array($config['default_style'], $valid_styles)) + { + set_config('default_style', $valid_styles[0]); + } + + // Reset styles for users + _sql('UPDATE ' . USERS_TABLE . ' SET user_style = 0 WHERE ' . $db->sql_in_set('user_style', $valid_styles, true), $errored, $error_ary); + } + } + $no_updates = false; if (!isset($config['assets_version'])) From ea8f83de6f87af8fc9413e1be557953cabe8811e Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Thu, 19 Apr 2012 03:06:40 +0200 Subject: [PATCH 2/6] [ticket/10759] Fix whitespace in database_update.php PHPBB3-10759 --- phpBB/install/database_update.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/phpBB/install/database_update.php b/phpBB/install/database_update.php index 0b33168496..cddc1c4164 100644 --- a/phpBB/install/database_update.php +++ b/phpBB/install/database_update.php @@ -2310,7 +2310,7 @@ function change_database_data(&$no_updates, $version) _add_modules($modules_to_install); $sql = 'DELETE FROM ' . MODULES_TABLE . " - WHERE (module_basename = 'styles' OR module_basename = 'acp_styles') AND (module_mode = 'imageset' OR module_mode = 'theme' OR module_mode = 'template')"; + WHERE (module_basename = 'styles' OR module_basename = 'acp_styles') AND (module_mode = 'imageset' OR module_mode = 'theme' OR module_mode = 'template')"; _sql($sql, $errored, $error_ary); // Localise Global Announcements @@ -2474,15 +2474,15 @@ function change_database_data(&$no_updates, $version) // Remove old styles tables $changes = array( - 'drop_columns' => array( - STYLES_TABLE => array( + 'drop_columns' => array( + STYLES_TABLE => array( 'imageset_id', 'template_id', 'theme_id', ), ), - 'drop_tables' => array( + 'drop_tables' => array( STYLES_IMAGESET_TABLE, STYLES_IMAGESET_DATA_TABLE, STYLES_TEMPLATE_TABLE, From c43373068636a4f361423f2fe41231ac2e32f70f Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Thu, 19 Apr 2012 03:18:20 +0200 Subject: [PATCH 3/6] [ticket/10759] Retrieve style_id after INSERT since we cannot set it PHPBB3-10759 --- phpBB/install/database_update.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/phpBB/install/database_update.php b/phpBB/install/database_update.php index cddc1c4164..d71226a29c 100644 --- a/phpBB/install/database_update.php +++ b/phpBB/install/database_update.php @@ -2503,10 +2503,17 @@ function change_database_data(&$no_updates, $version) // No valid styles: remove everything and add prosilver _sql('DELETE FROM ' . STYLES_TABLE, $errored, $error_ary); - $sql = 'INSERT INTO ' . STYLES_TABLE . " (style_id, style_name, style_copyright, style_active, style_path, bbcode_bitfield, style_parent_id, style_parent_tree) VALUES (1, 'prosilver', '© phpBB Group', 1, 'prosilver', 'kNg=', 0, '')"; + $sql = 'INSERT INTO ' . STYLES_TABLE . " (style_name, style_copyright, style_active, style_path, bbcode_bitfield, style_parent_id, style_parent_tree) VALUES ('prosilver', '© phpBB Group', 1, 'prosilver', 'kNg=', 0, '')"; _sql($sql, $errored, $error_ary); - set_config('default_style', '1'); + $sql = 'SELECT style_id + FROM ' . $table . " + WHERE style_name = 'prosilver'"; + $result = _sql($sql, $errored, $error_ary); + $default_style = $db->sql_fetchfield($result); + $db->sql_freeresult($result); + + set_config('default_style', $default_style); $sql = 'UPDATE ' . USERS_TABLE . ' SET user_style = 0'; _sql($sql, $errored, $error_ary); From 0b7a0fa2d11c4f30d31e36f2c11c574491432d4a Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Thu, 19 Apr 2012 03:27:42 +0200 Subject: [PATCH 4/6] [ticket/10759] Clarify comments a bit PHPBB3-10759 --- phpBB/install/database_update.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/phpBB/install/database_update.php b/phpBB/install/database_update.php index d71226a29c..c30508dcee 100644 --- a/phpBB/install/database_update.php +++ b/phpBB/install/database_update.php @@ -2404,7 +2404,7 @@ function change_database_data(&$no_updates, $version) // Check if styles table was already updated if ($db_tools->sql_table_exists(STYLES_THEME_TABLE)) { - // Get list of valid installed styles + // Get list of valid 3.1 styles $available_styles = array('prosilver'); $iterator = new DirectoryIterator($phpbb_root_path . 'styles'); @@ -2447,12 +2447,12 @@ function change_database_data(&$no_updates, $version) } $db->sql_freeresult($result); - // Check each style + // Decide which styles to keep, all others will be deleted $valid_styles = array(); foreach ($styles as $style_row) { if ( - // Ignore styles with parent style + // Delete styles with parent style (not supported yet) $style_row['template_inherits_id'] == 0 && // Check if components match $style_row['template_path'] == $style_row['theme_path'] && (!isset($style_row['imageset_path']) || $style_row['template_path'] == $style_row['imageset_path']) && From 18704215216cf31e8d96058d2df7ef7cf20a3f7b Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Thu, 19 Apr 2012 03:38:01 +0200 Subject: [PATCH 5/6] [ticket/10759] Don't select imageset_id, it's not needed PHPBB3-10759 --- phpBB/install/database_update.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/install/database_update.php b/phpBB/install/database_update.php index c30508dcee..88ebb8209e 100644 --- a/phpBB/install/database_update.php +++ b/phpBB/install/database_update.php @@ -2425,7 +2425,7 @@ function change_database_data(&$no_updates, $version) // Get all installed styles if ($db_tools->sql_table_exists(STYLES_IMAGESET_TABLE)) { - $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, i.imageset_id + $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 FROM ' . STYLES_TABLE . ' s, ' . STYLES_TEMPLATE_TABLE . ' t, ' . STYLES_THEME_TABLE . ' c, ' . STYLES_IMAGESET_TABLE . " i WHERE t.template_id = s.template_id AND c.theme_id = s.theme_id From 439ade4ee2f2fcd301ad3690624a821a779c95ee Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Thu, 19 Apr 2012 03:51:17 +0200 Subject: [PATCH 6/6] [ticket/10759] Make sure style ids are integers PHPBB3-10759 --- phpBB/install/database_update.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpBB/install/database_update.php b/phpBB/install/database_update.php index 88ebb8209e..a0892005d2 100644 --- a/phpBB/install/database_update.php +++ b/phpBB/install/database_update.php @@ -2468,7 +2468,7 @@ function change_database_data(&$no_updates, $version) 'style_parent_tree' => '', ); _sql('UPDATE ' . STYLES_TABLE . ' SET ' . $db->sql_build_array('UPDATE', $sql_ary) . ' WHERE style_id = ' . $style_row['style_id'], $errored, $error_ary); - $valid_styles[] = $style_row['style_id']; + $valid_styles[] = (int) $style_row['style_id']; } }