IndexableChunk.class.php 2.5 KB

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