Code coverage for /20080809/includes/module.inc

Line #Times calledCode
1
<?php
2
// $Id: module.inc,v 1.122 2008/08/02 19:01:02 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
 */
122027
function module_load_all() {
132027
  foreach (module_list(TRUE, FALSE) as $module) {
142027
    drupal_load('module', $module);
152027
  }
162027
}
17
18
/**
19
 * Collect a list of all loaded modules. During the bootstrap, return only
20
 * vital modules. See bootstrap.inc
21
 *
22
 * @param $refresh
23
 *   Whether to force the module list to be regenerated (such as after the
24
 *   administrator has changed the system settings).
25
 * @param $bootstrap
26
 *   Whether to return the reduced set of modules loaded in "bootstrap
mode"
27
 *   for cached pages. See bootstrap.inc.
28
 * @param $sort
29
 *   By default, modules are ordered by weight and filename, settings this
option
30
 *   to TRUE, module list will be ordered by module name.
31
 * @param $fixed_list
32
 *   (Optional) Override the module list with the given modules. Stays
until the
33
 *   next call with $refresh = TRUE.
34
 * @return
35
 *   An associative array whose keys and values are the names of all
loaded
36
 *   modules.
37
 */
382027
function module_list($refresh = FALSE, $bootstrap = TRUE, $sort = FALSE,
$fixed_list = NULL) {
392087
  static $list, $sorted_list;
40
412087
  if ($refresh || $fixed_list) {
422087
    unset($sorted_list);
432087
    $list = array();
442087
    if ($fixed_list) {
450
      foreach ($fixed_list as $name => $module) {
460
        drupal_get_filename('module', $name, $module['filename']);
470
        $list[$name] = $name;
480
      }
490
    }
50
    else {
512087
      if ($bootstrap) {
522087
        $result = db_query("SELECT name, filename FROM {system} WHERE type
= 'module' AND status = 1 AND bootstrap = 1 ORDER BY weight ASC, filename
ASC");
532087
      }
54
      else {
552087
        $result = db_query("SELECT name, filename FROM {system} WHERE type
= 'module' AND status = 1 ORDER BY weight ASC, filename ASC");
56
      }
572087
      while ($module = db_fetch_object($result)) {
582087
        if (file_exists($module->filename)) {
592087
          drupal_get_filename('module', $module->name, $module->filename);
602087
          $list[$module->name] = $module->name;
612087
        }
622087
      }
63
    }
642087
  }
652087
  if ($sort) {
6624
    if (!isset($sorted_list)) {
6724
      $sorted_list = $list;
6824
      ksort($sorted_list);
6924
    }
7024
    return $sorted_list;
710
  }
722087
  return $list;
730
}
74
75
/**
76
 * Rebuild the database cache of module files.
77
 *
78
 * @return
79
 *   The array of filesystem objects used to rebuild the cache.
80
 */
812027
function module_rebuild_cache() {
82
  // Get current list of modules
8376
  $files = drupal_system_listing('\.module$', 'modules', 'name', 0);
84
85
  // Extract current files from database.
8676
  system_get_files_database($files, 'module');
87
8876
  ksort($files);
89
90
  // Set defaults for module info
91
  $defaults = array(
9276
    'dependencies' => array(),
9376
    'dependents' => array(),
9476
    'description' => '',
9576
    'version' => NULL,
9676
    'php' => DRUPAL_MINIMUM_PHP,
9776
  );
98
9976
  foreach ($files as $filename => $file) {
100
    // Look for the info file.
10176
    $file->info = drupal_parse_info_file(dirname($file->filename) . '/' .
$file->name . '.info');
102
103
    // Skip modules that don't provide info.
10476
    if (empty($file->info)) {
1050
      unset($files[$filename]);
1060
      continue;
1070
    }
108
    // Merge in defaults and save.
10976
    $files[$filename]->info = $file->info + $defaults;
110
111
    // Invoke hook_system_info_alter() to give installed modules a chance
to
112
    // modify the data in the .info files if necessary.
11376
    drupal_alter('system_info', $files[$filename]->info,
$files[$filename]);
114
115
    // Log the critical hooks implemented by this module.
11676
    $bootstrap = 0;
11776
    foreach (bootstrap_hooks() as $hook) {
11876
      if (module_hook($file->name, $hook)) {
1190
        $bootstrap = 1;
1200
        break;
1210
      }
12276
    }
123
124
    // Update the contents of the system table:
12576
    if (isset($file->status) || (isset($file->old_filename) &&
$file->old_filename != $file->filename)) {
12676
      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);
12776
    }
128
    else {
129
      // This is a new module.
13062
      $files[$filename]->status = 0;
13162
      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);
132
    }
13376
  }
13476
  $files = _module_build_dependencies($files);
13576
  return $files;
1360
}
137
138
/**
139
 * Find dependencies any level deep and fill in dependents information
too.
140
 *
141
 * If module A depends on B which in turn depends on C then this function
will
142
 * add C to the list of modules A depends on. This will be repeated until
143
 * module A has a list of all modules it depends on. If it depends on
itself,
144
 * called a circular dependency, that's marked by adding a nonexistent
module,
145
 * called -circular- to this list of modules. Because this does not exist,
146
 * it'll be impossible to switch module A on.
147
 *
148
 * Also we fill in a dependents array in $file->info. Using the names
above,
149
 * the dependents array of module B lists A.
150
 *
151
 * @param $files
152
 *   The array of filesystem objects used to rebuild the cache.
153
 * @return
154
 *   The same array with dependencies and dependents added where
applicable.
155
 */
