Guest User

Untitled

a guest
Oct 22nd, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Model_Logic_Sphinx
  5. */
  6. class Model_Logic_Sphinx {
  7.  
  8. const INDEX_ARTICLES = 'articles';
  9.  
  10. protected $_oSphinx;
  11. protected $_aConfig;
  12.  
  13. /**
  14. *
  15. * @var Model_Logic_Sphinx
  16. */
  17. static private $_oInstance;
  18.  
  19. /**
  20. * Liczba wyników
  21. * @var int
  22. */
  23. protected $_iTotalFound = 0;
  24.  
  25. /**
  26. *
  27. * @return Model_Logic_Sphinx
  28. */
  29. public static function getInstance() {
  30. if (self::$_oInstance == null) {
  31. self::$_oInstance = new self();
  32. }
  33. return self::$_oInstance;
  34. }
  35.  
  36. /**
  37. * Construct method
  38. */
  39. public function __construct() {
  40.  
  41. $this->_aConfig =Model_Resource_Options::get('sphinx');
  42.  
  43. $this->_oSphinx = new Common_SphinxClient();
  44. $this->_oSphinx->SetServer($this->_aConfig['host'], $this->_aConfig['port']);
  45. $this->_oSphinx->SetMatchMode($this->_aConfig['mode']);
  46. $this->_oSphinx->SetArrayResult(true);
  47. $this->_oSphinx->SetLimits(0, 1000);
  48. }
  49.  
  50. /**
  51. *
  52. * @param string $sQuery
  53. * @param array $aFilters Filtering values $aFilter = array('{col}' => array('wartosci'))
  54. * @param array $aParams array with limit and offset
  55. * @return mixed
  56. */
  57. public function query($sQuery, $aFilters = array(), array $aParams = array()) {
  58. $this->_oSphinx->ResetFilters();
  59.  
  60. if (isset($aParams['limit']) && isset($aParams['offset'])) {
  61. $this->_oSphinx->SetLimits((int) $aParams['offset'], (int) $aParams['limit']);
  62. }
  63.  
  64. if (!empty($aFilters)) {
  65. foreach ($aFilters as $aAttrib => $aFilter) {
  66. $this->_oSphinx->SetFilter($aAttrib, $aFilter);
  67. }
  68. }
  69.  
  70. $sIndex = $aParams['index'];
  71. $aResult = $this->_oSphinx->Query($sQuery, $sIndex);
  72.  
  73. if ($aResult === false) {
  74. throw new Zend_Exception("Sphinx query failed: " . $this->_oSphinx->GetLastError());
  75. } else {
  76. if ($this->_oSphinx->GetLastWarning()) {
  77. throw new Zend_Exception("Sphinx warning: " . $this->_oSphinx->GetLastWarning());
  78. }
  79. }
  80.  
  81. $this->_iTotalFound = $aResult['total_found'];
  82.  
  83. $aIds = array();
  84. if (!empty($aResult['matches'])) {
  85. foreach ($aResult['matches'] as $aMatch) {
  86. $aIds[] = $aMatch['id'];
  87. }
  88. }
  89. return $aIds;
  90. }
  91.  
  92. public function getTotalFound() {
  93. return $this->_iTotalFound;
  94. }
  95.  
  96. }
Add Comment
Please, Sign In to add comment