apachesolr_search.install 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. /**
  3. * @file
  4. * Install and related hooks for apachesolr_search.
  5. */
  6. /**
  7. * Implements hook_install().
  8. */
  9. function apachesolr_search_install() {
  10. // Add a taxonomy search page to the database
  11. $settings = array(
  12. 'apachesolr_search_search_type' => 'tid',
  13. 'apachesolr_search_per_page' => 10,
  14. 'apachesolr_search_browse' => 'results',
  15. 'apachesolr_search_spellcheck' => FALSE,
  16. 'apachesolr_search_search_box' => FALSE,
  17. );
  18. $settings = serialize($settings);
  19. $fields = array(
  20. 'page_id' => 'taxonomy_search',
  21. 'label' => 'Taxonomy Search',
  22. 'description' => 'Search all items with given term',
  23. 'search_path' => 'taxonomy/term/%',
  24. 'env_id' => '',
  25. 'page_title' => '%value',
  26. 'settings' => $settings,
  27. );
  28. db_insert('apachesolr_search_page')->fields($fields)->execute();
  29. }
  30. /**
  31. * Implements hook_enable().
  32. */
  33. function apachesolr_search_enable() {
  34. // Make sure the default core search page is installed.
  35. $search_page = apachesolr_search_page_load('core_search');
  36. if (empty($search_page)) {
  37. // Add Default search page (core search)
  38. // This is a duplication from update_7004 but it is intended
  39. // so future changes are possible without breaking the update
  40. $settings = array(
  41. 'apachesolr_search_search_type' => 'custom',
  42. 'apachesolr_search_per_page' => 10,
  43. 'apachesolr_search_browse' => 'browse',
  44. 'apachesolr_search_spellcheck' => TRUE,
  45. 'apachesolr_search_not_removable' => TRUE,
  46. 'apachesolr_search_search_box' => TRUE,
  47. );
  48. $settings = serialize($settings);
  49. $fields = array(
  50. 'page_id' => 'core_search',
  51. 'label' => 'Core Search',
  52. 'description' => 'Core Search',
  53. 'search_path' => 'search/site',
  54. 'env_id' => 'solr',
  55. 'page_title' => 'Site',
  56. 'settings' => $settings,
  57. );
  58. db_insert('apachesolr_search_page')->fields($fields)->execute();
  59. }
  60. $active = variable_get('search_active_modules', array('node', 'user'));
  61. $active[] = 'apachesolr_search';
  62. variable_set('search_active_modules', array_unique($active));
  63. }
  64. /**
  65. * Implements hook_schema().
  66. */
  67. function apachesolr_search_schema() {
  68. $schema = array();
  69. $schema['apachesolr_search_page'] = array(
  70. 'description' => 'Apache Solr Search search page settings.',
  71. 'export' => array(
  72. // Environment machine name.
  73. 'key' => 'page_id',
  74. // Description of key.
  75. 'key name' => 'search page machine name',
  76. // Variable name to use in exported code.
  77. 'identifier' => 'searcher',
  78. // Use the same hook as the API name below.
  79. 'default hook' => 'apachesolr_search_default_searchers',
  80. 'status' => 'apachesolr_search_page_status',
  81. // CTools API implementation.
  82. 'api' => array(
  83. 'owner' => 'apachesolr_search',
  84. 'api' => 'apachesolr_search_defaults',
  85. 'minimum_version' => 3,
  86. 'current_version' => 3,
  87. ),
  88. ),
  89. 'fields' => array(
  90. 'page_id' => array(
  91. 'description' => 'The machine readable name of the search page.',
  92. 'type' => 'varchar',
  93. 'length' => 32,
  94. 'not null' => TRUE,
  95. 'default' => '',
  96. ),
  97. 'label' => array(
  98. 'description' => 'The human readable name of the search page.',
  99. 'type' => 'varchar',
  100. 'length' => 32,
  101. 'not null' => TRUE,
  102. 'default' => '',
  103. ),
  104. 'description' => array(
  105. 'description' => 'The description of the search page.',
  106. 'type' => 'varchar',
  107. 'length' => 255,
  108. 'not null' => TRUE,
  109. 'default' => '',
  110. ),
  111. 'search_path' => array(
  112. 'description' => 'The path to the search page.',
  113. 'type' => 'varchar',
  114. 'length' => 255,
  115. 'not null' => TRUE,
  116. 'default' => '',
  117. ),
  118. 'page_title' => array(
  119. 'description' => 'The title of the search page.',
  120. 'type' => 'varchar',
  121. 'length' => 255,
  122. 'not null' => TRUE,
  123. 'default' => '',
  124. ),
  125. 'env_id' => array(
  126. 'description' => 'The machine name of the search environment.',
  127. 'type' => 'varchar',
  128. 'length' => 64,
  129. 'not null' => TRUE,
  130. 'default' => '',
  131. ),
  132. 'settings' => array(
  133. 'description' => 'Serialized storage of general settings.',
  134. 'type' => 'text',
  135. 'serialize' => TRUE,
  136. ),
  137. ),
  138. 'primary key' => array('page_id'),
  139. 'indexes' => array(
  140. 'env_id' => array('env_id'),
  141. ),
  142. );
  143. return $schema;
  144. }
  145. /**
  146. * Implements hook_uninstall().
  147. */
  148. function apachesolr_search_uninstall() {
  149. $stored = variable_get('apachesolr_index_last', array());
  150. unset($stored['apachesolr_search']);
  151. variable_set('apachesolr_index_last', $stored);
  152. $active = variable_get('search_active_modules', array('node', 'user'));
  153. $idx = array_search('apachesolr_search', $active);
  154. if ($idx !== FALSE) {
  155. unset($active[$idx]);
  156. variable_set('search_active_modules', $active);
  157. }
  158. // Remove variables.
  159. variable_del('apachesolr_search_spellcheck');
  160. variable_del('apachesolr_search_mlt_blocks');
  161. variable_del('apachesolr_search_default_search_page');
  162. // Remove blocks.
  163. db_delete('block')->condition('module', 'apachesolr_search')->execute();
  164. }
  165. /**
  166. * Various updates for Drupal 7.
  167. */
  168. function apachesolr_search_update_7000() {
  169. $taxo_links = variable_get('apachesolr_search_taxonomy_links', 0);
  170. // TODO - enable the new contrib module?
  171. variable_del('apachesolr_search_taxonomy_links');
  172. // TODO - possibly rename block deltas, etc.
  173. $active = variable_get('search_active_modules', array('node', 'user'));
  174. $active[] = 'apachesolr_search';
  175. variable_set('search_active_modules', array_unique($active));
  176. if (variable_get('apachesolr_search_make_default', 0)) {
  177. variable_set('search_default_module', 'apachesolr_search');
  178. }
  179. variable_del('apachesolr_search_make_default');
  180. }
  181. /**
  182. * Add apachesolr_search_page table.
  183. */
  184. function apachesolr_search_update_7001() {
  185. // Moved to 7002
  186. }
  187. /**
  188. * Add apachesolr_search_page table for real.
  189. */
  190. function apachesolr_search_update_7002() {
  191. $schema['apachesolr_search_page'] = array(
  192. 'description' => 'Apache Solr Search search page settings.',
  193. 'export' => array(
  194. 'key' => 'page_id',
  195. 'identifier' => 'searcher',
  196. 'default hook' => 'apachesolr_search_default_searchers',
  197. 'status' => 'apachesolr_search_page_status',
  198. 'api' => array(
  199. 'owner' => 'apachesolr_search',
  200. 'api' => 'apachesolr_search_defaults',
  201. 'minimum_version' => 3,
  202. 'current_version' => 3,
  203. ),
  204. ),
  205. 'fields' => array(
  206. 'page_id' => array(
  207. 'description' => 'The machine readable name of the search page.',
  208. 'type' => 'varchar',
  209. 'length' => 32,
  210. 'not null' => TRUE,
  211. 'default' => '',
  212. ),
  213. 'label' => array(
  214. 'description' => 'The human readable name of the search page.',
  215. 'type' => 'varchar',
  216. 'length' => 32,
  217. 'not null' => TRUE,
  218. 'default' => '',
  219. ),
  220. 'description' => array(
  221. 'description' => 'The description of the search page.',
  222. 'type' => 'varchar',
  223. 'length' => 255,
  224. 'not null' => TRUE,
  225. 'default' => '',
  226. ),
  227. 'search_path' => array(
  228. 'description' => 'The path to the search page.',
  229. 'type' => 'varchar',
  230. 'length' => 255,
  231. 'not null' => TRUE,
  232. 'default' => '',
  233. ),
  234. 'page_title' => array(
  235. 'description' => 'The title of the search page.',
  236. 'type' => 'varchar',
  237. 'length' => 255,
  238. 'not null' => TRUE,
  239. 'default' => '',
  240. ),
  241. 'env_id' => array(
  242. 'description' => 'The machine name of the search environment.',
  243. 'type' => 'varchar',
  244. 'length' => 32,
  245. 'not null' => TRUE,
  246. 'default' => '',
  247. ),
  248. 'settings' => array(
  249. 'description' => 'Serialized storage of general settings.',
  250. 'type' => 'text',
  251. 'serialize' => TRUE,
  252. ),
  253. ),
  254. 'primary key' => array('page_id'),
  255. 'indexes' => array(
  256. 'env_id' => array('env_id'),
  257. ),
  258. );
  259. if (db_table_exists('apachesolr_search_page')) {
  260. // Just in case you are chasing HEAD.
  261. db_drop_table('apachesolr_search_page');
  262. }
  263. db_create_table('apachesolr_search_page', $schema['apachesolr_search_page']);
  264. }
  265. /**
  266. * Delete all Apache Solr Search blocks - they moved to Facet API.
  267. */
  268. function apachesolr_search_update_7003() {
  269. // Remove blocks.
  270. db_delete('block')->condition('module', 'apachesolr_search')->execute();
  271. }
  272. /**
  273. * Add a default search page for core
  274. * Add a taxonomy page if the taxonomy module was ever active
  275. */
  276. function apachesolr_search_update_7004() {
  277. // Add Default search page (core search)
  278. $settings = array(
  279. 'apachesolr_search_search_type' => 'custom',
  280. 'apachesolr_search_per_page' => variable_get('apachesolr_rows', 10),
  281. 'apachesolr_search_browse' => variable_get('apachesolr_search_browse', 'browse'),
  282. 'apachesolr_search_spellcheck' => variable_get('apachesolr_search_spellcheck', TRUE),
  283. 'apachesolr_search_not_removable' => TRUE,
  284. 'apachesolr_search_search_box' => TRUE,
  285. );
  286. $settings = serialize($settings);
  287. $fields = array(
  288. 'page_id' => 'core_search',
  289. 'label' => 'Core Search',
  290. 'description' => 'Site search',
  291. 'search_path' => 'search/site',
  292. 'env_id' => 'solr',
  293. 'page_title' => 'Site',
  294. 'settings' => $settings,
  295. );
  296. db_insert('apachesolr_search_page')->fields($fields)->execute();
  297. // Remove variables.
  298. variable_del('apachesolr_search_spellcheck');
  299. variable_del('apachesolr_search_browse');
  300. // Add this taxonomy search page to the database
  301. $settings = array(
  302. 'apachesolr_search_search_type' => 'tid',
  303. 'apachesolr_search_per_page' => 10,
  304. 'apachesolr_search_browse' => 'results',
  305. 'apachesolr_search_spellcheck' => FALSE,
  306. 'apachesolr_search_search_box' => FALSE,
  307. );
  308. $settings = serialize($settings);
  309. $fields = array(
  310. 'page_id' => 'taxonomy_search',
  311. 'label' => 'Taxonomy Search',
  312. 'description' => 'Search all items with given term',
  313. 'search_path' => 'taxonomy/term/%',
  314. 'env_id' => '',
  315. 'page_title' => '%value',
  316. 'settings' => $settings,
  317. );
  318. db_insert('apachesolr_search_page')->fields($fields)->execute();
  319. // Check if the taxonomy module was ever present
  320. $status = db_query("SELECT 1 FROM {system} WHERE name = 'apachesolr_taxonomy'")->fetchField();
  321. if ($status) {
  322. $message = t('If you had the apachesolr_taxonomy module enabled please go to the !link and enable the Taxonomy Term page', array('!link' => l('Apache Solr custom pages', 'admin/config/search/apachesolr/search-pages')));
  323. drupal_set_message($message, 'warning');
  324. }
  325. }
  326. /**
  327. * Make the env_id length on the apachesolr_search_page table 64 characters
  328. * to match the length of the env_id on all other tables
  329. */
  330. function apachesolr_search_update_7005(&$sandbox) {
  331. db_drop_index('apachesolr_search_page', 'env_id');
  332. db_change_field('apachesolr_search_page', 'env_id', 'env_id',
  333. array(
  334. 'description' => 'The machine name of the search environment.',
  335. 'type' => 'varchar',
  336. 'length' => 64,
  337. 'not null' => TRUE,
  338. 'default' => '',
  339. ),
  340. array(
  341. 'indexes' => array(
  342. 'env_id' => array('env_id'),
  343. )
  344. )
  345. );
  346. }
  347. /**
  348. * Remove all apachesolr_search env variables for show_facets if it is zero
  349. */
  350. function apachesolr_search_update_7006() {
  351. module_load_include('module', 'apachesolr');
  352. $environments = apachesolr_load_all_environments();
  353. foreach ($environments as $environment) {
  354. $show_facets = apachesolr_environment_variable_get($environment['env_id'], 'apachesolr_search_show_facets', 0);
  355. if ($show_facets === 0) {
  356. apachesolr_environment_variable_del($environment['env_id'], 'apachesolr_search_show_facets');
  357. }
  358. }
  359. }