Provide API for "Is this module /really/ in use?" reporting

Created on 4 June 2009, over 15 years ago
Updated 10 October 2024, 2 months ago

A problem my team frequently faces is revisiting a Drupal site that was developed by a third party (or developed by us in a previous lifetime) and figuring out whether modules that are enabled are really being used, and if so, where and how β€” to devise a strategy for site cleanup and upgrade.

Modules vary widely in functionality, and determining a module's behavior in a generic sense reduces to the Halting Problem, so this isn't really tractable to solve within a single contrib module or core β€” but rather it could be reasonably handled by specifying a light API in core, and asking modules to self-report their usage.

I'm defining in use as true when any of the following conditions are true:

  • The module is storing user-generated data in its own schema or variables.
  • End-users (i.e., not uid 1) have the ability (permission or other configuration) to potentially generate data the module will store.
  • Other modules depending on this module are in use.

Perhaps modules could implement a hook such as the following in the .install file, next to the similar hook_requirements():

/**
 * Report Comment module usage.
 *
 * @return
 *   An array describing the current usage of the Comment module.
 */
function comment_usage() {
  $aspects = array();

  // The Comment module is 'in use' if:
  //    - there are rows in {comment}
  //        (i.e., comments have actually been submitted, regardless of moderation)
  //    - or there are rows in {node} for which the comment field is COMMENT_NODE_OPEN
  //        (i.e., nodes exist upon which comments could be submitted)

  $comments = db_query('SELECT COUNT(*) FROM {comment}')->fetchField();
  $aspects['comments']['description'] = format_plural($count,'1 comment','@count comments');
  if ($comments) {
    $aspects['comments']['usage'] = USAGE_USED;
  }
  else {
    $aspects['comments']['usage'] = USAGE_UNUSED;
  }

  $comments_allowed_on_nodes = db_query('SELECT COUNT(*) FROM {node} WHERE comment = :open', array(':open' => COMMENT_NODE_OPEN))->fetchField();
  $aspects['comments_allowed_on_nodes']['description'] = format_plural($count,'1 commentable node','@count commentable nodes');
  if ($comments_allowed_on_nodes) {
    $aspects['comments_allowed_on_nodes']['usage'] = USAGE_USED;
  }
  else {
    $aspects['comments_allowed_on_nodes']['usage'] = USAGE_UNUSED;
  }


  $usage = array();
  $usage['comment'] = array(
    'title' => t('Comment'),
    'aspects' => $aspects,
  );
  return $usage;
}

...and then core could module_invoke_all() and summarize the information either on admin/build/modules or a MENU_LOCAL_TASK thereunder. Perhaps an additional warning could be raised when the user tries to disable or uninstall a module that's USAGE_USED.

A module-wide verdict could be established according to the following rules:

  • If any of the aspects of the module are USAGE_USED, or any of the aspects of the modules depending on this module are USAGE_USED, the module-wide verdict is USAGE_USED.
  • If none of the aspects of the module are USAGE_USED, the module-wide verdict is USAGE_UNUSED.

Thoughts?

✨ Feature request
Status

Closed: works as designed

Version

11.0 πŸ”₯

Component

base system

Created by

πŸ‡ΊπŸ‡ΈUnited States smokris Athens, Ohio, USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡³πŸ‡ΏNew Zealand quietone

    Two core committers think this is best done in contrib. And combined with the lack of discussion for eight years I am closing this.

    Thanks for the idea!

Production build 0.71.5 2024