synonyms_search.module 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @file
  4. * Provides synonyms integration with searching.
  5. */
  6. /**
  7. * Implements hook_ctools_plugin_directory().
  8. */
  9. function synonyms_search_ctools_plugin_directory($owner, $plugin_type) {
  10. switch ($owner) {
  11. case 'synonyms':
  12. switch ($plugin_type) {
  13. case 'behavior':
  14. return 'plugins/' . $plugin_type;
  15. }
  16. break;
  17. }
  18. }
  19. /**
  20. * Implements hook_taxonomy_term_update().
  21. */
  22. function synonyms_search_taxonomy_term_update($term) {
  23. // If we notice at least some change in synonyms of this term, we want to
  24. // trigger search re-indexing of nodes, where this term is referenced, since
  25. // change in term synonyms affects nodes ranking in the search.
  26. if (isset($term->original)) {
  27. $bundle = field_extract_bundle('taxonomy_term', $term);
  28. $behavior_implementations = synonyms_behavior_get('search', 'taxonomy_term', $bundle, TRUE);
  29. $current_search_synonyms = array();
  30. $previous_search_synonyms = array();
  31. foreach ($behavior_implementations as $behavior_implementation) {
  32. $current_search_synonyms = array_merge($current_search_synonyms, synonyms_extract_synonyms($term, $behavior_implementation));
  33. $previous_search_synonyms = array_merge($previous_search_synonyms, synonyms_extract_synonyms($term->original, $behavior_implementation));
  34. }
  35. $diff = array_diff($current_search_synonyms, $previous_search_synonyms);
  36. if (!empty($diff) || count($current_search_synonyms) != count($previous_search_synonyms)) {
  37. module_load_include('inc', 'synonyms_search', 'synonyms_search.pages');
  38. synonyms_search_reindex_nodes_by_terms(array($term->tid));
  39. }
  40. }
  41. }
  42. /**
  43. * Implements hook_node_update_index().
  44. */
  45. function synonyms_search_node_update_index($node) {
  46. $output = array();
  47. foreach (field_info_instances('node', $node->type) as $instance) {
  48. // We go a field by field looking for taxonomy term reference and if that
  49. // vocabulary has enabled search synonyms, we add them to the search index.
  50. $field_info = field_info_field($instance['field_name']);
  51. if ($field_info['type'] == 'taxonomy_term_reference') {
  52. // For each term referenced in this node we have to add synonyms.
  53. $terms = field_get_items('node', $node, $instance['field_name']);
  54. if (is_array($terms) && !empty($terms)) {
  55. foreach ($terms as $v) {
  56. $output[] = $v['tid'];
  57. }
  58. }
  59. }
  60. }
  61. if (!empty($output)) {
  62. $terms = taxonomy_term_load_multiple($output);
  63. $output = array();
  64. foreach ($terms as $term) {
  65. $bundle = field_extract_bundle('taxonomy_term', $term);
  66. $behavior_implementations = synonyms_behavior_get('search', 'taxonomy_term', $bundle, TRUE);
  67. foreach ($behavior_implementations as $implementation) {
  68. $output = array_merge($output, synonyms_extract_synonyms($term, $implementation));
  69. }
  70. }
  71. }
  72. return empty($output) ? '' : '<strong>' . implode($output, ', ') . '</strong>';
  73. }
  74. /**
  75. * Implements hook_term_update_index().
  76. */
  77. function synonyms_search_term_update_index($term) {
  78. $bundle = taxonomy_vocabulary_load($term->vid);
  79. $bundle = field_extract_bundle('taxonomy_term', $bundle);
  80. $behavior_implementations = synonyms_behavior_get('search', 'taxonomy_term', $bundle, TRUE);
  81. $synonyms = array();
  82. foreach ($behavior_implementations as $implementation) {
  83. $synonyms = array_merge($synonyms, synonyms_extract_synonyms($term, $implementation));
  84. }
  85. return implode(', ', $synonyms);
  86. }
  87. /**
  88. * Implements hook_synonyms_behavior_implementation_info().
  89. */
  90. function synonyms_search_synonyms_behavior_implementation_info($behavior) {
  91. switch ($behavior) {
  92. case 'search':
  93. return array(
  94. 'number_integer' => 'SearchTextSynonymsBehavior',
  95. 'number_decimal' => 'SearchTextSynonymsBehavior',
  96. 'number_float' => 'SearchTextSynonymsBehavior',
  97. 'text' => 'SearchTextSynonymsBehavior',
  98. 'taxonomy_term_reference' => 'SearchTaxonomySynonymsBehavior',
  99. 'entityreference' => 'SearchEntityReferenceSynonymsBehavior',
  100. );
  101. break;
  102. }
  103. return array();
  104. }