features.export.inc 37 KB

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