module.inc 43 KB

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