exec('set @@foreign_key_checks=OFF'); function date_at(string $time): string { return date('Y-m-s h:i:s', $time); } function insert_tags() { global $xn_cfg,$fm_cfg,$dbh_xn,$dbh_fl; $sth = $dbh_xn->prepare('SELECT * FROM `' . $xn_cfg['tablepre'] . 'forum`'); $sth->execute(); // $result = $sth->fetchAll(PDO::FETCH_ASSOC); $dbh_fl->exec('DELETE FROM `' . $fm_cfg['prefix'] . 'tags`'); // $dbh_fl->exec('TRUNCATE TABLE`' . $fm_cfg['prefix'] . 'tags`'); while($row = $sth->fetch(PDO::FETCH_ASSOC)) { //print_r($row); $stmt = $dbh_fl->prepare('INSERT INTO `' . $fm_cfg['prefix'] . 'tags` (id,name,slug,description,position) VALUES (?,?,?,?,?)'); try { $data = [ $row['fid'], $row['name'], 'tag_' . $row['fid'], $row['brief'], $row['fid'], ]; $dbh_fl->beginTransaction(); $stmt->execute($data); $dbh_fl->commit(); // print $dbh_fl->lastInsertId(); } catch(PDOExecption $e) { $dbh_fl->rollback(); print "Error!: " . $e->getMessage() . "\n"; exit; } } print 'Insert table ' . $fm_cfg['prefix'] . "tags\n"; } function insert_discussions() { global $xn_cfg,$fm_cfg,$dbh_xn,$dbh_fl; $sth = $dbh_xn->prepare('SELECT * FROM `' . $xn_cfg['tablepre'] . 'thread`'); $sth->execute(); // $result = $sth->fetchAll(PDO::FETCH_ASSOC); $dbh_fl->exec('DELETE FROM `' . $fm_cfg['prefix'] . 'discussions`'); // $dbh_fl->exec('TRUNCATE TABLE`' . $fm_cfg['prefix'] . 'discussions`'); $dbh_fl->exec('TRUNCATE TABLE`' . $fm_cfg['prefix'] . 'discussion_tag`'); $dbh_fl->exec('TRUNCATE TABLE`' . $fm_cfg['prefix'] . 'discussion_user`'); while($row = $sth->fetch(PDO::FETCH_ASSOC)) { // print_r($row); $stmt = $dbh_fl->prepare('INSERT INTO `' . $fm_cfg['prefix'] . 'discussions` (id,title,participant_count,created_at,user_id,first_post_id,last_posted_at,last_posted_user_id,last_post_id,slug,is_sticky) VALUES (?,?,1,FROM_UNIXTIME(?),?,?,FROM_UNIXTIME(?),?,?,"",?)'); try { $data = [ $row['tid'], $row['subject'], $row['create_date'], $row['uid'], $row['firstpid'], $row['last_date'], $row['lastuid'], $row['lastpid'], $row['top'] > 0 ? 1 : 0, ]; $dbh_fl->beginTransaction(); $stmt->execute($data); $dbh_fl->commit(); // print $dbh_fl->lastInsertId(); } catch(PDOExecption $e) { $dbh_fl->rollback(); print "Error!: " . $e->getMessage() . "\n"; exit; } $tag_sql = 'INSERT INTO `' . $fm_cfg['prefix'] . 'discussion_tag` (discussion_id,tag_id) VALUES (?,?)'; $stmt = $dbh_fl->prepare($tag_sql); try { $data = [ $row['tid'], $row['fid'], ]; $dbh_fl->beginTransaction(); $stmt->execute($data); $dbh_fl->commit(); // print $dbh_fl->lastInsertId(); } catch(PDOExecption $e) { $dbh_fl->rollback(); print "Error!: " . $e->getMessage() . "\n"; exit; } $tag_sql = 'INSERT INTO `' . $fm_cfg['prefix'] . 'discussion_user` (user_id,discussion_id,last_read_at,last_read_post_number) VALUES (?,?,FROM_UNIXTIME(?),1)'; $stmt = $dbh_fl->prepare($tag_sql); try { $data = [ $row['uid'], $row['tid'], $row['last_date'], ]; $dbh_fl->beginTransaction(); $stmt->execute($data); $dbh_fl->commit(); // print $dbh_fl->lastInsertId(); } catch(PDOExecption $e) { $dbh_fl->rollback(); print "Error!: " . $e->getMessage() . "\n"; exit; } } print 'Insert table ' . $fm_cfg['prefix'] . "discussions\n"; } function insert_posts() { global $xn_cfg,$fm_cfg,$dbh_xn,$dbh_fl; $sth = $dbh_xn->prepare('SELECT * FROM `' . $xn_cfg['tablepre'] . 'post` ORDER BY `create_date` ASC'); $sth->execute(); // $result = $sth->fetchAll(PDO::FETCH_ASSOC); $dbh_fl->exec('DELETE FROM `' . $fm_cfg['prefix'] . 'posts`'); // $dbh_fl->exec('TRUNCATE TABLE`' . $fm_cfg['prefix'] . 'posts`'); $totals = []; while($row = $sth->fetch(PDO::FETCH_ASSOC)) { // print_r($row); $stmt = $dbh_fl->prepare('INSERT INTO `' . $fm_cfg['prefix'] . 'posts` (id,discussion_id,number,created_at,user_id,type,content,ip_address) VALUES (?,?,?,FROM_UNIXTIME(?),?,"comment",?,?)'); try { if (! isset($totals[$row['tid']])) { $totals[$row['tid']] = []; } $totals[$row['tid']][] = 1; // if ($row['isfirst'] === 1) { // $number_id = 1; // } else { // $totals[$row['tid']][] = 1; // } $number_id = count($totals[$row['tid']]); // XML representation, that's what you should store in your database $xml = TextFormatter::parse($row['message']); // HTML rendering, that's what you display to the user $html = TextFormatter::render($xml); $data = [ $row['pid'], $row['tid'], $number_id, $row['create_date'], $row['uid'], // '' . $row['message'] . '', // '' . $row['message_fmt'] . '', '' . $html . '', // $html, long2ip($row['userip']), ]; $dbh_fl->beginTransaction(); $stmt->execute($data); $dbh_fl->commit(); // print $dbh_fl->lastInsertId(); } catch(PDOExecption $e) { $dbh_fl->rollback(); print "Error!: " . $e->getMessage() . "\n"; exit; } } print 'Insert table ' . $fm_cfg['prefix'] . "posts\n"; } function fix_discussion_count() { global $fm_cfg,$dbh_fl; $sql = 'SELECT count(*) AS counts, `discussion_id` FROM `' . $fm_cfg['prefix'] . 'posts` GROUP BY `discussion_id`'; $sth = $dbh_fl->prepare($sql); $sth->execute(); while($row = $sth->fetch(PDO::FETCH_ASSOC)) { // print_r($row); $stmt = $dbh_fl->prepare('UPDATE `' . $fm_cfg['prefix'] . 'discussions` SET `comment_count` = ?, post_number_index = ?, last_post_number = ? WHERE id = ?'); try { $data = [ $row['counts'], $row['counts'], 1, $row['discussion_id'], ]; $dbh_fl->beginTransaction(); $stmt->execute($data); $dbh_fl->commit(); // print $dbh_fl->lastInsertId(); } catch(PDOExecption $e) { $dbh_fl->rollback(); print "Error!: " . $e->getMessage() . "\n"; exit; } } print 'Update table ' . $fm_cfg['prefix'] . "discussions counts\n"; } function fix_tag_stat() { global $fm_cfg,$dbh_fl; $sql = 'SELECT *, count(*) AS counts FROM `'. $fm_cfg['prefix'] . 'discussions` GROUP BY `slug` DESC;'; $sth = $dbh_fl->prepare($sql); $sth->execute(); while($row = $sth->fetch(PDO::FETCH_ASSOC)) { // print_r($row); $stmt = $dbh_fl->prepare('UPDATE `' . $fm_cfg['prefix'] . 'tags` SET `discussion_count` = ?, last_posted_at = ? , last_posted_discussion_id = ?, last_posted_user_id = ? WHERE slug = ?'); $last_uid = $row['last_posted_user_id']; if ($last_uid == 0) { $last_uid = $row['user_id']; } try { $data = [ $row['counts'], $row['last_posted_at'], $row['id'], $last_uid, $row['slug'], ]; $dbh_fl->beginTransaction(); $stmt->execute($data); $dbh_fl->commit(); // print $dbh_fl->lastInsertId(); } catch(PDOExecption $e) { $dbh_fl->rollback(); print "Error!: " . $e->getMessage() . "\n"; exit; } } print 'Update table ' . $fm_cfg['prefix'] . "tags stat\n"; } insert_tags(); insert_discussions(); insert_posts(); fix_discussion_count(); fix_tag_stat();