features.module 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294
  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 satisfy a certain use-case.
  7. */
  8. define('FEATURES_API', '2');
  9. define('FEATURES_MODULE_ENABLED', 1);
  10. define('FEATURES_MODULE_DISABLED', 0);
  11. define('FEATURES_MODULE_MISSING', -1);
  12. define('FEATURES_MODULE_CONFLICT', 2);
  13. define('FEATURES_REBUILDABLE', -1);
  14. define('FEATURES_DEFAULT', 0);
  15. define('FEATURES_OVERRIDDEN', 1);
  16. define('FEATURES_NEEDS_REVIEW', 2);
  17. define('FEATURES_REBUILDING', 3);
  18. define('FEATURES_CONFLICT', 4);
  19. define('FEATURES_DISABLED', 5);
  20. define('FEATURES_CHECKING', 6);
  21. define('FEATURES_ALTER_TYPE_NORMAL', 'normal');
  22. define('FEATURES_ALTER_TYPE_INLINE', 'inline');
  23. define('FEATURES_ALTER_TYPE_NONE', 'none');
  24. // Duration of rebuild semaphore: 10 minutes.
  25. define('FEATURES_SEMAPHORE_TIMEOUT', 10 * 60);
  26. /**
  27. * Components with this 'default_file' flag will have exports written to the
  28. * common defaults file 'MODULENAME.features.inc'. This is the default
  29. * behavior.
  30. */
  31. define('FEATURES_DEFAULTS_INCLUDED_COMMON', 0);
  32. /**
  33. * Components with this 'default_file' flag will have exports written to a
  34. * defaults based on the component name like 'MODULENAME.features.COMPONENT-NAME.inc'.
  35. * Any callers to this component's defaults hook must call
  36. * features_include_defaults('component') in order to include this file.
  37. */
  38. define('FEATURES_DEFAULTS_INCLUDED', 1);
  39. /**
  40. * Components with this 'default_file' flag must specify a filename for their
  41. * exports. Additionally a stub will NOT be written to 'MODULENAME.features.inc'
  42. * allowing the file to be included directly by the implementing module.
  43. */
  44. define('FEATURES_DEFAULTS_CUSTOM', 2);
  45. /**
  46. * Components with this 'duplicates' flag may not have multiple features provide the
  47. * same component key in their info files. This is the default behavior.
  48. */
  49. define('FEATURES_DUPLICATES_CONFLICT', 0);
  50. /**
  51. * Components with this 'duplicates' flag are allowed to have multiple features
  52. * provide the same component key in their info files.
  53. */
  54. define('FEATURES_DUPLICATES_ALLOWED', 1);
  55. /**
  56. * The default destination path for features exported via the UI.
  57. */
  58. define('FEATURES_DEFAULT_EXPORT_PATH', 'sites/all/modules');
  59. /**
  60. * Implements hook_menu().
  61. */
  62. function features_menu() {
  63. $items = array();
  64. $items['admin/structure/features'] = array(
  65. 'title' => 'Features',
  66. 'description' => 'Manage features.',
  67. 'page callback' => 'drupal_get_form',
  68. 'page arguments' => array('features_admin_form'),
  69. 'type' => MENU_NORMAL_ITEM,
  70. 'file' => 'features.admin.inc',
  71. );
  72. $items['admin/structure/features/cleanup'] = array(
  73. 'title' => 'Cleanup',
  74. 'description' => 'Clear cache after enabling/disabling a feature.',
  75. 'page callback' => 'features_cleanup',
  76. 'type' => MENU_CALLBACK,
  77. 'file' => 'features.admin.inc',
  78. 'weight' => 1,
  79. );
  80. $items['admin/structure/features/manage'] = array(
  81. 'title' => 'Manage',
  82. 'description' => 'Enable and disable features.',
  83. 'page callback' => 'drupal_get_form',
  84. 'page arguments' => array('features_admin_form'),
  85. 'type' => MENU_DEFAULT_LOCAL_TASK,
  86. 'file' => 'features.admin.inc',
  87. );
  88. $items['admin/structure/features/create'] = array(
  89. 'title' => 'Create feature',
  90. 'description' => 'Create a new feature.',
  91. 'page callback' => 'drupal_get_form',
  92. 'page arguments' => array('features_export_form'),
  93. 'access callback' => 'user_access',
  94. 'access arguments' => array('administer features'),
  95. 'type' => MENU_LOCAL_TASK,
  96. 'file' => "features.admin.inc",
  97. 'weight' => 10,
  98. );
  99. $items['admin/structure/features/settings'] = array(
  100. 'title' => 'Settings',
  101. 'description' => 'Adjust settings for using features module.',
  102. 'page callback' => 'drupal_get_form',
  103. 'page arguments' => array('features_settings_form'),
  104. 'access callback' => 'user_access',
  105. 'access arguments' => array('administer features'),
  106. 'type' => MENU_LOCAL_TASK,
  107. 'file' => "features.admin.inc",
  108. 'weight' => 11,
  109. );
  110. $items['admin/structure/features/%feature'] = array(
  111. 'title callback' => 'features_get_feature_title',
  112. 'title arguments' => array(3),
  113. 'description' => 'Display components of a feature.',
  114. 'page callback' => 'drupal_get_form',
  115. 'page arguments' => array('features_admin_components', 3),
  116. 'load arguments' => array(TRUE),
  117. 'access callback' => 'user_access',
  118. 'access arguments' => array('administer features'),
  119. 'type' => MENU_CALLBACK,
  120. 'file' => 'features.admin.inc',
  121. );
  122. $items['admin/structure/features/%feature/view'] = array(
  123. 'title' => 'View',
  124. 'description' => 'Display components of a feature.',
  125. 'access callback' => 'user_access',
  126. 'access arguments' => array('administer features'),
  127. 'type' => MENU_DEFAULT_LOCAL_TASK,
  128. 'weight' => -10,
  129. );
  130. $items['admin/structure/features/%feature/recreate'] = array(
  131. 'title' => 'Recreate',
  132. 'description' => 'Recreate an existing feature.',
  133. 'page callback' => 'drupal_get_form',
  134. 'page arguments' => array('features_export_form', 3),
  135. 'load arguments' => array(TRUE),
  136. 'access callback' => 'user_access',
  137. 'access arguments' => array('administer features'),
  138. 'type' => MENU_LOCAL_TASK,
  139. 'file' => "features.admin.inc",
  140. 'weight' => 11,
  141. );
  142. if (module_exists('diff')) {
  143. $items['admin/structure/features/%feature/diff'] = array(
  144. 'title' => 'Review overrides',
  145. 'description' => 'Compare default and current feature.',
  146. 'page callback' => 'features_feature_diff',
  147. 'page arguments' => array(3, 5),
  148. 'load arguments' => array(TRUE),
  149. 'access callback' => 'features_access_override_actions',
  150. 'access arguments' => array(3),
  151. 'type' => MENU_LOCAL_TASK,
  152. 'file' => 'features.admin.inc',
  153. );
  154. }
  155. $items['admin/structure/features/%feature/lock'] = array(
  156. 'title' => 'Lock',
  157. 'description' => 'Lock a feature or components.',
  158. 'page callback' => 'features_admin_lock',
  159. 'page arguments' => array(3, 5, 6),
  160. 'load arguments' => array(TRUE),
  161. 'access arguments' => array('administer features'),
  162. 'type' => MENU_CALLBACK,
  163. 'file' => 'features.admin.inc',
  164. );
  165. $items['admin/structure/features/%feature/status'] = array(
  166. 'title' => 'Status',
  167. 'description' => 'Javascript status call back.',
  168. 'page callback' => 'features_feature_status',
  169. 'page arguments' => array(3),
  170. 'load arguments' => array(TRUE),
  171. 'access callback' => 'user_access',
  172. 'access arguments' => array('administer features'),
  173. 'type' => MENU_CALLBACK,
  174. 'file' => 'features.admin.inc',
  175. );
  176. $items['features/autocomplete/packages'] = array(
  177. 'page callback' => 'features_autocomplete_packages',
  178. 'access arguments' => array('administer features'),
  179. 'type' => MENU_CALLBACK,
  180. 'file' => 'features.admin.inc',
  181. );
  182. $items['features/ajaxcallback/%'] = array(
  183. 'title callback' => 'features_get_feature_components',
  184. 'description' => 'Return components of a feature.',
  185. 'page callback' => 'features_export_components_json',
  186. 'page arguments' => array(2),
  187. 'access callback' => 'user_access',
  188. 'access arguments' => array('administer features'),
  189. 'type' => MENU_CALLBACK,
  190. 'file' => 'features.admin.inc',
  191. );
  192. foreach ($items as $path => $item) {
  193. if (!isset($item['access callback'])) {
  194. $items[$path]['access callback'] = 'user_access';
  195. $items[$path]['access arguments'] = array('manage features');
  196. }
  197. }
  198. return $items;
  199. }
  200. /**
  201. * Implements hook_theme().
  202. */
  203. function features_theme() {
  204. $base = array(
  205. 'path' => drupal_get_path('module', 'features') . '/theme',
  206. 'file' => 'theme.inc',
  207. );
  208. $items = array();
  209. $items['features_module_status'] = array(
  210. 'variables' => array('module' => NULL, 'status' => NULL)
  211. ) + $base;
  212. $items['features_components'] = array(
  213. 'variables' => array('info' => NULL, 'sources' => NULL),
  214. ) + $base;
  215. $items['features_component_key'] = $base;
  216. $items['features_component_list'] = array(
  217. 'variables' => array('components' => array(), 'source' => array(), 'conflicts' => array()),
  218. ) + $base;
  219. $items['features_storage_link'] = array(
  220. 'variables' => array('storage' => NULL, 'text' => NULL, 'path' => NULL, 'options' => array()),
  221. ) + $base;
  222. $items['features_lock_link'] = array(
  223. 'variables' => array('feature' => NULL, 'component' => NULL, 'locked' => FALSE),
  224. ) + $base;
  225. $items['features_form_components'] =
  226. $items['features_form_export'] =
  227. $items['features_form_package'] = array(
  228. 'render element' => 'form',
  229. ) + $base;
  230. $items['features_form_buttons'] = array(
  231. 'render element' => 'element',
  232. ) + $base;
  233. $items['features_admin_components'] = array(
  234. 'render element' => 'form',
  235. 'template' => 'features-admin-components',
  236. ) + $base;
  237. return $items;
  238. }
  239. /**
  240. * Implements hook_flush_caches().
  241. */
  242. function features_flush_caches() {
  243. if (($modules_changed = variable_get('features_modules_changed', FALSE)) || variable_get('features_rebuild_on_flush', TRUE)) {
  244. if ($modules_changed) {
  245. variable_set('features_modules_changed', FALSE);
  246. }
  247. features_rebuild();
  248. // Don't flush the modules cache during installation, for performance reasons.
  249. if (variable_get('install_task') == 'done') {
  250. features_get_modules(NULL, TRUE);
  251. }
  252. }
  253. if (db_table_exists('cache_features')) {
  254. return array('cache_features');
  255. }
  256. return array();
  257. }
  258. /**
  259. * Implements hook_form().
  260. */
  261. function features_form($node, $form_state) {
  262. return node_content_form($node, $form_state);
  263. }
  264. /**
  265. * Implements hook_permission().
  266. */
  267. function features_permission() {
  268. return array(
  269. 'administer features' => array(
  270. 'title' => t('Administer features'),
  271. 'description' => t('Perform administration tasks on features.'),
  272. 'restrict access' => TRUE,
  273. ),
  274. 'manage features' => array(
  275. 'title' => t('Manage features'),
  276. 'description' => t('View, enable and disable features.'),
  277. 'restrict access' => TRUE,
  278. ),
  279. 'generate features' => array(
  280. 'title' => t('Generate features'),
  281. 'description' => t('Allow feature exports to be generated and written directly to site.'),
  282. 'restrict access' => TRUE,
  283. ),
  284. 'rename features' => array(
  285. 'title' => t('Edit feature machine name'),
  286. 'description' => t('Allows editing machine name of a disabled feature'),
  287. 'restrict access' => TRUE,
  288. ),
  289. );
  290. }
  291. /**
  292. * Implements hook_help().
  293. */
  294. function features_help($path, $arg) {
  295. switch ($path) {
  296. case 'admin/help#features':
  297. $output = file_get_contents(drupal_get_path('module', 'features') . '/README.txt');
  298. return module_exists('markdown') ? filter_xss_admin(module_invoke('markdown', 'filter', 'process', 0, -1, $output)) : '<pre>' . check_plain($output) . '</pre>';
  299. case 'admin/build/features':
  300. 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>';
  301. }
  302. }
  303. /**
  304. * Implements hook_modules_disabled().
  305. */
  306. function features_modules_disabled($modules) {
  307. // Go through all modules and gather features that can be disabled.
  308. $items = array();
  309. foreach ($modules as $module) {
  310. if ($feature = features_load_feature($module)) {
  311. $items[$module] = array_keys($feature->info['features']);
  312. }
  313. }
  314. if (!empty($items)) {
  315. _features_restore('disable', $items);
  316. // Rebuild the list of features includes.
  317. features_include(TRUE);
  318. }
  319. }
  320. /**
  321. * Implements hook_modules_enabled().
  322. */
  323. function features_modules_enabled($modules) {
  324. // Allow distributions to disable this behavior and rebuild the features
  325. // manually inside a batch.
  326. if (!variable_get('features_rebuild_on_module_install', TRUE)) {
  327. return;
  328. }
  329. // mark modules as being changed for test in features_flush_caches
  330. variable_set('features_modules_changed', TRUE);
  331. // Go through all modules and gather features that can be enabled.
  332. $items = array();
  333. foreach ($modules as $module) {
  334. if ($feature = features_load_feature($module)) {
  335. $items[$module] = array_keys($feature->info['features']);
  336. }
  337. }
  338. if (!empty($items)) {
  339. // Need to include any new files.
  340. // @todo Redo function so can take in list of modules to include.
  341. features_include_defaults(NULL, TRUE);
  342. _features_restore('enable', $items);
  343. // Rebuild the list of features includes.
  344. features_include(TRUE);
  345. // Reorders components to match hook order and removes non-existant.
  346. $all_components = array_keys(features_get_components());
  347. foreach ($items as $module => $components) {
  348. $items[$module] = array_intersect($all_components, $components);
  349. }
  350. _features_restore('rebuild', $items);
  351. }
  352. }
  353. /**
  354. * Load includes for any modules that implement the features API and
  355. * load includes for those provided by features.
  356. */
  357. function features_include($reset = FALSE) {
  358. static $once;
  359. if (!isset($once) || $reset) {
  360. $once = TRUE;
  361. // Features provides integration on behalf of these modules.
  362. // The features include provides handling for the feature dependencies.
  363. // Note that ctools is placed last because it implements hooks "dynamically" for other modules.
  364. $modules = array('features', 'block', 'contact', 'context', 'field', 'filter', 'image', 'locale', 'menu', 'node', 'taxonomy', 'user', 'views', 'ctools');
  365. foreach (array_filter($modules, 'module_exists') as $module) {
  366. module_load_include('inc', 'features', "includes/features.$module");
  367. }
  368. if (module_exists('ctools')) {
  369. // Finally, add ctools eval'd implementations.
  370. ctools_features_declare_functions($reset);
  371. }
  372. // Clear static cache, since we've now included new implementers.
  373. foreach (features_get_components(NULL, 'file', $reset) as $file) {
  374. if (is_file(DRUPAL_ROOT . '/' . $file)) {
  375. require_once DRUPAL_ROOT . '/' . $file;
  376. }
  377. }
  378. }
  379. }
  380. /**
  381. * Load features includes for all components that require includes before
  382. * collecting defaults.
  383. */
  384. function features_include_defaults($components = NULL, $reset = FALSE) {
  385. static $include_components;
  386. // Build an array of components that require inclusion:
  387. // Views, CTools components and those using FEATURES_DEFAULTS_INCLUDED.
  388. if (!isset($include_components) || $reset) {
  389. $include_components = features_get_components();
  390. foreach ($include_components as $component => $info) {
  391. if (!isset($info['api']) && (!isset($info['default_file']) || $info['default_file'] !== FEATURES_DEFAULTS_INCLUDED)) {
  392. unset($include_components[$component]);
  393. }
  394. }
  395. }
  396. // If components are specified, only include for the specified components.
  397. if (isset($components)) {
  398. $components = is_array($components) ? $components : array($components);
  399. }
  400. // Use all include components if none are explicitly specified.
  401. else {
  402. $components = array_keys($include_components);
  403. }
  404. foreach ($components as $component) {
  405. if (isset($include_components[$component])) {
  406. $info = $include_components[$component];
  407. // Inclusion of ctools components.
  408. if (isset($info['api'], $info['module'], $info['current_version'])) {
  409. ctools_include('plugins');
  410. ctools_plugin_api_include($info['module'], $info['api'], $info['current_version'], $info['current_version']);
  411. }
  412. // Inclusion of defaults for components using FEATURES_DEFAULTS_INCLUDED.
  413. else {
  414. $features = isset($features) ? $features : features_get_features(NULL, $reset);
  415. foreach ($features as $feature) {
  416. $filename = isset($info['default_file']) && $info['default_file'] == FEATURES_DEFAULTS_CUSTOM ? $info['default_filename'] : "features.{$component}";
  417. if (module_exists($feature->name) && isset($feature->info['features'][$component])) {
  418. module_load_include('inc', $feature->name, "{$feature->name}.$filename");
  419. }
  420. }
  421. }
  422. }
  423. }
  424. }
  425. /**
  426. * Feature object loader. DEPRECATED but included for backwards compatibility
  427. */
  428. function feature_load($name, $reset = FALSE) {
  429. return features_load_feature($name, $reset);
  430. }
  431. /**
  432. * Feature object loader.
  433. */
  434. function features_load_feature($name, $reset = FALSE) {
  435. // Use an alternative code path during installation, for better performance.
  436. if (variable_get('install_task') != 'done') {
  437. static $features;
  438. if (!isset($features[$name])) {
  439. // Set defaults for module info.
  440. $defaults = array(
  441. 'dependencies' => array(),
  442. 'description' => '',
  443. 'package' => 'Other',
  444. 'version' => NULL,
  445. 'php' => DRUPAL_MINIMUM_PHP,
  446. 'files' => array(),
  447. 'bootstrap' => 0,
  448. );
  449. $info = drupal_parse_info_file(drupal_get_path('module', $name) . '/' . $name . '.info');
  450. $features[$name] = FALSE;
  451. if (!empty($info['features']) && empty($info['hidden'])) {
  452. // Build a fake file object with the data needed during installation.
  453. $features[$name] = new stdClass;
  454. $features[$name]->name = $name;
  455. $features[$name]->filename = drupal_get_path('module', $name) . '/' . $name . '.module';
  456. $features[$name]->type = 'module';
  457. $features[$name]->info = $info + $defaults;
  458. }
  459. }
  460. return $features[$name];
  461. }
  462. else {
  463. return features_get_features($name, $reset);
  464. }
  465. }
  466. /**
  467. * Return a module 'object' including .info information.
  468. *
  469. * @param $name
  470. * The name of the module to retrieve information for. If omitted,
  471. * an array of all available modules will be returned.
  472. * @param $reset
  473. * Whether to reset the cache.
  474. *
  475. * @return
  476. * If a module is request (and exists) a module object is returned. If no
  477. * module is requested info for all modules is returned.
  478. */
  479. function features_get_modules($name = NULL, $reset = FALSE) {
  480. return features_get_info('module', $name, $reset);
  481. }
  482. /**
  483. * Returns the array of supported components.
  484. *
  485. * @see hook_features_api
  486. *
  487. * @param $component
  488. * A specific type of component that supports features.
  489. * @param $key
  490. * A key that hook_features_api supports.
  491. *
  492. * @return An array of component labels keyed by the component names.
  493. */
  494. function features_get_components($component = NULL, $key = NULL, $reset = FALSE) {
  495. features_include();
  496. $components = &drupal_static(__FUNCTION__);
  497. $component_by_key = &drupal_static(__FUNCTION__ . '_by_key');
  498. if ($reset || !isset($components) || !isset($component_by_key)) {
  499. $components = $component_by_key = array();
  500. if (!$reset && ($cache = cache_get('features_api', 'cache_features'))) {
  501. $components = $cache->data;
  502. }
  503. else {
  504. $components = module_invoke_all('features_api');
  505. drupal_alter('features_api', $components);
  506. cache_set('features_api', $components, 'cache_features');
  507. }
  508. foreach ($components as $component_type => $component_information) {
  509. foreach ($component_information as $component_key => $component_value) {
  510. $component_by_key[$component_key][$component_type] = $component_value;
  511. }
  512. }
  513. }
  514. if ($key && $component) {
  515. return !empty($components[$component][$key]) ? $components[$component][$key] : NULL;
  516. }
  517. elseif ($key) {
  518. return !empty($component_by_key[$key]) ? $component_by_key[$key] : array();
  519. }
  520. elseif ($component) {
  521. return $components[$component];
  522. }
  523. return $components;
  524. }
  525. /**
  526. * Returns components that are offered as an option on feature creation.
  527. */
  528. function features_get_feature_components() {
  529. return array_intersect_key(features_get_components(), array_filter(features_get_components(NULL, 'feature_source')));
  530. }
  531. /**
  532. * Invoke a component callback.
  533. */
  534. function features_invoke($component, $callback) {
  535. $args = func_get_args();
  536. unset($args[0], $args[1]);
  537. // Append the component name to the arguments.
  538. $args[] = $component;
  539. if ($function = features_hook($component, $callback)) {
  540. return call_user_func_array($function, $args);
  541. }
  542. }
  543. /**
  544. * Checks whether a component implements the given hook.
  545. *
  546. * @return
  547. * The function implementing the hook, or FALSE.
  548. */
  549. function features_hook($component, $hook, $reset = FALSE) {
  550. // Determine the function callback base.
  551. $base = features_get_components($component, 'base');
  552. $base = isset($base) ? $base : $component;
  553. return function_exists($base . '_' . $hook) ? $base . '_' . $hook : FALSE;
  554. }
  555. /**
  556. * Enables and installs an array of modules, ignoring those
  557. * already enabled & installed. Consider this a helper or
  558. * extension to drupal_install_modules().
  559. *
  560. * @param $modules
  561. * An array of modules to install.
  562. * @param $reset
  563. * Clear the module info cache.
  564. */
  565. function features_install_modules($modules) {
  566. variable_set('features_modules_changed', TRUE);
  567. module_load_include('inc', 'features', 'features.export');
  568. $files = system_rebuild_module_data();
  569. // Build maximal list of dependencies.
  570. $install = array();
  571. foreach ($modules as $name) {
  572. // Parse the dependency string into the module name and version information.
  573. $parsed_name = drupal_parse_dependency($name);
  574. $name = $parsed_name['name'];
  575. if ($file = $files[$name]) {
  576. $install[] = $name;
  577. if (!empty($file->info['dependencies'])) {
  578. $install = array_merge($install, _features_export_maximize_dependencies($file->info['dependencies']));
  579. }
  580. }
  581. }
  582. // Filter out enabled modules.
  583. $enabled = array_filter($install, 'module_exists');
  584. $install = array_diff($install, $enabled);
  585. if (!empty($install)) {
  586. // Make sure the install API is available.
  587. $install = array_unique($install);
  588. include_once DRUPAL_ROOT . '/' . './includes/install.inc';
  589. module_enable($install);
  590. }
  591. }
  592. /**
  593. * Wrapper around features_get_info() that returns an array
  594. * of module info objects that are features.
  595. */
  596. function features_get_features($name = NULL, $reset = FALSE) {
  597. return features_get_info('feature', $name, $reset);
  598. }
  599. /**
  600. * Helper for retrieving info from system table.
  601. */
  602. function features_get_info($type = 'module', $name = NULL, $reset = FALSE) {
  603. static $cache;
  604. if (!isset($cache)) {
  605. $cache = cache_get('features_module_info', 'cache_features');
  606. }
  607. if (empty($cache) || $reset) {
  608. $data = array(
  609. 'feature' => array(),
  610. 'module' => array(),
  611. );
  612. $ignored = variable_get('features_ignored_orphans', array());
  613. $files = system_rebuild_module_data();
  614. foreach ($files as $row) {
  615. // Remove modification timestamp, added in Drupal 7.33.
  616. if (isset($row->info['mtime'])) {
  617. unset($row->info['mtime']);
  618. }
  619. // Avoid false-reported feature overrides for php = 5.2.4 line in .info file.
  620. if (isset($row->info['php'])) {
  621. unset($row->info['php']);
  622. }
  623. // If module is no longer enabled, remove it from the ignored orphans list.
  624. if (in_array($row->name, $ignored, TRUE) && !$row->status) {
  625. $key = array_search($row->name, $ignored, TRUE);
  626. unset($ignored[$key]);
  627. }
  628. if (!empty($row->info['features'])) {
  629. // Fix css/js paths
  630. if (!empty($row->info['stylesheets'])) {
  631. foreach ($row->info['stylesheets'] as $media => $css) {
  632. $row->info['stylesheets'][$media] = array_keys($css);
  633. }
  634. }
  635. if (!empty($row->info['scripts'])) {
  636. $row->info['scripts'] = array_keys($row->info['scripts']);
  637. }
  638. // Rework the features array, to change the vocabulary permission
  639. // features.
  640. foreach ($row->info['features'] as $component => $features) {
  641. if ($component == 'user_permission') {
  642. foreach ($features as $key => $feature) {
  643. // Export vocabulary permissions using the machine name, instead
  644. // of vocabulary id.
  645. _user_features_change_term_permission($feature);
  646. $row->info['features'][$component][$key] = $feature;
  647. }
  648. }
  649. }
  650. $data['feature'][$row->name] = $row;
  651. $data['feature'][$row->name]->components = array_keys($row->info['features']);
  652. if (!empty($row->info['dependencies'])) {
  653. $data['feature'][$row->name]->components[] = 'dependencies';
  654. }
  655. }
  656. $data['module'][$row->name] = $row;
  657. }
  658. // Sort features according to dependencies.
  659. // @see install_profile_modules()
  660. $required = array();
  661. $non_required = array();
  662. $modules = array_keys($data['feature']);
  663. foreach ($modules as $module) {
  664. if ($files[$module]->requires) {
  665. $modules = array_merge($modules, array_keys($files[$module]->requires));
  666. }
  667. }
  668. $modules = array_unique($modules);
  669. foreach ($modules as $module) {
  670. if (!empty($files[$module]->info['features'])) {
  671. if (!empty($files[$module]->info['required'])) {
  672. $required[$module] = $files[$module]->sort;
  673. }
  674. else {
  675. $non_required[$module] = $files[$module]->sort;
  676. }
  677. }
  678. }
  679. arsort($required);
  680. arsort($non_required);
  681. $sorted = array();
  682. foreach ($required + $non_required as $module => $weight) {
  683. $sorted[$module] = $data['feature'][$module];
  684. }
  685. $data['feature'] = $sorted;
  686. variable_set('features_ignored_orphans', $ignored);
  687. cache_set('features_module_info', $data, 'cache_features');
  688. $cache = new stdClass();
  689. $cache->data = $data;
  690. }
  691. if (!empty($name)) {
  692. return !empty($cache->data[$type][$name]) ? clone $cache->data[$type][$name] : FALSE;
  693. }
  694. return !empty($cache->data[$type]) ? $cache->data[$type] : FALSE;
  695. }
  696. /**
  697. * Generate an array of feature dependencies that have been orphaned.
  698. */
  699. function features_get_orphans($reset = FALSE) {
  700. static $orphans;
  701. if (!isset($orphans) || $reset) {
  702. module_load_include('inc', 'features', 'features.export');
  703. $orphans = array();
  704. // Build a list of all dependencies for enabled and disabled features.
  705. $dependencies = array('enabled' => array(), 'disabled' => array());
  706. $features = features_get_features();
  707. foreach ($features as $feature) {
  708. $key = module_exists($feature->name) ? 'enabled' : 'disabled';
  709. if (!empty($feature->info['dependencies'])) {
  710. $dependencies[$key] = array_merge($dependencies[$key], _features_export_maximize_dependencies($feature->info['dependencies']));
  711. }
  712. }
  713. $dependencies['enabled'] = array_unique($dependencies['enabled']);
  714. $dependencies['disabled'] = array_unique($dependencies['disabled']);
  715. // Find the list of orphaned modules.
  716. $orphaned = array_diff($dependencies['disabled'], $dependencies['enabled']);
  717. $orphaned = array_intersect($orphaned, module_list(FALSE, FALSE));
  718. $orphaned = array_diff($orphaned, drupal_required_modules());
  719. $orphaned = array_diff($orphaned, array('features'));
  720. // Build final list of modules that can be disabled.
  721. $modules = features_get_modules(NULL, TRUE);
  722. $enabled = module_list();
  723. _module_build_dependencies($modules);
  724. foreach ($orphaned as $module) {
  725. if (!empty($modules[$module]->required_by)) {
  726. foreach ($modules[$module]->required_by as $module_name => $dependency) {
  727. $modules[$module]->required_by[$module_name] = $dependency['name'];
  728. }
  729. // Determine whether any dependents are actually enabled.
  730. $dependents = array_intersect($modules[$module]->required_by, $enabled);
  731. if (empty($dependents)) {
  732. $info = features_get_modules($module);
  733. $orphans[$module] = $info;
  734. }
  735. }
  736. }
  737. }
  738. return $orphans;
  739. }
  740. /**
  741. * Detect potential conflicts between any features that provide
  742. * identical components.
  743. */
  744. function features_get_conflicts($reset = FALSE) {
  745. $conflicts = array();
  746. $component_info = features_get_components();
  747. $map = features_get_component_map(NULL, $reset);
  748. foreach ($map as $type => $components) {
  749. // Only check conflicts for components we know about.
  750. if (isset($component_info[$type])) {
  751. foreach ($components as $component => $modules) {
  752. if (isset($component_info[$type]['duplicates']) && $component_info[$type]['duplicates'] == FEATURES_DUPLICATES_ALLOWED) {
  753. continue;
  754. }
  755. elseif (count($modules) > 1) {
  756. foreach ($modules as $module) {
  757. if (!isset($conflicts[$module])) {
  758. $conflicts[$module] = array();
  759. }
  760. foreach ($modules as $m) {
  761. if ($m != $module) {
  762. $conflicts[$module][$m][$type][] = $component;
  763. }
  764. }
  765. }
  766. }
  767. }
  768. }
  769. }
  770. return $conflicts;
  771. }
  772. /**
  773. * Provide a component to feature map.
  774. */
  775. function features_get_component_map($key = NULL, $reset = FALSE) {
  776. static $map;
  777. if (!isset($map) || $reset) {
  778. $map = array();
  779. $features = features_get_features(NULL, $reset);
  780. foreach ($features as $feature) {
  781. foreach ($feature->info['features'] as $type => $components) {
  782. if (!isset($map[$type])) {
  783. $map[$type] = array();
  784. }
  785. foreach ($components as $component) {
  786. $map[$type][$component][] = $feature->name;
  787. }
  788. }
  789. }
  790. }
  791. if (isset($key)) {
  792. return isset($map[$key]) ? $map[$key] : array();
  793. }
  794. return $map;
  795. }
  796. /**
  797. * Simple wrapper returns the status of a module.
  798. */
  799. function features_get_module_status($module) {
  800. if (module_exists($module)) {
  801. return FEATURES_MODULE_ENABLED;
  802. }
  803. elseif (features_get_modules($module)) {
  804. return FEATURES_MODULE_DISABLED;
  805. }
  806. else {
  807. return FEATURES_MODULE_MISSING;
  808. }
  809. }
  810. /**
  811. * Menu title callback.
  812. */
  813. function features_get_feature_title($feature) {
  814. return $feature->info['name'];
  815. }
  816. /**
  817. * Menu access callback for whether a user should be able to access
  818. * override actions for a given feature.
  819. */
  820. function features_access_override_actions($feature) {
  821. if (user_access('administer features')) {
  822. static $access = array();
  823. if (!isset($access[$feature->name])) {
  824. // Set a value first. We may get called again from within features_detect_overrides().
  825. $access[$feature->name] = FALSE;
  826. features_include();
  827. module_load_include('inc', 'features', 'features.export');
  828. $access[$feature->name] = in_array(features_get_storage($feature->name), array(FEATURES_DEFAULT, FEATURES_OVERRIDDEN, FEATURES_NEEDS_REVIEW));
  829. }
  830. return $access[$feature->name];
  831. }
  832. return FALSE;
  833. }
  834. /**
  835. * Implements hook_form_alter() for system_modules form().
  836. */
  837. function features_form_system_modules_alter(&$form) {
  838. if (variable_get('features_rebuild_modules_page', FALSE)) {
  839. features_rebuild();
  840. }
  841. }
  842. /**
  843. * Restore the specified modules to the default state.
  844. */
  845. function _features_restore($op, $items = array()) {
  846. $lockable = FALSE;
  847. // Set this variable in $conf if having timeout issues during install/rebuild.
  848. if (variable_get('features_restore_time_limit_' . $op, FALSE) !== FALSE) {
  849. drupal_set_time_limit(variable_get('features_restore_time_limit_' . $op, FALSE));
  850. }
  851. module_load_include('inc', 'features', 'features.export');
  852. features_include();
  853. switch ($op) {
  854. case 'revert':
  855. $restore_states = array(FEATURES_OVERRIDDEN, FEATURES_REBUILDABLE, FEATURES_NEEDS_REVIEW);
  856. $restore_hook = 'features_revert';
  857. $log_action = 'Revert';
  858. $lockable = TRUE;
  859. break;
  860. case 'rebuild':
  861. $restore_states = array(FEATURES_REBUILDABLE);
  862. $restore_hook = 'features_rebuild';
  863. $log_action = 'Rebuild';
  864. $lockable = variable_get('features_lock_mode', 'all') == 'all';
  865. break;
  866. case 'disable':
  867. $restore_hook = 'features_disable_feature';
  868. $log_action = 'Disable';
  869. break;
  870. case 'enable':
  871. $restore_hook = 'features_enable_feature';
  872. $log_action = 'Enable';
  873. break;
  874. }
  875. if (empty($items)) {
  876. // Drush may execute a whole chain of commands that may trigger feature
  877. // rebuilding multiple times during a single request. Make sure we do not
  878. // rebuild the same cached list of modules over and over again by setting
  879. // $reset to TRUE.
  880. // Note: this may happen whenever more than one feature will be enabled
  881. // in chain, for example also using features_install_modules().
  882. $states = features_get_component_states(array(), ($op == 'rebuild'), defined('DRUSH_BASE_PATH'));
  883. foreach ($states as $module_name => $components) {
  884. foreach ($components as $component => $state) {
  885. if (in_array($state, $restore_states)) {
  886. $items[$module_name][] = $component;
  887. }
  888. }
  889. }
  890. }
  891. // Invoke global pre restore hook.
  892. module_invoke_all('features_pre_restore', $op, $items);
  893. foreach ($items as $module_name => $components) {
  894. // If feature is totally locked, do not execute past this stage.
  895. if ($lockable && features_feature_is_locked($module_name)) {
  896. watchdog('features', 'Tried @actioning a locked @module_name, aborted.', array('@action' => $log_action, '@module_name' => $module_name));
  897. continue;
  898. }
  899. foreach ($components as $component) {
  900. // If feature is totally locked, do not execute past this stage.
  901. if ($lockable && features_feature_is_locked($module_name, $component)) {
  902. watchdog('features', 'Tried @actioning a locked @module_name / @component, aborted.', array('@action' => $log_action, '@component' => $component, '@module_name' => $module_name));
  903. continue;
  904. }
  905. // Invoke pre hook
  906. $pre_hook = 'pre_' . $restore_hook;
  907. module_invoke($module_name, $pre_hook, $component);
  908. if (features_hook($component, $restore_hook)) {
  909. // Set a semaphore to prevent other instances of the same script from running concurrently.
  910. watchdog('features', '@actioning @module_name / @component.', array('@action' => $log_action, '@component' => $component, '@module_name' => $module_name));
  911. features_semaphore('set', $component);
  912. features_invoke($component, $restore_hook, $module_name);
  913. // If the script completes, remove the semaphore and set the code signature.
  914. features_semaphore('del', $component);
  915. features_set_signature($module_name, $component);
  916. watchdog('features', '@action completed for @module_name / @component.', array('@action' => $log_action, '@component' => $component, '@module_name' => $module_name));
  917. }
  918. // Invoke post hook
  919. $post_hook = 'post_' . $restore_hook;
  920. module_invoke($module_name, $post_hook, $component);
  921. }
  922. }
  923. // Invoke global post restore hook.
  924. module_invoke_all('features_post_restore', $op, $items);
  925. }
  926. /**
  927. * Wrapper around _features_restore().
  928. */
  929. function features_revert($revert = array()) {
  930. return _features_restore('revert', $revert);
  931. }
  932. /**
  933. * Wrapper around _features_restore().
  934. */
  935. function features_rebuild($rebuild = array()) {
  936. return _features_restore('rebuild', $rebuild);
  937. }
  938. /**
  939. * Revert a single features module.
  940. *
  941. * @param string $module
  942. * A features module machine name. This module must be a
  943. * features module and enabled.
  944. */
  945. function features_revert_module($module) {
  946. if (($feature = feature_load($module, TRUE)) && module_exists($module)) {
  947. $components = array();
  948. foreach (array_keys($feature->info['features']) as $component) {
  949. if (features_hook($component, 'features_revert')) {
  950. $components[] = $component;
  951. }
  952. }
  953. features_revert(array($module => $components));
  954. }
  955. }
  956. /**
  957. * Utility functions ==================================================
  958. */
  959. /**
  960. * Log a message, environment agnostic.
  961. *
  962. * @param $message
  963. * The message to log.
  964. * @param $severity
  965. * The severity of the message: status, warning or error.
  966. */
  967. function features_log($message, $severity = 'status') {
  968. if (function_exists('drush_verify_cli')) {
  969. $message = strip_tags($message);
  970. if ($severity == 'status') {
  971. $severity = 'ok';
  972. }
  973. elseif ($severity == 'error') {
  974. drush_set_error($message);
  975. return;
  976. }
  977. drush_log($message, $severity);
  978. return;
  979. }
  980. drupal_set_message($message, $severity, FALSE);
  981. }
  982. /**
  983. * Implements hook_hook_info().
  984. */
  985. function features_hook_info() {
  986. $hooks = array(
  987. 'features_api',
  988. 'features_pipe_alter',
  989. 'features_export_alter',
  990. 'features_export_options_alter',
  991. );
  992. return array_fill_keys($hooks, array('group' => 'features'));
  993. }
  994. /**
  995. * Change vocabularies permission, from vocab id to machine name and vice versa.
  996. */
  997. function _user_features_change_term_permission(&$perm, $type = 'vid') {
  998. if (!module_exists('taxonomy')) {
  999. return;
  1000. }
  1001. // Export vocabulary permissions using the machine name, instead of vocabulary
  1002. // id.
  1003. if (strpos($perm, 'edit terms in ') !== FALSE || strpos($perm, 'delete terms in ') !== FALSE) {
  1004. preg_match("/(?<=\040)([^\s]+?)$/", trim($perm), $voc_id);
  1005. $vid = $voc_id[0];
  1006. if (is_numeric($vid) && $type == 'vid') {
  1007. if (function_exists('taxonomy_vocabulary_load')) {
  1008. if ($voc = taxonomy_vocabulary_load($vid)) {
  1009. $perm = str_replace($vid, $voc->machine_name, $perm);
  1010. }
  1011. }
  1012. }
  1013. elseif ($type == 'machine_name') {
  1014. if ($voc = taxonomy_vocabulary_machine_name_load($vid)) {
  1015. $perm = str_replace($vid, $voc->vid, $perm);
  1016. }
  1017. }
  1018. }
  1019. }
  1020. /**
  1021. * Recursively computes the difference of arrays with additional index check.
  1022. *
  1023. * This is a version of array_diff_assoc() that supports multidimensional
  1024. * arrays.
  1025. *
  1026. * @param array $array1
  1027. * The array to compare from.
  1028. * @param array $array2
  1029. * The array to compare to.
  1030. *
  1031. * @return array
  1032. * Returns an array containing all the values from array1 that are not present
  1033. * in array2.
  1034. */
  1035. function features_array_diff_assoc_recursive(array $array1, array $array2) {
  1036. $difference = array();
  1037. foreach ($array1 as $key => $value) {
  1038. if (is_array($value)) {
  1039. if (!isset($array2[$key]) || !is_array($array2[$key])) {
  1040. $difference[$key] = $value;
  1041. }
  1042. else {
  1043. $new_diff = features_array_diff_assoc_recursive($value, $array2[$key]);
  1044. if (!empty($new_diff)) {
  1045. $difference[$key] = $new_diff;
  1046. }
  1047. }
  1048. }
  1049. elseif (!isset($array2[$key]) || $array2[$key] != $value) {
  1050. $difference[$key] = $value;
  1051. }
  1052. }
  1053. return $difference;
  1054. }
  1055. /**
  1056. * Returns an array of deprecated components
  1057. * Rather than deprecating the component directly, we look for other components
  1058. * that supersedes the component
  1059. * @param $components
  1060. * The array of components (component_info) from features_get_components typically.
  1061. */
  1062. function features_get_deprecated($components = array()) {
  1063. if (empty($components)) {
  1064. $components = features_get_components();
  1065. }
  1066. $deprecated = array();
  1067. foreach ($components as $component => $component_info) {
  1068. if (!empty($component_info['supersedes'])) {
  1069. $deprecated[$component_info['supersedes']] = $component_info['supersedes'];
  1070. }
  1071. }
  1072. return $deprecated;
  1073. }
  1074. /**
  1075. * Returns whether a feature or it's component is locked.
  1076. */
  1077. function features_feature_is_locked($feature, $component = NULL, $check_global_component_setting = TRUE) {
  1078. $locked = variable_get('features_feature_locked', array());
  1079. if ($component) {
  1080. return ($check_global_component_setting && features_component_is_locked($component)) || !empty($locked[$feature][$component]);
  1081. }
  1082. else {
  1083. return !empty($locked[$feature]['_all']);
  1084. }
  1085. }
  1086. /**
  1087. * Returns whether a component is locked.
  1088. */
  1089. function features_component_is_locked($component) {
  1090. return variable_get('features_component_locked_' . $component, FALSE);
  1091. }
  1092. /**
  1093. * Locks a feature or it's component.
  1094. */
  1095. function features_feature_lock($feature, $component = NULL) {
  1096. $locked = variable_get('features_feature_locked', array());
  1097. $locked[$feature] = !empty($locked[$feature]) ? $locked[$feature] : array();
  1098. if ($component) {
  1099. $locked[$feature][$component] = TRUE;
  1100. }
  1101. else {
  1102. $locked[$feature]['_all'] = TRUE;
  1103. }
  1104. variable_set('features_feature_locked', $locked);
  1105. }
  1106. /**
  1107. * Unlocks a feature or it's component.
  1108. */
  1109. function features_feature_unlock($feature, $component = NULL) {
  1110. $locked = variable_get('features_feature_locked', array());
  1111. if ($component) {
  1112. unset($locked[$feature][$component]);
  1113. }
  1114. else {
  1115. unset($locked[$feature]['_all']);
  1116. }
  1117. variable_set('features_feature_locked', $locked);
  1118. }
  1119. /**
  1120. * Sets/Returns the current language to english to ensure a proper export.
  1121. */
  1122. function _features_export_language($language = NULL) {
  1123. $current = $GLOBALS['language'];
  1124. if (isset($language)) {
  1125. $GLOBALS['language'] = $language;
  1126. }
  1127. elseif ($GLOBALS['language']->language != 'en') {
  1128. // Create the language object as language_default() does.
  1129. $GLOBALS['language'] = (object) array(
  1130. 'language' => 'en',
  1131. 'name' => 'English',
  1132. 'native' => 'English',
  1133. 'direction' => 0,
  1134. 'enabled' => 1,
  1135. 'plurals' => 0,
  1136. 'formula' => '',
  1137. 'domain' => '',
  1138. 'prefix' => '',
  1139. 'weight' => 0,
  1140. 'javascript' => '',
  1141. );
  1142. }
  1143. return $current;
  1144. }
  1145. /**
  1146. * Implements hook_features_ignore().
  1147. */
  1148. function features_features_ignore($component) {
  1149. // Determine which keys need to be ignored for override diff for various components.
  1150. // Value is how many levels deep the key is.
  1151. $ignores = array();
  1152. switch ($component) {
  1153. case 'views_view':
  1154. $ignores['current_display'] = 0;
  1155. $ignores['display_handler'] = 0;
  1156. $ignores['handler'] = 2;
  1157. $ignores['query'] = 0;
  1158. $ignores['localization_plugin'] = 0;
  1159. // Views automatically adds these two on export to set values.
  1160. $ignores['api_version'] = 0;
  1161. $ignores['disabled'] = 0;
  1162. break;
  1163. case 'image':
  1164. $ignores['module'] = 0;
  1165. $ignores['name'] = 0;
  1166. $ignores['storage'] = 0;
  1167. // Various properties are loaded into the effect in image_styles.
  1168. $ignores['summary theme'] = 2;
  1169. $ignores['module'] = 2;
  1170. $ignores['label'] = 2;
  1171. $ignores['help'] = 2;
  1172. $ignores['form callback'] = 2;
  1173. $ignores['effect callback'] = 2;
  1174. $ignores['dimensions callback'] = 2;
  1175. break;
  1176. case 'field':
  1177. $ignores['locked'] = 1;
  1178. break;
  1179. case 'field_base':
  1180. $ignores['indexes'] = 0;
  1181. break;
  1182. case 'taxonomy':
  1183. $ignores['hierarchy'] = 0;
  1184. }
  1185. return $ignores;
  1186. }