[feature/migrations] Fix minor issues in migrations data script

Fixed incorrect IDs, line endings, and add tabbing.

B3P-101
This commit is contained in:
Marc Alexander
2013-03-08 23:22:56 +01:00
parent bd2ea48ecb
commit 7e25bf46aa

View File

@@ -1,291 +1,297 @@
<?php <?php
/** /**
* *
* @package Board3 Portal v2.1 * @package Board3 Portal v2.1
* @copyright (c) 2013 Board3 Group ( www.board3.de ) * @copyright (c) 2013 Board3 Group ( www.board3.de )
* @license http://opensource.org/licenses/gpl-license.php GNU Public License * @license http://opensource.org/licenses/gpl-license.php GNU Public License
* *
*/ */
die('This script shouldn\'t be run unless you really know what you do. If this script exists on a live board, please delete it.'); die('This script shouldn\'t be run unless you really know what you do. If this script exists on a live board, please delete it.');
define('IN_PHPBB', true); define('IN_PHPBB', true);
define('B3_MODULE_ENABLED', 1); define('B3_MODULE_ENABLED', 1);
define('GROUPS_TABLE', '$this->table_prefix . \'groups'); define('GROUPS_TABLE', '$this->table_prefix . \'groups');
$php_ex = substr(strrchr(__FILE__, '.'), 1); $php_ex = substr(strrchr(__FILE__, '.'), 1);
$phpEx = $php_ex; $phpEx = $php_ex;
$config_entry = $portal_config_entry = $db_data = array(); $config_entry = $portal_config_entry = $db_data = array();
$root_path = '../'; // one directory down $root_path = '../'; // one directory down
function set_config($name, $val) function set_config($name, $val)
{ {
global $config_entry; global $config_entry;
if (isset($config_entry[$name])) if (isset($config_entry[$name]))
{ {
trigger_error('Duplicate entry: ' . $name); trigger_error('Duplicate entry: ' . $name);
} }
handle_string($val); handle_string($val);
$config_entry[$name] = $val; $config_entry[$name] = $val;
} }
function handle_string(&$str) function handle_string(&$str)
{ {
if (is_string($str)) if (is_string($str))
{ {
$str = "'$str'"; $str = "'$str'";
} }
if (empty($str)) if (empty($str))
{ {
$str = "''"; $str = "''";
} }
} }
function set_portal_config($name, $val) function set_portal_config($name, $val)
{ {
global $portal_config_entry; global $portal_config_entry;
if (isset($portal_config_entry[$name])) if (isset($portal_config_entry[$name]))
{ {
trigger_error('Duplicate entry: ' . $name); trigger_error('Duplicate entry: ' . $name);
} }
handle_string($val); handle_string($val);
// we do not want serialized entries // we do not want serialized entries as they are hard to read
if (strpos($val, 'a:') === 1) if (strpos($val, 'a:') === 1)
{ {
// cut preceding and appended quote // cut preceding and appended quote
$val = substr($val, 1, -1); $val = substr($val, 1, -1);
// start unserializing and building // start unserializing and building
$val = unserialize($val); $val = unserialize($val);
$after_val = 'array(<br />'; $after_val = 'serialize(array(<br />';
foreach ($val as $key => $entry) foreach ($val as $key => $entry)
{ {
if (is_array($entry)) if (is_array($entry))
{ {
$after_val .= '&nbsp;&nbsp;array(<br />'; $after_val .= ' array(<br />';
foreach ($entry as $one => $two) foreach ($entry as $one => $two)
{ {
handle_string($one); handle_string($one);
handle_string($two); handle_string($two);
$after_val .= '&nbsp;&nbsp;&nbsp;' . $one . '&nbsp;&nbsp;&nbsp;=> ' . $two . ',<br />'; $after_val .= ' ' . $one . ' => ' . $two . ',<br />';
} }
$after_val .= '&nbsp;&nbsp;),<br />'; $after_val .= ' ),<br />';
} }
else else
{ {
handle_string($key); handle_string($key);
handle_string($entry); handle_string($entry);
$after_val .= '&nbsp;&nbsp;' . $key . '&nbsp;&nbsp;&nbsp;=> ' . $entry . ',<br />'; $after_val .= ' ' . $key . ' => ' . $entry . ',<br />';
} }
} }
$after_val .= ')'; $after_val .= ' ))';
$val = $after_val; $val = $after_val;
} }
$portal_config_entry[$name] = $val; $portal_config_entry[$name] = $val;
} }
$db = new db($db_data); $db = new db($db_data);
board3_get_install_data($db, $root_path, $php_ex, $db_data); board3_get_install_data($db, $root_path, $php_ex, $db_data);
echo 'set_config entries for migrations:<br /><pre>'; echo 'set_config entries for migrations:<br /><pre>';
foreach ($config_entry as $name => $val) foreach ($config_entry as $name => $val)
{ {
echo 'array(\'config.add\', array(\'' . $name . '\', ' . $val . ')),<br />'; echo 'array(\'config.add\', array(\'' . $name . '\', ' . $val . ')),<br />';
} }
echo '</pre>'; echo '</pre>';
echo '<br /><br />set_portal_config entries for migrations:<br /><pre>'; echo '<br /><br />set_portal_config entries for migrations:<br /><pre>';
foreach ($portal_config_entry as $name => $val) foreach ($portal_config_entry as $name => $val)
{ {
echo 'set_portal_config(\'' . $name . '\', ' . $val . ');<br />'; echo ' set_portal_config(\'' . $name . '\', ' . $val . ');<br />';
} }
echo '</pre>'; echo '</pre>';
echo '<br /><br />database entries:<br /><pre>'; echo '<br /><br />database entries:<br /><pre>';
echo $db_data . '</pre><br />'; echo $db_data . '</pre><br />';
/** /**
* This function will install the basic set of portal modules * This function will install the basic set of portal modules
* *
* only set $purge_modules to false if you already know that the table is empty * only set $purge_modules to false if you already know that the table is empty
* set $u_action to where the user should be redirected after this * set $u_action to where the user should be redirected after this
* note that already existing data won't be deleted from the config and portal_config * note that already existing data won't be deleted from the config and portal_config
* just to make sure we don't overwrite anything, the IDs won't be reset * just to make sure we don't overwrite anything, the IDs won't be reset
* !! this function should usually only be executed once upon installing the portal !! * !! this function should usually only be executed once upon installing the portal !!
* DO NOT set $purge_modules to false unless you want to auto-add all modules again after deleting them (i.e. if your database was corrupted) * DO NOT set $purge_modules to false unless you want to auto-add all modules again after deleting them (i.e. if your database was corrupted)
*/ */
function board3_get_install_data($db, $root_path, $php_ex, &$db_data) function board3_get_install_data($db, $root_path, $php_ex, &$db_data)
{ {
$directory = $root_path . 'portal/modules/'; $directory = $root_path . 'portal/modules/';
$db_data = '$board3_sql_query = array(<br />'; $db_data = ' $board3_sql_query = array(<br />';
/* /*
* this is a list of the basic modules that will be installed * this is a list of the basic modules that will be installed
* module_name => array(module_column, module_order) * module_name => array(module_column, module_order)
*/ */
$modules_ary = array( $modules_ary = array(
// left column // left column
'portal_main_menu' => array(1, 1), 'portal_main_menu' => array(1, 1),
'portal_stylechanger' => array(1, 2), 'portal_stylechanger' => array(1, 2),
'portal_birthday_list' => array(1, 3), 'portal_birthday_list' => array(1, 3),
'portal_clock' => array(1, 4), 'portal_clock' => array(1, 4),
'portal_search' => array(1, 5), 'portal_search' => array(1, 5),
'portal_attachments' => array(1, 6), 'portal_attachments' => array(1, 6),
'portal_topposters' => array(1, 7), 'portal_topposters' => array(1, 7),
'portal_latest_members' => array(1, 8), 'portal_latest_members' => array(1, 8),
'portal_link_us' => array(1, 9), 'portal_link_us' => array(1, 9),
// center column // center column
'portal_welcome' => array(2, 1), 'portal_welcome' => array(2, 1),
'portal_recent' => array(2, 2), 'portal_recent' => array(2, 2),
'portal_announcements' => array(2, 3), 'portal_announcements' => array(2, 3),
'portal_news' => array(2, 4), 'portal_news' => array(2, 4),
'portal_poll' => array(2, 5), 'portal_poll' => array(2, 5),
'portal_whois_online' => array(2, 6), 'portal_whois_online' => array(2, 6),
// right column // right column
'portal_user_menu' => array(3, 1), 'portal_user_menu' => array(3, 1),
'portal_statistics' => array(3, 2), 'portal_statistics' => array(3, 2),
'portal_calendar' => array(3, 3), 'portal_calendar' => array(3, 3),
'portal_leaders' => array(3, 4), 'portal_leaders' => array(3, 4),
'portal_latest_bots' => array(3, 5), 'portal_latest_bots' => array(3, 5),
'portal_links' => array(3, 6), 'portal_links' => array(3, 6),
); );
foreach ($modules_ary as $module_name => $module_data) foreach ($modules_ary as $module_name => $module_data)
{ {
$class_name = $module_name . '_module'; $class_name = $module_name . '_module';
if (!class_exists($class_name)) if (!class_exists($class_name))
{ {
include($directory . $module_name . '.' . $php_ex); include($directory . $module_name . '.' . $php_ex);
} }
if (!class_exists($class_name)) if (!class_exists($class_name))
{ {
trigger_error('Class not found', E_USER_ERROR); trigger_error('Class not found', E_USER_ERROR);
} }
$c_class = new $class_name(); $c_class = new $class_name();
$sql_ary = array( $sql_ary = array(
'module_classname' => substr($module_name, 7), 'module_classname' => substr($module_name, 7),
'module_column' => $module_data[0], 'module_column' => $module_data[0],
'module_order' => $module_data[1], 'module_order' => $module_data[1],
'module_name' => $c_class->name, 'module_name' => $c_class->name,
'module_image_src' => $c_class->image_src, 'module_image_src' => $c_class->image_src,
'module_group_ids' => '', 'module_group_ids' => '',
'module_image_width' => 16, 'module_image_width' => 16,
'module_image_height' => 16, 'module_image_height' => 16,
'module_status' => B3_MODULE_ENABLED, 'module_status' => B3_MODULE_ENABLED,
); );
$sql = 'INSERT INTO \' . $this->table_prefix . \'portal_modules ' . $db->sql_build_array('INSERT', $sql_ary); $sql = 'INSERT INTO \' . $this->table_prefix . \'portal_modules ' . $db->sql_build_array('INSERT', $sql_ary);
$db->sql_query($sql); $db->sql_query($sql, true);
$data1 = array(); $data1 = array();
$data2 = array(); $data2 = array();
$db_data .= 'array(<br />'; $db_data .= ' array(<br />';
foreach ($sql_ary as $key => $val) foreach ($sql_ary as $key => $val)
{ {
$key = (is_string($key)) ? '\'' . $key . '\'' : $key; $key = (is_string($key)) ? '\'' . $key . '\'' : $key;
$val = (is_string($val)) ? '\'' . $val . '\'' : $val; $val = (is_string($val)) ? '\'' . $val . '\'' : $val;
$db_data .= '&nbsp;' . $key . '&nbsp;&nbsp;&nbsp;=> ' . $val . ',<br />'; $db_data .= ' ' . $key . ' => ' . $val . ',<br />';
} }
$db_data .= '),<br />'; $db_data .= ' ),<br />';
$c_class->install($db->sql_id());
$c_class->install($db->sql_id()); }
} $db_data .= ' );';
} }
class db class db
{ {
// start at 0 // start at 0
private $id = 0; private $sql_id = 0;
private $int_pointer = 0; private $id = 0;
private $sql_ary = array(); private $int_pointer = 0;
private $sql_in_set = array(); private $sql_ary = array();
private $data = array(); private $sql_in_set = array();
public function __construct(&$data) private $data = array();
{
$this->data = &$data; public function __construct(&$data)
} {
public function sql_id() $this->data = &$data;
{ }
return $this->id; public function sql_id()
} {
return $this->sql_id;
public function sql_query($sql) }
{
if (strpos($sql, 'INSERT') !== false) public function sql_query($sql, $increase = false)
{ {
//$this->data[] = $sql; if (strpos($sql, 'INSERT') !== false)
} {
$this->id++; //$this->data[] = $sql;
$this->sql_ary[$this->id] = $sql; }
return $this->id; if ($increase)
} {
$this->sql_id++;
public function sql_build_array($type, $ary) }
{ $this->id++;
$data1 = array(); $this->sql_ary[$this->id] = $sql;
$data2 = array(); return $this->id;
foreach ($ary as $key => $val) }
{
$data1[] = $key; public function sql_build_array($type, $ary)
$data2[] = (is_string($val)) ? '\'' . $val . '\'' : $val; {
} $data1 = array();
return '(' . implode(', ', $data1) . ') VALUES (' . implode(', ', $data2) . ');'; $data2 = array();
} foreach ($ary as $key => $val)
{
public function sql_in_set($data1, $data2, $bool = -1) $data1[] = $key;
{ $data2[] = (is_string($val)) ? '\'' . $val . '\'' : $val;
$this->sql_in_set[$this->id + 1] = array($data1, $data2); }
return '\' . $db->sql_in_set('. $data1 . ', array(' . implode(',', $data2) . (($bool !== -1) ? '), ' . $bool : '') . '))'; return '(' . implode(', ', $data1) . ') VALUES (' . implode(', ', $data2) . ');';
} }
public function sql_fetchrow($id) public function sql_in_set($data1, $data2, $bool = -1)
{ {
if (isset($this->sql_ary[$id])) $this->sql_in_set[$this->id + 1] = array($data1, $data2);
{ return '\' . $db->sql_in_set('. $data1 . ', array(' . implode(',', $data2) . (($bool !== -1) ? '), ' . $bool : '') . '))';
preg_match_all('/SELECT+[a-z0-9A-Z,_ ]+FROM/', $this->sql_ary[$id], $match); }
if (!empty($match))
{ public function sql_fetchrow($id)
// cut "SELECT " and " FROM" {
$match = substr($match[0][0], 7, strlen($match[0][0]) - 5 - 7); if (isset($this->sql_ary[$id]))
$match = str_replace(', ', ',', $match); {
$match = explode(',', $match); preg_match_all('/SELECT+[a-z0-9A-Z,_ ]+FROM/', $this->sql_ary[$id], $match);
if (isset($this->sql_in_set[$id][1][$this->int_pointer])) if (!empty($match))
{ {
$ret = array(); // cut "SELECT " and " FROM"
foreach ($match as $key) $match = substr($match[0][0], 7, strlen($match[0][0]) - 5 - 7);
{ $match = str_replace(', ', ',', $match);
if ($key == $this->sql_in_set[$id][0]) $match = explode(',', $match);
{ if (isset($this->sql_in_set[$id][1][$this->int_pointer]))
$ret[$key] = $this->sql_in_set[$id][1][$this->int_pointer]; {
} $ret = array();
else foreach ($match as $key)
{ {
$ret[$key] = "{foobar.{$key}.{$this->sql_in_set[$id][1][$this->int_pointer]}}"; if ($key == $this->sql_in_set[$id][0])
} {
} $ret[$key] = $this->sql_in_set[$id][1][$this->int_pointer];
$this->int_pointer++;; }
return $ret; else
} {
} $ret[$key] = "{foobar.{$key}.{$this->sql_in_set[$id][1][$this->int_pointer]}}";
} }
else }
{ $this->int_pointer++;;
return false; return $ret;
} }
} }
} }
else
{
return false;
}
}
}