Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added backdrop-pm-lost command. #152

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions backdrop.drush.inc
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ function backdrop_drush_command_alter(&$command) {
case 'pm-list':
$backdrop_command = 'backdrop-pm-list';
break;
case 'pm-lost':
$backdrop_command = 'backdrop-pm-lost';
break;
}

// Commands that work with Backdrop with no adjustments.
Expand Down
157 changes: 157 additions & 0 deletions commands/pm/backdrop_pm_lost.drush.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php
/**
* @file
* Drush command to list disabled or not-installed projects in Backdrop sites.
*
* This can be useful for detecting projects which were downloaded and then
* forgotten, or projects which have been uninstalled/disabled but not removed
* from the file system.
*
* Inspired by the Drupal version: https://gist.github.com/kalabro/3187485
*/

/**
* Implementation of hook_drush_command().
*/
function backdrop_pm_lost_drush_command() {
return array(
'backdrop-pm-lost' => array(
'description' => 'Show a list of lost (downloaded but disabled) projects.',
'aliases' => array('lost'),
),
);
}

/**
* Callback function for the 'backdrop-pm-lost' command.
*/
function drush_backdrop_pm_lost() {
// Output a leading newline.
drush_print();

// Get 'lost' projects.
$projects = backdrop_pm_lost_get_list();

if (!empty($projects)) {
// Sort projects.
uasort($projects, '_backdrop_pm_lost_sort_projects');

// Output table with header row.
$rows = array(
array(dt('NAME'), dt('TYPE'), dt('PACKAGE'), dt('VERSION'), dt('STATUS')),
);

// Output each project's details.
foreach ($projects as $name => $project) {
$rows[] = array(
$project->name . ' (' . $project->info['name'] . ')',
ucfirst($project->type),
!empty($project->info['package']) ? $project->info['package'] : '',
$project->info['version'],
_backdrop_pm_lost_status($project),
);
}

// Display output.
drush_print_table($rows, TRUE);
}
else {
drush_print(dt("No 'lost' projects."), 1);
}

// Output a trailing newline.
drush_print();
}

/**
* Return a list of 'lost' projects.
*/
function backdrop_pm_lost_get_list() {
$lost_projects = array();

// Get all modules & themes.
// Copied from modules_list_callback() in backdrop_pm_list.drush.inc.
$modules = system_rebuild_module_data();
$themes = system_rebuild_theme_data();
$query = db_select('system', 's')
->fields('s')
->execute();
$projects = $query->fetchAll();

// Find 'lost' projects.
foreach ($projects as $project) {
if ($project->status == 0) {
$lost = TRUE;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BWPanda Isn't this going to mark a module as lost when it is just disabled? I thought the definition of lost was a module that still has a datbase record, but is not actually in the filesystem.

Maybe i've got that wrong.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this going to mark a module as lost when it is just disabled?

Yes. Kate describes her original Drush command for Drupal like so: "Drush command 'pm-lost' lists projects which are not installed or disabled. It can be useful to detect waste modules which was downloaded once and then forgotten."

I didn't think about modules existing in the database but not in the file system... If that's a possibility, then that might make a good feature request ;-)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so how is this different than drush pml?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tested both on my site.

lost shows one contrib theme I have downloaded which isn't enabled.

pml shows all modules and themes on my site regardless of status.

Basically, lost doesn't show:

  • 'Enabled' projects
  • Sub-projects (e.g. I have 'devel' enabled, but not its sub-module 'devel_node_access')
  • Core projects

So again, the purpose of lost is to run it on a site and get a list of contrib modules and themes that can be removed from the file system (as they're not being used), or to remind you to enable them if you do need them.


$project->info = unserialize($project->info);
if (!empty($project->info['project'])) {
// Ignore core projects.
if ($project->info['project'] == 'backdrop') {
$lost = FALSE;
}

// Ignore sub-projects.
if ($project->info['project'] != $project->name) {
$lost = FALSE;
}
}

// Ignore base themes that have an active sub-theme.
if ($project->type == 'theme') {
foreach ($themes as $theme) {
if (!empty($theme->info['base theme']) && $theme->info['base theme'] == $project->name && $theme->status == 1) {
$lost = FALSE;
}
}
}

// Make a list of 'lost' projects.
if ($lost) {
$lost_projects[$project->name] = $project;
}
}
}

return $lost_projects;
}

/**
* Sort callback function for sorting projects.
*
* It will sort by type, then machine name.
*
* Partly copied from _drush_pm_sort_extensions() in pm.drush.inc.
*/
function _backdrop_pm_lost_sort_projects($a, $b) {
if ($a->type == 'module' && $b->type == 'theme') {
return -1;
}
if ($a->type == 'theme' && $b->type == 'module') {
return 1;
}

$cmp = strcasecmp($a->name, $b->name);

return $cmp;
}

/**
* Determine a project's status based on its current status and schema version.
*
* Partly copied from drush_get_extension_status() in pm.drush.inc.
*/
function _backdrop_pm_lost_status($project) {
$status = 'Disabled';

if ($project->type == 'module') {
// Reminder to uninstall first.
$status .= ' (needs uninstalling)';

if ($project->schema_version == -1) {
$status = 'Not installed';
}
}

return $status;
}