1562027
function _module_build_dependencies($files) {
157
  do {
15876
    $new_dependency = FALSE;
15976
    foreach ($files as $filename => $file) {
160
      // We will modify this object (module A, see doxygen for module A, B,
C).
16176
      $file = &$files[$filename];
16276
      if (isset($file->info['dependencies']) &&
is_array($file->info['dependencies'])) {
16376
        foreach ($file->info['dependencies'] as $dependency_name) {
164
          // This is a nonexistent module.
16576
          if ($dependency_name == '-circular-' ||
!isset($files[$dependency_name])) {
1660
            continue;
1670
          }
168
          // $dependency_name is module B (again, see doxygen).
16976
          $files[$dependency_name]->info['dependents'][$filename] =
$filename;
17076
          $dependency = $files[$dependency_name];
17176
          if (isset($dependency->info['dependencies']) &&
is_array($dependency->info['dependencies'])) {
172
            // Let's find possible C modules.
17376
            foreach ($dependency->info['dependencies'] as $candidate) {
1740
              if (array_search($candidate, $file->info['dependencies']) ===
FALSE) {
175
                // Is this a circular dependency?
1760
                if ($candidate == $filename) {
177
                  // As a module name can not contain dashes, this makes
178
                  // impossible to switch on the module.
1790
                  $candidate = '-circular-';
180
                  // Do not display the message or add -circular- more than
once.
1810
                  if (array_search($candidate, $file->info['dependencies'])
!== FALSE) {
1820
                    continue;
1830
                  }
1840
                  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');
1850
                }
186
                else {
187
                  // We added a new dependency to module A. The next loop
will
188
                  // be able to use this as "B module" thus finding even
189
                  // deeper dependencies.
1900
                  $new_dependency = TRUE;
191
                }
1920
                $file->info['dependencies'][] = $candidate;
1930
              }
1940
            }
19576
          }
19676
        }
19776
      }
198
      // Don't forget to break the reference.
19976
      unset($file);
20076
    }
2010
  } while ($new_dependency);
20276
  return $files;
2030
}
204
205
/**
206
 * Determine whether a given module exists.
207
 *
208
 * @param $module
209
 *   The name of the module (without the .module extension).
210
 * @return
211
 *   TRUE if the module is both installed and enabled.
212
 */
2132027
function module_exists($module) {
2141531
  $list = module_list();
2151531
  return isset($list[$module]);
2160
}
217
218
/**
219
 * Load a module's installation hooks.
220
 */
2212027
function module_load_install($module) {
222
  // Make sure the installation API is available
22386
  include_once './includes/install.inc';
224
22586
  module_load_include('install', $module);
22686
}
227
228
/**
229
 * Load a module include file.
230
 *
231
 * @param $type
232
 *   The include file's type (file extension).
233
 * @param $module
234
 *   The module to which the include file belongs.
235
 * @param $name
236
 *   Optionally, specify the file name. If not set, the module's name is
used.
237
 */
2382027
function module_load_include($type, $module, $name = NULL) {
23989
  if (empty($name)) {
24086
    $name = $module;
24186
  }
242
24389
  $file = './' . drupal_get_path('module', $module) . "/$name.$type";
244
24589
  if (is_file($file)) {
24689
    require_once $file;
24789
    return $file;
2480
  }
249
  else {
25081
    return FALSE;
251
  }
2520
}
253
254
/**
255
 * Load an include file for each of the modules that have been enabled in
256
 * the system table.
257
 */
