module.inc 43 KB

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