PageRenderTime 73ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/features/features.module

https://bitbucket.org/hjain/trinet
Unknown | 896 lines | 812 code | 84 blank | 0 comment | 0 complexity | 2fcb5c338886da24fe7c8c685179275c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, AGPL-1.0
  1. <?php
  2. /**
  3. * @file
  4. * Module file for the features module, which enables the capture and
  5. * management of features in Drupal. A feature is a collection of Drupal
  6. * entities which taken together statisfy a certain use-case.
  7. */
  8. define('FEATURES_MODULE_ENABLED', 1);
  9. define('FEATURES_MODULE_DISABLED', 0);
  10. define('FEATURES_MODULE_MISSING', -1);
  11. define('FEATURES_MODULE_CONFLICT', 2);
  12. define('FEATURES_REBUILDABLE', -1);
  13. define('FEATURES_DEFAULT', 0);
  14. define('FEATURES_OVERRIDDEN', 1);
  15. define('FEATURES_NEEDS_REVIEW', 2);
  16. define('FEATURES_REBUILDING', 3);
  17. define('FEATURES_CONFLICT', 4);
  18. define('FEATURES_DISABLED', 5);
  19. define('FEATURES_CHECKING', 6);
  20. define('FEATURES_ALTER_TYPE_NORMAL', 'normal');
  21. define('FEATURES_ALTER_TYPE_INLINE', 'inline');
  22. define('FEATURES_ALTER_TYPE_NONE', 'none');
  23. // Duration of rebuild semaphore: 10 minutes.
  24. define('FEATURES_SEMAPHORE_TIMEOUT', 10 * 60);
  25. /**
  26. * Components with this 'default_file' flag will have exports written to the
  27. * common defaults file 'MODULENAME.features.inc'. This is the default
  28. * behavior.
  29. */
  30. define('FEATURES_DEFAULTS_INCLUDED_COMMON', 0);
  31. /**
  32. * Components with this 'default_file' flag will have exports written to a
  33. * defaults based on the component name like 'MODULENAME.features.COMPONENT-NAME.inc'.
  34. * Any callers to this component's defaults hook must call
  35. * features_include_defaults('component') in order to include this file.
  36. */
  37. define('FEATURES_DEFAULTS_INCLUDED', 1);
  38. /**
  39. * Components with this 'default_file' flag must specify a filename for their
  40. * exports. Additionally a stub will NOT be written to 'MODULENAME.features.inc'
  41. * allowing the file to be included directly by the implementing module.
  42. */
  43. define('FEATURES_DEFAULTS_CUSTOM', 2);
  44. /**
  45. * Components with this 'duplicates' flag may not have multiple features provide the
  46. * same component key in their info files. This is the default behavior.
  47. */
  48. define('FEATURES_DUPLICATES_CONFLICT', 0);
  49. /**
  50. * Components with this 'duplicates' flag are allowed to have multiple features
  51. * provide the same component key in their info files.
  52. */
  53. define('FEATURES_DUPLICATES_ALLOWED', 1);
  54. /**
  55. * Implements hook_menu().
  56. */
  57. function features_menu() {
  58. $items = array();
  59. $items['admin/structure/features'] = array(
  60. 'title' => 'Features',
  61. 'description' => 'Manage features.',
  62. 'page callback' => 'drupal_get_form',
  63. 'page arguments' => array('features_admin_form'),
  64. 'type' => MENU_NORMAL_ITEM,
  65. 'file' => 'features.admin.inc',
  66. );
  67. $items['admin/structure/features/cleanup'] = array(
  68. 'title' => 'Cleanup',
  69. 'description' => 'Detect and disable any orphaned feature dependencies.',
  70. 'page callback' => 'drupal_get_form',
  71. 'page arguments' => array('features_cleanup_form', 4),
  72. 'type' => MENU_CALLBACK,
  73. 'file' => 'features.admin.inc',
  74. 'weight' => 1,
  75. );
  76. $items['admin/structure/features/manage'] = array(
  77. 'title' => 'Manage',
  78. 'description' => 'Enable and disable features.',
  79. 'page callback' => 'drupal_get_form',
  80. 'page arguments' => array('features_admin_form'),
  81. 'type' => MENU_DEFAULT_LOCAL_TASK,
  82. 'file' => 'features.admin.inc',
  83. );
  84. $items['admin/structure/features/create'] = array(
  85. 'title' => 'Create feature',
  86. 'description' => 'Create a new feature.',
  87. 'page callback' => 'drupal_get_form',
  88. 'page arguments' => array('features_export_form'),
  89. 'access callback' => 'user_access',
  90. 'access arguments' => array('administer features'),
  91. 'type' => MENU_LOCAL_TASK,
  92. 'file' => "features.admin.inc",
  93. 'weight' => 10,
  94. );
  95. $items['admin/structure/features/%feature'] = array(
  96. 'title callback' => 'features_get_feature_title',
  97. 'title arguments' => array(3),
  98. 'description' => 'Display components of a feature.',
  99. 'page callback' => 'drupal_get_form',
  100. 'page arguments' => array('features_admin_components', 3),
  101. 'load arguments' => array(3, TRUE),
  102. 'access callback' => 'user_access',
  103. 'access arguments' => array('administer features'),
  104. 'type' => MENU_CALLBACK,
  105. 'file' => 'features.admin.inc',
  106. );
  107. $items['admin/structure/features/%feature/view'] = array(
  108. 'title' => 'View',
  109. 'description' => 'Display components of a feature.',
  110. 'access callback' => 'user_access',
  111. 'access arguments' => array('administer features'),
  112. 'type' => MENU_DEFAULT_LOCAL_TASK,
  113. 'weight' => -10,
  114. );
  115. $items['admin/structure/features/%feature/recreate'] = array(
  116. 'title' => 'Recreate',
  117. 'description' => 'Recreate an existing feature.',
  118. 'page callback' => 'drupal_get_form',
  119. 'page arguments' => array('features_export_form', 3),
  120. 'load arguments' => array(3, TRUE),
  121. 'access callback' => 'user_access',
  122. 'access arguments' => array('administer features'),
  123. 'type' => MENU_LOCAL_TASK,
  124. 'file' => "features.admin.inc",
  125. 'weight' => 11,
  126. );
  127. if (module_exists('diff')) {
  128. $items['admin/structure/features/%feature/diff'] = array(
  129. 'title' => 'Review overrides',
  130. 'description' => 'Compare default and current feature.',
  131. 'page callback' => 'features_feature_diff',
  132. 'page arguments' => array(3, 5),
  133. 'load arguments' => array(3, TRUE),
  134. 'access callback' => 'features_access_override_actions',
  135. 'access arguments' => array(3),
  136. 'type' => MENU_LOCAL_TASK,
  137. 'file' => 'features.admin.inc',
  138. );
  139. }
  140. $items['admin/structure/features/%feature/status'] = array(
  141. 'title' => 'Status',
  142. 'description' => 'Javascript status call back.',
  143. 'page callback' => 'features_feature_status',
  144. 'page arguments' => array(3),
  145. 'load arguments' => array(3, TRUE),
  146. 'access callback' => 'user_access',
  147. 'access arguments' => array('administer features'),
  148. 'type' => MENU_CALLBACK,
  149. 'file' => 'features.admin.inc',
  150. );
  151. $items['features/autocomplete/packages'] = array(
  152. 'page callback' => 'features_autocomplete_packages',
  153. 'access arguments' => array('administer features'),
  154. 'type' => MENU_CALLBACK,
  155. 'file' => 'features.admin.inc',
  156. );
  157. foreach ($items as $path => $item) {
  158. if (!isset($item['access callback'])) {
  159. $items[$path]['access callback'] = 'user_access';
  160. $items[$path]['access arguments'] = array('manage features');
  161. }
  162. }
  163. return $items;
  164. }
  165. /**
  166. * Implements hook_theme().
  167. */
  168. function features_theme() {
  169. $base = array(
  170. 'path' => drupal_get_path('module', 'features') . '/theme',
  171. 'file' => 'theme.inc',
  172. );
  173. $items = array();
  174. $items['features_module_status'] = array(
  175. 'variables' => array('module' => null, 'status' => null)
  176. ) + $base;
  177. $items['features_components'] = array(
  178. 'variables' => array('info' => null, 'sources' => null),
  179. ) + $base;
  180. $items['features_component_key'] = $base;
  181. $items['features_component_list'] = array(
  182. 'variables' => array('components' => array(), 'source' => array(), 'conflicts' => array()),
  183. ) + $base;
  184. $items['features_storage_link'] = array(
  185. 'variables' => array('storage' => null, 'text' => null, 'path' => null, 'options' => array()),
  186. ) + $base;
  187. $items['features_form_components'] =
  188. $items['features_form_export'] =
  189. $items['features_form_package'] = array(
  190. 'render element' => 'form',
  191. ) + $base;
  192. $items['features_form_buttons'] = array(
  193. 'render element' => 'element',
  194. ) + $base;
  195. $items['features_admin_components'] = array(
  196. 'render element' => 'form',
  197. 'template' => 'features-admin-components',
  198. ) + $base;
  199. return $items;
  200. }
  201. /**
  202. * Implements hook_flush_caches().
  203. */
  204. function features_flush_caches() {
  205. features_rebuild();
  206. features_get_modules(NULL, TRUE);
  207. return array();
  208. }
  209. /**
  210. * Implements hook_form().
  211. */
  212. function features_form($node, $form_state) {
  213. return node_content_form($node, $form_state);
  214. }
  215. /**
  216. * Implemenation of hook_node_access()
  217. */
  218. function features_node_access($node, $op, $account) {
  219. return node_node_access($node, $op, $account);
  220. }
  221. /**
  222. * Implements hook_permission().
  223. */
  224. function features_permission() {
  225. return array(
  226. 'administer features' => array(
  227. 'title' => t('Administer features'),
  228. 'description' => t('Perform administration tasks on features.'),
  229. ),
  230. 'manage features' => array(
  231. 'title' => t('Manage features'),
  232. 'description' => t('View, enable and disable features.'),
  233. ),
  234. );
  235. }
  236. /**
  237. * Implements hook_help().
  238. */
  239. function features_help($path, $arg) {
  240. switch ($path) {
  241. case 'admin/help#features':
  242. $output = file_get_contents(drupal_get_path('module', 'features') .'/README.txt');
  243. return module_exists('markdown') ? filter_xss_admin(module_invoke('markdown', 'filter', 'process', 0, -1, $output)) : '<pre>'. check_plain($output) .'</pre>';
  244. case 'admin/build/features':
  245. return '<p>'. t('A "Feature" is a certain type of Drupal module which contains a package of configuration that, when enabled, provides a new set of functionality for your Drupal site. Enable features by selecting the checkboxes below and clicking the Save configuration button. If the configuration of the feature has been changed its "State" will be either "overridden" or "needs review", otherwise it will be "default", indicating that the configuration has not been changed. Click on the state to see more details about the feature and its components.') .'</p>';
  246. }
  247. }
  248. /**
  249. * Implements hook_modules_disabled().
  250. */
  251. function features_modules_disabled($modules) {
  252. // Find any features modules that were disabled.
  253. if ($modules = array_intersect(array_keys(features_get_features()), $modules)) {
  254. foreach ($modules as $module) {
  255. features_disable_feature($module);
  256. }
  257. }
  258. }
  259. /**
  260. * Implements hook_modules_enabled().
  261. */
  262. function features_modules_enabled($modules) {
  263. // Find any features modules that were enabled.
  264. if ($modules = array_intersect(array_keys(features_get_features()), $modules)) {
  265. foreach ($modules as $module) {
  266. features_enable_feature($module);
  267. }
  268. }
  269. }
  270. /**
  271. * Load includes for any modules that implement the features API and
  272. * load includes for those provided by features.
  273. */
  274. function features_include($reset = FALSE) {
  275. static $once;
  276. if (!isset($once) || $reset) {
  277. $once = TRUE;
  278. // Features provides integration on behalf of these modules.
  279. // The features include provides handling for the feature dependencies.
  280. // Note that ctools is placed last because it implements hooks "dynamically" for other modules.
  281. $modules = array('features', 'block', 'context', 'field', 'filter', 'image', 'locale', 'menu', 'node', 'taxonomy', 'user', 'views', 'ctools');
  282. foreach (array_filter($modules, 'module_exists') as $module) {
  283. module_load_include('inc', 'features', "includes/features.$module");
  284. }
  285. if (module_exists('ctools')) {
  286. // Finally, add ctools eval'd implementations.
  287. ctools_features_declare_functions();
  288. }
  289. // Clear static cache, since we've now included new implementers.
  290. foreach (features_get_components(NULL, 'file') as $file) {
  291. require_once DRUPAL_ROOT . '/' . $file;
  292. }
  293. }
  294. }
  295. /**
  296. * Load features includes for all components that require includes before
  297. * collecting defaults.
  298. */
  299. function features_include_defaults($components = NULL, $reset = FALSE) {
  300. static $included = array();
  301. static $include_components;
  302. // Build an array of components that require inclusion:
  303. // Views, CTools components and those using FEATURES_DEFAULTS_INCLUDED.
  304. if (!isset($include_components) || $reset) {
  305. $include_components = features_get_components();
  306. foreach ($include_components as $component => $info) {
  307. if ($component !== 'views' && !isset($info['api']) && (!isset($info['default_file']) || $info['default_file'] !== FEATURES_DEFAULTS_INCLUDED)) {
  308. unset($include_components[$component]);
  309. }
  310. }
  311. }
  312. // If components are specified, only include for the specified components.
  313. if (isset($components)) {
  314. $components = is_array($components) ? $components : array($components);
  315. }
  316. // Use all include components if none are explicitly specified.
  317. else {
  318. $components = $include_components;
  319. }
  320. foreach ($components as $component) {
  321. if (isset($include_components[$component]) && (!isset($included[$component]) || $reset)) {
  322. $info = $include_components[$component];
  323. // Inclusion of defaults for Views.
  324. if (isset($info['api'], $info['module'], $info['current_version'])) {
  325. ctools_include('plugins');
  326. ctools_plugin_api_include($info['module'], $info['api'], $info['current_version'], $info['current_version']);
  327. }
  328. // Inclusion of defaults for components using FEATURES_DEFAULTS_INCLUDED.
  329. else {
  330. $features = isset($features) ? $features : features_get_features(NULL, $reset);
  331. foreach ($features as $feature) {
  332. module_load_include('inc', $feature->name, "{$feature->name}.features.{$component}");
  333. }
  334. }
  335. $included[$component] = TRUE;
  336. }
  337. }
  338. }
  339. /**
  340. * Feature object loader.
  341. */
  342. function feature_load($name, $reset = FALSE) {
  343. return features_get_features($name, $reset);
  344. }
  345. /**
  346. * Return a module 'object' including .info information.
  347. *
  348. * @param $name
  349. * The name of the module to retrieve information for. If ommitted,
  350. * an array of all available modules will be returned.
  351. * @param $reset
  352. * Whether to reset the cache.
  353. *
  354. * @return
  355. * If a module is request (and exists) a module object is returned. If no
  356. * module is requested info for all modules is returned.
  357. */
  358. function features_get_modules($name = NULL, $reset = FALSE) {
  359. return features_get_info('module', $name, $reset);
  360. }
  361. /**
  362. * Returns the array of supported components.
  363. *
  364. * @see hook_features_api
  365. *
  366. * @param $component
  367. * A specific type of component that supports features.
  368. * @param $key
  369. * A key that hook_features_api supports.
  370. *
  371. * @return An array of component labels keyed by the component names.
  372. */
  373. function features_get_components($component = NULL, $key = NULL, $reset = FALSE) {
  374. features_include();
  375. $components = &drupal_static(__FUNCTION__);
  376. $component_by_key = &drupal_static(__FUNCTION__ . '_by_key');
  377. if ($reset || !isset($components) || !isset($component_by_key)) {
  378. $components = $component_by_key = array();
  379. if (!$reset && ($cache = cache_get('features_api'))) {
  380. $components = $cache->data;
  381. }
  382. else {
  383. $components = module_invoke_all('features_api');
  384. drupal_alter('features_api', $components);
  385. cache_set('features_api', $components);
  386. }
  387. foreach ($components as $component_type => $component_information) {
  388. foreach ($component_information as $component_key => $component_value) {
  389. $component_by_key[$component_key][$component_type] = $component_value;
  390. }
  391. }
  392. }
  393. if ($key && $component) {
  394. return !empty($components[$component][$key]) ? $components[$component][$key] : NULL;
  395. }
  396. elseif ($key) {
  397. return !empty($component_by_key[$key]) ? $component_by_key[$key] : array();
  398. }
  399. elseif ($component) {
  400. return $components[$component];
  401. }
  402. return $components;
  403. }
  404. /**
  405. * Returns components that are offered as an option on feature creation.
  406. */
  407. function features_get_feature_components() {
  408. return array_intersect_key(features_get_components(), array_filter(features_get_components(NULL, 'feature_source')));
  409. }
  410. /**
  411. * Invoke a component callback.
  412. */
  413. function features_invoke($component, $callback) {
  414. $args = func_get_args();
  415. unset($args[0], $args[1]);
  416. // Append the component name to the arguments.
  417. $args[] = $component;
  418. if ($function = features_hook($component, $callback)) {
  419. return call_user_func_array($function, $args);
  420. }
  421. }
  422. /**
  423. * Checks whether a component implements the given hook.
  424. *
  425. * @return
  426. * The function implementing the hook, or FALSE.
  427. */
  428. function features_hook($component, $hook, $reset = FALSE) {
  429. // Determine the function callback base.
  430. $base = features_get_components($component, 'base');
  431. $base = isset($base) ? $base : $component;
  432. return function_exists($base . '_' . $hook) ? $base . '_' . $hook : FALSE;
  433. }
  434. /**
  435. * Enables and installs an array of modules, ignoring those
  436. * already enabled & installed. Consider this a helper or
  437. * extension to drupal_install_modules().
  438. *
  439. * @param $modules
  440. * An array of modules to install.
  441. * @param $reset
  442. * Clear the module info cache.
  443. */
  444. function features_install_modules($modules) {
  445. module_load_include('inc', 'features', 'features.export');
  446. $files = system_rebuild_module_data();
  447. // Build maximal list of dependencies.
  448. $install = array();
  449. foreach ($modules as $name) {
  450. // Parse the dependency string into the module name and version information.
  451. $parsed_name = drupal_parse_dependency($name);
  452. $name = $parsed_name['name'];
  453. if ($file = $files[$name]) {
  454. $install[] = $name;
  455. if (!empty($file->info['dependencies'])) {
  456. $install = array_merge($install, _features_export_maximize_dependencies($file->info['dependencies']));
  457. }
  458. }
  459. }
  460. // Filter out enabled modules.
  461. $enabled = array_filter($install, 'module_exists');
  462. $install = array_diff($install, $enabled);
  463. if (!empty($install)) {
  464. // Make sure the install API is available.
  465. $install = array_unique($install);
  466. include_once DRUPAL_ROOT . '/' . './includes/install.inc';
  467. module_enable($install);
  468. }
  469. }
  470. /**
  471. * Wrapper around features_get_info() that returns an array
  472. * of module info objects that are features.
  473. */
  474. function features_get_features($name = NULL, $reset = FALSE) {
  475. return features_get_info('feature', $name, $reset);
  476. }
  477. /**
  478. * Helper for retrieving info from system table.
  479. */
  480. function features_get_info($type = 'module', $name = NULL, $reset = FALSE) {
  481. static $cache;
  482. if (!isset($cache)) {
  483. $cache = cache_get('features_module_info');
  484. }
  485. if (empty($cache) || $reset) {
  486. $data = array();
  487. $ignored = variable_get('features_ignored_orphans', array());
  488. $files = system_rebuild_module_data();
  489. // Filter out intentionally hidden features.
  490. module_load_include('inc', 'features', 'features.admin');
  491. $files = array_filter($files, 'features_filter_hidden');
  492. foreach ($files as $row) {
  493. // If module is no longer enabled, remove it from the ignored orphans list.
  494. if (in_array($row->name, $ignored, TRUE) && !$row->status) {
  495. $key = array_search($row->name, $ignored, TRUE);
  496. unset($ignored[$key]);
  497. }
  498. if (!empty($row->info['features'])) {
  499. // Fix css/js paths
  500. if (!empty($row->info['stylesheets'])) {
  501. foreach($row->info['stylesheets'] as $media => $css) {
  502. $row->info['stylesheets'][$media] = array_keys($css);
  503. }
  504. }
  505. if (!empty($row->info['scripts'])) {
  506. $row->info['scripts'] = array_keys($row->info['scripts']);
  507. }
  508. $data['feature'][$row->name] = $row;
  509. }
  510. $data['module'][$row->name] = $row;
  511. }
  512. // Sort features according to dependencies.
  513. // @see install_profile_modules()
  514. $required = array();
  515. $non_required = array();
  516. $modules = array_keys($data['feature']);
  517. foreach ($modules as $module) {
  518. if ($files[$module]->requires) {
  519. $modules = array_merge($modules, array_keys($files[$module]->requires));
  520. }
  521. }
  522. $modules = array_unique($modules);
  523. foreach ($modules as $module) {
  524. if (!empty($files[$module]->info['features'])) {
  525. if (!empty($files[$module]->info['required'])) {
  526. $required[$module] = $files[$module]->sort;
  527. }
  528. else {
  529. $non_required[$module] = $files[$module]->sort;
  530. }
  531. }
  532. }
  533. arsort($required);
  534. arsort($non_required);
  535. $sorted = array();
  536. foreach ($required + $non_required as $module => $weight) {
  537. $sorted[$module] = $data['feature'][$module];
  538. }
  539. $data['feature'] = $sorted;
  540. variable_set('features_ignored_orphans', $ignored);
  541. cache_set("features_module_info", $data);
  542. $cache = new stdClass();
  543. $cache->data = $data;
  544. }
  545. if (!empty($name)) {
  546. return !empty($cache->data[$type][$name]) ? clone $cache->data[$type][$name] : array();
  547. }
  548. return !empty($cache->data[$type]) ? $cache->data[$type] : array();
  549. }
  550. /**
  551. * Generate an array of feature dependencies that have been orphaned.
  552. */
  553. function features_get_orphans($reset = FALSE) {
  554. static $orphans;
  555. if (!isset($orphans) || $reset) {
  556. module_load_include('inc', 'features', 'features.export');
  557. $orphans = array();
  558. // Build a list of all dependencies for enabled and disabled features.
  559. $dependencies = array('enabled' => array(), 'disabled' => array());
  560. $features = features_get_features();
  561. foreach ($features as $feature) {
  562. $key = module_exists($feature->name) ? 'enabled' : 'disabled';
  563. if (!empty($feature->info['dependencies'])) {
  564. $dependencies[$key] = array_merge($dependencies[$key], _features_export_maximize_dependencies($feature->info['dependencies']));
  565. }
  566. }
  567. $dependencies['enabled'] = array_unique($dependencies['enabled']);
  568. $dependencies['disabled'] = array_unique($dependencies['disabled']);
  569. // Find the list of orphaned modules.
  570. $orphaned = array_diff($dependencies['disabled'], $dependencies['enabled']);
  571. $orphaned = array_intersect($orphaned, module_list(FALSE, FALSE));
  572. $orphaned = array_diff($orphaned, drupal_required_modules());
  573. $orphaned = array_diff($orphaned, array('features'));
  574. // Build final list of modules that can be disabled.
  575. $modules = features_get_modules(NULL, TRUE);
  576. $enabled = module_list();
  577. _module_build_dependencies($modules);
  578. foreach ($orphaned as $module) {
  579. if (!empty($modules[$module]->required_by)) {
  580. // Determine whether any dependents are actually enabled.
  581. $dependents = array_intersect($modules[$module]->required_by, $enabled);
  582. if (empty($dependents)) {
  583. $info = features_get_modules($module);
  584. $orphans[$module] = $info;
  585. }
  586. }
  587. }
  588. }
  589. return $orphans;
  590. }
  591. /**
  592. * Detect potential conflicts between any features that provide
  593. * identical components.
  594. */
  595. function features_get_conflicts($reset = FALSE) {
  596. $conflicts = array();
  597. $component_info = features_get_components();
  598. $map = features_get_component_map(NULL, $reset);
  599. foreach ($map as $type => $components) {
  600. // Only check conflicts for components we know about.
  601. if (isset($component_info[$type])) {
  602. foreach ($components as $component => $modules) {
  603. if (isset($component_info[$type]['duplicates']) && $component_info[$type]['duplicates'] == FEATURES_DUPLICATES_ALLOWED) {
  604. continue;
  605. }
  606. else if (count($modules) > 1) {
  607. foreach ($modules as $module) {
  608. if (!isset($conflicts[$module])) {
  609. $conflicts[$module] = array();
  610. }
  611. foreach ($modules as $m) {
  612. if ($m != $module) {
  613. $conflicts[$module][$m][$type][] = $component;
  614. }
  615. }
  616. }
  617. }
  618. }
  619. }
  620. }
  621. return $conflicts;
  622. }
  623. /**
  624. * Provide a component to feature map.
  625. */
  626. function features_get_component_map($key = NULL, $reset = FALSE) {
  627. static $map;
  628. if (!isset($map) || $reset) {
  629. $map = array();
  630. $features = features_get_features(NULL, $reset);
  631. foreach ($features as $feature) {
  632. foreach ($feature->info['features'] as $type => $components) {
  633. if (!isset($map[$type])) {
  634. $map[$type] = array();
  635. }
  636. foreach ($components as $component) {
  637. $map[$type][$component][] = $feature->name;
  638. }
  639. }
  640. }
  641. }
  642. if (isset($key)) {
  643. return isset($map[$key]) ? $map[$key] : array();
  644. }
  645. return $map;
  646. }
  647. /**
  648. * Simple wrapper returns the status of a module.
  649. */
  650. function features_get_module_status($module) {
  651. if (module_exists($module)) {
  652. return FEATURES_MODULE_ENABLED;
  653. }
  654. else if (features_get_modules($module)) {
  655. return FEATURES_MODULE_DISABLED;
  656. }
  657. else {
  658. return FEATURES_MODULE_MISSING;
  659. }
  660. }
  661. /**
  662. * Menu title callback.
  663. */
  664. function features_get_feature_title($feature) {
  665. return $feature->info['name'];
  666. }
  667. /**
  668. * Menu access callback for whether a user should be able to access
  669. * override actions for a given feature.
  670. */
  671. function features_access_override_actions($feature) {
  672. if (user_access('administer features')) {
  673. static $access = array();
  674. if (!isset($access[$feature->name])) {
  675. // Set a value first. We may get called again from within features_detect_overrides().
  676. $access[$feature->name] = FALSE;
  677. features_include();
  678. module_load_include('inc', 'features', 'features.export');
  679. $access[$feature->name] = in_array(features_get_storage($feature->name), array(FEATURES_OVERRIDDEN, FEATURES_NEEDS_REVIEW)) && user_access('administer features');
  680. }
  681. return $access[$feature->name];
  682. }
  683. return FALSE;
  684. }
  685. /**
  686. * Implements hook_form_alter() for system_modules form().
  687. */
  688. function features_form_system_modules_alter(&$form) {
  689. features_rebuild();
  690. }
  691. /**
  692. * Restore the specified modules to the default state.
  693. */
  694. function _features_restore($op, $items = array()) {
  695. module_load_include('inc', 'features', 'features.export');
  696. features_include();
  697. switch ($op) {
  698. case 'revert':
  699. $restore_states = array(FEATURES_OVERRIDDEN, FEATURES_REBUILDABLE, FEATURES_NEEDS_REVIEW);
  700. $restore_hook = 'features_revert';
  701. $log_action = 'Revert';
  702. break;
  703. case 'rebuild':
  704. $restore_states = array(FEATURES_REBUILDABLE);
  705. $restore_hook = 'features_rebuild';
  706. $log_action = 'Rebuild';
  707. break;
  708. case 'disable':
  709. $restore_hook = 'features_disable_feature';
  710. $log_action = 'Disable';
  711. break;
  712. case 'enable':
  713. $restore_hook = 'features_enable_feature';
  714. $log_action = 'Enable';
  715. break;
  716. }
  717. if (empty($items)) {
  718. // Drush may execute a whole chain of commands that may trigger feature
  719. // rebuilding multiple times during a single request. Make sure we do not
  720. // rebuild the same cached list of modules over and over again by setting
  721. // $reset to TRUE.
  722. // Note: this may happen whenever more than one feature will be enabled
  723. // in chain, for example also using features_install_modules().
  724. $states = features_get_component_states(array(), ($op == 'rebuild'), defined('DRUSH_BASE_PATH'));
  725. foreach ($states as $module_name => $components) {
  726. foreach ($components as $component => $state) {
  727. if (in_array($state, $restore_states)) {
  728. $items[$module_name][] = $component;
  729. }
  730. }
  731. }
  732. }
  733. foreach ($items as $module_name => $components) {
  734. foreach ($components as $component) {
  735. if (features_hook($component, $restore_hook)) {
  736. // Set a semaphore to prevent other instances of the same script from running concurrently.
  737. watchdog('features', '@actioning @module_name / @component.', array('@action' => $log_action, '@component' => $component, '@module_name' => $module_name));
  738. features_semaphore('set', $component);
  739. features_invoke($component, $restore_hook, $module_name);
  740. // If the script completes, remove the semaphore and set the code signature.
  741. features_semaphore('del', $component);
  742. features_set_signature($module_name, $component);
  743. watchdog('features', '@action completed for @module_name / @component.', array('@action' => $log_action, '@component' => $component, '@module_name' => $module_name));
  744. }
  745. }
  746. }
  747. }
  748. /**
  749. * Wrapper around _features_restore().
  750. */
  751. function features_revert($revert = array()) {
  752. return _features_restore('revert', $revert);
  753. }
  754. /**
  755. * Wrapper around _features_restore().
  756. */
  757. function features_rebuild($rebuild = array()) {
  758. return _features_restore('rebuild', $rebuild);
  759. }
  760. /**
  761. * Wrapper around _features_restore().
  762. */
  763. function features_disable_feature($module) {
  764. $feature = feature_load($module);
  765. $items[$module] = array_keys($feature->info['features']);
  766. return _features_restore('disable', $items);
  767. }
  768. /**
  769. * Wrapper around _features_restore().
  770. */
  771. function features_enable_feature($module) {
  772. $feature = feature_load($module);
  773. $items[$module] = array_keys($feature->info['features']);
  774. return _features_restore('enable', $items);
  775. }
  776. /**
  777. * Utility functions ==================================================
  778. */
  779. /**
  780. * Log a message, environment agnostic.
  781. *
  782. * @param $message
  783. * The message to log.
  784. * @param $severity
  785. * The severity of the message: status, warning or error.
  786. */
  787. function features_log($message, $severity = 'status') {
  788. if (function_exists('drush_verify_cli')) {
  789. $message = strip_tags($message);
  790. if ($severity == 'status') {
  791. $severity = 'ok';
  792. }
  793. elseif ($severity == 'error') {
  794. drush_set_error($message);
  795. return;
  796. }
  797. drush_log($message, $severity);
  798. return;
  799. }
  800. drupal_set_message($message, $severity, FALSE);
  801. }
  802. /**
  803. * Implements hook_hook_info().
  804. */
  805. function features_hook_info() {
  806. $hooks = array(
  807. 'features_api',
  808. 'features_pipe_alter',
  809. 'features_export_alter',
  810. );
  811. return array_fill_keys($hooks, array('group' => 'features'));
  812. }