2582027
function module_load_all_includes($type, $name = NULL) {
25977
  $modules = module_list();
26077
  foreach ($modules as $module) {
26177
    module_load_include($type, $module, $name);
26277
  }
26377
}
264
265
/**
266
 * Enable a given list of modules.
267
 *
268
 * @param $module_list
269
 *   An array of module names.
270
 */
2712027
function module_enable($module_list) {
27265
  $invoke_modules = array();
27365
  foreach ($module_list as $module) {
27465
    $existing = db_fetch_object(db_query("SELECT status FROM {system} WHERE
type = '%s' AND name = '%s'", 'module', $module));
27565
    if ($existing->status == 0) {
27664
      module_load_install($module);
27764
      db_query("UPDATE {system} SET status = %d WHERE type = '%s' AND name
= '%s'", 1, 'module', $module);
27864
      drupal_load('module', $module);
27964
      $invoke_modules[] = $module;
28064
    }
28165
  }
282
28365
  if (!empty($invoke_modules)) {
284
    // Refresh the module list to include the new enabled module.
28564
    module_list(TRUE, FALSE);
286
    // Force to regenerate the stored list of hook implementations.
28764
    registry_rebuild();
28864
  }
289
29065
  foreach ($invoke_modules as $module) {
29164
    module_invoke($module, 'enable');
292
    // Check if node_access table needs rebuilding.
293
    // We check for the existence of node_access_needs_rebuild() since
294
    // at install time, module_enable() could be called while node.module
295
    // is not enabled yet.
29664
    if (drupal_function_exists('node_access_needs_rebuild') &&
!node_access_needs_rebuild() && module_hook($module, 'node_grants')) {
2970
      node_access_needs_rebuild(TRUE);
2980
    }
29964
  }
30065
}
301
302
/**
303
 * Disable a given set of modules.
304
 *
305
 * @param $module_list
306
 *   An array of module names.
307
 */
3082027
function module_disable($module_list) {
3093
  $invoke_modules = array();
3103
  foreach ($module_list as $module) {
3113
    if (module_exists($module)) {
312
      // Check if node_access table needs rebuilding.
3131
      if (!node_access_needs_rebuild() && module_hook($module,
'node_grants')) {
3140
        node_access_needs_rebuild(TRUE);
3150
      }
316
3171
      module_load_install($module);
3181
      module_invoke($module, 'disable');
3191
      db_query("UPDATE {system} SET status = %d WHERE type = '%s' AND name
= '%s'", 0, 'module', $module);
3201
      $invoke_modules[] = $module;
3211
    }
3223
  }
323
3243
  if (!empty($invoke_modules)) {
325
    // Refresh the module list to exclude the disabled modules.
3261
    module_list(TRUE, FALSE);
327
    // Force to regenerate the stored list of hook implementations.
3281
    registry_rebuild();
3291
  }
330
331
  // If there remains no more node_access module, rebuilding will be
332
  // straightforward, we can do it right now.
3333
  if (node_access_needs_rebuild() &&
count(module_implements('node_grants')) == 0) {
3340
    node_access_rebuild();
3350
  }
3363
}
337
338
/**
339
 * @defgroup hooks Hooks
340
 * @{
341
 * Allow modules to interact with the Drupal core.
342
 *
343
 * Drupal's module system is based on the concept of "hooks". A hook is a
PHP
344
 * function that is named foo_bar(), where "foo" is the name of the module
(whose
345
 * filename is thus foo.module) and "bar" is the name of the hook. Each
hook has
346
 * a defined set of parameters and a specified result type.
347
 *
348
 * To extend Drupal, a module need simply implement a hook. When Drupal
wishes to
349
 * allow intervention from modules, it determines which modules implement a
hook
350
 * and call that hook in all enabled modules that implement it.
351
 *
352
 * The available hooks to implement are explained here in the Hooks section
of
353
 * the developer documentation. The string "hook" is used as a placeholder
for
354
 * the module name is the hook definitions. For example, if the module file
is
355
 * called example.module, then hook_help() as implemented by that module
would be
356
 * defined as example_help().
357
 */
358
359
/**
360
 * Determine whether a module implements a hook.
361
 *
362
 * @param $module
363
 *   The name of the module (without the .module extension).
364
 * @param $hook
365
 *   The name of the hook (e.g. "help" or "menu").
366
 * @return
367
 *   TRUE if the module is both installed and enabled, and the hook is
368
 *   implemented in that module.
369
 */
3702027
function module_hook($module, $hook) {
3712086
  $function = $module . '_' . $hook;
3722086
  if (defined('MAINTENANCE_MODE')) {
3730
    return function_exists($function);
3740
  }
375
  else {
3762086
    return drupal_function_exists($function);
377
  }
3780
}
379
380
/**
381
 * Determine which modules are implementing a hook.
382
 *
383
 * @param $hook
384
 *   The name of the hook (e.g. "help" or "menu").
385
 * @param $sort
386
 *   By default, modules are ordered by weight and filename, settings this
option
387
 *   to TRUE, module list will be ordered by module name.
388
 * @param $refresh
389
 *   For internal use only: Whether to force the stored list of hook
390
 *   implementations to be regenerated (such as after enabling a new
module,
391
 *   before processing hook_enable).
392
 * @return
393
 *   An array with the names of the modules which are implementing this
hook.
394
 */
3952027
function module_implements($hook, $sort = FALSE, $refresh = FALSE) {
3962087
  static $implementations = array();
397
3982087
  if ($refresh) {
39982
    $implementations = array();
40082
  }
4012087
  else if (!defined('MAINTENANCE_MODE') && empty($implementations)) {
4022027
    $implementations = registry_get_hook_implementations_cache();
4032027
  }
404
4052087
  if (!isset($implementations[$hook])) {
4061512
    $implementations[$hook] = array();
4071512
    foreach (module_list() as $module) {
4081512
      if (module_hook($module, $hook)) {
4091021
        $implementations[$hook][] = $module;
4101021
      }
4111512
    }
4121512
  }
4132087
  registry_cache_hook_implementations(array('hook' => $hook, 'modules' =>
$implementations[$hook]));
414
415
  // The explicit cast forces a copy to be made. This is needed because
416
  // $implementations[$hook] is only a reference to an element of
417
  // $implementations and if there are nested foreaches (due to nested
node
418
  // API calls, for example), they would both manipulate the same array's
419
  // references, which causes some modules' hooks not to be called.
420
  // See also http://www.zend.com/zend/art/ref-count.php.
4212087
  return (array)$implementations[$hook];
4220
}
423
424
/**
425
 * Invoke a hook in a particular module.
426
 *
427
 * @param $module
428
 *   The name of the module (without the .module extension).
429
 * @param $hook
430
 *   The name of the hook to invoke.
431
 * @param ...
432
 *   Arguments to pass to the hook implementation.
433
 * @return
434
 *   The return value of the hook implementation.
435
 */
4362027
function module_invoke() {
4372086
  $args = func_get_args();
4382086
  $module = $args[0];
4392086
  $hook = $args[1];
4402086
  unset($args[0], $args[1]);
4412086
  if (module_hook($module, $hook)) {
4422086
    return call_user_func_array($module . '_' . $hook, $args);
4430
  }
444271
}
445
/**
446
 * Invoke a hook in all enabled modules that implement it.
447
 *
448
 * @param $hook
449
 *   The name of the hook to invoke.
450
 * @param ...
451
 *   Arguments to pass to the hook.
452
 * @return
453
 *   An array of return values of the hook implementations. If modules
return
454
 *   arrays from their implementations, those are merged into one array.
455
 */
4562027
function module_invoke_all() {
4572087
  $args = func_get_args();
4582087
  $hook = $args[0];
4592087
  unset($args[0]);
4602087
  $return = array();
4612087
  foreach (module_implements($hook) as $module) {
4622087
    $function = $module . '_' . $hook;
4632087
    if (drupal_function_exists($function)) {
4642087
      $result = call_user_func_array($function, $args);
4652087
      if (isset($result) && is_array($result)) {
466734
        $return = array_merge_recursive($return, $result);
467734
      }
4682087
      else if (isset($result)) {
4690
        $return[] = $result;
4700
      }
4712087
    }
4722087
  }
473
4742087
  return $return;
4750
}
476
477
/**
478
 * @} End of "defgroup hooks".
479
 */
480
481
/**
482
 * Array of modules required by core.
483
 */
4842027
function drupal_required_modules() {
48571
  return array('block', 'filter', 'node', 'system', 'user');
4860
}
4872027