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. include_once 'xapian/XapianIndexer.class.php';
  8. /**
  9. * Class wrapper
  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. * @return boolean False on error or nothing to do, true otherwise
  25. */
  26. function set_terms(
  27. $terms_string,
  28. $prefix,
  29. $course_code,
  30. $tool_id,
  31. $ref_id_high_level,
  32. $ref_id_second_level,
  33. $search_did
  34. ) {
  35. $terms_string = trim($terms_string);
  36. $terms = explode(',', $terms_string);
  37. array_walk($terms, 'trim_value');
  38. $stored_terms = $this->get_terms_on_db($prefix, $course_code, $tool_id, $ref_id_high_level);
  39. // don't do anything if no change, verify only at DB, not the search engine
  40. if ((count(array_diff($terms, $stored_terms)) == 0) &&
  41. (count(array_diff($stored_terms, $terms)) == 0)
  42. ) {
  43. return false;
  44. }
  45. require_once api_get_path(LIBRARY_PATH).'search/xapian/XapianQuery.php';
  46. // compare terms
  47. $doc = $this->get_document($search_did);
  48. $xapian_terms = xapian_get_doc_terms($doc, $prefix);
  49. $xterms = array();
  50. foreach ($xapian_terms as $xapian_term) {
  51. $xterms[] = substr($xapian_term['name'], 1);
  52. }
  53. $dterms = $terms;
  54. $missing_terms = array_diff($dterms, $xterms);
  55. $deprecated_terms = array_diff($xterms, $dterms);
  56. // save it to search engine
  57. foreach ($missing_terms as $term) {
  58. $this->add_term_to_doc($prefix.$term, $doc);
  59. }
  60. foreach ($deprecated_terms as $term) {
  61. $this->remove_term_from_doc($prefix.$term, $doc);
  62. }
  63. // don't do anything if no change
  64. if ((count($missing_terms) > 0) || (count($deprecated_terms) > 0)) {
  65. $this->replace_document($doc, (int) $search_did);
  66. }
  67. return true;
  68. }
  69. /**
  70. * Get the terms stored at database
  71. * @return array Array of terms
  72. */
  73. function get_terms_on_db($prefix, $course_code, $tool_id, $ref_id)
  74. {
  75. require_once api_get_path(LIBRARY_PATH).'specific_fields_manager.lib.php';
  76. $terms = get_specific_field_values_list_by_prefix($prefix, $course_code, $tool_id, $ref_id);
  77. $prefix_terms = array();
  78. foreach ($terms as $term) {
  79. $prefix_terms[] = $term['value'];
  80. }
  81. return $prefix_terms;
  82. }
  83. }
  84. if (!function_exists('trim_value')) {
  85. function trim_value(&$value)
  86. {
  87. $value = trim($value);
  88. }
  89. }