module.inc 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. <?php
  2. /**
  3. * @file
  4. * API for loading and interacting with Drupal modules.
  5. */
  6. /**
  7. * Loads all the modules that have been enabled in the system table.
  8. *
  9. * @param $bootstrap
  10. * Whether to load only the reduced set of modules loaded in "bootstrap mode"
  11. * for cached pages. See bootstrap.inc.
  12. *
  13. * @return
  14. * If $bootstrap is NULL, return a boolean indicating whether all modules
  15. * have been loaded.
  16. */
  17. function module_load_all($bootstrap = FALSE) {
  18. static $has_run = FALSE;
  19. if (isset($bootstrap)) {
  20. foreach (module_list(TRUE, $bootstrap) as $module) {
  21. drupal_load('module', $module);
  22. }
  23. // $has_run will be TRUE if $bootstrap is FALSE.
  24. $has_run = !$bootstrap;
  25. }
  26. return $has_run;
  27. }
  28. /**
  29. * Returns a list of currently active modules.
  30. *
  31. * Usually, this returns a list of all enabled modules. When called early on in
  32. * the bootstrap, it will return a list of vital modules only (those needed to
  33. * generate cached pages).
  34. *
  35. * All parameters to this function are optional and should generally not be
  36. * changed from their defaults.
  37. *
  38. * @param $refresh
  39. * (optional) Whether to force the module list to be regenerated (such as
  40. * after the administrator has changed the system settings). Defaults to
  41. * FALSE.
  42. * @param $bootstrap_refresh
  43. * (optional) When $refresh is TRUE, setting $bootstrap_refresh to TRUE forces
  44. * the module list to be regenerated using the reduced set of modules loaded
  45. * in "bootstrap mode" for cached pages. Otherwise, setting $refresh to TRUE
  46. * generates the complete list of enabled modules.
  47. * @param $sort
  48. * (optional) By default, modules are ordered by weight and module name. Set
  49. * this option to TRUE to return a module list ordered only by module name.
  50. * @param $fixed_list
  51. * (optional) If an array of module names is provided, this will override the
  52. * module list with the given set of modules. This will persist until the next
  53. * call with $refresh set to TRUE or with a new $fixed_list passed in. This
  54. * parameter is primarily intended for internal use (e.g., in install.php and
  55. * update.php).
  56. *
  57. * @return
  58. * An associative array whose keys and values are the names of the modules in
  59. * the list.
  60. */
  61. function module_list($refresh = FALSE, $bootstrap_refresh = FALSE, $sort = FALSE, $fixed_list = NULL) {
  62. static $list = array(), $sorted_list;
  63. if (empty($list) || $refresh || $fixed_list) {
  64. $list = array();
  65. $sorted_list = NULL;
  66. if ($fixed_list) {
  67. foreach ($fixed_list as $name => $module) {
  68. drupal_get_filename('module', $name, $module['filename']);
  69. $list[$name] = $name;
  70. }
  71. }
  72. else {
  73. if ($refresh) {
  74. // For the $refresh case, make sure that system_list() returns fresh
  75. // data.
  76. drupal_static_reset('system_list');
  77. }
  78. if ($bootstrap_refresh) {
  79. $list = system_list('bootstrap');
  80. }
  81. else {
  82. // Not using drupal_map_assoc() here as that requires common.inc.
  83. $list = array_keys(system_list('module_enabled'));
  84. $list = (!empty($list) ? array_combine($list, $list) : array());
  85. }
  86. }
  87. }
  88. if ($sort) {
  89. if (!isset($sorted_list)) {
  90. $sorted_list = $list;
  91. ksort($sorted_list);
  92. }
  93. return $sorted_list;
  94. }
  95. return $list;
  96. }
  97. /**
  98. * Builds a list of bootstrap modules and enabled modules and themes.
  99. *
  100. * @param $type
  101. * The type of list to return:
  102. * - module_enabled: All enabled modules.
  103. * - bootstrap: All enabled modules required for bootstrap.
  104. * - theme: All themes.
  105. *
  106. * @return
  107. * An associative array of modules or themes, keyed by name. For $type
  108. * 'bootstrap', the array values equal the keys. For $type 'module_enabled'
  109. * or 'theme', the array values are objects representing the respective
  110. * database row, with the 'info' property already unserialized.
  111. *
  112. * @see module_list()
  113. * @see list_themes()
  114. */
  115. function system_list($type) {
  116. $lists = &drupal_static(__FUNCTION__);
  117. // For bootstrap modules, attempt to fetch the list from cache if possible.
  118. // if not fetch only the required information to fire bootstrap hooks
  119. // in case we are going to serve the page from cache.
  120. if ($type == 'bootstrap') {
  121. if (isset($lists['bootstrap'])) {
  122. return $lists['bootstrap'];
  123. }
  124. if ($cached = cache_get('bootstrap_modules', 'cache_bootstrap')) {
  125. $bootstrap_list = $cached->data;
  126. }
  127. else {
  128. $bootstrap_list = db_query("SELECT name, filename FROM {system} WHERE status = 1 AND bootstrap = 1 AND type = 'module' ORDER BY weight ASC, name ASC")->fetchAllAssoc('name');
  129. cache_set('bootstrap_modules', $bootstrap_list, 'cache_bootstrap');
  130. }
  131. // To avoid a separate database lookup for the filepath, prime the
  132. // drupal_get_filename() static cache for bootstrap modules only.
  133. // The rest is stored separately to keep the bootstrap module cache small.
  134. foreach ($bootstrap_list as $module) {
  135. drupal_get_filename('module', $module->name, $module->filename);
  136. }
  137. // We only return the module names here since module_list() doesn't need
  138. // the filename itself.
  139. $lists['bootstrap'] = array_keys($bootstrap_list);
  140. }
  141. // Otherwise build the list for enabled modules and themes.
  142. elseif (!isset($lists['module_enabled'])) {
  143. if ($cached = cache_get('system_list', 'cache_bootstrap')) {
  144. $lists = $cached->data;
  145. }
  146. else {
  147. $lists = array(
  148. 'module_enabled' => array(),
  149. 'theme' => array(),
  150. 'filepaths' => array(),
  151. );
  152. // The module name (rather than the filename) is used as the fallback
  153. // weighting in order to guarantee consistent behavior across different
  154. // Drupal installations, which might have modules installed in different
  155. // locations in the file system. The ordering here must also be
  156. // consistent with the one used in module_implements().
  157. $result = db_query("SELECT * FROM {system} WHERE type = 'theme' OR (type = 'module' AND status = 1) ORDER BY weight ASC, name ASC");
  158. foreach ($result as $record) {
  159. $record->info = unserialize($record->info);
  160. // Build a list of all enabled modules.
  161. if ($record->type == 'module') {
  162. $lists['module_enabled'][$record->name] = $record;
  163. }
  164. // Build a list of themes.
  165. if ($record->type == 'theme') {
  166. $lists['theme'][$record->name] = $record;
  167. }
  168. // Build a list of filenames so drupal_get_filename can use it.
  169. if ($record->status) {
  170. $lists['filepaths'][] = array('type' => $record->type, 'name' => $record->name, 'filepath' => $record->filename);
  171. }
  172. }
  173. foreach ($lists['theme'] as $key => $theme) {
  174. if (!empty($theme->info['base theme'])) {
  175. // Make a list of the theme's base themes.
  176. require_once DRUPAL_ROOT . '/includes/theme.inc';
  177. $lists['theme'][$key]->base_themes = drupal_find_base_themes($lists['theme'], $key);
  178. // Don't proceed if there was a problem with the root base theme.
  179. if (!current($lists['theme'][$key]->base_themes)) {
  180. continue;
  181. }
  182. // Determine the root base theme.
  183. $base_key = key($lists['theme'][$key]->base_themes);
  184. // Add to the list of sub-themes for each of the theme's base themes.
  185. foreach (array_keys($lists['theme'][$key]->base_themes) as $base_theme) {
  186. $lists['theme'][$base_theme]->sub_themes[$key] = $lists['theme'][$key]->info['name'];
  187. }
  188. // Add the base theme's theme engine info.
  189. $lists['theme'][$key]->info['engine'] = isset($lists['theme'][$base_key]->info['engine']) ? $lists['theme'][$base_key]->info['engine'] : 'theme';
  190. }
  191. else {
  192. // A plain theme is its own engine.
  193. $base_key = $key;
  194. if (!isset($lists['theme'][$key]->info['engine'])) {
  195. $lists['theme'][$key]->info['engine'] = 'theme';
  196. }
  197. }
  198. // Set the theme engine prefix.
  199. $lists['theme'][$key]->prefix = ($lists['theme'][$key]->info['engine'] == 'theme') ? $base_key : $lists['theme'][$key]->info['engine'];
  200. }
  201. cache_set('system_list', $lists, 'cache_bootstrap');
  202. }
  203. // To avoid a separate database lookup for the filepath, prime the
  204. // drupal_get_filename() static cache with all enabled modules and themes.
  205. foreach ($lists['filepaths'] as $item) {
  206. drupal_get_filename($item['type'], $item['name'], $item['filepath']);
  207. }
  208. }
  209. return $lists[$type];
  210. }
  211. /**
  212. * Resets all system_list() caches.
  213. */
  214. function system_list_reset() {
  215. drupal_static_reset('system_list');
  216. drupal_static_reset('system_rebuild_module_data');
  217. drupal_static_reset('list_themes');
  218. cache_clear_all('bootstrap_modules', 'cache_bootstrap');
  219. cache_clear_all('system_list', 'cache_bootstrap');
  220. // Clean up the bootstrap file scan cache.
  221. drupal_static_reset('_drupal_file_scan_cache');
  222. cache_clear_all('_drupal_file_scan_cache', 'cache_bootstrap');
  223. }
  224. /**
  225. * Determines which modules require and are required by each module.
  226. *
  227. * @param $files
  228. * The array of filesystem objects used to rebuild the cache.
  229. *
  230. * @return
  231. * The same array with the new keys for each module:
  232. * - requires: An array with the keys being the modules that this module
  233. * requires.
  234. * - required_by: An array with the keys being the modules that will not work
  235. * without this module.
  236. */
  237. function _module_build_dependencies($files) {
  238. require_once DRUPAL_ROOT . '/includes/graph.inc';
  239. foreach ($files as $filename => $file) {
  240. $graph[$file->name]['edges'] = array();
  241. if (isset($file->info['dependencies']) && is_array($file->info['dependencies'])) {
  242. foreach ($file->info['dependencies'] as $dependency) {
  243. $dependency_data = drupal_parse_dependency($dependency);
  244. $graph[$file->name]['edges'][$dependency_data['name']] = $dependency_data;
  245. }
  246. }
  247. }
  248. drupal_depth_first_search($graph);
  249. foreach ($graph as $module => $data) {
  250. $files[$module]->required_by = isset($data['reverse_paths']) ? $data['reverse_paths'] : array();
  251. $files[$module]->requires = isset($data['paths']) ? $data['paths'] : array();
  252. $files[$module]->sort = $data['weight'];
  253. }
  254. return $files;
  255. }
  256. /**
  257. * Determines whether a given module exists.
  258. *
  259. * @param string $module
  260. * The name of the module (without the .module extension).
  261. *
  262. * @return bool
  263. * TRUE if the module is both installed and enabled, FALSE otherwise.
  264. */
  265. function module_exists($module) {
  266. $list = module_list();
  267. return isset($list[$module]);
  268. }
  269. /**
  270. * Loads a module's installation hooks.
  271. *
  272. * @param $module
  273. * The name of the module (without the .module extension).
  274. *
  275. * @return
  276. * The name of the module's install file, if successful; FALSE otherwise.
  277. */
  278. function module_load_install($module) {
  279. // Make sure the installation API is available
  280. include_once DRUPAL_ROOT . '/includes/install.inc';
  281. return module_load_include('install', $module);
  282. }
  283. /**
  284. * Loads a module include file.
  285. *
  286. * Examples:
  287. * @code
  288. * // Load node.admin.inc from the node module.
  289. * module_load_include('inc', 'node', 'node.admin');
  290. * // Load content_types.inc from the node module.
  291. * module_load_include('inc', 'node', 'content_types');
  292. * @endcode
  293. *
  294. * Do not use this function to load an install file, use module_load_install()
  295. * instead. Do not use this function in a global context since it requires
  296. * Drupal to be fully bootstrapped, use require_once DRUPAL_ROOT . '/path/file'
  297. * instead.
  298. *
  299. * @param $type
  300. * The include file's type (file extension).
  301. * @param $module
  302. * The module to which the include file belongs.
  303. * @param $name
  304. * (optional) The base file name (without the $type extension). If omitted,
  305. * $module is used; i.e., resulting in "$module.$type" by default.
  306. *
  307. * @return
  308. * The name of the included file, if successful; FALSE otherwise.
  309. */
  310. function module_load_include($type, $module, $name = NULL) {
  311. static $files = array();
  312. if (!isset($name)) {
  313. $name = $module;
  314. }
  315. $key = $type . ':' . $module . ':' . $name;
  316. if (isset($files[$key])) {
  317. return $files[$key];
  318. }
  319. if (function_exists('drupal_get_path')) {
  320. $file = DRUPAL_ROOT . '/' . drupal_get_path('module', $module) . "/$name.$type";
  321. if (is_file($file)) {
  322. require_once $file;
  323. $files[$key] = $file;
  324. return $file;
  325. }
  326. else {
  327. $files[$key] = FALSE;
  328. }
  329. }
  330. return FALSE;
  331. }
  332. /**
  333. * Loads an include file for each module enabled in the {system} table.
  334. */
  335. function module_load_all_includes($type, $name = NULL) {
  336. $modules = module_list();
  337. foreach ($modules as $module) {
  338. module_load_include($type, $module, $name);
  339. }
  340. }
  341. /**
  342. * Enables or installs a given list of modules.
  343. *
  344. * Definitions:
  345. * - "Enabling" is the process of activating a module for use by Drupal.
  346. * - "Disabling" is the process of deactivating a module.
  347. * - "Installing" is the process of enabling it for the first time or after it
  348. * has been uninstalled.
  349. * - "Uninstalling" is the process of removing all traces of a module.
  350. *
  351. * Order of events:
  352. * - Gather and add module dependencies to $module_list (if applicable).
  353. * - For each module that is being enabled:
  354. * - Install module schema and update system registries and caches.
  355. * - If the module is being enabled for the first time or had been
  356. * uninstalled, invoke hook_install() and add it to the list of installed
  357. * modules.
  358. * - Invoke hook_enable().
  359. * - Invoke hook_modules_installed().
  360. * - Invoke hook_modules_enabled().
  361. *
  362. * @param string[] $module_list
  363. * An array of module names.
  364. * @param bool $enable_dependencies
  365. * If TRUE, dependencies will automatically be added and enabled in the
  366. * correct order. This incurs a significant performance cost, so use FALSE
  367. * if you know $module_list is already complete and in the correct order.
  368. *
  369. * @return bool
  370. * FALSE if one or more dependencies are missing, TRUE otherwise.
  371. *
  372. * @see hook_install()
  373. * @see hook_enable()
  374. * @see hook_modules_installed()
  375. * @see hook_modules_enabled()
  376. * @see module_disable()
  377. * @see drupal_uninstall_modules()
  378. */
  379. function module_enable($module_list, $enable_dependencies = TRUE) {
  380. if ($enable_dependencies) {
  381. // Get all module data so we can find dependencies and sort.
  382. $module_data = system_rebuild_module_data();
  383. // Create an associative array with weights as values.
  384. $module_list = array_flip(array_values($module_list));
  385. while (list($module) = each($module_list)) {
  386. if (!isset($module_data[$module])) {
  387. // This module is not found in the filesystem, abort.
  388. return FALSE;
  389. }
  390. if ($module_data[$module]->status) {
  391. // Skip already enabled modules.
  392. unset($module_list[$module]);
  393. continue;
  394. }
  395. $module_list[$module] = $module_data[$module]->sort;
  396. // Add dependencies to the list, with a placeholder weight.
  397. // The new modules will be processed as the while loop continues.
  398. foreach (array_keys($module_data[$module]->requires) as $dependency) {
  399. if (!isset($module_list[$dependency])) {
  400. $module_list[$dependency] = 0;
  401. }
  402. }
  403. }
  404. if (!$module_list) {
  405. // Nothing to do. All modules already enabled.
  406. return TRUE;
  407. }
  408. // Sort the module list by pre-calculated weights.
  409. arsort($module_list);
  410. $module_list = array_keys($module_list);
  411. }
  412. // Required for module installation checks.
  413. include_once DRUPAL_ROOT . '/includes/install.inc';
  414. $modules_installed = array();
  415. $modules_enabled = array();
  416. foreach ($module_list as $module) {
  417. // Only process modules that are not already enabled.
  418. $existing = db_query("SELECT status FROM {system} WHERE type = :type AND name = :name", array(
  419. ':type' => 'module',
  420. ':name' => $module))
  421. ->fetchObject();
  422. if ($existing->status == 0) {
  423. // Load the module's code.
  424. drupal_load('module', $module);
  425. module_load_install($module);
  426. // Update the database and module list to reflect the new module. This
  427. // needs to be done first so that the module's hook implementations,
  428. // hook_schema() in particular, can be called while it is being
  429. // installed.
  430. db_update('system')
  431. ->fields(array('status' => 1))
  432. ->condition('type', 'module')
  433. ->condition('name', $module)
  434. ->execute();
  435. // Refresh the module list to include it.
  436. system_list_reset();
  437. module_list(TRUE);
  438. module_implements('', FALSE, TRUE);
  439. _system_update_bootstrap_status();
  440. // Update the registry to include it.
  441. registry_update();
  442. // Refresh the schema to include it.
  443. drupal_get_schema(NULL, TRUE);
  444. // Update the theme registry to include it.
  445. drupal_theme_rebuild();
  446. // Clear entity cache.
  447. entity_info_cache_clear();
  448. // Now install the module if necessary.
  449. if (drupal_get_installed_schema_version($module, TRUE) == SCHEMA_UNINSTALLED) {
  450. drupal_install_schema($module);
  451. // Set the schema version to the number of the last update provided
  452. // by the module.
  453. $versions = drupal_get_schema_versions($module);
  454. $version = $versions ? max($versions) : SCHEMA_INSTALLED;
  455. // If the module has no current updates, but has some that were
  456. // previously removed, set the version to the value of
  457. // hook_update_last_removed().
  458. if ($last_removed = module_invoke($module, 'update_last_removed')) {
  459. $version = max($version, $last_removed);
  460. }
  461. drupal_set_installed_schema_version($module, $version);
  462. // Allow the module to perform install tasks.
  463. module_invoke($module, 'install');
  464. // Record the fact that it was installed.
  465. $modules_installed[] = $module;
  466. watchdog('system', '%module module installed.', array('%module' => $module), WATCHDOG_INFO);
  467. }
  468. // Enable the module.
  469. module_invoke($module, 'enable');
  470. // Record the fact that it was enabled.
  471. $modules_enabled[] = $module;
  472. watchdog('system', '%module module enabled.', array('%module' => $module), WATCHDOG_INFO);
  473. }
  474. }
  475. // If any modules were newly installed, invoke hook_modules_installed().
  476. if (!empty($modules_installed)) {
  477. module_invoke_all('modules_installed', $modules_installed);
  478. }
  479. // If any modules were newly enabled, invoke hook_modules_enabled().
  480. if (!empty($modules_enabled)) {
  481. module_invoke_all('modules_enabled', $modules_enabled);
  482. }
  483. return TRUE;
  484. }
  485. /**
  486. * Disables a given set of modules.
  487. *
  488. * @param string[] $module_list
  489. * An array of module names.
  490. * @param bool $disable_dependents
  491. * If TRUE, dependent modules will automatically be added and disabled in the
  492. * correct order. This incurs a significant performance cost, so use FALSE
  493. * if you know $module_list is already complete and in the correct order.
  494. *
  495. * @see drupal_uninstall_modules()
  496. * @see module_enable()
  497. */
  498. function module_disable($module_list, $disable_dependents = TRUE) {
  499. if ($disable_dependents) {
  500. // Get all module data so we can find dependents and sort.
  501. $module_data = system_rebuild_module_data();
  502. // Create an associative array with weights as values.
  503. $module_list = array_flip(array_values($module_list));
  504. $profile = drupal_get_profile();
  505. while (list($module) = each($module_list)) {
  506. if (!isset($module_data[$module]) || !$module_data[$module]->status) {
  507. // This module doesn't exist or is already disabled, skip it.
  508. unset($module_list[$module]);
  509. continue;
  510. }
  511. $module_list[$module] = $module_data[$module]->sort;
  512. // Add dependent modules to the list, with a placeholder weight.
  513. // The new modules will be processed as the while loop continues.
  514. foreach ($module_data[$module]->required_by as $dependent => $dependent_data) {
  515. if (!isset($module_list[$dependent]) && $dependent != $profile) {
  516. $module_list[$dependent] = 0;
  517. }
  518. }
  519. }
  520. // Sort the module list by pre-calculated weights.
  521. asort($module_list);
  522. $module_list = array_keys($module_list);
  523. }
  524. $invoke_modules = array();
  525. foreach ($module_list as $module) {
  526. if (module_exists($module)) {
  527. // Check if node_access table needs rebuilding.
  528. if (!node_access_needs_rebuild() && module_hook($module, 'node_grants')) {
  529. node_access_needs_rebuild(TRUE);
  530. }
  531. module_load_install($module);
  532. module_invoke($module, 'disable');
  533. db_update('system')
  534. ->fields(array('status' => 0))
  535. ->condition('type', 'module')
  536. ->condition('name', $module)
  537. ->execute();
  538. $invoke_modules[] = $module;
  539. watchdog('system', '%module module disabled.', array('%module' => $module), WATCHDOG_INFO);
  540. }
  541. }
  542. if (!empty($invoke_modules)) {
  543. // Refresh the module list to exclude the disabled modules.
  544. system_list_reset();
  545. module_list(TRUE);
  546. module_implements('', FALSE, TRUE);
  547. entity_info_cache_clear();
  548. // Invoke hook_modules_disabled before disabling modules,
  549. // so we can still call module hooks to get information.
  550. module_invoke_all('modules_disabled', $invoke_modules);
  551. // Update the registry to remove the newly-disabled module.
  552. registry_update();
  553. _system_update_bootstrap_status();
  554. // Update the theme registry to remove the newly-disabled module.
  555. drupal_theme_rebuild();
  556. }
  557. // If there remains no more node_access module, rebuilding will be
  558. // straightforward, we can do it right now.
  559. if (node_access_needs_rebuild() && count(module_implements('node_grants')) == 0) {
  560. node_access_rebuild();
  561. }
  562. }
  563. /**
  564. * @defgroup hooks Hooks
  565. * @{
  566. * Allow modules to interact with the Drupal core.
  567. *
  568. * Drupal's module system is based on the concept of "hooks". A hook is a PHP
  569. * function that is named foo_bar(), where "foo" is the name of the module
  570. * (whose filename is thus foo.module) and "bar" is the name of the hook. Each
  571. * hook has a defined set of parameters and a specified result type.
  572. *
  573. * To extend Drupal, a module need simply implement a hook. When Drupal wishes
  574. * to allow intervention from modules, it determines which modules implement a
  575. * hook and calls that hook in all enabled modules that implement it.
  576. *
  577. * The available hooks to implement are explained here in the Hooks section of
  578. * the developer documentation. The string "hook" is used as a placeholder for
  579. * the module name in the hook definitions. For example, if the module file is
  580. * called example.module, then hook_help() as implemented by that module would
  581. * be defined as example_help().
  582. *
  583. * The example functions included are not part of the Drupal core, they are
  584. * just models that you can modify. Only the hooks implemented within modules
  585. * are executed when running Drupal.
  586. *
  587. * @see themeable
  588. * @see callbacks
  589. */
  590. /**
  591. * @defgroup callbacks Callbacks
  592. * @{
  593. * Callback function signatures.
  594. *
  595. * Drupal's API sometimes uses callback functions to allow you to define how
  596. * some type of processing happens. A callback is a function with a defined
  597. * signature, which you define in a module. Then you pass the function name as
  598. * a parameter to a Drupal API function or return it as part of a hook
  599. * implementation return value, and your function is called at an appropriate
  600. * time. For instance, when setting up batch processing you might need to
  601. * provide a callback function for each processing step and/or a callback for
  602. * when processing is finished; you would do that by defining these functions
  603. * and passing their names into the batch setup function.
  604. *
  605. * Callback function signatures, like hook definitions, are described by
  606. * creating and documenting dummy functions in a *.api.php file; normally, the
  607. * dummy callback function's name should start with "callback_", and you should
  608. * document the parameters and return value and provide a sample function body.
  609. * Then your API documentation can refer to this callback function in its
  610. * documentation. A user of your API can usually name their callback function
  611. * anything they want, although a standard name would be to replace "callback_"
  612. * with the module name.
  613. *
  614. * @see hooks
  615. * @see themeable
  616. *
  617. * @}
  618. */
  619. /**
  620. * Determines whether a module implements a hook.
  621. *
  622. * @param $module
  623. * The name of the module (without the .module extension).
  624. * @param $hook
  625. * The name of the hook (e.g. "help" or "menu").
  626. *
  627. * @return
  628. * TRUE if the module is both installed and enabled, and the hook is
  629. * implemented in that module.
  630. */
  631. function module_hook($module, $hook) {
  632. $function = $module . '_' . $hook;
  633. if (function_exists($function)) {
  634. return TRUE;
  635. }
  636. // If the hook implementation does not exist, check whether it may live in an
  637. // optional include file registered via hook_hook_info().
  638. $hook_info = module_hook_info();
  639. if (isset($hook_info[$hook]['group'])) {
  640. module_load_include('inc', $module, $module . '.' . $hook_info[$hook]['group']);
  641. if (function_exists($function)) {
  642. return TRUE;
  643. }
  644. }
  645. return FALSE;
  646. }
  647. /**
  648. * Determines which modules are implementing a hook.
  649. *
  650. * Lazy-loaded include files specified with "group" via hook_hook_info() or
  651. * hook_module_implements_alter() will be automatically included by this
  652. * function when necessary.
  653. *
  654. * @param string $hook
  655. * The name of the hook (e.g. "help" or "menu").
  656. * @param bool $sort
  657. * By default, modules are ordered by weight and filename, settings this option
  658. * to TRUE, module list will be ordered by module name.
  659. * @param bool $reset
  660. * For internal use only: Whether to force the stored list of hook
  661. * implementations to be regenerated (such as after enabling a new module,
  662. * before processing hook_enable).
  663. *
  664. * @return
  665. * An array with the names of the modules which are implementing this hook.
  666. *
  667. * @see module_implements_write_cache()
  668. */
  669. function module_implements($hook, $sort = FALSE, $reset = FALSE) {
  670. // Use the advanced drupal_static() pattern, since this is called very often.
  671. static $drupal_static_fast;
  672. if (!isset($drupal_static_fast)) {
  673. $drupal_static_fast['implementations'] = &drupal_static(__FUNCTION__);
  674. $drupal_static_fast['verified'] = &drupal_static(__FUNCTION__ . ':verified');
  675. }
  676. $implementations = &$drupal_static_fast['implementations'];
  677. $verified = &$drupal_static_fast['verified'];
  678. // We maintain a persistent cache of hook implementations in addition to the
  679. // static cache to avoid looping through every module and every hook on each
  680. // request. Benchmarks show that the benefit of this caching outweighs the
  681. // additional database hit even when using the default database caching
  682. // backend and only a small number of modules are enabled. The cost of the
  683. // cache_get() is more or less constant and reduced further when non-database
  684. // caching backends are used, so there will be more significant gains when a
  685. // large number of modules are installed or hooks invoked, since this can
  686. // quickly lead to module_hook() being called several thousand times
  687. // per request.
  688. if ($reset) {
  689. $implementations = array();
  690. $verified = array();
  691. cache_set('module_implements', array(), 'cache_bootstrap');
  692. drupal_static_reset('module_hook_info');
  693. drupal_static_reset('drupal_alter');
  694. cache_clear_all('hook_info', 'cache_bootstrap');
  695. cache_clear_all('system_cache_tables', 'cache');
  696. return;
  697. }
  698. // Fetch implementations from cache.
  699. // This happens on the first call to module_implements(*, *, FALSE) during a
  700. // request, but also when $implementations have been reset, e.g. after
  701. // module_enable().
  702. if (empty($implementations)) {
  703. $implementations = cache_get('module_implements', 'cache_bootstrap');
  704. if ($implementations === FALSE) {
  705. $implementations = array();
  706. }
  707. else {
  708. $implementations = $implementations->data;
  709. }
  710. // Forget all previously "verified" hooks, in case that $implementations
  711. // were cleared via drupal_static_reset('module_implements') instead of
  712. // module_implements(*, *, TRUE).
  713. $verified = array();
  714. }
  715. if (!isset($implementations[$hook])) {
  716. // The hook is not cached, so ensure that whether or not it has
  717. // implementations, that the cache is updated at the end of the request.
  718. $implementations['#write_cache'] = TRUE;
  719. // Discover implementations for this hook.
  720. $hook_info = module_hook_info();
  721. $implementations[$hook] = array();
  722. $list = module_list(FALSE, FALSE, $sort);
  723. foreach ($list as $module) {
  724. $include_file = isset($hook_info[$hook]['group']) && module_load_include('inc', $module, $module . '.' . $hook_info[$hook]['group']);
  725. // Since module_hook() may needlessly try to load the include file again,
  726. // function_exists() is used directly here.
  727. if (function_exists($module . '_' . $hook)) {
  728. $implementations[$hook][$module] = $include_file ? $hook_info[$hook]['group'] : FALSE;
  729. }
  730. }
  731. // Allow modules to change the weight of specific implementations, but avoid
  732. // an infinite loop.
  733. if ($hook != 'module_implements_alter') {
  734. // Remember the implementations before hook_module_implements_alter().
  735. $implementations_before = $implementations[$hook];
  736. drupal_alter('module_implements', $implementations[$hook], $hook);
  737. // Verify implementations that were added or modified.
  738. foreach (array_diff_assoc($implementations[$hook], $implementations_before) as $module => $group) {
  739. // If drupal_alter('module_implements') changed or added a $group, the
  740. // respective file needs to be included.
  741. if ($group) {
  742. module_load_include('inc', $module, "$module.$group");
  743. }
  744. // If a new implementation was added, verify that the function exists.
  745. if (!function_exists($module . '_' . $hook)) {
  746. unset($implementations[$hook][$module]);
  747. }
  748. }
  749. }
  750. // Implementations for this hook are now "verified".
  751. $verified[$hook] = TRUE;
  752. }
  753. elseif (!isset($verified[$hook])) {
  754. // Implementations for this hook were in the cache, but they are not
  755. // "verified" yet.
  756. foreach ($implementations[$hook] as $module => $group) {
  757. // If this hook implementation is stored in a lazy-loaded file, so include
  758. // that file first.
  759. if ($group) {
  760. module_load_include('inc', $module, "$module.$group");
  761. }
  762. // It is possible that a module removed a hook implementation without the
  763. // implementations cache being rebuilt yet, so we check whether the
  764. // function exists on each request to avoid undefined function errors.
  765. // Since module_hook() may needlessly try to load the include file again,
  766. // function_exists() is used directly here.
  767. if (!function_exists($module . '_' . $hook)) {
  768. // Clear out the stale implementation from the cache and force a cache
  769. // refresh to forget about no longer existing hook implementations.
  770. unset($implementations[$hook][$module]);
  771. $implementations['#write_cache'] = TRUE;
  772. }
  773. }
  774. $verified[$hook] = TRUE;
  775. }
  776. return array_keys($implementations[$hook]);
  777. }
  778. /**
  779. * Retrieves a list of hooks that are declared through hook_hook_info().
  780. *
  781. * @return
  782. * An associative array whose keys are hook names and whose values are an
  783. * associative array containing a group name. The structure of the array
  784. * is the same as the return value of hook_hook_info().
  785. *
  786. * @see hook_hook_info()
  787. */
  788. function module_hook_info() {
  789. // This function is indirectly invoked from bootstrap_invoke_all(), in which
  790. // case common.inc, subsystems, and modules are not loaded yet, so it does not
  791. // make sense to support hook groups resp. lazy-loaded include files prior to
  792. // full bootstrap.
  793. if (drupal_bootstrap(NULL, FALSE) != DRUPAL_BOOTSTRAP_FULL) {
  794. return array();
  795. }
  796. $hook_info = &drupal_static(__FUNCTION__);
  797. if (!isset($hook_info)) {
  798. $hook_info = array();
  799. $cache = cache_get('hook_info', 'cache_bootstrap');
  800. if ($cache === FALSE) {
  801. // Rebuild the cache and save it.
  802. // We can't use module_invoke_all() here or it would cause an infinite
  803. // loop.
  804. foreach (module_list() as $module) {
  805. $function = $module . '_hook_info';
  806. if (function_exists($function)) {
  807. $result = $function();
  808. if (isset($result) && is_array($result)) {
  809. $hook_info = array_merge_recursive($hook_info, $result);
  810. }
  811. }
  812. }
  813. // We can't use drupal_alter() for the same reason as above.
  814. foreach (module_list() as $module) {
  815. $function = $module . '_hook_info_alter';
  816. if (function_exists($function)) {
  817. $function($hook_info);
  818. }
  819. }
  820. cache_set('hook_info', $hook_info, 'cache_bootstrap');
  821. }
  822. else {
  823. $hook_info = $cache->data;
  824. }
  825. }
  826. return $hook_info;
  827. }
  828. /**
  829. * Writes the hook implementation cache.
  830. *
  831. * @see module_implements()
  832. */
  833. function module_implements_write_cache() {
  834. // The list of implementations includes vital modules only before full
  835. // bootstrap, so do not write cache if we are not fully bootstrapped yet.
  836. if (drupal_get_bootstrap_phase() != DRUPAL_BOOTSTRAP_FULL) {
  837. return;
  838. }
  839. $implementations = &drupal_static('module_implements');
  840. if (isset($implementations['#write_cache'])) {
  841. unset($implementations['#write_cache']);
  842. cache_set('module_implements', $implementations, 'cache_bootstrap');
  843. }
  844. }
  845. /**
  846. * Invokes a hook in a particular module.
  847. *
  848. * All arguments are passed by value. Use drupal_alter() if you need to pass
  849. * arguments by reference.
  850. *
  851. * @param $module
  852. * The name of the module (without the .module extension).
  853. * @param $hook
  854. * The name of the hook to invoke.
  855. * @param ...
  856. * Arguments to pass to the hook implementation.
  857. *
  858. * @return
  859. * The return value of the hook implementation.
  860. *
  861. * @see drupal_alter()
  862. */
  863. function module_invoke($module, $hook) {
  864. $args = func_get_args();
  865. // Remove $module and $hook from the arguments.
  866. unset($args[0], $args[1]);
  867. if (module_hook($module, $hook)) {
  868. return call_user_func_array($module . '_' . $hook, $args);
  869. }
  870. }
  871. /**
  872. * Invokes a hook in all enabled modules that implement it.
  873. *
  874. * All arguments are passed by value. Use drupal_alter() if you need to pass
  875. * arguments by reference.
  876. *
  877. * @param $hook
  878. * The name of the hook to invoke.
  879. * @param ...
  880. * Arguments to pass to the hook.
  881. *
  882. * @return
  883. * An array of return values of the hook implementations. If modules return
  884. * arrays from their implementations, those are merged into one array
  885. * recursively. Note: integer keys in arrays will be lost, as the merge is
  886. * done using array_merge_recursive().
  887. *
  888. * @see drupal_alter()
  889. */
  890. function module_invoke_all($hook) {
  891. $args = func_get_args();
  892. // Remove $hook from the arguments.
  893. unset($args[0]);
  894. $return = array();
  895. foreach (module_implements($hook) as $module) {
  896. $function = $module . '_' . $hook;
  897. if (function_exists($function)) {
  898. $result = call_user_func_array($function, $args);
  899. if (isset($result) && is_array($result)) {
  900. $return = array_merge_recursive($return, $result);
  901. }
  902. elseif (isset($result)) {
  903. $return[] = $result;
  904. }
  905. }
  906. }
  907. return $return;
  908. }
  909. /**
  910. * @} End of "defgroup hooks".
  911. */
  912. /**
  913. * Returns an array of modules required by core.
  914. */
  915. function drupal_required_modules() {
  916. $files = drupal_system_listing('/^' . DRUPAL_PHP_FUNCTION_PATTERN . '\.info$/', 'modules', 'name', 0);
  917. $required = array();
  918. // An installation profile is required and one must always be loaded.
  919. $required[] = drupal_get_profile();
  920. foreach ($files as $name => $file) {
  921. $info = drupal_parse_info_file($file->uri);
  922. if (!empty($info) && !empty($info['required']) && $info['required']) {
  923. $required[] = $name;
  924. }
  925. }
  926. return $required;
  927. }
  928. /**
  929. * Passes alterable variables to specific hook_TYPE_alter() implementations.
  930. *
  931. * This dispatch function hands off the passed-in variables to type-specific
  932. * hook_TYPE_alter() implementations in modules. It ensures a consistent
  933. * interface for all altering operations.
  934. *
  935. * A maximum of 2 alterable arguments is supported (a third is supported for
  936. * legacy reasons, but should not be used in new code). In case more arguments
  937. * need to be passed and alterable, modules provide additional variables
  938. * assigned by reference in the last $context argument:
  939. * @code
  940. * $context = array(
  941. * 'alterable' => &$alterable,
  942. * 'unalterable' => $unalterable,
  943. * 'foo' => 'bar',
  944. * );
  945. * drupal_alter('mymodule_data', $alterable1, $alterable2, $context);
  946. * @endcode
  947. *
  948. * Note that objects are always passed by reference in PHP5. If it is absolutely
  949. * required that no implementation alters a passed object in $context, then an
  950. * object needs to be cloned:
  951. * @code
  952. * $context = array(
  953. * 'unalterable_object' => clone $object,
  954. * );
  955. * drupal_alter('mymodule_data', $data, $context);
  956. * @endcode
  957. *
  958. * @param $type
  959. * A string describing the type of the alterable $data. 'form', 'links',
  960. * 'node_content', and so on are several examples. Alternatively can be an
  961. * array, in which case hook_TYPE_alter() is invoked for each value in the
  962. * array, ordered first by module, and then for each module, in the order of
  963. * values in $type. For example, when Form API is using drupal_alter() to
  964. * execute both hook_form_alter() and hook_form_FORM_ID_alter()
  965. * implementations, it passes array('form', 'form_' . $form_id) for $type.
  966. * @param $data
  967. * The variable that will be passed to hook_TYPE_alter() implementations to be
  968. * altered. The type of this variable depends on the value of the $type
  969. * argument. For example, when altering a 'form', $data will be a structured
  970. * array. When altering a 'profile', $data will be an object.
  971. * @param $context1
  972. * (optional) An additional variable that is passed by reference.
  973. * @param $context2
  974. * (optional) An additional variable that is passed by reference. If more
  975. * context needs to be provided to implementations, then this should be an
  976. * associative array as described above.
  977. * @param $context3
  978. * (optional) An additional variable that is passed by reference. This
  979. * parameter is deprecated and will not exist in Drupal 8; consequently, it
  980. * should not be used for new Drupal 7 code either. It is here only for
  981. * backwards compatibility with older code that passed additional arguments
  982. * to drupal_alter().
  983. */
  984. function drupal_alter($type, &$data, &$context1 = NULL, &$context2 = NULL, &$context3 = NULL) {
  985. // Use the advanced drupal_static() pattern, since this is called very often.
  986. static $drupal_static_fast;
  987. if (!isset($drupal_static_fast)) {
  988. $drupal_static_fast['functions'] = &drupal_static(__FUNCTION__);
  989. }
  990. $functions = &$drupal_static_fast['functions'];
  991. // Most of the time, $type is passed as a string, so for performance,
  992. // normalize it to that. When passed as an array, usually the first item in
  993. // the array is a generic type, and additional items in the array are more
  994. // specific variants of it, as in the case of array('form', 'form_FORM_ID').
  995. if (is_array($type)) {
  996. $cid = implode(',', $type);
  997. $extra_types = $type;
  998. $type = array_shift($extra_types);
  999. // Allow if statements in this function to use the faster isset() rather
  1000. // than !empty() both when $type is passed as a string, or as an array with
  1001. // one item.
  1002. if (empty($extra_types)) {
  1003. unset($extra_types);
  1004. }
  1005. }
  1006. else {
  1007. $cid = $type;
  1008. }
  1009. // Some alter hooks are invoked many times per page request, so statically
  1010. // cache the list of functions to call, and on subsequent calls, iterate
  1011. // through them quickly.
  1012. if (!isset($functions[$cid])) {
  1013. $functions[$cid] = array();
  1014. $hook = $type . '_alter';
  1015. $modules = module_implements($hook);
  1016. if (!isset($extra_types)) {
  1017. // For the more common case of a single hook, we do not need to call
  1018. // function_exists(), since module_implements() returns only modules with
  1019. // implementations.
  1020. foreach ($modules as $module) {
  1021. $functions[$cid][] = $module . '_' . $hook;
  1022. }
  1023. }
  1024. else {
  1025. // For multiple hooks, we need $modules to contain every module that
  1026. // implements at least one of them.
  1027. $extra_modules = array();
  1028. foreach ($extra_types as $extra_type) {
  1029. $extra_modules = array_merge($extra_modules, module_implements($extra_type . '_alter'));
  1030. }
  1031. // If any modules implement one of the extra hooks that do not implement
  1032. // the primary hook, we need to add them to the $modules array in their
  1033. // appropriate order. module_implements() can only return ordered
  1034. // implementations of a single hook. To get the ordered implementations
  1035. // of multiple hooks, we mimic the module_implements() logic of first
  1036. // ordering by module_list(), and then calling
  1037. // drupal_alter('module_implements').
  1038. if (array_diff($extra_modules, $modules)) {
  1039. // Merge the arrays and order by module_list().
  1040. $modules = array_intersect(module_list(), array_merge($modules, $extra_modules));
  1041. // Since module_implements() already took care of loading the necessary
  1042. // include files, we can safely pass FALSE for the array values.
  1043. $implementations = array_fill_keys($modules, FALSE);
  1044. // Let modules adjust the order solely based on the primary hook. This
  1045. // ensures the same module order regardless of whether this if block
  1046. // runs. Calling drupal_alter() recursively in this way does not result
  1047. // in an infinite loop, because this call is for a single $type, so we
  1048. // won't end up in this code block again.
  1049. drupal_alter('module_implements', $implementations, $hook);
  1050. $modules = array_keys($implementations);
  1051. }
  1052. foreach ($modules as $module) {
  1053. // Since $modules is a merged array, for any given module, we do not
  1054. // know whether it has any particular implementation, so we need a
  1055. // function_exists().
  1056. $function = $module . '_' . $hook;
  1057. if (function_exists($function)) {
  1058. $functions[$cid][] = $function;
  1059. }
  1060. foreach ($extra_types as $extra_type) {
  1061. $function = $module . '_' . $extra_type . '_alter';
  1062. if (function_exists($function)) {
  1063. $functions[$cid][] = $function;
  1064. }
  1065. }
  1066. }
  1067. }
  1068. // Allow the theme to alter variables after the theme system has been
  1069. // initialized.
  1070. global $theme, $base_theme_info;
  1071. if (isset($theme)) {
  1072. $theme_keys = array();
  1073. foreach ($base_theme_info as $base) {
  1074. $theme_keys[] = $base->name;
  1075. }
  1076. $theme_keys[] = $theme;
  1077. foreach ($theme_keys as $theme_key) {
  1078. $function = $theme_key . '_' . $hook;
  1079. if (function_exists($function)) {
  1080. $functions[$cid][] = $function;
  1081. }
  1082. if (isset($extra_types)) {
  1083. foreach ($extra_types as $extra_type) {
  1084. $function = $theme_key . '_' . $extra_type . '_alter';
  1085. if (function_exists($function)) {
  1086. $functions[$cid][] = $function;
  1087. }
  1088. }
  1089. }
  1090. }
  1091. }
  1092. }
  1093. foreach ($functions[$cid] as $function) {
  1094. $function($data, $context1, $context2, $context3);
  1095. }
  1096. }