You are here

function upgrade_assist_states in Upgrade Status 6

Same name and namespace in other branches
  1. 5 upgrade_assist/upgrade_assist.module \upgrade_assist_states()

Return upgrade states.

Parameters

$item_list: (optional) Whether to return states in a structure suitable for theme_item_list().

1 call to upgrade_assist_states()
upgrade_assist_block_view in upgrade_assist/upgrade_assist.module
Implements hook_block_view().

File

upgrade_assist/upgrade_assist.module, line 44
Assists in upgrading Drupal.

Code

function upgrade_assist_states($item_list = FALSE) {
  $core = variable_get('upgrade_assist_core', NULL);

  // @todo Should only be set by the module update function, not in hook_install().
  $core_current = variable_get('upgrade_assist_core_current', NULL);
  if (!isset($core) || !isset($core_current)) {
    drupal_set_message(t('Upgrade Assist installation failure. Re-install the module to start over.'), 'error');
    return;
  }
  $core_is_upgraded = $core > $core_current;
  if (module_exists('upgrade_status')) {
    $states['upgrade-status'] = array(
      'title' => t('Check upgrade status'),
      'phase' => 'pre-upgrade',
      // This is purely informational, so always TRUE.
      'status' => TRUE,
      'href' => 'admin/reports/updates/upgrade',
    );
  }
  $states['update-modules'] = array(
    'title' => t('Update modules'),
    'phase' => 'pre-upgrade',
    // If Update (Status) module is not enabled, the module update status has to
    // be confirmed manually.
    'status' => NULL,
  );
  switch ($core) {
    case 6:
      $states['update-modules']['href'] = module_exists('update') ? 'admin/reports/updates' : 'admin/build/modules';
      break;
    case 7:
      $states['update-modules']['href'] = module_exists('update') ? 'admin/reports/updates' : 'admin/modules';
      break;
  }
  if (module_exists('update')) {
    module_load_install('update');
    $status = update_requirements('runtime');
    foreach (array(
      'core',
      'contrib',
    ) as $report_type) {
      $type = 'update_' . $report_type;
      if (isset($status[$type]['severity'])) {
        if ($status[$type]['severity'] == REQUIREMENT_ERROR) {
          $states['update-modules']['status'] = FALSE;
        }
        elseif ($status[$type]['severity'] == REQUIREMENT_WARNING) {
          $states['update-modules']['status'] = FALSE;
        }
      }
    }

    // If Update module did not report any errors or warnings, then we must be
    // update to date.
    if (!isset($states['update-modules']['status'])) {
      $states['update-modules']['status'] = TRUE;
    }
  }
  $states['maintenance-mode'] = array(
    'title' => t('Enable maintenance mode'),
    'phase' => 'pre-upgrade',
    'status' => NULL,
  );
  switch ($core) {
    case 6:
      $states['maintenance-mode']['status'] = variable_get('site_offline', 0);
      $states['maintenance-mode']['href'] = 'admin/settings/site-maintenance';
      break;
    case 7:
      $states['maintenance-mode']['status'] = variable_get('maintenance_mode', 0);
      $states['maintenance-mode']['href'] = 'admin/config/development/maintenance';
      break;
  }
  $states['maintenance-theme'] = array(
    'title' => t('Verify maintenance theme'),
    'phase' => 'pre-upgrade',
    'status' => FALSE,
    'href' => 'upgrade/assist/upgrade_assist_maintenance_theme',
  );
  switch ($core) {
    case 6:
      $upgrade_theme = 'garland';
      break;
    case 7:
      $upgrade_theme = 'seven';
      break;
  }
  $theme_path = drupal_get_path('theme', $upgrade_theme);
  $maintenance_theme = variable_get('maintenance_theme', NULL);
  if (file_exists($theme_path) && (!isset($maintenance_theme) || $maintenance_theme == $upgrade_theme)) {
    $states['maintenance-theme']['status'] = $maintenance_theme;
  }
  $states['update-access'] = array(
    'title' => t('Verify access to update.php'),
    'phase' => 'pre-upgrade',
    'status' => $GLOBALS['user']->uid == 1 || !empty($GLOBALS['update_free_access']),
  );
  $states['disable-modules'] = array(
    'title' => t('Disable non-core modules'),
    'phase' => 'pre-upgrade',
    'status' => variable_get('upgrade_assist_modules_disabled', FALSE),
    'href' => 'upgrade/assist/upgrade_assist_disable_modules',
  );
  $states['theme-default'] = array(
    'title' => t('Verify default theme'),
    'phase' => 'pre-upgrade',
    'status' => NULL,
  );
  switch ($core) {
    case 6:
      $states['theme-default']['href'] = 'admin/build/themes';
      if (variable_get('theme_default', '') == 'garland') {
        $states['theme-default']['status'] = TRUE;
      }
      break;
    case 7:
      $states['theme-default']['href'] = 'admin/appearance';
      break;
  }
  $states['backup-current'] = array(
    'title' => t('Backup current site'),
    'phase' => 'pre-upgrade',
    'status' => NULL,
  );
  if (module_exists('demo')) {

    // @todo Add drupal_alter() and move into Demo module.
    module_load_include('inc', 'demo', 'demo.admin');
    $fileconfig = demo_get_fileconfig('pre-upgrade');
    $states['backup-current']['status'] = file_exists($fileconfig['sqlfile']);
    $states['backup-current']['href'] = 'upgrade/assist/upgrade_assist_backup_current/pre-upgrade';
  }
  $states['replace-core'] = array(
    'title' => t('Replace Drupal core'),
    'phase' => 'pre-upgrade',
    'status' => !($core == $core_current),
    'href' => 'upgrade/assist/upgrade_assist_replace_core',
  );
  $states['upgrade-core'] = array(
    'title' => t('Run update.php for core'),
    'phase' => 'pre-upgrade',
    'status' => !($core == $core_current),
    'href' => $GLOBALS['base_url'] . '/update.php',
    'options' => array(
      'external' => TRUE,
    ),
  );
  $modules_upgraded = FALSE;

  // Check whether any of the disabled modules have been replaced with new
  // versions.
  $module_files_upgraded = FALSE;
  $disabled_modules = variable_get('upgrade_assist_modules_disabled', array());

  // As always, there are exceptions to the rule: certain modules have been
  // re-owned by Drupal core. Since the module names are identical, the upgrade
  // check below will lead to false positives, so explicitly exclude them here.
  $disabled_modules = array_diff_key($disabled_modules, array_flip(array(
    // CCK / Field type modules.
    'text',
    'number',
    // Image module.
    'image',
  )));
  if ($core_is_upgraded && $disabled_modules) {
    $module_files_upgraded = array();
    foreach ($disabled_modules as $module => $upgrade_info) {
      $info_file = drupal_get_path('module', $module) . "/{$module}.info";
      $module_info = drupal_parse_info_file($info_file);
      if (isset($module_info['core']) && $module_info['core'] == DRUPAL_CORE_COMPATIBILITY) {
        $module_files_upgraded[] = $module;
      }
    }
    if ($module_files_upgraded) {
      $result = db_query('SELECT name, schema_version FROM {system} WHERE type = :type AND name IN (:modules)', array(
        ':type' => 'module',
        ':modules' => $module_files_upgraded,
      ))
        ->fetchAllKeyed();
      foreach ($result as $module_name => $module_version) {
        if ($module_version[0] == $core && drupal_strlen($module_version) > 1) {
          $modules_upgraded = TRUE;
          break;
        }
      }
    }
  }
  $states['download-modules'] = array(
    'title' => t('Download new project versions'),
    'phase' => 'post-upgrade',
    'status' => $module_files_upgraded,
    'href' => 'upgrade/assist/upgrade_assist_download_projects',
  );

  // @todo Demo module is not as smart as Upgrade Assist ;)
  if ($module_files_upgraded && !module_exists('demo')) {
    system_rebuild_module_data();
    switch ($core) {
      case 7:
        $demo_info = db_query('SELECT info FROM {system} WHERE name = :name', array(
          ':name' => 'demo',
        ))
          ->fetchField();
        $demo_info = unserialize($demo_info);
        break;
    }
    if ($demo_info['core'] == DRUPAL_CORE_COMPATIBILITY) {
      module_enable(array(
        'demo',
      ));
      drupal_flush_all_caches();
    }
  }
  $states['backup-pre-upgrade-modules'] = array(
    'title' => t('Backup current site'),
    'phase' => 'post-upgrade',
    'status' => NULL,
  );
  if (module_exists('demo')) {

    // @todo Add drupal_alter() and move into Demo module.
    module_load_include('inc', 'demo', 'demo.admin');
    $fileconfig = demo_get_fileconfig('pre-upgrade-modules');
    $states['backup-pre-upgrade-modules']['status'] = file_exists($fileconfig['sqlfile']);
    $states['backup-pre-upgrade-modules']['href'] = 'upgrade/assist/upgrade_assist_backup_current/pre-upgrade-modules';
  }
  $states['upgrade-modules'] = array(
    'title' => t('Run update.php for modules'),
    'phase' => 'post-upgrade',
    'status' => $modules_upgraded === TRUE,
    'href' => $GLOBALS['base_url'] . '/update.php',
    'options' => array(
      'external' => TRUE,
    ),
  );
  $states['enable-modules'] = array(
    'title' => t('Re-enable modules'),
    'phase' => 'post-upgrade',
    'status' => variable_get('upgrade_assist_modules_enabled', FALSE),
    'href' => 'upgrade/assist/upgrade_assist_enable_modules',
  );
  $status = FALSE;
  if ($modules_upgraded === TRUE) {

    // Don't perform duplicate checks on admin/reports/status.
    $status = NULL;

    //    if (strpos($_GET['q'], 'admin/reports') !== 0) {
    //      module_load_include('inc', 'system', 'system.admin');
    //      $status = system_status(TRUE);
    //    }
  }
  $states['status-report'] = array(
    'title' => t('Check status report'),
    'phase' => 'post-upgrade',
    'status' => $status,
  );
  switch ($core) {
    case 6:
    case 7:
      $states['status-report']['href'] = 'admin/reports/status';
      break;
  }

  // Override task statuses, depending on core version.
  foreach ($states as $name => $state) {
    if ($state['phase'] == 'pre-upgrade' && $core_is_upgraded) {
      $states[$name]['status'] = TRUE;
    }
  }

  // Set the active (next) task. That is the first step having a status of FALSE
  // (not NULL, as that means we don't know).
  foreach ($states as $name => $state) {
    if (isset($state['status']) && $state['status'] == FALSE) {
      $states[$name]['active'] = TRUE;
      break;
    }
  }

  // Prepare states to be displayed in block as tasks.
  if ($item_list) {
    $items = array();
    $active_step_found = FALSE;
    foreach ($states as $name => $state) {
      $class = array();

      // All steps before the active step can be already done or we don't know
      // the status.
      if (!$active_step_found) {
        if (isset($state['href'])) {
          $state += array(
            'options' => array(),
          );
          $items[$name]['data'] = l($state['title'], $state['href'], $state['options']);
        }
        else {
          $items[$name]['data'] = $state['title'];
        }
        if (!empty($state['active'])) {
          $class[] = 'active';
          $active_step_found = $name;
        }
        if (!empty($state['status'])) {
          $class[] = 'done';
        }
        elseif (!isset($state['status'])) {
          $class[] = 'unsure';
        }
      }
      else {
        $items[$name]['data'] = $state['title'];
        $class[] = 'todo';
      }
      $items[$name]['class'] = $core >= 7 ? $class : implode(' ', $class);
    }
    return $items;
  }
  return $states;
}