module.inc 2.75 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");
Dries's avatar
   
Dries committed
54
    $result = db_query("SELECT name, filename, throttle, bootstrap FROM {system} WHERE type = 'module' AND status = '1' ORDER BY name");
55
    while ($module = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
56
      if (file_exists($module->filename)) {
Dries's avatar
   
Dries committed
57
58
59
60
61
62
63
        /*
        ** 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.
        */
        $throttle = (variable_get("throttle_enable", 0) && $module->throttle && (variable_get("throttle_level", 0) >= 5));
        if ($bootstrap && !$throttle) {
Dries's avatar
   
Dries committed
64
65
          $list[$module->name] = array("name"=> $module->name, "bootstrap" => $module->bootstrap, "filename" => $module->filename);
        }
Dries's avatar
   
Dries committed
66
        else if (!$throttle) {
Dries's avatar
   
Dries committed
67
68
69
          $list[$module->name] = $module->name;
          include_once $module->filename;
        }
70
      }
71
    }
72
    natcasesort($list);
73
74
75
76
77
78
79
  }
  return $list;
}

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

// 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
88
?>