ChamiloIndexer.class.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.include.search
  5. */
  6. require_once __DIR__.'/../../global.inc.php';
  7. /**
  8. * Class wrapper.
  9. *
  10. * @package chamilo.include.search
  11. */
  12. class ChamiloIndexer extends XapianIndexer
  13. {
  14. /**
  15. * Set terms on search_did given.
  16. *
  17. * @param string $terms_string Comma-separated list of terms from input form
  18. * @param string $prefix Search engine prefix
  19. * @param string $course_code Course code
  20. * @param string $tool_id Tool id from mainapi.lib.php
  21. * @param int $ref_id_high_level Main id of the entity to index (Ex. lp_id)
  22. * @param int $ref_id_second_level Secondary id of the entity to index (Ex. lp_item)
  23. * @param int $search_did Search engine document id from search_engine_ref table
  24. *
  25. * @return bool False on error or nothing to do, true otherwise
  26. */
  27. public function set_terms(
  28. $terms_string,
  29. $prefix,
  30. $course_code,
  31. $tool_id,
  32. $ref_id_high_level,
  33. $ref_id_second_level,
  34. $search_did
  35. ) {
  36. $terms_string = trim($terms_string);
  37. $terms = explode(',', $terms_string);
  38. array_walk($terms, 'trim_value');
  39. $stored_terms = $this->get_terms_on_db($prefix, $course_code, $tool_id, $ref_id_high_level);
  40. // don't do anything if no change, verify only at DB, not the search engine
  41. if ((count(array_diff($terms, $stored_terms)) == 0) &&
  42. (count(array_diff($stored_terms, $terms)) == 0)
  43. ) {
  44. return false;
  45. }
  46. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  47. // compare terms
  48. $doc = $this->get_document($search_did);
  49. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  50. $xterms = [];
  51. foreach ($xapian_terms as $xapian_term) {
  52. $xterms[] = substr($xapian_term['name'], 1);
  53. }
  54. $dterms = $terms;
  55. $missing_terms = array_diff($dterms, $xterms);
  56. $deprecated_terms = array_diff($xterms, $dterms);
  57. // save it to search engine
  58. foreach ($missing_terms as $term) {
  59. $this->add_term_to_doc($prefix.$term, $doc);
  60. }
  61. foreach ($deprecated_terms as $term) {
  62. $this->remove_term_from_doc($prefix.$term, $doc);
  63. }
  64. // don't do anything if no change
  65. if ((count($missing_terms) > 0) || (count($deprecated_terms) > 0)) {
  66. $this->replace_document($doc, (int) $search_did);
  67. }
  68. return true;
  69. }
  70. /**
  71. * Get the terms stored at database.
  72. *
  73. * @return array Array of terms
  74. */
  75. public function get_terms_on_db($prefix, $course_code, $tool_id, $ref_id)
  76. {
  77. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  78. $terms = get_specific_field_values_list_by_prefix(
  79. $prefix,
  80. $course_code,
  81. $tool_id,
  82. $ref_id
  83. );
  84. $prefix_terms = [];
  85. foreach ($terms as $term) {
  86. $prefix_terms[] = $term['value'];
  87. }
  88. return $prefix_terms;
  89. }
  90. }