| 1 |
<?php |
<?php |
| 2 |
// $Id: db_maintenance.module,v 1.11.2.1 2008/02/16 01:49:15 deekayen Exp $ |
// $Id: db_maintenance.module,v 1.11.2.2 2008/05/28 18:57:38 deekayen Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 7 |
* |
* |
| 8 |
* @author David Kent Norman |
* @author David Kent Norman |
| 9 |
* @link http://deekayen.net/ |
* @link http://deekayen.net/ |
|
* |
|
|
* @todo |
|
|
* - some sort of backup dump saver/emailer/ftp/etc |
|
| 10 |
*/ |
*/ |
| 11 |
|
|
| 12 |
/** |
/** |
| 13 |
* Implementation of hook_help(). |
* Implement hook_help(). |
| 14 |
* |
* |
| 15 |
* @param $section string |
* @param $section string |
| 16 |
* @return string |
* @return string |
| 38 |
} |
} |
| 39 |
|
|
| 40 |
/** |
/** |
| 41 |
* Implementation of hook_menu(). |
* Implement hook_menu(). |
| 42 |
* |
* |
| 43 |
* @return array |
* @return array |
| 44 |
*/ |
*/ |
| 67 |
|
|
| 68 |
/** |
/** |
| 69 |
* Callback page for manually optimizing tables. |
* Callback page for manually optimizing tables. |
|
* |
|
| 70 |
*/ |
*/ |
| 71 |
function db_maintenance_optimize_tables_page() { |
function db_maintenance_optimize_tables_page() { |
| 72 |
db_maintenance_optimize_tables(); |
db_maintenance_optimize_tables(); |
| 81 |
* @return array representing the tables in the specified database. |
* @return array representing the tables in the specified database. |
| 82 |
*/ |
*/ |
| 83 |
function _db_maintenance_list_tables($db) { |
function _db_maintenance_list_tables($db) { |
| 84 |
|
global $databases; |
| 85 |
|
|
| 86 |
$table_names = array(); |
$table_names = array(); |
| 87 |
// Set the database to query. |
// Set the database to query. |
| 88 |
$previous = db_set_active($db); |
$previous = db_set_active($db); |
| 89 |
if (_db_maintenance_determine_software() == 'mysql') { |
if ($databases['default']['default']['driver'] == 'mysql') { |
| 90 |
$result = db_query('SHOW TABLES'); |
$result = db_query('SHOW TABLES', array(), array('fetch' => PDO::FETCH_ASSOC)); |
| 91 |
} |
} |
| 92 |
elseif (_db_maintenance_determine_software() == 'pgsql') { |
elseif ($databases['default']['default']['driver'] == 'pgsql') { |
| 93 |
$result = db_query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_name"); |
$result = db_query("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_name", array(), array('fetch' => PDO::FETCH_ASSOC)); |
| 94 |
} |
} |
| 95 |
// Return to the previously set database. |
// Return to the previously set database. |
| 96 |
db_set_active($previous); |
db_set_active($previous); |
| 97 |
while ($table_name = db_fetch_array($result)) { |
foreach ($result as $table_name) { |
| 98 |
$table_name = current($table_name); |
$table_name = current($table_name); |
| 99 |
$table_names[$table_name] = $table_name; |
$table_names[$table_name] = $table_name; |
| 100 |
} |
} |
| 102 |
} |
} |
| 103 |
|
|
| 104 |
/** |
/** |
| 105 |
* Implementation of hook_cron(). |
* Implement hook_cron(). |
|
* |
|
| 106 |
*/ |
*/ |
| 107 |
function db_maintenance_cron() { |
function db_maintenance_cron() { |
| 108 |
$last_run = variable_get('db_maintenance_cron_last', 0); |
$last_run = variable_get('db_maintenance_cron_last', 0); |
| 115 |
|
|
| 116 |
/** |
/** |
| 117 |
* Perform the maintenance. |
* Perform the maintenance. |
|
* |
|
| 118 |
*/ |
*/ |
| 119 |
function db_maintenance_optimize_tables() { |
function db_maintenance_optimize_tables() { |
| 120 |
global $db_url; |
global $databases; |
| 121 |
|
|
| 122 |
// Set the databases array if not already set in $db_url. |
$db_name = $db = $databases['default']['default']['database']; |
|
if (is_array(($db_url))) { |
|
|
$databases = $db_url; |
|
|
} |
|
|
else { |
|
|
$databases['default'] = $db_url; |
|
|
} |
|
| 123 |
// Loop through each database optimizing any selected tables. |
// Loop through each database optimizing any selected tables. |
| 124 |
foreach ($databases as $db => $connection) { |
// foreach ($databases as $db => $connection) { |
| 125 |
$config_tables = variable_get('db_maintenance_table_list_'. $db, NULL); |
$config_tables = variable_get('db_maintenance_table_list_' . $db, NULL); |
| 126 |
// Only proceed if tables are selected for this database. |
// Only proceed if tables are selected for this database. |
| 127 |
if (is_array($config_tables) && sizeof($config_tables) > 0) { |
if (is_array($config_tables) && sizeof($config_tables) > 0) { |
|
$db_name = $db == 'default' ? 'Drupal' : $db; |
|
| 128 |
while (list(, $table_name) = each($config_tables)) { |
while (list(, $table_name) = each($config_tables)) { |
| 129 |
// Set the database to query. |
// Set the database to query. |
| 130 |
$previous = db_set_active($db); |
$previous = db_set_active($db); |
| 131 |
if (db_table_exists($table_name)) { |
if (db_table_exists($table_name)) { |
| 132 |
if (_db_maintenance_determine_software() == 'mysql') { |
// PDO doesn't replace table names with ? or : |
| 133 |
db_query('OPTIMIZE TABLE %s', $table_name); |
if ($databases['default']['default']['driver'] == 'mysql') { |
| 134 |
|
db_query(sprintf("OPTIMIZE TABLE %s", $table_name)); |
| 135 |
} |
} |
| 136 |
elseif (_db_maintenance_determine_software() == 'pgsql') { |
elseif ($databases['default']['default']['driver'] == 'pgsql') { |
| 137 |
db_query('VACUUM ANALYZE %s', $table_name); |
db_query(sprintf("VACUUM ANALYZE %s", $table_name)); |
| 138 |
} |
} |
| 139 |
} |
} |
| 140 |
else { |
else { |
| 149 |
watchdog('db_maintenance', 'Optimized tables in @db database: @tables', array('@db' => $db_name, '@tables' => $tables), WATCHDOG_INFO); |
watchdog('db_maintenance', 'Optimized tables in @db database: @tables', array('@db' => $db_name, '@tables' => $tables), WATCHDOG_INFO); |
| 150 |
} |
} |
| 151 |
} |
} |
| 152 |
} |
// } |
| 153 |
variable_set('db_maintenance_cron_last', time()); |
variable_set('db_maintenance_cron_last', time()); |
| 154 |
} |
} |
| 155 |
|
|
| 162 |
* @return array |
* @return array |
| 163 |
*/ |
*/ |
| 164 |
function db_maintenance_admin_settings() { |
function db_maintenance_admin_settings() { |
| 165 |
global $db_url; |
global $databases; |
| 166 |
|
|
| 167 |
$form = array(); |
$form = array(); |
| 168 |
$form['db_maintenance_log'] = array( |
$form['db_maintenance_log'] = array( |
| 187 |
'#title' => t('Optimize tables'), |
'#title' => t('Optimize tables'), |
| 188 |
'#options' => $options, |
'#options' => $options, |
| 189 |
'#default_value' => variable_get('db_maintenance_cron_frequency', 86400), |
'#default_value' => variable_get('db_maintenance_cron_frequency', 86400), |
| 190 |
'#description' => t('Select how often database tables should be optimized.') .' '. l(t('Optimize now.'), 'db_maintenance/optimize'), |
'#description' => t('Select how often database tables should be optimized.') . ' ' . l(t('Optimize now.'), 'db_maintenance/optimize'), |
| 191 |
); |
); |
| 192 |
// Set the databases array if not already set in $db_url. |
// Set the databases array if not already set in $db_url. |
|
if (is_array($db_url)) { |
|
|
$databases = $db_url; |
|
|
} |
|
|
else { |
|
|
$databases['default'] = $db_url; |
|
|
} |
|
| 193 |
$options = array(); |
$options = array(); |
| 194 |
// Loop through each database and list the possible tables to optimize. |
// Loop through each database and list the possible tables to optimize. |
| 195 |
foreach ($databases as $db => $connection) { |
// foreach ($databases as $db => $connection) { |
| 196 |
$options = _db_maintenance_list_tables($db); |
$options = _db_maintenance_list_tables($databases['default']['default']['database']); |
| 197 |
|
|
| 198 |
$form['db_maintenance_table_list_'. $db] = array( |
$form['db_maintenance_table_list_' . $databases['default']['default']['database']] = array( |
| 199 |
'#type' => 'select', |
'#type' => 'select', |
| 200 |
'#title' => t('Tables in the !db database', array('!db' => $db == 'default' ? 'Drupal' : $db)), |
'#title' => t('Tables in the !db database', array('!db' => $databases['default']['default']['database'] == 'default' ? 'Drupal' : $databases['default']['default']['database'])), |
| 201 |
'#options' => $options, |
'#options' => $options, |
| 202 |
'#default_value' => variable_get('db_maintenance_table_list_'. $db, ''), |
'#default_value' => variable_get('db_maintenance_table_list_' . $databases['default']['default']['database'], ''), |
| 203 |
'#description' => t('Selected tables will be optimized during cron runs.'), |
'#description' => t('Selected tables will be optimized during cron runs.'), |
| 204 |
'#multiple' => true, |
'#multiple' => TRUE, |
| 205 |
'#attributes' => array('size' => count($options)), |
'#attributes' => array('size' => count($options)), |
| 206 |
); |
); |
| 207 |
} |
// } |
| 208 |
|
|
| 209 |
return system_settings_form($form); |
return system_settings_form($form); |
| 210 |
} |
} |
|
|
|
|
/** |
|
|
* Determine which database software is in use |
|
|
*/ |
|
|
function _db_maintenance_determine_software() { |
|
|
global $db_url; |
|
|
static $db_type; |
|
|
|
|
|
if (!empty($db_type)) { |
|
|
return $db_type; |
|
|
} |
|
|
elseif (strpos($db_url, 'mysql://') === 0 || strpos($db_url, 'mysqli://') === 0) { |
|
|
$db_type = 'mysql'; |
|
|
return $db_type; |
|
|
} |
|
|
elseif (strpos($db_url, 'pgsql://') === 0) { |
|
|
$db_type = 'pgsql'; |
|
|
return $db_type; |
|
|
} |
|
|
else { |
|
|
return false; |
|
|
} |
|
|
} |
|