apachesolr.install 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. <?php
  2. /**
  3. * @file
  4. * Install and related hooks for apachesolr_search.
  5. */
  6. /**
  7. * Implements hook_requirements().
  8. */
  9. function apachesolr_requirements($phase) {
  10. $requirements = array();
  11. if ($phase != 'runtime') {
  12. return $requirements;
  13. }
  14. // Ensure translations don't break at install time
  15. $t = get_t();
  16. $has_settings = FALSE;
  17. $id = apachesolr_default_environment();
  18. $environment = apachesolr_environment_load($id);
  19. if (!$environment || empty($environment['url'])) {
  20. $requirements['apachesolr'] = array(
  21. 'title' => $t('Apache Solr'),
  22. 'value' => $t('Missing environment configuration'),
  23. 'description' => $t('Missing or invalid Solr environment record for the default environment ID %id.', array('%id' => $id)),
  24. 'severity' => REQUIREMENT_ERROR,
  25. );
  26. }
  27. else {
  28. $has_settings = TRUE;
  29. }
  30. if ($has_settings) {
  31. $ping = FALSE;
  32. try {
  33. $solr = apachesolr_get_solr($id);
  34. $ping = @$solr->ping(variable_get('apachesolr_ping_timeout', 4));
  35. // If there is no $solr object, there is no instance available, so don't continue.
  36. if (!$ping) {
  37. throw new Exception(t('No Solr instance available when checking requirements.'));
  38. }
  39. }
  40. catch (Exception $e) {
  41. watchdog('Apache Solr', nl2br(check_plain($e->getMessage())), NULL, WATCHDOG_ERROR);
  42. }
  43. $value = $ping ? $t('Your site has contacted the Apache Solr server.') : $t('Your site was unable to contact the Apache Solr server.');
  44. $severity = $ping ? REQUIREMENT_OK : REQUIREMENT_ERROR;
  45. $requirements['apachesolr'] = array(
  46. 'title' => $t('Apache Solr'),
  47. 'value' => $value,
  48. 'description' => $t('Default environment url: <br/> %url', array('%url' => $environment['url'])),
  49. 'severity' => $severity,
  50. );
  51. }
  52. return $requirements;
  53. }
  54. /**
  55. * Implements hook_install().
  56. */
  57. function apachesolr_install() {
  58. module_load_include('inc', 'apachesolr', 'apachesolr_search.admin');
  59. module_load_include('inc', 'apachesolr', 'apachesolr.index');
  60. // Create one MLT block.
  61. apachesolr_search_mlt_save_block(array('name' => st('More like this')));
  62. db_insert('apachesolr_environment')->fields(array('env_id' => 'solr', 'name' => 'localhost server', 'url' => 'http://localhost:8983/solr'))->execute();
  63. // Initialize the entities to index. We enable all node types by default
  64. $info = entity_get_info('node');
  65. $bundles = array_keys($info['bundles']);
  66. $env_id = apachesolr_default_environment();
  67. apachesolr_index_set_bundles($env_id, 'node', $bundles);
  68. drupal_set_message(st('Apache Solr is enabled. Visit the <a href="@settings_link">settings page</a>.', array('@settings_link' => url('admin/config/search/apachesolr'))));
  69. }
  70. /**
  71. * Implements hook_enable().
  72. */
  73. function apachesolr_enable() {
  74. // Completely build the index table.
  75. module_load_include('inc', 'apachesolr', 'apachesolr.index');
  76. $env_id = apachesolr_default_environment();
  77. apachesolr_index_mark_for_reindex($env_id);
  78. }
  79. /**
  80. * Implements hook_schema().
  81. */
  82. function apachesolr_schema() {
  83. $table = drupal_get_schema_unprocessed('system', 'cache');
  84. $table['description'] = 'Cache table for apachesolr to store Luke data and indexing information.';
  85. $schema['cache_apachesolr'] = $table;
  86. $schema['apachesolr_environment'] = array(
  87. 'description' => 'The Solr search environment table.',
  88. // Enable CTools exportables based on this table.
  89. 'export' => array(
  90. // Environment machine name.
  91. 'key' => 'env_id',
  92. // Description of key.
  93. 'key name' => 'Environment machine name',
  94. // Apache Solr doesn't allow disabling environments.
  95. 'can disable' => FALSE,
  96. // Variable name to use in exported code.
  97. 'identifier' => 'environment',
  98. // Thin wrapper for the environment save callback.
  99. 'save callback' => 'apachesolr_ctools_environment_save',
  100. // Thin wrapper for the environment delete callback.
  101. 'delete callback' => 'apachesolr_ctools_environment_delete',
  102. // Includes the environment variables in 'conf' as well as the fields in this table.
  103. 'export callback' => 'apachesolr_ctools_environment_export',
  104. // Use the same hook as the API name below.
  105. 'default hook' => 'apachesolr_environments',
  106. // CTools API implementation.
  107. 'api' => array(
  108. 'owner' => 'apachesolr',
  109. 'api' => 'apachesolr_environments',
  110. 'minimum_version' => 1,
  111. 'current_version' => 1,
  112. ),
  113. ),
  114. 'fields' => array(
  115. 'env_id' => array(
  116. 'description' => 'Unique identifier for the environment',
  117. 'type' => 'varchar',
  118. 'length' => 64,
  119. 'not null' => TRUE,
  120. ),
  121. 'name' => array(
  122. 'description' => 'Human-readable name for the server',
  123. 'type' => 'varchar',
  124. 'length' => 255,
  125. 'not null' => TRUE,
  126. 'default' => ''
  127. ),
  128. 'url' => array(
  129. 'description' => 'Full url for the server',
  130. 'type' => 'varchar',
  131. 'length' => 1000,
  132. 'not null' => TRUE,
  133. ),
  134. 'service_class' => array(
  135. 'description' => 'Optional class name to use for connection',
  136. 'type' => 'varchar',
  137. 'length' => 255,
  138. 'not null' => TRUE,
  139. 'default' => ''
  140. ),
  141. ),
  142. 'primary key' => array('env_id'),
  143. );
  144. $schema['apachesolr_environment_variable'] = array(
  145. 'description' => 'Variable values for each Solr search environment.',
  146. 'fields' => array(
  147. 'env_id' => array(
  148. 'description' => 'Unique identifier for the environment',
  149. 'type' => 'varchar',
  150. 'length' => 64,
  151. 'not null' => TRUE,
  152. ),
  153. 'name' => array(
  154. 'description' => 'The name of the variable.',
  155. 'type' => 'varchar',
  156. 'length' => 128,
  157. 'not null' => TRUE,
  158. 'default' => '',
  159. ),
  160. 'value' => array(
  161. 'description' => 'The value of the variable.',
  162. 'type' => 'blob',
  163. 'not null' => TRUE,
  164. 'size' => 'big',
  165. ),
  166. ),
  167. 'primary key' => array('env_id', 'name'),
  168. );
  169. // Technically the entity system does not require an integer ID.
  170. // However, documentation mentions :
  171. // id: The name of the property that contains the primary id of the
  172. // entity. Every entity object passed to the Field API must have this
  173. // property and its value must be numeric.
  174. //Predefine an amount of types that get their own table
  175. $types = array(
  176. 'other' => 'apachesolr_index_entities',
  177. 'node' => 'apachesolr_index_entities_node',
  178. );
  179. foreach ($types as $type => $table) {
  180. $schema[$table] = array(
  181. 'description' => 'Stores a record of when an entity changed to determine if it needs indexing by Solr.',
  182. 'fields' => array(
  183. 'entity_type' => array(
  184. 'description' => 'The type of entity.',
  185. 'type' => 'varchar',
  186. 'length' => 32,
  187. 'not null' => TRUE,
  188. ),
  189. 'entity_id' => array(
  190. 'description' => 'The primary identifier for an entity.',
  191. 'type' => 'int',
  192. 'unsigned' => TRUE,
  193. 'not null' => TRUE,
  194. ),
  195. 'bundle' => array(
  196. 'description' => 'The bundle to which this entity belongs.',
  197. 'type' => 'varchar',
  198. 'length' => 128,
  199. 'not null' => TRUE,
  200. ),
  201. 'status' => array(
  202. 'description' => 'Boolean indicating whether the entity should be in the index.',
  203. 'type' => 'int',
  204. 'not null' => TRUE,
  205. 'default' => 1,
  206. ),
  207. 'changed' => array(
  208. 'description' => 'The Unix timestamp when an entity was changed.',
  209. 'type' => 'int',
  210. 'not null' => TRUE,
  211. 'default' => 0,
  212. ),
  213. ),
  214. 'indexes' => array(
  215. 'bundle_changed' => array('bundle', 'changed'),
  216. ),
  217. 'primary key' => array('entity_id'),
  218. );
  219. if ($type == 'other') {
  220. // Need the entity type also in the pkey for multiple entities in one table.
  221. $schema[$table]['primary key'][] = 'entity_type';
  222. }
  223. }
  224. $schema['apachesolr_index_bundles'] = array(
  225. 'description' => 'Records what bundles we should be indexing for a given environment.',
  226. 'fields' => array(
  227. 'env_id' => array(
  228. 'description' => 'The name of the environment.',
  229. 'type' => 'varchar',
  230. 'length' => 64,
  231. 'not null' => TRUE,
  232. ),
  233. 'entity_type' => array(
  234. 'description' => 'The type of entity.',
  235. 'type' => 'varchar',
  236. 'length' => 32,
  237. 'not null' => TRUE,
  238. ),
  239. 'bundle' => array(
  240. 'description' => 'The bundle to index.',
  241. 'type' => 'varchar',
  242. 'length' => 128,
  243. 'not null' => TRUE,
  244. ),
  245. ),
  246. 'primary key' => array('env_id', 'entity_type', 'bundle'),
  247. );
  248. return $schema;
  249. }
  250. /**
  251. * Implements hook_uninstall().
  252. */
  253. function apachesolr_uninstall() {
  254. // Remove variables.
  255. variable_del('apachesolr_default_environment');
  256. variable_del('apachesolr_rows');
  257. variable_del('apachesolr_site_hash');
  258. variable_del('apachesolr_index_last');
  259. variable_del('apachesolr_search_mlt_blocks');
  260. variable_del('apachesolr_cron_limit');
  261. variable_del('apachesolr_exclude_nodeapi_types');
  262. variable_del('apachesolr_failure');
  263. variable_del('apachesolr_index_updated');
  264. variable_del('apachesolr_read_only');
  265. variable_del('apachesolr_set_nodeapi_messages');
  266. variable_del('apachesolr_last_optimize');
  267. variable_del('apachesolr_update_from_6303');
  268. // Remove blocks.
  269. db_delete('block')->condition('module', 'apachesolr')->execute();
  270. }
  271. /**
  272. * Add a table to track Solr servers.
  273. */
  274. function apachesolr_update_7000() {
  275. if (variable_get('apachesolr_update_from_6303', FALSE)) {
  276. return NULL;
  277. }
  278. $schema['apachesolr_server'] = array(
  279. 'description' => 'The Solr server table.',
  280. 'fields' => array(
  281. 'server_id' => array(
  282. 'description' => 'Unique identifier for the server',
  283. 'type' => 'varchar',
  284. 'length' => 64,
  285. 'not null' => TRUE,
  286. ),
  287. 'name' => array(
  288. 'description' => 'Human-readable name for the server',
  289. 'type' => 'varchar',
  290. 'length' => 255,
  291. 'not null' => TRUE,
  292. 'default' => ''
  293. ),
  294. 'scheme' => array(
  295. 'description' => 'Preferred scheme for the registered server',
  296. 'type' => 'varchar',
  297. 'length' => 10,
  298. 'not null' => TRUE,
  299. 'default' => 'http'
  300. ),
  301. 'host' => array(
  302. 'description' => 'Host name for the registered server',
  303. 'type' => 'varchar',
  304. 'length' => 255,
  305. 'not null' => TRUE,
  306. 'default' => ''
  307. ),
  308. 'port' => array(
  309. 'description' => 'Port number for the registered server',
  310. 'type' => 'int',
  311. 'not null' => TRUE,
  312. ),
  313. 'path' => array(
  314. 'description' => 'Path to the registered server',
  315. 'type' => 'varchar',
  316. 'length' => 255,
  317. 'not null' => TRUE,
  318. 'default' => ''
  319. ),
  320. 'service_class' => array(
  321. 'description' => 'Optional class name to use for connection',
  322. 'type' => 'varchar',
  323. 'length' => 255,
  324. 'not null' => TRUE,
  325. 'default' => ''
  326. ),
  327. ),
  328. 'primary key' => array('server_id'),
  329. );
  330. db_create_table('apachesolr_server', $schema['apachesolr_server']);
  331. // Insert into the table the current single server record.
  332. $host = variable_get('apachesolr_host', 'localhost');
  333. $port = variable_get('apachesolr_port', '8983');
  334. $path = variable_get('apachesolr_path', '/solr');
  335. db_insert('apachesolr_server')->fields(array('server_id' => 'solr', 'name' => 'Apache Solr server', 'host' => $host, 'port' => $port, 'path' => $path))->execute();
  336. variable_set('apachesolr_default_server', 'solr');
  337. variable_del('apachesolr_host');
  338. variable_del('apachesolr_port');
  339. variable_del('apachesolr_path');
  340. $value = variable_get('apachesolr_service_class', NULL);
  341. if (is_array($value)) {
  342. list($module, $filepath, $class) = $value;
  343. variable_set('apachesolr_service_class', $class);
  344. }
  345. variable_del('apachesolr_logging');
  346. }
  347. /**
  348. * Re-jigger the schema to use fewer, shorter keys.
  349. */
  350. function apachesolr_update_7001() {
  351. if (variable_get('apachesolr_update_from_6303', FALSE)) {
  352. return NULL;
  353. }
  354. if (db_field_exists('apachesolr_server', 'asid')) {
  355. // You installed the beta1 and need to be fixed up.
  356. db_drop_field('apachesolr_server', 'asid');
  357. db_drop_unique_key('apachesolr_server', 'server_id');
  358. db_add_primary_key('apachesolr_server', array('server_id'));
  359. db_drop_unique_key('apachesolr_server', 'host_post_path');
  360. db_change_field('apachesolr_server', 'port', 'port',
  361. array(
  362. 'description' => 'Port number for the registered server',
  363. 'type' => 'int',
  364. 'not null' => TRUE,
  365. )
  366. );
  367. }
  368. }
  369. /**
  370. * Create the per-server variable table.
  371. */
  372. function apachesolr_update_7002() {
  373. if (variable_get('apachesolr_update_from_6303', FALSE)) {
  374. return NULL;
  375. }
  376. $schema['apachesolr_server_variable'] = array(
  377. 'description' => 'Variable values for each Solr server.',
  378. 'fields' => array(
  379. 'server_id' => array(
  380. 'description' => 'Unique identifier for the server',
  381. 'type' => 'varchar',
  382. 'length' => 64,
  383. 'not null' => TRUE,
  384. ),
  385. 'name' => array(
  386. 'description' => 'The name of the variable.',
  387. 'type' => 'varchar',
  388. 'length' => 128,
  389. 'not null' => TRUE,
  390. 'default' => '',
  391. ),
  392. 'value' => array(
  393. 'description' => 'The value of the variable.',
  394. 'type' => 'blob',
  395. 'not null' => TRUE,
  396. 'size' => 'big',
  397. ),
  398. ),
  399. 'primary key' => array('server_id', 'name'),
  400. );
  401. db_create_table('apachesolr_server_variable', $schema['apachesolr_server_variable']);
  402. $server_id = variable_get('apachesolr_default_server', 'solr');
  403. // Variables to be migrated:
  404. $conf['apachesolr_enabled_facets'] = variable_get('apachesolr_enabled_facets', NULL);
  405. $conf['apachesolr_search_query_fields'] = variable_get('apachesolr_search_query_fields', NULL);
  406. $conf['apachesolr_search_type_boosts'] = variable_get('apachesolr_search_type_boosts', NULL);
  407. $conf['apachesolr_search_comment_boost'] = variable_get('apachesolr_search_comment_boost', NULL);
  408. $conf['apachesolr_search_changed_boost'] = variable_get('apachesolr_search_changed_boost', NULL);
  409. $conf['apachesolr_search_sticky_boost'] = variable_get('apachesolr_search_sticky_boost', NULL);
  410. $conf['apachesolr_search_promote_boost'] = variable_get('apachesolr_search_promote_boost', NULL);
  411. $conf['apachesolr_search_excluded_types'] = variable_get('apachesolr_search_excluded_types', NULL);
  412. foreach ($conf as $name => $value) {
  413. if ($value !== NULL) {
  414. db_merge('apachesolr_server_variable')
  415. ->key(array('server_id' => $server_id, 'name' => $name))
  416. ->fields(array('value' => serialize($value)))
  417. ->execute();
  418. }
  419. variable_del($name);
  420. }
  421. }
  422. /**
  423. * Move excluded comment types into a new variable.
  424. */
  425. function apachesolr_update_7003() {
  426. if (variable_get('apachesolr_update_from_6303', FALSE)) {
  427. return NULL;
  428. }
  429. // Same as apachesolr_update_6006()
  430. $exclude_comment_types = variable_get('apachesolr_exclude_comments_types', NULL);
  431. if (is_array($exclude_comment_types)) {
  432. $exclude = array();
  433. foreach ($exclude_comment_types as $type) {
  434. $exclude[$type]['comment'] = TRUE;
  435. }
  436. variable_set('apachesolr_exclude_nodeapi_types', $exclude);
  437. }
  438. variable_del('apachesolr_exclude_comments_types');
  439. }
  440. /**
  441. * Update apachesolr_failure variable.
  442. */
  443. function apachesolr_update_7004() {
  444. if (variable_get('apachesolr_update_from_6303', FALSE)) {
  445. return NULL;
  446. }
  447. $failure = variable_get('apachesolr_failure', NULL);
  448. switch ($failure) {
  449. case 'show_error':
  450. variable_set('apachesolr_failure', 'apachesolr:show_error');
  451. break;
  452. case 'show_drupal_results':
  453. variable_set('apachesolr_failure', 'node');
  454. break;
  455. case 'show_no_results':
  456. variable_set('apachesolr_failure', 'apachesolr:show_no_results');
  457. break;
  458. }
  459. }
  460. /**
  461. * Re-jigger the schema to use just a url column.
  462. */
  463. function apachesolr_update_7005() {
  464. if (variable_get('apachesolr_update_from_6303', FALSE)) {
  465. return NULL;
  466. }
  467. if (db_field_exists('apachesolr_server', 'port')) {
  468. // You installed the beta3 and need to be fixed up.
  469. $servers = db_query('SELECT * FROM {apachesolr_server}')->fetchAllAssoc('server_id', PDO::FETCH_ASSOC);
  470. db_drop_field('apachesolr_server', 'scheme');
  471. db_drop_field('apachesolr_server', 'port');
  472. db_drop_field('apachesolr_server', 'path');
  473. db_change_field('apachesolr_server', 'host', 'url',
  474. array(
  475. 'description' => 'Full url for the server',
  476. 'type' => 'varchar',
  477. 'length' => 1000,
  478. 'not null' => TRUE,
  479. )
  480. );
  481. foreach ($servers as $id => $server) {
  482. $port = $server['port'] ? ':' . $server['port'] : '';
  483. $url = $server['scheme'] . '://' . $server['host'] . $port . $server['path'];
  484. db_update('apachesolr_server')
  485. ->fields(array('url' => $url))
  486. ->condition('server_id', $id)
  487. ->execute();
  488. }
  489. }
  490. }
  491. /**
  492. * Remove facet-related variable deprecated by the Facet API integration.
  493. */
  494. function apachesolr_update_7006() {
  495. if (variable_get('apachesolr_update_from_6303', FALSE)) {
  496. return NULL;
  497. }
  498. variable_del('apachesolr_facetstyle');
  499. variable_del('apachesolr_facet_show_children');
  500. variable_del('apachesolr_facet_query_limits');
  501. variable_del('apachesolr_facet_query_limit_default');
  502. }
  503. /**
  504. * Rename tables to make them more generic.
  505. */
  506. function apachesolr_update_7007() {
  507. if (variable_get('apachesolr_update_from_6303', FALSE)) {
  508. return NULL;
  509. }
  510. db_drop_primary_key('apachesolr_server');
  511. db_drop_primary_key('apachesolr_server_variable');
  512. db_rename_table('apachesolr_server', 'apachesolr_environment');
  513. db_rename_table('apachesolr_server_variable', 'apachesolr_environment_variable');
  514. db_change_field('apachesolr_environment', 'server_id', 'env_id', array(
  515. 'description' => 'Unique identifier for the environment',
  516. 'type' => 'varchar',
  517. 'length' => 64,
  518. 'not null' => TRUE)
  519. );
  520. db_change_field('apachesolr_environment_variable', 'server_id', 'env_id', array(
  521. 'description' => 'Unique identifier for the environment',
  522. 'type' => 'varchar',
  523. 'length' => 64,
  524. 'not null' => TRUE)
  525. );
  526. db_add_primary_key('apachesolr_environment', array('env_id'));
  527. db_add_primary_key('apachesolr_environment_variable', array('env_id', 'name'));
  528. $id = variable_get('apachesolr_default_server', NULL);
  529. if (isset($id)) {
  530. variable_set('apachesolr_default_environment', $id);
  531. }
  532. variable_del('apachesolr_default_server');
  533. }
  534. /**
  535. * Remove more facet-related variable deprecated by the Facet API integration.
  536. */
  537. function apachesolr_update_7008() {
  538. if (variable_get('apachesolr_update_from_6303', FALSE)) {
  539. return NULL;
  540. }
  541. variable_del('apachesolr_facet_missing');
  542. variable_del('apachesolr_facet_query_initial_limits');
  543. variable_del('apachesolr_facet_query_sorts');
  544. variable_del('apachesolr_facet_sort_active');
  545. variable_del('apachesolr_operator');
  546. }
  547. /**
  548. * Update Facet API block deltas to account for removal of numeric ID from field names.
  549. */
  550. function apachesolr_update_7009() {
  551. if (variable_get('apachesolr_update_from_6303', FALSE)) {
  552. return NULL;
  553. }
  554. // Only run when facetapi is available and/or installed
  555. if (module_exists('facetapi')) {
  556. module_load_include('inc', 'facetapi', 'facetapi.block');
  557. // Get all searchers
  558. $searchers = facetapi_get_searcher_info();
  559. $realms = facetapi_get_realm_info();
  560. foreach ($searchers as $searcher_id => $searcher) {
  561. foreach ($realms as $realm_id => $realm) {
  562. foreach (field_info_fields() as $field_name => $field) {
  563. // Generate the old delta
  564. $facet_name_old = $field['id'] . '_' . $field['field_name'];
  565. $delta_old = facetapi_build_delta($searcher['name'], $realm['name'], $facet_name_old);
  566. $delta_old = substr(drupal_hash_base64($delta_old), 0, 32);
  567. // Generate the new delta
  568. $facet_name = $field['field_name'];
  569. $delta = facetapi_build_delta($searcher['name'], $realm['name'], $facet_name);
  570. $delta = substr(drupal_hash_base64($delta), 0, 32);
  571. db_update('block')
  572. ->fields(array('delta' => $delta))
  573. ->condition('module', 'facetapi')
  574. ->condition('delta', $delta_old)
  575. ->execute();
  576. }
  577. }
  578. }
  579. }
  580. }
  581. /**
  582. * Update cache table schema for Drupal 7.
  583. */
  584. function apachesolr_update_7010() {
  585. if (variable_get('apachesolr_update_from_6303', FALSE)) {
  586. return NULL;
  587. }
  588. db_drop_field('cache_apachesolr', 'headers');
  589. return 'Updated cache table schema for Drupal 7.';
  590. }
  591. /**
  592. * Change the namespace for the indexer from apachesolr_search to apachesolr
  593. */
  594. function apachesolr_update_7011() {
  595. if (variable_get('apachesolr_update_from_6303', FALSE)) {
  596. return NULL;
  597. }
  598. $stored = variable_get('apachesolr_index_last', array());
  599. if (isset($stored['apachesolr_search'])) {
  600. $stored['apachesolr'] = $stored['apachesolr_search'];
  601. unset($stored['apachesolr_search']);
  602. variable_set('apachesolr_index_last', $stored);
  603. }
  604. return 'Updated the namespace variable for the index process.';
  605. }
  606. /**
  607. * Rename some variables and update the database tables
  608. */
  609. function apachesolr_update_7012() {
  610. if (variable_get('apachesolr_update_from_6303', FALSE)) {
  611. return NULL;
  612. }
  613. // @see: drupal_load()
  614. if (!function_exists('apachesolr_default_environment')) {
  615. include_once dirname(__FILE__) . '/apachesolr.module';
  616. }
  617. $env_id = apachesolr_default_environment();
  618. // Variable changed from integer to array with environment integers
  619. $stored = variable_get('apachesolr_index_last', array());
  620. if (isset($stored['apachesolr'])) {
  621. $stored[$env_id]['node']['last_entity_id'] = $stored['apachesolr']['last_nid'];
  622. $stored[$env_id]['node']['last_changed'] = $stored['apachesolr']['last_change'];
  623. unset($stored['apachesolr']);
  624. variable_set('apachesolr_index_last', $stored);
  625. }
  626. $last = variable_get('apachesolr_index_updated', NULL);
  627. if (isset($last)) {
  628. variable_set('apachesolr_index_updated', array($env_id => (int) $last));
  629. }
  630. // Change namespace to environment id
  631. $excluded_types = apachesolr_environment_variable_get('apachesolr', 'apachesolr_search_excluded_types', array());
  632. if (!empty($excluded_types)) {
  633. apachesolr_environment_variable_set($env_id, 'apachesolr_search_excluded_types', $excluded_types);
  634. apachesolr_environment_variable_del('apachesolr', 'apachesolr_search_excluded_types');
  635. }
  636. // Install the new schema
  637. //Predefine an amount of types that get their own table
  638. $types = array(
  639. 'other' => 'apachesolr_index_entities',
  640. 'node' => 'apachesolr_index_entities_node',
  641. );
  642. foreach ($types as $type => $table) {
  643. $schema[$table] = array(
  644. 'description' => 'Stores a record of when an entity changed to determine if it needs indexing by Solr.',
  645. 'fields' => array(
  646. 'entity_type' => array(
  647. 'description' => 'The type of entity.',
  648. 'type' => 'varchar',
  649. 'length' => 32,
  650. 'not null' => TRUE,
  651. ),
  652. 'entity_id' => array(
  653. 'description' => 'The primary identifier for an entity.',
  654. 'type' => 'int',
  655. 'unsigned' => TRUE,
  656. 'not null' => TRUE,
  657. ),
  658. 'bundle' => array(
  659. 'description' => 'The bundle to which this entity belongs.',
  660. 'type' => 'varchar',
  661. 'length' => 128,
  662. 'not null' => TRUE,
  663. ),
  664. 'status' => array(
  665. 'description' => 'Boolean indicating whether the entity is visible to non-administrators (eg, published for nodes).',
  666. 'type' => 'int',
  667. 'not null' => TRUE,
  668. 'default' => 1,
  669. ),
  670. 'changed' => array(
  671. 'description' => 'The Unix timestamp when an entity was changed.',
  672. 'type' => 'int',
  673. 'not null' => TRUE,
  674. 'default' => 0,
  675. ),
  676. ),
  677. 'indexes' => array(
  678. 'changed' => array('bundle', 'status', 'changed'),
  679. ),
  680. 'primary key' => array('entity_id'),
  681. );
  682. if ($type == 'other') {
  683. // Need the entity type also in the pkey for multiple entities in one table.
  684. $schema[$table]['primary key'][] = 'entity_type';
  685. }
  686. // Create the table
  687. db_create_table($table, $schema[$table]);
  688. }
  689. $schema['apachesolr_index_bundles'] = array(
  690. 'description' => 'Records what bundles we should be indexing for a given environment.',
  691. 'fields' => array(
  692. 'env_id' => array(
  693. 'description' => 'The name of the environment.',
  694. 'type' => 'varchar',
  695. 'length' => 64,
  696. 'not null' => TRUE,
  697. ),
  698. 'entity_type' => array(
  699. 'description' => 'The type of entity.',
  700. 'type' => 'varchar',
  701. 'length' => 32,
  702. 'not null' => TRUE,
  703. ),
  704. 'bundle' => array(
  705. 'description' => 'The bundle to index.',
  706. 'type' => 'varchar',
  707. 'length' => 128,
  708. 'not null' => TRUE,
  709. ),
  710. ),
  711. 'primary key' => array('env_id', 'entity_type', 'bundle'),
  712. );
  713. db_create_table('apachesolr_index_bundles', $schema['apachesolr_index_bundles']);
  714. // Move the data from apachesolr_search_node to apachesolr_index_entities_node
  715. $select = db_select('apachesolr_search_node', 'asn');
  716. $select->join('node', 'n', 'asn.nid = n.nid');
  717. $select->addField('n', 'nid', 'entity_id');
  718. $select->addField('n', 'type', 'bundle');
  719. $select->addField('asn', 'status', 'status');
  720. $select->addField('asn', 'changed', 'changed');
  721. $select->addExpression("'node'", 'entity_type');
  722. $return_value = db_insert('apachesolr_index_entities_node')
  723. ->fields(array('entity_id', 'bundle', 'status', 'changed', 'entity_type'))
  724. ->from($select)
  725. ->execute();
  726. // Drop the table apachesolr_search_node
  727. db_drop_table('apachesolr_search_node');
  728. $environments = apachesolr_load_all_environments();
  729. foreach ($environments as $env_id => $environment) {
  730. $excluded_types = apachesolr_environment_variable_get($env_id, 'apachesolr_search_excluded_types', array());
  731. // Get indexable entity types
  732. $options = array();
  733. foreach (entity_get_info() as $entity_type => $entity_info) {
  734. if ($entity_type == 'node') {
  735. foreach ($entity_info['bundles'] as $key => $info) {
  736. // See if it was excluded & only of entity node. We will not enable
  737. // other entity types by default
  738. if (empty($excluded_types[$key])) {
  739. $options[$entity_type][$key] = $key;
  740. }
  741. }
  742. }
  743. }
  744. // Set all except the excluded types
  745. // @see apachesolr_index_set_bundles()
  746. foreach ($options as $entity_type => $bundles) {
  747. db_delete('apachesolr_index_bundles')
  748. ->condition('env_id', $env_id)
  749. ->condition('entity_type', $entity_type)
  750. ->execute();
  751. if ($bundles) {
  752. $insert = db_insert('apachesolr_index_bundles')
  753. ->fields(array('env_id', 'entity_type', 'bundle'));
  754. foreach ($bundles as $bundle) {
  755. $insert->values(array(
  756. 'env_id' => $env_id,
  757. 'entity_type' => $entity_type,
  758. 'bundle' => $bundle,
  759. ));
  760. }
  761. $insert->execute();
  762. }
  763. }
  764. // Remove the excluded types
  765. apachesolr_environment_variable_del($env_id, 'apachesolr_search_excluded_types');
  766. }
  767. }
  768. /**
  769. * Make consistent (and reduce) field lengths which cause excess pkey length.
  770. */
  771. function apachesolr_update_7013() {
  772. if (variable_get('apachesolr_update_from_6303', FALSE)) {
  773. return NULL;
  774. }
  775. db_drop_primary_key('apachesolr_index_entities');
  776. db_change_field('apachesolr_index_entities', 'entity_type', 'entity_type', array(
  777. 'description' => 'The type of entity.',
  778. 'type' => 'varchar',
  779. 'length' => 32,
  780. 'not null' => TRUE)
  781. );
  782. db_add_primary_key('apachesolr_index_entities', array('entity_id', 'entity_type'));
  783. db_change_field('apachesolr_index_entities_node', 'entity_type', 'entity_type', array(
  784. 'description' => 'The type of entity.',
  785. 'type' => 'varchar',
  786. 'length' => 32,
  787. 'not null' => TRUE)
  788. );
  789. db_drop_primary_key('apachesolr_index_bundles');
  790. db_change_field('apachesolr_index_bundles', 'env_id', 'env_id', array(
  791. 'description' => 'Unique identifier for the environment',
  792. 'type' => 'varchar',
  793. 'length' => 64,
  794. 'not null' => TRUE)
  795. );
  796. db_change_field('apachesolr_index_bundles', 'entity_type', 'entity_type', array(
  797. 'description' => 'The type of entity.',
  798. 'type' => 'varchar',
  799. 'length' => 32,
  800. 'not null' => TRUE)
  801. );
  802. db_add_primary_key('apachesolr_index_bundles', array('env_id', 'entity_type', 'bundle'));
  803. }
  804. /**
  805. * Remove status from the key.
  806. */
  807. function apachesolr_update_7014() {
  808. if (variable_get('apachesolr_update_from_6303', FALSE)) {
  809. return NULL;
  810. }
  811. $types = array(
  812. 'other' => 'apachesolr_index_entities',
  813. 'node' => 'apachesolr_index_entities_node',
  814. );
  815. foreach ($types as $type => $table) {
  816. db_drop_index($table, 'changed');
  817. db_add_index($table, 'bundle_changed', array('bundle', 'changed'));
  818. }
  819. }
  820. /**
  821. * Fix primary key schema mismatch for those who cleanly installed with beta16.
  822. */
  823. function apachesolr_update_7015() {
  824. if (variable_get('apachesolr_update_from_6303', FALSE)) {
  825. return NULL;
  826. }
  827. // Brand new installations since update_7013 have the wrong primary key.
  828. db_drop_primary_key('apachesolr_index_entities');
  829. db_add_primary_key('apachesolr_index_entities', array('entity_id', 'entity_type'));
  830. }
  831. /**
  832. * Clean up apachesolr_update_from_6303.
  833. *
  834. * This variable had been used to bypass 7.x-1.x updates which are redundant
  835. * with 6.x-3.x.
  836. */
  837. function apachesolr_update_7016() {
  838. variable_del('apachesolr_update_from_6303');
  839. }
  840. /**
  841. * Turn global variables in environment specific ones.
  842. */
  843. function apachesolr_update_7017() {
  844. // @see: drupal_load()
  845. if (!function_exists('apachesolr_environment_variable_set')) {
  846. include_once dirname(__FILE__) . '/apachesolr.module';
  847. }
  848. $stored = variable_get('apachesolr_index_last', array());
  849. foreach ($stored as $env_id => $entity_info) {
  850. apachesolr_environment_variable_set($env_id, 'apachesolr_index_last', $entity_info);
  851. }
  852. variable_del('apachesolr_index_last');
  853. $updated = variable_get('apachesolr_index_updated', array());
  854. $optimized = variable_get('apachesolr_last_optimize', 0);
  855. foreach ($updated as $env_id => $timestamp) {
  856. apachesolr_environment_variable_set($env_id, 'apachesolr_index_updated', $timestamp);
  857. apachesolr_environment_variable_set($env_id, 'apachesolr_last_optimize', $optimized);
  858. }
  859. }