synonyms.install 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 providers. Only enabled synonyms behavior implementations are included in this table.',
  13. 'fields' => array(
  14. 'entity_type' => array(
  15. 'description' => 'Entity type whose behavior implementation is stored in this row.',
  16. 'type' => 'varchar',
  17. 'length' => 128,
  18. 'not null' => TRUE,
  19. ),
  20. 'bundle' => array(
  21. 'description' => 'Bundle name whose behavior implementation is stored in this row.',
  22. 'type' => 'varchar',
  23. 'length' => 128,
  24. 'not null' => TRUE,
  25. ),
  26. 'provider' => array(
  27. 'description' => 'Provider name whose behavior implementation is stored in this row.',
  28. 'type' => 'varchar',
  29. 'length' => 255,
  30. 'not null' => TRUE,
  31. ),
  32. 'behavior' => array(
  33. 'description' => 'Name of the synonyms behavior (ctools plugin), whose settings are stored in this row.',
  34. 'type' => 'varchar',
  35. 'length' => 255,
  36. 'not null' => TRUE,
  37. ),
  38. 'settings_serialized' => array(
  39. 'description' => 'Settings of the specified synonyms behavior for the specified field instance.',
  40. 'type' => 'text',
  41. 'serialize' => TRUE,
  42. 'not null' => TRUE,
  43. ),
  44. ),
  45. 'unique keys' => array(
  46. // We build 2 different indexes on the same column set because there are
  47. // 2 different functions that may query this table and the columns they
  48. // filter on may vary.
  49. 'behavior_implementation' => array('behavior', 'entity_type', 'bundle', 'provider'),
  50. 'all_enabled' => array('entity_type', 'bundle', 'provider', 'behavior'),
  51. ),
  52. );
  53. return $schema;
  54. }
  55. /**
  56. * Implements hook_uninstall().
  57. */
  58. function synonyms_uninstall() {
  59. // Cleaning all configure variables.
  60. $results = db_select('variable', 'var')
  61. ->fields('var', array('name'))
  62. ->condition('var.name', db_like('synonyms_') . '%', 'LIKE')
  63. ->execute();
  64. foreach ($results as $var) {
  65. variable_del($var->name);
  66. }
  67. }
  68. /**
  69. * Implements hook_requirements().
  70. */
  71. function synonyms_requirements($phase) {
  72. $requirements = array();
  73. switch ($phase) {
  74. case 'update':
  75. // Synonyms has changed its dependencies. It used to be Taxonomy and now
  76. // it is Entity module. We make sure Entity is enabled otherwise we halt
  77. // the update for Synonyms.
  78. // TODO: remove this requirement check at some point, when it becomes
  79. // obsolete.
  80. $t = get_t();
  81. $requirements['synonyms_entity_dependency'] = array(
  82. 'title' => $t('Synonyms depends on Entity'),
  83. 'description' => $t('Synonyms module depends on Entity module since 4-Mar-2016. At the same time it no longer depends on Taxonomy module while does integrate with the latter. If you do not have installed/enabled Entity module, do so before running updates on Synonyms module. Until Entity module is enabled and updates are executed, Synonyms module performance may degrade and even trigger PHP errors. See <a href="@url">@url</a> for more info.', array(
  84. '@url' => 'https://www.drupal.org/node/1194802',
  85. )),
  86. 'value' => module_exists('entity') ? $t('Passed') : $t('Failed'),
  87. 'severity' => module_exists('entity') ? REQUIREMENT_INFO : REQUIREMENT_ERROR,
  88. );
  89. break;
  90. }
  91. return $requirements;
  92. }
  93. /**
  94. * Implements hook_update_N().
  95. *
  96. * Update to version 7.x-1.1 of Synonyms module.
  97. */
  98. function synonyms_update_7101() {
  99. $result = db_select('variable', 'var')
  100. ->fields('var', array('name'))
  101. ->condition('var.name', db_like('synonyms_settings_') . '%', 'LIKE')
  102. ->execute();
  103. foreach ($result as $var) {
  104. $settings = variable_get($var->name);
  105. // Term merging has been deprecated in favor of Term Merge module.
  106. unset($settings['term_merge']);
  107. // Enabled synonyms now stored as field names, since the field independency
  108. // has been introduced. See issue http://drupal.org/node/1850748.
  109. drupal_load('module', 'synonyms');
  110. $settings['synonyms'] = $settings['synonyms'] ? array('synonyms_synonyms') : array();
  111. variable_set($var->name, $settings);
  112. }
  113. return t('Updated settings of synonyms.');
  114. }
  115. /**
  116. * Multiple adjustments in the internal structures of the module.
  117. *
  118. * Unlock the 'synonyms_synonyms' field, because Synonyms module no longer uses
  119. * it.
  120. * Create 'synonyms_settings' table.
  121. * Enable 'synonyms_search' module if the core Search module is enabled.
  122. * Enable all available behaviors on all "source of synonyms" fields with the
  123. * default settings.
  124. */
  125. function synonyms_update_7102() {
  126. $field = field_info_field('synonyms_synonyms');
  127. if ($field !== NULL) {
  128. $field['locked'] = FALSE;
  129. field_update_field($field);
  130. }
  131. db_create_table('synonyms_settings', array(
  132. 'description' => 'Stores synonyms settings for all the entities and fields. Only enabled synonyms behaviors are included in this table.',
  133. 'fields' => array(
  134. 'instance_id' => array(
  135. 'description' => 'Reference to {field_config_instance}.id of the instance, whose synonyms settings are stored in this row.',
  136. 'type' => 'int',
  137. 'not null' => TRUE,
  138. 'unsigned' => TRUE,
  139. ),
  140. 'behavior' => array(
  141. 'description' => 'Name of the synonyms behavior (ctools plugin), whose settings are stored in this row.',
  142. 'type' => 'varchar',
  143. 'length' => 255,
  144. 'not null' => TRUE,
  145. ),
  146. 'settings_serialized' => array(
  147. 'description' => 'Settings of the specified synonyms behavior for the specified field instance.',
  148. 'type' => 'text',
  149. 'serialize' => TRUE,
  150. 'not null' => TRUE,
  151. ),
  152. ),
  153. 'unique keys' => array(
  154. 'instance_behavior' => array('instance_id', 'behavior'),
  155. ),
  156. 'foreign keys' => array(
  157. 'field_config_instance' => array(
  158. 'table' => 'field_config_instance',
  159. 'columns' => array('instance_id' => 'id'),
  160. ),
  161. ),
  162. 'indexes' => array(
  163. 'behavior' => array('behavior'),
  164. ),
  165. ));
  166. if (module_exists('search')) {
  167. module_enable(array('synonyms_search'));
  168. }
  169. $vars = db_select('variable', 'v')
  170. ->fields('v', array('name'))
  171. ->condition('name', db_like('synonyms_settings_') . '%', 'LIKE')
  172. ->execute();
  173. foreach ($vars as $row) {
  174. $var = variable_get($row->name);
  175. $vid = substr($row->name, drupal_strlen('synonyms_settings_'));
  176. $vocabulary = taxonomy_vocabulary_load($vid);
  177. if ($vocabulary) {
  178. $bundle = $vocabulary->machine_name;
  179. foreach ($var['synonyms'] as $field) {
  180. $instance = field_info_instance('taxonomy_term', $field, $bundle);
  181. foreach (synonyms_behaviors() as $behavior) {
  182. switch ($behavior['name']) {
  183. case 'synonyms':
  184. case 'search':
  185. default:
  186. $settings = array();
  187. break;
  188. case 'select':
  189. $settings = array(
  190. 'wording' => '@synonym',
  191. );
  192. break;
  193. case 'autocomplete':
  194. $settings = array(
  195. 'wording' => '@synonym, synonym of @term',
  196. );
  197. break;
  198. }
  199. $settings = array(
  200. 'instance_id' => $instance['id'],
  201. 'behavior' => $behavior['name'],
  202. 'settings' => $settings,
  203. );
  204. synonyms_behavior_settings_save($settings);
  205. }
  206. }
  207. }
  208. variable_del($row->name);
  209. }
  210. }
  211. /**
  212. * Expanding Synonyms module to work with all entity types.
  213. *
  214. * Namely, the following happens:
  215. * - general "synonyms" behavior gets removed as too ambiguous.
  216. * - alter schema of synonyms table
  217. * - "select" and "autocomplete" behaviors undergo change in settings ("@term"
  218. * placeholder is replaced with "@entity" one)
  219. * - taxonomy term reference widgets get renamed to their new names
  220. * - all enabled behavior implementations are renamed to their new names in
  221. * order to support field- and property-based synonyms implementations
  222. */
  223. function synonyms_update_7103() {
  224. db_delete('synonyms_settings')
  225. ->condition('behavior', 'synonyms')
  226. ->execute();
  227. db_drop_index('synonyms_settings', 'behavior');
  228. db_drop_unique_key('synonyms_settings', 'instance_behavior');
  229. db_add_field('synonyms_settings', 'entity_type', array(
  230. 'description' => 'Entity type whose behavior implementation is stored in this row.',
  231. 'type' => 'varchar',
  232. 'length' => 255,
  233. 'not null' => TRUE,
  234. 'default' => '',
  235. ));
  236. db_add_field('synonyms_settings', 'bundle', array(
  237. 'description' => 'Bundle name whose behavior implementation is stored in this row.',
  238. 'type' => 'varchar',
  239. 'length' => 255,
  240. 'not null' => TRUE,
  241. 'default' => '',
  242. ));
  243. db_add_field('synonyms_settings', 'provider', array(
  244. 'description' => 'Provider name whose behavior implementation is stored in this row.',
  245. 'type' => 'varchar',
  246. 'length' => 255,
  247. 'not null' => TRUE,
  248. 'default' => '',
  249. ));
  250. db_query('UPDATE {synonyms_settings} s INNER JOIN {field_config_instance} i ON i.id = s.instance_id SET s.entity_type = i.entity_type, s.bundle = i.bundle, s.provider = i.field_name');
  251. db_drop_field('synonyms_settings', 'instance_id');
  252. db_add_unique_key('synonyms_settings', 'behavior_implementation', array('behavior', 'entity_type', 'bundle', 'provider'));
  253. db_add_unique_key('synonyms_settings', 'all_enabled', array('entity_type', 'bundle', 'provider', 'behavior'));
  254. module_enable(array('synonyms_provider_field'));
  255. $alter_behaviors = array('select', 'autocomplete');
  256. foreach (synonyms_behavior_get_all_enabled() as $behavior_implementation) {
  257. db_delete('synonyms_settings')
  258. ->condition('provider', $behavior_implementation['provider'])
  259. ->execute();
  260. $behavior_implementation['provider'] = synonyms_provider_field_provider_name(field_info_field($behavior_implementation['provider']));
  261. if (in_array($behavior_implementation['behavior'], $alter_behaviors)) {
  262. $behavior_implementation['settings']['wording'] = str_replace('@term', '@entity', $behavior_implementation['settings']['wording']);
  263. }
  264. synonyms_behavior_implementation_save($behavior_implementation);
  265. }
  266. // Keys are the old widget names, whereas corresponding values are the new
  267. // widget names.
  268. $migrate_map = array(
  269. 'synonyms_select' => 'synonyms_select_taxonomy_term',
  270. 'synonyms_autocomplete' => 'synonyms_autocomplete_taxonomy_term',
  271. );
  272. foreach (field_info_field_map() as $field_name => $field_info) {
  273. if ($field_info['type'] == 'taxonomy_term_reference') {
  274. foreach ($field_info['bundles'] as $entity_type => $bundles) {
  275. foreach ($bundles as $bundle) {
  276. $instance = field_read_instance($entity_type, $field_name, $bundle);
  277. if (in_array($instance['widget']['type'], array_keys($migrate_map))) {
  278. $instance['widget']['type'] = $migrate_map[$instance['widget']['type']];
  279. if ($instance['widget']['type'] == 'synonyms_autocomplete_taxonomy_term' && $instance['widget']['settings']['synonyms_autocomplete_path'] == 'synonyms/autocomplete') {
  280. $instance['widget']['settings']['synonyms_autocomplete_path'] = 'synonyms/autocomplete-taxonomy-term';
  281. }
  282. field_update_instance($instance);
  283. }
  284. }
  285. }
  286. }
  287. }
  288. }
  289. /**
  290. * Truncating 'entity_type' and 'bundle' columns to 128 length.
  291. */
  292. function synonyms_update_7104() {
  293. $table = 'synonyms_settings';
  294. $indexes = array(
  295. 'unique keys' => array(
  296. // We build 2 different indexes on the same column set because there are
  297. // 2 different functions that may query this table and the columns they
  298. // filter on may vary.
  299. 'behavior_implementation' => array('behavior', 'entity_type', 'bundle', 'provider'),
  300. 'all_enabled' => array('entity_type', 'bundle', 'provider', 'behavior'),
  301. ),
  302. );
  303. $fields = array(
  304. 'entity_type' => array(
  305. 'description' => 'Entity type whose behavior implementation is stored in this row.',
  306. 'type' => 'varchar',
  307. 'length' => 128,
  308. 'not null' => TRUE,
  309. ),
  310. 'bundle' => array(
  311. 'description' => 'Bundle name whose behavior implementation is stored in this row.',
  312. 'type' => 'varchar',
  313. 'length' => 128,
  314. 'not null' => TRUE,
  315. ),
  316. );
  317. foreach ($fields as $field => $schema) {
  318. foreach ($indexes['unique keys'] as $index_name => $index_specification) {
  319. db_drop_unique_key($table, $index_name);
  320. }
  321. db_change_field($table, $field, $field, $schema, $indexes);
  322. }
  323. }
  324. /**
  325. * Making actual module tables follow the declared schema.
  326. */
  327. function synonyms_update_7105() {
  328. db_drop_unique_key('synonyms_settings', 'behavior_implementation');
  329. db_drop_unique_key('synonyms_settings', 'all_enabled');
  330. db_change_field('synonyms_settings', 'provider', 'provider', array(
  331. 'description' => 'Provider name whose behavior implementation is stored in this row.',
  332. 'type' => 'varchar',
  333. 'length' => 255,
  334. 'not null' => TRUE,
  335. ), array(
  336. 'unique keys' => array(
  337. 'behavior_implementation' => array('behavior', 'entity_type', 'bundle', 'provider'),
  338. 'all_enabled' => array('entity_type', 'bundle', 'provider', 'behavior'),
  339. ),
  340. ));
  341. }