synonyms.install 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. /**
  3. * @file
  4. * Install, update, and uninstall functions for the Synonyms module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function synonyms_schema() {
  10. $schema = array();
  11. $schema['synonyms_settings'] = array(
  12. 'description' => 'Stores synonyms settings for all the entities and fields. Only enabled synonyms behaviors are included in this table.',
  13. 'fields' => array(
  14. 'instance_id' => array(
  15. 'description' => 'Reference to {field_config_instance}.id of the instance, whose synonyms settings are stored in this row.',
  16. 'type' => 'int',
  17. 'not null' => TRUE,
  18. 'unsigned' => TRUE,
  19. ),
  20. 'behavior' => array(
  21. 'description' => 'Name of the synonyms behavior (ctools plugin), whose settings are stored in this row.',
  22. 'type' => 'varchar',
  23. 'length' => 255,
  24. 'not null' => TRUE,
  25. ),
  26. 'settings_serialized' => array(
  27. 'description' => 'Settings of the specified synonyms behavior for the specified field instance.',
  28. 'type' => 'text',
  29. 'serialize' => TRUE,
  30. 'not null' => TRUE,
  31. ),
  32. ),
  33. 'unique keys' => array(
  34. 'instance_behavior' => array('instance_id', 'behavior'),
  35. ),
  36. 'foreign keys' => array(
  37. 'field_config_instance' => array(
  38. 'table' => 'field_config_instance',
  39. 'columns' => array('instance_id' => 'id'),
  40. ),
  41. ),
  42. 'indexes' => array(
  43. 'behavior' => array('behavior'),
  44. ),
  45. );
  46. return $schema;
  47. }
  48. /**
  49. * Implements hook_uninstall().
  50. */
  51. function synonyms_uninstall() {
  52. // Cleaning all configure variables.
  53. $results = db_select('variable', 'var')
  54. ->fields('var', array('name'))
  55. ->condition('var.name', db_like('synonyms_') . '%', 'LIKE')
  56. ->execute();
  57. foreach ($results as $var) {
  58. variable_del($var->name);
  59. }
  60. }
  61. /**
  62. * Implements hook_update_N().
  63. *
  64. * Update to version 7.x-1.1 of Synonyms module.
  65. */
  66. function synonyms_update_7101() {
  67. $result = db_select('variable', 'var')
  68. ->fields('var', array('name'))
  69. ->condition('var.name', db_like('synonyms_settings_') . '%', 'LIKE')
  70. ->execute();
  71. foreach ($result as $var) {
  72. $settings = variable_get($var->name);
  73. // Term merging has been deprecated in favor of Term Merge module.
  74. unset($settings['term_merge']);
  75. // Enabled synonyms now stored as field names, since the field independency
  76. // has been introduced. See issue http://drupal.org/node/1850748.
  77. drupal_load('module', 'synonyms');
  78. $settings['synonyms'] = $settings['synonyms'] ? array('synonyms_synonyms') : array();
  79. variable_set($var->name, $settings);
  80. }
  81. return t('Updated settings of synonyms.');
  82. }
  83. /**
  84. * Multiple adjustments in the internal structures of the module.
  85. *
  86. * Unlock the 'synonyms_synonyms' field, because Synonyms module no longer uses
  87. * it.
  88. * Create 'synonyms_settings' table.
  89. * Enable 'synonyms_search' module if the core Search module is enabled.
  90. * Enable all available behaviors on all "source of synonyms" fields with the
  91. * default settings.
  92. */
  93. function synonyms_update_7102() {
  94. $field = field_info_field('synonyms_synonyms');
  95. $field['locked'] = FALSE;
  96. field_update_field($field);
  97. db_create_table('synonyms_settings', array(
  98. 'description' => 'Stores synonyms settings for all the entities and fields. Only enabled synonyms behaviors are included in this table.',
  99. 'fields' => array(
  100. 'instance_id' => array(
  101. 'description' => 'Reference to {field_config_instance}.id of the instance, whose synonyms settings are stored in this row.',
  102. 'type' => 'int',
  103. 'not null' => TRUE,
  104. 'unsigned' => TRUE,
  105. ),
  106. 'behavior' => array(
  107. 'description' => 'Name of the synonyms behavior (ctools plugin), whose settings are stored in this row.',
  108. 'type' => 'varchar',
  109. 'length' => 255,
  110. 'not null' => TRUE,
  111. ),
  112. 'settings_serialized' => array(
  113. 'description' => 'Settings of the specified synonyms behavior for the specified field instance.',
  114. 'type' => 'text',
  115. 'serialize' => TRUE,
  116. 'not null' => TRUE,
  117. ),
  118. ),
  119. 'unique keys' => array(
  120. 'instance_behavior' => array('instance_id', 'behavior'),
  121. ),
  122. 'foreign keys' => array(
  123. 'field_config_instance' => array(
  124. 'table' => 'field_config_instance',
  125. 'columns' => array('instance_id' => 'id'),
  126. ),
  127. ),
  128. 'indexes' => array(
  129. 'behavior' => array('behavior'),
  130. ),
  131. ));
  132. if (module_exists('search')) {
  133. module_enable(array('synonyms_search'));
  134. }
  135. $vars = db_select('variable', 'v')
  136. ->fields('v', array('name'))
  137. ->condition('name', db_like('synonyms_settings_') . '%', 'LIKE')
  138. ->execute();
  139. foreach ($vars as $row) {
  140. $var = variable_get($row->name);
  141. $vid = substr($row->name, drupal_strlen('synonyms_settings_'));
  142. $vocabulary = taxonomy_vocabulary_load($vid);
  143. if ($vocabulary) {
  144. $bundle = $vocabulary->machine_name;
  145. foreach ($var['synonyms'] as $field) {
  146. $instance = field_info_instance('taxonomy_term', $field, $bundle);
  147. foreach (synonyms_behaviors() as $behavior) {
  148. switch ($behavior['name']) {
  149. case 'synonyms':
  150. case 'search':
  151. default:
  152. $settings = array();
  153. break;
  154. case 'select':
  155. $settings = array(
  156. 'wording' => '@synonym',
  157. );
  158. break;
  159. case 'autocomplete':
  160. $settings = array(
  161. 'wording' => '@synonym, synonym of @term',
  162. );
  163. break;
  164. }
  165. $settings = array(
  166. 'instance_id' => $instance['id'],
  167. 'behavior' => $behavior['name'],
  168. 'settings' => $settings,
  169. );
  170. synonyms_behavior_settings_save($settings);
  171. }
  172. }
  173. }
  174. variable_del($row->name);
  175. }
  176. }