features.install 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the features module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function features_schema() {
  10. $schema['cache_features'] = drupal_get_schema_unprocessed('system', 'cache');
  11. $schema['cache_features']['description'] = 'Cache table for features to store module info.';
  12. $schema['features_signature'] = array(
  13. 'description' => 'Stores hashes that reflect the last known state of a features component.',
  14. 'fields' => array(
  15. 'module' => array(
  16. 'description' => 'Name of the feature module.',
  17. 'type' => 'varchar',
  18. 'length' => 64,
  19. 'not null' => TRUE,
  20. ),
  21. 'component' => array(
  22. 'description' => 'Name of the features component.',
  23. 'type' => 'varchar',
  24. 'length' => 128,
  25. 'not null' => TRUE,
  26. ),
  27. 'signature' => array(
  28. 'description' => 'Hash reflecting the last approved state of the component in code.',
  29. 'type' => 'varchar',
  30. 'length' => 128,
  31. 'not null' => TRUE,
  32. ),
  33. 'updated' => array(
  34. 'description' => 'Timestamp when the signature was last updated.',
  35. 'type' => 'int',
  36. 'not null' => TRUE,
  37. 'default' => 0,
  38. ),
  39. 'message' => array(
  40. 'description' => 'Message to document why the component was updated.',
  41. 'type' => 'varchar',
  42. 'length' => 255,
  43. 'not null' => FALSE,
  44. ),
  45. ),
  46. 'primary key' => array('module', 'component'),
  47. 'indexes' => array(
  48. 'module' => array('module'),
  49. 'component' => array('component'),
  50. ),
  51. );
  52. return $schema;
  53. }
  54. /**
  55. * Implements hook_install().
  56. */
  57. function features_install() {
  58. _features_install_menu();
  59. db_update('system')
  60. ->fields(array('weight' => 20))
  61. ->condition('name', 'features')
  62. ->condition('type', 'module')
  63. ->execute();
  64. }
  65. /**
  66. * Implements hook_uninstall().
  67. */
  68. function features_uninstall() {
  69. variable_del('features_codecache');
  70. variable_del('features_default_export_path');
  71. variable_del('features_semaphore');
  72. variable_del('features_ignored_orphans');
  73. variable_del('features_feature_locked');
  74. variable_del('features_lock_mode');
  75. variable_del('features_update_7202_fixed_tmp');
  76. variable_del('features_update_7202_possibly_broken');
  77. db_delete('variable')
  78. ->condition('name', 'features_admin_show_component_%', 'LIKE')
  79. ->execute();
  80. db_delete('variable')
  81. ->condition('name', 'features_component_locked_%', 'LIKE')
  82. ->execute();
  83. if (db_table_exists('menu_custom')) {
  84. db_delete('menu_custom')
  85. ->condition('menu_name', 'features')
  86. ->execute();
  87. }
  88. db_delete('menu_links')
  89. ->condition('module', 'features')
  90. ->execute();
  91. }
  92. /**
  93. * Create menu. See menu.install for an example.
  94. */
  95. function _features_install_menu() {
  96. if (db_table_exists('menu_custom') && !db_query("SELECT menu_name FROM {menu_custom} WHERE menu_name = :menu_name", array(':menu_name' => 'features'))->fetchField()) {
  97. $t = get_t();
  98. $id = db_insert('menu_custom')
  99. ->fields(array(
  100. 'menu_name' => 'features',
  101. 'title' => $t('Features'),
  102. 'description' => $t('Menu items for any enabled features.'),
  103. ))
  104. ->execute();
  105. }
  106. }
  107. /**
  108. * Update 6100: Set module on all feature node types to 'features'.
  109. *
  110. * This update can be re-run as needed to repair any node types that are not
  111. * removed after disabling the associated feature.
  112. *
  113. * Any feature implementing a node component that was exported prior to this
  114. * version of the features.module will need to have its 'module' declaration
  115. * in hook_node_info() changed from 'node' to 'features'.
  116. */
  117. function features_update_6100() {
  118. $ret = array();
  119. foreach (features_get_features(NULL, TRUE) as $feature) {
  120. if (module_exists($feature->name) && $types = module_invoke($feature->name, 'node_info')) {
  121. foreach ($types as $type => $type_data) {
  122. $sql = "SELECT COUNT(*) FROM {node_type} WHERE module = 'node' AND type = '%s'";
  123. // Only update if the hook_node_info() type's module is 'features' and
  124. // the db type's module is 'node'.
  125. if (($type_data['module'] == 'features') && db_query($sql, $type)->fetchField()) {
  126. $ret[] = update_sql("UPDATE {node_type} SET module = 'features' WHERE type = '$type'");
  127. }
  128. }
  129. }
  130. }
  131. return $ret;
  132. }
  133. /**
  134. * Update 6101: Set codestate signature for all features.
  135. *
  136. * This update generates a codestate for all feature/component pairs which
  137. * have been installed prior to this version of Features. This prevents
  138. * automatic rebuilds from occurring against any rebuildable components
  139. * that have been overridden.
  140. */
  141. function features_update_6101() {
  142. // Ensure all of our own API functions still exist in this version
  143. // of Features. It's possible that the "future me" will not have these
  144. // functions, so I should check.
  145. module_load_include('inc', 'features', "features.export");
  146. $functions = array(
  147. 'features_include',
  148. 'features_hook',
  149. 'features_get_components',
  150. 'features_get_features',
  151. 'features_get_signature',
  152. 'features_set_signature',
  153. );
  154. $doit = TRUE;
  155. foreach ($functions as $function) {
  156. $doit = $doit && function_exists($function);
  157. }
  158. if ($doit) {
  159. features_include();
  160. $features = array_keys(features_get_features(NULL, TRUE));
  161. $components = array_keys(features_get_components());
  162. foreach ($features as $feature) {
  163. if (module_exists($feature)) {
  164. foreach ($components as $component) {
  165. if (features_hook($component, 'features_rebuild') && features_get_signature('cache', $feature, $component) === FALSE) {
  166. features_set_signature($feature, $component, -1);
  167. }
  168. }
  169. }
  170. }
  171. }
  172. return array();
  173. }
  174. /**
  175. * Add {cache_features} table.
  176. */
  177. function features_update_7200() {
  178. if (!db_table_exists('cache_features')) {
  179. $schema = drupal_get_schema_unprocessed('system', 'cache');
  180. db_create_table('cache_features', $schema);
  181. }
  182. }
  183. /**
  184. * Add {cache_featurestate} table.
  185. */
  186. function features_update_7201() {
  187. // This update hook is no longer active.
  188. }
  189. /**
  190. * Create a new table 'features_signature' to store signatures.
  191. */
  192. function features_update_7202() {
  193. if (!db_table_exists('features_signature')) {
  194. // Create the new table for signatures.
  195. $schema = array(
  196. 'description' => 'Stores hashes that reflect the last known state of a features component.',
  197. 'fields' => array(
  198. 'module' => array(
  199. 'description' => 'Name of the feature module.',
  200. 'type' => 'varchar',
  201. 'length' => 64,
  202. 'not null' => TRUE,
  203. ),
  204. 'component' => array(
  205. 'description' => 'Name of the features component.',
  206. 'type' => 'varchar',
  207. 'length' => 128,
  208. 'not null' => TRUE,
  209. ),
  210. 'signature' => array(
  211. 'description' => 'Hash reflecting the last approved state of the component in code.',
  212. 'type' => 'varchar',
  213. 'length' => 128,
  214. 'not null' => TRUE,
  215. ),
  216. 'updated' => array(
  217. 'description' => 'Timestamp when the signature was last updated.',
  218. 'type' => 'int',
  219. 'not null' => TRUE,
  220. 'default' => 0,
  221. ),
  222. 'message' => array(
  223. 'description' => 'Message to document why the component was updated.',
  224. 'type' => 'varchar',
  225. 'length' => 255,
  226. 'not null' => FALSE,
  227. ),
  228. ),
  229. 'primary key' => array('module', 'component'),
  230. 'indexes' => array(
  231. 'module' => array('module'),
  232. 'component' => array('component'),
  233. ),
  234. );
  235. db_create_table('features_signature', $schema);
  236. }
  237. // Load existing signatures.
  238. if (db_table_exists('cache_featurestate')) {
  239. // The old version of features_update_7201() has run in the past, and a
  240. // cache table was created to replace the 'features_codecache' variable.
  241. $cache = cache_get('features_codecache', 'cache_featurestate');
  242. $signaturess = !empty($cache->data)
  243. ? $cache->data
  244. : array();
  245. $message = __FUNCTION__ . '() - from cache storage';
  246. }
  247. else {
  248. // The current (inactive) version of features_update_7201() has run, and the
  249. // 'features_codecache' variable still exists.
  250. $signaturess = variable_get('features_codecache', array());
  251. $message = __FUNCTION__ . '() - from variable storage';
  252. }
  253. // Prevent existing records from being inserted again.
  254. // This way we don't need a REPLACE query.
  255. // This only applies if the table was already created e.g. in a previous
  256. // failed attempt to run this update.
  257. $q = db_select('features_signature', 'fs')->fields('fs');
  258. if ($qr = $q->execute()) {
  259. foreach ($qr as $obj) {
  260. unset($signaturess[$obj->module][$obj->component]);
  261. }
  262. }
  263. // Get a timestamp to be stored in each record.
  264. $timestamp = time();
  265. // Build the insert query.
  266. $insert = db_insert('features_signature')
  267. ->fields(array('module', 'component', 'signature', 'updated', 'message'));
  268. foreach ($signaturess as $module => $signatures) {
  269. foreach ($signatures as $component => $signature) {
  270. $record = array(
  271. 'module' => $module,
  272. 'component' => $component,
  273. 'signature' => $signature,
  274. 'updated' => $timestamp,
  275. 'message' => $message,
  276. );
  277. $insert->values($record);
  278. }
  279. }
  280. // Execute the insert query.
  281. // On failure, allow the exception to trickle up.
  282. $insert->execute();
  283. // Set a temporary marker variable for subsequent updates.
  284. // This variable was not set in an older version of this update.
  285. variable_set('features_update_7202_fixed_tmp', TRUE);
  286. // Delete the old table and variable if the data migration was successful.
  287. variable_del('features_codecache');
  288. if (db_table_exists('cache_featurestate')) {
  289. db_drop_table('cache_featurestate');
  290. }
  291. // Reset the static cache that determines the storage type.
  292. drupal_static_reset('_features_get_signature_storage_type');
  293. }
  294. /**
  295. * Reduce varchar lengths in 'features_signature' table. See #3181858.
  296. */
  297. function features_update_7203() {
  298. // The following operations are only needed for sites that previously ran an
  299. // older version of features_update_7202(). Trying to check this would
  300. // probably be more copmlex, so we run it anyway.
  301. db_change_field('features_signature', 'module', 'module', array(
  302. 'description' => 'Name of the feature module.',
  303. 'type' => 'varchar',
  304. 'length' => 64,
  305. 'not null' => TRUE,
  306. ));
  307. db_change_field('features_signature', 'component', 'component', array(
  308. 'description' => 'Name of the features component.',
  309. 'type' => 'varchar',
  310. 'length' => 128,
  311. 'not null' => TRUE,
  312. ));
  313. }
  314. /**
  315. * Set a marker variable, if the site could be affected by undesired reverts.
  316. */
  317. function features_update_7204() {
  318. if (variable_get('features_update_7202_fixed_tmp')) {
  319. // The fixed version of features_update_7202() did run.
  320. // Delete the temporary marker variable.
  321. variable_del('features_update_7202_fixed_tmp');
  322. }
  323. else {
  324. // Either the old broken version of features_update_7202() ran, or features
  325. // was newly installed in a version where 7202 or 7203 was the latest
  326. // update.
  327. // Set a marker variable, indicating that some overridden features
  328. // components might have been accidentally reverted. See #3183346.
  329. variable_set('features_update_7202_possibly_broken', TRUE);
  330. }
  331. }