module.inc 2.69 KB
Newer Older
1 2 3 4 5
<?php
// $Id$

// initialize modules:
function module_init() {
6
  // Note: changing this also requires changing system_listing() @ modules/system.module.
Dries's avatar
 
Dries committed
7
  require_once "modules/admin.module";
8
  require_once "modules/filter.module";
Kjartan's avatar
Kjartan committed
9
  require_once "modules/system.module";
10
  require_once "modules/user.module";
Kjartan's avatar
Kjartan committed
11
  require_once "modules/watchdog.module";
12
  module_list();
Dries's avatar
 
Dries committed
13
  module_invoke_all("init");
14 15 16 17
}

// apply function $function to every known module:
function module_iterate($function, $argument = "") {
Kjartan's avatar
Kjartan committed
18 19 20
  foreach (module_list() as $name) {
    $function($name, $argument);
  }
21 22 23
}

// invoke hook $hook of module $name with optional arguments:
Dries's avatar
 
Dries committed
24 25
function module_invoke($name, $hook, $a1 = NULL, $a2 = NULL, $a3 = NULL, $a4 = NULL) {
  $function = $name ."_". $hook;
26
  if (function_exists($function)) {
Dries's avatar
 
Dries committed
27
    return $function($a1, $a2, $a3, $a4);
28 29 30
  }
}

Dries's avatar
 
Dries committed
31
// invoke $hook for all appropriate modules:
Dries's avatar
 
Dries committed
32
function module_invoke_all($hook, $a1 = NULL, $a2 = NULL, $a3 = NULL, $a4 = NULL) {
Dries's avatar
 
Dries committed
33 34
  $return = array();
  foreach (module_list() as $name) {
Dries's avatar
 
Dries committed
35 36 37
    $result = module_invoke($name, $hook, $a1, $a2, $a3, $a4);
    if (isset($result)) {
      $return = array_merge($return, $result);
Dries's avatar
 
Dries committed
38 39 40 41 42 43
    }
  }

  return $return;
}

Dries's avatar
 
Dries committed
44 45
// return array of module names (includes lazy module loading if not in bootstrap mode)
function module_list($refresh = 0, $bootstrap = 0) {
46 47
  static $list;

Dries's avatar
 
Dries committed
48 49 50 51
  if ($refresh) {
    unset($list);
  }

52
  if (!$list) {
53
    $list = array("admin" => "admin", "filter" => "filter", "system" => "system", "user" => "user", "watchdog" => "watchdog");
54 55 56 57 58 59
    if ($bootstrap) {
      $result = db_query("SELECT name, filename, throttle, bootstrap FROM {system} WHERE type = 'module' AND status = 1 AND bootstrap = 1");
    }
    else {
      $result = db_query("SELECT name, filename, throttle, bootstrap FROM {system} WHERE type = 'module' AND status = 1");
    }
60
    while ($module = db_fetch_object($result)) {
Dries's avatar
 
Dries committed
61
      if (file_exists($module->filename)) {
Dries's avatar
 
Dries committed
62 63 64 65 66
        /*
        ** Determine the current throttle status and see if module should be
        **  loaded based on server load.  We have to directly access the
        **  throttle variables as the throttle.module may not be loaded yet.
        */
Dries's avatar
 
Dries committed
67
        $throttle = ($module->throttle && variable_get("throttle_level", 0) > 4);
68
        if (!$throttle) {
Dries's avatar
 
Dries committed
69 70 71
          $list[$module->name] = $module->name;
          include_once $module->filename;
        }
72
      }
73
    }
Dries's avatar
 
Dries committed
74
    asort($list);
75 76 77 78 79 80 81
  }
  return $list;
}

// return 1 if module $name exists, 0 otherwise:
function module_exist($name) {
  $list = module_list();
Dries's avatar
 
Dries committed
82
  return isset($list[$name]) ? 1 : 0;
83 84 85 86 87 88 89
}

// return 1 if module $name implements hook $hook, 0 otherwise:
function module_hook($name, $hook) {
  return function_exists($name ."_". $hook);
}

Dries's avatar
 
Dries committed
90
?>