00001 <?php
00002
00003
00012 function module_load_all() {
00013 foreach (module_list(TRUE, FALSE) as $module) {
00014 drupal_load('module', $module);
00015 }
00016 }
00017
00021 function module_iterate($function, $argument = '') {
00022 foreach (module_list() as $name) {
00023 $function($name, $argument);
00024 }
00025 }
00026
00047 function module_list($refresh = FALSE, $bootstrap = TRUE, $sort = FALSE, $fixed_list = NULL) {
00048 static $list, $sorted_list;
00049
00050 if ($refresh || $fixed_list) {
00051 unset($sorted_list);
00052 $list = array();
00053 if ($fixed_list) {
00054 foreach ($fixed_list as $name => $module) {
00055 drupal_get_filename('module', $name, $module['filename']);
00056 $list[$name] = $name;
00057 }
00058 }
00059 else {
00060 if ($bootstrap) {
00061 $result = db_query("SELECT name, filename FROM {system} WHERE type = 'module' AND status = 1 AND bootstrap = 1 ORDER BY weight ASC, filename ASC");
00062 }
00063 else {
00064 $result = db_query("SELECT name, filename FROM {system} WHERE type = 'module' AND status = 1 ORDER BY weight ASC, filename ASC");
00065 }
00066 while ($module = db_fetch_object($result)) {
00067 if (file_exists($module->filename)) {
00068 drupal_get_filename('module', $module->name, $module->filename);
00069 $list[$module->name] = $module->name;
00070 }
00071 }
00072 }
00073 }
00074 if ($sort) {
00075 if (!isset($sorted_list)) {
00076 $sorted_list = $list;
00077 ksort($sorted_list);
00078 }
00079 return $sorted_list;
00080 }
00081 return $list;
00082 }
00083
00090 function module_rebuild_cache() {
00091
00092 $files = drupal_system_listing('\.module$', 'modules', 'name', 0);
00093
00094
00095 system_get_files_database($files, 'module');
00096
00097 ksort($files);
00098
00099
00100 $defaults = array(
00101 'dependencies' => array(),
00102 'dependents' => array(),
00103 'description' => '',
00104 'version' => NULL,
00105 'php' => DRUPAL_MINIMUM_PHP,
00106 );
00107
00108 foreach ($files as $filename => $file) {
00109
00110 $file->info = drupal_parse_info_file(dirname($file->filename) . '/' . $file->name . '.info');
00111
00112
00113 if (empty($file->info)) {
00114 unset($files[$filename]);
00115 continue;
00116 }
00117
00118 $files[$filename]->info = $file->info + $defaults;
00119
00120
00121
00122 drupal_alter('system_info', $files[$filename]->info, $files[$filename]);
00123
00124
00125 $bootstrap = 0;
00126 foreach (bootstrap_hooks() as $hook) {
00127 if (module_hook($file->name, $hook)) {
00128 $bootstrap = 1;
00129 break;
00130 }
00131 }
00132
00133
00134 if (isset($file->status) || (isset($file->old_filename) && $file->old_filename != $file->filename)) {
00135 db_query("UPDATE {system} SET info = '%s', name = '%s', filename = '%s', bootstrap = %d WHERE filename = '%s'", serialize($files[$filename]->info), $file->name, $file->filename, $bootstrap, $file->old_filename);
00136 }
00137 else {
00138
00139 $files[$filename]->status = 0;
00140 db_query("INSERT INTO {system} (name, info, type, filename, status, bootstrap) VALUES ('%s', '%s', '%s', '%s', %d, %d)", $file->name, serialize($files[$filename]->info), 'module', $file->filename, 0, $bootstrap);
00141 }
00142 }
00143 $files = _module_build_dependencies($files);
00144 return $files;
00145 }
00146
00165 function _module_build_dependencies($files) {
00166 do {
00167 $new_dependency = FALSE;
00168 foreach ($files as $filename => $file) {
00169
00170 $file = &$files[$filename];
00171 if (isset($file->info['dependencies']) && is_array($file->info['dependencies'])) {
00172 foreach ($file->info['dependencies'] as $dependency_name) {
00173
00174 if ($dependency_name == '-circular-' || !isset($files[$dependency_name])) {
00175 continue;
00176 }
00177
00178 $files[$dependency_name]->info['dependents'][$filename] = $filename;
00179 $dependency = $files[$dependency_name];
00180 if (isset($dependency->info['dependencies']) && is_array($dependency->info['dependencies'])) {
00181
00182 foreach ($dependency->info['dependencies'] as $candidate) {
00183 if (array_search($candidate, $file->info['dependencies']) === FALSE) {
00184
00185 if ($candidate == $filename) {
00186
00187
00188 $candidate = '-circular-';
00189
00190 if (array_search($candidate, $file->info['dependencies']) !== FALSE) {
00191 continue;
00192 }
00193 drupal_set_message(t('%module is part of a circular dependency. This is not supported and you will not be able to switch it on.', array('%module' => $file->info['name'])), 'error');
00194 }
00195 else {
00196
00197
00198
00199 $new_dependency = TRUE;
00200 }
00201 $file->info['dependencies'][] = $candidate;
00202 }
00203 }
00204 }
00205 }
00206 }
00207
00208 unset($file);
00209 }
00210 } while ($new_dependency);
00211 return $files;
00212 }
00213
00222 function module_exists($module) {
00223 $list = module_list();
00224 return isset($list[$module]);
00225 }
00226
00230 function module_load_install($module) {
00231
00232 include_once './includes/install.inc';
00233
00234 module_load_include('install', $module);
00235 }
00236
00247 function module_load_include($type, $module, $name = NULL) {
00248 if (empty($name)) {
00249 $name = $module;
00250 }
00251
00252 $file = './' . drupal_get_path('module', $module) . "/$name.$type";
00253
00254 if (is_file($file)) {
00255 require_once $file;
00256 return $file;
00257 }
00258 else {
00259 return FALSE;
00260 }
00261 }
00262
00267 function module_load_all_includes($type, $name = NULL) {
00268 $modules = module_list();
00269 foreach ($modules as $module) {
00270 module_load_include($type, $module, $name);
00271 }
00272 }
00273
00280 function module_enable($module_list) {
00281 $invoke_modules = array();
00282 foreach ($module_list as $module) {
00283 $existing = db_fetch_object(db_query("SELECT status FROM {system} WHERE type = '%s' AND name = '%s'", 'module', $module));
00284 if ($existing->status == 0) {
00285 module_load_install($module);
00286 db_query("UPDATE {system} SET status = %d WHERE type = '%s' AND name = '%s'", 1, 'module', $module);
00287 drupal_load('module', $module);
00288 $invoke_modules[] = $module;
00289 }
00290 }
00291
00292 if (!empty($invoke_modules)) {
00293
00294 module_list(TRUE, FALSE);
00295
00296 drupal_rebuild_code_registry();
00297 }
00298
00299 foreach ($invoke_modules as $module) {
00300 module_invoke($module, 'enable');
00301
00302
00303
00304
00305 if (drupal_function_exists('node_access_needs_rebuild') && !node_access_needs_rebuild() && module_hook($module, 'node_grants')) {
00306 node_access_needs_rebuild(TRUE);
00307 }
00308 }
00309 }
00310
00317 function module_disable($module_list) {
00318 $invoke_modules = array();
00319 foreach ($module_list as $module) {
00320 if (module_exists($module)) {
00321
00322 if (!node_access_needs_rebuild() && module_hook($module, 'node_grants')) {
00323 node_access_needs_rebuild(TRUE);
00324 }
00325
00326 module_load_install($module);
00327 module_invoke($module, 'disable');
00328 db_query("UPDATE {system} SET status = %d WHERE type = '%s' AND name = '%s'", 0, 'module', $module);
00329 $invoke_modules[] = $module;
00330 }
00331 }
00332
00333 if (!empty($invoke_modules)) {
00334
00335 module_list(TRUE, FALSE);
00336
00337 drupal_rebuild_code_registry();
00338 }
00339
00340
00341
00342 if (node_access_needs_rebuild() && count(module_implements('node_grants')) == 0) {
00343 node_access_rebuild();
00344 }
00345 }
00346
00379 function module_hook($module, $hook) {
00380 $function = $module . '_' . $hook;
00381 if (defined('MAINTENANCE_MODE')) {
00382 return function_exists($function);
00383 }
00384 else {
00385 return drupal_function_exists($function);
00386 }
00387 }
00388
00404 function module_implements($hook, $sort = FALSE, $refresh = FALSE) {
00405 static $implementations = array();
00406
00407 if ($refresh) {
00408 $implementations = array();
00409 }
00410 else if (!defined('MAINTENANCE_MODE') && empty($implementations)) {
00411 $implementations = registry_get_hook_implementations_cache();
00412 }
00413
00414 if (!isset($implementations[$hook])) {
00415 $implementations[$hook] = array();
00416 foreach (module_list() as $module) {
00417 if (module_hook($module, $hook)) {
00418 $implementations[$hook][] = $module;
00419 }
00420 }
00421 }
00422 registry_cache_hook_implementations(array('hook' => $hook, 'modules' => $implementations[$hook]));
00423
00424
00425
00426
00427
00428
00429
00430 return (array)$implementations[$hook];
00431 }
00432
00445 function module_invoke() {
00446 $args = func_get_args();
00447 $module = $args[0];
00448 $hook = $args[1];
00449 unset($args[0], $args[1]);
00450 if (module_hook($module, $hook)) {
00451 return call_user_func_array($module . '_' . $hook, $args);
00452 }
00453 }
00465 function module_invoke_all() {
00466 $args = func_get_args();
00467 $hook = $args[0];
00468 unset($args[0]);
00469 $return = array();
00470 foreach (module_implements($hook) as $module) {
00471 $function = $module . '_' . $hook;
00472 if (drupal_function_exists($function)) {
00473 $result = call_user_func_array($function, $args);
00474 if (isset($result) && is_array($result)) {
00475 $return = array_merge_recursive($return, $result);
00476 }
00477 else if (isset($result)) {
00478 $return[] = $result;
00479 }
00480 }
00481 }
00482
00483 return $return;
00484 }
00485
00493 function drupal_required_modules() {
00494 return array('block', 'filter', 'node', 'system', 'user');
00495 }