module.inc 42 KB

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