facetapi.install 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * @file
  4. * Install, update, and uninstall functions for the Facet API module.
  5. */
  6. /**
  7. * Implementation of hook_schema().
  8. */
  9. function facetapi_schema() {
  10. $schema['facetapi'] = array(
  11. 'description' => 'Facet configurations.',
  12. 'export' => array(
  13. 'key' => 'name',
  14. 'identifier' => 'facet',
  15. 'default hook' => 'facetapi_default_facet_settings',
  16. 'api' => array(
  17. 'owner' => 'facetapi',
  18. 'api' => 'facetapi_defaults',
  19. 'minimum_version' => 1,
  20. 'current_version' => 1,
  21. ),
  22. ),
  23. 'fields' => array(
  24. 'name' => array(
  25. 'description' => 'The machine readable name of the configuration.',
  26. 'type' => 'varchar',
  27. 'length' => 255,
  28. 'not null' => TRUE,
  29. 'default' => '',
  30. ),
  31. 'searcher' => array(
  32. 'description' => 'The machine readable name of the searcher.',
  33. 'type' => 'varchar',
  34. 'length' => 64,
  35. 'not null' => TRUE,
  36. 'default' => '',
  37. ),
  38. 'realm' => array(
  39. 'description' => 'The machine readable name of the realm.',
  40. 'type' => 'varchar',
  41. 'length' => 64,
  42. 'not null' => TRUE,
  43. 'default' => '',
  44. ),
  45. 'facet' => array(
  46. 'description' => 'The machine readable name of the facet.',
  47. 'type' => 'varchar',
  48. 'length' => 255,
  49. 'not null' => TRUE,
  50. 'default' => '',
  51. ),
  52. 'enabled' => array(
  53. 'description' => 'Whether the facet is enabled.',
  54. 'type' => 'int',
  55. 'size' => 'tiny',
  56. 'not null' => TRUE,
  57. 'default' => 0,
  58. ),
  59. 'settings' => array(
  60. 'description' => 'Serialized storage of general settings.',
  61. 'type' => 'blob',
  62. 'serialize' => TRUE,
  63. ),
  64. ),
  65. 'primary key' => array('name'),
  66. );
  67. return $schema;
  68. }
  69. /**
  70. * Implementation of hook_install().
  71. */
  72. function facetapi_install() {
  73. // Nothing to do...
  74. }
  75. /**
  76. * Implementation of hook_uninstall().
  77. */
  78. function facetapi_uninstall() {
  79. // Remove all variables that start with "facetapi:".
  80. $args = array(':module' => 'facetapi%');
  81. $result = db_query('SELECT name FROM {variable} WHERE name LIKE :module', $args);
  82. foreach ($result as $record) {
  83. variable_del($record->name);
  84. }
  85. // Remove blocks.
  86. if (db_table_exists('block')) {
  87. db_delete('block')->condition('module', 'facetapi')->execute();
  88. }
  89. }
  90. /**
  91. * Update hashed block deltas to a URL-safe form.
  92. */
  93. function facetapi_update_7000() {
  94. $result = db_query("SELECT name FROM {facetapi}");
  95. foreach ($result as $f) {
  96. if (strlen($r->name) > 32) {
  97. $orig_delta = substr(base64_encode(hash('sha256', $r->name, TRUE)), 0, 32);
  98. $new_delta = strtr($orig_delta, array('+' => '-', '/' => '_', '=' => ''));
  99. db_update('block')
  100. ->fields(array(
  101. 'delta' => $new_delta,
  102. ))
  103. ->condition('module', 'facetapi')
  104. ->condition('delta', $orig_delta)
  105. ->execute();
  106. }
  107. }
  108. }
  109. /**
  110. * Hashes all blocks deltas related to Facet API.
  111. */
  112. function facetapi_update_7001() {
  113. // Clears the delta cache.
  114. db_delete('cache')
  115. ->condition('cid', 'facetapi:delta_map')
  116. ->execute();
  117. // Deletes blocks that are not enabled so they will get re-hashed.
  118. db_delete('block')
  119. ->condition('module', 'facetapi')
  120. ->condition('status', 0)
  121. ->execute();
  122. // Rehashes deltas of enabled facet blocks.
  123. $result = db_query("SELECT delta, bid FROM {block} WHERE module = 'facetapi'");
  124. foreach ($result as $record) {
  125. $current_search = FALSE;
  126. // Extracts the searcher, realm name, and facet name from $delta.
  127. // Process the parts from the end in case the searcher includes a ':'.
  128. $parts = explode(':', $record->delta);
  129. $facet_name = array_pop($parts);
  130. $realm_name = array_pop($parts);
  131. $searcher = implode(':', $parts);
  132. // We are viewing the current search block.
  133. if (!$searcher && 'current_search' == $facet_name) {
  134. $current_search = TRUE;
  135. }
  136. // If we don't have a searcher and we aren't viewing the current search
  137. // block, delta is probably hashed and we should continue to the next one.
  138. if (!$current_search && !$searcher) {
  139. // Let's do some block cleanup. Anything less that 32 chars is NOT a hash
  140. // and doesn't need to be in the database.
  141. if (strlen($record->delta) == 32) {
  142. continue;
  143. }
  144. else {
  145. db_delete('block')
  146. ->condition('bid', $record->bid)
  147. ->execute();
  148. }
  149. }
  150. // Hashes the delta and updates.
  151. $delta = substr(drupal_hash_base64($record->delta), 0, 32);
  152. db_update('block')
  153. ->fields(array('delta' => $delta))
  154. ->condition('module', 'facetapi')
  155. ->condition('delta', $record->delta)
  156. ->execute();
  157. }
  158. }
  159. /**
  160. * Ensures hashes are alpha-numeric.
  161. */
  162. function facetapi_update_7002() {
  163. // Updates the block table with alpha-numeric hashes.
  164. $result = db_query("SELECT delta, bid FROM {block} WHERE module = 'facetapi'");
  165. foreach ($result as $record) {
  166. $hash = strtr($record->delta, array('-' => '0', '_' => '1'));
  167. db_update('block')
  168. ->fields(array('delta' => $hash))
  169. ->condition('module', 'facetapi')
  170. ->condition('delta', $record->delta)
  171. ->execute();
  172. }
  173. // Clears the delta cache.
  174. cache_clear_all('facetapi:delta_map', 'cache');
  175. }
  176. /**
  177. * Increase the length of the facetapi.facet column.
  178. */
  179. function facetapi_update_7101() {
  180. // Change the length of the facet field.
  181. db_change_field('facetapi', 'facet', 'facet', array(
  182. 'description' => 'The machine readable name of the facet.',
  183. 'type' => 'varchar',
  184. 'length' => 255,
  185. 'not null' => TRUE,
  186. 'default' => '',
  187. ));
  188. }
  189. /**
  190. * Fix Postgres compatibility issue.
  191. */
  192. function facetapi_update_7102() {
  193. db_change_field('facetapi', 'settings', 'settings', array(
  194. 'type' => 'blob',
  195. 'description' => 'Serialized storage of general settings.',
  196. 'serialize' => TRUE,
  197. ));
  198. }
  199. /**
  200. * Clears facetapi:delta_map cache item to ensure that new realms are mapped.
  201. */
  202. function facetapi_update_7103() {
  203. cache_clear_all('facetapi:delta_map', 'cache');
  204. }