You are here

function demo_enum_tables in Demonstration site (Sandbox / Snapshot) 8

Same name and namespace in other branches
  1. 5 demo.admin.inc \demo_enum_tables()
  2. 6 demo.admin.inc \demo_enum_tables()
  3. 7 demo.admin.inc \demo_enum_tables()

Returns a list of tables in the active database.

Only returns tables whose prefix matches the configured one (or ones, if there are multiple).

2 calls to demo_enum_tables()
DemoDumpForm::buildForm in src/Form/DemoDumpForm.php
form to create database snapshots.
_demo_reset in ./demo.module
Reset site using snapshot.

File

./demo.module, line 470

Code

function demo_enum_tables() {
  $tables = [];
  $connection = Database::getConnection();
  $db_options = $connection
    ->getConnectionOptions();

  // Create a regex that matches the table prefix(es).
  // We are only interested in non-empty table prefixes.
  $prefixes = [];
  if (!empty($db_options['prefix'])) {
    if (is_array($db_options['prefix'])) {
      $prefixes = array_filter($db_options['prefix']);
    }
    elseif ($db_options['prefix'] != '') {
      $prefixes['default'] = $db_options['prefix'];
    }
    $rx = '/^' . implode('|', $prefixes) . '/';
  }

  // Query the database engine for the table list.
  $result = _demo_enum_tables();
  foreach ($result as $table) {
    if (!empty($prefixes)) {

      // Check if table name matches a configured prefix.
      if (preg_match($rx, $table, $matches)) {
        $table_prefix = $matches[0];
        $plain_table = substr($table, strlen($table_prefix));
        if (isset($prefixes[$plain_table]) && $prefixes[$plain_table] == $table_prefix || $prefixes['default'] == $table_prefix) {
          $tables[$table] = [
            'schema' => TRUE,
            'data' => TRUE,
          ];
        }
      }
    }
    else {
      $tables[$table] = [
        'schema' => TRUE,
        'data' => TRUE,
      ];
    }
  }

  // Apply default exclude list.
  $excludes = [
    // Drupal core.
    '{cache}',
    '{cache_bootstrap}',
    '{cache_block}',
    '{cache_content}',
    '{cache_field}',
    '{cache_filter}',
    '{cache_form}',
    '{cache_menu}',
    '{cache_page}',
    '{cache_path}',
    '{cache_update}',
    '{watchdog}',
    // CTools.
    '{ctools_object_cache}',
    // Administration menu.
    '{cache_admin_menu}',
    // Panels.
    '{panels_object_cache}',
    // Views.
    '{cache_views}',
    '{cache_views_data}',
    '{views_object_cache}',
    '{cache_config}',
    '{cache_container}',
    '{cache_data}',
    '{cache_default}',
    '{cache_discovery}',
    '{cache_render}',
    '{cachetags}',
    '{cache_entity}',
  ];
  foreach (array_map([
    $connection,
    'prefixTables',
  ], $excludes) as $table) {
    if (isset($tables[$table])) {
      $tables[$table]['data'] = FALSE;
    }
  }
  return $tables;
}