module.inc

  1. 7.x drupal-7.x/includes/module.inc
  2. 6.x drupal-6.x/includes/module.inc

API for loading and interacting with Drupal modules.

File

drupal-6.x/includes/module.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * API for loading and interacting with Drupal modules.
  5. */
  6. /**
  7. * Load all the modules that have been enabled in the system table.
  8. */
  9. function module_load_all() {
  10. foreach (module_list(TRUE, FALSE) as $module) {
  11. drupal_load('module', $module);
  12. }
  13. }
  14. /**
  15. * Call a function repeatedly with each module in turn as an argument.
  16. */
  17. function module_iterate($function, $argument = '') {
  18. foreach (module_list() as $name) {
  19. $function($name, $argument);
  20. }
  21. }
  22. /**
  23. * Collect a list of all loaded modules. During the bootstrap, return only
  24. * vital modules. See bootstrap.inc
  25. *
  26. * @param $refresh
  27. * Whether to force the module list to be regenerated (such as after the
  28. * administrator has changed the system settings).
  29. * @param $bootstrap
  30. * Whether to return the reduced set of modules loaded in "bootstrap mode"
  31. * for cached pages. See bootstrap.inc.
  32. * @param $sort
  33. * By default, modules are ordered by weight and filename, settings this option
  34. * to TRUE, module list will be ordered by module name.
  35. * @param $fixed_list
  36. * (Optional) Override the module list with the given modules. Stays until the
  37. * next call with $refresh = TRUE.
  38. * @return
  39. * An associative array whose keys and values are the names of all loaded
  40. * modules.
  41. */
  42. function module_list($refresh = FALSE, $bootstrap = TRUE, $sort = FALSE, $fixed_list = NULL) {
  43. static $list, $sorted_list;
  44. if ($refresh || $fixed_list) {
  45. $list = array();
  46. $sorted_list = NULL;
  47. if ($fixed_list) {
  48. foreach ($fixed_list as $name => $module) {
  49. drupal_get_filename('module', $name, $module['filename']);
  50. $list[$name] = $name;
  51. }
  52. }
  53. else {
  54. if ($bootstrap) {
  55. $result = db_query("SELECT name, filename, throttle FROM {system} WHERE type = 'module' AND status = 1 AND bootstrap = 1 ORDER BY weight ASC, filename ASC");
  56. }
  57. else {
  58. $result = db_query("SELECT name, filename, throttle FROM {system} WHERE type = 'module' AND status = 1 ORDER BY weight ASC, filename ASC");
  59. }
  60. while ($module = db_fetch_object($result)) {
  61. if (file_exists($module->filename)) {
  62. // Determine the current throttle status and see if the module should be
  63. // loaded based on server load. We have to directly access the throttle
  64. // variables, since throttle.module may not be loaded yet.
  65. $throttle = ($module->throttle && variable_get('throttle_level', 0) > 0);
  66. if (!$throttle) {
  67. drupal_get_filename('module', $module->name, $module->filename);
  68. $list[$module->name] = $module->name;
  69. }
  70. }
  71. }
  72. }
  73. }
  74. if ($sort) {
  75. if (!isset($sorted_list)) {
  76. $sorted_list = $list;
  77. ksort($sorted_list);
  78. }
  79. return $sorted_list;
  80. }
  81. return $list;
  82. }
  83. /**
  84. * Rebuild the database cache of module files.
  85. *
  86. * @return
  87. * The array of filesystem objects used to rebuild the cache.
  88. */
  89. function module_rebuild_cache() {
  90. $write_database = TRUE;
  91. // If lock not acquired, return $files data without writing to database.
  92. if (!lock_acquire('module_rebuild_cache')) {
  93. $write_database = FALSE;
  94. // Wait for the parallel thread to be done so we are more likely
  95. // to get updated and consistent data.
  96. lock_wait('module_rebuild_cache');
  97. }
  98. // Get current list of modules
  99. $files = drupal_system_listing('\.module$', 'modules', 'name', 0);
  100. // Extract current files from database.
  101. system_get_files_database($files, 'module');
  102. ksort($files);
  103. // Set defaults for module info
  104. $defaults = array(
  105. 'dependencies' => array(),
  106. 'dependents' => array(),
  107. 'description' => '',
  108. 'version' => NULL,
  109. 'php' => DRUPAL_MINIMUM_PHP,
  110. );
  111. foreach ($files as $filename => $file) {
  112. // Look for the info file.
  113. $file->info = drupal_parse_info_file(dirname($file->filename) .'/'. $file->name .'.info');
  114. // Skip modules that don't provide info.
  115. if (empty($file->info)) {
  116. unset($files[$filename]);
  117. continue;
  118. }
  119. // Invoke hook_system_info_alter() to give installed modules a chance to
  120. // modify the data in the .info files if necessary.
  121. drupal_alter('system_info', $files[$filename]->info, $files[$filename]);
  122. // Merge in defaults and save.
  123. $files[$filename]->info = $file->info + $defaults;
  124. }
  125. // If lock not acquired, return $files data without writing to database.
  126. if ($write_database) {
  127. foreach ($files as $filename => $file) {
  128. // Log the critical hooks implemented by this module.
  129. $bootstrap = 0;
  130. foreach (bootstrap_hooks() as $hook) {
  131. if (module_hook($file->name, $hook)) {
  132. $bootstrap = 1;
  133. break;
  134. }
  135. }
  136. // Update the contents of the system table:
  137. if (isset($file->status)) {
  138. 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);
  139. }
  140. else {
  141. // This is a new module.
  142. $files[$filename]->status = 0;
  143. $files[$filename]->throttle = 0;
  144. db_query("INSERT INTO {system} (name, info, type, filename, status, throttle, bootstrap) VALUES ('%s', '%s', '%s', '%s', %d, %d, %d)", $file->name, serialize($files[$filename]->info), 'module', $file->filename, 0, 0, $bootstrap);
  145. }
  146. }
  147. lock_release('module_rebuild_cache');
  148. }
  149. $files = _module_build_dependencies($files);
  150. return $files;
  151. }
  152. /**
  153. * Find dependencies any level deep and fill in dependents information too.
  154. *
  155. * If module A depends on B which in turn depends on C then this function will
  156. * add C to the list of modules A depends on. This will be repeated until
  157. * module A has a list of all modules it depends on. If it depends on itself,
  158. * called a circular dependency, that's marked by adding a nonexistent module,
  159. * called -circular- to this list of modules. Because this does not exist,
  160. * it'll be impossible to switch module A on.
  161. *
  162. * Also we fill in a dependents array in $file->info. Using the names above,
  163. * the dependents array of module B lists A.
  164. *
  165. * @param $files
  166. * The array of filesystem objects used to rebuild the cache.
  167. * @return
  168. * The same array with dependencies and dependents added where applicable.
  169. */
  170. function _module_build_dependencies($files) {
  171. do {
  172. $new_dependency = FALSE;
  173. foreach ($files as $filename => $file) {
  174. // We will modify this object (module A, see doxygen for module A, B, C).
  175. $file = &$files[$filename];
  176. if (isset($file->info['dependencies']) && is_array($file->info['dependencies'])) {
  177. foreach ($file->info['dependencies'] as $dependency_name) {
  178. // This is a nonexistent module.
  179. if ($dependency_name == '-circular-' || !isset($files[$dependency_name])) {
  180. continue;
  181. }
  182. // $dependency_name is module B (again, see doxygen).
  183. $files[$dependency_name]->info['dependents'][$filename] = $filename;
  184. $dependency = $files[$dependency_name];
  185. if (isset($dependency->info['dependencies']) && is_array($dependency->info['dependencies'])) {
  186. // Let's find possible C modules.
  187. foreach ($dependency->info['dependencies'] as $candidate) {
  188. if (array_search($candidate, $file->info['dependencies']) === FALSE) {
  189. // Is this a circular dependency?
  190. if ($candidate == $filename) {
  191. // As a module name can not contain dashes, this makes
  192. // impossible to switch on the module.
  193. $candidate = '-circular-';
  194. // Do not display the message or add -circular- more than once.
  195. if (array_search($candidate, $file->info['dependencies']) !== FALSE) {
  196. continue;
  197. }
  198. 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');
  199. }
  200. else {
  201. // We added a new dependency to module A. The next loop will
  202. // be able to use this as "B module" thus finding even
  203. // deeper dependencies.
  204. $new_dependency = TRUE;
  205. }
  206. $file->info['dependencies'][] = $candidate;
  207. }
  208. }
  209. }
  210. }
  211. }
  212. // Don't forget to break the reference.
  213. unset($file);
  214. }
  215. } while ($new_dependency);
  216. return $files;
  217. }
  218. /**
  219. * Determine whether a given module exists.
  220. *
  221. * @param $module
  222. * The name of the module (without the .module extension).
  223. * @return
  224. * TRUE if the module is both installed and enabled.
  225. */
  226. function module_exists($module) {
  227. $list = module_list();
  228. return array_key_exists($module, $list);
  229. }
  230. /**
  231. * Load a module's installation hooks.
  232. */
  233. function module_load_install($module) {
  234. // Make sure the installation API is available
  235. include_once './includes/install.inc';
  236. module_load_include('install', $module);
  237. }
  238. /**
  239. * Load a module include file.
  240. *
  241. * Examples:
  242. * @code
  243. * // Load node.admin.inc from the node module.
  244. * module_load_include('inc', 'node', 'node.admin');
  245. * // Load content_types.inc from the node module.
  246. * module_load_include('inc', 'node', 'content_types');
  247. * @endcode
  248. *
  249. * Do not use this function to load an install file. Use module_load_install()
  250. * instead.
  251. *
  252. * @param $type
  253. * The include file's type (file extension).
  254. * @param $module
  255. * The module to which the include file belongs.
  256. * @param $name
  257. * Optionally, specify the base file name (without the $type extension).
  258. * If not set, $module is used.
  259. */
  260. function module_load_include($type, $module, $name = NULL) {
  261. if (empty($name)) {
  262. $name = $module;
  263. }
  264. $file = './'. drupal_get_path('module', $module) ."/$name.$type";
  265. if (is_file($file)) {
  266. require_once $file;
  267. }
  268. else {
  269. return FALSE;
  270. }
  271. }
  272. /**
  273. * Load an include file for each of the modules that have been enabled in
  274. * the system table.
  275. */
  276. function module_load_all_includes($type, $name = NULL) {
  277. $modules = module_list();
  278. foreach ($modules as $module) {
  279. module_load_include($type, $module, $name);
  280. }
  281. }
  282. /**
  283. * Enable a given list of modules.
  284. *
  285. * @param $module_list
  286. * An array of module names.
  287. */
  288. function module_enable($module_list) {
  289. $invoke_modules = array();
  290. foreach ($module_list as $module) {
  291. $existing = db_fetch_object(db_query("SELECT status FROM {system} WHERE type = '%s' AND name = '%s'", 'module', $module));
  292. if ($existing->status == 0) {
  293. module_load_install($module);
  294. db_query("UPDATE {system} SET status = %d, throttle = %d WHERE type = '%s' AND name = '%s'", 1, 0, 'module', $module);
  295. drupal_load('module', $module);
  296. $invoke_modules[] = $module;
  297. }
  298. }
  299. if (!empty($invoke_modules)) {
  300. // Refresh the module list to include the new enabled module.
  301. module_list(TRUE, FALSE);
  302. // Force to regenerate the stored list of hook implementations.
  303. module_implements('', FALSE, TRUE);
  304. }
  305. foreach ($invoke_modules as $module) {
  306. module_invoke($module, 'enable');
  307. // Check if node_access table needs rebuilding.
  308. // We check for the existence of node_access_needs_rebuild() since
  309. // at install time, module_enable() could be called while node.module
  310. // is not enabled yet.
  311. if (function_exists('node_access_needs_rebuild') && !node_access_needs_rebuild() && module_hook($module, 'node_grants')) {
  312. node_access_needs_rebuild(TRUE);
  313. }
  314. }
  315. }
  316. /**
  317. * Disable a given set of modules.
  318. *
  319. * @param $module_list
  320. * An array of module names.
  321. */
  322. function module_disable($module_list) {
  323. $invoke_modules = array();
  324. foreach ($module_list as $module) {
  325. if (module_exists($module)) {
  326. // Check if node_access table needs rebuilding.
  327. if (!node_access_needs_rebuild() && module_hook($module, 'node_grants')) {
  328. node_access_needs_rebuild(TRUE);
  329. }
  330. module_load_install($module);
  331. module_invoke($module, 'disable');
  332. db_query("UPDATE {system} SET status = %d, throttle = %d WHERE type = '%s' AND name = '%s'", 0, 0, 'module', $module);
  333. $invoke_modules[] = $module;
  334. }
  335. }
  336. if (!empty($invoke_modules)) {
  337. // Refresh the module list to exclude the disabled modules.
  338. module_list(TRUE, FALSE);
  339. // Force to regenerate the stored list of hook implementations.
  340. module_implements('', FALSE, TRUE);
  341. }
  342. // If there remains no more node_access module, rebuilding will be
  343. // straightforward, we can do it right now.
  344. if (node_access_needs_rebuild() && count(module_implements('node_grants')) == 0) {
  345. node_access_rebuild();
  346. }
  347. }
  348. /**
  349. * @defgroup hooks Hooks
  350. * @{
  351. * Allow modules to interact with the Drupal core.
  352. *
  353. * Drupal's module system is based on the concept of "hooks". A hook is a PHP
  354. * function that is named foo_bar(), where "foo" is the name of the module
  355. * (whose filename is thus foo.module) and "bar" is the name of the hook. Each
  356. * hook has a defined set of parameters and a specified result type.
  357. *
  358. * To extend Drupal, a module need simply implement a hook. When Drupal wishes
  359. * to allow intervention from modules, it determines which modules implement a
  360. * hook and calls that hook in all enabled modules that implement it.
  361. *
  362. * The available hooks to implement are explained here in the Hooks section of
  363. * the developer documentation. The string "hook" is used as a placeholder for
  364. * the module name in the hook definitions. For example, if the module file is
  365. * called example.module, then hook_help() as implemented by that module would
  366. * be defined as example_help().
  367. */
  368. /**
  369. * Determine whether a module implements a hook.
  370. *
  371. * @param $module
  372. * The name of the module (without the .module extension).
  373. * @param $hook
  374. * The name of the hook (e.g. "help" or "menu").
  375. * @return
  376. * TRUE if the module is both installed and enabled, and the hook is
  377. * implemented in that module.
  378. */
  379. function module_hook($module, $hook) {
  380. return function_exists($module .'_'. $hook);
  381. }
  382. /**
  383. * Determine which modules are implementing a hook.
  384. *
  385. * @param $hook
  386. * The name of the hook (e.g. "help" or "menu").
  387. * @param $sort
  388. * By default, modules are ordered by weight and filename, settings this option
  389. * to TRUE, module list will be ordered by module name.
  390. * @param $refresh
  391. * For internal use only: Whether to force the stored list of hook
  392. * implementations to be regenerated (such as after enabling a new module,
  393. * before processing hook_enable).
  394. * @return
  395. * An array with the names of the modules which are implementing this hook.
  396. */
  397. function module_implements($hook, $sort = FALSE, $refresh = FALSE) {
  398. static $implementations;
  399. if ($refresh) {
  400. $implementations = array();
  401. return;
  402. }
  403. if (!isset($implementations[$hook])) {
  404. $implementations[$hook] = array();
  405. $list = module_list(FALSE, TRUE, $sort);
  406. foreach ($list as $module) {
  407. if (module_hook($module, $hook)) {
  408. $implementations[$hook][] = $module;
  409. }
  410. }
  411. }
  412. // The explicit cast forces a copy to be made. This is needed because
  413. // $implementations[$hook] is only a reference to an element of
  414. // $implementations and if there are nested foreaches (due to nested node
  415. // API calls, for example), they would both manipulate the same array's
  416. // references, which causes some modules' hooks not to be called.
  417. // See also http://www.zend.com/zend/art/ref-count.php.
  418. return (array)$implementations[$hook];
  419. }
  420. /**
  421. * Invoke a hook in a particular module.
  422. *
  423. * @param $module
  424. * The name of the module (without the .module extension).
  425. * @param $hook
  426. * The name of the hook to invoke.
  427. * @param ...
  428. * Arguments to pass to the hook implementation.
  429. * @return
  430. * The return value of the hook implementation.
  431. */
  432. function module_invoke() {
  433. $args = func_get_args();
  434. $module = $args[0];
  435. $hook = $args[1];
  436. unset($args[0], $args[1]);
  437. $function = $module .'_'. $hook;
  438. if (module_hook($module, $hook)) {
  439. return call_user_func_array($function, $args);
  440. }
  441. }
  442. /**
  443. * Invoke a hook in all enabled modules that implement it.
  444. *
  445. * @param $hook
  446. * The name of the hook to invoke.
  447. * @param ...
  448. * Arguments to pass to the hook.
  449. * @return
  450. * An array of return values of the hook implementations. If modules return
  451. * arrays from their implementations, those are merged into one array.
  452. */
  453. function module_invoke_all() {
  454. $args = func_get_args();
  455. $hook = $args[0];
  456. unset($args[0]);
  457. $return = array();
  458. foreach (module_implements($hook) as $module) {
  459. $function = $module .'_'. $hook;
  460. $result = call_user_func_array($function, $args);
  461. if (isset($result) && is_array($result)) {
  462. $return = array_merge_recursive($return, $result);
  463. }
  464. else if (isset($result)) {
  465. $return[] = $result;
  466. }
  467. }
  468. return $return;
  469. }
  470. /**
  471. * @} End of "defgroup hooks".
  472. */
  473. /**
  474. * Array of modules required by core.
  475. */
  476. function drupal_required_modules() {
  477. return array('block', 'filter', 'node', 'system', 'user');
  478. }