synonyms_search.pages.inc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * @file
  4. * Supportive functions for the module.
  5. */
  6. /**
  7. * Mark nodes that reference specific terms for search re-indexing.
  8. *
  9. * This is particularly useful, when the terms in question have been changed.
  10. *
  11. * @param $tids array
  12. * Array of tids of the terms
  13. */
  14. function synonyms_search_reindex_nodes_by_terms($tids) {
  15. // In order to speed up the process, we will query DB for nid's that reference
  16. // handled to us tids, and at the end we'll trigger their re-indexing just in
  17. // a single SQL query. Probably it is better to use search_touch_node(), but
  18. // that would imply a big amount of SQL queries on some websites.
  19. $found_nids = array();
  20. foreach (field_info_field_map() as $field_name => $v) {
  21. // TODO: explore possibility of using foreign keys instead of hard coding
  22. // fields types in here.
  23. if ($v['type'] == 'taxonomy_term_reference' && isset($v['bundles']['node'])) {
  24. $query = new EntityFieldQuery();
  25. $result = $query->entityCondition('entity_type', 'node')
  26. ->fieldCondition($field_name, 'tid', $tids, 'IN')
  27. ->execute();
  28. if (isset($result['node'])) {
  29. $found_nids = array_merge($found_nids, array_keys($result['node']));
  30. }
  31. }
  32. if ($v['type'] == 'entityreference' && isset($v['bundles']['node'])) {
  33. $field = field_info_field($field_name);
  34. if ($field['settings']['target_type'] == 'taxonomy_term') {
  35. $query = new EntityFieldQuery();
  36. $result = $query->entityCondition('entity_type', 'node')
  37. ->fieldCondition($field_name, 'target_id', $tids, 'IN')
  38. ->execute();
  39. if (isset($result['node'])) {
  40. $found_nids = array_merge($found_nids, array_keys($result['node']));
  41. }
  42. }
  43. }
  44. }
  45. if (!empty($found_nids)) {
  46. db_update('search_dataset')
  47. ->fields(array('reindex' => REQUEST_TIME))
  48. ->condition('type', 'node')
  49. ->condition('sid', $found_nids, 'IN')
  50. ->execute();
  51. }
  52. // Integration with Term Search module: reset terms index too.
  53. if (module_exists('term_search')) {
  54. db_update('search_dataset')
  55. ->fields(array('reindex' => REQUEST_TIME))
  56. ->condition('type', 'term')
  57. ->condition('sid', $tids)
  58. ->execute();
  59. }
  60. }
  61. /**
  62. * Mark nodes that reference terms from a vocabulary for search re-indexing.
  63. *
  64. * @param $vocabulary object
  65. * Fully loaded vocabulary object
  66. */
  67. function synonyms_search_reindex_nodes_by_vocabulary($vocabulary) {
  68. $tids = db_select('taxonomy_term_data', 't')
  69. ->fields('t', array('tid'))
  70. ->condition('vid', $vocabulary->vid)
  71. ->execute()
  72. ->fetchCol();
  73. if (!empty($tids)) {
  74. synonyms_search_reindex_nodes_by_terms($tids);
  75. }
  76. }