module.inc 3.14 KB
Newer Older
Dries's avatar
   
Dries committed
1
<?php
Dries's avatar
   
Dries committed
2

Dries's avatar
   
Dries committed
3
4
5
6
7
8
// initialize modules:
function module_init() {
  module_list();
}

// apply function $function to every known module:
Dries's avatar
   
Dries committed
9
function module_iterate($function, $argument = "") {
Dries's avatar
   
Dries committed
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
  foreach (module_list() as $name) $function($name, $argument);
}

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

// return true if module $name supports hook $hook, and false otherwise:
function module_is_hook($name, $hook) {
  return function_exists($name ."_". $hook);
}

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

  if (!$list) {
    $handle = opendir("modules");
    $list = array();
    while ($file = readdir($handle)) {
      if (".module" == substr($file, -7)) {
        $filename = substr($file, 0, -7);
        include "modules/$filename.module";
        $list[$filename] = $filename;
      }
    }
    closedir($handle);
39
    asort($list);
Dries's avatar
   
Dries committed
40
  }
Dries's avatar
   
Dries committed
41
42

  return $list;
Dries's avatar
   
Dries committed
43
44
}

Dries's avatar
   
Dries committed
45
46
47
48
// return 1 if module $name exists, 0 otherwise:
function module_exist($name) {
  $list = module_list();
  return ($list[$name]) ? 1 : 0;
Dries's avatar
   
Dries committed
49
50
}

Dries's avatar
   
Dries committed
51
52
53
// 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
54
55
}

Dries's avatar
   
Dries committed
56
57
58
// rehash module-exported crons:
function module_rehash_crons($name) {
  if (module_hook($name, "cron")) {
Dries's avatar
   
Dries committed
59
    if (!db_fetch_object(db_query("SELECT * FROM crons WHERE module = '$name'"))) {
Dries's avatar
   
Dries committed
60
      db_query("INSERT INTO crons (module, scheduled, timestamp) VALUES ('$name', '172800', '0')");
Dries's avatar
   
Dries committed
61
    }
Dries's avatar
   
Dries committed
62
63
64
65
66
67
  }
  else {
    db_query("DELETE FROM crons WHERE module = '$name'");
  }
}

Dries's avatar
   
Dries committed
68
69
// rehash module-exported blocks:
function module_rehash_blocks($name) {
70
  db_query("UPDATE blocks SET remove = '1' WHERE module = '$name'");
Dries's avatar
   
Dries committed
71
  if ($blocks = module_invoke($name, "block")) {
Dries's avatar
   
Dries committed
72
    foreach ($blocks as $offset=>$block) {
Kjartan's avatar
Changes    
Kjartan committed
73
74
75
      foreach ($block as $item=>$data) {
        $block[$item] = addslashes($data);
      }
Dries's avatar
   
Dries committed
76
77
      if (!db_fetch_object(db_query("SELECT * FROM blocks WHERE module = '$name' AND name = '$block[info]'"))) {
        db_query("INSERT INTO blocks (name, module, offset) VALUES ('$block[info]', '$name', '$offset')");
78
79
      }
      else {
Dries's avatar
   
Dries committed
80
        db_query("UPDATE blocks SET offset = '$offset', remove = '0' WHERE module = '$name' AND name = '$block[info]'");
81
      }
Dries's avatar
   
Dries committed
82
83
    }
  }
84
  db_query("DELETE FROM blocks WHERE module = '$name' AND remove = '1'");
Dries's avatar
   
Dries committed
85
86
}

Dries's avatar
   
Dries committed
87
// rehash a module:
Dries's avatar
   
Dries committed
88
function module_rehash($name) {
Dries's avatar
   
Dries committed
89
  if (module_exist($name)) {
Dries's avatar
   
Dries committed
90
91
92
    $result = db_query("SELECT * FROM modules WHERE name = '$name'");

    if (!$object = db_fetch_object($result)) {
Dries's avatar
   
Dries committed
93
      db_query("INSERT INTO modules (name) VALUES ('$name')");
Dries's avatar
   
Dries committed
94
95
    }

Dries's avatar
   
Dries committed
96
97
    // rehash module-exported crons (if necessary):
    module_rehash_crons($name);
Dries's avatar
   
Dries committed
98

Dries's avatar
   
Dries committed
99
100
    // rehash module-exported blocks (if necessary):
    module_rehash_blocks($name);
Dries's avatar
   
Dries committed
101
102
103
104
105
106
107
108
109
  }
  else {
    // remove all reference to module:
    db_query("DELETE FROM modules WHERE name = '$name'");
    db_query("DELETE FROM blocks WHERE module = '$name'");
    db_query("DELETE FROM crons WHERE module = '$name'");
  }
}

Dries's avatar
   
Dries committed
110
?>