Skip to content

Instantly share code, notes, and snippets.

@kode54
Created January 10, 2016 08:15
Show Gist options
  • Save kode54/a4ec219b9ce4b1b750d3 to your computer and use it in GitHub Desktop.
Save kode54/a4ec219b9ce4b1b750d3 to your computer and use it in GitHub Desktop.
<?php
$dsns = 'mysql:dbname=ipb21;host=localhost';
$dsn = 'mysql:dbname=elkarte;host=localhost';
$dbuser = '<user>';
$dbuserpw = '<password>';
$sql = 'SELECT * FROM `ibf_polls` LIMIT ';
$sql2 = ', 100';
$sql_upd = 'UPDATE elkarte_polls SET question = :question WHERE id_poll = :id_poll';
$sql_ins = 'INSERT INTO elkarte_poll_choices VALUES (:id_poll, :id_choice, :label, :votes)';
$dbh = 0;
$dbhw = 0;
$sth_upd = 0;
$sth_ins = 0;
$update_count = 0;
$offset = 0;
$orphaned_polls = array();
try
{
$dbh = new PDO($dsns, $dbuser, $dbuserpw);
$dbhw = new PDO($dsn, $dbuser, $dbuserpw);
$sth_upd = $dbhw->prepare($sql_upd, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$sth_ins = $dbhw->prepare($sql_ins, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
}
catch (PDOException $e)
{
echo 'There was a problem connecting to the database: ' . $e->getMessage();
die;
}
$dbhw->exec('TRUNCATE elkarte_poll_choices');
while (1)
{
$st = $dbh->query($sql . intval($offset) . $sql2);
if ($st)
{
$rows = $st->fetchAll(PDO::FETCH_ASSOC);
if (!count($rows))
break;
foreach ($rows as $row)
{
$ar = unserialize($row['choices']);
if (is_array($ar))
{
foreach ($ar as $a)
{
if (!isset($a['choice']))
{
// Old style poll
$sth_upd->execute(array(':id_poll' => $row['pid'], ':question' => $row['poll_question'] ? $row['poll_question'] : ''));
foreach ($ar as $choice)
{
$sth_ins->execute(array(':id_poll' => $row['pid'], ':id_choice' => $choice[0], ':label' => $choice[1], ':votes' => $choice[2]));
}
}
else
{
// New poll
// Blargh, drop the other questions
$poll = $a;
$sth_upd->execute(array(':id_poll' => $row['pid'], ':question' => isset($poll['question']) ? $poll['question'] : ''));
foreach ($poll['choice'] as $idx => $choice)
{
$sth_ins->execute(array(':id_poll' => $row['pid'], ':id_choice' => $idx, ':label' => $choice, ':votes' => $poll['votes'][$idx]));
}
}
break;
}
}
}
$offset += 100;
}
else
break;
}
echo "\n\n";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment