You are here

function drush_paranoia_list_projects_to_delete in Paranoia 7

Callback for paranoia-list-projects-to-delete.

Deletes directories and files for unused projects.

File

./paranoia.drush.inc, line 153
Drush integration for the paranoia module.

Code

function drush_paranoia_list_projects_to_delete() {

  // TODO: also handle profiles?
  // Get a list of all projects that are not profiles.
  $all_projects = db_query("SELECT name, filename, status, type FROM {system} WHERE filename not like '%.profile' ORDER BY filename ASC")
    ->fetchAllAssoc('name');
  $enabled_parent_dirs = $disabled_projects = array();
  $themes = list_themes();

  // Allow a site to declare modules to keep. Handy if the site disables
  // modules in a backup process.
  $modules_to_keep = module_invoke_all('paranoia_get_required_modules');

  // Get a list of directories that are the parent for enabled projects.
  foreach ($all_projects as $project) {

    // Themes list their .info file.
    $file_type = $project->type == 'module' ? 'module' : 'info';
    $project->directory = str_replace($project->name . '.' . $file_type, '', $project->filename);
    if ($project->status || $project->type == 'theme' && drupal_theme_access($project->name) || in_array($project->name, $modules_to_keep)) {
      $enabled_parent_dirs[$project->name] = $project->directory;
    }
    else {
      $disabled_projects[$project->name] = $project->directory;
    }
  }

  // Include base themes on enabled themes in list dirs in use.
  foreach ($all_projects as $project) {
    if ($project->type == 'theme' && isset($themes[$project->name]) && drupal_theme_access($project->name) && !empty($themes[$project->name]->base_theme)) {

      // Mark it enabled for sure.
      $enabled_parent_dirs[$themes[$project->name]->base_theme] = $all_projects[$themes[$project->name]->base_theme]->directory;

      // Unset it from disabled in case it is.
      unset($disabled_projects[$themes[$project->name]->base_theme]);
    }
  }
  $dirs_to_delete = array();
  $common_types = array(
    'inc',
    'admin.inc',
    'module',
    'info',
    'install',
  );
  foreach ($disabled_projects as $disabled_project_name => $disabled_project_dir) {

    // Only remove a directory if it's not the beginning (strpos) of an enabled dir.
    if (!_paranoia_dir_is_beginning_of_dirs($disabled_project_dir, $enabled_parent_dirs)) {

      // Make a list. Use the dir as key to remove dups.
      $dirs_to_delete[$disabled_project_dir] = $disabled_project_dir;
    }
    else {

      // For disabled projects that *do* match the parent directory, at least
      // some files can be deleted. Print those.
      foreach ($common_types as $type) {
        if (file_exists("{$disabled_project_dir}{$disabled_project_name}.{$type}")) {
          echo "rm -f {$disabled_project_dir}{$disabled_project_name}.{$type}" . PHP_EOL;
        }
      }
    }
  }

  // Print an 'rm -rf' for the parent directory of disabled projects.
  foreach ($dirs_to_delete as $dir_to_delete) {
    echo 'rm -rf ' . $dir_to_delete . PHP_EOL;
  }

  // A few bonus things that might break a site.
  $risky = drush_get_option('remove-lots-risky-htaccess', FALSE);
  if ($risky) {
    echo "rm -f scripts/" . PHP_EOL;
    echo "rm -f profiles/testing" . PHP_EOL;
    echo "rm CHANGELOG.txt COPYRIGHT.txt INSTALL.mysql.txt INSTALL.pgsql.txt INSTALL.sqlite.txt INSTALL.txt LICENSE.txt MAINTAINERS.txt README.txt UPGRADE.txt authorize.php cron.php install.php update.php web.config xmlrpc.php .htaccess .gitignore" . PHP_EOL;
  }
}