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

  ViewVC Help
Powered by ViewVC 1.1.2