ChamiloIndexer.class.php 3.1 KB

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