/[drupal]/drupal/includes/module.inc
ViewVC logotype

Contents of /drupal/includes/module.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.164 - (show annotations) (download) (as text)
Sun Nov 1 22:10:07 2009 UTC (3 weeks, 6 days ago) by webchick
Branch: MAIN
Changes since 1.163: +18 -4 lines
File MIME type: text/x-php
#592008 follow-up by chx and moshe weitzman: All theme() function calls in hook_init().
1 <?php
2 // $Id: module.inc,v 1.163 2009/10/30 21:54:45 dries Exp $
3
4 /**
5 * @file
6 * API for loading and interacting with Drupal modules.
7 */
8
9 /**
10 * Load all the modules that have been enabled in the system table.
11 *
12 * @param $bootstrap
13 * Whether to load only the reduced set of modules loaded in "bootstrap mode"
14 * for cached pages. See bootstrap.inc.
15 * @return
16 * If $bootstrap is NULL, return a boolean indicating whether all modules
17 * have been loaded.
18 */
19 function module_load_all($bootstrap = FALSE) {
20 static $has_run = FALSE;
21
22 if (isset($bootstrap)) {
23 foreach (module_list(TRUE, $bootstrap) as $module) {
24 drupal_load('module', $module);
25 }
26 // $has_run will be TRUE if $bootstrap is FALSE.
27 $has_run = !$bootstrap;
28 }
29 return $has_run;
30 }
31
32
33 /**
34 * Collect a list of all loaded modules. During the bootstrap, return only
35 * vital modules. See bootstrap.inc
36 *
37 * @param $refresh
38 * Whether to force the module list to be regenerated (such as after the
39 * administrator has changed the system settings).
40 * @param $bootstrap
41 * Whether to return the reduced set of modules loaded in "bootstrap mode"
42 * for cached pages. See bootstrap.inc.
43 * @param $sort
44 * By default, modules are ordered by weight and module name. Set this option
45 * to TRUE to return a module list ordered only by module name.
46 * @param $fixed_list
47 * (Optional) Override the module list with the given modules. Stays until the
48 * next call with $refresh = TRUE.
49 * @return
50 * An associative array whose keys and values are the names of all loaded
51 * modules.
52 */
53 function module_list($refresh = FALSE, $bootstrap = FALSE, $sort = FALSE, $fixed_list = NULL) {
54 static $list = array(), $sorted_list;
55
56 if (empty($list) || $refresh || $fixed_list) {
57 $list = array();
58 $sorted_list = NULL;
59 if ($fixed_list) {
60 foreach ($fixed_list as $name => $module) {
61 drupal_get_filename('module', $name, $module['filename']);
62 $list[$name] = $name;
63 }
64 }
65 else {
66 // The module name (rather than the filename) is used as the fallback
67 // weighting in order to guarantee consistent behavior across different
68 // Drupal installations, which might have modules installed in different
69 // locations in the file system. The ordering here must also be
70 // consistent with the one used in module_implements().
71 if ($bootstrap) {
72 $result = db_query("SELECT name, filename FROM {system} WHERE type = 'module' AND status = 1 AND bootstrap = 1 ORDER BY weight ASC, name ASC");
73 }
74 else {
75 $result = db_query("SELECT name, filename FROM {system} WHERE type = 'module' AND status = 1 ORDER BY weight ASC, name ASC");
76 }
77 foreach ($result as $module) {
78 if (file_exists($module->filename)) {
79 // First call drupal_get_filename() to prime the static cache for
80 // later lookups of the module path. Since we've already queried for
81 // the filename and can pass that in as an argument, this avoids a
82 // database hit for every module when drupal_get_filename() is
83 // subsequently called by drupal_load().
84 drupal_get_filename('module', $module->name, $module->filename);
85 $list[$module->name] = $module->name;
86 }
87 }
88 }
89 }
90 if ($sort) {
91 if (!isset($sorted_list)) {
92 $sorted_list = $list;
93 ksort($sorted_list);
94 }
95 return $sorted_list;
96 }
97 return $list;
98 }
99
100 /**
101 * Find dependencies any level deep and fill in required by information too.
102 *
103 * @param $files
104 * The array of filesystem objects used to rebuild the cache.
105 * @return
106 * The same array with the new keys for each module:
107 * - requires: An array with the keys being the modules that this module
108 * requires.
109 * - required_by: An array with the keys being the modules that will not work
110 * without this module.
111 */
112 function _module_build_dependencies($files) {
113 require_once DRUPAL_ROOT . '/includes/graph.inc';
114 $roots = $files;
115 foreach ($files as $filename => $file) {
116 $graph[$file->name]['edges'] = array();
117 if (isset($file->info['dependencies']) && is_array($file->info['dependencies'])) {
118 foreach ($file->info['dependencies'] as $dependency) {
119 $dependency_data = drupal_parse_dependency($dependency);
120 $graph[$file->name]['edges'][$dependency_data['name']] = $dependency_data;
121 unset($roots[$dependency_data['name']]);
122 }
123 }
124 }
125 drupal_depth_first_search($graph, array_keys($roots));
126 foreach ($graph as $module => $data) {
127 $files[$module]->required_by = isset($data['reverse_paths']) ? $data['reverse_paths'] : array();
128 $files[$module]->requires = isset($data['paths']) ? $data['paths'] : array();
129 $files[$module]->sort = $data['weight'];
130 }
131 return $files;
132 }
133
134 /**
135 * Determine whether a given module exists.
136 *
137 * @param $module
138 * The name of the module (without the .module extension).
139 * @return
140 * TRUE if the module is both installed and enabled.
141 */
142 function module_exists($module) {
143 $list = module_list();
144 return isset($list[$module]);
145 }
146
147 /**
148 * Load a module's installation hooks.
149 */
150 function module_load_install($module) {
151 // Make sure the installation API is available
152 include_once DRUPAL_ROOT . '/includes/install.inc';
153
154 module_load_include('install', $module);
155 }
156
157 /**
158 * Load a module include file.
159 *
160 * Examples:
161 * @code
162 * // Load node.admin.inc from the node module.
163 * module_load_include('inc', 'node', 'node.admin');
164 * // Load content_types.inc from the node module.
165 * module_load_include('inc', 'node', 'content_types');
166 * @endcode
167 *
168 * Do not use this function to load an install file. Use module_load_install()
169 * instead.
170 *
171 * @param $type
172 * The include file's type (file extension).
173 * @param $module
174 * The module to which the include file belongs.
175 * @param $name
176 * Optionally, specify the base file name (without the $type extension).
177 * If not set, $module is used.
178 */
179 function module_load_include($type, $module, $name = NULL) {
180 if (empty($name)) {
181 $name = $module;
182 }
183
184 if (function_exists('drupal_get_path')) {
185 $file = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . "/$name.$type";
186 if (is_file($file)) {
187 require_once $file;
188 return $file;
189 }
190 }
191 return FALSE;
192 }
193
194 /**
195 * Load an include file for each of the modules that have been enabled in
196 * the system table.
197 */
198 function module_load_all_includes($type, $name = NULL) {
199 $modules = module_list();
200 foreach ($modules as $module) {
201 module_load_include($type, $module, $name);
202 }
203 }
204
205 /**
206 * Enable a given list of modules.
207 *
208 * @param $module_list
209 * An array of module names.
210 * @param $disable_modules_installed_hook
211 * Normally just testing wants to set this to TRUE.
212 */
213 function module_enable($module_list, $disable_modules_installed_hook = FALSE) {
214 $invoke_modules = array();
215
216 // Try to install the enabled modules and collect which were installed.
217 // $module_list is not changed and already installed modules are ignored.
218 $modules_installed = array_filter($module_list, '_drupal_install_module');
219 foreach ($module_list as $module) {
220 $existing = db_query("SELECT status FROM {system} WHERE type = :type AND name = :name", array(
221 ':type' => 'module',
222 ':name' => $module))
223 ->fetchObject();
224 if ($existing->status == 0) {
225 module_load_install($module);
226 db_update('system')
227 ->fields(array('status' => 1))
228 ->condition('type', 'module')
229 ->condition('name', $module)
230 ->execute();
231 drupal_load('module', $module);
232 $invoke_modules[] = $module;
233 watchdog('system', '%module module enabled.', array('%module' => $module), WATCHDOG_INFO);
234 }
235 }
236
237 if (!empty($invoke_modules)) {
238 // Refresh the module list to exclude the disabled modules.
239 module_list(TRUE);
240 module_implements('', FALSE, TRUE);
241 // Force to regenerate the stored list of hook implementations.
242 registry_rebuild();
243 // Refresh the schema to include the new enabled module.
244 drupal_get_schema(NULL, TRUE);
245
246 // If any modules were newly installed, execute the hook for them.
247 if (!$disable_modules_installed_hook && !empty($modules_installed)) {
248 module_invoke_all('modules_installed', $modules_installed);
249 }
250 }
251
252 foreach ($invoke_modules as $module) {
253 module_invoke($module, 'enable');
254 // Check if node_access table needs rebuilding.
255 // We check for the existence of node_access_needs_rebuild() since
256 // at install time, module_enable() could be called while node.module
257 // is not enabled yet.
258 if (function_exists('node_access_needs_rebuild') && !node_access_needs_rebuild() && module_hook($module, 'node_grants')) {
259 node_access_needs_rebuild(TRUE);
260 }
261 }
262
263 if (!empty($invoke_modules)) {
264 // Invoke hook_modules_enabled after all the modules have been
265 // enabled.
266 module_invoke_all('modules_enabled', $invoke_modules);
267 }
268 }
269
270 /**
271 * Disable a given set of modules.
272 *
273 * @param $module_list
274 * An array of module names.
275 */
276 function module_disable($module_list) {
277 $invoke_modules = array();
278 foreach ($module_list as $module) {
279 if (module_exists($module)) {
280 // Check if node_access table needs rebuilding.
281 if (!node_access_needs_rebuild() && module_hook($module, 'node_grants')) {
282 node_access_needs_rebuild(TRUE);
283 }
284
285 module_load_install($module);
286 module_invoke($module, 'disable');
287 db_update('system')
288 ->fields(array('status' => 0))
289 ->condition('type', 'module')
290 ->condition('name', $module)
291 ->execute();
292 $invoke_modules[] = $module;
293 watchdog('system', '%module module disabled.', array('%module' => $module), WATCHDOG_INFO);
294 }
295 }
296
297 if (!empty($invoke_modules)) {
298 // Refresh the module list to exclude the disabled modules.
299 module_list(TRUE);
300 module_implements('', FALSE, TRUE);
301 // Invoke hook_modules_disabled before disabling modules,
302 // so we can still call module hooks to get information.
303 module_invoke_all('modules_disabled', $invoke_modules);
304 // Force to regenerate the stored list of hook implementations.
305 registry_rebuild();
306 }
307
308 // If there remains no more node_access module, rebuilding will be
309 // straightforward, we can do it right now.
310 if (node_access_needs_rebuild() && count(module_implements('node_grants')) == 0) {
311 node_access_rebuild();
312 }
313 }
314
315 /**
316 * @defgroup hooks Hooks
317 * @{
318 * Allow modules to interact with the Drupal core.
319 *
320 * Drupal's module system is based on the concept of "hooks". A hook is a PHP
321 * function that is named foo_bar(), where "foo" is the name of the module (whose
322 * filename is thus foo.module) and "bar" is the name of the hook. Each hook has
323 * a defined set of parameters and a specified result type.
324 *
325 * To extend Drupal, a module need simply implement a hook. When Drupal wishes to
326 * allow intervention from modules, it determines which modules implement a hook
327 * and call that hook in all enabled modules that implement it.
328 *
329 * The available hooks to implement are explained here in the Hooks section of
330 * the developer documentation. The string "hook" is used as a placeholder for
331 * the module name is the hook definitions. For example, if the module file is
332 * called example.module, then hook_help() as implemented by that module would be
333 * defined as example_help().
334 */
335
336 /**
337 * Determine whether a module implements a hook.
338 *
339 * @param $module
340 * The name of the module (without the .module extension).
341 * @param $hook
342 * The name of the hook (e.g. "help" or "menu").
343 * @return
344 * TRUE if the module is both installed and enabled, and the hook is
345 * implemented in that module.
346 */
347 function module_hook($module, $hook) {
348 return function_exists($module . '_' . $hook);
349 }
350
351 /**
352 * Determine which modules are implementing a hook.
353 *
354 * @param $hook
355 * The name of the hook (e.g. "help" or "menu").
356 * @param $sort
357 * By default, modules are ordered by weight and filename, settings this option
358 * to TRUE, module list will be ordered by module name.
359 * @param $reset
360 * For internal use only: Whether to force the stored list of hook
361 * implementations to be regenerated (such as after enabling a new module,
362 * before processing hook_enable).
363 *
364 * @return
365 * An array with the names of the modules which are implementing this hook.
366 *
367 * @see module_implements_write_cache().
368 */
369 function module_implements($hook, $sort = FALSE, $reset = FALSE) {
370 $implementations = &drupal_static(__FUNCTION__, array());
371
372 // We maintain a persistent cache of hook implementations in addition to the
373 // static cache to avoid looping through every module and every hook on each
374 // request. Benchmarks show that the benefit of this caching outweighs the
375 // additional database hit even when using the default database caching
376 // backend and only a small number of modules are enabled. The cost of the
377 // cache_get() is more or less constant and reduced further when non-database
378 // caching backends are used, so there will be more significant gains when a
379 // large number of modules are installed or hooks invoked, since this can
380 // quickly lead to module_hook() being called several thousand times
381 // per request.
382 if ($reset) {
383 $implementations = array();
384 cache_set('module_implements', array());
385 drupal_static_reset('module_hook_info');
386 cache_clear_all('hook_info', 'cache');
387 return;
388 }
389
390 // Fetch implementations from cache.
391 if (empty($implementations)) {
392 $implementations = cache_get('module_implements');
393 if ($implementations === FALSE) {
394 $implementations = array();
395 }
396 else {
397 $implementations = $implementations->data;
398 }
399 }
400
401 if (!isset($implementations[$hook])) {
402 $hook_info = module_hook_info();
403 $implementations[$hook] = array();
404 $list = module_list(FALSE, FALSE, $sort);
405 foreach ($list as $module) {
406 $include_file = FALSE;
407 if (module_hook($module, $hook) || (isset($hook_info[$hook]['group']) && $include_file = module_load_include('inc', $module, $module . '.' . $hook_info[$hook]['group']) && module_hook($module, $hook))) {
408 $implementations[$hook][$module] = $include_file ? $hook_info[$hook]['group'] : FALSE;
409 // We added something to the cache, so write it when we are done.
410 $implementations['#write_cache'] = TRUE;
411 }
412 }
413 }
414 else {
415 foreach ($implementations[$hook] as $module => $group) {
416 // If this hook implementation is stored in a lazy-loaded file, so include
417 // that file first.
418 if ($group) {
419 module_load_include('inc', $module, "$module.$group");
420 }
421 // It is possible that a module removed a hook implementation without the
422 // implementations cache being rebuilt yet, so we check module_hook() on
423 // each request to avoid undefined function errors.
424 if (!module_hook($module, $hook)) {
425 // Clear out the stale implementation from the cache and force a cache
426 // refresh to forget about no longer existing hook implementations.
427 unset($implementations[$hook][$module]);
428 $implementations['#write_cache'] = TRUE;
429 }
430 }
431 }
432
433 return array_keys($implementations[$hook]);
434 }
435
436 /**
437 * Retrieve a list of what hooks are explicitly declared.
438 */
439 function module_hook_info() {
440 $hook_info = &drupal_static(__FUNCTION__, array());
441
442 if (empty($hook_info)) {
443 $cache = cache_get('hook_info');
444 if ($cache === FALSE) {
445 // Rebuild the cache and save it.
446 // We can't use module_invoke_all() here or it would cause an infinite
447 // loop.
448 foreach (module_list() as $module) {
449 $function = $module . '_hook_info';
450 if (function_exists($function)) {
451 $result = $function();
452 if (isset($result) && is_array($result)) {
453 $hook_info = array_merge_recursive($hook_info, $result);
454 }
455 }
456 }
457 // We can't use drupal_alter() for the same reason as above.
458 foreach (module_list() as $module) {
459 $function = $module . '_hook_info_alter';
460 if (function_exists($function)) {
461 $function($hook_info);
462 }
463 }
464 cache_set('hook_info', $hook_info);
465 }
466 else {
467 $hook_info = $cache->data;
468 }
469 }
470
471 return $hook_info;
472 }
473
474 /**
475 * Writes the hook implementation cache.
476 *
477 * @see module_implements()
478 */
479 function module_implements_write_cache() {
480 $implementations = &drupal_static('module_implements');
481 // Check whether we need to write the cache. We do not want to cache hooks
482 // which are only invoked on HTTP POST requests since these do not need to be
483 // optimized as tightly, and not doing so keeps the cache entry smaller.
484 if (isset($implementations['#write_cache']) && ($_SERVER['REQUEST_METHOD'] == 'GET' || $_SERVER['REQUEST_METHOD'] == 'HEAD')) {
485 unset($implementations['#write_cache']);
486 cache_set('module_implements', $implementations);
487 }
488 }
489
490 /**
491 * Invoke a hook in a particular module.
492 *
493 * @param $module
494 * The name of the module (without the .module extension).
495 * @param $hook
496 * The name of the hook to invoke.
497 * @param ...
498 * Arguments to pass to the hook implementation.
499 * @return
500 * The return value of the hook implementation.
501 */
502 function module_invoke() {
503 $args = func_get_args();
504 $module = $args[0];
505 $hook = $args[1];
506 unset($args[0], $args[1]);
507 if (module_hook($module, $hook)) {
508 return call_user_func_array($module . '_' . $hook, $args);
509 }
510 }
511 /**
512 * Invoke a hook in all enabled modules that implement it.
513 *
514 * @param $hook
515 * The name of the hook to invoke.
516 * @param ...
517 * Arguments to pass to the hook.
518 * @return
519 * An array of return values of the hook implementations. If modules return
520 * arrays from their implementations, those are merged into one array.
521 */
522 function module_invoke_all() {
523 $args = func_get_args();
524 $hook = $args[0];
525 unset($args[0]);
526 $return = array();
527 foreach (module_implements($hook) as $module) {
528 $function = $module . '_' . $hook;
529 if (function_exists($function)) {
530 $result = call_user_func_array($function, $args);
531 if (isset($result) && is_array($result)) {
532 $return = array_merge_recursive($return, $result);
533 }
534 elseif (isset($result)) {
535 $return[] = $result;
536 }
537 }
538 }
539
540 return $return;
541 }
542
543 /**
544 * @} End of "defgroup hooks".
545 */
546
547 /**
548 * Array of modules required by core.
549 */
550 function drupal_required_modules() {
551 $files = drupal_system_listing('/\.info$/', 'modules', 'name', 0);
552 $required = array();
553
554 // An install profile is required and one must always be loaded.
555 $required[] = drupal_get_profile();
556
557 foreach ($files as $name => $file) {
558 $info = drupal_parse_info_file($file->uri);
559 if (!empty($info) && !empty($info['required']) && $info['required']) {
560 $required[] = $name;
561 }
562 }
563
564 return $required;
565 }

  ViewVC Help
Powered by ViewVC 1.1.2