features.module 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641
  1. <?php
  2. /**
  3. * @file
  4. * Main *.module file for the 'features' module.
  5. */
  6. define('FEATURES_API', '2');
  7. define('FEATURES_MODULE_ENABLED', 1);
  8. define('FEATURES_MODULE_DISABLED', 0);
  9. define('FEATURES_MODULE_MISSING', -1);
  10. define('FEATURES_MODULE_CONFLICT', 2);
  11. define('FEATURES_REBUILDABLE', -1);
  12. define('FEATURES_DEFAULT', 0);
  13. define('FEATURES_OVERRIDDEN', 1);
  14. define('FEATURES_NEEDS_REVIEW', 2);
  15. define('FEATURES_REBUILDING', 3);
  16. define('FEATURES_CONFLICT', 4);
  17. define('FEATURES_DISABLED', 5);
  18. define('FEATURES_CHECKING', 6);
  19. define('FEATURES_ALTER_TYPE_NORMAL', 'normal');
  20. define('FEATURES_ALTER_TYPE_INLINE', 'inline');
  21. define('FEATURES_ALTER_TYPE_NONE', 'none');
  22. /**
  23. * Duration of rebuild semaphore: 10 minutes.
  24. */
  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. /* @see \drupal_get_form() */
  68. 'page callback' => 'drupal_get_form',
  69. /* @see \features_admin_form() */
  70. 'page arguments' => array('features_admin_form'),
  71. 'type' => MENU_NORMAL_ITEM,
  72. 'file' => 'features.admin.inc',
  73. );
  74. $items['admin/structure/features/cleanup'] = array(
  75. 'title' => 'Cleanup',
  76. 'description' => 'Clear cache after enabling/disabling a feature.',
  77. /* @see \features_cleanup() */
  78. 'page callback' => 'features_cleanup',
  79. 'type' => MENU_CALLBACK,
  80. 'file' => 'features.admin.inc',
  81. 'weight' => 1,
  82. );
  83. $items['admin/structure/features/manage'] = array(
  84. 'title' => 'Manage',
  85. 'description' => 'Enable and disable features.',
  86. /* @see \drupal_get_form() */
  87. 'page callback' => 'drupal_get_form',
  88. /* @see \features_admin_form() */
  89. 'page arguments' => array('features_admin_form'),
  90. 'type' => MENU_DEFAULT_LOCAL_TASK,
  91. 'file' => 'features.admin.inc',
  92. );
  93. $items['admin/structure/features/create'] = array(
  94. 'title' => 'Create feature',
  95. 'description' => 'Create a new feature.',
  96. /* @see \drupal_get_form() */
  97. 'page callback' => 'drupal_get_form',
  98. /* @see \features_export_form() */
  99. 'page arguments' => array('features_export_form'),
  100. /* @see \user_access() */
  101. 'access callback' => 'user_access',
  102. 'access arguments' => array('administer features'),
  103. 'type' => MENU_LOCAL_TASK,
  104. 'file' => "features.admin.inc",
  105. 'weight' => 10,
  106. );
  107. $items['admin/structure/features/settings'] = array(
  108. 'title' => 'Settings',
  109. 'description' => 'Adjust settings for using features module.',
  110. /* @see \drupal_get_form() */
  111. 'page callback' => 'drupal_get_form',
  112. /* @see \features_settings_form() */
  113. 'page arguments' => array('features_settings_form'),
  114. /* @see \user_access() */
  115. 'access callback' => 'user_access',
  116. 'access arguments' => array('administer features'),
  117. 'type' => MENU_LOCAL_TASK,
  118. 'file' => "features.admin.inc",
  119. 'weight' => 11,
  120. );
  121. /* @see \feature_load() - the wildcard loader callback. */
  122. $items['admin/structure/features/%feature'] = array(
  123. /* @see \features_get_feature_title() */
  124. 'title callback' => 'features_get_feature_title',
  125. 'title arguments' => array(3),
  126. 'description' => 'Display components of a feature.',
  127. /* @see \drupal_get_form() */
  128. 'page callback' => 'drupal_get_form',
  129. /* @see \features_admin_components() */
  130. 'page arguments' => array('features_admin_components', 3),
  131. 'load arguments' => array(TRUE),
  132. /* @see \user_access() */
  133. 'access callback' => 'user_access',
  134. 'access arguments' => array('administer features'),
  135. 'type' => MENU_CALLBACK,
  136. 'file' => 'features.admin.inc',
  137. );
  138. $items['admin/structure/features/%feature/view'] = array(
  139. 'title' => 'View',
  140. 'description' => 'Display components of a feature.',
  141. /* @see \user_access() */
  142. 'access callback' => 'user_access',
  143. 'access arguments' => array('administer features'),
  144. 'type' => MENU_DEFAULT_LOCAL_TASK,
  145. 'weight' => -10,
  146. );
  147. $items['admin/structure/features/%feature/recreate'] = array(
  148. 'title' => 'Recreate',
  149. 'description' => 'Recreate an existing feature.',
  150. /* @see \drupal_get_form() */
  151. 'page callback' => 'drupal_get_form',
  152. /* @see \features_export_form() */
  153. 'page arguments' => array('features_export_form', 3),
  154. 'load arguments' => array(TRUE),
  155. /* @see \user_access() */
  156. 'access callback' => 'user_access',
  157. 'access arguments' => array('administer features'),
  158. 'type' => MENU_LOCAL_TASK,
  159. 'file' => "features.admin.inc",
  160. 'weight' => 11,
  161. );
  162. if (module_exists('diff')) {
  163. $items['admin/structure/features/%feature/diff'] = array(
  164. 'title' => 'Review overrides',
  165. 'description' => 'Compare default and current feature.',
  166. /* @see \features_feature_diff() */
  167. 'page callback' => 'features_feature_diff',
  168. 'page arguments' => array(3, 5),
  169. 'load arguments' => array(TRUE),
  170. /* @see \features_access_override_actions() */
  171. 'access callback' => 'features_access_override_actions',
  172. 'access arguments' => array(3),
  173. 'type' => MENU_LOCAL_TASK,
  174. 'file' => 'features.admin.inc',
  175. );
  176. }
  177. $items['admin/structure/features/%feature/lock'] = array(
  178. 'title' => 'Lock',
  179. 'description' => 'Lock a feature or components.',
  180. /* @see \features_admin_lock() */
  181. 'page callback' => 'features_admin_lock',
  182. 'page arguments' => array(3, 5, 6),
  183. 'load arguments' => array(TRUE),
  184. 'access arguments' => array('administer features'),
  185. 'type' => MENU_CALLBACK,
  186. 'file' => 'features.admin.inc',
  187. );
  188. $items['admin/structure/features/%feature/status'] = array(
  189. 'title' => 'Status',
  190. 'description' => 'Javascript status call back.',
  191. /* @see \features_feature_status() */
  192. 'page callback' => 'features_feature_status',
  193. 'page arguments' => array(3),
  194. 'load arguments' => array(TRUE),
  195. /* @see \user_access() */
  196. 'access callback' => 'user_access',
  197. 'access arguments' => array('administer features'),
  198. 'type' => MENU_CALLBACK,
  199. 'file' => 'features.admin.inc',
  200. );
  201. $items['features/autocomplete/packages'] = array(
  202. /* @see \features_autocomplete_packages() */
  203. 'page callback' => 'features_autocomplete_packages',
  204. 'access arguments' => array('administer features'),
  205. 'type' => MENU_CALLBACK,
  206. 'file' => 'features.admin.inc',
  207. );
  208. $items['features/ajaxcallback/%'] = array(
  209. 'title' => 'AJAX callback',
  210. 'description' => 'Return components of a feature.',
  211. /* @see \features_export_components_json() */
  212. 'page callback' => 'features_export_components_json',
  213. 'page arguments' => array(2),
  214. /* @see \user_access() */
  215. 'access callback' => 'user_access',
  216. 'access arguments' => array('administer features'),
  217. 'type' => MENU_CALLBACK,
  218. 'file' => 'features.admin.inc',
  219. );
  220. foreach ($items as $path => $item) {
  221. if (!isset($item['access callback'])) {
  222. /* @see \user_access() */
  223. $items[$path]['access callback'] = 'user_access';
  224. $items[$path]['access arguments'] = array('manage features');
  225. }
  226. }
  227. return $items;
  228. }
  229. /**
  230. * Implements hook_theme().
  231. */
  232. function features_theme() {
  233. $base = array(
  234. 'path' => drupal_get_path('module', 'features') . '/theme',
  235. 'file' => 'theme.inc',
  236. );
  237. $items = array();
  238. /* @see \theme_features_module_status() */
  239. $items['features_module_status'] = array(
  240. 'variables' => array('module' => NULL, 'status' => NULL),
  241. ) + $base;
  242. /* @see \theme_features_components() */
  243. $items['features_components'] = array(
  244. 'variables' => array('info' => NULL, 'sources' => NULL),
  245. ) + $base;
  246. /* @see \theme_features_component_key() */
  247. $items['features_component_key'] = $base;
  248. /* @see \theme_features_component_list() */
  249. $items['features_component_list'] = array(
  250. 'variables' => array('components' => array(), 'source' => array(), 'conflicts' => array()),
  251. ) + $base;
  252. /* @see \theme_features_storage_link() */
  253. $items['features_storage_link'] = array(
  254. 'variables' => array('storage' => NULL, 'text' => NULL, 'path' => NULL, 'options' => array()),
  255. ) + $base;
  256. /* @see \theme_features_lock_link() */
  257. $items['features_lock_link'] = array(
  258. 'variables' => array('feature' => NULL, 'component' => NULL, 'locked' => FALSE),
  259. ) + $base;
  260. /* @see \theme_features_form_components() */
  261. $items['features_form_components'] =
  262. /* @see \theme_features_form_export() */
  263. $items['features_form_export'] =
  264. /* @see \theme_features_form_package() */
  265. $items['features_form_package'] = array(
  266. 'render element' => 'form',
  267. ) + $base;
  268. /* @see \theme_features_form_buttons() */
  269. $items['features_form_buttons'] = array(
  270. 'render element' => 'element',
  271. ) + $base;
  272. // Theme hook for 'features_admin_components' form.
  273. /* @see \template_preprocess_features_admin_components() */
  274. /* @see \features_admin_components() */
  275. $items['features_admin_components'] = array(
  276. 'render element' => 'form',
  277. 'template' => 'features-admin-components',
  278. ) + $base;
  279. return $items;
  280. }
  281. /**
  282. * Implements hook_flush_caches().
  283. */
  284. function features_flush_caches() {
  285. if (($modules_changed = variable_get('features_modules_changed', FALSE)) || variable_get('features_rebuild_on_flush', TRUE)) {
  286. if ($modules_changed) {
  287. variable_set('features_modules_changed', FALSE);
  288. }
  289. features_rebuild();
  290. // Don't flush the modules cache during installation, for performance
  291. // reasons.
  292. if (variable_get('install_task') == 'done') {
  293. features_get_modules(NULL, TRUE);
  294. }
  295. }
  296. if (db_table_exists('cache_features')) {
  297. return array('cache_features');
  298. }
  299. return array();
  300. }
  301. /**
  302. * Implements hook_form().
  303. */
  304. function features_form($node, $form_state) {
  305. return node_content_form($node, $form_state);
  306. }
  307. /**
  308. * Implements hook_permission().
  309. */
  310. function features_permission() {
  311. return array(
  312. 'administer features' => array(
  313. 'title' => t('Administer features'),
  314. 'description' => t('Perform administration tasks on features.'),
  315. 'restrict access' => TRUE,
  316. ),
  317. 'manage features' => array(
  318. 'title' => t('Manage features'),
  319. 'description' => t('View, enable and disable features.'),
  320. 'restrict access' => TRUE,
  321. ),
  322. 'generate features' => array(
  323. 'title' => t('Generate features'),
  324. 'description' => t('Allow feature exports to be generated and written directly to site.'),
  325. 'restrict access' => TRUE,
  326. ),
  327. 'rename features' => array(
  328. 'title' => t('Edit feature machine name'),
  329. 'description' => t('Allows editing machine name of a disabled feature'),
  330. 'restrict access' => TRUE,
  331. ),
  332. );
  333. }
  334. /**
  335. * Implements hook_help().
  336. */
  337. function features_help($path, $arg) {
  338. switch ($path) {
  339. case 'admin/help#features':
  340. // Use dirname(__FILE__) instead of __DIR__ for PHP < 5.3 (ouch).
  341. $path_to_readme = dirname(__FILE__) . '/README.txt';
  342. if (!is_readable($path_to_readme) || !is_file($path_to_readme)) {
  343. // The README.txt is not readable, or does not exist.
  344. // This can happen in sites that remove or hide README.txt files on
  345. // deployment. See #2473935.
  346. return NULL;
  347. }
  348. $output = file_get_contents($path_to_readme);
  349. return module_exists('markdown') ? filter_xss_admin(module_invoke('markdown', 'filter', 'process', 0, -1, $output)) : '<pre>' . check_plain($output) . '</pre>';
  350. case 'admin/build/features':
  351. 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>';
  352. }
  353. }
  354. /**
  355. * Implements hook_modules_disabled().
  356. */
  357. function features_modules_disabled($modules) {
  358. // Go through all modules and gather features that can be disabled.
  359. $items = array();
  360. foreach ($modules as $module) {
  361. if ($feature = features_load_feature($module)) {
  362. $items[$module] = array_keys($feature->info['features']);
  363. }
  364. }
  365. if (!empty($items)) {
  366. _features_restore('disable', $items);
  367. // Rebuild the list of features includes.
  368. features_include(TRUE);
  369. }
  370. }
  371. /**
  372. * Implements hook_modules_enabled().
  373. */
  374. function features_modules_enabled($modules) {
  375. // Allow distributions to disable this behavior and rebuild the features
  376. // manually inside a batch.
  377. if (!variable_get('features_rebuild_on_module_install', TRUE)) {
  378. return;
  379. }
  380. // Mark modules as being changed for test in features_flush_caches.
  381. variable_set('features_modules_changed', TRUE);
  382. // Go through all modules and gather features that can be enabled.
  383. $items = array();
  384. foreach ($modules as $module) {
  385. if ($feature = features_load_feature($module)) {
  386. $items[$module] = array_keys($feature->info['features']);
  387. }
  388. }
  389. if (!empty($items)) {
  390. // Need to include any new files.
  391. // @todo Redo function so can take in list of modules to include.
  392. features_include_defaults(NULL, TRUE);
  393. _features_restore('enable', $items);
  394. // Rebuild the list of features includes.
  395. features_include(TRUE);
  396. // Reorders components to match hook order and removes non-existant.
  397. $all_components = array_keys(features_get_components());
  398. foreach ($items as $module => $components) {
  399. $items[$module] = array_intersect($all_components, $components);
  400. }
  401. _features_restore('rebuild', $items);
  402. }
  403. }
  404. /**
  405. * Includes PHP files where features components are defined.
  406. *
  407. * The function does the following:
  408. * - Include the files in 'includes/features.*.inc', where features defines
  409. * components on behalf of other modules.
  410. * - Dynamically declare the functions for features components based on ctools.
  411. * - Include files that contain component hooks, as specified in the 'file'
  412. * property of each component.
  413. *
  414. * @param bool $reset
  415. * (optional) If TRUE, the operation will run again even if it has run before
  416. * in the same request. This is useful e.g. if additional modules were enabled
  417. * since then.
  418. */
  419. function features_include($reset = FALSE) {
  420. static $once;
  421. if (!isset($once) || $reset) {
  422. $once = TRUE;
  423. // Features provides integration on behalf of these modules.
  424. // The features include provides handling for the feature dependencies.
  425. // Note that ctools is placed last because it implements hooks "dynamically"
  426. // for other modules.
  427. $modules = array('features', 'block', 'contact', 'context', 'field', 'filter', 'image', 'locale', 'menu', 'node', 'taxonomy', 'user', 'views', 'ctools');
  428. foreach (array_filter($modules, 'module_exists') as $module) {
  429. module_load_include('inc', 'features', "includes/features.$module");
  430. }
  431. if (module_exists('ctools')) {
  432. // Finally, add ctools eval'd implementations.
  433. ctools_features_declare_functions($reset);
  434. }
  435. // Clear static cache, since we've now included new implementers.
  436. foreach (features_get_components(NULL, 'file', $reset) as $file) {
  437. if (is_file(DRUPAL_ROOT . '/' . $file)) {
  438. require_once DRUPAL_ROOT . '/' . $file;
  439. }
  440. }
  441. }
  442. }
  443. /**
  444. * Includes PHP files that contain (generated) default hook implementations.
  445. *
  446. * The file to include for each feature module for a given component can be
  447. * specified in the component info in hook_features_api().
  448. *
  449. * @param string[]|string|null $components
  450. * (optional) A list of component names, or one component name, or NULL.
  451. * If provided, only the files for the specified components are included.
  452. * @param bool $reset
  453. * (optional) If TRUE, the operation will run again even if it has already run
  454. * during the same request. This is useful e.g. when new modules were enabled.
  455. */
  456. function features_include_defaults($components = NULL, $reset = FALSE) {
  457. static $include_components;
  458. // Build an array of components that require inclusion:
  459. // Views, CTools components and those using FEATURES_DEFAULTS_INCLUDED.
  460. if (!isset($include_components) || $reset) {
  461. $include_components = features_get_components();
  462. foreach ($include_components as $component => $info) {
  463. if (!isset($info['api']) && (!isset($info['default_file']) || $info['default_file'] !== FEATURES_DEFAULTS_INCLUDED)) {
  464. unset($include_components[$component]);
  465. }
  466. }
  467. }
  468. // If components are specified, only include for the specified components.
  469. if (isset($components)) {
  470. $components = is_array($components) ? $components : array($components);
  471. }
  472. // Use all include components if none are explicitly specified.
  473. else {
  474. $components = array_keys($include_components);
  475. }
  476. foreach ($components as $component) {
  477. if (isset($include_components[$component])) {
  478. $info = $include_components[$component];
  479. // Inclusion of ctools components.
  480. if (isset($info['api'], $info['module'], $info['current_version'])) {
  481. ctools_include('plugins');
  482. ctools_plugin_api_include($info['module'], $info['api'], $info['current_version'], $info['current_version']);
  483. }
  484. // Inclusion of defaults for components using FEATURES_DEFAULTS_INCLUDED.
  485. else {
  486. $features = isset($features) ? $features : features_get_features(NULL, $reset);
  487. foreach ($features as $feature) {
  488. $filename = isset($info['default_file']) && $info['default_file'] == FEATURES_DEFAULTS_CUSTOM ? $info['default_filename'] : "features.{$component}";
  489. if (module_exists($feature->name) && isset($feature->info['features'][$component])) {
  490. module_load_include('inc', $feature->name, "{$feature->name}.$filename");
  491. }
  492. }
  493. }
  494. }
  495. }
  496. }
  497. /**
  498. * Menu wildcard loader for '%feature'.
  499. *
  500. * @param string $name
  501. * Path fragment.
  502. * @param bool $reset
  503. * (optional) If TRUE, the cache will be cleared before fetching.
  504. *
  505. * @return \stdClass|false
  506. * Object with module info, or FALSE if not found or not a feature.
  507. */
  508. function feature_load($name, $reset = FALSE) {
  509. return features_load_feature($name, $reset);
  510. }
  511. /**
  512. * Gets an object with information about a feature module.
  513. *
  514. * @param string $name
  515. * Name of a (feature) module.
  516. * @param bool $reset
  517. * (optional) If TRUE, the cache will be cleared before fetching.
  518. *
  519. * @return \stdClass|false
  520. * Object with module info, or FALSE if not found or not a feature.
  521. */
  522. function features_load_feature($name, $reset = FALSE) {
  523. // Use an alternative code path during installation, for better performance.
  524. if (variable_get('install_task') != 'done') {
  525. static $features;
  526. if (!isset($features[$name])) {
  527. // Set defaults for module info.
  528. $defaults = array(
  529. 'dependencies' => array(),
  530. 'description' => '',
  531. 'package' => 'Other',
  532. 'version' => NULL,
  533. 'php' => DRUPAL_MINIMUM_PHP,
  534. 'files' => array(),
  535. 'bootstrap' => 0,
  536. );
  537. $info = drupal_parse_info_file(drupal_get_path('module', $name) . '/' . $name . '.info');
  538. $features[$name] = FALSE;
  539. if (!empty($info['features']) && empty($info['hidden'])) {
  540. // Build a fake file object with the data needed during installation.
  541. $features[$name] = new stdClass();
  542. $features[$name]->name = $name;
  543. $features[$name]->filename = drupal_get_path('module', $name) . '/' . $name . '.module';
  544. $features[$name]->type = 'module';
  545. $features[$name]->status = module_exists($name);
  546. $features[$name]->info = $info + $defaults;
  547. }
  548. }
  549. return $features[$name];
  550. }
  551. else {
  552. return features_get_features($name, $reset);
  553. }
  554. }
  555. /**
  556. * Return a module 'object' including .info information.
  557. *
  558. * @param string|null $name
  559. * (optional) The name of the module to retrieve information for. If omitted,
  560. * an array of all available modules will be returned.
  561. * @param bool $reset
  562. * (optional) If TRUE, the cache will be reset before fetching.
  563. *
  564. * @return \stdClass[]|\stdClass|false
  565. * If $name is NULL: $[$module] = $module_info_object
  566. * If $name is not NULL: A specific module info object.
  567. * If no matching module is found, FALSE is returned.
  568. */
  569. function features_get_modules($name = NULL, $reset = FALSE) {
  570. return features_get_info('module', $name, $reset);
  571. }
  572. /**
  573. * Gets component types declared with hook_features_api().
  574. *
  575. * @param string $component
  576. * (optional) A specific type of component that supports features.
  577. * E.g. "field_instance".
  578. * @param string $key
  579. * (optional) The name of a property to retrieve for each component type.
  580. * E.g. "name", "default_hook" or "base".
  581. * @param bool $reset
  582. * (optional) If TRUE, the components cache will be cleared.
  583. *
  584. * @return mixed|mixed[]|array|array[]|null
  585. * Return value depending on parameters:
  586. * - If both $component and $key are provided:
  587. * A specific value from the info of the specified component.
  588. * - If only $key is provided:
  589. * List with the same value from each component.
  590. * - If only $component is provided:
  591. * Info array of the specified component.
  592. * If the component is not found, a notice is triggered, and NULL is returned.
  593. * - If neither $component nor $key are provided:
  594. * List of component info arrays, keyed by component.
  595. *
  596. * @see \hook_features_api()
  597. */
  598. function features_get_components($component = NULL, $key = NULL, $reset = FALSE) {
  599. features_include();
  600. $components = &drupal_static(__FUNCTION__);
  601. $component_by_key = &drupal_static(__FUNCTION__ . '_by_key');
  602. if ($reset || !isset($components) || !isset($component_by_key)) {
  603. $components = $component_by_key = array();
  604. if (!$reset && ($cache = cache_get('features_api', 'cache_features'))) {
  605. $components = $cache->data;
  606. }
  607. else {
  608. $components = module_invoke_all('features_api');
  609. drupal_alter('features_api', $components);
  610. cache_set('features_api', $components, 'cache_features');
  611. }
  612. foreach ($components as $component_type => $component_information) {
  613. foreach ($component_information as $component_key => $component_value) {
  614. $component_by_key[$component_key][$component_type] = $component_value;
  615. }
  616. }
  617. }
  618. if ($key && $component) {
  619. return !empty($components[$component][$key]) ? $components[$component][$key] : NULL;
  620. }
  621. elseif ($key) {
  622. return !empty($component_by_key[$key]) ? $component_by_key[$key] : array();
  623. }
  624. elseif ($component) {
  625. return $components[$component];
  626. }
  627. return $components;
  628. }
  629. /**
  630. * Returns components that are offered as an option on feature creation.
  631. *
  632. * @return array[]
  633. * Components from hook_features_api() where 'features_source' is TRUE.
  634. * Format: $[$component] = $component_info
  635. */
  636. function features_get_feature_components() {
  637. return array_intersect_key(features_get_components(), array_filter(features_get_components(NULL, 'feature_source')));
  638. }
  639. /**
  640. * Invoke a component callback.
  641. *
  642. * @param string $component
  643. * Component name, e.g. 'field_instance'.
  644. * @param string $callback
  645. * Component hook name, e.g. 'features_revert'.
  646. *
  647. * @return mixed|void
  648. * The return value from the component hook callback.
  649. * If the callback does not exist, NULL ("void") is returned.
  650. *
  651. * @todo Explicitly return NULL if hook not found, replace the 'void' doc.
  652. */
  653. function features_invoke($component, $callback) {
  654. $args = func_get_args();
  655. unset($args[0], $args[1]);
  656. // Append the component name to the arguments.
  657. $args[] = $component;
  658. if ($function = features_hook($component, $callback)) {
  659. return call_user_func_array($function, $args);
  660. }
  661. }
  662. /**
  663. * Checks whether a component implements the given hook.
  664. *
  665. * @param string $component
  666. * A component name, e.g. 'field_instance'.
  667. * @param string $hook
  668. * A component hook name, e.g. 'features_revert'.
  669. * @param bool $reset
  670. * (obsolete) The parameter has no effect.
  671. *
  672. * @return string|false
  673. * The function implementing the hook, or FALSE.
  674. * E.g. 'field_instance_features_revert'.
  675. */
  676. function features_hook($component, $hook, $reset = FALSE) {
  677. // Determine the function callback base.
  678. $base = features_get_components($component, 'base');
  679. $base = isset($base) ? $base : $component;
  680. return function_exists($base . '_' . $hook) ? $base . '_' . $hook : FALSE;
  681. }
  682. /**
  683. * Enables an array of (feature) modules and their dependencies.
  684. *
  685. * Modules that are already enabled and installed are ignored.
  686. * Consider this a helper or extension to module_enable().
  687. *
  688. * @param string[] $modules
  689. * An array of module names or dependency strings.
  690. *
  691. * @see \module_enable()
  692. */
  693. function features_install_modules($modules) {
  694. variable_set('features_modules_changed', TRUE);
  695. module_load_include('inc', 'features', 'features.export');
  696. $files = system_rebuild_module_data();
  697. // Build maximal list of dependencies.
  698. $install = array();
  699. foreach ($modules as $name) {
  700. // Parse the dependency string into the module name and version information.
  701. $parsed_name = drupal_parse_dependency($name);
  702. $name = $parsed_name['name'];
  703. if ($file = $files[$name]) {
  704. $install[] = $name;
  705. if (!empty($file->info['dependencies'])) {
  706. $install = array_merge($install, _features_export_maximize_dependencies($file->info['dependencies']));
  707. }
  708. }
  709. }
  710. // Filter out enabled modules.
  711. $enabled = array_filter($install, 'module_exists');
  712. $install = array_diff($install, $enabled);
  713. if (!empty($install)) {
  714. // Make sure the install API is available.
  715. $install = array_unique($install);
  716. include_once DRUPAL_ROOT . '/' . './includes/install.inc';
  717. module_enable($install);
  718. }
  719. }
  720. /**
  721. * Gets a list of module info objects that are features.
  722. *
  723. * @param string|null $name
  724. * (optional) Name of a feature module.
  725. * @param bool $reset
  726. * (optional) If TRUE, the cache will be cleared before fetching.
  727. *
  728. * @return \stdClass[]|\stdClass|false
  729. * If $name is NULL: $[$module] = $module_info_object
  730. * If $name is not NULL: A specific module info object.
  731. * If no matching module is found, FALSE is returned.
  732. */
  733. function features_get_features($name = NULL, $reset = FALSE) {
  734. return features_get_info('feature', $name, $reset);
  735. }
  736. /**
  737. * Retrieves module info from the system table.
  738. *
  739. * @param string $type
  740. * (optional) One of 'module' or 'feature'.
  741. * If 'module', all modules are returned.
  742. * If 'feature', only those modules are returned that act as features.
  743. * @param string|null $name
  744. * (optional) Name of a (feature or not) module.
  745. * If the module is not a feature, and $type is 'feature', FALSE will be
  746. * returned instead.
  747. * @param bool $reset
  748. * (optional) If TRUE, the cache will be cleared before fetching.
  749. *
  750. * @return \stdClass[]|\stdClass|false
  751. * If $name is NULL or empty:
  752. * List of modules according to $type.
  753. * Format: $[$module] = $module_info_object
  754. * If $name is not NULL or empty:
  755. * Module info object, if it matches $type.
  756. * FALSE, if the module does not exist, or if $type is 'feature' and the
  757. * module is not a feature.
  758. * If $type is something other than 'module' or 'feature':
  759. * FALSE, no matter the other parameters.
  760. *
  761. * @see system_rebuild_module_data()
  762. */
  763. function features_get_info($type = 'module', $name = NULL, $reset = FALSE) {
  764. static $cache;
  765. if (!isset($cache)) {
  766. $cache = cache_get('features_module_info', 'cache_features');
  767. }
  768. if (empty($cache) || $reset) {
  769. $data = array(
  770. 'feature' => array(),
  771. 'module' => array(),
  772. );
  773. $ignored = variable_get('features_ignored_orphans', array());
  774. $files = system_rebuild_module_data();
  775. foreach ($files as $row) {
  776. // Remove modification timestamp, added in Drupal 7.33.
  777. if (isset($row->info['mtime'])) {
  778. unset($row->info['mtime']);
  779. }
  780. // Avoid false-reported feature overrides for php = 5.2.4 line in .info
  781. // file.
  782. if (isset($row->info['php'])) {
  783. unset($row->info['php']);
  784. }
  785. // If module is no longer enabled, remove it from the ignored orphans
  786. // list.
  787. if (in_array($row->name, $ignored, TRUE) && !$row->status) {
  788. $key = array_search($row->name, $ignored, TRUE);
  789. unset($ignored[$key]);
  790. }
  791. if (!empty($row->info['features'])) {
  792. // Fix css/js paths.
  793. if (!empty($row->info['stylesheets'])) {
  794. foreach ($row->info['stylesheets'] as $media => $css) {
  795. $row->info['stylesheets'][$media] = array_keys($css);
  796. }
  797. }
  798. if (!empty($row->info['scripts'])) {
  799. $row->info['scripts'] = array_keys($row->info['scripts']);
  800. }
  801. // Rework the features array, to change the vocabulary permission
  802. // features.
  803. foreach ($row->info['features'] as $component => $features) {
  804. if ($component == 'user_permission') {
  805. foreach ($features as $key => $feature) {
  806. // Export vocabulary permissions using the machine name, instead
  807. // of vocabulary id.
  808. _user_features_change_term_permission($feature);
  809. $row->info['features'][$component][$key] = $feature;
  810. }
  811. }
  812. }
  813. $data['feature'][$row->name] = $row;
  814. $data['feature'][$row->name]->components = array_keys($row->info['features']);
  815. if (!empty($row->info['dependencies'])) {
  816. $data['feature'][$row->name]->components[] = 'dependencies';
  817. }
  818. }
  819. $data['module'][$row->name] = $row;
  820. }
  821. // Sort features according to dependencies.
  822. // @see install_profile_modules()
  823. $required = array();
  824. $non_required = array();
  825. $modules = array_keys($data['feature']);
  826. foreach ($modules as $module) {
  827. if ($files[$module]->requires) {
  828. $modules = array_merge($modules, array_keys($files[$module]->requires));
  829. }
  830. }
  831. $modules = array_unique($modules);
  832. foreach ($modules as $module) {
  833. if (!empty($files[$module]->info['features'])) {
  834. if (!empty($files[$module]->info['required'])) {
  835. $required[$module] = $files[$module]->sort;
  836. }
  837. else {
  838. $non_required[$module] = $files[$module]->sort;
  839. }
  840. }
  841. }
  842. arsort($required);
  843. arsort($non_required);
  844. $sorted = array();
  845. foreach ($required + $non_required as $module => $weight) {
  846. $sorted[$module] = $data['feature'][$module];
  847. }
  848. $data['feature'] = $sorted;
  849. variable_set('features_ignored_orphans', $ignored);
  850. cache_set('features_module_info', $data, 'cache_features');
  851. $cache = new stdClass();
  852. $cache->data = $data;
  853. }
  854. if (!empty($name)) {
  855. return !empty($cache->data[$type][$name]) ? clone $cache->data[$type][$name] : FALSE;
  856. }
  857. return !empty($cache->data[$type]) ? $cache->data[$type] : FALSE;
  858. }
  859. /**
  860. * Generate an array of feature dependencies that have been orphaned.
  861. */
  862. function features_get_orphans($reset = FALSE) {
  863. static $orphans;
  864. if (!isset($orphans) || $reset) {
  865. module_load_include('inc', 'features', 'features.export');
  866. $orphans = array();
  867. // Build a list of all dependencies for enabled and disabled features.
  868. $dependencies = array('enabled' => array(), 'disabled' => array());
  869. $features = features_get_features();
  870. foreach ($features as $feature) {
  871. $key = module_exists($feature->name) ? 'enabled' : 'disabled';
  872. if (!empty($feature->info['dependencies'])) {
  873. $dependencies[$key] = array_merge($dependencies[$key], _features_export_maximize_dependencies($feature->info['dependencies']));
  874. }
  875. }
  876. $dependencies['enabled'] = array_unique($dependencies['enabled']);
  877. $dependencies['disabled'] = array_unique($dependencies['disabled']);
  878. // Find the list of orphaned modules.
  879. $orphaned = array_diff($dependencies['disabled'], $dependencies['enabled']);
  880. $orphaned = array_intersect($orphaned, module_list(FALSE, FALSE));
  881. $orphaned = array_diff($orphaned, drupal_required_modules());
  882. $orphaned = array_diff($orphaned, array('features'));
  883. // Build final list of modules that can be disabled.
  884. $modules = features_get_modules(NULL, TRUE);
  885. $enabled = module_list();
  886. _module_build_dependencies($modules);
  887. foreach ($orphaned as $module) {
  888. if (!empty($modules[$module]->required_by)) {
  889. foreach ($modules[$module]->required_by as $module_name => $dependency) {
  890. $modules[$module]->required_by[$module_name] = $dependency['name'];
  891. }
  892. // Determine whether any dependents are actually enabled.
  893. $dependents = array_intersect($modules[$module]->required_by, $enabled);
  894. if (empty($dependents)) {
  895. $info = features_get_modules($module);
  896. $orphans[$module] = $info;
  897. }
  898. }
  899. }
  900. }
  901. return $orphans;
  902. }
  903. /**
  904. * Detects potential conflicts between features that provide the same items.
  905. *
  906. * @param bool $reset
  907. * If TRUE, relevant caches will be reset.
  908. *
  909. * @return string[][][][]
  910. * Format: $[$module_A][$module_B][$component][] = $name
  911. * E.g. $['myfeature']['otherfeature']['node'][] = 'article'.
  912. * Nested array/map of (feature) modules that have conflicting features.
  913. */
  914. function features_get_conflicts($reset = FALSE) {
  915. $conflicts = array();
  916. $component_info = features_get_components();
  917. $map = features_get_component_map(NULL, $reset);
  918. foreach ($map as $type => $components) {
  919. // Only check conflicts for components we know about.
  920. if (isset($component_info[$type])) {
  921. foreach ($components as $component => $modules) {
  922. if (isset($component_info[$type]['duplicates']) && $component_info[$type]['duplicates'] == FEATURES_DUPLICATES_ALLOWED) {
  923. continue;
  924. }
  925. elseif (count($modules) > 1) {
  926. foreach ($modules as $module) {
  927. if (!isset($conflicts[$module])) {
  928. $conflicts[$module] = array();
  929. }
  930. foreach ($modules as $m) {
  931. if ($m != $module) {
  932. $conflicts[$module][$m][$type][] = $component;
  933. }
  934. }
  935. }
  936. }
  937. }
  938. }
  939. }
  940. return $conflicts;
  941. }
  942. /**
  943. * Provide a component to feature map.
  944. *
  945. * @param string|null $key
  946. * (optional) Component type to filter by.
  947. * @param bool $reset
  948. * (optional) If TRUE, relevant caches are reset.
  949. *
  950. * @return string[][][]|string[][]
  951. * Format:
  952. * - If $key is NULL:
  953. * $[$component][$name][] = $module
  954. * - If $key is not NULL:
  955. * $[$name][] = $module
  956. */
  957. function features_get_component_map($key = NULL, $reset = FALSE) {
  958. static $map;
  959. if (!isset($map) || $reset) {
  960. $map = array();
  961. $features = features_get_features(NULL, $reset);
  962. foreach ($features as $feature) {
  963. foreach ($feature->info['features'] as $type => $components) {
  964. if (!isset($map[$type])) {
  965. $map[$type] = array();
  966. }
  967. foreach ($components as $component) {
  968. $map[$type][$component][] = $feature->name;
  969. }
  970. }
  971. }
  972. }
  973. if (isset($key)) {
  974. return isset($map[$key]) ? $map[$key] : array();
  975. }
  976. return $map;
  977. }
  978. /**
  979. * Checks whether a module is enabled, disabled or missing.
  980. *
  981. * @param string $module
  982. * A module name.
  983. *
  984. * @return int
  985. * One of the constants mentioned below, depending on the module status.
  986. *
  987. * @see FEATURES_MODULE_ENABLED
  988. * @see FEATURES_MODULE_DISABLED
  989. * @see FEATURES_MODULE_MISSING
  990. */
  991. function features_get_module_status($module) {
  992. if (module_exists($module)) {
  993. return FEATURES_MODULE_ENABLED;
  994. }
  995. elseif (features_get_modules($module)) {
  996. return FEATURES_MODULE_DISABLED;
  997. }
  998. else {
  999. return FEATURES_MODULE_MISSING;
  1000. }
  1001. }
  1002. /**
  1003. * Menu title callback.
  1004. *
  1005. * @param \stdClass $feature
  1006. * Feature module object.
  1007. *
  1008. * @return string
  1009. * The human name of the module, as specified in the *.info file.
  1010. */
  1011. function features_get_feature_title($feature) {
  1012. return $feature->info['name'];
  1013. }
  1014. /**
  1015. * Menu access callback for 'admin/structure/features/%feature/diff'.
  1016. *
  1017. * Checks whether a user should be able to access override actions for a given
  1018. * feature.
  1019. *
  1020. * @param \stdClass $feature
  1021. * Feature module object.
  1022. *
  1023. * @return bool
  1024. * TRUE to give access, FALSE to deny access.
  1025. */
  1026. function features_access_override_actions($feature) {
  1027. if (user_access('administer features')) {
  1028. static $access = array();
  1029. if (!isset($access[$feature->name])) {
  1030. // Set a value first. We may get called again from within
  1031. // features_detect_overrides().
  1032. $access[$feature->name] = FALSE;
  1033. features_include();
  1034. module_load_include('inc', 'features', 'features.export');
  1035. $access[$feature->name] = in_array(features_get_storage($feature->name), array(FEATURES_DEFAULT, FEATURES_OVERRIDDEN, FEATURES_NEEDS_REVIEW));
  1036. }
  1037. return $access[$feature->name];
  1038. }
  1039. return FALSE;
  1040. }
  1041. /**
  1042. * Implements hook_form_FORM_ID_alter() for 'system_modules' form().
  1043. */
  1044. function features_form_system_modules_alter(&$form) {
  1045. if (variable_get('features_rebuild_modules_page', FALSE)) {
  1046. features_rebuild();
  1047. }
  1048. }
  1049. /**
  1050. * Restore the specified modules to the default state.
  1051. *
  1052. * @param string $op
  1053. * One of 'revert', 'rebuild', 'disable', 'enable'.
  1054. * @param string[][] $items
  1055. * Modules and their components to be processed according to $op.
  1056. * If empty, all applicable feature modules will be processed.
  1057. * Format: $[$module_name][] = $component
  1058. * E.g. $['myfeature'][] = 'field_instance'.
  1059. */
  1060. function _features_restore($op, $items = array()) {
  1061. $lockable = FALSE;
  1062. // Set this variable in $conf if having timeout issues during install/rebuild.
  1063. if (variable_get('features_restore_time_limit_' . $op, FALSE) !== FALSE) {
  1064. drupal_set_time_limit(variable_get('features_restore_time_limit_' . $op, FALSE));
  1065. }
  1066. module_load_include('inc', 'features', 'features.export');
  1067. features_include();
  1068. switch ($op) {
  1069. case 'revert':
  1070. $restore_states = array(FEATURES_OVERRIDDEN, FEATURES_REBUILDABLE, FEATURES_NEEDS_REVIEW);
  1071. /* @see \hook_pre_features_revert() - module hook. */
  1072. /* @see \hook_features_revert() - component hook. */
  1073. /* @see \hook_post_features_revert() - module hook. */
  1074. $restore_hook = 'features_revert';
  1075. $log_action = 'Revert';
  1076. $lockable = TRUE;
  1077. break;
  1078. case 'rebuild':
  1079. $restore_states = array(FEATURES_REBUILDABLE);
  1080. /* @see \hook_pre_features_rebuild() - module hook. */
  1081. /* @see \hook_features_rebuild() - component hook. */
  1082. /* @see \hook_post_features_rebuild() - module hook. */
  1083. $restore_hook = 'features_rebuild';
  1084. $log_action = 'Rebuild';
  1085. $lockable = variable_get('features_lock_mode', 'all') == 'all';
  1086. break;
  1087. case 'disable':
  1088. /* @see \hook_pre_features_disable_feature() - module hook. */
  1089. /* @see \hook_features_disable_feature() - component hook. */
  1090. /* @see \hook_post_features_disable_feature() - module hook. */
  1091. $restore_hook = 'features_disable_feature';
  1092. $log_action = 'Disable';
  1093. break;
  1094. case 'enable':
  1095. /* @see \hook_pre_features_enable_feature() - module hook. */
  1096. /* @see \hook_features_enable_feature() - component hook. */
  1097. /* @see \hook_post_features_enable_feature() - module hook. */
  1098. $restore_hook = 'features_enable_feature';
  1099. $log_action = 'Enable';
  1100. break;
  1101. }
  1102. if (empty($items)) {
  1103. // Drush may execute a whole chain of commands that may trigger feature
  1104. // rebuilding multiple times during a single request. Make sure we do not
  1105. // rebuild the same cached list of modules over and over again by setting
  1106. // $reset to TRUE.
  1107. // Note: this may happen whenever more than one feature will be enabled
  1108. // in chain, for example also using features_install_modules().
  1109. $states = features_get_component_states(array(), ($op == 'rebuild'), defined('DRUSH_BASE_PATH'));
  1110. foreach ($states as $module_name => $components) {
  1111. foreach ($components as $component => $state) {
  1112. if (in_array($state, $restore_states)) {
  1113. $items[$module_name][] = $component;
  1114. }
  1115. }
  1116. }
  1117. }
  1118. // Invoke global pre restore hook.
  1119. module_invoke_all('features_pre_restore', $op, $items);
  1120. foreach ($items as $module_name => $components) {
  1121. // If feature is totally locked, do not execute past this stage.
  1122. if ($lockable && features_feature_is_locked($module_name)) {
  1123. watchdog('features', 'Tried @actioning a locked @module_name, aborted.', array('@action' => $log_action, '@module_name' => $module_name));
  1124. continue;
  1125. }
  1126. foreach ($components as $component) {
  1127. // If feature is totally locked, do not execute past this stage.
  1128. if ($lockable && features_feature_is_locked($module_name, $component)) {
  1129. watchdog('features', 'Tried @actioning a locked @module_name / @component, aborted.', array('@action' => $log_action, '@component' => $component, '@module_name' => $module_name));
  1130. continue;
  1131. }
  1132. // Invoke pre hook.
  1133. $pre_hook = 'pre_' . $restore_hook;
  1134. module_invoke($module_name, $pre_hook, $component);
  1135. if (features_hook($component, $restore_hook)) {
  1136. // Set a semaphore to prevent other instances of the same script from
  1137. // running concurrently.
  1138. watchdog('features', '@actioning @module_name / @component.', array('@action' => $log_action, '@component' => $component, '@module_name' => $module_name));
  1139. features_semaphore('set', $component);
  1140. features_invoke($component, $restore_hook, $module_name);
  1141. // If the script completes, remove the semaphore and set the code
  1142. // signature.
  1143. features_semaphore('del', $component);
  1144. features_set_signature($module_name, $component, NULL, __FUNCTION__ . '(' . $log_action . ')');
  1145. watchdog('features', '@action completed for @module_name / @component.', array('@action' => $log_action, '@component' => $component, '@module_name' => $module_name));
  1146. }
  1147. // Invoke post hook.
  1148. $post_hook = 'post_' . $restore_hook;
  1149. module_invoke($module_name, $post_hook, $component);
  1150. }
  1151. }
  1152. // Invoke global post restore hook.
  1153. module_invoke_all('features_post_restore', $op, $items);
  1154. }
  1155. /**
  1156. * Wrapper around _features_restore().
  1157. *
  1158. * @param string[][] $revert
  1159. * Module components to be reverted.
  1160. * If empty, all applicable feature modules will be reverted.
  1161. * Format: $[$module_name][] = $component
  1162. * E.g. $['myfeature'][] = 'field_instance'.
  1163. */
  1164. function features_revert($revert = array()) {
  1165. return _features_restore('revert', $revert);
  1166. }
  1167. /**
  1168. * Wrapper around _features_restore().
  1169. *
  1170. * @param string[][] $rebuild
  1171. * Module components to be rebuilt.
  1172. * If empty, all applicable feature modules will be rebuilt.
  1173. * Format: $[$module_name][] = $component
  1174. * E.g. $['myfeature'][] = 'field_instance'.
  1175. */
  1176. function features_rebuild($rebuild = array()) {
  1177. return _features_restore('rebuild', $rebuild);
  1178. }
  1179. /**
  1180. * Revert a single features module.
  1181. *
  1182. * @param string $module
  1183. * A features module machine name. This module must be a
  1184. * features module and enabled.
  1185. */
  1186. function features_revert_module($module) {
  1187. if (($feature = feature_load($module, TRUE)) && module_exists($module)) {
  1188. $components = array();
  1189. foreach (array_keys($feature->info['features']) as $component) {
  1190. if (features_hook($component, 'features_revert')) {
  1191. $components[] = $component;
  1192. }
  1193. }
  1194. features_revert(array($module => $components));
  1195. }
  1196. }
  1197. /**
  1198. * Log a message, environment agnostic.
  1199. *
  1200. * @param string $message
  1201. * The message to log.
  1202. * @param string $severity
  1203. * The severity of the message: status, warning or error.
  1204. */
  1205. function features_log($message, $severity = 'status') {
  1206. if (function_exists('drush_verify_cli')) {
  1207. $message = strip_tags($message);
  1208. if ($severity == 'status') {
  1209. $severity = 'ok';
  1210. }
  1211. elseif ($severity == 'error') {
  1212. drush_set_error($message);
  1213. return;
  1214. }
  1215. drush_log($message, $severity);
  1216. return;
  1217. }
  1218. drupal_set_message($message, $severity, FALSE);
  1219. }
  1220. /**
  1221. * Implements hook_hook_info().
  1222. */
  1223. function features_hook_info() {
  1224. $hooks = array(
  1225. 'features_api',
  1226. 'features_pipe_alter',
  1227. 'features_export_alter',
  1228. 'features_export_options_alter',
  1229. );
  1230. return array_fill_keys($hooks, array('group' => 'features'));
  1231. }
  1232. /**
  1233. * Implements hook_ctools_plugin_api_alter().
  1234. *
  1235. * Force features default hooks to be executed after non-feature default hooks.
  1236. * So features can override module-provided defaults.
  1237. */
  1238. function features_ctools_plugin_api_alter(&$modules, $owner, $api) {
  1239. // A datastructure for finding feature components based on $owner and $api.
  1240. features_include();
  1241. $components = _ctools_features_get_info();
  1242. $apis = array();
  1243. foreach ($components as $name => $c) {
  1244. // When writing this there was no information about when the 'api' is set or
  1245. // has to be set. For now we only make this work for schemas with it.
  1246. if (isset($c['api'])) {
  1247. $c['component'] = $name;
  1248. $apis[$c['module']][$c['api']] = $c;
  1249. }
  1250. }
  1251. if (isset($apis[$owner][$api])) {
  1252. // Split $modules into features and non-features based on whether the
  1253. // matching features component is declared in the modules .info file.
  1254. $component = $apis[$owner][$api]['component'];
  1255. $module_data = features_get_info();
  1256. $new_modules = array();
  1257. $features = array();
  1258. foreach ($modules as $name => $plugin) {
  1259. if (!empty($module_data[$name]->components) && in_array($component, $module_data[$name]->components)) {
  1260. $features[$name] = $plugin;
  1261. }
  1262. else {
  1263. $new_modules[$name] = $plugin;
  1264. }
  1265. }
  1266. // Non-features will be executed before features.
  1267. $modules = array_merge($new_modules, $features);
  1268. }
  1269. }
  1270. /**
  1271. * Change vocabularies permission, from vocab id to machine name and vice versa.
  1272. *
  1273. * The taxonomy module builds permission machine names from auto-increment
  1274. * taxonomy vocabulary vids, which is not useful when exporting to features.
  1275. *
  1276. * On features export, these permission names are converted, replacing the vids
  1277. * with taxonomy machine names. On import, this is reversed.
  1278. *
  1279. * @param string $perm
  1280. * A permission machine name from the real permissions system,
  1281. * or a modified permission machine name from a feature module.
  1282. * This will by modified (by reference), if it is one of:
  1283. * - 'edit terms in ' . $vid_or_name
  1284. * - 'delete terms in ' . $vid_or_name.
  1285. * @param string $type
  1286. * One of 'vid' or 'machine_name'.
  1287. * If 'vid', vocabulary ids will be replaced by machine names.
  1288. * If 'machine_name', vocabulary machine names will be replaced by vids.
  1289. *
  1290. * @see \taxonomy_permission()
  1291. */
  1292. function _user_features_change_term_permission(&$perm, $type = 'vid') {
  1293. if (!module_exists('taxonomy')) {
  1294. return;
  1295. }
  1296. // Export vocabulary permissions using the machine name, instead of vocabulary
  1297. // id.
  1298. if (strpos($perm, 'edit terms in ') !== FALSE || strpos($perm, 'delete terms in ') !== FALSE) {
  1299. preg_match("/(?<=\040)([^\s]+?)$/", trim($perm), $voc_id);
  1300. $vid = $voc_id[0];
  1301. if (is_numeric($vid) && $type == 'vid') {
  1302. if (function_exists('taxonomy_vocabulary_load')) {
  1303. if ($voc = taxonomy_vocabulary_load($vid)) {
  1304. $perm = str_replace($vid, $voc->machine_name, $perm);
  1305. }
  1306. }
  1307. }
  1308. elseif ($type == 'machine_name') {
  1309. if ($voc = taxonomy_vocabulary_machine_name_load($vid)) {
  1310. $perm = str_replace($vid, $voc->vid, $perm);
  1311. }
  1312. }
  1313. }
  1314. }
  1315. /**
  1316. * Recursively computes the difference of arrays with additional index check.
  1317. *
  1318. * This is a version of array_diff_assoc() that supports multidimensional
  1319. * arrays.
  1320. *
  1321. * @deprecated Use drupal_array_diff_assoc_recursive().
  1322. *
  1323. * @param array $array1
  1324. * The array to compare from.
  1325. * @param array $array2
  1326. * The array to compare to.
  1327. *
  1328. * @return array
  1329. * Returns an array containing all the values from array1 that are not present
  1330. * in array2.
  1331. */
  1332. function features_array_diff_assoc_recursive(array $array1, array $array2) {
  1333. $difference = array();
  1334. foreach ($array1 as $key => $value) {
  1335. if (is_array($value)) {
  1336. if (!isset($array2[$key]) || !is_array($array2[$key])) {
  1337. $difference[$key] = $value;
  1338. }
  1339. else {
  1340. $new_diff = features_array_diff_assoc_recursive($value, $array2[$key]);
  1341. if (!empty($new_diff)) {
  1342. $difference[$key] = $new_diff;
  1343. }
  1344. }
  1345. }
  1346. elseif (!isset($array2[$key]) || $array2[$key] != $value) {
  1347. $difference[$key] = $value;
  1348. }
  1349. }
  1350. return $difference;
  1351. }
  1352. /**
  1353. * Returns an array of deprecated components.
  1354. *
  1355. * Rather than deprecating the component directly, we look for other components
  1356. * that supersedes the component.
  1357. *
  1358. * @param array[] $components
  1359. * (optional) An array of components.
  1360. * Format: $[$component] = $component_info.
  1361. *
  1362. * @return string[]
  1363. * Format: $[$deprecated_component_name] = $deprecated_component_name
  1364. */
  1365. function features_get_deprecated($components = array()) {
  1366. if (empty($components)) {
  1367. $components = features_get_components();
  1368. }
  1369. $deprecated = array();
  1370. foreach ($components as $component => $component_info) {
  1371. if (!empty($component_info['supersedes'])) {
  1372. $deprecated[$component_info['supersedes']] = $component_info['supersedes'];
  1373. }
  1374. }
  1375. return $deprecated;
  1376. }
  1377. /**
  1378. * Returns whether a feature or its component is locked.
  1379. *
  1380. * @param string $feature
  1381. * A feature module name.
  1382. * @param string|null $component
  1383. * (optional) A component name, e.g. 'field_instance'.
  1384. * @param bool $check_global_component_setting
  1385. * (optional) If TRUE, the module component is also considered as locked if
  1386. * the component is marked as locked globally.
  1387. *
  1388. * @return bool
  1389. * TRUE, if the feature or component is locked.
  1390. */
  1391. function features_feature_is_locked($feature, $component = NULL, $check_global_component_setting = TRUE) {
  1392. $locked = variable_get('features_feature_locked', array());
  1393. if ($component) {
  1394. return ($check_global_component_setting && features_component_is_locked($component)) || !empty($locked[$feature][$component]);
  1395. }
  1396. else {
  1397. return !empty($locked[$feature]['_all']);
  1398. }
  1399. }
  1400. /**
  1401. * Returns whether a component is locked.
  1402. *
  1403. * @param string $component
  1404. * A component name, e.g. 'field_instance'.
  1405. *
  1406. * @return bool
  1407. * TRUE, if the component is locked globally.
  1408. */
  1409. function features_component_is_locked($component) {
  1410. return variable_get('features_component_locked_' . $component, FALSE);
  1411. }
  1412. /**
  1413. * Locks a feature or its component.
  1414. *
  1415. * @param string $feature
  1416. * A (feature) module name.
  1417. * @param string|null $component
  1418. * (optional) A component name, e.g. 'field_instance'.
  1419. */
  1420. function features_feature_lock($feature, $component = NULL) {
  1421. $locked = variable_get('features_feature_locked', array());
  1422. $locked[$feature] = !empty($locked[$feature]) ? $locked[$feature] : array();
  1423. if ($component) {
  1424. $locked[$feature][$component] = TRUE;
  1425. }
  1426. else {
  1427. $locked[$feature]['_all'] = TRUE;
  1428. }
  1429. variable_set('features_feature_locked', $locked);
  1430. }
  1431. /**
  1432. * Unlocks a feature or its component.
  1433. *
  1434. * @param string $feature
  1435. * A (feature) module name.
  1436. * @param string|null $component
  1437. * (optional) A component name, e.g. 'field_instance'.
  1438. */
  1439. function features_feature_unlock($feature, $component = NULL) {
  1440. $locked = variable_get('features_feature_locked', array());
  1441. if ($component) {
  1442. unset($locked[$feature][$component]);
  1443. }
  1444. else {
  1445. unset($locked[$feature]['_all']);
  1446. }
  1447. variable_set('features_feature_locked', $locked);
  1448. }
  1449. /**
  1450. * Sets/Returns the current language to English to ensure a proper export.
  1451. *
  1452. * @param \stdClass|null $language
  1453. * (optional) The language object to set, or NULL for English.
  1454. *
  1455. * @return \stdClass
  1456. * The previously active language object.
  1457. * This should be kept in a variable, to restore the original language later.
  1458. */
  1459. function _features_export_language($language = NULL) {
  1460. $current = $GLOBALS['language'];
  1461. if (isset($language)) {
  1462. $GLOBALS['language'] = $language;
  1463. }
  1464. elseif ($GLOBALS['language']->language != 'en') {
  1465. // Create the language object as language_default() does.
  1466. $GLOBALS['language'] = (object) array(
  1467. 'language' => 'en',
  1468. 'name' => 'English',
  1469. 'native' => 'English',
  1470. 'direction' => 0,
  1471. 'enabled' => 1,
  1472. 'plurals' => 0,
  1473. 'formula' => '',
  1474. 'domain' => '',
  1475. 'prefix' => '',
  1476. 'weight' => 0,
  1477. 'javascript' => '',
  1478. );
  1479. }
  1480. return $current;
  1481. }
  1482. /**
  1483. * Implements hook_features_ignore().
  1484. */
  1485. function features_features_ignore($component) {
  1486. // Determine which keys need to be ignored for override diff for various
  1487. // components.
  1488. // Value is how many levels deep the key is.
  1489. $ignores = array();
  1490. switch ($component) {
  1491. case 'views_view':
  1492. $ignores['current_display'] = 0;
  1493. $ignores['display_handler'] = 0;
  1494. $ignores['handler'] = 2;
  1495. $ignores['query'] = 0;
  1496. $ignores['localization_plugin'] = 0;
  1497. // Views automatically adds these two on export to set values.
  1498. $ignores['api_version'] = 0;
  1499. $ignores['disabled'] = 0;
  1500. break;
  1501. case 'image':
  1502. $ignores['module'] = 0;
  1503. $ignores['name'] = 0;
  1504. $ignores['storage'] = 0;
  1505. // Various properties are loaded into the effect in image_styles.
  1506. $ignores['summary theme'] = 2;
  1507. $ignores['module'] = 2;
  1508. $ignores['label'] = 2;
  1509. $ignores['help'] = 2;
  1510. $ignores['form callback'] = 2;
  1511. $ignores['effect callback'] = 2;
  1512. $ignores['dimensions callback'] = 2;
  1513. break;
  1514. case 'field':
  1515. $ignores['locked'] = 1;
  1516. break;
  1517. case 'field_base':
  1518. $ignores['indexes'] = 0;
  1519. break;
  1520. case 'taxonomy':
  1521. $ignores['hierarchy'] = 0;
  1522. }
  1523. return $ignores;
  1524. }