PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/ xvweb/core/libraries/SearchClass.XVWeb.class.php

http://xvweb.googlecode.com/
PHP | 148 lines | 122 code | 25 blank | 1 comment | 12 complexity | 8a373c02b242cd83f0283e9ae9858add MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, LGPL-3.0
  1. <?php
  2. class SearchClass
  3. {
  4. var $Date;
  5. public function __construct(&$Xvweb) {
  6. $this->Date['XVweb'] = &$Xvweb;
  7. $GLOBALS['Debug']['Classes'][] = array("ClassName"=>get_class(), "File"=>__FILE__, "Time"=>microtime(true), "MemoryUsage"=>memory_get_usage());
  8. }
  9. public function Search($String, $ActualPage = 0, $EveryPage =30){
  10. $LLimit = 0;
  11. $RLimit = $EveryPage;
  12. if(!is_numeric($LLimit) or !is_numeric($RLimit))
  13. return false;
  14. $match = '`AA`.{Articles:Topic} ,`AA`.{Articles:Contents} ';
  15. $OneVersion = 'AND `AA`.{Articles:Version} = `IA`.{ListArticles:ActualVersion}';
  16. if(!$this->Date['XVweb']->SearchInVersion)
  17. $OneVersion = '';
  18. $LLimit = ($ActualPage*$EveryPage);
  19. $SearchSQL = $this->Date['XVweb']->DataBase->prepare('SELECT SQL_CALC_FOUND_ROWS
  20. `AA`.{Articles:Version} as `Version`,
  21. '.($this->get("noContent") == true ? '' : '`AA`.{Articles:Contents} as `Contents`,' ).'
  22. `AA`.{Articles:AdressInSQL} as `AdressInSQL`,
  23. `AA`.{Articles:Topic} as `Topic`,
  24. MATCH('.$match.') AGAINST( :SearchExecute ) AS `Relevance`,
  25. IA.{ListArticles:URL} as `URL`
  26. FROM
  27. {Articles} AS `AA`
  28. INNER JOIN
  29. {ListArticles} AS `IA` ON (`AA`.{Articles:AdressInSQL} = `IA`.{ListArticles:AdressInSQL})
  30. WHERE
  31. MATCH ('.$match.') AGAINST (:SearchExecute)
  32. AND
  33. IA.{ListArticles:Accepted} = "yes"
  34. '.(is_numeric($this->get("SearchExcept")) ? 'AND IA.{ListArticles:ID} <> "'.$this->get("SearchExcept").'" ' : "").'
  35. '.$OneVersion.'
  36. '.($this->get("Group") == true ? 'GROUP BY `IA`.{ListArticles:AdressInSQL} ' : '' ).'
  37. ORDER BY `Relevance` DESC
  38. LIMIT '.$LLimit.' , '.$EveryPage.''
  39. );
  40. $SearchSQL->execute(array(
  41. ':SearchExecute' => $String
  42. ));
  43. $return = $SearchSQL->fetchAll();
  44. if($this->get("noContent") != true){
  45. foreach ($return as $key => $value) {
  46. $return[$key]['Lenght'] = strlen($value['Contents']);
  47. $return[$key]['StrByte'] = $return[$key]['Lenght']*8;
  48. $return[$key]['Contents'] = $this->SearchTextExtract($String, $value['Contents']);
  49. }
  50. }
  51. $this->Date['XVweb']->Date['SearchResultCount'] = $this->Date['XVweb']->DataBase->pquery('SELECT FOUND_ROWS() AS `SearchCount`;')->fetch(PDO::FETCH_OBJ)->SearchCount;
  52. return $return;
  53. }
  54. var $text_ary = array();
  55. function BlodSearch($content)
  56. {
  57. static $pattern_ary = array();
  58. static $replacement_ary = array();
  59. if ( !count($pattern_ary) )
  60. {
  61. foreach ( $this->text_ary as $index => $word )
  62. {
  63. $pattern_ary[] = "/\b" . preg_quote($this->text_ary[$index]) . "\b/i";
  64. $replacement_ary[] = '<span style="font-weight: bold;">\\0</span>';
  65. }
  66. }
  67. return (preg_replace($pattern_ary, $replacement_ary, $content));
  68. }
  69. public function set($var, $val){
  70. return ($this->Date[$var] = $val);
  71. }
  72. public function get($var){
  73. return isset($this->Date[$var]) ? $this->Date[$var] : null;
  74. }
  75. function SearchTextExtract($keyword, $content)
  76. {
  77. $drop_char_match = array('--', '\'', ',', '@', '\\',);
  78. $keyword = str_replace($drop_char_match, '', $keyword);
  79. preg_match_all('/(^| |\+|\-)"(.*?)"/', $keyword, $match_ary);
  80. foreach ( $match_ary[0] as $word_id => $word )
  81. {
  82. $this->text_ary = $word;
  83. $keyword = str_replace($word, '', $keyword);
  84. }
  85. $this->text_ary = array_merge($this->text_ary, array_unique(preg_split('#\s+#', $keyword)));
  86. for ( $i = 0; $i < count($this->text_ary); $i++ )
  87. {
  88. if ( strlen($this->text_ary[$i]) < 3 )
  89. {
  90. unset($this->text_ary[$i]);
  91. continue;
  92. }
  93. }
  94. if ( ($sizeof = sizeof($this->text_ary)) > 5 )
  95. {
  96. $sizeof = 5;
  97. }
  98. foreach ( $this->text_ary as $index => $word )
  99. {
  100. $this->text_ary[$index] = trim(str_replace(array('"', '+', '-'), '', $this->text_ary[$index]));
  101. }
  102. $word_countS = count($this->text_ary);
  103. $keyword = implode(' ', $this->text_ary);
  104. if ( !$keyword )
  105. {
  106. return array(substr(htmlspecialchars($content), 0, 255), 0);
  107. }
  108. $content = strip_tags($content);
  109. $word_count = 0;
  110. $output = '';
  111. foreach ( $this->text_ary as $index => $word )
  112. {
  113. if($this->text_ary[$index])
  114. $word_count += substr_count($content, $this->text_ary[$index]);
  115. $output .= ' ...'.substr($content, stripos($content, $this->text_ary[$index]), (255 / $word_countS)) . '... ';
  116. }
  117. //, $word_count)
  118. return $this->BlodSearch($output);
  119. }
  120. }
  121. ?>