You are here

public function DefaultController::schema_compare in Schema 8

1 string reference to 'DefaultController::schema_compare'
schema.routing.yml in ./schema.routing.yml
schema.routing.yml

File

src/Controller/DefaultController.php, line 54
Contains \Drupal\schema\Controller\DefaultController.

Class

DefaultController
Default controller for the schema module.

Namespace

Drupal\schema\Controller

Code

public function schema_compare() {
  $build = array();
  $states = array(
    'same' => t('Match'),
    'different' => t('Mismatch'),
    'missing' => t('Missing'),
  );
  $descs = array(
    'same' => t('Tables for which the schema and database agree.'),
    'different' => t('Tables for which the schema and database are different.'),
    'missing' => t('Tables in the schema that are not present in the database.'),
  );
  $schema = schema_get_schema(TRUE);
  $info = schema_compare_schemas($schema);

  // The info array is keyed by state (same/different/missing/extra/warn). For missing,
  // the value is a simple array of table names. For warn, it is a simple array of warnings.
  // Get those out of the way first
  if (isset($info['warn'])) {
    foreach ($info['warn'] as $message) {
      drupal_set_message($message, 'warning');
    }
    unset($info['warn']);
  }
  $build['extra'] = array(
    '#type' => 'details',
    '#title' => t('Extra (@count)', array(
      '@count' => isset($info['extra']) ? count($info['extra']) : 0,
    )),
    '#description' => t('Tables in the database that are not present in the schema. This indicates previously installed modules that are disabled but not un-installed or modules that do not use the Schema API.'),
    '#weight' => 50,
  );
  $build['extra']['tablelist'] = array(
    '#theme' => 'item_list',
    '#items' => isset($info['extra']) ? $info['extra'] : array(),
  );
  unset($info['extra']);

  // For the other states, the value is an array keyed by module name. Each value
  // in that array is an array keyed by tablename, and each of those values is an
  // array containing 'status' (same as the state), an array of reasons, and an array of notes.
  $weight = 0;
  foreach ($info as $state => $modules) {

    // We'll fill in the fieldset title below, once we have the counts
    $build[$state] = array(
      '#type' => 'details',
      '#description' => $descs[$state],
      '#weight' => $weight++,
    );
    $counts[$state] = 0;
    foreach ($modules as $module => $tables) {
      $counts[$state] += count($tables);
      $build[$state][$module] = array(
        '#type' => 'details',
        '#title' => $module,
      );
      switch ($state) {
        case 'same':
        case 'missing':
          $build[$state][$module]['tablelist'] = array(
            '#theme' => 'item_list',
            '#items' => array_keys($tables),
          );
          break;
        case 'different':
          $items = array();
          foreach ($tables as $name => $stuff) {
            $build[$state][$module][$name] = array(
              '#type' => 'details',
              '#title' => $name,
            );
            $build[$state][$module][$name]['reasons'] = array(
              '#theme' => 'item_list',
              '#items' => array_merge($tables[$name]['reasons'], $tables[$name]['notes']),
            );
          }
          break;
      }
    }
  }

  // Fill in counts in titles
  foreach ($states as $state => $description) {
    $build[$state]['#title'] = t('@state (@count)', array(
      '@state' => $states[$state],
      '@count' => isset($counts[$state]) ? $counts[$state] : 0,
    ));
  }
  return $build;
}