IndexableChunk.class.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. * @package chamilo.include.search
  20. */
  21. abstract class _IndexableChunk
  22. {
  23. /* struct (array)
  24. * {
  25. * string title; <- nombre de archivo/elemento
  26. * string content; <- texto a indexar
  27. * string ids; <- los flags a guardar "cidReq:lp_id:path"
  28. * }
  29. */
  30. public $data;
  31. /**
  32. * array (
  33. * 'SE_COURSE_ID' => string <- course id from course table on main db
  34. * 'SE_TOOL_ID' => string <- tool id from mainapi lib constants
  35. * 'SE_DATA' => mixed <- extra information, depends on SE_TOOL_ID
  36. * 'SE_USER' => id <- user id from user table in main db
  37. * )
  38. */
  39. public $xapian_data;
  40. /**
  41. * array(
  42. * name => string
  43. * flag => char
  44. * )
  45. */
  46. public $terms;
  47. /**
  48. * Add a value to the indexed item
  49. * @param string Key
  50. * @param string Value
  51. * @return void
  52. */
  53. function addValue($key, $value) {
  54. $this->data[$key] = $value;
  55. }
  56. /**
  57. * Add a term (like xapian definition)
  58. * @param string Term
  59. * @param string Flag (one character)
  60. */
  61. public function addTerm($term, $flag) {
  62. global $charset;
  63. if (strlen($flag) == 1) {
  64. $this->terms[] = array('name' => api_convert_encoding(stripslashes($term), 'UTF-8', $charset), 'flag' => $flag);
  65. }
  66. }
  67. /**
  68. * Class constructor. Just generates an empty 'data' array attribute
  69. */
  70. function __construct() {
  71. $this->data = array();
  72. }
  73. /**
  74. * Class desctructor. Unsets attributes.
  75. */
  76. function __destruct() {
  77. unset($this->data);
  78. unset($this->terms);
  79. }
  80. }
  81. /**
  82. * Extension of the _IndexableChunk class to make IndexableChunk extensible.
  83. * @package chamilo.include.search
  84. */
  85. class IndexableChunk extends _IndexableChunk
  86. {
  87. /**
  88. * Let add course id term
  89. */
  90. public function addCourseId($course_id)
  91. {
  92. $this->addTerm($course_id, XAPIAN_PREFIX_COURSEID);
  93. }
  94. /**
  95. * Let add tool id term
  96. */
  97. public function addToolId($tool_id)
  98. {
  99. $this->addTerm($tool_id, XAPIAN_PREFIX_TOOLID);
  100. }
  101. }