optimizedb.module in OptimizeDB 8
Same filename and directory in other branches
Database Optimization.
Optimizing the site database.
File
optimizedb.moduleView source
<?php
/**
* @file
* Database Optimization.
*
* Optimizing the site database.
*/
/**
* Status messages after execute commands.
*
* @param array $form
* An associative array containing the structure of the form.
*/
function optimizedb_operation_messages(array &$form) {
$operations_tables_result = isset($_SESSION['optimizedb_list_tables_operations']) ? $_SESSION['optimizedb_list_tables_operations'] : NULL;
if (!is_null($operations_tables_result)) {
if ($operations_tables_result == []) {
\Drupal::messenger()
->addMessage(t('The operation completed successfully.'));
}
else {
$form['operations_tables'] = [
'#type' => 'fieldset',
'#title' => t('Errors that arose during the operation:'),
];
$form['operations_tables']['errors'] = [
'#markup' => [
'#theme' => 'table',
'#header' => [
[
'data' => t('Table name'),
],
[
'data' => t('Type of problem'),
],
[
'data' => t('Information about the problem'),
],
],
'#rows' => $operations_tables_result,
],
];
}
}
$_SESSION['optimizedb_list_tables_operations'] = NULL;
}
/**
* Performing operations on tables.
*
* @param array $tables
* Tables names.
* @param string $operation_name
* The operation to be performed with tables.
*
* @see \Drupal\optimizedb\Form\OptimizedbListTablesForm::submitForm()
* @see _optimizedb_list_tables_operation_execute_run()
*/
function _optimizedb_list_tables_operation_execute(array $tables, $operation_name) {
if (\Drupal::database()
->driver() == 'mysql') {
if (!empty($tables)) {
$operations = [];
foreach ($tables as $table_name => $selected) {
if ($table_name === $selected) {
$operations[] = [
'_optimizedb_list_tables_operation_execute_run',
[
[
'name' => $table_name,
'operation' => $operation_name,
],
],
];
}
}
if (empty($operations)) {
\Drupal::messenger()
->addError(t('To execute, you must select at least one table from the list.'));
}
else {
$batch = [
'operations' => $operations,
'finished' => '_optimizedb_list_tables_operation_execute_finished',
];
batch_set($batch);
}
}
else {
\Drupal::messenger()
->addError(t('To execute, you must select at least one table from the list.'));
}
}
else {
\Drupal::messenger()
->addError(t('Function to check tables is only available for databases MySQL.'));
}
}
/**
* Batch callback: Common tasks checking, repairs and optimizes tables.
*
* @param array $param
* Table names and types of operations.
* @param array $context
* Reference to an array used for Batch API storage.
*/
function _optimizedb_list_tables_operation_execute_run(array $param, array &$context) {
$operation_result = \Drupal::database()
->query($param['operation'] . ' ' . $param['name'])
->fetchAll();
foreach ($operation_result as $result) {
if (isset($result->Msg_type) && in_array(strtolower($result->Msg_type), [
'error',
'warning',
])) {
$context['results']['errors'][] = [
[
'data' => $param['name'],
],
[
'data' => $result->Msg_type,
],
[
'data' => $result->Msg_text,
],
];
}
}
}
/**
* Output results when a batch is complete.
*
* Callback for batch_set().
*
* @param bool $success
* A boolean indicating whether the batch operation successfully concluded.
* @param int $results
* The results from the batch process.
* @param array $operations
* The batch operations that remained unprocessed. Only relevant if $success
* is FALSE.
*
* @ingroup callbacks
*/
function _optimizedb_list_tables_operation_execute_finished($success, $results, array $operations) {
if ($success) {
if (isset($results['errors'])) {
$result = $results['errors'];
}
else {
$result = [];
}
$_SESSION['optimizedb_list_tables_operations'] = $result;
}
else {
\Drupal::messenger()
->addError(t('Completed with errors.'));
}
}
/**
* Output how much time passed from the specified date.
*
* @param int $timestamp
* The date in Unix format.
*
* @return string
* The formatted date.
*/
function _optimizedb_date($timestamp) {
$timestamp = (int) $timestamp;
if ($timestamp == 0) {
return t('None');
}
$difference = \Drupal::time()
->getRequestTime() - $timestamp;
$text = \Drupal::service('date.formatter')
->formatInterval($difference, 1);
return $text;
}
/**
* List and the size of the database tables.
*
* @return array
* An array with a list of database tables.
*/
function _optimizedb_tables_list() {
$tables = FALSE;
switch (\Drupal::database()
->driver()) {
case 'mysql':
$tables = \Drupal::database()
->query("SHOW TABLE STATUS")
->fetchAll();
break;
case 'pgsql':
$tables = \Drupal::database()
->query("SELECT table_name as \"Name\",\n pg_total_relation_size(table_name) AS \"Data_length\",\n 0 as \"Index_length\"\n FROM information_schema.tables\n WHERE table_schema = 'public'\n ORDER BY table_name")
->fetchAll();
break;
}
$result = [];
if ($tables) {
$size_tables = 0;
foreach ($tables as $table) {
$length = $table->Data_length + $table->Index_length;
$result[$table->Name] = [
'name' => $table->Name,
'size' => format_size($length),
'size_byte' => $length,
];
$size_tables += $length;
}
// The total size of the tables.
\Drupal::configFactory()
->getEditable('optimizedb.settings')
->set('tables_size', $size_tables)
->save();
}
return (array) $result;
}
/**
* Implements hook_cron().
*
* In Cron operations are performed:
* - Display a message on the need to optimize.
*/
function optimizedb_cron() {
$config = \Drupal::configFactory()
->getEditable('optimizedb.settings');
// Check whether there is a need to optimize.
$optimization_period = (int) $config
->get('optimization_period');
if ($optimization_period !== 0) {
$last_optimization = $config
->get('last_optimization');
$time_next_optimization = strtotime('+ ' . $optimization_period . ' day', $last_optimization == 0 ? \Drupal::time()
->getRequestTime() : $last_optimization);
if ($time_next_optimization <= \Drupal::time()
->getRequestTime()) {
$config
->set('notify_optimize', TRUE)
->save();
}
}
}
Functions
Name | Description |
---|---|
optimizedb_cron | Implements hook_cron(). |
optimizedb_operation_messages | Status messages after execute commands. |
_optimizedb_date | Output how much time passed from the specified date. |
_optimizedb_list_tables_operation_execute | Performing operations on tables. |
_optimizedb_list_tables_operation_execute_finished | Output results when a batch is complete. |
_optimizedb_list_tables_operation_execute_run | Batch callback: Common tasks checking, repairs and optimizes tables. |
_optimizedb_tables_list | List and the size of the database tables. |