Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ticket/10759] Fix styles in database updater #760

Merged
merged 6 commits into from Apr 19, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
164 changes: 135 additions & 29 deletions phpBB/install/database_update.php
Expand Up @@ -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,
),
),
);
}
Expand All @@ -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)
{
Expand Down Expand Up @@ -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',
Expand All @@ -2331,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
Expand Down Expand Up @@ -2421,13 +2400,140 @@ 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 3.1 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
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);

// Decide which styles to keep, all others will be deleted
$valid_styles = array();
foreach ($styles as $style_row)
{
if (
// 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']) &&
// 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[] = (int) $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_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);

$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);
}
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']))
Expand Down