IndexableChunk.class.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.include.search
  5. */
  6. // some constants to avoid serialize string keys on serialized data array
  7. define('SE_COURSE_ID', 0);
  8. define('SE_TOOL_ID', 1);
  9. define('SE_DATA', 2);
  10. define('SE_USER', 3);
  11. // in some cases we need top differenciate xapian documents of the same tool
  12. define('SE_DOCTYPE_EXERCISE_EXERCISE', 0);
  13. define('SE_DOCTYPE_EXERCISE_QUESTION', 1);
  14. // xapian prefixes
  15. define('XAPIAN_PREFIX_COURSEID', 'C');
  16. define('XAPIAN_PREFIX_TOOLID', 'O');
  17. /**
  18. * Class.
  19. *
  20. * @package chamilo.include.search
  21. */
  22. abstract class _IndexableChunk
  23. {
  24. /* struct (array)
  25. * {
  26. * string title; <- nombre de archivo/elemento
  27. * string content; <- texto a indexar
  28. * string ids; <- los flags a guardar "cidReq:lp_id:path"
  29. * }
  30. */
  31. public $data;
  32. /**
  33. * array (
  34. * 'SE_COURSE_ID' => string <- course id from course table on main db
  35. * 'SE_TOOL_ID' => string <- tool id from mainapi lib constants
  36. * 'SE_DATA' => mixed <- extra information, depends on SE_TOOL_ID
  37. * 'SE_USER' => id <- user id from user table in main db
  38. * ).
  39. */
  40. public $xapian_data;
  41. /**
  42. * array(
  43. * name => string
  44. * flag => char
  45. * ).
  46. */
  47. public $terms;
  48. /**
  49. * Class constructor. Just generates an empty 'data' array attribute.
  50. */
  51. public function __construct()
  52. {
  53. $this->data = [];
  54. }
  55. /**
  56. * Class desctructor. Unsets attributes.
  57. */
  58. public function __destruct()
  59. {
  60. unset($this->data);
  61. unset($this->terms);
  62. }
  63. /**
  64. * Add a value to the indexed item.
  65. *
  66. * @param string Key
  67. * @param string Value
  68. */
  69. public function addValue($key, $value)
  70. {
  71. $this->data[$key] = $value;
  72. }
  73. /**
  74. * Add a term (like xapian definition).
  75. *
  76. * @param string Term
  77. * @param string Flag (one character)
  78. */
  79. public function addTerm($term, $flag)
  80. {
  81. global $charset;
  82. if (strlen($flag) == 1) {
  83. $this->terms[] = ['name' => api_convert_encoding(stripslashes($term), 'UTF-8', $charset), 'flag' => $flag];
  84. }
  85. }
  86. }
  87. /**
  88. * Extension of the _IndexableChunk class to make IndexableChunk extensible.
  89. *
  90. * @package chamilo.include.search
  91. */
  92. class IndexableChunk extends _IndexableChunk
  93. {
  94. /**
  95. * Let add course id term.
  96. */
  97. public function addCourseId($course_id)
  98. {
  99. $this->addTerm($course_id, XAPIAN_PREFIX_COURSEID);
  100. }
  101. /**
  102. * Let add tool id term.
  103. */
  104. public function addToolId($tool_id)
  105. {
  106. $this->addTerm($tool_id, XAPIAN_PREFIX_TOOLID);
  107. }
  108. }