Index: demo.info =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/demo/demo.info,v retrieving revision 1.1.2.3 diff -u -r1.1.2.3 demo.info --- demo.info 10 Sep 2007 20:12:14 -0000 1.1.2.3 +++ demo.info 12 Sep 2007 12:20:24 -0000 @@ -1,6 +1,5 @@ ; $Id: demo.info,v 1.1.2.3 2007/09/10 20:12:14 smk Exp $ name = "Demo Site" description = "Create snapshots and reset the site for demonstration or testing purposes." -dependencies = dba package = Development Index: demo.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/demo/demo.module,v retrieving revision 1.1.2.10 diff -u -r1.1.2.10 demo.module --- demo.module 10 Sep 2007 20:12:14 -0000 1.1.2.10 +++ demo.module 12 Sep 2007 12:20:36 -0000 @@ -226,22 +226,28 @@ } function demo_dump_submit($form_id, $values) { + global $db_type; + // Write .info file $info = demo_set_info($values); if (!$info) { return false; } + // Include database specific functions + switch ($db_type) { + case 'mysqli': + $engine = 'mysql'; + break; + default: + $engine = $db_type; + break; + } + require_once drupal_get_path('module', 'demo') ."/database_{$engine}_dump.inc"; + // Perform dump $fileconfig = demo_get_fileconfig($info['filename']); - module_invoke('dba', 'auto_backup', - $fileconfig['path'] . $fileconfig['site'], - $fileconfig['sql'], - array(), - false, - false, - false - ); + demo_dump_db($fileconfig['sqlfile']); drupal_goto('admin/settings/demo/manage'); } @@ -297,12 +303,9 @@ if (file_exists($fileconfig['sqlfile'])) { if ($fp = fopen($fileconfig['sqlfile'], 'r')) { - // Fetch list of all tables of this installation (dba deals with prefixes). - $tables = module_invoke('dba', 'get_tables'); - - // Drop those tables. - foreach ($tables as $table) { - module_invoke('dba', 'drop_table', $table, false); + // Drop all tables. + foreach (demo_enum_tables() as $table) { + db_query("DROP TABLE %s", $table); } // Load data from snapshot @@ -406,7 +409,7 @@ } if (count($info['modules']) > 1) { // Remove required core modules and obvious modules from module list. - $info['modules'] = array_diff($info['modules'], array('block', 'filter', 'node', 'system', 'user', 'watchdog', 'demo', 'dba')); + $info['modules'] = array_diff($info['modules'], array('block', 'filter', 'node', 'system', 'user', 'watchdog', 'demo')); // Sort module list alphabetically. sort($info['modules']); $option['#description'] .= t('Modules: ') . implode(', ', $info['modules']); @@ -482,6 +485,61 @@ } /** + * Returns a list of tables in the active database. + * + * Only returns tables whose prefix matches the configured one (or ones, if + * there are multiple). + */ +function demo_enum_tables() { + global $db_prefix; + + $tables = array(); + + if (is_array($db_prefix)) { + // Create a regular expression for table prefix matching. + $rx = '/^('. implode('|', array_filter($db_prefix)) .')/'; + } + else if ($db_prefix != '') { + $rx = '/^'. $db_prefix .'/'; + } + + switch ($GLOBALS['db_type']) { + case 'mysql': + case 'mysqli': + $result = db_query("SHOW TABLES"); + break; + case 'pgsql': + $result = db_query("SELECT table_name FROM information_schema.tables WHERE table_schema = '%s'", 'public'); + break; + } + + while ($table = db_fetch_array($result)) { + $table = reset($table); + if (is_array($db_prefix)) { + // Check if table matches any configured prefix. + if (preg_match($rx, $table)) { + // One arbitrary prefix matched, now let's find out which one. + $plain_table = preg_replace($rx, '', $table); + $table_prefix = substr($table, 0, strlen($table) - strlen($plain_table)); + if ($db_prefix[$plain_table] == $table_prefix) { + $tables[] = $table; + } + } + } + else if ($db_prefix != '') { + if (preg_match($rx, $table)) { + $tables[] = $table; + } + } + else { + $tables[] = $table; + } + } + + return $tables; +} + +/** * Implementation of hook_cron(). */ function demo_cron() { --- D:/htdocs/test/drupal/drupal-5.0/sites/all/modules/demo/database_mysql_dump.inc +++ D:/htdocs/test/drupal/drupal-5.0/sites/all/modules/demo/database_mysql_dump.inc @@ -0,0 +1,133 @@ +numeric + && $fields['meta'][$i]->type != 'timestamp' + && !($fields['meta'][$i]->blob)) { + $values[] = $value; + } + // A binary field + // Note: with mysqli, under MySQL 4.1.3, we get the flag + // "binary" for those field types (I don't know why) + else if (stristr($fields['flags'][$i], 'binary') + && $fields['meta'][$i]->type != 'datetime' + && $fields['meta'][$i]->type != 'date' + && $fields['meta'][$i]->type != 'time' + && $fields['meta'][$i]->type != 'timestamp') { + // Empty blobs need to be different, but '0' is also empty :-( + if (empty($value) && $value != '0') { + $values[] = "''"; + } else { + $values[] = '0x' . bin2hex($value); + } + } + // Something else -> treat as a string + else { + $values[] = "'". str_replace($search, $replace, $value) ."'"; + } + $i++; + } + $output .= "INSERT INTO `$table` VALUES (". implode(', ', $values) . ");\n"; + } + return $output; +} + +/** + * Return CREATE TABLE definition. + */ +function _demo_show_create_table($table) { + $create = db_fetch_array(db_query("SHOW CREATE TABLE %s", $table)); + return $create['Create Table']; +} + +/** + * Return table fields and their properties. + */ +function _demo_get_fields($result) { + $fields = array(); + $num_fields = mysql_num_fields($result); + for ($i = 0; $i < $num_fields; $i++) { + $fields['meta'][] = mysql_fetch_field($result, $i); + $fields['flags'][] = mysql_field_flags($result, $i); + } + return $fields; +} + --- D:/htdocs/test/drupal/drupal-5.0/sites/all/modules/demo/database_pgsql_dump.inc +++ D:/htdocs/test/drupal/drupal-5.0/sites/all/modules/demo/database_pgsql_dump.inc @@ -0,0 +1,14 @@ +