diff --git a/tyl-converter.php b/tyl-converter.php index 1fb06a3..9289108 100644 --- a/tyl-converter.php +++ b/tyl-converter.php @@ -1,17 +1,29 @@ get_input('from', MyBB::INPUT_STRING); if (!$db->table_exists($prefix.'thankyoulike_thankyoulike')) { echo 'Fatal error: The Thank You/Like System plugin is not installed.'; exit; -} else if ($db->table_exists('thx')) { - $do_conversion = true; - $query = $db->simple_select('thx', 'pid, adduid as uid, uid as puid, time as dateline'); - $tyl_from_type = 'thanks'; - $from_plugin_name = 'the Thanks plugin'; -} else if ($db->table_exists('post_likes')) { +} else if ($from === 'thanks' || empty($from) && $db->table_exists('thx')) { + if (!$db->table_exists('thx')) { + echo 'Fatal error: You selected via the "from" query parameter to convert from the Thanks plugin, however, its table was not found in the database.'; + } else { + $do_conversion = true; + $query = $db->simple_select('thx', 'pid, adduid as uid, uid as puid, time as dateline'); + $tyl_from_type = 'thanks'; + $from_plugin_name = 'the Thanks plugin'; + } +} else if ($from == 'simplelikes' || empty($from) && $db->table_exists('post_likes')) { + if (!$db->table_exists('post_likes')) { + echo 'Fatal error: You selected via the "from" query parameter to convert from the SimpleLikes plugin, however, its table was not found in the database.'; + } else { + $do_conversion = true; + $query = $db->query('SELECT pl.post_id as pid, pl.user_id as uid, p.uid as puid, UNIX_TIMESTAMP(pl.created_at) as dateline FROM '.TABLE_PREFIX.'post_likes pl INNER JOIN '.TABLE_PREFIX.'posts p ON pl.post_id = p.pid'); + $tyl_from_type = 'likes'; + $from_plugin_name = 'the SimpleLikes plugin'; + } +} else if ($from === 'reputation') { $do_conversion = true; - $query = $db->query('SELECT pl.post_id as pid, pl.user_id as uid, p.uid as puid, UNIX_TIMESTAMP(pl.created_at) as dateline FROM '.TABLE_PREFIX.'post_likes pl INNER JOIN '.TABLE_PREFIX.'posts p ON pl.post_id = p.pid'); - $tyl_from_type = 'likes'; - $from_plugin_name = 'the SimpleLikes plugin'; + $query = $db->simple_select('reputation', 'pid, adduid as uid, uid as puid, dateline', 'reputation > 0 AND pid > 0'); + $tyl_from_type = 'reputations'; + $from_plugin_name = 'core reputations (compatible with the MyLikes plugin)'; + $sort_function = function(&$rows) { + //https://stackoverflow.com/a/3233009 + # get a list of sort columns and their data to pass to array_multisort + $sort = array(); + $sort['pid'] = $sort['uid'] = array(); + foreach ($rows as $k => $v) { + $sort['pid'][$k] = $v['pid']; + $sort['uid'][$k] = $v['uid']; + } + # sort by pid asc and then uid asc + array_multisort($sort['pid'], SORT_ASC, $sort['uid'], SORT_ASC, $rows); + }; } else { - echo 'Fatal error: a compatible thanks/likes system from which to convert thanks/likes was not detected.'; + echo 'Fatal error: a compatible thanks/likes plugin from which to convert thanks/likes was not detected.'; exit; } @@ -43,22 +81,35 @@ if (empty($query)) { echo "Warning: no {$tyl_from_type} found in the database table for {$from_plugin_name}. Nothing to convert, so no conversion performed."; } else { - $batch = $total = 0; - $rows = array(); + $sel_rows = array(); while ($row = $db->fetch_array($query)) { - $rows[] = $row; - $batch++; - $total++; + $sel_rows[] = $row; + } + $db->free_result($query); + + if ($sort_function) { + $sort_function($sel_rows); + } + + $prev_pid = $prev_uid = $batch = $total = 0; + foreach ($sel_rows as $row) { + if ($prev_pid != (int)$row['pid'] || $prev_uid != (int)$row['uid']) { + $ins_rows[] = $row; + $prev_pid = (int)$row['pid']; + $prev_uid = (int)$row['uid']; + $batch++; + $total++; + } if ($batch == 1000) { - $db->insert_query_multiple($prefix.'thankyoulike_thankyoulike', $rows); - $rows = array(); + $db->insert_query_multiple($prefix.'thankyoulike_thankyoulike', $ins_rows); + $ins_rows = array(); $batch = 0; - echo "Converted {$total} {$tyl_from_type} from {$from_plugin_name} into thank yous / likes for the Thank You/Like System plugin so far.
"; + echo "Converted {$total} {$tyl_from_type} so far from {$from_plugin_name} into thank yous / likes for the Thank You/Like System plugin.
"; } } if ($batch > 0) { - $db->insert_query_multiple($prefix.'thankyoulike_thankyoulike', $rows); + $db->insert_query_multiple($prefix.'thankyoulike_thankyoulike', $ins_rows); } - echo "Done! Converted {$total} {$tyl_from_type} from {$from_plugin_name} into thank yous / likes for the Thank You/Like System plugin in total."; + echo "Done! Converted {$total} {$tyl_from_type} in total from {$from_plugin_name} into thank yous / likes for the Thank You/Like System plugin."; } }