You are here

function drush_features_revert in Features 7

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

Revert a feature to it's code definition. Optionally accept a list of components to revert.

File

./features.drush.inc, line 585
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');

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

          // If we received just a feature name, this means that we need all of it's 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) {
      if (($feature = features_load_feature($module, TRUE)) && module_exists($module)) {
        $components = array();

        // Forcefully revert all components of a feature.
        if ($force) {
          foreach (array_keys($feature->info['features']) as $component) {
            if (features_hook($component, 'features_revert')) {
              $components[] = $component;
            }
          }
        }
        else {
          $states = features_get_component_states(array(
            $feature->name,
          ), FALSE);
          foreach ($states[$feature->name] as $component => $state) {
            if (in_array($state, array(
              FEATURES_OVERRIDDEN,
              FEATURES_NEEDS_REVIEW,
              FEATURES_REBUILDABLE,
            )) && 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) {
            if (drush_confirm(dt('Do you really want to revert @component?', array(
              '@component' => $component,
            )))) {
              features_revert(array(
                $module => array(
                  $component,
                ),
              ));
              drush_log(dt('Reverted @component.', array(
                '@component' => $component,
              )), 'ok');
            }
            else {
              drush_log(dt('Skipping @component.', array(
                '@component' => $component,
              )), 'ok');
            }
          }
        }
      }
      else {
        if ($feature) {
          _features_drush_set_error($module, 'FEATURES_FEATURE_NOT_ENABLED');
        }
        else {
          _features_drush_set_error($module);
        }
      }
    }
  }
  else {
    drush_features_list();
    return;
  }
}