You are here

function variable_clean_form in Variable Cleanup 6

Same name and namespace in other branches
  1. 7 variable_clean.module \variable_clean_form()

Form builder for variable cleanup.

See also

variable_clean_form_submit()

1 string reference to 'variable_clean_form'
variable_clean_menu in ./variable_clean.module
Implementation of hook_menu()

File

./variable_clean.module, line 29
Allows you to remove variables not currently used.

Code

function variable_clean_form(&$form_state) {
  _variable_clean_timeouts();

  // Confirmation form.
  if (empty($form_state['storage']['step']) || $form_state['storage']['step'] == 'confirm') {
    $form_state['storage']['step'] = 'confirm';
    return confirm_form(array(), t('Cleanup variables'), 'admin/settings', t('Are you sure you want to scan your codebase for all used variables (This may take a long time)?'), t('Scan codebase'), t('Cancel'));
  }

  // Scan results form.
  $form_state['storage']['step'] = 'scan';
  $code_lines = $not_in = array();
  $return_var = 0;
  _variable_clean_timeouts();

  // Compile a list of directories to scan.
  $code_directories = array(
    'update.php',
    'install.php',
    'includes' . DIRECTORY_SEPARATOR,
    'modules' . DIRECTORY_SEPARATOR,
    'profiles' . DIRECTORY_SEPARATOR,
    'themes' . DIRECTORY_SEPARATOR,
    'sites' . DIRECTORY_SEPARATOR . 'all' . DIRECTORY_SEPARATOR,
  );
  $site_directory = dirname($_SERVER['SCRIPT_FILENAME']) . DIRECTORY_SEPARATOR . conf_path() . DIRECTORY_SEPARATOR;
  foreach ((array) glob($site_directory . '*', GLOB_ONLYDIR) as $directory) {
    $sub_directory = str_replace($site_directory, '', $directory);
    if (!in_array($sub_directory, array(
      'files',
      'CVS',
      '.svn',
    ))) {
      $code_directories[] = conf_path() . DIRECTORY_SEPARATOR . $sub_directory . DIRECTORY_SEPARATOR;
    }
  }

  // Get a list of all lines of code using variables.
  chdir(dirname($_SERVER['SCRIPT_FILENAME']));
  foreach ($code_directories as $code_directory) {
    drupal_set_message(t('Scaning %directory.', array(
      '%directory' => $code_directory,
    )));
    exec('grep -rn "^[:space:]*[^/\\*]*variable_[g,s]et" ' . escapeshellarg($code_directory), $code_lines, $return_var);
  }
  drupal_set_message(t('Scaning complete.'));

  // If there's less than 50 instances, something went horribly wrong.
  $variable_count = count($code_lines);
  if (!$variable_count || $variable_count < 50) {
    $message = t('Only %variable_count variable uses were found in code. This could not possibly be correct. <pre>!dump</pre>', array(
      '%variable_count' => $variable_count,
      '!dump' => filter_xss_admin(var_export($code_lines, TRUE)),
    ));
    $form['error']['#value'] = '<p>' . $message . '</p>';
    return $form;
  }

  // Reduce the list of code to a list of variables.
  extract(_variable_clean_code_get_variables($code_lines));

  //  dpm($static_variables, '$static_variables');
  //  dpm($dynamic_variables, '$dynamic_variables');
  //  dpm($non_processable_variables, '$non_processable_variables');
  if ($non_processable_variables) {

    // Remove our test vars.
    foreach ($non_processable_variables as $key => $variable) {
      if (strpos($variable, 'variable_clean_test') !== FALSE) {
        unset($non_processable_variables[$key]);
      }
    }
    $form['non_processable_variables']['#value'] = '<p>' . t('The following code prevents an accurate determination of what variables are in use.  Proceed carefully! !variables', array(
      '!variables' => '<br />' . filter_xss_admin(theme_item_list($non_processable_variables)),
    )) . '</p>';
  }
  $form['variables'] = array(
    '#type' => 'checkboxes',
    '#title' => t('The following variables appear to be unused and could be deleted.  Do so at your own risk'),
    '#options' => array(),
  );

  // Get all variables in the DB that are not used as static variables.
  if ($static_variables) {
    foreach ($static_variables as $variable) {
      $not_in[] = "'" . db_escape_string($variable) . "'";
    }
    $result = db_query('SELECT name FROM {variable}
      WHERE name NOT IN(' . implode(',', $not_in) . ')
      ORDER BY name');
    while ($db_variable = db_fetch_object($result)) {

      // Is it a dynamic variable?
      foreach ($dynamic_variables as $variable) {
        if (strpos($db_variable->name, $variable) === 0) {
          continue 2;
        }
      }

      // We found an honest-to-goodness unused variable.
      $form['variables']['#options'][$db_variable->name] = $db_variable->name;
    }
  }
  else {
    $form['error']['#value'] = '<p>' . t('Error.  No variables were found in the code.') . '/<p>';
  }
  if ($form['variables']['#options']) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
    );
  }
  else {
    unset($form['variables']);
    $form['error']['#value'] = '<p>' . t('All variables in the database are used in code.') . '</p>';
  }
  return $form;
}