search_api_page.install 5.6 KB

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