You are here

function apdqc_modules_installed in Asynchronous Prefetch Database Query Cache 7

Implements hook_modules_installed().

File

./apdqc.module, line 130
Asynchronous Prefetch Database Query Cache module.

Code

function apdqc_modules_installed($modules) {
  $collation = variable_get('apdqc_table_collations', APDQC_TABLE_COLLATIONS);
  $innodb = variable_get('apdqc_innodb', APDQC_INNODB);
  $table_indexes = variable_get('apdqc_table_indexes', APDQC_TABLE_INDEXES);
  $sessions_schema = variable_get('apdqc_sessions_schema', APDQC_SESSIONS_SCHEMA);
  if (empty($collation) && !empty($innodb) && !empty($table_indexes)) {

    // Cache tables have not been altered.
    return;
  }
  if ($collation === TRUE) {
    $collation = 'utf8_bin';
  }
  $ascii = array(
    'binary' => TRUE,
    'collation' => 'ascii_bin',
    'charset' => 'ascii',
    'mysql_character_set' => 'ascii',
  );

  // Ensure we have the admin functions.
  module_load_include('admin.inc', 'apdqc');

  // Check for cache tables in the recently enabled modules.
  foreach ($modules as $module) {
    $schema = drupal_get_schema_unprocessed($module);
    if (empty($schema)) {
      continue;
    }
    _drupal_schema_initialize($schema, $module, FALSE);
    foreach ($schema as $table_name => &$values) {
      if ($sessions_schema) {
        $changed = FALSE;
        foreach ($values['fields'] as $column => &$attributes) {

          // Convert other session schemas to match the new session schema.
          if (($column === 'sid' || $column === 'ssid') && !empty($attributes['length']) && $attributes['length'] >= 43 && stripos($attributes['type'], 'char') !== FALSE) {
            $attributes['length'] = 43;
            $attributes['type'] = 'char';
            $attributes += $ascii;
            $changed = TRUE;
          }
        }
        if ($changed) {
          apdqc_admin_sessions_table_duplicates(TRUE, FALSE, array(
            $table_name => $values,
          ));
        }
      }
      if (strpos($table_name, 'cache') !== 0) {

        // Remove if not a cache* table.
        unset($schema[$table_name]);
        continue;
      }
      if (empty($schema[$table_name]['fields']['cid'])) {

        // Remove if no cid field.
        unset($schema[$table_name]);
        continue;
      }
    }
    if (empty($schema)) {

      // Skip if this module doesn't have any cache tables.
      continue;
    }
    $schema_keys = array_keys($schema);
    if (!empty($innodb)) {
      apdqc_admin_change_table_engine(TRUE, $schema_keys);
    }
    if (!empty($collation)) {
      apdqc_admin_change_table_collation(TRUE, $collation, $schema);
    }
    if (!empty($table_indexes)) {

      // Drop the expire index; use expire_created.
      $before = array(
        'expire',
      );
      $after = array(
        'expire',
        'created',
      );
      apdqc_convert_cache_index($before, $after, $schema_keys);
    }
  }
}