1
0
mirror of https://github.com/e107inc/e107.git synced 2025-07-26 01:11:28 +02:00

More work on forum upgrade

This commit is contained in:
mcfly
2008-12-20 00:55:29 +00:00
parent 3bbe99b45c
commit 47bc0ec970
2 changed files with 606 additions and 476 deletions

View File

@@ -11,20 +11,20 @@
| GNU General Public License (http://gnu.org).
|
| $Source: /cvs_backup/e107_0.8/e107_handlers/db_table_admin_class.php,v $
| $Revision: 1.6 $
| $Date: 2008-11-02 14:54:44 $
| $Author: e107steved $
| $Revision: 1.7 $
| $Date: 2008-12-20 00:55:29 $
| $Author: mcfly_e107 $
+----------------------------------------------------------------------------+
*/
/*
Database utilities for admin tasks:
Get structure of a table from a database
Get structure of a table from a file
First level parse of table structure
Parse of field definitions part of table structure
Comparison of two structures, including generation of MySQL to make them the same
Some crude printing utilities
Get structure of a table from a database
Get structure of a table from a file
First level parse of table structure
Parse of field definitions part of table structure
Comparison of two structures, including generation of MySQL to make them the same
Some crude printing utilities
Note: there are some uncommented 'echo' statements which are intentional to highlight that something's gone wrong! (not that it should, of course)
*/
@@ -34,63 +34,63 @@ class db_table_admin
var $file_buffer = ''; // Contents of a file
var $last_file = '';
// Get list of fields and keys for a table - return FALSE if unsuccessful
// Return as for get_table_def
// Get list of fields and keys for a table - return FALSE if unsuccessful
// Return as for get_table_def
function get_current_table($table_name, $prefix = "")
{
global $sql;
if (!$prefix) $prefix = MPREFIX;
// echo "Get table structure for: {$table_name}, prefix: {$prefix}<br />";
if (!$prefix) { $prefix = MPREFIX; }
// echo "Get table structure for: {$table_name}, prefix: {$prefix}<br />";
$sql->db_Select_gen('SET SQL_QUOTE_SHOW_CREATE = 1');
$qry = 'SHOW CREATE TABLE `'.$prefix.$table_name."`";
if (!($z = $sql->db_Select_gen($qry))) return FALSE;
if (!($z = $sql->db_Select_gen($qry))) { return FALSE; }
$row = $sql->db_Fetch();
$tmp = str_replace("`", "", stripslashes($row[1])).';'; // Add semicolon to work with our parser
$count = preg_match_all("#CREATE\s+?TABLE\s+?`{0,1}({$prefix}{$table_name})`{0,1}\s+?\((.*?)\)\s+?(?:TYPE|ENGINE)\s*\=\s*(.*?);#is",$tmp,$matches,PREG_SET_ORDER);
if ($count === FALSE) return "Error occurred";
if (!$count) return "No matches";
if ($count === FALSE) { return "Error occurred";}
if (!$count) { return "No matches"; }
return $matches;
}
// Routine to do first-level parse of table structure
//---------------------------------------------------
// Given the name of a file, returns an array, with each element being a table creation definition.
// Tracks the last file read - only reads it once
// If the file name is an empty string, uses a previously read/set buffer
// If a table name is given, returns only that table's info; otherwise returns a list of all tables
// The table name must include a prefix where appropriate (although not required with standard E107 table definition files)
// Each element is itself an array:
// [0] - The complete string which creates a table (unless a prefix needs adding to the table name), including terminating ';'
// [1] - The table name. Any backticks are stripped
// [2] - Field definitions, with the surrounding (...) stripped
// [3] - The 'TYPE' field ('TYPE=' is stripped) and any AUTO-INCREMENT definition or other text.
// function get_table_def($table_name='',$file_name = e_ADMIN."sql/core_sql.php")
// Routine to do first-level parse of table structure
//---------------------------------------------------
// Given the name of a file, returns an array, with each element being a table creation definition.
// Tracks the last file read - only reads it once
// If the file name is an empty string, uses a previously read/set buffer
// If a table name is given, returns only that table's info; otherwise returns a list of all tables
// The table name must include a prefix where appropriate (although not required with standard E107 table definition files)
// Each element is itself an array:
// [0] - The complete string which creates a table (unless a prefix needs adding to the table name), including terminating ';'
// [1] - The table name. Any backticks are stripped
// [2] - Field definitions, with the surrounding (...) stripped
// [3] - The 'TYPE' field ('TYPE=' is stripped) and any AUTO-INCREMENT definition or other text.
// function get_table_def($table_name='',$file_name = e_ADMIN."sql/core_sql.php")
function get_table_def($table_name='',$file_name ="")
{
if ($file_name != '')
{ // Read in and buffer a new file (if we've not already got one)
if ($this->last_file != $file_name)
{
if (!is_readable($file_name)) return "No file";
if (!is_readable($file_name)) { return "No file"; }
$temp = file_get_contents($file_name);
// Strip any php header
$this->file_buffer = preg_replace("#\<\?php.*?\?\>#mis",'',$temp);
$this->last_file = $file_name;
}
}
if (!$table_name) $table_name = '\w+?';
if (!$table_name) { $table_name = '\w+?'; }
// Regex should be identical to that in get_current_table (apart from the source text variable name)
$count = preg_match_all("#CREATE\s+?TABLE\s+?`{0,1}({$table_name})`{0,1}\s+?\((.*?)\)\s+?(?:TYPE|ENGINE)\s*\=\s*(.*?);#is",$this->file_buffer,$matches,PREG_SET_ORDER);
if ($count === FALSE) return "Error occurred";
if (!$count) return "No matches";
if ($count === false) { return "Error occurred"; }
if (!$count) { return "No matches"; }
return $matches;
}
// Parses the block of lines which make up the field and index definitions
// Returns an array where each entry is the definitions of a field or index
// Parses the block of lines which make up the field and index definitions
// Returns an array where each entry is the definitions of a field or index
function parse_field_defs($text)
{
$ans = array();
@@ -100,8 +100,8 @@ class db_table_admin
{
unset($defs);
$fv = trim(str_replace(' ',' ',$fv));
if (substr($fv,-1) == ',') $fv = trim(substr($fv,0,-1));
// echo "Line: ".$fv."<br />";
if (substr($fv,-1) == ',') { $fv = trim(substr($fv,0,-1)); }
// echo "Line: ".$fv."<br />";
if ($fv)
{
$fd = explode(' ',$fv);
@@ -131,7 +131,13 @@ class db_table_admin
case 'KEY' :
$defs['type'] = 'key';
$defs['name'] = $fd[1];
if (isset($fd[2])) $defs['keyfield'] = $fd[2]; else $defs['keyfield'] = '['.$fd[1].']';
if (isset($fd[2])) {
$defs['keyfield'] = $fd[2];
}
else
{
$defs['keyfield'] = '['.$fd[1].']';
}
break;
default : // Must be a DB field name
$defs['type'] = 'field';
@@ -180,10 +186,17 @@ class db_table_admin
$i++;
}
}
if (count($defs) > 1) $ans[] = $defs; else echo "Partial definition<br />";
if (count($defs) > 1)
{
$ans[] = $defs;
}
else
{
echo "Partial definition<br />";
}
}
if (!count($ans)) return FALSE;
}
if (!count($ans)) { return FALSE; }
return $ans;
}
@@ -202,11 +215,11 @@ class db_table_admin
return 'PRIMARY KEY ('.$list['name'].')';
case 'field' : // Require a field - got a key. so add a field at the end
$def = $list['name'];
if (isset($list['fieldtype'])) $def .= ' '.$list['fieldtype'];
if (isset($list['vartype'])) $def .= ' '.$list['vartype'];
if (isset($list['nulltype'])) $def .= ' '.$list['nulltype'];
if (isset($list['default'])) $def .= ' default '.$list['default'];
if (varsettrue($list['autoinc'])) $def .= ' auto_increment';
if (isset($list['fieldtype'])) { $def .= ' '.$list['fieldtype']; }
if (isset($list['vartype'])) { $def .= ' '.$list['vartype']; }
if (isset($list['nulltype'])) { $def .= ' '.$list['nulltype']; }
if (isset($list['default'])) { $def .= ' default '.$list['default']; }
if (varsettrue($list['autoinc'])) { $def .= ' auto_increment'; }
return $def;
}
return "Cannot generate definition for: ".$list['type'].' '.$list['name'];
@@ -229,7 +242,7 @@ class db_table_admin
{
if (count($list2) == 0)
{ // Missing field at end
if ($stop_on_error) return FALSE;
if ($stop_on_error) { return FALSE; }
$error_list[] = 'Missing field at end: '.$list1[$i]['name'];
$change_list[] = 'ADD '.$this->make_def($list1[$i]);
$created_list[$j] = $list1[$i]['name'];
@@ -237,22 +250,22 @@ class db_table_admin
}
elseif ($list1[$i]['type'] == $list2[0]['type'])
{ // Worth doing a compare - fields are same type
// echo $i.': compare - '.$list1[$i]['name'].', '.$list2[0]['name'].'<br />';
// echo $i.': compare - '.$list1[$i]['name'].', '.$list2[0]['name'].'<br />';
if (strcasecmp($list1[$i]['name'],$list2[0]['name']) != 0)
{ // Names differ, so need to add or subtract a field.
// echo $i.': names differ - '.$list1[$i]['name'].', '.$list2[0]['name'].'<br />';
if ($stop_on_error) return FALSE;
// echo $i.': names differ - '.$list1[$i]['name'].', '.$list2[0]['name'].'<br />';
if ($stop_on_error) { return FALSE; }
$found = FALSE;
for ($k = $i+1; $k < count($list1); $k++)
{
// echo "Compare ".$list1[$k]['name'].' with '.$list2[0]['name'];
// echo "Compare ".$list1[$k]['name'].' with '.$list2[0]['name'];
if (strcasecmp($list1[$k]['name'],$list2[0]['name']) == 0)
{ // Field in list2 found later in list1; do nothing
// echo " - match<br />";
// echo " - match<br />";
$found = TRUE;
break;
}
// echo " - no match<br />";
// echo " - no match<br />";
}
if (!$found)
@@ -266,14 +279,14 @@ class db_table_admin
$found = FALSE;
for ($k = 0; $k < count($list2); $k++)
{
// echo "Compare ".$list1[$i]['name'].' with '.$list2[$k]['name'];
// echo "Compare ".$list1[$i]['name'].' with '.$list2[$k]['name'];
if (strcasecmp($list1[$i]['name'],$list2[$k]['name']) == 0)
{ // Field found; we need to move it up
// echo " - match<br />";
// echo " - match<br />";
$found = TRUE;
break;
}
// echo " - no match<br />";
// echo " - no match<br />";
}
if ($found)
{
@@ -310,15 +323,15 @@ class db_table_admin
else
{ // Field/index is present as required; may be changes though
// Any difference and we need to update the table
// echo $i.': name match - '.$list1[$i]['name'].'<br />';
// echo $i.': name match - '.$list1[$i]['name'].'<br />';
foreach ($list1[$i] as $fi => $v)
{
$t = $list2[0][$fi];
if (stripos($v,'varchar') !== FALSE) $v = substr($v,3); // Treat char, varchar the same
if (stripos($t,'varchar') !== FALSE) $t = substr($t,3); // Treat char, varchar the same
if (stripos($v,'varchar') !== FALSE) { $v = substr($v,3); } // Treat char, varchar the same
if (stripos($t,'varchar') !== FALSE) { $t = substr($t,3); } // Treat char, varchar the same
if (strcasecmp($t , $v) !== 0)
{
if ($stop_on_error) return FALSE;
if ($stop_on_error) { return FALSE; }
$error_list[] = 'Incorrect definition: '.$fi.' = '.$v;
$change_list[] = 'MODIFY '.$this->make_def($list1[$i]);
break;
@@ -331,8 +344,8 @@ class db_table_admin
}
else
{ // Field type has changed. We know fields come before indexes. So something's missing
// echo $i.': types differ - '.$list1[$i]['type'].' '.$list1[$i]['name'].', '.$list2[$k]['type'].' '.$list2[$k]['name'].'<br />';
if ($stop_on_error) return FALSE;
// echo $i.': types differ - '.$list1[$i]['type'].' '.$list1[$i]['name'].', '.$list2[$k]['type'].' '.$list2[$k]['name'].'<br />';
if ($stop_on_error) { return FALSE; }
switch ($list1[$i]['type'])
{
case 'key' :
@@ -368,7 +381,7 @@ class db_table_admin
}
if (count($list2))
{ // Surplus fields in actual table
// Echo count($list2)." fields at end to delete<br />";
// Echo count($list2)." fields at end to delete<br />";
foreach ($list2 as $f)
{
switch ($f['type'])
@@ -392,7 +405,7 @@ class db_table_admin
function make_changes_list($result)
{
if (!is_array($result)) return "Not an array<br />";
if (!is_array($result)) { return "Not an array<br />"; }
$text = "<table>";
for ($i = 0; $i < count($result[0]); $i++)
{
@@ -408,7 +421,7 @@ class db_table_admin
// Return a table of info from the output of get_table_def
function make_table_list($result)
{
if (!is_array($result)) return "Not an array<br />";
if (!is_array($result)) { return "Not an array<br />"; }
$text = "<table>";
for ($i = 0; $i < count($result); $i++)
{
@@ -441,11 +454,28 @@ class db_table_admin
break;
case 'field' :
$text .= "<tr><td>FIELD</td><td>{$f['name']}</td><td>{$f['fieldtype']}";
if (isset($f['vartype'])) $text .= " ".$f['vartype'];
if (isset($f['vartype'])) { $text .= " ".$f['vartype']; }
$text .= "</td>";
if (isset($f['nulltype'])) $text .= "<td>{$f['nulltype']}</td>"; else $text .= "<td>&nbsp;</td>";
if (isset($f['default'])) $text .= "<td>default {$f['default']}</td>"; elseif
(isset($f['autoinc'])) $text .= "<td>AUTO_INCREMENT</td>"; else $text .= "<td>&nbsp;</td>";
if (isset($f['nulltype']))
{
$text .= "<td>{$f['nulltype']}</td>";
}
else
{
$text .= "<td>&nbsp;</td>";
}
if (isset($f['default']))
{
$text .= "<td>default {$f['default']}</td>";
}
elseif (isset($f['autoinc']))
{
$text .= "<td>AUTO_INCREMENT</td>";
}
else
{
$text .= "<td>&nbsp;</td>";
}
$text .= "</tr>";
break;
default :
@@ -476,12 +506,12 @@ class db_table_admin
$tableName = $newStructure[1];
if (!$sql->db_Table_exists($tableName))
{
if ($makeNewifNotExist === FALSE) return 'Table doesn\'t exist';
if ($sql->db_Select_gen($newStructure[0])) return TRUE;
if ($makeNewifNotExist === FALSE) { return 'Table doesn\'t exist'; }
if ($sql->db_Select_gen($newStructure[0])) { return TRUE; }
return 'Error creating new table: '.$tableName;
}
$reqFields = $this->parse_field_defs($newStructure[2]); // Required field definitions
if (E107_DBG_FILLIN8) echo "Required table structure: <br />".$this->make_field_list($reqFields);
if (E107_DBG_FILLIN8) { echo "Required table structure: <br />".$this->make_field_list($reqFields); }
if ((($actualDefs = $this->get_current_table($tableName)) === FALSE) || !is_array($actualDefs)) // Get actual table definition (Adds current default prefix)
{
@@ -489,18 +519,18 @@ class db_table_admin
}
else
{
// echo $db_parser->make_table_list($actual_defs);
// echo $db_parser->make_table_list($actual_defs);
$actualFields = $this->parse_field_defs($actualDefs[0][2]); // Split into field definitions
if (E107_DBG_FILLIN8) echo 'Actual table structure: <br />'.$this->make_field_list($actualFields);
if (E107_DBG_FILLIN8) { echo 'Actual table structure: <br />'.$this->make_field_list($actualFields); }
$diffs = $this->compare_field_lists($reqFields,$actualFields); // Work out any differences
if (count($diffs[0]))
{ // Changes needed
if ($justCheck) return 'Field changes rqd; table: '.$tableName.'<br />';
if ($justCheck) { return 'Field changes rqd; table: '.$tableName.'<br />'; }
// Do the changes here
if (E107_DBG_FILLIN8) echo "List of changes found:<br />".$this->make_changes_list($diffs);
if (E107_DBG_FILLIN8) { echo "List of changes found:<br />".$this->make_changes_list($diffs); }
$qry = 'ALTER TABLE '.MPREFIX.$tableName.' '.implode(', ',$diffs[1]);
if (E107_DBG_FILLIN8) echo 'Update Query used: '.$qry.'<br />';
if (E107_DBG_FILLIN8) { echo 'Update Query used: '.$qry.'<br />'; }
if ($mlUpdate)
{
$ret = $sql->db_Query_all($qry); // Returns TRUE = success, FALSE = fail
@@ -518,6 +548,22 @@ class db_table_admin
}
return FALSE;
}
function createTable($pathToSqlFile='', $tableName='', $addPrefix=true, $renameTable='')
{
$e107 = e107::getInstance();
$tmp = $this->get_table_def($tableName, $pathToSqlFile);
$createText = $tmp[0][0];
$newTableName = ($renameTable ? $renameTable : $tableName);
if($addPrefix) { $newTableName = MPREFIX.$newTableName; }
if($newTableName != $tableName)
{
$createText = preg_replace('#create +table +(\w*?) +#i', 'CREATE TABLE '.$newTableName.' ', $createText);
}
return $e107->sql->db_Select_gen($createText);
}
}

View File

@@ -11,8 +11,8 @@
| GNU General Public License (http://gnu.org).
|
| $Source: /cvs_backup/e107_0.8/e107_plugins/forum/forum_update.php,v $
| $Revision: 1.4 $
| $Date: 2008-12-19 21:56:37 $
| $Revision: 1.5 $
| $Date: 2008-12-20 00:55:29 $
| $Author: mcfly_e107 $
+----------------------------------------------------------------------------+
*/
@@ -67,10 +67,9 @@ $stepParms = (isset($stepParms) ? $stepParms : '');
if(function_exists('step'.$currentStep))
{
call_user_func('step'.$currentStep, $stepParms);
$result = call_user_func('step'.$currentStep, $stepParms);
}
require(e_ADMIN.'footer.php');
exit;
@@ -91,7 +90,7 @@ function step1()
";
foreach($f->error['attach'] as $e)
{
$errorText .= '** '.$e.'<br />';
$text .= '** '.$e.'<br />';
}
$text .= "
<br />
@@ -131,19 +130,105 @@ function step2()
return;
}
if($sql = file_get_contents(e_PLUGIN.'forum/forum_sql.php'))
require_once(e_HANDLER.'db_table_admin_class.php');
$db = new db_table_admin;
$tabList = array('forum' => 'forum_new', 'forum_thread' => '', 'forum_post' => '', 'forum_track' => '');
$ret = '';
$failed = false;
$text = '';
foreach($tabList as $name => $rename)
{
echo $sql;
$text .= 'Creating table '.($rename ? $rename : $name).' -> ';
$result = $db->createTable(e_PLUGIN.'forum/forum_sql.php', $name, true, $rename);
if($result)
{
$text .= 'Success <br />';
}
else
{
echo 'failed';
$text .= 'Failed <br />';
$failed = true;
}
}
if($failed)
{
$text .= "
<br /><br />
Creation of table(s) failed. You can not continue until these are create successfully!
";
}
else
{
$text .= "
<br /><br />
<form method='post'>
<input class='button' type='submit' name='nextStep[3]' value='Proceed to step 3' />
</form>
";
}
$e107->ns->tablerender('Step 2: Forum table creation', $text);
}
function step3()
{
$e107 = e107::getInstance();
$stepCaption = 'Step 3: Extended user field creation';
if(!isset($_POST['create_extended']))
{
$text = "
This step will create the new extended user fields required for the new forum code: <br />
* user_plugin_forum_posts (to track number of posts for each user)<br />
* user_plugin_forum_viewed (to track threads viewed by each user<br />
<br /><br />
<form method='post'>
<input class='button' type='submit' name='create_extended' value='Proceed with field creation' />
</form>
";
$e107->ns->tablerender($stepCaption, $text);
return;
}
require_once(e_HANDLER.'user_extended_class.php');
$ue = new e107_user_extended;
$fieldList = array(
'plugin_forum_posts' => EUF_INTEGER,
'plugin_forum_viewed' => EUF_TEXTAREA
);
$failed = false;
foreach($fieldList as $fieldName => $fieldType)
{
$text .= 'Creating extended user field user_'.$fieldName.' -> ';
$result = $ue->user_extended_add_system($fieldName, $fieldType);
if($result)
{
$text .= 'Success <br />';
}
else
{
$text .= 'Failed <br />';
$failed = true;
}
}
if($failed)
{
$text .= '
<br /><br />
Creation of extended field(s) failed. You can not continue until these are create successfully!
';
}
else
{
$text .= "
<br /><br />
<form method='post'>
<input class='button' type='submit' name='nextStep[4]' value='Proceed to step 4' />
</form>
";
}
$e107->ns->tablerender($stepCaption, $text);
}
//print_a($f->error);
class forumUpgrade
{
@@ -156,7 +241,6 @@ class forumUpgrade
$this->getUpdateInfo();
}
function checkAttachmentDirs()
{
$dirs = array(