<?php
/**
 * This script is forced to patch user_datebirth in existing databases.
 * Copy it to your Cotonti root as patch.php and run in a web browser.
 *
 * @package Cotonti
 * @version 0.0.6
 * @author Trustmaster
 * @copyright Copyright (c) Cotonti Team 2008-2009
 * @license BSD
 */

define('SED_CODE', TRUE);
$location = 'Pages';

require_once('./datas/config.php');
require_once($cfg['system_dir'].'/functions.php');
require_once($cfg['system_dir'].'/common.php');

// Create temporary table and copy existing values
$sql = <<<SQL
CREATE TABLE sed_tmp (u_id int(11) unsigned NOT NULL, u_birthdate int(11) NOT NULL);
INSERT INTO sed_tmp (u_id, u_birthdate)
	SELECT user_id, user_birthdate FROM sed_users
SQL;

foreach (explode(';', $sql) as $q) sed_sql_query($q);

// Drop user_birthdate and restore it from temp
$sql = <<<SQL
ALTER TABLE sed_users DROP user_birthdate;
ALTER TABLE sed_users ADD user_birthdate DATE NOT NULL DEFAULT '1970-01-01'
SQL;
foreach (explode(';', $sql) as $q) sed_sql_query($q);

$res = sed_sql_query('SELECT u_id, u_birthdate FROM sed_tmp');
while ($row = sed_sql_fetchassoc($res))
{
	$bdate = sed_stamp2date($row['u_birthdate']);
	sed_sql_query("UPDATE sed_users SET user_birthdate = '$bdate' WHERE user_id = " . $row['u_id']);
}
sed_sql_freeresult($res);

sed_sql_query('DROP TABLE sed_tmp');

// Rename [list=1] to [ol]
$res = sed_sql_query("SELECT page_id, page_text FROM $db_pages WHERE page_text LIKE '%[list=1]%'");
while ($row = sed_sql_fetchassoc($res))
{
	$text = sed_sql_prep(preg_replace('#\[list=1\](.*?)\[/list\]#mis', '[ol]$1[/ol]', $row['page_text']));
	sed_sql_query("UPDATE $db_pages SET page_text='$text' WHERE page_id = " . $row['page_id']);
}
sed_sql_freeresult($res);

// Clear the cache
sed_cache_clearall();
sed_cache_clearhtml();

echo 'Conversion completed';
ob_end_flush();
?>
