EXunSearch.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * Yii-Xunsearch class file
  4. *
  5. * @author Hightman <hightman2[at]yahoo[dot]com[dot]cn>
  6. * @link http://www.xunsearch.com/
  7. * @version 1.0
  8. */
  9. /**
  10. * Xunsearch wrapper as an application component for YiiFramework
  11. *
  12. * @method XSIndex getIndex()
  13. * @method XSSearch getSearch()
  14. *
  15. * @author hightman
  16. * @version $Id$
  17. * @since 1.0
  18. */
  19. class EXunSearch extends CApplicationComponent
  20. {
  21. public $xsRoot, $project, $charset;
  22. private $_xs, $_scws;
  23. public function __call($name, $parameters)
  24. {
  25. // check methods of xs
  26. if ($this->_xs !== null && method_exists($this->_xs, $name)) {
  27. return call_user_func_array(array($this->_xs, $name), $parameters);
  28. }
  29. // check methods of index object
  30. if ($this->_xs !== null && method_exists('XSIndex', $name)) {
  31. $ret = call_user_func_array(array($this->_xs->index, $name), $parameters);
  32. if ($ret === $this->_xs->index) {
  33. return $this;
  34. }
  35. return $ret;
  36. }
  37. // check methods of search object
  38. if ($this->_xs !== null && method_exists('XSSearch', $name)) {
  39. $ret = call_user_func_array(array($this->_xs->search, $name), $parameters);
  40. if ($ret === $this->_xs->search) {
  41. return $this;
  42. }
  43. return $ret;
  44. }
  45. return parent::__call($name, $parameters);
  46. }
  47. public function init()
  48. {
  49. if ($this->xsRoot === null) {
  50. $lib = dirname(__FILE__) . '/../../lib/XS.class.php';
  51. } else {
  52. if (strpos($this->xsRoot, '.') !== false && strpos($this->xsRoot, DIRECTORY_SEPARATOR) === false) {
  53. $this->xsRoot = Yii::getPathOfAlias($this->xsRoot);
  54. }
  55. $lib = $this->xsRoot . '/' . (is_dir($this->xsRoot . '/sdk') ? '' : 'xunsearch-') . 'sdk/php/lib/XS.php';
  56. }
  57. if (!file_exists($lib)) {
  58. throw new CException('"XS.php" or "XS.class.php" not found, please check value of ' . __CLASS__ . '::$xsRoot');
  59. }
  60. if (($path = Yii::getPathOfAlias($this->project)) !== false) {
  61. $this->project = $path . '.ini';
  62. }
  63. require_once $lib;
  64. $this->_xs = new XS($this->project);
  65. $this->_xs->setDefaultCharset($this->charset);
  66. }
  67. /**
  68. * Quickly add a new document (without checking key conflicts)
  69. * @param mixed $data XSDocument object or data array to be added
  70. */
  71. public function add($data)
  72. {
  73. $this->update($data, true);
  74. }
  75. /**
  76. * @param mixed $data XSDocument object or data array to be updated
  77. * @param boolean $add whether to add directly, default to false
  78. */
  79. public function update($data, $add = false)
  80. {
  81. if ($data instanceof XSDocument) {
  82. $this->_xs->index->update($data, $add);
  83. } else {
  84. $doc = new XSDocument($data);
  85. $this->_xs->index->update($doc, $add);
  86. }
  87. }
  88. /**
  89. * @return XSTokenizerScws get scws tokenizer
  90. */
  91. public function getScws()
  92. {
  93. if ($this->_scws === null) {
  94. $this->_scws = new XSTokenizerScws;
  95. }
  96. return $this->_scws;
  97. }
  98. }