? module_implements.patch
? module_implements_12.patch
Index: includes/bootstrap.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/bootstrap.inc,v
retrieving revision 1.231
diff -u -p -r1.231 bootstrap.inc
--- includes/bootstrap.inc	11 Oct 2008 04:56:34 -0000	1.231
+++ includes/bootstrap.inc	11 Oct 2008 16:46:01 -0000
@@ -1438,31 +1438,6 @@ function registry_rebuild() {
 }
 
 /**
- * Save hook implementations cache.
- *
- * @param $hook
- *   Array with the hook name and list of modules that implement it.
- * @param $write_to_persistent_cache
- *   Whether to write to the persistent cache.
- */
-function registry_cache_hook_implementations($hook, $write_to_persistent_cache = FALSE) {
-  static $implementations;
-
-  if ($hook) {
-    // Newer is always better, so overwrite anything that's come before.
-    $implementations[$hook['hook']] = $hook['modules'];
-  }
-
-  if ($write_to_persistent_cache === TRUE) {
-    // Only write this to cache if the implementations data we are going to cache
-    // is different to what we loaded earlier in the request.
-    if ($implementations != module_implements()) {
-      cache_set('hooks', $implementations, 'cache_registry');
-    }
-  }
-}
-
-/**
  * Save the files required by the registry for this path.
  */
 function registry_cache_path_files() {
Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.803
diff -u -p -r1.803 common.inc
--- includes/common.inc	11 Oct 2008 02:32:32 -0000	1.803
+++ includes/common.inc	11 Oct 2008 16:46:01 -0000
@@ -1531,7 +1531,7 @@ function drupal_page_footer() {
 
   module_invoke_all('exit');
 
-  registry_cache_hook_implementations(FALSE, TRUE);
+  module_implements(MODULE_IMPLEMENTS_WRITE_CACHE);
   registry_cache_path_files();
 }
 
@@ -2523,8 +2523,6 @@ function _drupal_bootstrap_full() {
   fix_gpc_magic();
   // Load all enabled modules
   module_load_all();
-  // Rebuild the module hook cache
-  module_implements('', NULL, TRUE);
 
   // Let all modules take action before menu system handles the request
   // We do not want this while running update.php.
Index: includes/form.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/form.inc,v
retrieving revision 1.292
diff -u -p -r1.292 form.inc
--- includes/form.inc	11 Oct 2008 04:06:27 -0000	1.292
+++ includes/form.inc	11 Oct 2008 16:46:02 -0000
@@ -1361,7 +1361,7 @@ function _element_info($type, $refresh =
     }
   }
 
-  return $cache[$type];
+  return isset($cache[$type]) ? $cache[$type] : array();
 }
 
 function form_options_flatten($array, $reset = TRUE) {
Index: includes/menu.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/menu.inc,v
retrieving revision 1.293
diff -u -p -r1.293 menu.inc
--- includes/menu.inc	8 Oct 2008 01:42:16 -0000	1.293
+++ includes/menu.inc	11 Oct 2008 16:46:02 -0000
@@ -1732,7 +1732,7 @@ function menu_router_build($reset = FALS
       // We need to manually call each module so that we can know which module
       // a given item came from.
       $callbacks = array();
-      foreach (module_implements('menu', NULL, TRUE) as $module) {
+      foreach (module_implements('menu', TRUE) as $module) {
         $router_items = call_user_func($module . '_menu');
         if (isset($router_items) && is_array($router_items)) {
           foreach (array_keys($router_items) as $path) {
Index: includes/module.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/module.inc,v
retrieving revision 1.127
diff -u -p -r1.127 module.inc
--- includes/module.inc	27 Sep 2008 19:03:30 -0000	1.127
+++ includes/module.inc	11 Oct 2008 16:46:02 -0000
@@ -7,6 +7,17 @@
  */
 
 /**
+ * Pass this to module_implements when its cache needs to be written.
+ */
+define('MODULE_IMPLEMENTS_WRITE_CACHE', -1);
+
+/**
+ * Pass this to module_implements when its cache needs to be cleared.
+ */
+define('MODULE_IMPLEMENTS_CLEAR_CACHE', -2);
+
+
+/**
  * Load all the modules that have been enabled in the system table.
  */
 function module_load_all() {
@@ -381,45 +392,145 @@ function module_hook($module, $hook) {
  * Determine which modules are implementing a hook.
  *
  * @param $hook
- *   The name of the hook (e.g. "help" or "menu").
+ *   The name of the hook (e.g. "help" or "menu"). Special cases:
+ *     MODULE_IMPLEMENTS_CLEAR_CACHE: Force the stored list of hook
+ *     implementations to be regenerated (such as after enabling a new module,
+ *     before processing hook_enable).
+ *     MODULE_IMPLEMENTS_WRITE_CACHE: Write the stored list of hook
+ *     implementations into the cache_registry table.
  * @param $sort
- *   By default, modules are ordered by weight and filename, settings this option
- *   to TRUE, module list will be ordered by module name.
- * @param $refresh
- *   For internal use only: Whether to force the stored list of hook
- *   implementations to be regenerated (such as after enabling a new module,
- *   before processing hook_enable).  Note that if $refresh is TRUE this function
- *   will always return NULL.
+ *   By default, modules are ordered by weight and filename, settings this
+ *   option to TRUE, module list will be ordered by module name.
  * @return
  *   An array with the names of the modules which are implementing this hook.
- *   If $hook is NULL then it will return the implementation cache.
+ *   All enabled modules are taken into consideration and the files containing
+ *   the implementations are loaded as necessary.
  */
-function module_implements($hook = NULL, $sort = FALSE, $refresh = FALSE) {
-  static $implementations = array();
+function module_implements($hook, $sort = FALSE) {
+  static $implementations = array(), $loaded = array(), $cache;
 
-  if (!isset($hook)) {
-    return $implementations;
+  if (defined('MAINTENANCE_MODE')) {
+    return _module_implements_maintenance($hook);
   }
-  if ($refresh) {
+  if ($hook == MODULE_IMPLEMENTS_CLEAR_CACHE) {
     $implementations = array();
+    $loaded = array();
+    cache_clear_all('hooks', 'cache_registry');
+    return;
   }
-  if (!defined('MAINTENANCE_MODE') && empty($implementations) && ($cache = cache_get('hooks', 'cache_registry'))) {
+  if ($hook == MODULE_IMPLEMENTS_WRITE_CACHE) {
+    // Only write this to cache if the implementations data we are going to cache
+    // is different to what we loaded earlier in the request or if there was no
+    // cache yet.
+    if (!$cache || $implementations != $cache->data) {
+      cache_set('hooks', $implementations, 'cache_registry');
+    }
+    return;
+  }
+
+  if (empty($implementations) && ($cache = cache_get('hooks', 'cache_registry'))) {
     $implementations = $cache->data;
   }
 
-  if ($hook) {
-    if (!isset($implementations[$hook])) {
-      $implementations[$hook] = array();
-      foreach (module_list() as $module) {
-        if (module_hook($module, $hook)) {
-          $implementations[$hook][] = $module;
-        }
-      }
+  if (empty($loaded[$hook])) {
+    if (isset($implementations[$hook])) {
+      _module_implements_check($implementations, $hook);
+    }
+    else {
+      $implementations[$hook] = _module_implements_build($hook);
+    }
+    $loaded[$hook] = TRUE;
+  }
+
+  return $sort ? _module_implements_sort($implementations, $hook) : $implementations[$hook];
+}
+
+/**
+ * This is the maintenance version of module_implements.
+ *
+ * @param $hook
+ *   The name of the hook (e.g. "help" or "menu").
+ * @return
+ *   An array with the names of the modules which are implementing this hook.
+ *   Only enabled and already loaded modules are taken into consideration.
+ */
+function _module_implements_maintenance($hook) {
+  $implementations = array();
+  foreach (module_list() as $module) {
+    $function = $module . '_' . $hook;
+    if (function_exists($function)) {
+      $implementations[] = $module;
+    }
+  }
+  return $implementations;
+}
+
+/**
+ * Checks whether the functions implementing a hook are in memory.
+ *
+ * @param $implementations
+ *   All hook implementations, as stored in {cache_registry} and returned by
+ *   _module_implements_build().
+ * @param $hook
+ *   The name of the hook.
+ *
+ * @see module_implements()
+ */
+function _module_implements_check($implementations, $hook) {
+  foreach ($implementations[$hook] as $module) {
+    $function = $module . '_' . $hook;
+    // Though drupal_function_exists itself checks function_exists,
+    // most of the time the function will exist because of the per router path
+    // hook cache so we can save lots of calls to drupal_function_exists.
+    if (!function_exists($function)) {
+      drupal_function_exists($function);
     }
-    registry_cache_hook_implementations(array('hook' => $hook, 'modules' => $implementations[$hook]));
+  }
+}
 
-    return $implementations[$hook];
+/**
+ * Collects the implementations of a hook from the registry table.
+ *
+ * @param $hook
+ *   The name of the hook.
+ * @return
+ *   An array with the names of the modules which are implementing this hook.
+ *   All enabled modules are taken into consideration and the files containing
+ *   the implementations are loaded as necessary.
+ */
+function _module_implements_build($hook) {
+  $return = array();
+  $result = db_query("SELECT name, filename, module FROM {registry} WHERE type = 'function' AND hook = '%s'", $hook);
+  while ($function = db_fetch_object($result)) {
+    $return[] = $function->module;
+    // We need to load the relevant file for this function.
+    drupal_function_exists($function->name);
+  }
+  return $return;
+}
+
+/**
+ * Return hook implementations, sorted by module name.
+ *
+ * @param $implementations
+ *   All hook implementations, as stored in {cache_registry} and returned by
+ *   _module_implements_build().
+ * @param $hook
+ *   The name of the hook (e.g. "help" or "menu").
+ * @return
+ *   A sorted array with the names of the modules which are implementing this
+ *   hook.
+ *
+ * @see module_implements()
+ */
+function _module_implements_sort($implementations, $hook) {
+  static $sorted = array(), $cache = array();
+  if (!isset($cache[$hook]) || $cache[$hook] != $implementations[$hook]) {
+    $cache[$hook] = $implementations[$hook];
+    $sorted[$hook] = $implementations[$hook];
+    sort($sorted[$hook]);
   }
+  return $sorted[$hook];
 }
 
 /**
Index: includes/registry.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/registry.inc,v
retrieving revision 1.5
diff -u -p -r1.5 registry.inc
--- includes/registry.inc	20 Sep 2008 20:22:23 -0000	1.5
+++ includes/registry.inc	11 Oct 2008 16:46:02 -0000
@@ -45,7 +45,7 @@ function _registry_rebuild() {
     if ($module->status) {
       $dir = dirname($module->filename);
       foreach ($module->info['files'] as $file) {
-        $files["$dir/$file"] = array();
+        $files["$dir/$file"] = array('module' => $module->name);
       }
     }
   }
@@ -67,6 +67,7 @@ function _registry_rebuild() {
   }
   _registry_parse_files($files);
 
+  module_implements(MODULE_IMPLEMENTS_CLEAR_CACHE);
   cache_clear_all('*', 'cache_registry', TRUE);
 }
 
@@ -87,7 +88,6 @@ function registry_get_parsed_files() {
  *  The list of files to check and parse.
  */
 function _registry_parse_files($files) {
-  $changed_files = array();
   foreach ($files as $filename => $file) {
     $contents = file_get_contents($filename);
     $md5 = md5($contents);
@@ -95,8 +95,7 @@ function _registry_parse_files($files) {
     if ($new_file || $md5 != $file['md5']) {
       // We update the md5 after we've saved the files resources rather than here, so if we
       // don't make it through this rebuild, the next run will reparse the file.
-      _registry_parse_file($filename, $contents);
-      $file['md5'] = $md5;
+      _registry_parse_file($filename, $contents, isset($file['module']) ? $file['module'] : '');
       db_merge('registry_file')
         ->key(array('filename' => $filename))
         ->fields(array('md5' => $md5))
@@ -109,11 +108,13 @@ function _registry_parse_files($files) {
  * Parse a file and save its function and class listings.
  *
  * @param $filename
- *  Name of the file we are going to parse.
+ *   Name of the file we are going to parse.
  * @param $contents
- *  Contents of the file we are going to parse as a string.
+ *   Contents of the file we are going to parse as a string.
+ * @param $module
+ *   Name of the module (optional) this file belongs to.
  */
-function _registry_parse_file($filename, $contents) {
+function _registry_parse_file($filename, $contents, $module = '') {
   static $map = array(T_FUNCTION => 'function', T_CLASS => 'class', T_INTERFACE => 'interface');
   // Delete registry entries for this file, so we can insert the new resources.
   db_delete('registry')->condition('filename', $filename)->execute();
@@ -123,6 +124,18 @@ function _registry_parse_file($filename,
     if (is_array($token) && isset($map[$token[0]])) {
       $type = $map[$token[0]];
       if ($resource_name = _registry_get_resource_name($tokens, $type)) {
+        $hook = '';
+        if ($type == 'function' && !empty($module)) {
+          $n = strlen($module);
+          if (substr($resource_name, 0, $n) == $module) {
+            $hook = substr($resource_name, $n + 1);
+          }
+        }
+        $fields = array(
+          'filename' => $filename,
+          'module' => $module,
+          'hook' => $hook,
+        );
         // Because some systems, such as cache, currently use duplicate function
         // names in separate files an insert query cannot be used here as it
         // would cause a key constraint violation.  Instead we use a merge query.
@@ -132,7 +145,7 @@ function _registry_parse_file($filename,
         // filename instead of another.
         // TODO: Convert this back to an insert query after all duplicate
         // function names have been purged from Drupal.
-        db_merge('registry')->key(array('name' => $resource_name, 'type' => $type))->fields(array('filename' => $filename))->execute();
+        db_merge('registry')->key(array('name' => $resource_name, 'type' => $type))->fields($fields)->execute();
 
         // We skip the body because classes may contain functions.
         _registry_skip_body($tokens);
Index: modules/system/system.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.install,v
retrieving revision 1.269
diff -u -p -r1.269 system.install
--- modules/system/system.install	27 Sep 2008 20:16:17 -0000	1.269
+++ modules/system/system.install	11 Oct 2008 16:46:03 -0000
@@ -1091,8 +1091,25 @@ function system_schema() {
         'length' => 255,
         'not null' => TRUE,
       ),
+      'module' => array(
+        'description' => t('Name of the module the file belongs to.'),
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+	'default' => ''
+      ),
+      'hook' => array(
+        'description' => t('Name of the hook this function implements, if any.'),
+        'type' => 'varchar',
+        'length' => 255,
+        'not null' => TRUE,
+	'default' => ''
+      ),
     ),
     'primary key' => array('name', 'type'),
+    'indexes' => array(
+      'hook' => array('hook'),
+    ),
   );
 
   $schema['registry_file'] = array(
@@ -2927,11 +2944,16 @@ function system_update_7006() {
   db_drop_field($ret, 'menu_router', 'file');
   $schema['registry'] = array(
     'fields' => array(
-      'name'   => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
-      'type'   => array('type' => 'varchar', 'length' => 9, 'not null' => TRUE, 'default' => ''),
-      'filename'   => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
+      'name'     => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
+      'type'     => array('type' => 'varchar', 'length' => 9,   'not null' => TRUE, 'default' => ''),
+      'filename' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
+      'module'   => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
+      'hook'     => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
     ),
     'primary key' => array('name', 'type'),
+    'indexes' => array(
+      'hook' => array('hook'),
+    ),
   );
   $schema['registry_file'] = array(
     'fields' => array(
