#!/usr/bin/env php
<?php
/**
 * This script converts the data in an SQL backend from any supported charset
 * to UTF-8.
 *
 * Copyright 2011-2016 Horde LLC (http://www.horde.org/)
 *
 * See the enclosed file LICENSE for license information (BSD). If you
 * did not receive this file, see http://www.horde.org/licenses/bsdl.php.
 *
 * @author Jan Schneider <jan@horde.org>
 */

if (file_exists(__DIR__ . '/../../whups/lib/Application.php')) {
    $baseDir = __DIR__ . '/../';
} else {
    require_once 'PEAR/Config.php';
    $baseDir = PEAR_Config::singleton()
        ->get('horde_dir', null, 'pear.horde.org') . '/whups/';
}
require_once $baseDir . 'lib/Application.php';
Horde_Registry::appInit('whups', array('cli' => true));

if ($conf['tickets']['driver'] != 'Sql') {
    exit("You must have an SQL backend configured.\n");
}

// Get current charset.
$charset = $cli->prompt('Please specify the current charset of the data',
                        null, 'ISO-8859-1');

$dbFactory = $injector->getInstance('Horde_Core_Factory_Db');
$config = $dbFactory->getConfig('tickets');
$config['charset'] = $charset;
$dbin = $dbFactory->create('whups', $config);
$config['charset'] = 'UTF-8';
$dbout = $dbFactory->create('whups', $config);
$mysql = stristr($dbout->adapterName(), 'mysql');

// Convert queues.
try {
    $results = $dbin->select('SELECT queue_id, queue_name, queue_description FROM whups_queues');
    $sth = 'UPDATE whups_queues SET queue_name = ?, queue_description = ?'
        . ' WHERE queue_id = ?';
    echo 'Converting queues';
    foreach ($results as $row) {
        $values = Horde_String::convertCharset(
            array($row['queue_name'], $row['queue_description']),
            $charset, 'UTF-8');
        $values[] = $row['queue_id'];
        $dbout->update($sth, $values);
        echo '.';
    }
    $cli->writeln($cli->green('Done'));
} catch (Horde_Db_Exception $e) {
    $cli->fatal($e->getMessage());
}

// Convert types.
try {
    $results = $dbin->select('SELECT type_id, type_name, type_description FROM whups_types');
    $sth = 'UPDATE whups_types SET type_name = ?, type_description = ?'
        . ' WHERE type_id = ?';
    echo 'Converting types';
    foreach ($results as $row) {
        $values = Horde_String::convertCharset(
            array($row['type_name'], $row['type_description']),
            $charset, 'UTF-8');
        $values[] = $row['type_id'];
        $dbout->update($sth, $values);
        echo '.';
    }
    $cli->writeln($cli->green('Done'));
} catch (Horde_Db_Exception $e) {
    $cli->fatal($e->getMessage());
}

// Convert states.
try {
    $results = $dbin->select('SELECT state_id, state_name, state_description, state_category FROM whups_states');
    $sth = 'UPDATE whups_states SET state_name = ?, state_description = ?, state_category = ?'
        . ' WHERE state_id = ?';
    echo 'Converting states';
    foreach ($results as $row) {
        $values = Horde_String::convertCharset(
            array($row['state_name'], $row['state_description'], $row['state_category']),
            $charset, 'UTF-8');
        $values[] = $row['state_id'];
        $dbout->update($sth, $values);
        echo '.';
    }
    $cli->writeln($cli->green('Done'));
} catch (Horde_Db_Exception $e) {
    $cli->fatal($e->getMessage());
}

// Convert priorities.
try {
    $results = $dbin->select('SELECT priority_id, priority_name, priority_description FROM whups_priorities');
    $sth = 'UPDATE whups_priorities SET priority_name = ?, priority_description = ?'
        . ' WHERE priority_id = ?';
    echo 'Converting priorities';
    foreach ($results as $row) {
        $values = Horde_String::convertCharset(
            array($row['priority_name'], $row['priority_description']),
            $charset, 'UTF-8');
        $values[] = $row['priority_id'];
        $dbout->update($sth, $values);
        echo '.';
    }
    $cli->writeln($cli->green('Done'));
} catch (Horde_Db_Exception $e) {
    $cli->fatal($e->getMessage());
}

// Convert versions.
try {
    $results = $dbin->select('SELECT version_id, version_name, version_description FROM whups_versions');
    $sth = 'UPDATE whups_versions SET version_name = ?, version_description = ?'
        . ' WHERE version_id = ?';
    echo 'Converting versions';
    foreach ($results as $row) {
        $values = Horde_String::convertCharset(
            array($row['version_name'], $row['version_description']),
            $charset, 'UTF-8');
        $values[] = $row['version_id'];
        $dbout->update($sth, $values);
        echo '.';
    }
    $cli->writeln($cli->green('Done'));
} catch (Horde_Db_Exception $e) {
    $cli->fatal($e->getMessage());
}

