search_api_page.install 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the Search pages module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function search_api_page_schema() {
  10. $schema['search_api_page'] = array(
  11. 'description' => '',
  12. 'fields' => array(
  13. 'id' => array(
  14. 'description' => 'The primary identifier for a search page.',
  15. 'type' => 'serial',
  16. 'unsigned' => TRUE,
  17. 'not null' => TRUE,
  18. ),
  19. 'index_id' => array(
  20. 'description' => 'The {search_api_index}.machine_name this page will search on.',
  21. 'type' => 'varchar',
  22. 'length' => 50,
  23. 'not null' => TRUE,
  24. ),
  25. 'path' => array(
  26. 'description' => 'The path at which this search page can be viewed.',
  27. 'type' => 'varchar',
  28. 'length' => 50,
  29. 'not null' => TRUE,
  30. ),
  31. 'name' => array(
  32. 'description' => 'The displayed name for a search page.',
  33. 'type' => 'varchar',
  34. 'length' => 50,
  35. 'not null' => TRUE,
  36. ),
  37. 'machine_name' => array(
  38. 'description' => 'The machine name for a search page.',
  39. 'type' => 'varchar',
  40. 'length' => 50,
  41. 'not null' => TRUE,
  42. ),
  43. 'description' => array(
  44. 'description' => 'The displayed description for a search page.',
  45. 'type' => 'text',
  46. 'not null' => FALSE,
  47. ),
  48. 'options' => array(
  49. 'description' => 'The options used to configure the search page.',
  50. 'type' => 'text',
  51. 'serialize' => TRUE,
  52. 'not null' => TRUE,
  53. ),
  54. 'enabled' => array(
  55. 'description' => 'A flag indicating whether the search page is enabled.',
  56. 'type' => 'int',
  57. 'size' => 'tiny',
  58. 'not null' => TRUE,
  59. 'default' => 1,
  60. ),
  61. 'status' => array(
  62. 'description' => 'The exportable status of the entity.',
  63. 'type' => 'int',
  64. 'not null' => TRUE,
  65. 'default' => 0x01,
  66. 'size' => 'tiny',
  67. ),
  68. 'module' => array(
  69. 'description' => 'The name of the providing module if the entity has been defined in code.',
  70. 'type' => 'varchar',
  71. 'length' => 255,
  72. 'not null' => FALSE,
  73. ),
  74. ),
  75. 'indexes' => array(
  76. 'enabled' => array('enabled'),
  77. 'index_id' => array('index_id'),
  78. ),
  79. 'unique' => array(
  80. 'path' => array('path'),
  81. 'machine_name' => array('machine_name'),
  82. ),
  83. 'primary key' => array('id'),
  84. );
  85. return $schema;
  86. }
  87. /**
  88. * Implements hook_update_dependencies().
  89. */
  90. function search_api_page_update_dependencies() {
  91. // This update should run after primary IDs have been changed to machine names in the framework.
  92. $dependencies['search_api_page'][7101] = array(
  93. 'search_api' => 7102,
  94. );
  95. return $dependencies;
  96. }
  97. /**
  98. * Make {search_api_page}.index_id the index' machine name.
  99. */
  100. function search_api_page_update_7101() {
  101. // Update of search_api_page:
  102. db_drop_index('search_api_page', 'index_id');
  103. $spec = array(
  104. 'description' => 'The {search_api_index}.machine_name this page will search on.',
  105. 'type' => 'varchar',
  106. 'length' => 50,
  107. 'not null' => TRUE,
  108. );
  109. db_change_field('search_api_page', 'index_id', 'index_id', $spec);
  110. db_add_index('search_api_page', 'index_id', array('index_id'));
  111. foreach (db_query('SELECT id, machine_name FROM {search_api_index}') as $index) {
  112. // We explicitly forbid numeric machine names, therefore we don't have to worry about conflicts here.
  113. db_update('search_api_page')
  114. ->fields(array(
  115. 'index_id' => $index->machine_name,
  116. ))
  117. ->condition('index_id', $index->id)
  118. ->execute();
  119. }
  120. }
  121. /**
  122. * Add a {search_api_page}.machine_name column.
  123. */
  124. function search_api_page_update_7102() {
  125. $tx = db_transaction();
  126. try {
  127. // Add the machine_name field, along with its unique key index.
  128. $spec = array(
  129. 'description' => 'The machine name for a search page.',
  130. 'type' => 'varchar',
  131. 'length' => 50,
  132. 'not null' => TRUE,
  133. 'default' => '',
  134. );
  135. db_add_field('search_api_page', 'machine_name', $spec);
  136. $names = array();
  137. foreach (db_query('SELECT id, name FROM {search_api_page}')->fetchAllKeyed() as $id => $name) {
  138. $base = $name = drupal_strtolower(preg_replace('/[^a-z0-9]+/i', '_', $name));
  139. $i = 0;
  140. while (isset($names[$name])) {
  141. $name = $base . '_' . ++$i;
  142. if (drupal_strlen($name) > 50) {
  143. $suffix_len = drupal_strlen('_' . $i);
  144. $base = drupal_substr($base, 0, 50 - $suffix_len);
  145. $name = $base . '_' . ++$i;
  146. }
  147. }
  148. $names[$name] = TRUE;
  149. db_update('search_api_page')
  150. ->fields(array(
  151. 'machine_name' => $name,
  152. ))
  153. ->condition('id', $id)
  154. ->execute();
  155. }
  156. db_add_unique_key('search_api_page', 'machine_name', array('machine_name'));
  157. // Add the status field.
  158. $spec = array(
  159. 'description' => 'The exportable status of the entity.',
  160. 'type' => 'int',
  161. 'not null' => TRUE,
  162. 'default' => 0x01,
  163. 'size' => 'tiny',
  164. );
  165. db_add_field('search_api_page', 'status', $spec);
  166. // Add the module field.
  167. $spec = array(
  168. 'description' => 'The name of the providing module if the entity has been defined in code.',
  169. 'type' => 'varchar',
  170. 'length' => 255,
  171. 'not null' => FALSE,
  172. );
  173. db_add_field('search_api_page', 'module', $spec);
  174. }
  175. catch (Exception $e) {
  176. $tx->rollback();
  177. try {
  178. db_drop_field('search_api_page', 'machine_name');
  179. db_drop_field('search_api_page', 'status');
  180. db_drop_field('search_api_page', 'module');
  181. }
  182. catch (Exception $e1) {
  183. // Ignore.
  184. }
  185. throw new DrupalUpdateException(t('An exception occurred during the update: @msg.', array('@msg' => $e->getMessage())));
  186. }
  187. }