synonyms_search.pages.inc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. if ($v['type'] == 'taxonomy_term_reference' && isset($v['bundles']['node'])) {
  22. // This field is taxonomy term reference and it is attached to nodes, so
  23. // we will run EntityFieldQuery on it.
  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. }
  33. if (!empty($found_nids)) {
  34. db_update('search_dataset')
  35. ->fields(array('reindex' => REQUEST_TIME))
  36. ->condition('type', 'node')
  37. ->condition('sid', $found_nids, 'IN')
  38. ->execute();
  39. }
  40. // Integration with Term Search module: reset terms index too.
  41. if (module_exists('term_search')) {
  42. db_update('search_dataset')
  43. ->fields(array('reindex' => REQUEST_TIME))
  44. ->condition('type', 'term')
  45. ->condition('sid', $tids)
  46. ->execute();
  47. }
  48. }
  49. /**
  50. * Mark nodes that reference terms from a vocabulary for search re-indexing.
  51. *
  52. * @param $vocabulary object
  53. * Fully loaded vocabulary object
  54. */
  55. function synonyms_search_reindex_nodes_by_vocabulary($vocabulary) {
  56. $tids = db_select('taxonomy_term_data', 't')
  57. ->fields('t', array('tid'))
  58. ->condition('vid', $vocabulary->vid)
  59. ->execute()
  60. ->fetchCol();
  61. if (!empty($tids)) {
  62. synonyms_search_reindex_nodes_by_terms($tids);
  63. }
  64. }