From 913db4e7608e04a65ce963f213e9d2b5bc1acd9f Mon Sep 17 00:00:00 2001 From: e107steved Date: Tue, 12 Aug 2008 20:26:53 +0000 Subject: [PATCH] Update DB table structures as required during plugin upgrade --- e107_handlers/db_table_admin_class.php | 67 +++++++++++++- e107_handlers/plugin_class.php | 92 +++++++++++-------- .../calendar_menu/languages/English.php | 6 +- 3 files changed, 120 insertions(+), 45 deletions(-) diff --git a/e107_handlers/db_table_admin_class.php b/e107_handlers/db_table_admin_class.php index 1c74c9494..21d6cd3cd 100644 --- a/e107_handlers/db_table_admin_class.php +++ b/e107_handlers/db_table_admin_class.php @@ -11,8 +11,8 @@ | GNU General Public License (http://gnu.org). | | $Source: /cvs_backup/e107_0.8/e107_handlers/db_table_admin_class.php,v $ -| $Revision: 1.4 $ -| $Date: 2008-06-16 20:48:47 $ +| $Revision: 1.5 $ +| $Date: 2008-08-12 20:26:43 $ | $Author: e107steved $ +----------------------------------------------------------------------------+ */ @@ -447,6 +447,69 @@ class db_table_admin $text .= "

--Ends--
"; return $text; } + + + + + //-------------------------------------------------- + // Update a table to required structure + //-------------------------------------------------- + + // $newStructure is an array element as returned from get_table_def() + // If $mlUpdate is TRUE, applies same query to all tables of same language + // Return TRUE on success. + // Return text string if $justCheck is TRUE and changes needed + // Return text string on most failures + // Return FALSE on certain failures (generally indicative of code/system problems) + function update_table_structure($newStructure, $justCheck=FALSE, $makeNewifNotExist = TRUE, $mlUpdate = FALSE) + { + global $sql; + // Pull out table name + $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; + return 'Error creating new table: '.$tableName; + } + $reqFields = $this->parse_field_defs($newStructure[2]); // Required field definitions + if (E107_DBG_FILLIN8) echo "Required table structure:
".$this->make_field_list($reqFields); + + if ((($actualDefs = $this->get_current_table($tableName)) === FALSE) || !is_array($actualDefs)) // Get actual table definition (Adds current default prefix) + { + return "Couldn't get table structure: {$tableName}
"; + } + else + { +// 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:
'.$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.'
'; + // Do the changes here + if (E107_DBG_FILLIN8) echo "List of changes found:
".$this->make_changes_list($diffs); + $qry = 'ALTER TABLE '.MPREFIX.$tableName.' '.implode(', ',$diffs[1]); + if (E107_DBG_FILLIN8) echo 'Update Query used: '.$qry.'
'; + if ($mlUpdate) + { + $ret = $sql->db_Query_all($qry); // Returns TRUE = success, FALSE = fail + } + else + { + $ret = $sql->db_Select_gen($qry); + } + if ($ret === FALSE) + { + return $sql->dbError() ; + } + } + return TRUE; // Success even if no changes required + } + return FALSE; + } } diff --git a/e107_handlers/plugin_class.php b/e107_handlers/plugin_class.php index 6949e09f0..71c6418dc 100644 --- a/e107_handlers/plugin_class.php +++ b/e107_handlers/plugin_class.php @@ -11,8 +11,8 @@ | GNU General Public License (http://gnu.org). | | $Source: /cvs_backup/e107_0.8/e107_handlers/plugin_class.php,v $ -| $Revision: 1.42 $ -| $Date: 2008-08-11 20:21:08 $ +| $Revision: 1.43 $ +| $Date: 2008-08-12 20:26:43 $ | $Author: e107steved $ +----------------------------------------------------------------------------+ */ @@ -452,14 +452,22 @@ class e107plugin } } + + + // Handle table updates - passed an array of actions. + // $var array: + // For 'add' - its a query to create the table + // For 'upgrade' - its a query to modify the table + // For 'remove' - its a table name + // 'upgrade' and 'remove' operate on all language variants of the same table function manage_tables($action, $var) { global $sql; + if (!is_array($var)) return FALSE; // Return if nothing to do + switch ($action) { case 'add' : - if (is_array($var)) - { foreach($var as $tab) { if (!$sql->db_Query($tab)) @@ -468,12 +476,8 @@ class e107plugin } } return TRUE; - } - return TRUE; break; case 'upgrade' : - if (is_array($var)) - { foreach($var as $tab) { if (!$sql->db_Query_all($tab)) @@ -481,13 +485,9 @@ class e107plugin return FALSE; } } - return TRUE; - } return TRUE; break; case 'remove' : - if (is_array($var)) - { foreach($var as $tab) { $qry = 'DROP TABLE '.MPREFIX.$tab; @@ -497,10 +497,9 @@ class e107plugin } } return TRUE; - } - return TRUE; break; } + return FALSE; } @@ -773,40 +772,53 @@ class e107plugin // All the dependencies are OK - can start the install now - // Let's call any custom pre functions defined in section - $txt .= $this->execute_function($path, $function, 'pre'); + if ($canContinue) + { // Let's call any custom pre functions defined in section + $txt .= $this->execute_function($path, $function, 'pre'); + } - // tables - // This will load each _sql.php file found in the plugin directory and parse it. - if(($function == 'install' || $function == 'uninstall') && count($sql_list)) - { - foreach($sql_list as $sql_file) + if ($canContinue && count($sql_list)) + { // Handle tables + require_once(e_HANDLER.'db_table_admin_class.php'); + $dbHandler = new db_table_admin; + foreach($sql_list as $sqlFile) { - if($sql_data = file_get_contents($path.$sql_file)) + $tableList = $dbHandler->get_table_def('',$path.$sqlFile); + if (!is_array($tableList)) { - preg_match_all("/create(.*?)myisam.*?;/si", $sql_data, $result ); - foreach ($result[0] as $sql_table) + $error[] = "Can't read SQL definition: ".$path.$sqlFile; + break; + } + // Got the required definition here + foreach ($tableList as $ct) + { // Process one table at a time (but they could be multi-language) + switch($function) { - preg_match("/CREATE TABLE(.*?)\(/si", $sql_table, $match); - $tablename = trim($match[1]); - - if($function == 'uninstall' && isset($_POST['delete_tables']) && $_POST['delete_tables']) - { - $txt .= "Removing table $tablename
"; - $this->manage_tables('remove', array($tablename)); - } - if($function == 'install') - { - $sql_table = preg_replace("/create table\s+/si", "CREATE TABLE ".MPREFIX, $sql_table); - $txt .= "Adding table: {$tablename} ... "; - $result = $this->manage_tables('add', array($sql_table)); - $txt .= ($result ? "Success" : "Failed!")."
"; - } + case 'install' : + $sqlTable = preg_replace("/create table\s+/si", "CREATE TABLE ".MPREFIX, $ct[0]); + $txt .= "Adding table: {$ct[1]} ... "; + $result = $this->manage_tables('add', array($sqlTable)); // Pass the statement to create the table + $txt .= ($result ? "Success" : "Failed!").'
'; + break; + case 'upgrade' : + $tmp = $dbHandler->update_table_structure($ct,FALSE,TRUE, $pref['multilanguage']); + if ($tmp === FALSE) + { + $error[] = "Unspecified error updating table: {$ct[1]}"; + } + elseif ($tmp !== TRUE) + { + $error[] = $tmp; + } + break; + case 'uninstall' : + $txt .= "Removing table {$ct[1]}
"; + $this->manage_tables('remove', array($ct[1])); // Delete the table + break; } } } } - //main menu items if(isset($plug_vars['menuLink'])) { diff --git a/e107_plugins/calendar_menu/languages/English.php b/e107_plugins/calendar_menu/languages/English.php index f9005ff66..c54c70ca0 100644 --- a/e107_plugins/calendar_menu/languages/English.php +++ b/e107_plugins/calendar_menu/languages/English.php @@ -11,8 +11,8 @@ | GNU General Public License (http://gnu.org). | | $Source: /cvs_backup/e107_0.8/e107_plugins/calendar_menu/languages/English.php,v $ -| $Revision: 1.6 $ -| $Date: 2008-08-12 19:59:59 $ +| $Revision: 1.7 $ +| $Date: 2008-08-12 20:26:53 $ | $Author: e107steved $ | +----------------------------------------------------------------------------+ @@ -151,7 +151,7 @@ define('EC_LAN_79', "Calendar View"); //define('EC_LAN_80', "Event List"); //define('EC_LAN_81', "Configure Event Calendar"); //define('EC_LAN_82', "To activate please go to your menus screen and select the calendar_menu into one of your menu areas."); -//define('EC_LAN_83', "Calendar"); +define('EC_LAN_83', "Calendar"); define('EC_LAN_84', " from "); define('EC_LAN_85', " until "); define('EC_LAN_86', "Individual events from entry");