// Convert replies.
try {
    $results = $dbin->select('SELECT reply_id, reply_name, reply_text FROM whups_replies');
    $sth = 'UPDATE whups_replies SET reply_name = ?, reply_text = ?'
        . ' WHERE reply_id = ?';
    echo 'Converting replies';
    foreach ($results as $row) {
        $values = Horde_String::convertCharset(
            array($row['reply_name'], $row['reply_text']),
            $charset, 'UTF-8');
        $values[] = $row['reply_id'];
        $dbout->update($sth, $values);
        echo '.';
    }
    $cli->writeln($cli->green('Done'));
} catch (Horde_Db_Exception $e) {
    $cli->fatal($e->getMessage());
}

// Convert tickets.
try {
    $results = $dbin->select('SELECT ticket_id, ticket_summary FROM whups_tickets');
    $sth = 'UPDATE whups_tickets SET ticket_summary = ?'
        . ' WHERE ticket_id = ?';
    echo 'Converting tickets';
    foreach ($results as $row) {
        $values = Horde_String::convertCharset(
            array($row['ticket_summary']),
            $charset, 'UTF-8');
        $values[] = $row['ticket_id'];
        $dbout->update($sth, $values);
        echo '.';
    }
    $cli->writeln($cli->green('Done'));
} catch (Horde_Db_Exception $e) {
    $cli->fatal($e->getMessage());
}

// Convert comments.
try {
    $results = $dbin->select('SELECT comment_id, comment_text FROM whups_comments');
    $sth = 'UPDATE whups_comments SET comment_text = ?'
        . ' WHERE comment_id = ?';
    echo 'Converting comments';
    foreach ($results as $row) {
        $values = Horde_String::convertCharset(
            array($row['comment_text']),
            $charset, 'UTF-8');
        $values[] = $row['comment_id'];
        $dbout->update($sth, $values);
        echo '.';
    }
    $cli->writeln($cli->green('Done'));
} catch (Horde_Db_Exception $e) {
    $cli->fatal($e->getMessage());
}

// Convert queries.
try {
    $results = $dbin->select('SELECT query_id, query_parameters, query_object FROM whups_queries');
    $sth = 'UPDATE whups_queries SET query_parameters = ?, query_object = ?'
        . ' WHERE query_id = ?';
    echo 'Converting queries';
    foreach ($results as $row) {
        $values = Horde_String::convertCharset(
            array($row['query_parameters'], $row['query_object']),
            $charset, 'UTF-8');
        $values[] = $row['query_id'];
        $dbout->update($sth, $values);
        echo '.';
    }
    $cli->writeln($cli->green('Done'));
} catch (Horde_Db_Exception $e) {
    $cli->fatal($e->getMessage());
}

// Convert attributes.
try {
    echo 'Converting attributes';
    $results = $dbin->select('SELECT attribute_id, attribute_name, attribute_description, attribute_params FROM whups_attributes_desc');
    $sth = 'UPDATE whups_attributes_desc SET attribute_name = ?, attribute_description = ?, attribute_params = ?'
        . ' WHERE attribute_id = ?';
    foreach ($results as $row) {
        $values = Horde_String::convertCharset(
            array($row['attribute_name'], $row['attribute_description']),
            $charset, 'UTF-8');
        $values[] = serialize(Horde_String::convertCharset(unserialize($row['attribute_params']), $charset, 'UTF-8'));
        $values[] = $row['attribute_id'];
        $dbout->update($sth, $values);
        echo '.';
    }

    $results = $dbin->select('SELECT ticket_id, attribute_id, attribute_value FROM whups_attributes');
    $sth = 'UPDATE whups_attributes SET attribute_value = ?'
        . ' WHERE ticket_id = ? AND attribute_id = ?';
    foreach ($results as $row) {
        $values = Horde_String::convertCharset(
            array($row['attribute_value']),
            $charset, 'UTF-8');
        $values[] = $row['ticket_id'];
        $values[] = $row['attribute_id'];
        $dbout->update($sth, $values);
        echo '.';
    }
    $cli->writeln($cli->green('Done'));
} catch (Horde_Db_Exception $e) {
    $cli->fatal($e->getMessage());
}

// Convert logs.
try {
    $results = $dbin->select('SELECT log_id, log_value FROM whups_logs');
    $sth = 'UPDATE whups_logs SET log_value = ?'
        . ' WHERE log_id = ?';
    echo 'Converting logs';
    foreach ($results as $row) {
        $values = Horde_String::convertCharset(
            array($row['log_value']),
            $charset, 'UTF-8');
        $values[] = $row['log_id'];
        $dbout->update($sth, $values);
        echo '.';
    }
    $cli->writeln($cli->green('Done'));
} catch (Horde_Db_Exception $e) {
    $cli->fatal($e->getMessage());
}
