| 1 | | <?php |
| 2 | | // $Id: module.inc,v 1.132 2008/10/31 02:18:22 dries Exp $ |
| 3 | | |
| 4 | | /** |
| 5 | | * @file |
| 6 | | * API for loading and interacting with Drupal modules. |
| 7 | | */ |
| 8 | | |
| 9 | | /** |
| 10 | | * Pass this to module_implements when its cache needs to be written. |
| 11 | | */ |
| 12 | 2367 | define('MODULE_IMPLEMENTS_WRITE_CACHE', -1); |
| 13 | | |
| 14 | | /** |
| 15 | | * Pass this to module_implements when its cache needs to be cleared. |
| 16 | | */ |
| 17 | 2367 | define('MODULE_IMPLEMENTS_CLEAR_CACHE', -2); |
| 18 | | |
| 19 | | |
| 20 | | /** |
| 21 | | * Load all the modules that have been enabled in the system table. |
| 22 | | */ |
| 23 | 2367 | function module_load_all() { |
| 24 | 2366 | foreach (module_list(TRUE, FALSE) as $module) { |
| 25 | 2366 | drupal_load('module', $module); |
| 26 | 2366 | } |
| 27 | 2366 | } |
| 28 | | |
| 29 | | /** |
| 30 | | * Collect a list of all loaded modules. During the bootstrap, return only |
| 31 | | * vital modules. See bootstrap.inc |
| 32 | | * |
| 33 | | * @param $refresh |
| 34 | | * Whether to force the module list to be regenerated (such as after the |
| 35 | | * administrator has changed the system settings). |
| 36 | | * @param $bootstrap |
| 37 | | * Whether to return the reduced set of modules loaded in "bootstrap
mode" |
| 38 | | * for cached pages. See bootstrap.inc. |
| 39 | | * @param $sort |
| 40 | | * By default, modules are ordered by weight and filename. Set this
option to |
| 41 | | * TRUE to return a module list ordered only by module name. |
| 42 | | * @param $fixed_list |
| 43 | | * (Optional) Override the module list with the given modules. Stays
until the |
| 44 | | * next call with $refresh = TRUE. |
| 45 | | * @return |
| 46 | | * An associative array whose keys and values are the names of all loaded |
| 47 | | * modules. |
| 48 | | */ |
| 49 | 2367 | function module_list($refresh = FALSE, $bootstrap = TRUE, $sort = FALSE,
$fixed_list = NULL) { |
| 50 | 2497 | static $list = array(), $sorted_list; |
| 51 | | |
| 52 | 2497 | if (empty($list) || $refresh || $fixed_list) { |
| 53 | 2497 | unset($sorted_list); |
| 54 | 2497 | $list = array(); |
| 55 | 2497 | if ($fixed_list) { |
| 56 | 0 | foreach ($fixed_list as $name => $module) { |
| 57 | 0 | drupal_get_filename('module', $name, $module['filename']); |
| 58 | 0 | $list[$name] = $name; |
| 59 | 0 | } |
| 60 | 0 | } |
| 61 | | else { |
| 62 | 2497 | if ($bootstrap) { |
| 63 | 2497 | $result = db_query("SELECT name, filename FROM {system} WHERE type
= 'module' AND status = 1 AND bootstrap = 1 ORDER BY weight ASC, filename
ASC"); |
| 64 | 2497 | } |
| 65 | | else { |
| 66 | 2496 | $result = db_query("SELECT name, filename FROM {system} WHERE type
= 'module' AND status = 1 ORDER BY weight ASC, filename ASC"); |
| 67 | | } |
| 68 | 2497 | while ($module = db_fetch_object($result)) { |
| 69 | 2496 | if (file_exists($module->filename)) { |
| 70 | 2496 | drupal_get_filename('module', $module->name, $module->filename); |
| 71 | 2496 | $list[$module->name] = $module->name; |
| 72 | 2496 | } |
| 73 | 2496 | } |
| 74 | | } |
| 75 | 2497 | } |
| 76 | 2497 | if ($sort) { |
| 77 | 30 | if (!isset($sorted_list)) { |
| 78 | 30 | $sorted_list = $list; |
| 79 | 30 | ksort($sorted_list); |
| 80 | 30 | } |
| 81 | 30 | return $sorted_list; |
| 82 | 0 | } |
| 83 | 2497 | return $list; |
| 84 | 0 | } |
| 85 | | |
| 86 | | /** |
| 87 | | * Rebuild the database cache of module files. |
| 88 | | * |
| 89 | | * @return |
| 90 | | * The array of filesystem objects used to rebuild the cache. |
| 91 | | */ |
| 92 | 2367 | function module_rebuild_cache() { |
| 93 | | // Get current list of modules |
| 94 | 154 | $files = drupal_system_listing('/\.module$/', 'modules', 'name', 0); |
| 95 | | |
| 96 | | // Extract current files from database. |
| 97 | 154 | system_get_files_database($files, 'module'); |
| 98 | | |
| 99 | 154 | ksort($files); |
| 100 | | |
| 101 | | // Set defaults for module info |
| 102 | | $defaults = array( |
| 103 | 154 | 'dependencies' => array(), |
| 104 | 154 | 'dependents' => array(), |
| 105 | 154 | 'description' => '', |
| 106 | 154 | 'package' => 'Other', |
| 107 | 154 | 'version' => NULL, |
| 108 | 154 | 'php' => DRUPAL_MINIMUM_PHP, |
| 109 | 154 | 'files' => array(), |
| 110 | 154 | ); |
| 111 | | |
| 112 | 154 | foreach ($files as $filename => $file) { |
| 113 | | // Look for the info file. |
| 114 | 154 | $file->info = drupal_parse_info_file(dirname($file->filename) . '/' .
$file->name . '.info'); |
| 115 | | |
| 116 | | // Skip modules that don't provide info. |
| 117 | 154 | if (empty($file->info)) { |
| 118 | 0 | unset($files[$filename]); |
| 119 | 0 | continue; |
| 120 | 0 | } |
| 121 | | // Merge in defaults and save. |
| 122 | 154 | $files[$filename]->info = $file->info + $defaults; |
| 123 | | |
| 124 | | // Invoke hook_system_info_alter() to give installed modules a chance
to |
| 125 | | // modify the data in the .info files if necessary. |
| 126 | 154 | drupal_alter('system_info', $files[$filename]->info,
$files[$filename]); |
| 127 | | |
| 128 | | // Log the critical hooks implemented by this module. |
| 129 | 154 | $bootstrap = 0; |
| 130 | 154 | foreach (bootstrap_hooks() as $hook) { |
| 131 | 154 | if (module_hook($file->name, $hook)) { |
| 132 | 0 | $bootstrap = 1; |
| 133 | 0 | break; |
| 134 | 0 | } |
| 135 | 154 | } |
| 136 | | |
| 137 | | // Update the contents of the system table: |
| 138 | 154 | if (isset($file->status) || (isset($file->old_filename) &&
$file->old_filename != $file->filename)) { |
| 139 | 154 | db_query("UPDATE {system} SET info = '%s', name = '%s', filename =
'%s', bootstrap = %d WHERE filename = '%s'",
serialize($files[$filename]->info), $file->name, $file->filename,
$bootstrap, $file->old_filename); |
| 140 | 154 | } |
| 141 | | else { |
| 142 | | // This is a new module. |
| 143 | 134 | $files[$filename]->status = 0; |
| 144 | 134 | db_query("INSERT INTO {system} (name, info, type, filename, status,
bootstrap) VALUES ('%s', '%s', '%s', '%s', %d, %d)", $file->name,
serialize($files[$filename]->info), 'module', $file->filename, 0,
$bootstrap); |
| 145 | | } |
| 146 | 154 | } |
| 147 | 154 | $files = _module_build_dependencies($files); |
| 148 | 154 | return $files; |
| 149 | 0 | } |
| 150 | | |
| 151 | | /** |
| 152 | | * Find dependencies any level deep and fill in dependents information too. |
| 153 | | * |
| 154 | | * If module A depends on B which in turn depends on C then this function
will |
| 155 | | * add C to the list of modules A depends on. This will be repeated until |
| 156 | | * module A has a list of all modules it depends on. If it depends on
itself, |
| 157 | | * called a circular dependency, that's marked by adding a nonexistent
module, |
| 158 | | * called -circular- to this list of modules. Because this does not exist, |
| 159 | | * it'll be impossible to switch module A on. |
| 160 | | * |
| 161 | | * Also we fill in a dependents array in $file->info. Using the names
above, |
| 162 | | * the dependents array of module B lists A. |
| 163 | | * |
| 164 | | * @param $files |
| 165 | | * The array of filesystem objects used to rebuild the cache. |
| 166 | | * @return |
| 167 | | * The same array with dependencies and dependents added where
applicable. |
| 168 | | */ |
| 169 | 2367 | function _module_build_dependencies($files) { |
| 170 | | do { |
| 171 | 154 | $new_dependency = FALSE; |
| 172 | 154 | foreach ($files as $filename => $file) { |
| 173 | | // We will modify this object (module A, see doxygen for module A, B,
C). |
| 174 | 154 | $file = &$files[$filename]; |
| 175 | 154 | if (isset($file->info['dependencies']) &&
is_array($file->info['dependencies'])) { |
| 176 | 154 | foreach ($file->info['dependencies'] as $dependency_name) { |
| 177 | | // This is a nonexistent module. |
| 178 | 154 | if ($dependency_name == '-circular-' ||
!isset($files[$dependency_name])) { |
| 179 | 0 | continue; |
| 180 | 0 | } |
| 181 | | // $dependency_name is module B (again, see doxygen). |
| 182 | 154 | $files[$dependency_name]->info['dependents'][$filename] =
$filename; |
| 183 | 154 | $dependency = $files[$dependency_name]; |
| 184 | 154 | if (isset($dependency->info['dependencies']) &&
is_array($dependency->info['dependencies'])) { |
| 185 | | // Let's find possible C modules. |
| 186 | 154 | foreach ($dependency->info['dependencies'] as $candidate) { |
| 187 | 0 | if (array_search($candidate, $file->info['dependencies']) ===
FALSE) { |
| 188 | | // Is this a circular dependency? |
| 189 | 0 | if ($candidate == $filename) { |
| 190 | | // As a module name can not contain dashes, this makes |
| 191 | | // impossible to switch on the module. |
| 192 | 0 | $candidate = '-circular-'; |
| 193 | | // Do not display the message or add -circular- more than
once. |
| 194 | 0 | if (array_search($candidate, $file->info['dependencies'])
!== FALSE) { |
| 195 | 0 | continue; |
| 196 | 0 | } |
| 197 | 0 | drupal_set_message(t('%module is part of a circular
dependency. This is not supported and you will not be able to switch it
on.', array('%module' => $file->info['name'])), 'error'); |
| 198 | 0 | } |
| 199 | | else { |
| 200 | | // We added a new dependency to module A. The next loop
will |
| 201 | | // be able to use this as "B module" thus finding even |
| 202 | | // deeper dependencies. |
| 203 | 0 | $new_dependency = TRUE; |
| 204 | | } |
| 205 | 0 | $file->info['dependencies'][] = $candidate; |
| 206 | 0 | } |
| 207 | 0 | } |
| 208 | 154 | } |
| 209 | 154 | } |
| 210 | 154 | } |
| 211 | | // Don't forget to break the reference. |
| 212 | 154 | unset($file); |
| 213 | 154 | } |
| 214 | 0 | } while ($new_dependency); |
| 215 | 154 | return $files; |
| 216 | 0 | } |
| 217 | | |
| 218 | | /** |
| 219 | | * Determine whether a given module exists. |
| 220 | | * |
| 221 | | * @param $module |
| 222 | | * The name of the module (without the .module extension). |
| 223 | | * @return |
| 224 | | * TRUE if the module is both installed and enabled. |
| 225 | | */ |
| 226 | 2367 | function module_exists($module) { |
| 227 | 1792 | $list = module_list(); |
| 228 | 1792 | return isset($list[$module]); |
| 229 | 0 | } |
| 230 | | |
| 231 | | /** |
| 232 | | * Load a module's installation hooks. |
| 233 | | */ |
| 234 | 2367 | function module_load_install($module) { |
| 235 | | // Make sure the installation API is available |
| 236 | 143 | include_once DRUPAL_ROOT . '/includes/install.inc'; |
| 237 | | |
| 238 | 143 | module_load_include('install', $module); |
| 239 | 143 | } |
| 240 | | |
| 241 | | /** |
| 242 | | * Load a module include file. |
| 243 | | * |
| 244 | | * @param $type |
| 245 | | * The include file's type (file extension). |
| 246 | | * @param $module |
| 247 | | * The module to which the include file belongs. |
| 248 | | * @param $name |
| 249 | | * Optionally, specify the file name. If not set, the module's name is
used. |
| 250 | | */ |
| 251 | 2367 | function module_load_include($type, $module, $name = NULL) { |
| 252 | 146 | if (empty($name)) { |
| 253 | 143 | $name = $module; |
| 254 | 143 | } |
| 255 | | |
| 256 | 146 | if (drupal_function_exists('drupal_get_path')) { |
| 257 | 146 | $file = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) .
"/$name.$type"; |
| 258 | 146 | if (is_file($file)) { |
| 259 | 146 | require_once $file; |
| 260 | 146 | return $file; |
| 261 | 0 | } |
| 262 | 138 | } |
| 263 | 138 | return FALSE; |
| 264 | 0 | } |
| 265 | | |
| 266 | | /** |
| 267 | | * Load an include file for each of the modules that have been enabled in |
| 268 | | * the system table. |
| 269 | | */ |
| 270 | 2367 | function module_load_all_includes($type, $name = NULL) { |
| 271 | 134 | $modules = module_list(); |
| 272 | 134 | foreach ($modules as $module) { |
| 273 | 134 | module_load_include($type, $module, $name); |
| 274 | 134 | } |
| 275 | 134 | } |
| 276 | | |
| 277 | | /** |
| 278 | | * Enable a given list of modules. |
| 279 | | * |
| 280 | | * @param $module_list |
| 281 | | * An array of module names. |
| 282 | | */ |
| 283 | 2367 | function module_enable($module_list) { |
| 284 | 136 | $invoke_modules = array(); |
| 285 | 136 | foreach ($module_list as $module) { |
| 286 | 136 | $existing = db_fetch_object(db_query("SELECT status FROM {system} WHERE
type = '%s' AND name = '%s'", 'module', $module)); |
| 287 | 136 | if ($existing->status == 0) { |
| 288 | 136 | module_load_install($module); |
| 289 | 136 | db_query("UPDATE {system} SET status = %d WHERE type = '%s' AND name
= '%s'", 1, 'module', $module); |
| 290 | 136 | drupal_load('module', $module); |
| 291 | 136 | $invoke_modules[] = $module; |
| 292 | 136 | } |
| 293 | 136 | } |
| 294 | | |
| 295 | 136 | if (!empty($invoke_modules)) { |
| 296 | | // Refresh the module list to include the new enabled module. |
| 297 | 136 | module_list(TRUE, FALSE); |
| 298 | | // Force to regenerate the stored list of hook implementations. |
| 299 | 136 | registry_rebuild(); |
| 300 | 136 | } |
| 301 | | |
| 302 | 136 | foreach ($invoke_modules as $module) { |
| 303 | 136 | module_invoke($module, 'enable'); |
| 304 | | // Check if node_access table needs rebuilding. |
| 305 | | // We check for the existence of node_access_needs_rebuild() since |
| 306 | | // at install time, module_enable() could be called while node.module |
| 307 | | // is not enabled yet. |
| 308 | 136 | if (drupal_function_exists('node_access_needs_rebuild') &&
!node_access_needs_rebuild() && module_hook($module, 'node_grants')) { |
| 309 | 0 | node_access_needs_rebuild(TRUE); |
| 310 | 0 | } |
| 311 | 136 | } |
| 312 | | |
| 313 | 136 | if (!empty($invoke_modules)) { |
| 314 | | // Invoke the hook_module_enable after all the modules have been |
| 315 | | // enabled. |
| 316 | 136 | module_invoke_all('modules_enabled', $invoke_modules); |
| 317 | 136 | } |
| 318 | 136 | } |
| 319 | | |
| 320 | | /** |
| 321 | | * Disable a given set of modules. |
| 322 | | * |
| 323 | | * @param $module_list |
| 324 | | * An array of module names. |
| 325 | | */ |
| 326 | 2367 | function module_disable($module_list) { |
| 327 | 1 | $invoke_modules = array(); |
| 328 | 1 | foreach ($module_list as $module) { |
| 329 | 1 | if (module_exists($module)) { |
| 330 | | // Check if node_access table needs rebuilding. |
| 331 | 1 | if (!node_access_needs_rebuild() && module_hook($module,
'node_grants')) { |
| 332 | 0 | node_access_needs_rebuild(TRUE); |
| 333 | 0 | } |
| 334 | | |
| 335 | 1 | module_load_install($module); |
| 336 | 1 | module_invoke($module, 'disable'); |
| 337 | 1 | db_query("UPDATE {system} SET status = %d WHERE type = '%s' AND name
= '%s'", 0, 'module', $module); |
| 338 | 1 | $invoke_modules[] = $module; |
| 339 | 1 | } |
| 340 | 1 | } |
| 341 | | |
| 342 | 1 | if (!empty($invoke_modules)) { |
| 343 | | // Invoke hook_module_disable before disabling modules, |
| 344 | | // so we can still call module hooks to get information. |
| 345 | 1 | module_invoke_all('modules_disabled', $invoke_modules); |
| 346 | | // Refresh the module list to exclude the disabled modules. |
| 347 | 1 | module_list(TRUE, FALSE); |
| 348 | | // Force to regenerate the stored list of hook implementations. |
| 349 | 1 | registry_rebuild(); |
| 350 | 1 | } |
| 351 | | |
| 352 | | // If there remains no more node_access module, rebuilding will be |
| 353 | | // straightforward, we can do it right now. |
| 354 | 1 | if (node_access_needs_rebuild() &&
count(module_implements('node_grants')) == 0) { |
| 355 | 0 | node_access_rebuild(); |
| 356 | 0 | } |
| 357 | 1 | } |
| 358 | | |
| 359 | | /** |
| 360 | | * @defgroup hooks Hooks |
| 361 | | * @{ |
| 362 | | * Allow modules to interact with the Drupal core. |
| 363 | | * |
| 364 | | * Drupal's module system is based on the concept of "hooks". A hook is a
PHP |
| 365 | | * function that is named foo_bar(), where "foo" is the name of the module
(whose |
| 366 | | * filename is thus foo.module) and "bar" is the name of the hook. Each
hook has |
| 367 | | * a defined set of parameters and a specified result type. |
| 368 | | * |
| 369 | | * To extend Drupal, a module need simply implement a hook. When Drupal
wishes to |
| 370 | | * allow intervention from modules, it determines which modules implement a
hook |
| 371 | | * and call that hook in all enabled modules that implement it. |
| 372 | | * |
| 373 | | * The available hooks to implement are explained here in the Hooks section
of |
| 374 | | * the developer documentation. The string "hook" is used as a placeholder
for |
| 375 | | * the module name is the hook definitions. For example, if the module file
is |
| 376 | | * called example.module, then hook_help() as implemented by that module
would be |
| 377 | | * defined as example_help(). |
| 378 | | */ |
| 379 | | |
| 380 | | /** |
| 381 | | * Determine whether a module implements a hook. |
| 382 | | * |
| 383 | | * @param $module |
| 384 | | * The name of the module (without the .module extension). |
| 385 | | * @param $hook |
| 386 | | * The name of the hook (e.g. "help" or "menu"). |
| 387 | | * @return |
| 388 | | * TRUE if the module is both installed and enabled, and the hook is |
| 389 | | * implemented in that module. |
| 390 | | */ |
| 391 | 2367 | function module_hook($module, $hook) { |
| 392 | 2482 | $function = $module . '_' . $hook; |
| 393 | 2482 | if (defined('MAINTENANCE_MODE')) { |
| 394 | 0 | return function_exists($function); |
| 395 | 0 | } |
| 396 | | else { |
| 397 | 2482 | return drupal_function_exists($function); |
| 398 | | } |
| 399 | 0 | } |
| 400 | | |
| 401 | | /** |
| 402 | | * Determine which modules are implementing a hook. |
| 403 | | * |
| 404 | | * @param $hook |
| 405 | | * The name of the hook (e.g. "help" or "menu"). Special cases: |
| 406 | | * MODULE_IMPLEMENTS_CLEAR_CACHE: Force the stored list of hook |
| 407 | | * implementations to be regenerated (such as after enabling a new
module, |
| 408 | | * before processing hook_enable). |
| 409 | | * MODULE_IMPLEMENTS_WRITE_CACHE: Write the stored list of hook |
| 410 | | * implementations into the cache_registry table. |
| 411 | | * @param $sort |
| 412 | | * By default, modules are ordered by weight and filename. By setting
this |
| 413 | | * option to TRUE, modules will be ordered by module name. |
| 414 | | * @return |
| 415 | | * An array with the names of the modules which are implementing this
hook. |
| 416 | | * All enabled modules are taken into consideration and the files
containing |
| 417 | | * the implementations are loaded as necessary. |
| 418 | | */ |
| 419 | 2367 | function module_implements($hook, $sort = FALSE) { |
| 420 | 2496 | static $implementations = array(), $sorted_implementations = array(),
$loaded = array(), $cached_hooks = 0; |
| 421 | | |
| 422 | 2496 | if (defined('MAINTENANCE_MODE')) { |
| 423 | 0 | return _module_implements_maintenance($hook, $sort); |
| 424 | 0 | } |
| 425 | 2496 | if ($hook === MODULE_IMPLEMENTS_CLEAR_CACHE) { |
| 426 | 146 | $implementations = array(); |
| 427 | 146 | $sorted_implementations = array(); |
| 428 | 146 | $loaded = array(); |
| 429 | 146 | $cached_hooks = 0; |
| 430 | 146 | cache_clear_all('hooks', 'cache_registry'); |
| 431 | 146 | return; |
| 432 | 0 | } |
| 433 | 2496 | if ($hook === MODULE_IMPLEMENTS_WRITE_CACHE) { |
| 434 | | // Only write this to cache if we loaded new implementations. |
| 435 | 1756 | if (count($implementations) > $cached_hooks) { |
| 436 | 551 | cache_set('hooks', $implementations, 'cache_registry'); |
| 437 | 551 | } |
| 438 | 1756 | return; |
| 439 | 0 | } |
| 440 | | |
| 441 | 2496 | if (!isset($loaded[$hook])) { |
| 442 | 2496 | if (empty($implementations) && ($cache = cache_get('hooks',
'cache_registry'))) { |
| 443 | 2252 | $implementations = $cache->data; |
| 444 | 2252 | $cached_hooks = count($implementations); |
| 445 | 2252 | } |
| 446 | 2496 | if (!isset($implementations[$hook])) { |
| 447 | 1634 | $implementations[$hook] = db_query("SELECT module FROM {registry}
WHERE type = 'function' AND suffix = :hook ORDER BY weight, module",
array(':hook' => $hook))->fetchCol(); |
| 448 | 1634 | } |
| 449 | 2496 | foreach ($implementations[$hook] as $module) { |
| 450 | 2496 | $function = $module . '_' . $hook; |
| 451 | 2496 | if (!function_exists($function)) { |
| 452 | 6 | drupal_function_exists($function); |
| 453 | 6 | } |
| 454 | 2496 | } |
| 455 | 2496 | $loaded[$hook] = TRUE; |
| 456 | 2496 | } |
| 457 | | |
| 458 | 2496 | if ($sort) { |
| 459 | 718 | if (!isset($sorted_implementations[$hook])) { |
| 460 | 718 | $sorted_implementations[$hook] = $implementations[$hook]; |
| 461 | 718 | sort($sorted_implementations[$hook]); |
| 462 | 718 | } |
| 463 | 718 | return $sorted_implementations[$hook]; |
| 464 | 0 | } |
| 465 | | else { |
| 466 | 2496 | return $implementations[$hook]; |
| 467 | | } |
| 468 | 0 | } |
| 469 | | |
| 470 | | /** |
| 471 | | * This is the maintenance version of module_implements for internal use
only. |
| 472 | | * |
| 473 | | * This function is called whenever MAINTENANCE_MODE is defined and is a |
| 474 | | * safe code path for Drupal installation or upgrade because it does not
use |
| 475 | | * the database, instead it uses module_list. @see module_list $fixed_list
on |
| 476 | | * how to make module_list also DB independent. |
| 477 | | * |
| 478 | | * @param $hook |
| 479 | | * The name of the hook (e.g. "help" or "menu"). |
| 480 | | * @param $sort |
| 481 | | * By default, modules are ordered by weight and filename, settings this |
| 482 | | * option to TRUE, module list will be ordered by module name. |
| 483 | | * @return |
| 484 | | * An array with the names of the modules which are implementing this
hook. |
| 485 | | * Only enabled and already loaded modules are taken into consideration. |
| 486 | | */ |
| 487 | 2367 | function _module_implements_maintenance($hook, $sort = FALSE) { |
| 488 | 0 | $implementations = array(); |
| 489 | 0 | foreach (module_list() as $module) { |
| 490 | 0 | $function = $module . '_' . $hook; |
| 491 | 0 | if (function_exists($function)) { |
| 492 | 0 | $implementations[] = $module; |
| 493 | 0 | } |
| 494 | 0 | if ($sort) { |
| 495 | 0 | sort($implementations); |
| 496 | 0 | } |
| 497 | 0 | } |
| 498 | 0 | return $implementations; |
| 499 | 0 | } |
| 500 | | |
| 501 | | /** |
| 502 | | * Invoke a hook in a particular module. |
| 503 | | * |
| 504 | | * @param $module |
| 505 | | * The name of the module (without the .module extension). |
| 506 | | * @param $hook |
| 507 | | * The name of the hook to invoke. |
| 508 | | * @param ... |
| 509 | | * Arguments to pass to the hook implementation. |
| 510 | | * @return |
| 511 | | * The return value of the hook implementation. |
| 512 | | */ |
| 513 | 2367 | function module_invoke() { |
| 514 | 2482 | $args = func_get_args(); |
| 515 | 2482 | $module = $args[0]; |
| 516 | 2482 | $hook = $args[1]; |
| 517 | 2482 | unset($args[0], $args[1]); |
| 518 | 2482 | if (module_hook($module, $hook)) { |
| 519 | 2482 | return call_user_func_array($module . '_' . $hook, $args); |
| 520 | 0 | } |
| 521 | 311 | } |
| 522 | | /** |
| 523 | | * Invoke a hook in all enabled modules that implement it. |
| 524 | | * |
| 525 | | * @param $hook |
| 526 | | * The name of the hook to invoke. |
| 527 | | * @param ... |
| 528 | | * Arguments to pass to the hook. |
| 529 | | * @return |
| 530 | | * An array of return values of the hook implementations. If modules
return |
| 531 | | * arrays from their implementations, those are merged into one array. |
| 532 | | */ |
| 533 | 2367 | function module_invoke_all() { |
| 534 | 2496 | $args = func_get_args(); |
| 535 | 2496 | $hook = $args[0]; |
| 536 | 2496 | unset($args[0]); |
| 537 | 2496 | $return = array(); |
| 538 | 2496 | foreach (module_implements($hook) as $module) { |
| 539 | 2496 | $function = $module . '_' . $hook; |
| 540 | 2496 | if (drupal_function_exists($function)) { |
| 541 | 2496 | $result = call_user_func_array($function, $args); |
| 542 | 2496 | if (isset($result) && is_array($result)) { |
| 543 | 859 | $return = array_merge_recursive($return, $result); |
| 544 | 859 | } |
| 545 | 2496 | elseif (isset($result)) { |
| 546 | 0 | $return[] = $result; |
| 547 | 0 | } |
| 548 | 2496 | } |
| 549 | 2496 | } |
| 550 | | |
| 551 | 2496 | return $return; |
| 552 | 0 | } |
| 553 | | |
| 554 | | /** |
| 555 | | * @} End of "defgroup hooks". |
| 556 | | */ |
| 557 | | |
| 558 | | /** |
| 559 | | * Array of modules required by core. |
| 560 | | */ |
| 561 | 2367 | function drupal_required_modules() { |
| 562 | 134 | $files = drupal_system_listing('/\.info$/', 'modules', 'name', 0); |
| 563 | 134 | $required = array(); |
| 564 | 134 | foreach ($files as $name => $file) { |
| 565 | 134 | $info = drupal_parse_info_file($file->filename); |
| 566 | 134 | if (!empty($info) && !empty($info['required']) && $info['required']) { |
| 567 | 134 | $required[] = $name; |
| 568 | 134 | } |
| 569 | 134 | } |
| 570 | 134 | return $required; |
| 571 | 0 | } |
| 572 | 2367 | |