summaryrefslogtreecommitdiff
path: root/includes/module.inc
blob: 92a4b26bf715071586320c94a3c8001b0d7de396 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
// $Id$

// initialize modules:
function module_init() {
  require_once("modules/user.module");
  require_once("modules/drupal.module");
  require_once("modules/system.module");
  require_once("modules/watchdog.module");
  module_list();
}

// apply function $function to every known module:
function module_iterate($function, $argument = "") {
  foreach (module_list() as $name) $function($name, $argument);
}

// invoke hook $hook of module $name with optional arguments:
function module_invoke($name, $hook, $a1 = NULL, $a2 = NULL, $a3 = NULL, $a4 = NULL) {
  $function = $name ."_". $hook;
  if (function_exists($function)) {
    return $function($a1, $a2, $a3, $a4);
  }
}

// invoke $hook for all appropriate modules:
function module_invoke_all($hook, $a1 = NULL, $a2 = NULL, $a3 = NULL, $a4 = NULL) {
  $return = array();
  foreach (module_list() as $name) {
    if (module_hook($name, $hook)) {
      if ($result = module_invoke($name, $hook, $a1, $a2, $a3, $a4)) {
        $return = array_merge($return, $result);
      }
    }
  }

  return $return;
}

// return array of module names (includes lazy module loading):
function module_list() {
  static $list;

  if (!$list) {
    $list = array("drupal" => "drupal", "system" => "system", "user" => "user", "watchdog" => "watchdog");
    $result = db_query("SELECT name, filename FROM system WHERE type = 'module' AND status = '1' ORDER BY name");
    while ($module = db_fetch_object($result)) {
      $list[$module->name] = $module->name;
      @include_once "modules/$module->filename";
    }
    asort($list);
  }

  return $list;
}

// return 1 if module $name exists, 0 otherwise:
function module_exist($name) {
  $list = module_list();
  return ($list[$name]) ? 1 : 0;
}

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

// rehash module-exported blocks:
function module_rehash_blocks($name) {
  db_query("UPDATE blocks SET remove = '1' WHERE module = '$name'");

  if ($blocks = module_invoke($name, "block")) {
    foreach ($blocks as $delta => $block) {
      foreach ($block as $item => $data) {
        $block[$item] = addslashes($data);
      }
      if (!db_fetch_object(db_query("SELECT * FROM blocks WHERE module = '$name' AND name = '$block[info]'"))) {
        db_query("INSERT INTO blocks (name, module, delta) VALUES ('$block[info]', '$name', '$delta')");
      }
      else {
        db_query("UPDATE blocks SET delta = '$delta', remove = '0' WHERE module = '$name' AND name = '$block[info]'");
      }
    }
  }

  db_query("DELETE FROM blocks WHERE module = '$name' AND remove = '1'");
}

// rehash a module:
function module_rehash($name) {
  if (module_exist($name)) {
    $result = db_query("SELECT * FROM modules WHERE name = '$name'");

    if (!$object = db_fetch_object($result)) {
      db_query("INSERT INTO modules (name) VALUES ('$name')");
    }

    // rehash module-exported blocks (if necessary):
    module_rehash_blocks($name);
  }
  else {
    // remove all reference to module:
    db_query("DELETE FROM modules WHERE name = '$name'");
    db_query("DELETE FROM blocks WHERE module = '$name'");
  }
}

?>