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,9 +11,9 @@
| 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 $
+----------------------------------------------------------------------------+
*/
@@ -40,16 +40,16 @@ class db_table_admin
{
global $sql;
if (!$prefix) $prefix = MPREFIX;
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;
}
@@ -73,18 +73,18 @@ class db_table_admin
{ // 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;
}
@@ -100,7 +100,7 @@ class db_table_admin
{
unset($defs);
$fv = trim(str_replace(' ',' ',$fv));
if (substr($fv,-1) == ',') $fv = trim(substr($fv,0,-1));
if (substr($fv,-1) == ',') { $fv = trim(substr($fv,0,-1)); }
// echo "Line: ".$fv."<br />";
if ($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'];
@@ -241,7 +254,7 @@ class db_table_admin
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;
if ($stop_on_error) { return FALSE; }
$found = FALSE;
for ($k = $i+1; $k < count($list1); $k++)
{
@@ -314,11 +327,11 @@ class db_table_admin
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;
@@ -332,7 +345,7 @@ 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;
if ($stop_on_error) { return FALSE; }
switch ($list1[$i]['type'])
{
case 'key' :
@@ -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)
{
@@ -491,16 +521,16 @@ class db_table_admin
{
// 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(