features.export.inc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  1. <?php
  2. /**
  3. * @param $info - feature info array
  4. * @param $module_name
  5. * @return fully populated export array
  6. */
  7. function features_populate($info, $module_name) {
  8. // Sanitize items.
  9. $items = !empty($info['features']) ? array_filter($info['features']) : array();
  10. $items['dependencies'] = !empty($info['dependencies']) ? drupal_map_assoc(array_filter($info['dependencies'])) : array();
  11. // Populate stub
  12. $stub = array('features' => array(), 'dependencies' => array(), 'conflicts' => array()) + $info + array('features_exclude' => array());
  13. $export = _features_populate($items, $stub, $module_name, TRUE);
  14. // Add Features API version. Any module with this entry in the .info file
  15. // will be treated as a Feature and included in the admin/build/features UI.
  16. $export['features']['features_api']['api:' . FEATURES_API] = TRUE;
  17. // Allow other modules to alter the export.
  18. drupal_alter('features_export', $export, $module_name);
  19. // Clean up and standardize order
  20. foreach (array_keys($export['features']) as $k) {
  21. ksort($export['features'][$k]);
  22. }
  23. ksort($export['features']);
  24. ksort($export['dependencies']);
  25. ksort($export['features_exclude']);
  26. return $export;
  27. }
  28. /**
  29. * Iterate and descend into a feature definition to extract module
  30. * dependencies and feature definition. Calls hook_features_export for modules
  31. * that implement it.
  32. *
  33. * @param $pipe
  34. * Associative of array of module => info-for-module
  35. * @param $export
  36. * Associative array of items, and module dependencies which define a feature.
  37. * Passed by reference.
  38. *
  39. * @return fully populated $export array.
  40. */
  41. function _features_populate($pipe, &$export, $module_name = '', $reset = FALSE) {
  42. static $processed = array();
  43. features_include();
  44. if ($reset) {
  45. $processed = array();
  46. }
  47. foreach ($pipe as $component => $data) {
  48. // Convert already defined items to dependencies.
  49. // _features_resolve_dependencies($data, $export, $module_name, $component);
  50. // Remove any excluded items.
  51. if (!empty($export['features_exclude'][$component])) {
  52. $data = array_diff($data, $export['features_exclude'][$component]);
  53. if ($component == 'dependencies' && !empty($export['dependencies'])) {
  54. $export['dependencies'] = array_diff($export['dependencies'], $export['features_exclude'][$component]);
  55. }
  56. }
  57. if (!empty($data) && $function = features_hook($component, 'features_export')) {
  58. // Pass module-specific data and export array.
  59. // We don't use features_invoke() here since we need to pass $export by reference.
  60. $more = $function($data, $export, $module_name, $component);
  61. // Add the context information.
  62. $export['component'] = $component;
  63. $export['module_name'] = $module_name;
  64. // Allow other modules to manipulate the pipe to add in additional modules.
  65. drupal_alter(array('features_pipe', 'features_pipe_' . $component), $more, $data, $export);
  66. // Remove the component information.
  67. unset($export['component']);
  68. unset($export['module_name']);
  69. // Allow for export functions to request additional exports, but avoid
  70. // circular references on already processed components.
  71. $processed[$component] = isset($processed[$component]) ? array_merge($processed[$component], $data) : $data;
  72. if (!empty($more)) {
  73. // Remove already processed components.
  74. foreach ($more as $component_name => $component_data) {
  75. if (isset($processed[$component_name])) {
  76. $more[$component_name] = array_diff($component_data, $processed[$component_name]);
  77. }
  78. }
  79. if ($more = array_filter($more)) {
  80. _features_populate($more, $export, $module_name);
  81. }
  82. }
  83. }
  84. }
  85. return $export;
  86. }
  87. /**
  88. * Iterates over data and convert to dependencies if already defined elsewhere.
  89. */
  90. function _features_resolve_dependencies(&$data, &$export, $module_name, $component) {
  91. if ($map = features_get_default_map($component)) {
  92. foreach ($data as $key => $item) {
  93. // If this node type is provided by a different module, add it as a dependency
  94. if (isset($map[$item]) && $map[$item] != $module_name) {
  95. $export['dependencies'][$map[$item]] = $map[$item];
  96. unset($data[$key]);
  97. }
  98. }
  99. }
  100. }
  101. /**
  102. * Iterates over a list of dependencies and kills modules that are
  103. * captured by other modules 'higher up'.
  104. */
  105. function _features_export_minimize_dependencies($dependencies, $module_name = '') {
  106. // Ensure that the module doesn't depend upon itself
  107. if (!empty($module_name) && !empty($dependencies[$module_name])) {
  108. unset($dependencies[$module_name]);
  109. }
  110. // Do some cleanup:
  111. // - Remove modules required by Drupal core.
  112. // - Protect against direct circular dependencies.
  113. // - Remove "intermediate" dependencies.
  114. $required = drupal_required_modules();
  115. foreach ($dependencies as $k => $v) {
  116. if (empty($v) || in_array($v, $required)) {
  117. unset($dependencies[$k]);
  118. }
  119. else {
  120. $module = features_get_modules($v);
  121. if ($module && !empty($module->info['dependencies'])) {
  122. // If this dependency depends on the module itself, we have a circular dependency.
  123. // Don't let it happen. Only you can prevent forest fires.
  124. if (in_array($module_name, $module->info['dependencies'])) {
  125. unset($dependencies[$k]);
  126. }
  127. // Iterate through the dependency's dependencies and remove any dependencies
  128. // that are captured by it.
  129. else {
  130. foreach ($module->info['dependencies'] as $j => $dependency) {
  131. if (array_search($dependency, $dependencies) !== FALSE) {
  132. $position = array_search($dependency, $dependencies);
  133. unset($dependencies[$position]);
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }
  140. return drupal_map_assoc(array_unique($dependencies));
  141. }
  142. /**
  143. * Iterates over a list of dependencies and maximize the list of modules.
  144. */
  145. function _features_export_maximize_dependencies($dependencies, $module_name = '', $maximized = array(), $first = TRUE) {
  146. foreach ($dependencies as $k => $v) {
  147. $parsed_dependency = drupal_parse_dependency($v);
  148. $name = $parsed_dependency['name'];
  149. if (!in_array($name, $maximized)) {
  150. $maximized[] = $name;
  151. $module = features_get_modules($name);
  152. if ($module && !empty($module->info['dependencies'])) {
  153. $maximized = array_merge($maximized, _features_export_maximize_dependencies($module->info['dependencies'], $module_name, $maximized, FALSE));
  154. }
  155. }
  156. }
  157. return array_unique($maximized);
  158. }
  159. /**
  160. * Prepare a feature export array into a finalized info array.
  161. */
  162. function features_export_prepare($export, $module_name, $reset = FALSE) {
  163. $existing = features_get_modules($module_name, $reset);
  164. // copy certain exports directly into info
  165. $copy_list = array('scripts', 'stylesheets');
  166. foreach ($copy_list as $item) {
  167. if(isset($export[$item])) {
  168. $existing->info[$item] = $export[$item];
  169. }
  170. }
  171. // Prepare info string -- if module exists, merge into its existing info file
  172. $defaults = $existing ? $existing->info : array('core' => '7.x', 'package' => 'Features');
  173. $export = array_merge($defaults, $export);
  174. // Cleanup info array
  175. foreach ($export['features'] as $component => $data) {
  176. $export['features'][$component] = array_keys($data);
  177. }
  178. if (isset($export['dependencies'])) {
  179. $export['dependencies'] = array_values($export['dependencies']);
  180. }
  181. if (isset($export['conflicts'])) {
  182. unset($export['conflicts']);
  183. }
  184. // Order info array.
  185. $standard_info = array();
  186. foreach (array_merge(array('name', 'description', 'core', 'package', 'php', 'version', 'project', 'dependencies'), $copy_list) as $item) {
  187. if (isset($export[$item])) {
  188. $standard_info[$item] = $export[$item];
  189. }
  190. }
  191. $export = array_diff_assoc($export, $standard_info);
  192. ksort($export);
  193. return array_merge($standard_info, $export);
  194. }
  195. /**
  196. * Generate an array of hooks and their raw code.
  197. */
  198. function features_export_render_hooks($export, $module_name, $reset = FALSE) {
  199. features_include();
  200. $code = array();
  201. // Sort components to keep exported code consistent
  202. ksort($export['features']);
  203. foreach ($export['features'] as $component => $data) {
  204. if (!empty($data)) {
  205. // Sort the items so that we don't generate different exports based on order
  206. asort($data);
  207. if (features_hook($component, 'features_export_render')) {
  208. $hooks = features_invoke($component, 'features_export_render', $module_name, $data, $export);
  209. $code[$component] = $hooks;
  210. }
  211. }
  212. }
  213. return $code;
  214. }
  215. /**
  216. * Render feature export into an array representing its files.
  217. *
  218. * @param $export
  219. * An exported feature definition.
  220. * @param $module_name
  221. * The name of the module to be exported.
  222. * @param $reset
  223. * Boolean flag for resetting the module cache. Only set to true when
  224. * doing a final export for delivery.
  225. *
  226. * @return array of info file and module file contents.
  227. */
  228. function features_export_render($export, $module_name, $reset = FALSE) {
  229. $code = array();
  230. // Generate hook code
  231. $component_hooks = features_export_render_hooks($export, $module_name, $reset);
  232. $components = features_get_components();
  233. // Group component code into their respective files
  234. foreach ($component_hooks as $component => $hooks) {
  235. $file = array('name' => 'features');
  236. if (isset($components[$component]['default_file'])) {
  237. switch ($components[$component]['default_file']) {
  238. case FEATURES_DEFAULTS_INCLUDED:
  239. $file['name'] = "features.$component";
  240. break;
  241. case FEATURES_DEFAULTS_CUSTOM:
  242. $file['name'] = $components[$component]['default_filename'];
  243. break;
  244. }
  245. }
  246. if (!isset($code[$file['name']])) {
  247. $code[$file['name']] = array();
  248. }
  249. foreach ($hooks as $hook_name => $hook_info) {
  250. $hook_code = is_array($hook_info) ? $hook_info['code'] : $hook_info;
  251. $hook_args = is_array($hook_info) && !empty($hook_info['args']) ? $hook_info['args'] : '';
  252. $hook_file = is_array($hook_info) && !empty($hook_info['file']) ? $hook_info['file'] : $file['name'];
  253. $code[$hook_file][$hook_name] = features_export_render_defaults($module_name, $hook_name, $hook_code, $hook_args);
  254. }
  255. }
  256. // Finalize strings to be written to files
  257. $code = array_filter($code);
  258. foreach ($code as $filename => $contents) {
  259. $code[$filename] = "<?php\n/**\n * @file\n * {$module_name}.{$filename}.inc\n */\n\n". implode("\n\n", $contents) ."\n";
  260. }
  261. // Generate info file output
  262. $export = features_export_prepare($export, $module_name, $reset);
  263. $code['info'] = features_export_info($export);
  264. // Used to create or manipulate the generated .module for features.inc.
  265. $modulefile_features_inc = "<?php\n/**\n * @file\n * Code for the {$export['name']} feature.\n */\n\ninclude_once '{$module_name}.features.inc';\n";
  266. $modulefile_blank = "<?php\n/**\n * @file\n * Drupal needs this blank file.\n */\n";
  267. // Prepare the module
  268. // If module exists, let it be and include it in the files
  269. if ($existing = features_get_modules($module_name, TRUE)) {
  270. $code['module'] = file_get_contents($existing->filename);
  271. // If the current module file does not reference the features.inc include,
  272. // @TODO this way of checking does not account for the possibility of inclusion instruction being commented out.
  273. if (isset($code['features']) && strpos($code['module'], "{$module_name}.features.inc") === FALSE) {
  274. // If .module does not begin with <?php\n, just add a warning.
  275. if (strpos($code['module'], "<?php\n") !== 0) {
  276. features_log(t('@module does not appear to include the @include file.', array('@module' => "{$module_name}.module", '@include' => "{$module_name}.features.inc")), 'warning');
  277. }
  278. else {
  279. // Remove the old message if it exists, else just remove the <?php
  280. $length = strpos($code['module'], $modulefile_blank) === 0 ? strlen($modulefile_blank) : 6;
  281. $code['module'] = $modulefile_features_inc . substr($code['module'], $length);
  282. }
  283. }
  284. // Deprecated files. Display a message for any of these files letting the
  285. // user know that they may be removed.
  286. $deprecated = array(
  287. "{$module_name}.defaults",
  288. "{$module_name}.features.views",
  289. "{$module_name}.features.node"
  290. );
  291. foreach (file_scan_directory(drupal_get_path('module', $module_name), '/.*/') as $file) {
  292. if (in_array($file->name, $deprecated, TRUE)) {
  293. features_log(t('The file @filename has been deprecated and can be removed.', array('@filename' => $file->filename)), 'status');
  294. }
  295. elseif ($file->name === "{$module_name}.features" && empty($code['features'])) {
  296. // Try and remove features.inc include.
  297. if (strpos($code['module'], "{$module_name}.features.inc")) {
  298. $code['module'] = str_replace($modulefile_features_inc, $modulefile_blank, $code['module']);
  299. }
  300. // If unable to remove the include, add a message to remove.
  301. if (strpos($code['module'], "{$module_name}.features.inc")) {
  302. $code['features'] = "<?php\n\n// This file is deprecated and can be removed.\n// Please remove include_once('{$module_name}.features.inc') in {$module_name}.module as well.\n";
  303. }
  304. else {
  305. $code['features'] = "<?php\n\n// This file is deprecated and can be removed.\n";
  306. }
  307. }
  308. }
  309. }
  310. // Add a stub module to include the defaults
  311. else if (!empty($code['features'])) {
  312. $code['module'] = $modulefile_features_inc;
  313. }
  314. else {
  315. $code['module'] = $modulefile_blank;
  316. }
  317. return $code;
  318. }
  319. /**
  320. * Detect differences between DB and code components of a feature.
  321. */
  322. function features_detect_overrides($module) {
  323. static $cache;
  324. if (!isset($cache)) {
  325. $cache = array();
  326. }
  327. if (!isset($cache[$module->name])) {
  328. // Rebuild feature from .info file description and prepare an export from current DB state.
  329. $export = features_populate($module->info, $module->name);
  330. $export = features_export_prepare($export, $module->name);
  331. $overridden = array();
  332. // Compare feature info
  333. _features_sanitize($module->info);
  334. _features_sanitize($export);
  335. $compare = array('normal' => features_export_info($export), 'default' => features_export_info($module->info));
  336. if ($compare['normal'] !== $compare['default']) {
  337. $overridden['info'] = $compare;
  338. }
  339. // Collect differences at a per-component level
  340. $states = features_get_component_states(array($module->name), FALSE);
  341. foreach ($states[$module->name] as $component => $state) {
  342. if ($state != FEATURES_DEFAULT) {
  343. $normal = features_get_normal($component, $module->name);
  344. $default = features_get_default($component, $module->name);
  345. _features_sanitize($normal);
  346. _features_sanitize($default);
  347. $compare = array('normal' => features_var_export($normal), 'default' => features_var_export($default));
  348. if (_features_linetrim($compare['normal']) !== _features_linetrim($compare['default'])) {
  349. $overridden[$component] = $compare;
  350. }
  351. }
  352. }
  353. $cache[$module->name] = $overridden;
  354. }
  355. return $cache[$module->name];
  356. }
  357. /**
  358. * Gets the available default hooks keyed by components.
  359. */
  360. function features_get_default_hooks($component = NULL, $reset = FALSE) {
  361. return features_get_components($component, 'default_hook', $reset);
  362. }
  363. /**
  364. * Gets the available default hooks keyed by components.
  365. */
  366. function features_get_default_alter_hook($component) {
  367. $default_hook = features_get_components($component, 'default_hook');
  368. $alter_hook = features_get_components($component, 'alter_hook');
  369. $alter_type = features_get_components($component, 'alter_type');
  370. return empty($alter_type) || $alter_type != 'none' ? ($alter_hook ? $alter_hook : $default_hook) : FALSE;
  371. }
  372. /**
  373. * Return a code string representing an implementation of a defaults module hook.
  374. */
  375. function features_export_render_defaults($module, $hook, $code, $args = '') {
  376. $output = array();
  377. $output[] = "/**";
  378. $output[] = " * Implements hook_{$hook}().";
  379. $output[] = " */";
  380. $output[] = "function {$module}_{$hook}(" . $args . ") {";
  381. $output[] = $code;
  382. $output[] = "}";
  383. return implode("\n", $output);
  384. }
  385. /**
  386. * Generate code friendly to the Drupal .info format from a structured array.
  387. *
  388. * @param $info
  389. * An array or single value to put in a module's .info file.
  390. * @param $parents
  391. * Array of parent keys (internal use only).
  392. *
  393. * @return
  394. * A code string ready to be written to a module's .info file.
  395. */
  396. function features_export_info($info, $parents = array()) {
  397. $output = '';
  398. if (is_array($info)) {
  399. foreach ($info as $k => $v) {
  400. $child = $parents;
  401. $child[] = $k;
  402. $output .= features_export_info($v, $child);
  403. }
  404. }
  405. else if (!empty($info) && count($parents)) {
  406. $line = array_shift($parents);
  407. foreach ($parents as $key) {
  408. $line .= is_numeric($key) ? "[]" : "[{$key}]";
  409. }
  410. $line .= " = {$info}\n";
  411. return $line;
  412. }
  413. return $output;
  414. }
  415. /**
  416. * Tar creation function. Written by dmitrig01.
  417. *
  418. * @param $name
  419. * Filename of the file to be tarred.
  420. * @param $contents
  421. * String contents of the file.
  422. *
  423. * @return
  424. * A string of the tar file contents.
  425. */
  426. function features_tar_create($name, $contents) {
  427. /* http://www.mkssoftware.com/docs/man4/tar.4.asp */
  428. /* http://www.phpclasses.org/browse/file/21200.html */
  429. $tar = '';
  430. $bigheader = $header = '';
  431. if (strlen($name) > 100) {
  432. $bigheader = pack("a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155a12",
  433. '././@LongLink', '0000000', '0000000', '0000000',
  434. sprintf("%011o", strlen($name)), '00000000000',
  435. ' ', 'L', '', 'ustar ', '0',
  436. '', '', '', '', '', '');
  437. $bigheader .= str_pad($name, floor((strlen($name) + 512 - 1) / 512) * 512, "\0");
  438. $checksum = 0;
  439. for ($i = 0; $i < 512; $i++) {
  440. $checksum += ord(substr($bigheader, $i, 1));
  441. }
  442. $bigheader = substr_replace($bigheader, sprintf("%06o", $checksum)."\0 ", 148, 8);
  443. }
  444. $header = pack("a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155a12", // book the memorie area
  445. substr($name,0,100), // 0 100 File name
  446. '100644 ', // File permissions
  447. ' 765 ', // UID,
  448. ' 765 ', // GID,
  449. sprintf("%11s ", decoct(strlen($contents))), // Filesize,
  450. sprintf("%11s", decoct(REQUEST_TIME)), // Creation time
  451. ' ', // 148 8 Check sum for header block
  452. '', // 156 1 Link indicator / ustar Type flag
  453. '', // 157 100 Name of linked file
  454. 'ustar ', // 257 6 USTAR indicator "ustar"
  455. ' ', // 263 2 USTAR version "00"
  456. '', // 265 32 Owner user name
  457. '', // 297 32 Owner group name
  458. '', // 329 8 Device major number
  459. '', // 337 8 Device minor number
  460. '', // 345 155 Filename prefix
  461. ''); // 500 12 ??
  462. $checksum = 0;
  463. for ($i = 0; $i < 512; $i++) {
  464. $checksum += ord(substr($header, $i, 1));
  465. }
  466. $header = substr_replace($header, sprintf("%06o", $checksum)."\0 ", 148, 8);
  467. $tar = $bigheader.$header;
  468. $buffer = str_split($contents, 512);
  469. foreach ($buffer as $item) {
  470. $tar .= pack("a512", $item);
  471. }
  472. return $tar;
  473. }
  474. /**
  475. * Export var function -- from Views.
  476. */
  477. function features_var_export($var, $prefix = '', $init = TRUE) {
  478. if (is_object($var)) {
  479. $output = method_exists($var, 'export') ? $var->export() : features_var_export((array) $var, '', FALSE);
  480. }
  481. else if (is_array($var)) {
  482. if (empty($var)) {
  483. $output = 'array()';
  484. }
  485. else {
  486. $output = "array(\n";
  487. foreach ($var as $key => $value) {
  488. // Using normal var_export on the key to ensure correct quoting.
  489. $output .= " " . var_export($key, TRUE) . " => " . features_var_export($value, ' ', FALSE) . ",\n";
  490. }
  491. $output .= ')';
  492. }
  493. }
  494. else if (is_bool($var)) {
  495. $output = $var ? 'TRUE' : 'FALSE';
  496. }
  497. else if (is_string($var) && strpos($var, "\n") !== FALSE) {
  498. // Replace line breaks in strings with a token for replacement
  499. // at the very end. This protects whitespace in strings from
  500. // unintentional indentation.
  501. $var = str_replace("\n", "***BREAK***", $var);
  502. $output = var_export($var, TRUE);
  503. }
  504. else {
  505. $output = var_export($var, TRUE);
  506. }
  507. if ($prefix) {
  508. $output = str_replace("\n", "\n$prefix", $output);
  509. }
  510. if ($init) {
  511. $output = str_replace("***BREAK***", "\n", $output);
  512. }
  513. return $output;
  514. }
  515. /**
  516. * Helper function to return an array of t()'d translatables strings.
  517. * Useful for providing a separate array of translatables with your
  518. * export so that string extractors like potx can detect them.
  519. */
  520. function features_translatables_export($translatables, $indent = '') {
  521. $output = '';
  522. $translatables = array_filter(array_unique($translatables));
  523. if (!empty($translatables)) {
  524. $output .= "{$indent}// Translatables\n";
  525. $output .= "{$indent}// Included for use with string extractors like potx.\n";
  526. sort($translatables);
  527. foreach ($translatables as $string) {
  528. $output .= "{$indent}t(" . features_var_export($string) . ");\n";
  529. }
  530. }
  531. return $output;
  532. }
  533. /**
  534. * Get a summary storage state for a feature.
  535. */
  536. function features_get_storage($module_name) {
  537. // Get component states, and array_diff against array(FEATURES_DEFAULT).
  538. // If the returned array has any states that don't match FEATURES_DEFAULT,
  539. // return the highest state.
  540. $states = features_get_component_states(array($module_name), FALSE);
  541. $states = array_diff($states[$module_name], array(FEATURES_DEFAULT));
  542. $storage = !empty($states) ? max($states) : FEATURES_DEFAULT;
  543. return $storage;
  544. }
  545. /**
  546. * Wrapper around features_get_[storage] to return an md5hash of a normalized
  547. * defaults/normal object array. Can be used to compare normal/default states
  548. * of a module's component.
  549. */
  550. function features_get_signature($state = 'default', $module_name, $component, $reset = FALSE) {
  551. switch ($state) {
  552. case 'cache':
  553. $codecache = variable_get('features_codecache', array());
  554. return isset($codecache[$module_name][$component]) ? $codecache[$module_name][$component] : FALSE;
  555. case 'default':
  556. $objects = features_get_default($component, $module_name, TRUE, $reset);
  557. break;
  558. case 'normal':
  559. $objects = features_get_normal($component, $module_name, $reset);
  560. break;
  561. }
  562. if (!empty($objects)) {
  563. $objects = (array) $objects;
  564. _features_sanitize($objects);
  565. return md5(_features_linetrim(features_var_export($objects)));
  566. }
  567. return FALSE;
  568. }
  569. /**
  570. * Set the signature of a module/component pair in the codecache.
  571. */
  572. function features_set_signature($module, $component, $signature = NULL) {
  573. $var_codecache = variable_get('features_codecache', array());
  574. $signature = isset($signature) ? $signature : features_get_signature('default', $module, $component, TRUE);
  575. $var_codecache[$module][$component] = $signature;
  576. variable_set('features_codecache', $var_codecache);
  577. }
  578. /**
  579. * Processing semaphore operations.
  580. */
  581. function features_semaphore($op, $component) {
  582. // Note: we don't use variable_get() here as the inited variable
  583. // static cache may be stale. Retrieving directly from the DB narrows
  584. // the possibility of collision.
  585. $semaphore = db_query("SELECT value FROM {variable} WHERE name = :name", array(':name' => 'features_semaphore'))->fetchField();
  586. $semaphore = !empty($semaphore) ? unserialize($semaphore) : array();
  587. switch ($op) {
  588. case 'get':
  589. return isset($semaphore[$component]) ? $semaphore[$component] : FALSE;
  590. case 'set':
  591. $semaphore[$component] = REQUEST_TIME;
  592. variable_set('features_semaphore', $semaphore);
  593. break;
  594. case 'del':
  595. if (isset($semaphore[$component])) {
  596. unset($semaphore[$component]);
  597. variable_set('features_semaphore', $semaphore);
  598. }
  599. break;
  600. }
  601. }
  602. /**
  603. * Get normal objects for a given module/component pair.
  604. */
  605. function features_get_normal($component, $module_name, $reset = FALSE) {
  606. static $cache;
  607. if (!isset($cache) || $reset) {
  608. $cache = array();
  609. }
  610. if (!isset($cache[$module_name][$component])) {
  611. features_include();
  612. $code = NULL;
  613. $module = features_get_features($module_name);
  614. // Special handling for dependencies component.
  615. if ($component === 'dependencies') {
  616. $cache[$module_name][$component] = isset($module->info['dependencies']) ? array_filter($module->info['dependencies'], 'module_exists') : array();
  617. }
  618. // All other components.
  619. else {
  620. $default_hook = features_get_default_hooks($component);
  621. if ($module && $default_hook && isset($module->info['features'][$component]) && features_hook($component, 'features_export_render')) {
  622. $code = features_invoke($component, 'features_export_render', $module_name, $module->info['features'][$component], NULL);
  623. $cache[$module_name][$component] = isset($code[$default_hook]) ? eval($code[$default_hook]) : FALSE;
  624. }
  625. }
  626. // Clear out vars for memory's sake.
  627. unset($code);
  628. unset($module);
  629. }
  630. return isset($cache[$module_name][$component]) ? $cache[$module_name][$component] : FALSE;
  631. }
  632. /**
  633. * Get defaults for a given module/component pair.
  634. */
  635. function features_get_default($component, $module_name = NULL, $alter = TRUE, $reset = FALSE) {
  636. static $cache = array();
  637. $alter = !empty($alter); // ensure $alter is a true/false boolean
  638. features_include();
  639. features_include_defaults($component);
  640. $default_hook = features_get_default_hooks($component);
  641. $components = features_get_components();
  642. // Collect defaults for all modules if no module name was specified.
  643. if (isset($module_name)) {
  644. $modules = array($module_name);
  645. }
  646. else {
  647. if ($component === 'dependencies') {
  648. $modules = array_keys(features_get_features());
  649. }
  650. else {
  651. $modules = array();
  652. foreach (features_get_component_map($component) as $component_modules) {
  653. $modules = array_merge($modules, $component_modules);
  654. }
  655. $modules = array_unique($modules);
  656. }
  657. }
  658. // Collect and cache information for each specified module.
  659. foreach ($modules as $m) {
  660. if (!isset($cache[$component][$alter][$m]) || $reset) {
  661. // Special handling for dependencies component.
  662. if ($component === 'dependencies') {
  663. $module = features_get_features($m);
  664. $cache[$component][$alter][$m] = isset($module->info['dependencies']) ? $module->info['dependencies'] : array();
  665. unset($module);
  666. }
  667. // All other components
  668. else {
  669. if ($default_hook && module_hook($m, $default_hook)) {
  670. $cache[$component][$alter][$m] = call_user_func("{$m}_{$default_hook}");
  671. if (is_array($cache[$component][$alter][$m])) {
  672. $alter_type = features_get_components('alter_type', $component);
  673. if ($alter && (!isset($alter_type) || $alter_type == FEATURES_ALTER_TYPE_NORMAL)) {
  674. if ($alter_hook = features_get_default_alter_hook($component)) {
  675. drupal_alter($alter_hook, $cache[$component][$alter][$m]);
  676. }
  677. }
  678. }
  679. else {
  680. $cache[$component][$alter][$m] = FALSE;
  681. }
  682. }
  683. else {
  684. $cache[$component][$alter][$m] = FALSE;
  685. }
  686. }
  687. }
  688. }
  689. // A specific module was specified. Retrieve only its components.
  690. if (isset($module_name)) {
  691. return isset($cache[$component][$alter][$module_name]) ? $cache[$component][$alter][$module_name] : FALSE;
  692. }
  693. // No module was specified. Retrieve all components.
  694. $all_defaults = array();
  695. if (isset($cache[$component][$alter])) {
  696. foreach (array_filter($cache[$component][$alter]) as $module_components) {
  697. $all_defaults = array_merge($all_defaults, $module_components);
  698. }
  699. }
  700. return $all_defaults;
  701. }
  702. /**
  703. * Get a map of components to their providing modules.
  704. */
  705. function features_get_default_map($component, $attribute = NULL, $callback = NULL, $reset = FALSE) {
  706. static $map = array();
  707. global $features_ignore_conflicts;
  708. if ($features_ignore_conflicts) {
  709. return FALSE;
  710. }
  711. features_include();
  712. features_include_defaults($component);
  713. if ((!isset($map[$component]) || $reset) && $default_hook = features_get_default_hooks($component)) {
  714. $map[$component] = array();
  715. foreach (module_implements($default_hook) as $module) {
  716. if ($defaults = features_get_default($component, $module)) {
  717. foreach ($defaults as $key => $object) {
  718. if (isset($callback)) {
  719. if ($object_key = $callback($object)) {
  720. $map[$component][$object_key] = $module;
  721. }
  722. }
  723. elseif (isset($attribute)) {
  724. if (is_object($object) && isset($object->{$attribute})) {
  725. $map[$component][$object->{$attribute}] = $module;
  726. }
  727. elseif (is_array($object) && isset($object[$attribute])) {
  728. $map[$component][$object[$attribute]] = $module;
  729. }
  730. }
  731. elseif (!isset($attribute) && !isset($callback)) {
  732. if (!is_numeric($key)) {
  733. $map[$component][$key] = $module;
  734. }
  735. }
  736. else {
  737. return FALSE;
  738. }
  739. }
  740. }
  741. }
  742. }
  743. return isset($map[$component]) ? $map[$component] : FALSE;
  744. }
  745. /**
  746. * Retrieve an array of features/components and their current states.
  747. */
  748. function features_get_component_states($features = array(), $rebuild_only = TRUE, $reset = FALSE) {
  749. static $cache;
  750. if (!isset($cache) || $reset) {
  751. $cache = array();
  752. }
  753. $features = !empty($features) ? $features : array_keys(features_get_features());
  754. // Retrieve only rebuildable components if requested.
  755. features_include();
  756. $components = array_keys(features_get_components());
  757. if ($rebuild_only) {
  758. foreach ($components as $k => $component) {
  759. if (!features_hook($component, 'features_rebuild')) {
  760. unset($components[$k]);
  761. }
  762. }
  763. }
  764. foreach ($features as $feature) {
  765. $cache[$feature] = isset($cache[$feature]) ? $cache[$feature] : array();
  766. if (module_exists($feature)) {
  767. foreach ($components as $component) {
  768. if (!isset($cache[$feature][$component])) {
  769. $normal = features_get_signature('normal', $feature, $component, $reset);
  770. $default = features_get_signature('default', $feature, $component, $reset);
  771. $codecache = features_get_signature('cache', $feature, $component, $reset);
  772. $semaphore = features_semaphore('get', $component);
  773. // DB and code states match, there is nothing more to check.
  774. if ($normal == $default) {
  775. $cache[$feature][$component] = FEATURES_DEFAULT;
  776. // Stale semaphores can be deleted.
  777. features_semaphore('del', $component);
  778. // Update code cache if it is stale, clear out semaphore if it stale.
  779. if ($default != $codecache) {
  780. features_set_signature($feature, $component, $default);
  781. }
  782. }
  783. // Component properly implements exportables.
  784. else if (!features_hook($component, 'features_rebuild')) {
  785. $cache[$feature][$component] = FEATURES_OVERRIDDEN;
  786. }
  787. // Component does not implement exportables.
  788. else {
  789. if (empty($semaphore)) {
  790. // Exception for dependencies. Dependencies are always rebuildable.
  791. if ($component === 'dependencies') {
  792. $cache[$feature][$component] = FEATURES_REBUILDABLE;
  793. }
  794. // All other rebuildable components require comparison.
  795. else {
  796. // Code has not changed, but DB does not match. User has DB overrides.
  797. if ($codecache == $default) {
  798. $cache[$feature][$component] = FEATURES_OVERRIDDEN;
  799. }
  800. // DB has no modifications to prior code state (or this is initial install).
  801. else if (empty($codecache) || $codecache == $normal) {
  802. $cache[$feature][$component] = FEATURES_REBUILDABLE;
  803. }
  804. // None of the states match. Requires user intervention.
  805. else if ($codecache != $default) {
  806. $cache[$feature][$component] = FEATURES_NEEDS_REVIEW;
  807. }
  808. }
  809. }
  810. else {
  811. // Semaphore is still within processing horizon. Do nothing.
  812. if ((REQUEST_TIME - $semaphore) < FEATURES_SEMAPHORE_TIMEOUT) {
  813. $cache[$feature][$component] = FEATURES_REBUILDING;
  814. }
  815. // A stale semaphore means a previous rebuild attempt did not complete.
  816. // Attempt to complete the rebuild.
  817. else {
  818. $cache[$feature][$component] = FEATURES_REBUILDABLE;
  819. }
  820. }
  821. }
  822. }
  823. }
  824. }
  825. }
  826. // Filter cached components on the way out to ensure that even if we have
  827. // cached more data than has been requested, the return value only reflects
  828. // the requested features/components.
  829. $return = $cache;
  830. $return = array_intersect_key($return, array_flip($features));
  831. foreach ($return as $k => $v) {
  832. $return[$k] = array_intersect_key($return[$k], array_flip($components));
  833. }
  834. return $return;
  835. }
  836. /**
  837. * Helper function to eliminate whitespace differences in code.
  838. */
  839. function _features_linetrim($code) {
  840. $code = explode("\n", $code);
  841. foreach ($code as $k => $line) {
  842. $code[$k] = trim($line);
  843. }
  844. return implode("\n", $code);
  845. }
  846. /**
  847. * "Sanitizes" an array recursively, performing two key operations:
  848. * - Sort an array by its keys (assoc) or values (non-assoc)
  849. * - Remove any null or empty values for associative arrays (array_filter()).
  850. */
  851. function _features_sanitize(&$array) {
  852. if (is_array($array)) {
  853. if (_features_is_assoc($array)) {
  854. ksort($array, SORT_STRING);
  855. $array = array_filter($array);
  856. }
  857. else {
  858. sort($array);
  859. }
  860. foreach ($array as $k => $v) {
  861. if (is_array($v)) {
  862. _features_sanitize($array[$k]);
  863. }
  864. }
  865. }
  866. }
  867. /**
  868. * Is the given array an associative array. This basically extracts the keys twice to get the
  869. * numerically ordered keys. It then does a diff with the original array and if there is no
  870. * key diff then the original array is not associative.
  871. *
  872. * NOTE: If you have non-sequential numerical keys, this will identify the array as assoc.
  873. *
  874. * Borrowed from: http://www.php.net/manual/en/function.is-array.php#96724
  875. *
  876. * @return True is the array is an associative array, false otherwise
  877. */
  878. function _features_is_assoc($array) {
  879. return (is_array($array) && (0 !== count(array_diff_key($array, array_keys(array_keys($array)))) || count($array)==0));
  880. }