diff options
author | Dries Buytaert <dries@buytaert.net> | 2001-05-05 13:57:29 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2001-05-05 13:57:29 +0000 |
commit | be8e898d23a3f9ca515f59fbcc8d82e112ed7ee8 (patch) | |
tree | cf0d05f6b767f36e7feb09a3bc59cbc9a01e459b /includes | |
parent | 16818777616eadeb8a4670324e099b95c8d53e3b (diff) | |
download | brdo-be8e898d23a3f9ca515f59fbcc8d82e112ed7ee8.tar.gz brdo-be8e898d23a3f9ca515f59fbcc8d82e112ed7ee8.tar.bz2 |
- Uhm. Rewrote the module system: less code clutter, less run-time
overhead, and a lot better (simpler) module API. I had to edit a
LOT of files to get this refactored but I'm sure it was worth the
effort.
For module writers / maintainers:
None of the hooks changed, so 95% of the old modules should still
work. You can remove some code instead as "$module = array(...)"
just became obsolete. Also - and let's thank God for this - the
global variable "$repository" has been eliminated to avoid modules
relying on, and poking in drupal's internal data structures. Take
a look at include/module.inc to investigate the details/changes.
- Improved design of the content modules "story", "book" and "node"
(to aid smooth integration of permisions + moderate.module). I'm
still working on the permissions but I got side tracked for which
I "Oops!".
Diffstat (limited to 'includes')
-rw-r--r-- | includes/common.inc | 3 | ||||
-rw-r--r-- | includes/module.inc | 96 | ||||
-rw-r--r-- | includes/search.inc | 2 | ||||
-rw-r--r-- | includes/theme.inc | 27 |
4 files changed, 70 insertions, 58 deletions
diff --git a/includes/common.inc b/includes/common.inc index a8b6f6769..ca942f5d2 100644 --- a/includes/common.inc +++ b/includes/common.inc @@ -24,7 +24,7 @@ function throttle($type, $rate) { if ($throttle = db_fetch_object(db_query("SELECT * FROM watchdog WHERE type = '$type' AND hostname = '". getenv("REMOTE_ADDR") ."' AND ". time() ." - timestamp < $rate"))) { watchdog("warning", "throttle: '". getenv("REMOTE_ADDR") ."' exceeded submission rate - $throttle->type"); header("Location: error.php?op=throttle"); - exit(); + die("submission rate exceeded"); } else { watchdog($type, "throttle control"); @@ -196,6 +196,7 @@ include_once "includes/user.inc"; include_once "includes/node.inc"; user_init(); +module_init(); $locale = locale_init(); $conf = variable_init(); $theme = theme_init(); diff --git a/includes/module.inc b/includes/module.inc index a23d142a0..732584861 100644 --- a/includes/module.inc +++ b/includes/module.inc @@ -1,28 +1,60 @@ <?php -// applies function $function to every known module: +// initialize modules: +function module_init() { + module_list(); +} + +// apply function $function to every known module: function module_iterate($function, $argument = "") { - global $repository; - foreach ($repository as $name=>$module) { - $function($name, $module, $argument); + 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); } + + return $list; } -// executes hook $hook of module $module with optional arguments: -function module_execute($module, $hook, $argument = "") { - global $repository; - return ($repository[$module][$hook]) ? $repository[$module][$hook]($argument) : ""; +// return 1 if module $name exists, 0 otherwise: +function module_exist($name) { + $list = module_list(); + return ($list[$name]) ? 1 : 0; } -// returns true if module $module supports hook $hook, and false otherwise: -function module_hook($module, $hook) { - global $repository; - return $repository[$module][$hook]; +// return 1 if module $name implements hook $hook, 0 otherwise: +function module_hook($name, $hook) { + return function_exists($name ."_". $hook); } -// rehashes the crons: -function module_rehash_crons($name, $module) { - if ($module["cron"]) { +// rehash module-exported crons: +function module_rehash_crons($name) { + if (module_hook($name, "cron")) { if (!db_fetch_object(db_query("SELECT * FROM crons WHERE module = '$name'"))) { db_query("INSERT INTO crons (module, scheduled, timestamp) VALUES ('$name', '172800', '0')"); } @@ -32,10 +64,10 @@ function module_rehash_crons($name, $module) { } } -// rehashes the blocks: -function module_rehash_blocks($name, $module) { +// rehash module-exported blocks: +function module_rehash_blocks($name) { db_query("UPDATE blocks SET remove = '1' WHERE module = '$name'"); - if ($module["block"] && $blocks = $module["block"]()) { + if ($blocks = module_invoke($name, "block")) { foreach ($blocks as $offset=>$block) { foreach ($block as $item=>$data) { $block[$item] = addslashes($data); @@ -51,22 +83,20 @@ function module_rehash_blocks($name, $module) { db_query("DELETE FROM blocks WHERE module = '$name' AND remove = '1'"); } -// rehashes a module: +// rehash a module: function module_rehash($name) { - global $repository; - - if ($module = $repository[$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 crons (if necessary): - module_rehash_crons($name, $module); + // rehash module-exported crons (if necessary): + module_rehash_crons($name); - // rehash blocks (if necessary): - module_rehash_blocks($name, $module); + // rehash module-exported blocks (if necessary): + module_rehash_blocks($name); } else { // remove all reference to module: @@ -76,16 +106,4 @@ function module_rehash($name) { } } -// load modules into repository: -$handle = opendir("modules"); -$repository = array(); -while ($file = readdir($handle)) { - if (".module" == substr($file, -7)) { - $filename = substr($file, 0, -7); - include "modules/$filename.module"; - $repository[$filename] = $module; - } -} -closedir($handle); - -?> +?>
\ No newline at end of file diff --git a/includes/search.inc b/includes/search.inc index 851bc3a3a..99ed78d74 100644 --- a/includes/search.inc +++ b/includes/search.inc @@ -11,7 +11,7 @@ function search_form($keys) { function search_data($keys, $type) { if ($keys && $type) { - $result = module_execute($type, "find", check_input($keys)); + $result = module_invoke($type, "find", check_input($keys)); foreach ($result as $entry) { $output .= "<P>\n"; $output .= " <B><U><A HREF=\"$entry[link]\">$entry[title]</A></U></B><BR>"; diff --git a/includes/theme.inc b/includes/theme.inc index 793824d06..8af929744 100644 --- a/includes/theme.inc +++ b/includes/theme.inc @@ -13,31 +13,22 @@ function theme_init() { } function theme_link($separator = " | ") { - global $repository; $links[] = "<A HREF=\"index.php\">". t("home") ."</A>"; $links[] = "<A HREF=\"search.php\">". t("search") ."</A>"; $links[] = "<A HREF=\"submit.php\">". t("submit") ."</A>"; - if ($repository[forum]) $links[] = "<A HREF=\"module.php?mod=forum\">".t("forum") ."</A>"; - if ($repository[diary]) $links[] = "<A HREF=\"module.php?mod=diary\">". t("diary") ."</A>"; $links[] = "<A HREF=\"account.php\">". t("account") ."</A>"; - if ($repository[book]) $links[] = "<A HREF=\"module.php?mod=book\">". t("handbook") ."</A>"; + if (module_exist("forum")) $links[] = "<A HREF=\"module.php?mod=forum\">".t("forum") ."</A>"; + if (module_exist("diary")) $links[] = "<A HREF=\"module.php?mod=diary\">". t("diary") ."</A>"; + if (module_exist("book")) $links[] = "<A HREF=\"module.php?mod=book\">". t("handbook") ."</A>"; return implode($separator, $links); } -function theme_menu($name, $module) { - global $menu; - if ($module["menu"]) $menu = ($menu) ? array_merge($menu, $module["menu"]()) : $module["menu"](); -} function theme_account($theme) { - global $user, $links, $menu; + global $user; if ($user->id) { - - - module_iterate("theme_menu"); - // Display account settings: $content .= "<LI><A HREF=\"account.php?op=track&topic=comments\">". t("track your comments") ."</A></LI>\n"; $content .= "<LI><A HREF=\"account.php?op=track&topic=nodes\">". t("track your nodes") ."</A></LI>\n"; @@ -53,10 +44,12 @@ function theme_account($theme) { $content .= "<P>\n"; } - if ($menu) { - foreach ($menu as $link) $content .= "<LI>$link</LI>\n"; - $content .= "<P>\n"; + foreach (module_list() as $name) { + if ($links = module_invoke($name, "menu")) { + foreach ($links as $link) $content .= "<LI>$link</LI>\n"; + } } + if ($link) $content .= "<P>\n"; $content .= "<LI><A HREF=\"account.php?op=logout\">". t("logout") ."</A></LI>\n"; @@ -92,7 +85,7 @@ function theme_blocks($region, $theme) { if ($user->id) $result = db_query("SELECT * FROM blocks b LEFT JOIN layout l ON b.name = l.block WHERE (b.status = 2 OR (b.status = 1 AND l.user = '$user->id'))". (($region == "left" || $region == "right") ? ($region == "left" ? " AND b.region = 0" : " AND b.region = 1") : "") ." ORDER BY weight"); else $result = db_query("SELECT * FROM blocks WHERE status = 2". (($region == "left" || $region == "right") ? ($region == "left" ? " AND region = 0" : " AND region = 1") : "") ." ORDER BY weight"); while ($block = db_fetch_object($result)) { - $blocks = module_execute($block->module, "block"); + $blocks = module_invoke($block->module, "block"); $theme->box(t($blocks[$block->offset]["subject"]), $blocks[$block->offset]["content"]); } break; |