wiclear-2007-07-19/admin/tools/utf8conversion/index.php

<?php
# ***** BEGIN LICENSE BLOCK *****
# This file is part of WiClear.
# Copyright (c) 2004-2007 David Jobet. All rights
# reserved.
#
# WiClear is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# WiClear is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with DotClear; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
#
# ***** END LICENSE BLOCK *****

require wc_inc_path.'inc/classes/check_list.class.php';

function convertTableToUTF8($table, $id, $colNames)
{
  global $con;

  //$con->setDebug(true);

  $query = 'SELECT '.$id;
  foreach ($colNames as $name)
  {
    $query .= ', '.$name;
  }
  $query .= ' FROM '.wc_db_prefix.$table;
  $result = $con->select($query);

  $nbRows = sizeof($result);
  for ($i = 0; $i < $nbRows; ++$i)
  {
    $nbCols = sizeof($result[$i]);

    for ($j = 1; $j < $nbCols; ++$j)
    {
      $result[$i][$j] = utf8_helper::latin1ToUTF8($result[$i][$j]);
    }

    $query = 'UPDATE '.wc_db_prefix.$table.' SET';
    $j = 1;
    foreach ($colNames as $name)
    {
      if ($j > 1)
      {
        $query .= ',';
      }

      $query .= ' '.$name."='".$con->escapeString($result[$i][$j])."'";
      ++$j;
    }
    $query .= ' WHERE '.$id."='".$result[$i][0]."';";
    if (!$con->execute($query))
    {
      return false;
    }
  }

  return true;
}

if (isset($_POST['submit']))
{
  // let's convert
  $check = new check_list('ok', 'nok');
  global $con;

  // retrieve list of tables
  $query = 'SHOW TABLES';
  $tables = $con->select($query);

  foreach ($tables as $table0)
  {
    $table = $table0[0];

    if (substr($table, 0, strlen(wc_db_prefix)) == wc_db_prefix)
    {
      // this is a wiclear table : convert it
      $query = <<<SQL
        ALTER TABLE $table CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
SQL;
      $ok = $con->execute($query);

      if ($ok)
      {
        // and now set default encoding
        $query = <<<SQL
          ALTER TABLE $table DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
SQL;

        $ok = $con->execute($query);
      }

      $wc_table = substr($table, strlen(wc_db_prefix));
      $check->addItem(
        $ok,
        tr('Table').' '.$wc_table.' '.tr('successfully converted'),
        tr('Error while converting table').' '.$wc_table.' : '.$con->error
      );
    }
  }

  // user
  $check->addItem(
    convertTableToUTF8('user', 'id', array('firstname', 'lastname')),
    tr('Table user successfully converted'),
    tr('Error while converting table user : '.$con->error)
  );

  // content
  $check->addItem(
    convertTableToUTF8('content', 'id', array('title', 'alternate_title', 'content')),
    tr('Table content successfully converted'),
    tr('Error while converting table content : '.$con->error)
  );

  // comment
  $check->addItem(
    convertTableToUTF8('comment', 'id', array('title', 'content', 'name')),
    tr('Table comment successfully converted'),
    tr('Error while converting table comment : '.$con->error)
  );

  // history
  $check->addItem(
    convertTableToUTF8('history', 'id', array('title', 'alternate_title', 'content')),
    tr('Table history successfully converted'),
    tr('Error while converting table history : '.$con->error)
  );

  // group
  $check->addItem(
    convertTableToUTF8('group', 'id', array('name')),
    tr('Table group successfully converted'),
    tr('Error while converting table group : '.$con->error)
  );

  // lang
  $check->addItem(
    convertTableToUTF8('lang', 'id', array('code', 'description')),
    tr('Table lang successfully converted'),
    tr('Error while converting table lang : '.$con->error)
  );

  $form = new form(createAdminURL(array('mode'=>'tools')), 'post');
  $form->addFieldset();

  // set charset in conf
  global $ini;
  $ini->addConstant('wc_charset', 'UTF-8');
  $form->addLegend(tr('Report'));

  $form->addHtml($check->htmlReport());

  if ($check->allOK())
  {
    $form->addHtml('<p>'.tr('Database converted to UTF-8').'</p>');
  }
  else
  {
    $form->addHtml('<p>'.tr('There were errors during conversion').'</p>');
  }

  if ($check->allOK)
  {
    if ($ini->forceSaveFile())
    {
      $form->addHtml('<p>'.tr('Configuration file saved').'</p>');
    }
    else
    {
      $form->addHtml('<p>'.tr("Could not save configuration file. Set manually 'wc_charset' to to 'UTF-8'").'</p>');
    }
  }
  else
  {
    $form->addHtml('<p>'.tr('Configuration file not saved because of previous error. Restore your database').'</p>');
  }

  $form->addButton('submit', tr('Return to tools'));

  echo $form->toHtml();

  return;
}

$form = new form(createToolURL('utf8conversion'), 'post');
$form->addFieldset();
$form->addLegend(tr('UTF8 conversion'));
$form->addHtml('<h1>'.tr('Welcome to the UTF8 conversion tool').'</h1>');
$form->addHtml('<p>'.tr('You should backup your database first').'</p>');

$disabled = false;
if (wc_charset == 'UTF-8')
{
  $form->addHtml('<p>'.tr('Your database is already in UTF-8 mode according to your configuration').'</p>');
  $disabled = true;
}
if (!function_exists('utf8_encode'))
{
  $form->addHtml('<p>'.tr('php utf8_encode function not found').'</p>');
  $disabled = true;
}

$form->addButton('submit', tr('Go'), '', $disabled);

echo $form->toHtml();
?>