features.export.inc 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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. if ($reset) {
  43. drupal_static_reset(__FUNCTION__);
  44. }
  45. $processed = &drupal_static(__FUNCTION__, array());
  46. features_include();
  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. $cache = &drupal_static(__FUNCTION__, array());
  356. if (!isset($cache[$module->name])) {
  357. // Rebuild feature from .info file description and prepare an export from current DB state.
  358. $export = features_populate($module->info, $module->name);
  359. $export = features_export_prepare($export, $module->name, FALSE, FALSE);
  360. $overridden = array();
  361. // Compare feature info
  362. _features_sanitize($module->info);
  363. _features_sanitize($export);
  364. $compare = array('normal' => features_export_info($export), 'default' => features_export_info($module->info));
  365. if ($compare['normal'] !== $compare['default']) {
  366. $overridden['info'] = $compare;
  367. }
  368. // Collect differences at a per-component level
  369. $states = features_get_component_states(array($module->name), FALSE);
  370. foreach ($states[$module->name] as $component => $state) {
  371. if ($state != FEATURES_DEFAULT) {
  372. $normal = features_get_normal($component, $module->name);
  373. $default = features_get_default($component, $module->name);
  374. _features_sanitize($normal);
  375. _features_sanitize($default);
  376. $compare = array('normal' => features_var_export($normal), 'default' => features_var_export($default));
  377. if (_features_linetrim($compare['normal']) !== _features_linetrim($compare['default'])) {
  378. $overridden[$component] = $compare;
  379. }
  380. }
  381. }
  382. $cache[$module->name] = $overridden;
  383. }
  384. return $cache[$module->name];
  385. }
  386. /**
  387. * Gets the available default hooks keyed by components.
  388. */
  389. function features_get_default_hooks($component = NULL, $reset = FALSE) {
  390. return features_get_components($component, 'default_hook', $reset);
  391. }
  392. /**
  393. * Gets the available default hooks keyed by components.
  394. */
  395. function features_get_default_alter_hook($component) {
  396. $default_hook = features_get_components($component, 'default_hook');
  397. $alter_hook = features_get_components($component, 'alter_hook');
  398. $alter_type = features_get_components($component, 'alter_type');
  399. return empty($alter_type) || $alter_type != 'none' ? ($alter_hook ? $alter_hook : $default_hook) : FALSE;
  400. }
  401. /**
  402. * Return a code string representing an implementation of a defaults module hook.
  403. */
  404. function features_export_render_defaults($module, $hook, $code, $args = '') {
  405. $output = array();
  406. $output[] = "/**";
  407. $output[] = " * Implements hook_{$hook}().";
  408. $output[] = " */";
  409. $output[] = "function {$module}_{$hook}(" . $args . ") {";
  410. $output[] = $code;
  411. $output[] = "}";
  412. return implode("\n", $output);
  413. }
  414. /**
  415. * Generate code friendly to the Drupal .info format from a structured array.
  416. *
  417. * @param $info
  418. * An array or single value to put in a module's .info file.
  419. * @param $parents
  420. * Array of parent keys (internal use only).
  421. *
  422. * @return
  423. * A code string ready to be written to a module's .info file.
  424. */
  425. function features_export_info($info, $parents = array()) {
  426. $output = '';
  427. if (is_array($info)) {
  428. foreach ($info as $k => $v) {
  429. $child = $parents;
  430. $child[] = $k;
  431. $output .= features_export_info($v, $child);
  432. }
  433. }
  434. else if (!empty($info) && count($parents)) {
  435. $line = array_shift($parents);
  436. foreach ($parents as $key) {
  437. $line .= is_numeric($key) ? "[]" : "[{$key}]";
  438. }
  439. $line .= " = {$info}\n";
  440. return $line;
  441. }
  442. return $output;
  443. }
  444. /**
  445. * Tar creation function. Written by dmitrig01.
  446. *
  447. * @param $name
  448. * Filename of the file to be tarred.
  449. * @param $contents
  450. * String contents of the file.
  451. *
  452. * @return
  453. * A string of the tar file contents.
  454. */
  455. function features_tar_create($name, $contents) {
  456. /* http://www.mkssoftware.com/docs/man4/tar.4.asp */
  457. /* http://www.phpclasses.org/browse/file/21200.html */
  458. $tar = '';
  459. $bigheader = $header = '';
  460. if (strlen($name) > 100) {
  461. $bigheader = pack("a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155a12",
  462. '././@LongLink', '0000000', '0000000', '0000000',
  463. sprintf("%011o", strlen($name)), '00000000000',
  464. ' ', 'L', '', 'ustar ', '0',
  465. '', '', '', '', '', '');
  466. $bigheader .= str_pad($name, floor((strlen($name) + 512 - 1) / 512) * 512, "\0");
  467. $checksum = 0;
  468. for ($i = 0; $i < 512; $i++) {
  469. $checksum += ord(substr($bigheader, $i, 1));
  470. }
  471. $bigheader = substr_replace($bigheader, sprintf("%06o", $checksum)."\0 ", 148, 8);
  472. }
  473. $header = pack("a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155a12", // book the memorie area
  474. substr($name,0,100), // 0 100 File name
  475. '100644 ', // File permissions
  476. ' 765 ', // UID,
  477. ' 765 ', // GID,
  478. sprintf("%11s ", decoct(strlen($contents))), // Filesize,
  479. sprintf("%11s", decoct(REQUEST_TIME)), // Creation time
  480. ' ', // 148 8 Check sum for header block
  481. '', // 156 1 Link indicator / ustar Type flag
  482. '', // 157 100 Name of linked file
  483. 'ustar ', // 257 6 USTAR indicator "ustar"
  484. ' ', // 263 2 USTAR version "00"
  485. '', // 265 32 Owner user name
  486. '', // 297 32 Owner group name
  487. '', // 329 8 Device major number
  488. '', // 337 8 Device minor number
  489. '', // 345 155 Filename prefix
  490. ''); // 500 12 ??
  491. $checksum = 0;
  492. for ($i = 0; $i < 512; $i++) {
  493. $checksum += ord(substr($header, $i, 1));
  494. }
  495. $header = substr_replace($header, sprintf("%06o", $checksum)."\0 ", 148, 8);
  496. $tar = $bigheader.$header;
  497. $buffer = str_split($contents, 512);
  498. foreach ($buffer as $item) {
  499. $tar .= pack("a512", $item);
  500. }
  501. return $tar;
  502. }
  503. /**
  504. * Export var function -- from Views.
  505. */
  506. function features_var_export($var, $prefix = '', $init = TRUE, $count = 0) {
  507. if ($count > 50) {
  508. watchdog('features', 'Recursion depth reached in features_var_export', array());
  509. return '';
  510. }
  511. if (is_object($var)) {
  512. $output = method_exists($var, 'export') ? $var->export() : features_var_export((array) $var, '', FALSE, $count+1);
  513. }
  514. else if (is_array($var)) {
  515. if (empty($var)) {
  516. $output = 'array()';
  517. }
  518. else {
  519. $output = "array(\n";
  520. foreach ($var as $key => $value) {
  521. // Using normal var_export on the key to ensure correct quoting.
  522. $output .= " " . var_export($key, TRUE) . " => " . features_var_export($value, ' ', FALSE, $count+1) . ",\n";
  523. }
  524. $output .= ')';
  525. }
  526. }
  527. else if (is_bool($var)) {
  528. $output = $var ? 'TRUE' : 'FALSE';
  529. }
  530. else if (is_int($var)) {
  531. $output = intval($var);
  532. }
  533. else if (is_numeric($var)) {
  534. $floatval = floatval($var);
  535. if (is_string($var) && ((string) $floatval !== $var)) {
  536. // Do not convert a string to a number if the string
  537. // representation of that number is not identical to the
  538. // original value.
  539. $output = var_export($var, TRUE);
  540. }
  541. else {
  542. $output = $floatval;
  543. }
  544. }
  545. else if (is_string($var) && strpos($var, "\n") !== FALSE) {
  546. // Replace line breaks in strings with a token for replacement
  547. // at the very end. This protects whitespace in strings from
  548. // unintentional indentation.
  549. $var = str_replace("\n", "***BREAK***", $var);
  550. $output = var_export($var, TRUE);
  551. }
  552. else {
  553. $output = var_export($var, TRUE);
  554. }
  555. if ($prefix) {
  556. $output = str_replace("\n", "\n$prefix", $output);
  557. }
  558. if ($init) {
  559. $output = str_replace("***BREAK***", "\n", $output);
  560. }
  561. return $output;
  562. }
  563. /**
  564. * Helper function to return an array of t()'d translatables strings.
  565. * Useful for providing a separate array of translatables with your
  566. * export so that string extractors like potx can detect them.
  567. */
  568. function features_translatables_export($translatables, $indent = '') {
  569. $output = '';
  570. $translatables = array_filter(array_unique($translatables));
  571. if (!empty($translatables)) {
  572. $output .= "{$indent}// Translatables\n";
  573. $output .= "{$indent}// Included for use with string extractors like potx.\n";
  574. sort($translatables);
  575. foreach ($translatables as $string) {
  576. $output .= "{$indent}t(" . features_var_export($string) . ");\n";
  577. }
  578. }
  579. return $output;
  580. }
  581. /**
  582. * Get a summary storage state for a feature.
  583. */
  584. function features_get_storage($module_name) {
  585. // Get component states, and array_diff against array(FEATURES_DEFAULT).
  586. // If the returned array has any states that don't match FEATURES_DEFAULT,
  587. // return the highest state.
  588. $states = features_get_component_states(array($module_name), FALSE);
  589. $states = array_diff($states[$module_name], array(FEATURES_DEFAULT));
  590. $storage = !empty($states) ? max($states) : FEATURES_DEFAULT;
  591. return $storage;
  592. }
  593. /**
  594. * Wrapper around features_get_[storage] to return an md5hash of a normalized
  595. * defaults/normal object array. Can be used to compare normal/default states
  596. * of a module's component.
  597. */
  598. function features_get_signature($state = 'default', $module_name, $component, $reset = FALSE) {
  599. switch ($state) {
  600. case 'cache':
  601. $codecache = variable_get('features_codecache', array());
  602. return isset($codecache[$module_name][$component]) ? $codecache[$module_name][$component] : FALSE;
  603. case 'default':
  604. $objects = features_get_default($component, $module_name, TRUE, $reset);
  605. break;
  606. case 'normal':
  607. $objects = features_get_normal($component, $module_name, $reset);
  608. break;
  609. }
  610. if (!empty($objects)) {
  611. $objects = (array) $objects;
  612. _features_sanitize($objects);
  613. return md5(_features_linetrim(features_var_export($objects)));
  614. }
  615. return FALSE;
  616. }
  617. /**
  618. * Set the signature of a module/component pair in the codecache.
  619. */
  620. function features_set_signature($module, $component, $signature = NULL) {
  621. $var_codecache = variable_get('features_codecache', array());
  622. $signature = isset($signature) ? $signature : features_get_signature('default', $module, $component, TRUE);
  623. $var_codecache[$module][$component] = $signature;
  624. variable_set('features_codecache', $var_codecache);
  625. }
  626. /**
  627. * Processing semaphore operations.
  628. */
  629. function features_semaphore($op, $component) {
  630. // Note: we don't use variable_get() here as the inited variable
  631. // static cache may be stale. Retrieving directly from the DB narrows
  632. // the possibility of collision.
  633. $semaphore = db_query("SELECT value FROM {variable} WHERE name = :name", array(':name' => 'features_semaphore'))->fetchField();
  634. $semaphore = !empty($semaphore) ? unserialize($semaphore) : array();
  635. switch ($op) {
  636. case 'get':
  637. return isset($semaphore[$component]) ? $semaphore[$component] : FALSE;
  638. case 'set':
  639. $semaphore[$component] = REQUEST_TIME;
  640. variable_set('features_semaphore', $semaphore);
  641. break;
  642. case 'del':
  643. if (isset($semaphore[$component])) {
  644. unset($semaphore[$component]);
  645. variable_set('features_semaphore', $semaphore);
  646. }
  647. break;
  648. }
  649. }
  650. /**
  651. * Get normal objects for a given module/component pair.
  652. */
  653. function features_get_normal($component, $module_name, $reset = FALSE) {
  654. if ($reset) {
  655. drupal_static_reset(__FUNCTION__);
  656. }
  657. $cache = &drupal_static(__FUNCTION__, array());
  658. if (!isset($cache[$module_name][$component])) {
  659. features_include();
  660. $code = NULL;
  661. $module = features_get_features($module_name);
  662. // Special handling for dependencies component.
  663. if ($component === 'dependencies') {
  664. $cache[$module_name][$component] = isset($module->info['dependencies']) ? array_filter($module->info['dependencies'], 'module_exists') : array();
  665. }
  666. // All other components.
  667. else {
  668. $default_hook = features_get_default_hooks($component);
  669. if ($module && $default_hook && isset($module->info['features'][$component]) && features_hook($component, 'features_export_render')) {
  670. $code = features_invoke($component, 'features_export_render', $module_name, $module->info['features'][$component], NULL);
  671. $cache[$module_name][$component] = isset($code[$default_hook]) ? eval($code[$default_hook]) : FALSE;
  672. }
  673. }
  674. // Clear out vars for memory's sake.
  675. unset($code);
  676. unset($module);
  677. }
  678. return isset($cache[$module_name][$component]) ? $cache[$module_name][$component] : FALSE;
  679. }
  680. /**
  681. * Get defaults for a given module/component pair.
  682. */
  683. function features_get_default($component, $module_name = NULL, $alter = TRUE, $reset = FALSE) {
  684. $cache = &drupal_static(__FUNCTION__, array());
  685. $alter = !empty($alter); // ensure $alter is a true/false boolean
  686. features_include();
  687. features_include_defaults($component);
  688. $default_hook = features_get_default_hooks($component);
  689. $components = features_get_components();
  690. // Collect defaults for all modules if no module name was specified.
  691. if (isset($module_name)) {
  692. $modules = array($module_name);
  693. }
  694. else {
  695. if ($component === 'dependencies') {
  696. $modules = array_keys(features_get_features());
  697. }
  698. else {
  699. $modules = array();
  700. foreach (features_get_component_map($component) as $component_modules) {
  701. $modules = array_merge($modules, $component_modules);
  702. }
  703. $modules = array_unique($modules);
  704. }
  705. }
  706. // Collect and cache information for each specified module.
  707. foreach ($modules as $m) {
  708. if (!isset($cache[$component][$alter][$m]) || $reset) {
  709. // Special handling for dependencies component.
  710. if ($component === 'dependencies') {
  711. $module = features_get_features($m);
  712. $cache[$component][$alter][$m] = isset($module->info['dependencies']) ? $module->info['dependencies'] : array();
  713. unset($module);
  714. }
  715. // All other components
  716. else {
  717. if ($default_hook && module_hook($m, $default_hook)) {
  718. $cache[$component][$alter][$m] = call_user_func("{$m}_{$default_hook}");
  719. if (is_array($cache[$component][$alter][$m])) {
  720. $alter_type = features_get_components('alter_type', $component);
  721. if ($alter && (!isset($alter_type) || $alter_type == FEATURES_ALTER_TYPE_NORMAL)) {
  722. if ($alter_hook = features_get_default_alter_hook($component)) {
  723. drupal_alter($alter_hook, $cache[$component][$alter][$m]);
  724. }
  725. }
  726. }
  727. else {
  728. $cache[$component][$alter][$m] = FALSE;
  729. }
  730. }
  731. else {
  732. $cache[$component][$alter][$m] = FALSE;
  733. }
  734. }
  735. }
  736. }
  737. // A specific module was specified. Retrieve only its components.
  738. if (isset($module_name)) {
  739. return isset($cache[$component][$alter][$module_name]) ? $cache[$component][$alter][$module_name] : FALSE;
  740. }
  741. // No module was specified. Retrieve all components.
  742. $all_defaults = array();
  743. if (isset($cache[$component][$alter])) {
  744. foreach (array_filter($cache[$component][$alter]) as $module_components) {
  745. $all_defaults = array_merge($all_defaults, $module_components);
  746. }
  747. }
  748. return $all_defaults;
  749. }
  750. /**
  751. * Get a map of components to their providing modules.
  752. */
  753. function features_get_default_map($component, $attribute = NULL, $callback = NULL, $reset = FALSE) {
  754. $map = &drupal_static(__FUNCTION__, array());
  755. global $features_ignore_conflicts;
  756. if ($features_ignore_conflicts) {
  757. return FALSE;
  758. }
  759. features_include();
  760. features_include_defaults($component);
  761. if ((!isset($map[$component]) || $reset) && $default_hook = features_get_default_hooks($component)) {
  762. $map[$component] = array();
  763. foreach (module_implements($default_hook) as $module) {
  764. if ($defaults = features_get_default($component, $module)) {
  765. foreach ($defaults as $key => $object) {
  766. if (isset($callback)) {
  767. if ($object_key = $callback($object)) {
  768. $map[$component][$object_key] = $module;
  769. }
  770. }
  771. elseif (isset($attribute)) {
  772. if (is_object($object) && isset($object->{$attribute})) {
  773. $map[$component][$object->{$attribute}] = $module;
  774. }
  775. elseif (is_array($object) && isset($object[$attribute])) {
  776. $map[$component][$object[$attribute]] = $module;
  777. }
  778. }
  779. elseif (!isset($attribute) && !isset($callback)) {
  780. if (!is_numeric($key)) {
  781. $map[$component][$key] = $module;
  782. }
  783. }
  784. else {
  785. return FALSE;
  786. }
  787. }
  788. }
  789. }
  790. }
  791. return isset($map[$component]) ? $map[$component] : FALSE;
  792. }
  793. /**
  794. * Retrieve an array of features/components and their current states.
  795. */
  796. function features_get_component_states($features = array(), $rebuild_only = TRUE, $reset = FALSE) {
  797. if ($reset) {
  798. drupal_static_reset(__FUNCTION__);
  799. }
  800. $cache = &drupal_static(__FUNCTION__, array());
  801. $all_features = features_get_features();
  802. $features = !empty($features) ? $features : array_keys($all_features);
  803. // Retrieve only rebuildable components if requested.
  804. features_include();
  805. $components = array_keys(features_get_components());
  806. if ($rebuild_only) {
  807. foreach ($components as $k => $component) {
  808. if (!features_hook($component, 'features_rebuild')) {
  809. unset($components[$k]);
  810. }
  811. }
  812. }
  813. foreach ($features as $feature) {
  814. $cache[$feature] = isset($cache[$feature]) ? $cache[$feature] : array();
  815. if (module_exists($feature) && !empty($all_features[$feature]->components)) {
  816. foreach (array_intersect($all_features[$feature]->components, $components) as $component) {
  817. if (!isset($cache[$feature][$component])) {
  818. $normal = features_get_signature('normal', $feature, $component, $reset);
  819. $default = features_get_signature('default', $feature, $component, $reset);
  820. $codecache = features_get_signature('cache', $feature, $component, $reset);
  821. $semaphore = features_semaphore('get', $component);
  822. // DB and code states match, there is nothing more to check.
  823. if ($normal == $default) {
  824. $cache[$feature][$component] = FEATURES_DEFAULT;
  825. // Stale semaphores can be deleted.
  826. features_semaphore('del', $component);
  827. // Update code cache if it is stale, clear out semaphore if it stale.
  828. if ($default != $codecache) {
  829. features_set_signature($feature, $component, $default);
  830. }
  831. }
  832. // Component properly implements exportables.
  833. else if (!features_hook($component, 'features_rebuild')) {
  834. $cache[$feature][$component] = FEATURES_OVERRIDDEN;
  835. }
  836. // Component does not implement exportables.
  837. else {
  838. if (empty($semaphore)) {
  839. // Exception for dependencies. Dependencies are always rebuildable.
  840. if ($component === 'dependencies') {
  841. $cache[$feature][$component] = FEATURES_REBUILDABLE;
  842. }
  843. // All other rebuildable components require comparison.
  844. else {
  845. // Code has not changed, but DB does not match. User has DB overrides.
  846. if ($codecache == $default) {
  847. $cache[$feature][$component] = FEATURES_OVERRIDDEN;
  848. }
  849. // DB has no modifications to prior code state (or this is initial install).
  850. else if (empty($codecache) || $codecache == $normal) {
  851. $cache[$feature][$component] = FEATURES_REBUILDABLE;
  852. }
  853. // None of the states match. Requires user intervention.
  854. else if ($codecache != $default) {
  855. $cache[$feature][$component] = FEATURES_NEEDS_REVIEW;
  856. }
  857. }
  858. }
  859. else {
  860. // Semaphore is still within processing horizon. Do nothing.
  861. if ((REQUEST_TIME - $semaphore) < FEATURES_SEMAPHORE_TIMEOUT) {
  862. $cache[$feature][$component] = FEATURES_REBUILDING;
  863. }
  864. // A stale semaphore means a previous rebuild attempt did not complete.
  865. // Attempt to complete the rebuild.
  866. else {
  867. $cache[$feature][$component] = FEATURES_REBUILDABLE;
  868. }
  869. }
  870. }
  871. }
  872. }
  873. }
  874. }
  875. // Filter cached components on the way out to ensure that even if we have
  876. // cached more data than has been requested, the return value only reflects
  877. // the requested features/components.
  878. $return = $cache;
  879. $return = array_intersect_key($return, array_flip($features));
  880. foreach ($return as $k => $v) {
  881. $return[$k] = array_intersect_key($return[$k], array_flip($components));
  882. }
  883. return $return;
  884. }
  885. /**
  886. * Helper function to eliminate whitespace differences in code.
  887. */
  888. function _features_linetrim($code) {
  889. $code = explode("\n", $code);
  890. foreach ($code as $k => $line) {
  891. $code[$k] = trim($line);
  892. }
  893. return implode("\n", $code);
  894. }
  895. /**
  896. * "Sanitizes" an array recursively, performing two key operations:
  897. * - Sort an array by its keys (assoc) or values (non-assoc)
  898. * - Remove any null or empty values for associative arrays (array_filter()).
  899. */
  900. function _features_sanitize(&$array) {
  901. if (is_array($array)) {
  902. $is_assoc = _features_is_assoc($array);
  903. if ($is_assoc) {
  904. ksort($array, SORT_STRING);
  905. $array = array_filter($array);
  906. }
  907. else {
  908. sort($array);
  909. }
  910. foreach ($array as $k => $v) {
  911. if (is_array($v)) {
  912. _features_sanitize($array[$k]);
  913. if ($is_assoc && empty($array[$k])) {
  914. unset($array[$k]);
  915. }
  916. }
  917. }
  918. }
  919. }
  920. /**
  921. * Is the given array an associative array. This basically extracts the keys twice to get the
  922. * numerically ordered keys. It then does a diff with the original array and if there is no
  923. * key diff then the original array is not associative.
  924. *
  925. * NOTE: If you have non-sequential numerical keys, this will identify the array as assoc.
  926. *
  927. * Borrowed from: http://www.php.net/manual/en/function.is-array.php#96724
  928. *
  929. * @return True is the array is an associative array, false otherwise
  930. */
  931. function _features_is_assoc($array) {
  932. return (is_array($array) && (0 !== count(array_diff_key($array, array_keys(array_keys($array)))) || count($array)==0));
  933. }