You are here

function drush_features_revert in Features 7.2

Same name and namespace in other branches
  1. 6 features.drush.inc \drush_features_revert()
  2. 7 features.drush.inc \drush_features_revert()

Drush command callback for 'features-revert'.

Reverts a feature to its code definition. Optionally accepts a list of components to revert.

File

./features.drush.inc, line 829
Features module drush integration.

Code

function drush_features_revert() {
  if ($args = func_get_args()) {
    module_load_include('inc', 'features', 'features.export');
    features_include();

    // Determine if revert should be forced.
    $force = drush_get_option('force');

    // Determine if -y was supplied. If so, we can filter out needless output
    // from this command.
    $skip_confirmation = drush_get_context('DRUSH_AFFIRMATIVE');

    // Parse list of arguments.
    $modules = array();
    foreach ($args as $arg) {
      $arg = explode('.', $arg);
      $module = array_shift($arg);
      $component = array_shift($arg);
      if (isset($module)) {
        if (empty($component)) {

          // If we received just a feature name, this means that we need all of
          // its components.
          $modules[$module] = TRUE;
        }
        elseif ($modules[$module] !== TRUE) {
          if (!isset($modules[$module])) {
            $modules[$module] = array();
          }
          $modules[$module][] = $component;
        }
      }
    }

    // Process modules.
    foreach ($modules as $module => $components_needed) {
      $dt_args['@module'] = $module;
      if (($feature = features_load_feature($module, TRUE)) && module_exists($module)) {
        $components = array();
        if ($force) {

          // Forcefully revert all components of a feature.
          foreach (array_keys($feature->info['features']) as $component) {
            if (features_hook($component, 'features_revert')) {
              $components[] = $component;
            }
          }
        }
        else {

          // Only revert components where the status is detected to be one of
          // "Overridden", "Needs review" or "rebuildable".
          $states = features_get_component_states(array(
            $feature->name,
          ), FALSE);
          foreach ($states[$feature->name] as $component => $state) {
            $revertable_states = array(
              FEATURES_OVERRIDDEN,
              FEATURES_NEEDS_REVIEW,
              FEATURES_REBUILDABLE,
            );
            if (in_array($state, $revertable_states) && features_hook($component, 'features_revert')) {
              $components[] = $component;
            }
          }
        }
        if (!empty($components_needed) && is_array($components_needed)) {
          $components = array_intersect($components, $components_needed);
        }
        if (empty($components)) {
          drush_log(dt('Current state already matches defaults, aborting.'), 'ok');
        }
        else {
          foreach ($components as $component) {
            $dt_args['@component'] = $component;
            $confirmation_message = 'Do you really want to revert @module.@component?';
            if ($skip_confirmation || drush_confirm(dt($confirmation_message, $dt_args))) {
              if (features_feature_is_locked($module, $component)) {
                drush_log(dt('Skipping locked @module.@component.', $dt_args), 'ok');
              }
              else {
                features_revert(array(
                  $module => array(
                    $component,
                  ),
                ));
                drush_log(dt('Reverted @module.@component.', $dt_args), 'ok');
              }
            }
            else {
              drush_log(dt('Skipping @module.@component.', $dt_args), 'ok');
            }
          }
        }
      }
      elseif ($feature) {
        _features_drush_set_error($module, 'FEATURES_FEATURE_NOT_ENABLED');
      }
      else {
        _features_drush_set_error($module);
      }
    }
  }
  else {
    drush_print_table(drush_features_list());
    return;
  }
}