XunSearch.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace Qii\Library;
  3. /**
  4. * 迅搜插件
  5. * @link http://www.xunsearch.com/
  6. */
  7. _require(__DIR__ . '/Third/XS.php');
  8. class XunSearch
  9. {
  10. public $xsRoot, $project;
  11. public $charset = 'utf-8';
  12. private $_xs, $_scws;
  13. public function __construct($iniFile)
  14. {
  15. $this->_xs = new XS($iniFile);
  16. $this->_xs->setDefaultCharset($this->charset);
  17. return $this;
  18. }
  19. /**
  20. * Quickly add a new document (without checking key conflicts)
  21. * @param mixed $data XSDocument object or data array to be added
  22. */
  23. public function add($data)
  24. {
  25. $this->update($data, true);
  26. }
  27. /**
  28. * @param mixed $data XSDocument object or data array to be updated
  29. * @param boolean $add whether to add directly, default to false
  30. */
  31. public function update($data, $add = false)
  32. {
  33. if ($data instanceof XSDocument) {
  34. $this->_xs->index->update($data, $add);
  35. } else {
  36. $doc = new XSDocument($data);
  37. $this->_xs->index->update($doc, $add);
  38. }
  39. }
  40. /**
  41. * @return XSTokenizerScws get scws tokenizer
  42. */
  43. public function getScws()
  44. {
  45. if ($this->_scws === null) {
  46. $this->_scws = new XSTokenizerScws;
  47. }
  48. return $this->_scws;
  49. }
  50. public function __call($name, $parameters)
  51. {
  52. // check methods of xs
  53. if ($this->_xs !== null && method_exists($this->_xs, $name)) {
  54. return call_user_func_array(array($this->_xs, $name), $parameters);
  55. }
  56. // check methods of index object
  57. if ($this->_xs !== null && method_exists('XSIndex', $name)) {
  58. $ret = call_user_func_array(array($this->_xs->index, $name), $parameters);
  59. if ($ret === $this->_xs->index) {
  60. return $this;
  61. }
  62. return $ret;
  63. }
  64. // check methods of search object
  65. if ($this->_xs !== null && method_exists('XSSearch', $name)) {
  66. $ret = call_user_func_array(array($this->_xs->search, $name), $parameters);
  67. if ($ret === $this->_xs->search) {
  68. return $this;
  69. }
  70. return $ret;
  71. }
  72. throw new \Qii\Exceptions\CallUndefinedClass(\Qii::i('1105', $name), __LINE__);
  73. }
  74. }