features.module 39 KB

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