IndexableChunk.class.php 2.6 KB

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