function pgsql_wrapper($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect) { $this->prefix = $db_prefix; if ($db_host) { if (strpos($db_host, ':') !== false) { list($db_host, $dbport) = explode(':', $db_host); $connect_str[] = 'host=' . $db_host . ' port=' . $dbport; } else { $connect_str[] = 'host=' . $db_host; } } if ($db_name) { $connect_str[] = 'dbname=' . $db_name; } if ($db_username) { $connect_str[] = 'user='******'password='******' ', $connect_str)); } else { $this->link_id = @pg_connect(implode(' ', $connect_str)); } if (!$this->link_id) { conv_error('Unable to connect to PostgreSQL server', __FILE__, __LINE__); } // Setup the client-server character set (UTF-8) if (!defined('FORUM_NO_SET_NAMES')) { $this->set_names('utf8'); } return $this->link_id; }
function mysql_wrapper($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect) { $this->prefix = $db_prefix; if ($p_connect) { $this->link_id = @mysql_pconnect($db_host, $db_username, $db_password); } else { $this->link_id = @mysql_connect($db_host, $db_username, $db_password); } if ($this->link_id) { if (!@mysql_select_db($db_name, $this->link_id)) { conv_error('Unable to select database. MySQL reported: ' . mysql_error(), __FILE__, __LINE__); } } else { conv_error('Unable to connect to MySQL server. MySQL reported: ' . mysql_error(), __FILE__, __LINE__); } // Setup the client-server character set (UTF-8) if (!defined('FORUM_NO_SET_NAMES')) { $this->set_names('utf8'); } return $this->link_id; }
function mysqli_innodb_wrapper($db_host, $db_username, $db_password, $db_name, $db_prefix, $p_connect) { $this->prefix = $db_prefix; // Was a custom port supplied with $db_host? if (strpos($db_host, ':') !== false) { list($db_host, $db_port) = explode(':', $db_host); } // Persistent connection in MySQLi are only available in PHP 5.3 and later releases $p_connect = $p_connect && version_compare(PHP_VERSION, '5.3.0', '>=') ? 'p:' : ''; if (isset($db_port)) { $this->link_id = @mysqli_connect($p_connect . $db_host, $db_username, $db_password, $db_name, $db_port); } else { $this->link_id = @mysqli_connect($p_connect . $db_host, $db_username, $db_password, $db_name); } if (!$this->link_id) { conv_error('Unable to connect to MySQL and select database. MySQL reported: ' . mysqli_connect_error(), __FILE__, __LINE__); } // Setup the client-server character set (UTF-8) if (!defined('FORUM_NO_SET_NAMES')) { $this->set_names('utf8'); } return $this->link_id; }
/** * Convert user id to FluxBB style */ function uid2uid($id) { static $last_uid; // id=0 is a guest user if ($id == 0) { return 1; } else { if ($id == 1) { if (!isset($last_uid)) { $result = $this->db->query_build(array('SELECT' => 'MAX(user_id)', 'FROM' => 'users')) or conv_error('Unable to fetch last user id', __FILE__, __LINE__, $this->db->error()); $last_uid = $this->db->result($result) + 1; } return $last_uid; } } return $id; }
} else { if (($key = array_search(strtolower($forum_config['type']), array_map('strtolower', $keys))) !== false) { $forum_config['type'] = $keys[$key]; } else { conv_error($lang_convert['Invalid forum software'] . ' ' . sprintf($lang_convert['Possible values'], "\n" . implode("\n", array_keys($forums)))); } } } if (!in_array($old_db_config['type'], $engines)) { conv_error($lang_convert['Invalid database type'] . ' ' . sprintf($lang_convert['Possible values'], "\n" . implode("\n", $engines))); } // Get database configuration from config.php $db_config = array('type' => $db_type, 'host' => $db_host, 'name' => $db_name, 'username' => $db_username, 'password' => $db_password, 'prefix' => $db_prefix); // Check we aren't trying to convert to the same database if ($old_db_config == $db_config) { conv_error('Same database tables'); } if (defined('CONV_LOG') && file_exists(CONV_LOG)) { @unlink(CONV_LOG); } conv_log('Running command line based converter for: ' . $forum_config['type'] . ' (' . gmdate('Y-m-d H:i:s') . ')'); conv_log('PHP version: ' . PHP_VERSION . ', OS: ' . PHP_OS); $session = array(); // Create a wrapper for fluxbb (has easy functions for adding users etc.) require CONV_ROOT . 'include/fluxbb.class.php'; $fluxbb = new FluxBB($pun_config); $db = $fluxbb->connect_database($db_config); // Load the migration script require CONV_ROOT . 'include/forum.class.php'; $forum = load_forum($forum_config, $fluxbb); $forum->connect_database($old_db_config);
/** * Recount all posts, topics after conversion * * @return type */ function sync_db() { conv_log('Updating post count for each user'); $start = get_microtime(); // Update user post count $result = $this->db->query_build(array('SELECT' => 'p.poster_id, COUNT(p.id) AS post_count, u.num_posts', 'JOINS' => array(array('INNER JOIN' => 'users AS u', 'ON' => 'u.id = p.poster_id')), 'FROM' => 'posts AS p', 'GROUP BY' => 'p.poster_id', 'WHERE' => 'p.poster_id > 0')) or conv_error('Unable to fetch user posts', __FILE__, __LINE__, $this->db->error()); while ($cur_user = $this->db->fetch_assoc($result)) { if ($cur_user['num_posts'] == $cur_user['post_count']) { continue; } $this->db->query_build(array('UPDATE' => 'users', 'SET' => 'num_posts = ' . $cur_user['post_count'], 'WHERE' => 'id = ' . $cur_user['poster_id'])) or conv_error('Unable to update user post count', __FILE__, __LINE__, $this->db->error()); } conv_log('Done in ' . round(get_microtime() - $start, 6) . "\n"); conv_log('Updating post count for each topic'); $start = get_microtime(); // Update post count for each topic $result = $this->db->query_build(array('SELECT' => 'p.topic_id, COUNT(p.id) AS post_count, t.num_replies', 'JOINS' => array(array('INNER JOIN' => 'topics AS t', 'ON' => 't.id = p.topic_id')), 'FROM' => 'posts AS p', 'GROUP BY' => 'p.topic_id')) or conv_error('Unable to fetch topic posts', __FILE__, __LINE__, $this->db->error()); while ($cur_topic = $this->db->fetch_assoc($result)) { if ($cur_topic['num_replies'] == $cur_topic['post_count'] - 1) { continue; } $this->db->query_build(array('UPDATE' => 'topics', 'SET' => 'num_replies = ' . ($cur_topic['post_count'] - 1), 'WHERE' => 'id = ' . $cur_topic['topic_id'])) or conv_error('Unable to update topic post count', __FILE__, __LINE__, $this->db->error()); } conv_log('Done in ' . round(get_microtime() - $start, 6) . "\n"); conv_log('Updating last post for each topic'); $start = get_microtime(); // Update last post for each topic $subquery = array('SELECT' => 'topic_id, MAX(posted) AS last_post', 'FROM' => 'posts', 'GROUP BY' => 'topic_id'); $result = $this->db->query_build(array('SELECT' => 'p.topic_id, p.id, p.posted, p.poster, t.last_post, t.last_post_id, t.last_poster', 'JOINS' => array(array('INNER JOIN' => $this->db->prefix . 'posts AS p', 'ON' => 'p.topic_id = s.topic_id AND p.posted = s.last_post'), array('INNER JOIN' => $this->db->prefix . 'topics AS t', 'ON' => 'p.topic_id = t.id')), 'FROM' => '(' . $this->db->query_build($subquery, true) . ') AS s', 'PARAMS' => array('NO_PREFIX' => true))) or conv_error('Unable to fetch topic last post', __FILE__, __LINE__, $this->db->error()); while ($cur_topic = $this->db->fetch_assoc($result)) { $values = array(); if ($cur_topic['posted'] != $cur_topic['last_post']) { $values[] = 'last_post = ' . $cur_topic['posted']; } if ($cur_topic['poster'] != $cur_topic['last_poster']) { $values[] = 'last_poster = \'' . $this->db->escape($cur_topic['poster']) . '\''; } if ($cur_topic['id'] != $cur_topic['last_post_id']) { $values[] = 'last_post_id = ' . $cur_topic['id']; } if (!empty($values)) { $this->db->query_build(array('UPDATE' => 'topics', 'SET' => implode(', ', $values), 'WHERE' => 'id = ' . $cur_topic['topic_id'])) or conv_error('Unable to update last post for topic', __FILE__, __LINE__, $this->db->error()); } } conv_log('Done in ' . round(get_microtime() - $start, 6) . "\n"); conv_log('Updating num topics and num posts for each forum'); $start = get_microtime(); // Update num_topics and num_posts for each forum $result = $this->db->query_build(array('SELECT' => 't.forum_id, COUNT(t.id) AS topic_count, SUM(t.num_replies) + COUNT(t.id) AS post_count, f.num_posts, f.num_topics', 'JOINS' => array(array('INNER JOIN' => 'forums AS f', 'ON' => 'f.id = t.forum_id')), 'FROM' => 'topics AS t', 'GROUP BY' => 't.forum_id')) or conv_error('Unable to fetch topics for forum', __FILE__, __LINE__, $this->db->error()); while ($cur_forum = $this->db->fetch_assoc($result)) { $values = array(); if ($cur_forum['topic_count'] != $cur_forum['num_topics']) { $values[] = 'num_topics = ' . $cur_forum['topic_count']; } if ($cur_forum['post_count'] != $cur_forum['num_posts']) { $values[] = 'num_posts = ' . $cur_forum['post_count']; } if (!empty($values)) { $this->db->query_build(array('UPDATE' => 'forums', 'SET' => implode(', ', $values), 'WHERE' => 'id = ' . $cur_forum['forum_id'])) or conv_error('Unable to update topic count for forum', __FILE__, __LINE__, $this->db->error()); } } conv_log('Done in ' . round(get_microtime() - $start, 6) . "\n"); conv_log('Updating last post for each forum'); $start = get_microtime(); // Update last post for each forum $subquery = array('SELECT' => 'forum_id, MAX(last_post) AS last_post', 'FROM' => 'topics', 'GROUP BY' => 'forum_id'); $result = $this->db->query_build(array('SELECT' => 't.forum_id, t.last_post_id AS new_last_post_id, t.last_post AS new_last_post, t.last_poster AS new_last_poster, f.last_post_id, f.last_poster, f.last_post', 'JOINS' => array(array('INNER JOIN' => $this->db->prefix . 'topics AS t', 'ON' => 't.forum_id = s.forum_id AND s.last_post = t.last_post'), array('INNER JOIN' => $this->db->prefix . 'forums AS f', 'ON' => 't.forum_id = f.id')), 'FROM' => '(' . $this->db->query_build($subquery, true) . ') AS s', 'PARAMS' => array('NO_PREFIX' => true))) or conv_error('Unable to fetch forum last post', __FILE__, __LINE__, $this->db->error()); while ($cur_forum = $this->db->fetch_assoc($result)) { $values = array(); if ($cur_forum['new_last_post'] != $cur_forum['last_post']) { $values[] = 'last_post = ' . $cur_forum['new_last_post']; } if ($cur_forum['new_last_poster'] != $cur_forum['last_poster']) { $values[] = 'last_poster = \'' . $this->db->escape($cur_forum['new_last_poster']) . '\''; } if ($cur_forum['new_last_post_id'] != $cur_forum['last_post_id']) { $values[] = 'last_post_id = ' . $cur_forum['new_last_post_id']; } if (!empty($values)) { $this->db->query_build(array('UPDATE' => 'forums', 'SET' => implode(', ', $values), 'WHERE' => 'id = ' . $cur_forum['forum_id'])) or conv_error('Unable to update last post for forum', __FILE__, __LINE__, $this->db->error()); } } conv_log('Done in ' . round(get_microtime() - $start, 6) . "\n"); }
function fetchUid($username) { $result = $this->fluxbb->db->query_build(array('SELECT' => 'id', 'FROM' => 'users', 'WHERE' => 'username = \'' . $this->fluxbb->db->escape($username) . '\'')) or conv_error('Unable to fetch user id', __FILE__, __LINE__, $this->fluxbb->db->error()); if ($this->fluxbb->db->num_rows($result)) { return $this->fluxbb->db->result($result); } return 0; }
function get_table_info($table_name, $no_prefix = false) { // Grab table info $result = $this->query('SELECT sql FROM sqlite_master WHERE tbl_name = \'' . ($no_prefix ? '' : $this->prefix) . $this->escape($table_name) . '\' ORDER BY type DESC') or conv_error('Unable to fetch table information', __FILE__, __LINE__, $this->error()); $num_rows = $this->num_rows($result); if ($num_rows == 0) { return; } $table = array(); $table['indices'] = array(); while ($cur_index = $this->fetch_assoc($result)) { if (empty($cur_index['sql'])) { continue; } if (!isset($table['sql'])) { $table['sql'] = $cur_index['sql']; } else { $table['indices'][] = $cur_index['sql']; } } // Work out the columns in the table currently $table_lines = explode("\n", $table['sql']); $table['columns'] = array(); foreach ($table_lines as $table_line) { $table_line = trim($table_line, " \t\n\r,"); // trim spaces, tabs, newlines, and commas if (substr($table_line, 0, 12) == 'CREATE TABLE') { continue; } else { if (substr($table_line, 0, 11) == 'PRIMARY KEY') { $table['primary_key'] = $table_line; } else { if (substr($table_line, 0, 6) == 'UNIQUE') { $table['unique'] = $table_line; } else { if (substr($table_line, 0, strpos($table_line, ' ')) != '') { $table['columns'][substr($table_line, 0, strpos($table_line, ' '))] = trim(substr($table_line, strpos($table_line, ' '))); } } } } } return $table; }
/** * Copy avatar file to the FluxBB avatars dir */ function convert_avatar($cur_user) { static $config; if (empty($cur_user['user_avatar'])) { return false; } // Fetch avatar from remote url if (strpos($cur_user['user_avatar'], '://') !== false) { return $this->fluxbb->save_avatar($cur_user['user_avatar'], $cur_user['id']); } else { if (isset($this->path)) { if (!isset($config)) { $config = array(); $result = $this->db->query_build(array('SELECT' => 'config_name, config_value', 'FROM' => 'config', 'WHERE' => 'config_name IN (\'avatar_path\', \'avatar_salt\', \'avatar_gallery_path\')')) or conv_error('Unable to fetch config', __FILE__, __LINE__, $this->db->error()); while ($cur_config = $this->db->fetch_assoc($result)) { $config[$cur_config['config_name']] = $cur_config['config_value']; } } // Look for user avatar from gallery $cur_avatar_file = $this->path . rtrim($config['avatar_gallery_path'], '/') . '/' . $cur_user['user_avatar']; if (file_exists($cur_avatar_file)) { return $this->fluxbb->save_avatar($cur_avatar_file, $cur_user['id']); } // Fetch avatar from local file $old_avatars_dir = $this->path . rtrim($config['avatar_path'], '/') . '/'; foreach ($this->fluxbb->avatar_exts as $cur_ext) { $cur_avatar_file = $old_avatars_dir . $config['avatar_salt'] . '_' . $cur_user['id'] . '.' . $cur_ext; if (file_exists($cur_avatar_file)) { return $this->fluxbb->save_avatar($cur_avatar_file, $cur_user['id']); } } } } return false; }
/** * Convert user id to FluxBB style */ function uid2uid($id) { static $last_uid; // Fetch new user id (id=1 is reserved for guest user) if ($id == 1) { if (!isset($last_uid)) { $result = $this->db->query_build(array('SELECT' => 'MAX(user_id)', 'FROM' => 'users')) or conv_error('Unable to fetch last user', __FILE__, __LINE__, $this->db->error()); $last_uid = $this->db->result($result) + 1; } return $last_uid; } return $id; }
/** * Copy avatar file to the FluxBB avatars dir */ function convert_avatar($cur_user) { static $config; if (empty($cur_user['pp_main_photo'])) { return false; } // Fetch avatar from remote url if (strpos($cur_user['pp_main_photo'], '://') !== false) { return $this->fluxbb->save_avatar($cur_user['pp_main_photo'], $cur_user['id']); } else { if (isset($this->path)) { if (!isset($config)) { $config = array(); $result = $this->db->query_build(array('SELECT' => 'conf_key, conf_value', 'FROM' => 'core_sys_conf_settings', 'WHERE' => 'conf_key = \'upload_dir\'')) or conv_error('Unable to fetch config', __FILE__, __LINE__, $this->db->error()); while ($cur_config = $this->db->fetch_assoc($result)) { $config[$cur_config['conf_key']] = $cur_config['conf_value']; } } // Fetch avatar from local file $cur_avatar_file = $this->path . rtrim($config['upload_dir'], '/') . '/' . $cur_user['pp_main_photo']; if (file_exists($cur_avatar_file)) { return $this->fluxbb->save_avatar($cur_avatar_file, $cur_user['id']); } } } return false; }
/** * Copy avatar file to the FluxBB avatars dir */ function convert_avatar($user_id) { static $config; if (!isset($config)) { $config = array(); $result = $this->db->query_build(array('SELECT' => 'conf_name, conf_value', 'FROM' => 'config', 'WHERE' => 'conf_name IN (\'o_avatars\', \'o_avatars_dir\')')) or conv_error('Unable to fetch avatars_dir', __FILE__, __LINE__, $this->db->error()); while ($cur_config = $this->db->fetch_assoc($result)) { $config[$cur_config['conf_name']] = $cur_config['conf_value']; } } if ($config['o_avatars'] == '0' || !isset($this->path)) { return false; } $old_avatars_dir = $this->path . rtrim($config['o_avatars_dir'], '/') . '/'; foreach ($this->fluxbb->avatar_exts as $cur_ext) { $cur_avatar_file = $old_avatars_dir . $user_id . $cur_ext; if (file_exists($cur_avatar_file)) { return $this->fluxbb->save_avatar($cur_avatar_file, $user_id); } } }
/** * Load forum converter class * * @param mixed $forum_type * @param mixed $db * @param mixed $fluxbb */ function load_forum($forum_config, $fluxbb) { if (!class_exists($forum_config['type'])) { if (!file_exists(CONV_ROOT . 'forums/' . $forum_config['type'] . '.php')) { conv_error('Unsupported forum type: ' . $forum_config['type'], __FILE__, __LINE__); } require CONV_ROOT . 'forums/' . $forum_config['type'] . '.php'; } $class = str_replace('.', '_', $forum_config['type']); return new $class($forum_config, $fluxbb); }
/** * Fetch row count for each (or one) tables * * @param string $table * @return type */ function fetch_item_count($table = null) { global $session; // First we look for whether we have this data stored in $session if (isset($session['forum_item_count'])) { $tables = $session['forum_item_count']; } else { $tables = array(); foreach ($this->steps as $cur_step => $table_info) { $count = 0; if (is_numeric($table_info)) { $count = $table_info; } else { if (is_array($table_info)) { $query = array('SELECT' => 'COUNT(' . $this->db->escape($table_info[1]) . ')', 'FROM' => $this->db->escape($table_info[0])); if (isset($table_info[2])) { $query['WHERE'] = $table_info[2]; } $result = $this->db->query_build($query) or conv_error('Unable to fetch num rows for ' . $cur_step, __FILE__, __LINE__, $this->db->error()); $count = $this->db->result($result); } } $tables[$cur_step] = $count; } $session['forum_item_count'] = $tables; } if (isset($table)) { return isset($tables[$table]) ? $tables[$table] : -1; } return $tables; }